Skip to content

Commit

Permalink
[Indexer] [break change] Split Object states and perf Indexer (#2490)
Browse files Browse the repository at this point in the history
* draft split indexer object states

* finish split indexer object states

* resolve repair and rebuild tools

* clean unused code

* Unified inscription_id query to {txid}i{index}

* redesign events indexer and support decode

* optmize transaction indexer by reduce field and index with order by field

* fixup test cases

* cleanup debug logs

* cheanup

* merge and fixup bench and db tools

* perf index for indexer

* fixup object cli

* fixup object cli

* remain gas used and tx status in tx indexer

* revert bitcoin test sleep changes
  • Loading branch information
baichuan3 authored Aug 26, 2024
1 parent b11ee0b commit cc96287
Show file tree
Hide file tree
Showing 58 changed files with 1,648 additions and 676 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions crates/rooch-benchmarks/benches/bench_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rooch_framework_tests::binding_test;
use rooch_indexer::indexer_reader::IndexerReader;
use rooch_indexer::store::traits::IndexerStoreTrait;
use rooch_indexer::IndexerStore;
use rooch_types::indexer::state::{IndexerObjectState, ObjectStateFilter};
use rooch_types::indexer::state::{IndexerObjectState, ObjectStateFilter, ObjectStateType};
use std::cell::RefCell;
use tokio::runtime::Runtime;

Expand Down Expand Up @@ -74,11 +74,12 @@ fn bench_read_with_reader(

rt.borrow().block_on(async {
let result1 = indexer_reader
.query_object_states_with_filter(
.query_object_ids_with_filter(
object_state_filter1,
None,
50,
true,
ObjectStateType::ObjectState,
)
.unwrap();
assert!(!result1.is_empty());
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl RoochDB {
let previous_tx_hash_opt = self
.rooch_store
.transaction_store
.get_tx_hashs(vec![previous_tx_order])?;
.get_tx_hashes(vec![previous_tx_order])?;
if previous_tx_hash_opt.is_empty() || previous_tx_hash_opt[0].is_none() {
return Err(Error::msg(format!(
"the previous tx hash not exist via previous_tx_order {}",
Expand Down
11 changes: 10 additions & 1 deletion crates/rooch-executor/src/actor/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,22 @@ impl Message for GetEventsByEventHandleMessage {
type Result = Result<Vec<Event>>;
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetAnnotatedEventsByEventIDsMessage {
pub event_ids: Vec<EventID>,
}

impl Message for GetAnnotatedEventsByEventIDsMessage {
type Result = Result<Vec<Option<AnnotatedEvent>>>;
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetEventsByEventIDsMessage {
pub event_ids: Vec<EventID>,
}

impl Message for GetEventsByEventIDsMessage {
type Result = Result<Vec<Option<AnnotatedEvent>>>;
type Result = Result<Vec<Option<Event>>>;
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
22 changes: 18 additions & 4 deletions crates/rooch-executor/src/actor/reader_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

use super::messages::{
AnnotatedStatesMessage, ExecuteViewFunctionMessage, GetAnnotatedEventsByEventHandleMessage,
GetEventsByEventHandleMessage, RefreshStateMessage, StatesMessage,
GetAnnotatedEventsByEventIDsMessage, GetEventsByEventHandleMessage, RefreshStateMessage,
StatesMessage,
};
use crate::actor::messages::{
GetEventsByEventIDsMessage, GetTxExecutionInfosByHashMessage, ListAnnotatedStatesMessage,
Expand Down Expand Up @@ -243,13 +244,13 @@ impl Handler<GetEventsByEventHandleMessage> for ReaderExecutorActor {
}

#[async_trait]
impl Handler<GetEventsByEventIDsMessage> for ReaderExecutorActor {
impl Handler<GetAnnotatedEventsByEventIDsMessage> for ReaderExecutorActor {
async fn handle(
&mut self,
msg: GetEventsByEventIDsMessage,
msg: GetAnnotatedEventsByEventIDsMessage,
_ctx: &mut ActorContext,
) -> Result<Vec<Option<AnnotatedEvent>>> {
let GetEventsByEventIDsMessage { event_ids } = msg;
let GetAnnotatedEventsByEventIDsMessage { event_ids } = msg;
let event_store = self.moveos().event_store();
let resolver = RootObjectResolver::new(self.root.clone(), &self.moveos_store);
event_store
Expand All @@ -267,6 +268,19 @@ impl Handler<GetEventsByEventIDsMessage> for ReaderExecutorActor {
}
}

#[async_trait]
impl Handler<GetEventsByEventIDsMessage> for ReaderExecutorActor {
async fn handle(
&mut self,
msg: GetEventsByEventIDsMessage,
_ctx: &mut ActorContext,
) -> Result<Vec<Option<Event>>> {
let GetEventsByEventIDsMessage { event_ids } = msg;
let event_store = self.moveos().event_store();
event_store.multi_get_events(event_ids)
}
}

#[async_trait]
impl Handler<GetTxExecutionInfosByHashMessage> for ReaderExecutorActor {
async fn handle(
Expand Down
18 changes: 14 additions & 4 deletions crates/rooch-executor/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

use crate::actor::messages::{
ConvertL2TransactionData, DryRunTransactionResult, GetEventsByEventHandleMessage,
GetEventsByEventIDsMessage, GetTxExecutionInfosByHashMessage, ListAnnotatedStatesMessage,
ListStatesMessage, RefreshStateMessage, ValidateL1BlockMessage, ValidateL1TxMessage,
ConvertL2TransactionData, DryRunTransactionResult, GetAnnotatedEventsByEventIDsMessage,
GetEventsByEventHandleMessage, GetEventsByEventIDsMessage, GetTxExecutionInfosByHashMessage,
ListAnnotatedStatesMessage, ListStatesMessage, RefreshStateMessage, ValidateL1BlockMessage,
ValidateL1TxMessage,
};
use crate::actor::reader_executor::ReaderExecutorActor;
use crate::actor::{
Expand Down Expand Up @@ -194,10 +195,19 @@ impl ExecutorProxy {
.await?
}

pub async fn get_events_by_event_ids(
pub async fn get_annotated_events_by_event_ids(
&self,
event_ids: Vec<EventID>,
) -> Result<Vec<Option<AnnotatedEvent>>> {
self.reader_actor
.send(GetAnnotatedEventsByEventIDsMessage { event_ids })
.await?
}

pub async fn get_events_by_event_ids(
&self,
event_ids: Vec<EventID>,
) -> Result<Vec<Option<Event>>> {
self.reader_actor
.send(GetEventsByEventIDsMessage { event_ids })
.await?
Expand Down
20 changes: 11 additions & 9 deletions crates/rooch-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ use rooch_types::address::BitcoinAddress;
use rooch_types::bitcoin::genesis::BitcoinGenesisContext;
use rooch_types::error::GenesisError;
use rooch_types::indexer::event::IndexerEvent;
use rooch_types::indexer::state::{handle_object_change, IndexerObjectStateChanges};
use rooch_types::indexer::state::{
handle_object_change, IndexerObjectStateChangeSet, IndexerObjectStatesIndexGenerator,
};
use rooch_types::indexer::transaction::IndexerTransaction;
use rooch_types::into_address::IntoAddress;
use rooch_types::rooch_network::{BuiltinChainID, RoochNetwork};
Expand Down Expand Up @@ -445,22 +447,22 @@ impl RoochGenesis {
.collect();
rooch_db.indexer_store.persist_events(events)?;

// 3. update indexer state
// indexer state index generator
let mut state_index_generator = 0u64;
let mut indexer_object_state_changes = IndexerObjectStateChanges::default();
// 3. update indexer full object state, including object_states, utxos and inscriptions
// indexer object state index generator
let mut state_index_generator = IndexerObjectStatesIndexGenerator::default();
let mut indexer_object_state_change_set = IndexerObjectStateChangeSet::default();

for (_field_key, object_change) in genesis_tx_output.changeset.changes {
state_index_generator = handle_object_change(
state_index_generator,
handle_object_change(
&mut state_index_generator,
genesis_tx_order,
&mut indexer_object_state_changes,
&mut indexer_object_state_change_set,
object_change,
)?;
}
rooch_db
.indexer_store
.update_object_states(indexer_object_state_changes)?;
.apply_object_states(indexer_object_state_change_set)?;

let genesis_info =
GenesisInfo::new(self.genesis_hash(), inited_root.clone(), self.encode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ CREATE TABLE transactions (
sender VARCHAR NOT NULL,
action_type SMALLINT NOT NULL,
auth_validator_id BIGINT NOT NULL,
authenticator_payload BLOB NOT NULL,
tx_accumulator_root VARCHAR NOT NULL,

state_root VARCHAR NOT NULL,
size BIGINT NOT NULL,
event_root VARCHAR NOT NULL,
gas_used BIGINT NOT NULL,
status VARCHAR NOT NULL,

created_at BIGINT NOT NULL,
UNIQUE (tx_hash)
);

CREATE INDEX idx_transactions_sender ON transactions (sender);
CREATE INDEX idx_transactions_created_at ON transactions (created_at);
CREATE INDEX idx_transactions_sender ON transactions (sender, tx_order);
--CREATE INDEX idx_transactions_tx_hash ON transactions (tx_hash, tx_order);
CREATE INDEX idx_transactions_created_at ON transactions (created_at, tx_order);
12 changes: 7 additions & 5 deletions crates/rooch-indexer/migrations/2023-10-31-085230_events/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ CREATE TABLE events
event_handle_id VARCHAR NOT NULL,
event_seq BIGINT NOT NULL,
event_type VARCHAR NOT NULL,
event_data BLOB NOT NULL,
event_index BIGINT NOT NULL,

tx_hash VARCHAR NOT NULL,
Expand All @@ -16,7 +15,10 @@ CREATE TABLE events
);


CREATE INDEX idx_events_tx_hash ON events (tx_hash);
CREATE INDEX idx_events_tx_order ON events (tx_order);
CREATE INDEX idx_events_sender ON events (sender);
CREATE INDEX idx_events_created_at ON events (created_at);
CREATE INDEX idx_events_sender_and_event_type ON events (sender, event_type, tx_order, event_index);
CREATE INDEX idx_events_event_type ON events (event_type, tx_order, event_index);
CREATE INDEX idx_events_sender_and_event_handle ON events (sender, event_handle_id, tx_order, event_index);
CREATE INDEX idx_events_event_handle ON events (event_handle_id, tx_order, event_index);
CREATE INDEX idx_events_sender ON events (sender, tx_order, event_index);
CREATE INDEX idx_events_tx_hash ON events (tx_hash, tx_order, event_index);
CREATE INDEX idx_events_created_at ON events (created_at, tx_order, event_index);
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
CREATE TABLE object_states
(
id VARCHAR NOT NULL PRIMARY KEY,
id VARCHAR NOT NULL PRIMARY KEY,
owner VARCHAR NOT NULL,
flag SMALLINT NOT NULL,
state_root VARCHAR NOT NULL,
size BIGINT NOT NULL,
object_type VARCHAR NOT NULL,
tx_order BIGINT NOT NULL,
state_index BIGINT NOT NULL,
Expand All @@ -13,8 +10,7 @@ CREATE TABLE object_states
UNIQUE (tx_order, state_index)
);

CREATE INDEX idx_object_states_owner ON object_states (owner);
CREATE INDEX idx_object_states_object_type_and_owner ON object_states (object_type, owner);
--CREATE INDEX idx_object_states_object_type ON object_states (object_type);
--CREATE INDEX idx_object_states_owner_and_object_type ON object_states (owner, object_type);
CREATE INDEX idx_object_states_created_at ON object_states (created_at);
CREATE INDEX idx_object_states_owner_and_object_type ON object_states (owner, object_type, tx_order, state_index);
CREATE INDEX idx_object_states_object_type ON object_states (object_type, tx_order, state_index);
CREATE INDEX idx_object_states_owner ON object_states (owner, tx_order, state_index);
CREATE INDEX idx_object_states_updated_at ON object_states (updated_at, tx_order, state_index);

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS utxos;
13 changes: 13 additions & 0 deletions crates/rooch-indexer/migrations/2024-08-19-113033_utxos/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE utxos
(
id VARCHAR NOT NULL PRIMARY KEY,
owner VARCHAR NOT NULL,
tx_order BIGINT NOT NULL,
state_index BIGINT NOT NULL,
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
UNIQUE (tx_order, state_index)
);

CREATE INDEX idx_object_state_utxos_owner ON utxos (owner, tx_order, state_index);
CREATE INDEX idx_object_state_utxos_updated_at ON utxos (updated_at, tx_order, state_index);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS inscriptions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE inscriptions
(
id VARCHAR NOT NULL PRIMARY KEY,
owner VARCHAR NOT NULL,
tx_order BIGINT NOT NULL,
state_index BIGINT NOT NULL,
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
UNIQUE (tx_order, state_index)
);

CREATE INDEX idx_object_state_inscriptions_owner ON inscriptions (owner, tx_order, state_index);
CREATE INDEX idx_object_state_inscriptions_updated_at ON inscriptions (updated_at, tx_order, state_index);
Loading

0 comments on commit cc96287

Please sign in to comment.