Merge pull request #67 from sr-gi/56-fix-blockchain-generate

Makes sure generated blocks have valid pow
This commit is contained in:
Sergi Delgado Segura 2022-07-11 10:33:30 +02:00 committed by GitHub
commit f30ee8a294
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -200,19 +200,20 @@ impl Blockchain {
None => vec![],
};
let hashes = txdata.iter().map(|obj| obj.txid().as_hash());
let block = Block {
header: BlockHeader {
version: 0,
prev_blockhash,
merkle_root: bitcoin_merkle_root(hashes).into(),
time,
bits,
nonce: 0,
},
txdata,
let mut header = BlockHeader {
version: 0,
prev_blockhash,
merkle_root: bitcoin_merkle_root(hashes).into(),
time,
bits,
nonce: 0,
};
while header.validate_pow(&header.target()).is_err() {
header.nonce += 1;
}
let block = Block { header, txdata };
self.blocks.push(block.clone());
block