Skip to content

Commit

Permalink
Cherry-picked Optimized Rothschild performance on high TPS (kaspanet#371
Browse files Browse the repository at this point in the history
)
  • Loading branch information
coderofstuff authored and KashProtocol committed Jan 8, 2024
1 parent a9d1f9b commit 8b2db36
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 70 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

25 changes: 14 additions & 11 deletions consensus/core/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,23 @@ impl Transaction {
gas: u64,
payload: Vec<u8>,
) -> Self {
let mut tx = Self {
version,
inputs,
outputs,
action,
lock_time,
subnetwork_id,
gas,
payload,
id: Default::default(), // Temp init before the finalize below
};
let mut tx = Self::new_non_finalized(version, inputs, outputs, action, lock_time, subnetwork_id, gas, payload);
tx.finalize();
tx
}

pub fn new_non_finalized(
version: u16,
inputs: Vec<TransactionInput>,
outputs: Vec<TransactionOutput>,
action: TransactionAction,
lock_time: u64,
subnetwork_id: SubnetworkId,
gas: u64,
payload: Vec<u8>,
) -> Self {
Self { version, inputs, outputs, action, lock_time, subnetwork_id, gas, payload, id: Default::default() }
}
}

impl Transaction {
Expand Down
10 changes: 10 additions & 0 deletions rothschild/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ kash-rpc-core.workspace = true
kash-addresses.workspace = true
kash-txscript.workspace = true
kash-utils.workspace = true
async-channel.workspace = true
parking_lot.workspace = true

clap.workspace = true
faster-hex.workspace = true
itertools.workspace = true
log.workspace = true
rayon.workspace = true
secp256k1 = { workspace = true, features = ["global-context", "rand-std"] }
tokio = { workspace = true, features = ["rt", "macros", "rt-multi-thread"] }

[dev-dependencies]
criterion.workspace = true

[[bench]]
name = "bench"
harness = false
50 changes: 50 additions & 0 deletions rothschild/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use criterion::{criterion_group, criterion_main, Criterion};
use rayon::prelude::*;

use kash_consensus_core::asset_type::AssetType::KSH;
use kash_consensus_core::tx::TransactionAction::TransferKSH;
use kash_consensus_core::{
constants::TX_VERSION,
subnets::SUBNETWORK_ID_NATIVE,
tx::{ScriptPublicKey, Transaction, TransactionInput, TransactionOutpoint, TransactionOutput},
Hash,
};

fn constuct_tx() -> Transaction {
let inputs = vec![TransactionInput {
previous_outpoint: TransactionOutpoint { transaction_id: Hash::from_bytes([0xFF; 32]), index: 0 },
signature_script: vec![],
sequence: 0,
sig_op_count: 1,
}];
let outputs =
vec![TransactionOutput { value: 10000, asset_type: KSH, script_public_key: ScriptPublicKey::from_vec(0, vec![0xff; 35]) }];
Transaction::new(TX_VERSION, inputs, outputs, TransferKSH, 0, SUBNETWORK_ID_NATIVE, 0, vec![])
}

fn construct_txs_serially() {
let _ = (0..10000)
.map(|_| {
constuct_tx();
})
.collect::<Vec<_>>();
}

fn construct_txs_parallel() {
let _ = (0..10000)
.into_par_iter()
.map(|_| {
constuct_tx();
})
.collect::<Vec<_>>();
}

pub fn bench_compare_tx_generation(c: &mut Criterion) {
let mut group = c.benchmark_group("compare txs");
group.bench_function("Transaction::SerialCreation", |b| b.iter(construct_txs_serially));
group.bench_function("Transaction::ParallelCreation", |b| b.iter(construct_txs_parallel));
group.finish();
}

criterion_group!(benches, bench_compare_tx_generation);
criterion_main!(benches);
Loading

0 comments on commit 8b2db36

Please sign in to comment.