Skip to content

Commit

Permalink
feat: Proper coinbase generation
Browse files Browse the repository at this point in the history
  • Loading branch information
netrome committed Jun 24, 2024
1 parent ca36f9e commit d3dd28d
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions signer/src/testing/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,7 @@ pub fn block<R: rand::RngCore + ?Sized>(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,
Expand Down Expand Up @@ -161,3 +151,25 @@ pub fn signature<R: rand::RngCore + ?Sized>(

p256k1::ecdsa::Signature::new(&multipurpose_bytes, &secret_key).unwrap()
}

/// Coinbase transaction with random block height
fn coinbase_tx<R: rand::RngCore + ?Sized>(
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::<i64, _>(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
}

0 comments on commit d3dd28d

Please sign in to comment.