From 01df3f7f71d1d4a1283f925d670ea274b7a14bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Blankfors?= Date: Thu, 20 Jun 2024 14:12:56 +0200 Subject: [PATCH 1/2] feat: Proper coinbase generation --- signer/src/testing/dummy.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/signer/src/testing/dummy.rs b/signer/src/testing/dummy.rs index 3c877faf3..dceb9fa81 100644 --- a/signer/src/testing/dummy.rs +++ b/signer/src/testing/dummy.rs @@ -14,17 +14,7 @@ pub fn block(config: &fake::Faker, rng: &mut R) -> bi .take(number_of_transactions) .collect(); - // TODO(#213): Proper coinbase generation - let coinbase_script = bitcoin::script::Builder::new() - .push_slice([1, 2, 3, 4]) - .into_script(); - - let mut coinbase_tx = tx(config, rng); - let mut coinbase_input = txin(config, rng); - coinbase_input.script_sig = coinbase_script; - coinbase_tx.input = vec![coinbase_input]; - - txdata.insert(0, coinbase_tx); + txdata.insert(0, coinbase_tx(config, rng)); let header = bitcoin::block::Header { version: bitcoin::block::Version::TWO, @@ -161,3 +151,25 @@ pub fn signature( p256k1::ecdsa::Signature::new(&multipurpose_bytes, &secret_key).unwrap() } + +/// Coinbase transaction with random block height +fn coinbase_tx( + config: &fake::Faker, + rng: &mut R, +) -> bitcoin::Transaction { + // Numbers below 17 are encoded differently which messes with the block height decoding + let min_block_height = 17; + let block_height_span = 10000; + let block_height = + config.fake_with_rng::(rng).abs() % block_height_span + min_block_height; + let coinbase_script = bitcoin::script::Builder::new() + .push_int(block_height) + .into_script(); + + let mut coinbase_tx = tx(config, rng); + let mut coinbase_input = txin(config, rng); + coinbase_input.script_sig = coinbase_script; + coinbase_tx.input = vec![coinbase_input]; + + coinbase_tx +} From b7e88cb99ecc8c78c3be8888960e353d16462d11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Blankfors?= Date: Wed, 26 Jun 2024 12:38:06 +0200 Subject: [PATCH 2/2] refactor: use rng.gen_range for block height in dummy.rs --- signer/src/testing/dummy.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/signer/src/testing/dummy.rs b/signer/src/testing/dummy.rs index dceb9fa81..4fbd43766 100644 --- a/signer/src/testing/dummy.rs +++ b/signer/src/testing/dummy.rs @@ -3,6 +3,7 @@ use bitcoin::hashes::Hash; use blockstack_lib::chainstate::{nakamoto, stacks}; use fake::Fake; +use rand::Rng; /// Dummy block pub fn block(config: &fake::Faker, rng: &mut R) -> bitcoin::Block { @@ -159,9 +160,8 @@ fn coinbase_tx( ) -> bitcoin::Transaction { // Numbers below 17 are encoded differently which messes with the block height decoding let min_block_height = 17; - let block_height_span = 10000; - let block_height = - config.fake_with_rng::(rng).abs() % block_height_span + min_block_height; + let max_block_height = 10000; + let block_height = rng.gen_range(min_block_height..max_block_height); let coinbase_script = bitcoin::script::Builder::new() .push_int(block_height) .into_script();