Merge rust-bitcoin/rust-bitcoin#775: Issue #394 - Refactor Block::merkle_root()

cd2435c807 Change deprecated version to 0.28. Remove redundant Block::merkle_root() logic. (Nils Loewen)
05788285f5 Issue #394 - Refactor Block::merkle_root() to Block::compute_merkle_root() and deprecate Block::merkle_root(). (Nils Loewen)

Pull request description:

  Refactor `Block::merkle_root()` to `Block::compute_merkle_root()` and deprecate `Block::merkle_root()`.

ACKs for top commit:
  apoelstra:
    ACK cd2435c807
  Kixunil:
    ACK cd2435c807

Tree-SHA512: 820d85d5a25b7316046d5df8e3ab1e8cd57f72c7fa63f0d6826b965c5da3ef1bfacd4704180810aa8c0a7a224dcd62f398a2cf93fc344e2b0d52d9d7024c6c27
This commit is contained in:
Andrew Poelstra 2022-01-13 14:32:11 +00:00
commit 907b3a7a6a
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 11 additions and 5 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,12 +212,18 @@ impl Block {
false false
} }
/// Calculate the transaction merkle root. /// Compute the transaction merkle root.
pub fn merkle_root(&self) -> Option<TxMerkleNode> { pub fn compute_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())
} }
/// Calculate the transaction merkle root.
#[deprecated(since = "0.28.0", note = "Please use `block::compute_merkle_root` instead.")]
pub fn merkle_root(&self) -> Option<TxMerkleNode> {
self.compute_merkle_root()
}
/// compute witness commitment for the transaction list /// compute witness commitment for the transaction list
pub fn compute_witness_commitment (witness_root: &WitnessMerkleNode, witness_reserved_value: &[u8]) -> WitnessCommitment { pub fn compute_witness_commitment (witness_root: &WitnessMerkleNode, witness_reserved_value: &[u8]) -> WitnessCommitment {
let mut encoder = WitnessCommitment::engine(); let mut encoder = WitnessCommitment::engine();
@ -377,7 +383,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 +419,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);