Skip to content

Commit

Permalink
fix: Impl requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
od-hunter committed Sep 6, 2024
1 parent 7728336 commit adebfc9
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/validation/coinbase.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,23 @@ pub fn validate_coinbase(
let sig_len = sig.len();

// Ensure byte length greater than 2 and less 100
assert(sig_len > 2 && sig_len < 100, 'bad sig script length');
if sig_len < 2 || sig_len > 100 {
return Result::Err("bad sig script length");
}

// Ensure script starts with the current block height
//
// First byte is number of bytes in the number (will be 0x03 on mainnet for the next
// 150 or so years with 223-1 blocks), following bytes are little-endian representation
// of the number
if sig[0] != 3 {
return Result::Err("Invalid number of bytes");
}

assert(sig[0] == 3, 'Invalid number of bytes');
let result = sig[1].into() + sig[2].into() * 256_u32 + sig[3].into() * 65536_u32;

assert(result == block_height.into(), 'wrong block height');
if result != block_height.into() {
return Result::Err("wrong block height");
}
}

// TODO: validate BIP-141 witness field
Expand Down Expand Up @@ -340,7 +345,6 @@ mod tests {
}

#[test]
#[should_panic(expected: ('bad sig script length',))]
fn test_validate_coinbase_BIP_34_sig_script_less_than_2() {
let tx = Transaction {
version: 1,
Expand Down Expand Up @@ -371,10 +375,9 @@ mod tests {
let total_fees = 5000000000_u64;
let block_height = 856563;

validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap();
validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap_err();
}
#[test]
#[should_panic(expected: ('bad sig script length',))]
fn test_validate_coinbase_BIP_34_sig_script_greater_than_100() {
let tx = Transaction {
version: 1,
Expand Down Expand Up @@ -407,11 +410,10 @@ mod tests {
let total_fees = 5000000000_u64;
let block_height = 856563;

validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap();
validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap_err();
}

#[test]
#[should_panic(expected: ('wrong block height',))]
fn test_validate_coinbase_BIP_34_sig_script_with_wrong_block_height() {
let tx = Transaction {
version: 1,
Expand Down Expand Up @@ -444,11 +446,10 @@ mod tests {
let total_fees = 5000000000_u64;
let block_height = 856563;

validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap();
validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap_err();
}

#[test]
#[should_panic(expected: ('Invalid number of bytes',))]
fn test_validate_coinbase_BIP_34_sig_script_with_Invalid_number_of_bytes() {
let tx = Transaction {
version: 1,
Expand Down Expand Up @@ -481,7 +482,7 @@ mod tests {
let total_fees = 5000000000_u64;
let block_height = 856563;

validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap();
validate_coinbase(@tx, total_fees, block_height, Default::default()).unwrap_err();
}

#[test]
Expand Down

0 comments on commit adebfc9

Please sign in to comment.