Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Proper coinbase generation #253

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
netrome marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading