Issue #394 - Refactor Block::merkle_root() to

Block::compute_merkle_root() and deprecate Block::merkle_root().
This commit is contained in:
Nils Loewen 2022-01-12 13:37:23 -08:00
parent bc9388e24a
commit 05788285f5
No known key found for this signature in database
GPG Key ID: D7A47D2C84312FF5
1 changed files with 10 additions and 3 deletions

View File

@ -176,7 +176,7 @@ impl Block {
/// check if merkle root of header matches merkle root of the transaction list /// check if merkle root of header matches merkle root of the transaction list
pub fn check_merkle_root (&self) -> bool { pub fn check_merkle_root (&self) -> bool {
match self.merkle_root() { match self.compute_merkle_root() {
Some(merkle_root) => self.header.merkle_root == merkle_root, Some(merkle_root) => self.header.merkle_root == merkle_root,
None => false, None => false,
} }
@ -212,7 +212,14 @@ impl Block {
false false
} }
/// Compute the transaction merkle root.
pub fn compute_merkle_root(&self) -> Option<TxMerkleNode> {
let hashes = self.txdata.iter().map(|obj| obj.txid().as_hash());
bitcoin_merkle_root(hashes).map(|h| h.into())
}
/// Calculate the transaction merkle root. /// Calculate the transaction merkle root.
#[deprecated(since = "0.27.1", note = "Please use `block::compute_merkle_root` instead.")]
pub fn merkle_root(&self) -> Option<TxMerkleNode> { pub fn merkle_root(&self) -> Option<TxMerkleNode> {
let hashes = self.txdata.iter().map(|obj| obj.txid().as_hash()); let hashes = self.txdata.iter().map(|obj| obj.txid().as_hash());
bitcoin_merkle_root(hashes).map(|h| h.into()) bitcoin_merkle_root(hashes).map(|h| h.into())
@ -377,7 +384,7 @@ mod tests {
let real_decode = decode.unwrap(); let real_decode = decode.unwrap();
assert_eq!(real_decode.header.version, 1); assert_eq!(real_decode.header.version, 1);
assert_eq!(serialize(&real_decode.header.prev_blockhash), prevhash); assert_eq!(serialize(&real_decode.header.prev_blockhash), prevhash);
assert_eq!(real_decode.header.merkle_root, real_decode.merkle_root().unwrap()); assert_eq!(real_decode.header.merkle_root, real_decode.compute_merkle_root().unwrap());
assert_eq!(serialize(&real_decode.header.merkle_root), merkle); assert_eq!(serialize(&real_decode.header.merkle_root), merkle);
assert_eq!(real_decode.header.time, 1231965655); assert_eq!(real_decode.header.time, 1231965655);
assert_eq!(real_decode.header.bits, 486604799); assert_eq!(real_decode.header.bits, 486604799);
@ -413,7 +420,7 @@ mod tests {
assert_eq!(real_decode.header.version, 0x20000000); // VERSIONBITS but no bits set assert_eq!(real_decode.header.version, 0x20000000); // VERSIONBITS but no bits set
assert_eq!(serialize(&real_decode.header.prev_blockhash), prevhash); assert_eq!(serialize(&real_decode.header.prev_blockhash), prevhash);
assert_eq!(serialize(&real_decode.header.merkle_root), merkle); assert_eq!(serialize(&real_decode.header.merkle_root), merkle);
assert_eq!(real_decode.header.merkle_root, real_decode.merkle_root().unwrap()); assert_eq!(real_decode.header.merkle_root, real_decode.compute_merkle_root().unwrap());
assert_eq!(real_decode.header.time, 1472004949); assert_eq!(real_decode.header.time, 1472004949);
assert_eq!(real_decode.header.bits, 0x1a06d450); assert_eq!(real_decode.header.bits, 0x1a06d450);
assert_eq!(real_decode.header.nonce, 1879759182); assert_eq!(real_decode.header.nonce, 1879759182);