-
Notifications
You must be signed in to change notification settings - Fork 392
feat(electrum): optimize merkle proof validation with batching #1957
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7a18cad
feat(electrum): optimize merkle proof validation with batching
LagginTimes b57768d
fix(electrum): improve tx validation and gap limit scanning
Keerthi421 f21a21d
test(electrum): add `criterion` benchmark for `sync`
LagginTimes ec4fd97
feat(electrum): batched `Header`s and `script_get_history`
LagginTimes 4ea5ea6
feat(electrum): batch `transaction.get_merkle` calls via `batch_call`
LagginTimes 156cbab
test(electrum): Improve benchmark
evanlinjin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use bdk_chain::bitcoin::{Address, Amount, ScriptBuf}; | ||
use bdk_core::{ | ||
bitcoin::{ | ||
consensus::WriteExt, | ||
hashes::Hash, | ||
key::{Secp256k1, UntweakedPublicKey}, | ||
Network, TapNodeHash, | ||
}, | ||
spk_client::SyncRequest, | ||
CheckPoint, | ||
}; | ||
use bdk_electrum::BdkElectrumClient; | ||
use bdk_testenv::{anyhow, bitcoincore_rpc::RpcApi, TestEnv}; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use electrum_client::ElectrumApi; | ||
use std::{collections::BTreeSet, time::Duration}; | ||
|
||
// Batch size for `sync_with_electrum`. | ||
const BATCH_SIZE: usize = 100; | ||
|
||
pub fn get_test_spk(i: usize) -> ScriptBuf { | ||
const PK_BYTES: &[u8] = &[ | ||
12, 244, 72, 4, 163, 4, 211, 81, 159, 82, 153, 123, 125, 74, 142, 40, 55, 237, 191, 231, | ||
31, 114, 89, 165, 83, 141, 8, 203, 93, 240, 53, 101, | ||
]; | ||
let secp = Secp256k1::new(); | ||
let pk = UntweakedPublicKey::from_slice(PK_BYTES).expect("Must be valid PK"); | ||
let mut engine = TapNodeHash::engine(); | ||
engine.emit_u64(i as u64).expect("must emit"); | ||
ScriptBuf::new_p2tr(&secp, pk, Some(TapNodeHash::from_engine(engine))) | ||
} | ||
|
||
fn sync_with_electrum<E: ElectrumApi>( | ||
client: &BdkElectrumClient<E>, | ||
spks: &[ScriptBuf], | ||
chain_tip: &CheckPoint, | ||
) -> anyhow::Result<()> { | ||
let update = client.sync( | ||
SyncRequest::builder() | ||
.chain_tip(chain_tip.clone()) | ||
.spks(spks.iter().cloned()), | ||
BATCH_SIZE, | ||
true, | ||
)?; | ||
|
||
assert!( | ||
!update.tx_update.txs.is_empty(), | ||
"expected some transactions from sync, but got none" | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn test_sync_performance(c: &mut Criterion) { | ||
let env = TestEnv::new().unwrap(); | ||
|
||
const NUM_BLOCKS: usize = 100; | ||
let mut spks = Vec::with_capacity(NUM_BLOCKS); | ||
|
||
// Mine some blocks and send transactions. | ||
env.mine_blocks(101, None).unwrap(); | ||
|
||
// Scatter UTXOs across many blocks. | ||
for i in 0..NUM_BLOCKS { | ||
let spk = get_test_spk(i); | ||
let addr = Address::from_script(&spk, Network::Regtest).unwrap(); | ||
env.send(&addr, Amount::from_sat(10_000)).unwrap(); | ||
env.mine_blocks(1, None).unwrap(); | ||
|
||
spks.push(spk); | ||
} | ||
let _ = env.wait_until_electrum_sees_block(Duration::from_secs(6)); | ||
assert_eq!( | ||
spks.iter().cloned().collect::<BTreeSet<_>>().len(), | ||
spks.len(), | ||
"all spks must be unique", | ||
); | ||
|
||
// Setup receiver. | ||
let genesis_cp = CheckPoint::new(bdk_core::BlockId { | ||
height: 0, | ||
hash: env.bitcoind.client.get_block_hash(0).unwrap(), | ||
}); | ||
|
||
{ | ||
let electrum_client = | ||
electrum_client::Client::new(env.electrsd.electrum_url.as_str()).unwrap(); | ||
let spks = spks.clone(); | ||
let genesis_cp = genesis_cp.clone(); | ||
c.bench_function("sync_with_electrum", move |b| { | ||
b.iter(|| { | ||
sync_with_electrum( | ||
&BdkElectrumClient::new(&electrum_client), | ||
&spks, | ||
&genesis_cp, | ||
) | ||
.expect("must not error") | ||
}) | ||
}); | ||
} | ||
|
||
{ | ||
let client = BdkElectrumClient::new( | ||
electrum_client::Client::new(env.electrsd.electrum_url.as_str()).unwrap(), | ||
); | ||
c.bench_function("sync_with_electrum_cached", move |b| { | ||
b.iter(|| sync_with_electrum(&client, &spks, &genesis_cp).expect("must not error")) | ||
}); | ||
} | ||
} | ||
|
||
criterion_group! { | ||
name = benches; | ||
config = Criterion::default() | ||
.sample_size(10); | ||
targets = test_sync_performance | ||
} | ||
criterion_main!(benches); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.