Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:

# Run benchmarks
- name: Run cargo bench
run: cargo bench
run: cargo bench --features test-utils

# Save cache only if the previous steps succeeded and there was not an exact cache key match
# This happens everytime we modify any `cargo.lock` or `cargo.toml`, or each two weeks (caching recent changes)
Expand Down
3 changes: 3 additions & 0 deletions crates/floresta-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ core2 = { version = "0.4.0", default-features = false }
floresta-common = { path = "../floresta-common", default-features = false, features = ["std"] }
bitcoinconsensus = { version = "0.106.0", optional = true, default-features = false }
metrics = { path = "../../metrics", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }

[dev-dependencies]
criterion = "0.5.1"
Expand All @@ -43,7 +44,9 @@ hex = "0.4.3"
default = []
bitcoinconsensus = ["bitcoin/bitcoinconsensus", "dep:bitcoinconsensus"]
metrics = ["dep:metrics"]
test-utils = ["dep:serde"]

[[bench]]
name = "chain_state_bench"
harness = false
required-features = ["test-utils"]
22 changes: 5 additions & 17 deletions crates/floresta-chain/benches/chain_state_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use bitcoin::consensus::deserialize;
use bitcoin::consensus::Decodable;
use bitcoin::Block;
use bitcoin::OutPoint;
use bitcoin::TxOut;
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::BatchSize;
Expand Down Expand Up @@ -49,28 +48,17 @@ fn decode_block_and_inputs(
let block_bytes = zstd::decode_all(block_file).unwrap();
let block: Block = deserialize(&block_bytes).unwrap();

// Get txos spent in the block
// Get utxos spent in the block
let stxos_bytes = zstd::decode_all(stxos_file).unwrap();
let mut stxos: Vec<TxOut> =
let mut stxos: Vec<UtxoData> =
serde_json::from_slice(&stxos_bytes).expect("Failed to deserialize JSON");

let inputs = block
.txdata
.iter()
.skip(1) // Skip the coinbase transaction
.flat_map(|tx| &tx.input)
.map(|txin| {
(
txin.previous_output,
UtxoData {
txout: stxos.remove(0),
is_coinbase: false,
// Using 0 for simplicity, we won't test the transaction time locks
creation_height: 0,
creation_time: 0,
},
)
})
.map(|txin| (txin.previous_output, stxos.remove(0)))
.collect();

assert!(stxos.is_empty(), "Moved all stxos to the inputs map");
Expand Down Expand Up @@ -135,7 +123,7 @@ fn connect_blocks_benchmark(c: &mut Criterion) {

fn validate_full_block_benchmark(c: &mut Criterion) {
let block_file = File::open("./testdata/block_866342/raw.zst").unwrap();
let stxos_file = File::open("./testdata/block_866342/spent_txos.zst").unwrap();
let stxos_file = File::open("./testdata/block_866342/spent_utxos.zst").unwrap();
let (block, inputs) = decode_block_and_inputs(block_file, stxos_file);

let chain = setup_test_chain(Network::Bitcoin, AssumeValidArg::Disabled);
Expand All @@ -160,7 +148,7 @@ fn validate_many_inputs_block_benchmark(c: &mut Criterion) {
}

let block_file = File::open("./testdata/block_367891/raw.zst").unwrap();
let stxos_file = File::open("./testdata/block_367891/spent_txos.zst").unwrap();
let stxos_file = File::open("./testdata/block_367891/spent_utxos.zst").unwrap();
let (block, inputs) = decode_block_and_inputs(block_file, stxos_file);

let chain = setup_test_chain(Network::Bitcoin, AssumeValidArg::Disabled);
Expand Down
22 changes: 5 additions & 17 deletions crates/floresta-chain/src/pruned_utreexo/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,6 @@ mod test {
use bitcoin::Block;
use bitcoin::BlockHash;
use bitcoin::OutPoint;
use bitcoin::TxOut;
use floresta_common::bhash;
use rand::Rng;
use rustreexo::accumulator::proof::Proof;
Expand Down Expand Up @@ -1403,28 +1402,17 @@ mod test {
let block_bytes = zstd::decode_all(block_file).unwrap();
let block: Block = deserialize(&block_bytes).unwrap();

// Get txos spent in the block
// Get utxos spent in the block
let stxos_bytes = zstd::decode_all(stxos_file).unwrap();
let mut stxos: Vec<TxOut> =
let mut stxos: Vec<UtxoData> =
serde_json::from_slice(&stxos_bytes).expect("Failed to deserialize JSON");

let inputs = block
.txdata
.iter()
.skip(1) // Skip the coinbase transaction
.flat_map(|tx| &tx.input)
.map(|txin| {
(
txin.previous_output,
UtxoData {
txout: stxos.remove(0),
is_coinbase: false,
// Using 0 for simplicity, we won't test the transaction time locks
creation_height: 0,
creation_time: 0,
},
)
})
.map(|txin| (txin.previous_output, stxos.remove(0)))
.collect();

assert!(stxos.is_empty(), "Moved all stxos to the inputs map");
Expand All @@ -1436,7 +1424,7 @@ mod test {
#[cfg_attr(debug_assertions, ignore = "this test is very slow in debug mode")]
fn test_validate_many_inputs_block() {
let block_file = File::open("./testdata/block_367891/raw.zst").unwrap();
let stxos_file = File::open("./testdata/block_367891/spent_txos.zst").unwrap();
let stxos_file = File::open("./testdata/block_367891/spent_utxos.zst").unwrap();
let (block, inputs) = decode_block_and_inputs(block_file, stxos_file);

assert_eq!(
Expand All @@ -1454,7 +1442,7 @@ mod test {
#[test]
fn test_validate_full_block() {
let block_file = File::open("./testdata/block_866342/raw.zst").unwrap();
let stxos_file = File::open("./testdata/block_866342/spent_txos.zst").unwrap();
let stxos_file = File::open("./testdata/block_866342/spent_utxos.zst").unwrap();
let (block, inputs) = decode_block_and_inputs(block_file, stxos_file);

assert_eq!(
Expand Down
7 changes: 5 additions & 2 deletions crates/floresta-chain/src/pruned_utreexo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use bitcoin::Block;
use bitcoin::BlockHash;
use bitcoin::OutPoint;
use bitcoin::Transaction;
use bitcoin::TxOut;
use rustreexo::accumulator::node_hash::BitcoinNodeHash;
use rustreexo::accumulator::proof::Proof;
use rustreexo::accumulator::stump::Stump;
Expand Down Expand Up @@ -377,9 +376,13 @@ impl<T: BlockchainInterface> BlockchainInterface for Arc<T> {

/// This module defines an [UtxoData] struct, helpful for transaction validation
pub mod utxo_data {
use super::*;
use bitcoin::TxOut;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(serde::Serialize, serde::Deserialize)
)]
/// Represents an unspent transaction output (UTXO) with additional metadata for validation.
pub struct UtxoData {
/// The unspent transaction output.
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
17 changes: 14 additions & 3 deletions doc/benchmarking.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
# Benchmarking
Floresta uses `criterion.rs` for benchmarking. You can run the default set of benchmarks with:

Floresta uses `criterion.rs` for benchmarking. Assuming you have the [Just](https://github.com/casey/just) command runner installed, you can run the default set of benchmarks with:

```bash
just bench
```

Under the hood this runs:

```bash
cargo bench
cargo bench --features test-utils
```

By default, benchmarks that are resource-intensive are excluded to allow for quicker testing. If you'd like to include all benchmarks, use the following command:

```bash
EXPENSIVE_BENCHES=1 cargo bench
# with Just:
EXPENSIVE_BENCHES=1 just bench

# or, without Just:
EXPENSIVE_BENCHES=1 cargo bench --features test-utils
```

> **Note**: Running with `EXPENSIVE_BENCHES=1` enables the full benchmark suite, which will take several minutes to complete.
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ test-functional-run:
test-functional-uv-run:
uv run tests/run_tests.py

# Run the benchmarks
bench:
cargo bench --features test-utils

# Generate documentation for all crates
doc:
@just test-doc
Expand Down