Skip to content

Commit

Permalink
Rooch Gas Profiling Implementation
Browse files Browse the repository at this point in the history
enable the gas event recording

finish the gas profiling

convert linear gas event list to stacked

implement flamegraph for execution

cargo clippy

render html template

add the content of the execution trace

Put the gas profiling in moveos repo

implement ClassifiedGasMeter and SwitchableGasMeter for GasProfiler

cargo fmt

add function execute_tx_locally

add an command line argument for getStates command

integrate gas profiler into the tx execution flow
  • Loading branch information
steelgeek091 committed Sep 23, 2024
1 parent 3ed1227 commit 1dd7886
Show file tree
Hide file tree
Showing 44 changed files with 2,072 additions and 127 deletions.
54 changes: 53 additions & 1 deletion Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"moveos/moveos-commons/bcs_ext",
"moveos/moveos-commons/moveos-common",
"moveos/moveos-commons/timeout-join-handler",
"moveos/moveos-commons/moveos-common",
"moveos/moveos-compiler",
"moveos/moveos-config",
"moveos/moveos-object-runtime",
Expand All @@ -18,6 +19,7 @@ members = [
"moveos/raw-store",
"moveos/smt",
"moveos/moveos-eventbus",
"moveos/moveos-gas-profiling",
"crates/data_verify",
"crates/rooch",
"crates/rooch-benchmarks",
Expand Down Expand Up @@ -103,6 +105,7 @@ moveos-object-runtime = { path = "moveos/moveos-object-runtime" }
moveos-compiler = { path = "moveos/moveos-compiler" }
moveos-eventbus = { path = "moveos/moveos-eventbus" }
accumulator = { path = "moveos/moveos-commons/accumulator" }
moveos-gas-profiling = { path = "moveos/moveos-gas-profiling" }

# crates for Rooch
rooch = { path = "crates/rooch" }
Expand Down Expand Up @@ -347,6 +350,9 @@ vergen-git2 = { version = "1.0.0", features = ["build", "cargo", "rustc"] }
vergen-pretty = "0.3.5"
crossbeam-channel = "0.5.13"

inferno = "0.11.14"
handlebars = "4.2.2"

# Note: the BEGIN and END comments below are required for external tooling. Do not remove.
# BEGIN MOVE DEPENDENCIES
move-abigen = { git = "https://github.com/rooch-network/move", rev = "c05b5f71a24beb498eb743d73be4f69d8d325e62" }
Expand Down
1 change: 1 addition & 0 deletions crates/rooch-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ rooch-types = { workspace = true }
rooch-genesis = { workspace = true }
rooch-event = { workspace = true }
rooch-store = { workspace = true }
hex = "0.4.3"
2 changes: 2 additions & 0 deletions crates/rooch-executor/src/actor/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use moveos_types::moveos_std::event::{AnnotatedEvent, Event, EventID};
use moveos_types::moveos_std::object::ObjectMeta;
use moveos_types::state::{AnnotatedState, FieldKey, ObjectState, StateChangeSetExt};
use moveos_types::state_resolver::{AnnotatedStateKV, StateKV};
use moveos_types::state_root_hash::StateRootHash;
use moveos_types::transaction::TransactionExecutionInfo;
use moveos_types::transaction::TransactionOutput;
use moveos_types::transaction::VerifiedMoveOSTransaction;
Expand Down Expand Up @@ -99,6 +100,7 @@ impl Message for ExecuteViewFunctionMessage {

#[derive(Debug, Serialize, Deserialize)]
pub struct StatesMessage {
pub state_root: StateRootHash,
pub access_path: AccessPath,
}

Expand Down
10 changes: 9 additions & 1 deletion crates/rooch-executor/src/actor/reader_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use moveos_store::transaction_store::TransactionStore;
use moveos_store::MoveOSStore;
use moveos_types::function_return_value::AnnotatedFunctionResult;
use moveos_types::function_return_value::AnnotatedFunctionReturnValue;
use moveos_types::h256::H256;
use moveos_types::moveos_std::event::EventHandle;
use moveos_types::moveos_std::event::{AnnotatedEvent, Event};
use moveos_types::moveos_std::object::ObjectMeta;
Expand Down Expand Up @@ -146,7 +147,14 @@ impl Handler<StatesMessage> for ReaderExecutorActor {
msg: StatesMessage,
_ctx: &mut ActorContext,
) -> Result<Vec<Option<ObjectState>>, anyhow::Error> {
let resolver = RootObjectResolver::new(self.root.clone(), &self.moveos_store);
let resolver = if !msg.state_root.is_empty() {
let hex_bytes = hex::decode(msg.state_root.0).expect("decode root state failed");
let state_root = H256::from_slice(hex_bytes.as_slice());
let root_object_meta = ObjectMeta::root_metadata(state_root, 55);
RootObjectResolver::new(root_object_meta, &self.moveos_store)
} else {
RootObjectResolver::new(self.root.clone(), &self.moveos_store)
};
resolver.get_states(msg.access_path)
}
}
Expand Down
51 changes: 34 additions & 17 deletions crates/rooch-executor/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use moveos_types::moveos_std::object::ObjectMeta;
use moveos_types::moveos_std::tx_context::TxContext;
use moveos_types::state::{FieldKey, StateChangeSetExt};
use moveos_types::state_resolver::{AnnotatedStateKV, StateKV};
use moveos_types::state_root_hash::StateRootHash;
use moveos_types::transaction::FunctionCall;
use moveos_types::transaction::TransactionExecutionInfo;
use moveos_types::transaction::TransactionOutput;
Expand Down Expand Up @@ -116,9 +117,16 @@ impl ExecutorProxy {
.await?
}

pub async fn get_states(&self, access_path: AccessPath) -> Result<Vec<Option<ObjectState>>> {
pub async fn get_states(
&self,
access_path: AccessPath,
state_root: StateRootHash,
) -> Result<Vec<Option<ObjectState>>> {
self.reader_actor
.send(StatesMessage { access_path })
.send(StatesMessage {
state_root,
access_path,
})
.await?
}

Expand Down Expand Up @@ -261,29 +269,38 @@ impl ExecutorProxy {
}

pub async fn chain_id(&self) -> Result<ChainID> {
self.get_states(AccessPath::object(ChainID::chain_id_object_id()))
.await?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("chain id not found"))
.and_then(|state| state.ok_or_else(|| anyhow::anyhow!("chain id not found")))
.and_then(|state| Ok(state.into_object::<ChainID>()?.value))
self.get_states(
AccessPath::object(ChainID::chain_id_object_id()),
StateRootHash::empty(),
)
.await?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("chain id not found"))
.and_then(|state| state.ok_or_else(|| anyhow::anyhow!("chain id not found")))
.and_then(|state| Ok(state.into_object::<ChainID>()?.value))
}

pub async fn bitcoin_network(&self) -> Result<BitcoinNetwork> {
self.get_states(AccessPath::object(BitcoinNetwork::object_id()))
.await?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("bitcoin network not found"))
.and_then(|state| state.ok_or_else(|| anyhow::anyhow!("bitcoin network not found")))
.and_then(|state| Ok(state.into_object::<BitcoinNetwork>()?.value))
self.get_states(
AccessPath::object(BitcoinNetwork::object_id()),
StateRootHash::empty(),
)
.await?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("bitcoin network not found"))
.and_then(|state| state.ok_or_else(|| anyhow::anyhow!("bitcoin network not found")))
.and_then(|state| Ok(state.into_object::<BitcoinNetwork>()?.value))
}

//TODO provide a trait to abstract the async state reader, elemiate the duplicated code bwteen RpcService and Client
pub async fn get_sequence_number(&self, address: AccountAddress) -> Result<u64> {
Ok(self
.get_states(AccessPath::object(Account::account_object_id(address)))
.get_states(
AccessPath::object(Account::account_object_id(address)),
StateRootHash::empty(),
)
.await?
.pop()
.flatten()
Expand Down
10 changes: 10 additions & 0 deletions crates/rooch-open-rpc-spec/schemas/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@
"$ref": "#/components/schemas/moveos_types::access_path::AccessPath"
}
},
{
"name": "state_root",
"required": true,
"schema": {
"$ref": "#/components/schemas/moveos_types::state_root_hash::StateRootHash"
}
},
{
"name": "state_option",
"schema": {
Expand Down Expand Up @@ -3402,6 +3409,9 @@
"moveos_types::state::FieldKey": {
"type": "string"
},
"moveos_types::state_root_hash::StateRootHash": {
"type": "string"
},
"primitive_types::H256": {
"type": "string"
},
Expand Down
13 changes: 10 additions & 3 deletions crates/rooch-rpc-api/src/api/rooch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ use crate::jsonrpc_types::{
FieldKeyView, FunctionCallView, H256View, IndexerEventPageView, IndexerObjectStatePageView,
IndexerStateIDView, ModuleABIView, ObjectIDVecView, ObjectIDView, ObjectStateFilterView,
ObjectStateView, QueryOptions, RoochAddressView, StateChangeSetPageView, StateOptions,
StatePageView, StrView, StructTagView, SyncStateFilterView, TransactionWithInfoPageView,
TxOptions,
StatePageView, StateRootHashView, StrView, StructTagView, SyncStateFilterView,
TransactionWithInfoPageView, TxOptions,
};
use crate::RpcResult;
use jsonrpsee::proc_macros::rpc;
use moveos_types::state_root_hash::StateRootHash;
use moveos_types::{access_path::AccessPath, state::FieldKey};
use rooch_open_rpc_macros::open_rpc;

Expand Down Expand Up @@ -58,6 +59,7 @@ pub trait RoochAPI {
async fn get_states(
&self,
access_path: AccessPathView,
state_root: StateRootHashView,
state_option: Option<StateOptions>,
) -> RpcResult<Vec<Option<ObjectStateView>>>;

Expand Down Expand Up @@ -91,7 +93,12 @@ pub trait RoochAPI {
let key_states = field_key.into_iter().map(FieldKey::from).collect();
let access_path_view =
AccessPathView::from(AccessPath::fields(object_id.into(), key_states));
self.get_states(access_path_view, state_option).await
self.get_states(
access_path_view,
StrView::from(StateRootHash::empty()),
state_option,
)
.await
}

/// List Object Fields via ObjectID.
Expand Down
17 changes: 17 additions & 0 deletions crates/rooch-rpc-api/src/jsonrpc_types/str_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use move_core_types::account_address::AccountAddress;
use moveos_types::move_std::string::MoveString;
use moveos_types::state_root_hash::StateRootHash;
use schemars::gen::SchemaGenerator;
use schemars::schema::{InstanceType, Schema, SchemaObject};
use schemars::JsonSchema;
Expand Down Expand Up @@ -325,3 +326,19 @@ where
self.0.to_human_readable_string(verbose, indent)
}
}

pub type StateRootHashView = StrView<StateRootHash>;

impl FromStr for StateRootHashView {
type Err = anyhow::Error;

fn from_str(s: &str) -> anyhow::Result<Self, Self::Err> {
Ok(Self(StateRootHash(s.to_string())))
}
}

impl std::fmt::Display for StateRootHashView {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.0)
}
}
Loading

0 comments on commit 1dd7886

Please sign in to comment.