Skip to content

Commit

Permalink
- fix: set wTXID for coinbase transaction to zeros
Browse files Browse the repository at this point in the history
- fix  tests
- clean up
  • Loading branch information
manlikeHB committed Sep 13, 2024
1 parent 03a08b9 commit cf8b7c1
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 34 deletions.
4 changes: 3 additions & 1 deletion scripts/data/regenerate_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ light_test_cases=(
2015 # First new epoch (2016)
24834 # Block containing first off ramp tx from Martti Malmi (24835)
32255 # First target adjustment (32256)
542212
57042 # Block containing pizza tx (57043)
150012 # Small Block (150013)
209999 # First halving block (210000)
478557 # Bitcoin Cash hard fork block (478558)
481823 # Segwit soft fork block (481824)
491406 # Bitcoin Gold hard fork block (491407)
542212 # (542213)
553723 # (553724)
629999 # Third halving block (630000)
709631 # Taproot soft fort block (709632)
757738 # Block with witness (757739)
Expand All @@ -37,6 +38,7 @@ light_test_cases=(
full_test_cases=(
169 # Block containing first P2P tx to Hal Finney (170)
542212
553723 # (553724)
757738 # Block with witness (757739)
)

Expand Down
9 changes: 8 additions & 1 deletion src/validation/block.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ pub fn compute_and_validate_tx_data(
let tx_bytes_segwit = @tx.encode_with_witness(tx_bytes_legacy);

let txid = double_sha256_byte_array(tx_bytes_legacy);
let wtxid = double_sha256_byte_array(tx_bytes_segwit);
let mut wtxid = double_sha256_byte_array(tx_bytes_segwit);

/// The wTXID for the coinbase transaction must be set to all zeros. This is because it's
/// eventually going to contain the commitment inside it
/// see https://learnmeabitcoin.com/technical/transaction/wtxid/#commitment
if i == 0 {
wtxid = Default::default();
}

// tx_byte_segwit represents all the bytes in the transaction, so the bytes in the segwit
// fields are tx_byte_segwit - tx_byte_legacy.
Expand Down
42 changes: 10 additions & 32 deletions src/validation/coinbase.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

use crate::types::transaction::{Transaction, TxIn, TxOut};
use crate::utils::{
bit_shifts::shr, hash::{Digest, DigestIntoByteArray}, hex::to_hex,
sha256::{double_sha256_byte_array}
bit_shifts::shr, hash::{Digest, DigestIntoByteArray}, sha256::{double_sha256_byte_array}
};


Expand Down Expand Up @@ -51,10 +50,8 @@ pub fn validate_coinbase(

// validate BIP-141 segwit output
if *tx.is_segwit {
// calculate wtxid commitment
let wtxid_commitment = calculate_wtxid_commitment(wtxid_root);

validate_segwit_output(*tx.outputs, wtxid_commitment)?;
// calculate expected wtxid commitment and validate segwit output
validate_segwit_output(*tx.outputs, calculate_wtxid_commitment(wtxid_root))?;
}
}

Expand Down Expand Up @@ -136,24 +133,18 @@ fn compute_block_reward(block_height: u32) -> u64 {

/// Calculate wtxid commitment
fn calculate_wtxid_commitment(wtxid_root: Digest) -> Digest {
/// Big endian
// construct witness reserved value
// 0000000000000000000000000000000000000000000000000000000000000000
let mut witness_value_byte: ByteArray = "";
witness_value_byte.append_word(WITNESS_VALUE, 32);

// convert wtxid_root to ByteArray
let wtxid_root_bytes: ByteArray = wtxid_root.into();

let mut res = ByteArrayTrait::concat(@wtxid_root_bytes.rev(), @witness_value_byte);
// concat (witness root hash | witness reserved value)
let mut res = ByteArrayTrait::concat(@wtxid_root_bytes, @witness_value_byte);

double_sha256_byte_array(@res)
// /// Little endian
// let mut witness_value_byte: ByteArray = "";
// witness_value_byte.append_word(WITNESS_VALUE, 32);

// let wtxid_root_bytes: ByteArray = wtxid_root.into();

// let mut res = ByteArrayTrait::concat(@wtxid_root_bytes, @witness_value_byte);

// double_sha256_byte_array(@res)
}

fn validate_segwit_output(
Expand Down Expand Up @@ -189,12 +180,6 @@ fn validate_segwit_output(
if expected_wtxid_commitment == extracted_wtxid_commitment {
is_wtxid_commitment_present = true;
break;
} else {
println!(
"commitments: {} - {}",
to_hex(@expected_wtxid_commitment),
to_hex(@extracted_wtxid_commitment)
);
}
}
};
Expand Down Expand Up @@ -597,7 +582,6 @@ mod tests {
]
.span();

/// little endian
let wtxid_commitment: Digest = hex_to_hash_rev(
"15e787d38637d8ed668d5e1e573d2241739c3315190220a8d89ca27b63e80265"
);
Expand All @@ -607,16 +591,10 @@ mod tests {

#[test]
fn test_calculate_wtxid_commitment() {
/// big endian
let witness_root_hash: Digest = hex_to_hash_rev(
"dbee9a868a8caa2a1ddf683af1642a88dfb7ac7ce3ecb5d043586811a41fdbf2"
"f2db1fa411685843d0b5ece37cacb7df882a64f13a68df1d2aaa8c8a869aeedb"
);

/// little endian
// let witness_root_hash: Digest = hex_to_hash_rev(
// "f2db1fa411685843d0b5ece37cacb7df882a64f13a68df1d2aaa8c8a869aeedb"
// );

let expected_wtxid_commitment = from_hex(
"6502e8637ba29cd8a820021915339c7341223d571e5e8d66edd83786d387e715"
);
Expand Down Expand Up @@ -690,7 +668,7 @@ mod tests {
let block_height = 500_000;

let wtxid_root_hash: Digest = hex_to_hash_rev(
"dbee9a868a8caa2a1ddf683af1642a88dfb7ac7ce3ecb5d043586811a41fdbf2"
"f2db1fa411685843d0b5ece37cacb7df882a64f13a68df1d2aaa8c8a869aeedb"
);

validate_coinbase(@tx, total_fees, block_height, wtxid_root_hash).unwrap();
Expand Down
214 changes: 214 additions & 0 deletions tests/data/full_553723.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
{
"chain_state": {
"block_height": 553723,
"total_work": "1349054508381181651674186396",
"best_block_hash": "0000000000000000000567a96e76bb24f8e954d24a981af937fb566f9624d70e",
"current_target": "4774638159061819979596346127394133648234752261950013440",
"epoch_start_time": 1543838368,
"prev_timestamps": [
1544748510,
1544748579,
1544748881,
1544749065,
1544749190,
1544749434,
1544750432,
1544750860,
1544750935,
1544751014,
1544751144
]
},
"blocks": [
{
"header": {
"version": 536870912,
"time": 1544751216,
"bits": 389142908,
"nonce": 430846328
},
"data": {
"variant_id": 1,
"transactions": [
{
"version": 1,
"is_segwit": true,
"inputs": [
{
"script": "0x03fc72081b4d696e656420627920416e74506f6f6c31384e00a103205c130870fabe6d6d0782f935d81e4948c75d613779fe7c53eab4c35616d073755df5fe49f3cdb16d0400000000000000cc050000d38d0200",
"sequence": 4294967295,
"previous_output": {
"txid": "0000000000000000000000000000000000000000000000000000000000000000",
"vout": 4294967295,
"data": {
"value": 0,
"pk_script": "0x",
"cached": false
},
"block_height": 0,
"block_time": 0,
"is_coinbase": false
},
"witness": [
"0x0000000000000000000000000000000000000000000000000000000000000000"
]
}
],
"outputs": [
{
"value": 1250005141,
"pk_script": "0x76a914edf10a7fac6b32e24daa5305c723f3de58db1bc888ac",
"cached": false
},
{
"value": 0,
"pk_script": "0x6a24aa21a9ed6502e8637ba29cd8a820021915339c7341223d571e5e8d66edd83786d387e715",
"cached": false
},
{
"value": 0,
"pk_script": "0x52534b424c4f434b3abcb062d40bf073e3eac877f2b1bad4eddc4f8d882ab014a8d2278563f832e95b",
"cached": false
}
],
"lock_time": 0
},
{
"version": 1,
"is_segwit": true,
"inputs": [
{
"script": "0x16001473e8ab17ce626fc190105f583f0073c76a6b37d9",
"sequence": 4294967295,
"previous_output": {
"txid": "bca6b51a545e7ba7c8c65c93a496b38c52d2dd17318e1ee515928514cd52e9d5",
"vout": 0,
"data": {
"value": 7520877,
"pk_script": "0xa914f351628496269cf29ce89a61a0b549641f38731e87",
"cached": false
},
"block_height": 553723,
"block_time": 1544751144,
"is_coinbase": false
},
"witness": [
"0x304402200fd489534abd79ef87d0c2a3e5db3d3e77b91c2c19d76421f7b61ce6cf3789e202206fa79d2e723462694880b37a93e4155a11b4a533c3a2c82f5d917f9cb30b134101",
"0x034d12143057d8154b0a0044548861a29fb93ec1e1da607c9661c46a1dfef6bf6f"
]
}
],
"outputs": [
{
"value": 218247,
"pk_script": "0x0014d3421512c645bcfa2af9743a18731001f0173904",
"cached": false
},
{
"value": 7300000,
"pk_script": "0xa914379056a3f6d10caa54ee8ef27131c348a7ccf31f87",
"cached": false
}
],
"lock_time": 0
},
{
"version": 1,
"is_segwit": true,
"inputs": [
{
"script": "0x",
"sequence": 4294967295,
"previous_output": {
"txid": "34c5844262f47603acc025d9cba6a34bae74120e4bb7b2c874471671a3662589",
"vout": 0,
"data": {
"value": 14467222,
"pk_script": "0x0014957970e18969de99dd4b9aed13b77811006a0b40",
"cached": false
},
"block_height": 553720,
"block_time": 1544750860,
"is_coinbase": false
},
"witness": [
"0x30440220212ed58e54c2fbf219fb6db8c4ea4854e62d56667ff1a74da91cd27fc62e10c4022018923a537ba525f68e64ce0b70a36b4839b55aecd791dab8d85052c69594f1f501",
"0x034e6622f089ad175ab1aca77487577e350fb00413a7420250d60422a2e0cc40a9"
]
}
],
"outputs": [
{
"value": 2427272,
"pk_script": "0x0014bed752ca565818fee0c7be61713dec3b9b4841e2",
"cached": false
},
{
"value": 12037687,
"pk_script": "0xa9144c13003630a952f25cfc24728b69e5b65dc7fc6787",
"cached": false
}
],
"lock_time": 0
},
{
"version": 2,
"is_segwit": false,
"inputs": [
{
"script": "0x483045022100da31f58b0e2ec9da2e8fadaa38e7b1d0084433fc62160e02c7b7016e6e31477f02206a0462df44e82869798337f5523286972c93d66d523f137c0db5a4baa6e6930d0121034b11147333a38bfb9466c60bec7ce3dc608a4fd2315227fae928a7c10556e9a5",
"sequence": 4294967294,
"previous_output": {
"txid": "13cc7b4ba4dc602ecb4c2580bcceb7276ef68d82807790952ef1206651fbec6f",
"vout": 0,
"data": {
"value": 1383599,
"pk_script": "0x76a9146c5e8d9e82ea507e34746a36471a66f6e2e1329e88ac",
"cached": false
},
"block_height": 549207,
"block_time": 1541643907,
"is_coinbase": false
},
"witness": []
}
],
"outputs": [
{
"value": 120000,
"pk_script": "0xa91401f7b90f5653e8c2a45093c3b2168cfa7e46811e87",
"cached": false
},
{
"value": 1263351,
"pk_script": "0x76a914b95067af2d4ebef77ddeae544963fa04df66bb0a88ac",
"cached": false
}
],
"lock_time": 553722
}
]
}
}
],
"expected": {
"block_height": 553724,
"total_work": "1349078759871112371043869813",
"best_block_hash": "0000000000000000002849bd7ea6df81fa2f07652af0600ffa0f2b0bc47d736c",
"current_target": "4774638159061819979596346127394133648234752261950013440",
"epoch_start_time": 1543838368,
"prev_timestamps": [
1544748579,
1544748881,
1544749065,
1544749190,
1544749434,
1544750432,
1544750860,
1544750935,
1544751014,
1544751144,
1544751216
]
}
}
Loading

0 comments on commit cf8b7c1

Please sign in to comment.