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

perf: calculate the main chain height more efficiently #237

Merged
merged 5 commits into from
Jul 26, 2023
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 Cargo.lock

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

1 change: 1 addition & 0 deletions benchmarks/drun.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
create
install rwlgt-iiaaa-aaaaa-aaaaa-cai ../target/wasm32-unknown-unknown/release/benchmarks.wasm.gz ""
query rwlgt-iiaaa-aaaaa-aaaaa-cai insert_300_blocks "DIDL\x00\x00"
query rwlgt-iiaaa-aaaaa-aaaaa-cai get_metrics "DIDL\x00\x00"
2 changes: 1 addition & 1 deletion benchmarks/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ bash ../scripts/build-canister.sh benchmarks
drun ./drun.txt --instruction-limit 99999999999999 \
| awk '{ print $3 }' \
| grep "44.*" -o \
| xargs didc decode
| xargs -L 1 didc decode
36 changes: 30 additions & 6 deletions benchmarks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ thread_local! {

#[init]
fn init() {
ic_btc_canister::init(Config {
network: Network::Testnet,
stability_threshold: 144,
..Config::default()
});

// Load the testnet blocks.
TESTNET_BLOCKS.with(|blocks| {
blocks.replace(
Expand All @@ -35,6 +29,12 @@ fn init() {
// Benchmarks inserting the first 300 blocks of the Bitcoin testnet.
#[query]
fn insert_300_blocks() -> u64 {
ic_btc_canister::init(Config {
network: Network::Testnet,
stability_threshold: 144,
..Config::default()
});

count_instructions(|| {
with_state_mut(|s| {
for i in 0..300 {
Expand All @@ -48,6 +48,30 @@ fn insert_300_blocks() -> u64 {
})
}

// Benchmarks gettings the metrics when there are many unstable blocks..
#[query]
fn get_metrics() -> u64 {
ic_btc_canister::init(Config {
network: Network::Testnet,
stability_threshold: 3000,
..Config::default()
});

with_state_mut(|s| {
for i in 0..3000 {
ic_btc_canister::state::insert_block(
s,
TESTNET_BLOCKS.with(|b| b.borrow()[i as usize].clone()),
)
.unwrap();
}
});

count_instructions(|| {
ic_btc_canister::get_metrics();
})
}

// Returns the number of instructions consumed by the given function.
fn count_instructions<R>(f: impl FnOnce() -> R) -> u64 {
let start = ic_cdk::api::performance_counter(0);
Expand Down
3,700 changes: 3,700 additions & 0 deletions benchmarks/src/testnet_blocks.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions canister/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
state::State,
types::{into_bitcoin_network, Block, HttpRequest, HttpResponse},
};
pub use api::get_metrics;
pub use api::send_transaction;
pub use api::set_config;
pub use heartbeat::heartbeat;
Expand Down
3 changes: 2 additions & 1 deletion canister/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ pub fn ingest_stable_blocks_into_utxoset(state: &mut State) -> bool {
}

pub fn main_chain_height(state: &State) -> Height {
unstable_blocks::get_main_chain(&state.unstable_blocks).len() as u32 + state.utxos.next_height()
unstable_blocks::get_main_chain_length(&state.unstable_blocks) as u32
+ state.utxos.next_height()
- 1
}

Expand Down
16 changes: 16 additions & 0 deletions canister/src/unstable_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ pub fn get_main_chain(blocks: &UnstableBlocks) -> BlockChain {
main_chain
}

/// Returns the length of the "main chain".
/// See `get_main_chain` for what defines a main chain.
pub fn get_main_chain_length(blocks: &UnstableBlocks) -> usize {
let blocks_by_height = blocks.blocks_with_depths_by_heights();

// Traverse the heights in reverse order. The highest height with a single block corresponds to
// the tip of the main chain.
for height in (0..blocks_by_height.len()).rev() {
if blocks_by_height[height].len() == 1 {
return height + 1;
}
}

unreachable!("There must be at least one height with exactly one block.");
}

pub fn get_blocks(blocks: &UnstableBlocks) -> Vec<&Block> {
blocktree::blockchains(&blocks.tree)
.into_iter()
Expand Down
Loading