From: Elias Rohrer Date: Mon, 15 Jul 2024 11:43:22 +0000 (+0200) Subject: Use upstreamed `validate_merkle_proof` X-Git-Tag: v0.0.124-beta~52^2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=b3a58e6db69a0d74c4f9d61fdcc9df80dac86854;p=rust-lightning Use upstreamed `validate_merkle_proof` We previously upstreamed the `validate_merkle_proof` utility method, which shipped with `electrum-client` 0.19.0. Since we upgraded to that version recently, we can now drop our local code and use the upstreamed version. --- diff --git a/lightning-transaction-sync/src/electrum.rs b/lightning-transaction-sync/src/electrum.rs index 8ba250f84..e7edb3b7e 100644 --- a/lightning-transaction-sync/src/electrum.rs +++ b/lightning-transaction-sync/src/electrum.rs @@ -1,9 +1,9 @@ use crate::common::{ConfirmedTx, FilterQueue, SyncState}; use crate::error::{InternalError, TxSyncError}; +use electrum_client::utils::validate_merkle_proof; use electrum_client::Client as ElectrumClient; use electrum_client::ElectrumApi; -use electrum_client::GetMerkleRes; use lightning::chain::WatchedOutput; use lightning::chain::{Confirm, Filter}; @@ -11,9 +11,6 @@ use lightning::util::logger::Logger; use lightning::{log_debug, log_error, log_trace}; use bitcoin::block::Header; -use bitcoin::hash_types::TxMerkleNode; -use bitcoin::hashes::sha256d::Hash as Sha256d; -use bitcoin::hashes::Hash; use bitcoin::{BlockHash, Script, Transaction, Txid}; use std::collections::HashSet; @@ -430,11 +427,7 @@ where match self.client.block_header(prob_conf_height as usize) { Ok(block_header) => { let pos = merkle_res.pos; - if !self.validate_merkle_proof( - &txid, - &block_header.merkle_root, - merkle_res, - )? { + if !validate_merkle_proof(&txid, &block_header.merkle_root, &merkle_res) { log_trace!( self.logger, "Inconsistency: Block {} was unconfirmed during syncing.", @@ -480,25 +473,6 @@ where pub fn client(&self) -> &ElectrumClient { &self.client } - - fn validate_merkle_proof( - &self, txid: &Txid, merkle_root: &TxMerkleNode, merkle_res: GetMerkleRes, - ) -> Result { - let mut index = merkle_res.pos; - let mut cur = txid.to_raw_hash(); - for mut bytes in merkle_res.merkle { - bytes.reverse(); - // unwrap() safety: `bytes` has len 32 so `from_slice` can never fail. - let next_hash = Sha256d::from_slice(&bytes).unwrap(); - let (left, right) = if index % 2 == 0 { (cur, next_hash) } else { (next_hash, cur) }; - - let data = [&left[..], &right[..]].concat(); - cur = Sha256d::hash(&data); - index /= 2; - } - - Ok(cur == merkle_root.to_raw_hash()) - } } impl Filter for ElectrumSyncClient