Skip to content

Commit

Permalink
Added a benchmark for the predicate with ed19 verification (#2127)
Browse files Browse the repository at this point in the history
### Before requesting review
- [x] I have reviewed the code myself
  • Loading branch information
xgreenx authored Aug 26, 2024
1 parent 882d264 commit 3ba77ae
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
90 changes: 85 additions & 5 deletions benches/benches/transaction_throughput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use criterion::{
Criterion,
SamplingMode,
};
use ed25519_dalek::Signer;
use fuel_core::service::config::Trigger;
use fuel_core_benches::*;
use fuel_core_storage::transactional::AtomicView;
Expand Down Expand Up @@ -49,6 +50,7 @@ use std::{
time::Duration,
};
use test_helpers::builder::{
local_chain_config,
TestContext,
TestSetupBuilder,
};
Expand Down Expand Up @@ -85,6 +87,7 @@ where
// disable automated block production
test_builder.trigger = Trigger::Never;
test_builder.utxo_validation = true;
test_builder.gas_limit = Some(10_000_000_000);

// spin up node
let transactions: Vec<Transaction> =
Expand All @@ -109,7 +112,12 @@ where
test_builder.finalize().await;

// insert all transactions
srv.shared.txpool_shared_state.insert(transactions).await;
let results =
srv.shared.txpool_shared_state.insert(transactions).await;
for result in results {
let result = result.expect("Should insert transaction");
assert_eq!(result.removed.len(), 0);
}
let _ = client.produce_blocks(1, None).await;

// sanity check block to ensure the transactions were actually processed
Expand Down Expand Up @@ -191,7 +199,6 @@ fn predicate_transfers(c: &mut Criterion) {
let owner = Input::predicate_owner(&predicate);

let mut tx = TransactionBuilder::script(vec![], vec![])
.script_gas_limit(10000)
.add_input(Input::coin_predicate(
rng.gen(),
owner,
Expand All @@ -215,7 +222,7 @@ fn predicate_transfers(c: &mut Criterion) {
.add_output(Output::coin(rng.gen(), 50, AssetId::default()))
.add_output(Output::change(rng.gen(), 0, AssetId::default()))
.finalize();
tx.estimate_predicates(&CheckPredicateParams::default(), MemoryInstance::new())
tx.estimate_predicates(&checked_parameters(), MemoryInstance::new())
.expect("Predicate check failed");
tx
};
Expand Down Expand Up @@ -281,17 +288,90 @@ fn predicate_transfers_eck1(c: &mut Criterion) {
.add_output(Output::coin(rng.gen(), 50, AssetId::default()))
.add_output(Output::change(rng.gen(), 0, AssetId::default()))
.finalize();
tx.estimate_predicates(&CheckPredicateParams::default(), MemoryInstance::new())
tx.estimate_predicates(&checked_parameters(), MemoryInstance::new())
.expect("Predicate check failed");
tx
};
bench_txs("predicate transfers eck1", c, generator);
}

fn predicate_transfers_ed19(c: &mut Criterion) {
let generator = |rng: &mut StdRng| {
let ed19_secret = ed25519_dalek::SigningKey::generate(&mut rand::rngs::OsRng {});
let public = ed19_secret.verifying_key();

let message = b"The gift of words is the gift of deception and illusion.";
let message = Message::new(message);

let signature = ed19_secret.sign(&*message).to_bytes();

let predicate = vec![
op::gm_args(0x20, GMArgs::GetVerifyingPredicate),
op::gtf_args(0x20, 0x20, GTFArgs::InputCoinPredicateData),
op::addi(0x21, 0x20, PublicKey::LEN as Immediate12),
op::addi(0x22, 0x21, signature.len() as Immediate12),
op::movi(0x24, message.as_ref().len() as Immediate18),
op::ed19(0x20, 0x21, 0x22, 0x24),
op::eq(0x12, RegId::ERR, RegId::ONE),
op::ret(0x12),
]
.into_iter()
.collect::<Vec<u8>>();
let owner = Input::predicate_owner(&predicate);

let predicate_data: Vec<u8> = public
.to_bytes()
.iter()
.copied()
.chain(
signature
.iter()
.copied()
.chain(message.as_ref().iter().copied()),
)
.collect();

let mut tx = TransactionBuilder::script(vec![], vec![])
.script_gas_limit(10000)
.add_input(Input::coin_predicate(
rng.gen(),
owner,
1000,
Default::default(),
Default::default(),
Default::default(),
predicate.clone(),
predicate_data.clone(),
))
.add_input(Input::coin_predicate(
rng.gen(),
owner,
1000,
Default::default(),
Default::default(),
Default::default(),
predicate,
predicate_data,
))
.add_output(Output::coin(rng.gen(), 50, AssetId::default()))
.add_output(Output::change(rng.gen(), 0, AssetId::default()))
.finalize();
tx.estimate_predicates(&checked_parameters(), MemoryInstance::new())
.expect("Predicate check failed");
tx
};
bench_txs("predicate transfers ed19", c, generator);
}

criterion_group!(
benches,
signed_transfers,
predicate_transfers,
predicate_transfers_eck1
predicate_transfers_eck1,
predicate_transfers_ed19
);
criterion_main!(benches);

fn checked_parameters() -> CheckPredicateParams {
local_chain_config().consensus_parameters.into()
}
10 changes: 7 additions & 3 deletions tests/test-helpers/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,7 @@ impl TestSetupBuilder {

// setup chainspec and spin up a fuel-node
pub async fn finalize(&mut self) -> TestContext {
let metadata =
SnapshotMetadata::read("../bin/fuel-core/chainspec/local-testnet").unwrap();
let mut chain_conf = ChainConfig::from_snapshot_metadata(&metadata).unwrap();
let mut chain_conf = local_chain_config();

if let Some(gas_limit) = self.gas_limit {
let tx_params = *chain_conf.consensus_parameters.tx_params();
Expand Down Expand Up @@ -257,3 +255,9 @@ impl Default for TestSetupBuilder {
}
}
}

pub fn local_chain_config() -> ChainConfig {
let metadata =
SnapshotMetadata::read("../bin/fuel-core/chainspec/local-testnet").unwrap();
ChainConfig::from_snapshot_metadata(&metadata).unwrap()
}

0 comments on commit 3ba77ae

Please sign in to comment.