From 024cb0a59b2915694035e211d96a71d31670ac6e Mon Sep 17 00:00:00 2001 From: jolestar Date: Tue, 11 Jun 2024 22:24:59 +0800 Subject: [PATCH] [bench] Fix and refactor the bench --- .../benches/bench_tx_sequence.rs | 18 ++---- .../benches/bench_tx_validate.rs | 18 +++--- crates/rooch-benchmarks/src/tx.rs | 14 +---- crates/rooch-benchmarks/src/tx_exec.rs | 57 +++++++++---------- .../rooch-framework-tests/src/binding_test.rs | 13 ++++- .../rooch-test-transaction-builder/src/lib.rs | 16 +++++- .../src/transaction/authenticator.rs | 7 +++ crates/rooch-types/src/transaction/rooch.rs | 8 ++- moveos/moveos-types/src/state_resolver.rs | 13 ++++- 9 files changed, 92 insertions(+), 72 deletions(-) diff --git a/crates/rooch-benchmarks/benches/bench_tx_sequence.rs b/crates/rooch-benchmarks/benches/bench_tx_sequence.rs index cef36e3fda..3f09a0e684 100644 --- a/crates/rooch-benchmarks/benches/bench_tx_sequence.rs +++ b/crates/rooch-benchmarks/benches/bench_tx_sequence.rs @@ -1,40 +1,32 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -use std::time::Duration; - use criterion::{criterion_group, criterion_main, Criterion}; - use rooch_benchmarks::config::{configure_criterion, BenchTxConfig}; use rooch_benchmarks::tx::{create_l2_tx, gen_sequencer}; use rooch_framework_tests::binding_test; -use rooch_key::keystore::account_keystore::AccountKeystore; -use rooch_key::keystore::memory_keystore::InMemKeystore; use rooch_test_transaction_builder::TestTransactionBuilder; use rooch_types::transaction::LedgerTxData; +use std::time::Duration; pub fn tx_sequence_benchmark(c: &mut Criterion) { let config = BenchTxConfig::load(); let binding_test = binding_test::RustBindingTest::new().unwrap(); - let keystore = InMemKeystore::new_insecure_for_tests(10); - let rooch_account = keystore.addresses()[0]; - let rooch_key_pair = keystore - .get_key_pair(&rooch_account, None) - .expect("key pair should have value"); + let rooch_key_pair = binding_test.sequencer_kp().copy(); + let sequencer_keypair = rooch_key_pair.copy(); let mut sequencer = gen_sequencer(sequencer_keypair, binding_test.executor().get_rooch_store()).unwrap(); let tx_type = config.tx_type.unwrap().clone(); - let mut test_transaction_builder = TestTransactionBuilder::new(rooch_account.into()); + let mut test_transaction_builder = TestTransactionBuilder::new(rooch_key_pair.copy()); let tx_cnt = 600; let transactions: Vec<_> = (0..tx_cnt) .map(|n| { - let tx = - create_l2_tx(&mut test_transaction_builder, &keystore, n, tx_type.clone()).unwrap(); + let tx = create_l2_tx(&mut test_transaction_builder, n, tx_type.clone()).unwrap(); LedgerTxData::L2Tx(tx.clone()) }) .collect(); diff --git a/crates/rooch-benchmarks/benches/bench_tx_validate.rs b/crates/rooch-benchmarks/benches/bench_tx_validate.rs index 06884d7e6e..e61432e67a 100644 --- a/crates/rooch-benchmarks/benches/bench_tx_validate.rs +++ b/crates/rooch-benchmarks/benches/bench_tx_validate.rs @@ -1,32 +1,30 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -use std::time::Duration; - use criterion::{criterion_group, criterion_main, Criterion}; - use rooch_benchmarks::config::{configure_criterion, BenchTxConfig}; use rooch_benchmarks::tx::create_l2_tx; use rooch_framework_tests::binding_test; -use rooch_key::keystore::account_keystore::AccountKeystore; -use rooch_key::keystore::memory_keystore::InMemKeystore; use rooch_test_transaction_builder::TestTransactionBuilder; +use rooch_types::crypto::RoochKeyPair; +use std::time::Duration; pub fn tx_validate_benchmark(c: &mut Criterion) { let config = BenchTxConfig::load(); let binding_test = binding_test::RustBindingTest::new().unwrap(); - let keystore = InMemKeystore::new_insecure_for_tests(10); - let default_account = keystore.addresses()[0]; - let mut test_transaction_builder = TestTransactionBuilder::new(default_account.into()); + let rooch_key_pair = RoochKeyPair::generate_secp256k1(); + + let mut test_transaction_builder = TestTransactionBuilder::new(rooch_key_pair); let tx_type = config.tx_type.unwrap().clone(); let tx_cnt = 600; let transactions: Vec<_> = (0..tx_cnt) - .map(|n| { - create_l2_tx(&mut test_transaction_builder, &keystore, n, tx_type.clone()).unwrap() + .map(|_n| { + //Because the validate function do not increase the sequence number, we use the sequence number 0 for all transactions + create_l2_tx(&mut test_transaction_builder, 0, tx_type.clone()).unwrap() }) .collect(); let mut transactions_iter = transactions.into_iter().cycle(); diff --git a/crates/rooch-benchmarks/src/tx.rs b/crates/rooch-benchmarks/src/tx.rs index f6f80f50f1..cbcc626005 100644 --- a/crates/rooch-benchmarks/src/tx.rs +++ b/crates/rooch-benchmarks/src/tx.rs @@ -7,8 +7,6 @@ use bitcoin::hashes::Hash; use bitcoin::hex::FromHex; use bitcoincore_rpc_json::bitcoin; use bitcoincore_rpc_json::bitcoin::Block; -use rooch_key::keystore::account_keystore::AccountKeystore; -use rooch_key::keystore::memory_keystore::InMemKeystore; use rooch_sequencer::actor::sequencer::SequencerActor; use rooch_store::RoochStore; use rooch_test_transaction_builder::TestTransactionBuilder; @@ -30,21 +28,16 @@ pub fn gen_sequencer(keypair: RoochKeyPair, rooch_store: RoochStore) -> Result Result { let publish_action = test_transaction_builder.new_publish_examples( EXAMPLE_SIMPLE_BLOG_PACKAGE_NAME, Some(EXAMPLE_SIMPLE_BLOG_NAMED_ADDRESS.to_string()), )?; - let tx_data = test_transaction_builder.build(publish_action); - let rooch_tx = - keystore.sign_transaction(&test_transaction_builder.sender.into(), tx_data, None)?; - Ok(rooch_tx) + test_transaction_builder.build_and_sign(publish_action) } pub fn create_l2_tx( test_transaction_builder: &mut TestTransactionBuilder, - keystore: &InMemKeystore, seq_num: u64, tx_type: TxType, ) -> Result { @@ -56,10 +49,7 @@ pub fn create_l2_tx( _ => panic!("Unsupported tx type"), }; - let tx_data = test_transaction_builder.build(action); - let rooch_tx = - keystore.sign_transaction(&test_transaction_builder.sender.into(), tx_data, None)?; - Ok(rooch_tx) + test_transaction_builder.build_and_sign(action) } pub fn find_block_height(dir: String) -> Result> { diff --git a/crates/rooch-benchmarks/src/tx_exec.rs b/crates/rooch-benchmarks/src/tx_exec.rs index 2a6599ea16..ada48ac68b 100644 --- a/crates/rooch-benchmarks/src/tx_exec.rs +++ b/crates/rooch-benchmarks/src/tx_exec.rs @@ -2,11 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 use criterion::{Criterion, SamplingMode}; - use rooch_framework_tests::binding_test; -use rooch_key::keystore::account_keystore::AccountKeystore; -use rooch_key::keystore::memory_keystore::InMemKeystore; use rooch_test_transaction_builder::TestTransactionBuilder; +use rooch_types::crypto::RoochKeyPair; +use rooch_types::transaction::LedgerTxData; +use std::collections::HashMap; use crate::config::BenchTxConfig; use crate::config::TxType::{BtcBlock, Empty, Transfer}; @@ -17,10 +17,8 @@ pub fn tx_exec_benchmark(c: &mut Criterion) { let config = BenchTxConfig::load(); let mut binding_test = binding_test::RustBindingTest::new().unwrap(); - let keystore = InMemKeystore::new_insecure_for_tests(10); - let default_account = keystore.addresses()[0]; - let kp = keystore.get_key_pair(&default_account, None).unwrap(); - let mut test_transaction_builder = TestTransactionBuilder::new(default_account.into()); + let kp = RoochKeyPair::generate_secp256k1(); + let mut test_transaction_builder = TestTransactionBuilder::new(kp); let tx_type = config.tx_type.unwrap(); let (bench_id, tx_cnt) = match tx_type { @@ -28,18 +26,13 @@ pub fn tx_exec_benchmark(c: &mut Criterion) { Transfer => ("l2_tx_transfer", 800), Empty => ("l2_tx_empty", 1000), }; - + let mut blocks = HashMap::new(); let mut transactions: Vec<_> = Vec::with_capacity(tx_cnt); if tx_type != BtcBlock { for n in 0..tx_cnt { - let tx = create_l2_tx( - &mut test_transaction_builder, - &keystore, - n as u64, - tx_type.clone(), - ) - .unwrap(); - transactions.push(binding_test.executor.validate_l2_tx(tx.clone()).unwrap()); + let tx = + create_l2_tx(&mut test_transaction_builder, n as u64, tx_type.clone()).unwrap(); + transactions.push(LedgerTxData::L2Tx(tx)); } } else { let btc_blk_dir = config.btc_block_dir.clone().unwrap(); @@ -52,28 +45,34 @@ pub fn tx_exec_benchmark(c: &mut Criterion) { let filename = format!("{}.hex", height); let file_path = [btc_blk_dir.clone(), "/".parse().unwrap(), filename].concat(); let l1_block = create_btc_blk_tx(height, file_path).unwrap(); - let ctx = binding_test.create_bt_blk_tx_ctx(cnt as u64, l1_block.clone()); - let move_tx = binding_test - .executor - .validate_l1_block( - ctx, - l1_block.clone(), - kp.public().bitcoin_address().unwrap(), - ) - .unwrap(); - transactions.push(move_tx); + transactions.push(LedgerTxData::L1Block(l1_block.block.clone())); + blocks.insert(l1_block.block.block_height, l1_block); } } - let mut transactions_iter = transactions.into_iter().cycle(); + let mut transactions_iter = transactions.into_iter(); let mut group = c.benchmark_group("bench_tx_exec"); group.sample_size(tx_cnt); group.sampling_mode(SamplingMode::Flat); group.bench_function(bench_id, |b| { b.iter(|| { - let tx = transactions_iter.next().unwrap(); - binding_test.execute_verified_tx(tx.clone()).unwrap() + let tx = match transactions_iter.next() { + Some(tx) => tx, + None => { + //TODO we can not use `transactions.into_iter().cycle()` to repeat the txs, because the sequence number can not be repeated + return; + } + }; + match tx { + LedgerTxData::L1Block(l1_block) => { + let l1_block_with_body = blocks.get(&l1_block.block_height).unwrap(); + binding_test + .execute_l1_block(l1_block_with_body.clone()) + .unwrap() + } + LedgerTxData::L2Tx(tx) => binding_test.execute(tx).unwrap(), + } }); }); group.finish(); diff --git a/crates/rooch-framework-tests/src/binding_test.rs b/crates/rooch-framework-tests/src/binding_test.rs index 8c1cd5f366..1632ed27bf 100644 --- a/crates/rooch-framework-tests/src/binding_test.rs +++ b/crates/rooch-framework-tests/src/binding_test.rs @@ -11,7 +11,7 @@ use moveos_types::module_binding::MoveFunctionCaller; use moveos_types::moveos_std::gas_schedule::GasScheduleConfig; use moveos_types::moveos_std::object::{ObjectEntity, RootObjectEntity}; use moveos_types::moveos_std::tx_context::TxContext; -use moveos_types::state_resolver::RootObjectResolver; +use moveos_types::state_resolver::{RootObjectResolver, StateReaderExt}; use moveos_types::transaction::{FunctionCall, VerifiedMoveOSTransaction}; use rooch_config::RoochOpt; use rooch_db::RoochDB; @@ -119,8 +119,7 @@ impl RustBindingTest { } pub fn execute_l1_block(&mut self, l1_block: L1BlockWithBody) -> Result<()> { - //TODO get the sequence_number from state - let sequence_number = 0; + let sequence_number = self.get_account_sequence_number(self.sequencer)?; let ctx = self.create_bt_blk_tx_ctx(sequence_number, l1_block.clone()); let verified_tx: VerifiedMoveOSTransaction = self.executor @@ -175,6 +174,14 @@ impl RustBindingTest { self.root = root; Ok(result) } + + pub fn get_account_sequence_number(&self, address: AccountAddress) -> Result { + Ok(self + .resolver() + .get_account(address)? + .map(|account| account.value.sequence_number) + .unwrap_or(0)) + } } impl MoveFunctionCaller for RustBindingTest { diff --git a/crates/rooch-test-transaction-builder/src/lib.rs b/crates/rooch-test-transaction-builder/src/lib.rs index 1b2f457495..126e99366d 100644 --- a/crates/rooch-test-transaction-builder/src/lib.rs +++ b/crates/rooch-test-transaction-builder/src/lib.rs @@ -12,26 +12,31 @@ use moveos_types::move_types::FunctionId; use moveos_types::state::MoveStructType; use moveos_types::transaction::{FunctionCall, MoveAction}; use moveos_verifier::build::run_verifier; +use rooch_types::crypto::RoochKeyPair; use rooch_types::error::RoochError; use rooch_types::framework::empty::Empty; use rooch_types::framework::gas_coin::GasCoin; use rooch_types::framework::transfer::TransferModule; use rooch_types::test_utils::{random_string, random_string_with_size}; use rooch_types::transaction::rooch::RoochTransactionData; +use rooch_types::transaction::RoochTransaction; use std::collections::BTreeMap; use std::io::stderr; use std::path::PathBuf; -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct TestTransactionBuilder { + pub kp: RoochKeyPair, pub sender: AccountAddress, pub sequence_number: u64, } impl TestTransactionBuilder { - pub fn new(sender: AccountAddress) -> Self { + pub fn new(kp: RoochKeyPair) -> Self { + let sender = kp.public().rooch_address().unwrap(); Self { - sender, + kp, + sender: sender.into(), sequence_number: 0, } } @@ -184,4 +189,9 @@ impl TestTransactionBuilder { pub fn build(&self, action: MoveAction) -> RoochTransactionData { RoochTransactionData::new_for_test(self.sender.into(), self.sequence_number, action) } + + pub fn build_and_sign(&self, action: MoveAction) -> Result { + let tx_data = self.build(action); + Ok(tx_data.sign(&self.kp)) + } } diff --git a/crates/rooch-types/src/transaction/authenticator.rs b/crates/rooch-types/src/transaction/authenticator.rs index 1d75264bc2..f96981f696 100644 --- a/crates/rooch-types/src/transaction/authenticator.rs +++ b/crates/rooch-types/src/transaction/authenticator.rs @@ -121,6 +121,13 @@ impl Authenticator { } } + pub fn sign(kp: &RoochKeyPair, tx_data: &RoochTransactionData) -> Self { + match kp.public().scheme() { + SignatureScheme::Ed25519 => Self::rooch(kp, tx_data), + SignatureScheme::Secp256k1 => Self::bitcoin(kp, tx_data), + } + } + /// Create a rooch authenticator for session key pub fn rooch(kp: &RoochKeyPair, tx_data: &RoochTransactionData) -> Self { debug_assert_eq!(kp.public().scheme(), SignatureScheme::Ed25519); diff --git a/crates/rooch-types/src/transaction/rooch.rs b/crates/rooch-types/src/transaction/rooch.rs index 19fc970ea3..5b12012522 100644 --- a/crates/rooch-types/src/transaction/rooch.rs +++ b/crates/rooch-types/src/transaction/rooch.rs @@ -4,6 +4,7 @@ use super::RawTransaction; use super::{authenticator::Authenticator, AuthenticatorInfo}; use crate::address::RoochAddress; +use crate::crypto::RoochKeyPair; use crate::rooch_network::BuiltinChainID; use anyhow::Result; use moveos_types::h256::H256; @@ -63,6 +64,11 @@ impl RoochTransactionData { pub fn tx_hash(&self) -> H256 { moveos_types::h256::sha3_256_of(self.encode().as_slice()) } + + pub fn sign(&self, kp: &RoochKeyPair) -> RoochTransaction { + let auth = Authenticator::sign(kp, self); + RoochTransaction::new(self.clone(), auth) + } } #[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)] @@ -150,7 +156,7 @@ impl RoochTransaction { //TODO use protest Arbitrary to generate mock data pub fn mock() -> RoochTransaction { - use crate::{address::RoochSupportedAddress, crypto::RoochKeyPair}; + use crate::address::RoochSupportedAddress; use move_core_types::{ account_address::AccountAddress, identifier::Identifier, language_storage::ModuleId, }; diff --git a/moveos/moveos-types/src/state_resolver.rs b/moveos/moveos-types/src/state_resolver.rs index d1f254817c..020c4ac615 100644 --- a/moveos/moveos-types/src/state_resolver.rs +++ b/moveos/moveos-types/src/state_resolver.rs @@ -3,7 +3,7 @@ use crate::moveos_std::account::Account; use crate::moveos_std::module_store::Package; -use crate::moveos_std::object::{ObjectID, RawObject, RootObjectEntity}; +use crate::moveos_std::object::{ObjectEntity, ObjectID, RawObject, RootObjectEntity}; use crate::state::{AnnotatedKeyState, KeyState}; use crate::{ access_path::AccessPath, @@ -306,3 +306,14 @@ pub trait AnnotatedStateReader: StateReader + MoveResolver { } impl AnnotatedStateReader for T where T: StateReader + MoveResolver {} + +pub trait StateReaderExt: StateReader { + fn get_account(&self, address: AccountAddress) -> Result>> { + let account_object_id = Account::account_object_id(address); + self.get_object(&account_object_id)? + .map(|obj| obj.into_object::()) + .transpose() + } +} + +impl StateReaderExt for T where T: StateReader {}