Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bench] Fix and refactor the bench #1867

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions crates/rooch-benchmarks/benches/bench_tx_sequence.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
18 changes: 8 additions & 10 deletions crates/rooch-benchmarks/benches/bench_tx_validate.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
14 changes: 2 additions & 12 deletions crates/rooch-benchmarks/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,21 +28,16 @@ pub fn gen_sequencer(keypair: RoochKeyPair, rooch_store: RoochStore) -> Result<S

pub fn create_publish_transaction(
test_transaction_builder: &TestTransactionBuilder,
keystore: &InMemKeystore,
) -> Result<RoochTransaction> {
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<RoochTransaction> {
Expand All @@ -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<Vec<u64>> {
Expand Down
57 changes: 28 additions & 29 deletions crates/rooch-benchmarks/src/tx_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -17,29 +17,22 @@ 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 {
BtcBlock => ("btc_blk", 20), // block after 800,000 always need seconds/block
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();
Expand All @@ -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();
Expand Down
13 changes: 10 additions & 3 deletions crates/rooch-framework-tests/src/binding_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -175,6 +174,14 @@ impl RustBindingTest {
self.root = root;
Ok(result)
}

pub fn get_account_sequence_number(&self, address: AccountAddress) -> Result<u64> {
Ok(self
.resolver()
.get_account(address)?
.map(|account| account.value.sequence_number)
.unwrap_or(0))
}
}

impl MoveFunctionCaller for RustBindingTest {
Expand Down
16 changes: 13 additions & 3 deletions crates/rooch-test-transaction-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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<RoochTransaction> {
let tx_data = self.build(action);
Ok(tx_data.sign(&self.kp))
}
}
7 changes: 7 additions & 0 deletions crates/rooch-types/src/transaction/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion crates/rooch-types/src/transaction/rooch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
};
Expand Down
13 changes: 12 additions & 1 deletion moveos/moveos-types/src/state_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -306,3 +306,14 @@ pub trait AnnotatedStateReader: StateReader + MoveResolver {
}

impl<T> AnnotatedStateReader for T where T: StateReader + MoveResolver {}

pub trait StateReaderExt: StateReader {
fn get_account(&self, address: AccountAddress) -> Result<Option<ObjectEntity<Account>>> {
let account_object_id = Account::account_object_id(address);
self.get_object(&account_object_id)?
.map(|obj| obj.into_object::<Account>())
.transpose()
}
}

impl<T> StateReaderExt for T where T: StateReader {}
Loading