Skip to content

Commit

Permalink
test cases for invalid input sequence (relative locktime) (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarak23 authored Sep 16, 2024
1 parent 151831c commit e7ebebe
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions packages/consensus/src/validation/locktime.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,88 @@ mod tests {
validate_relative_locktime(@input, 603450, 1573555667).unwrap();
}

#[test]
fn test_relative_locktime_block_height_lt_relative_locktime() {
let input = TxIn {
script: @from_hex(""),
sequence: 144, // Lower sequence number with locktime enabled (block-based locktime)
previous_output: OutPoint {
txid: hex_to_hash_rev(
"0000000000000000000000000000000000000000000000000000000000000000"
),
vout: 0,
data: TxOut { value: 188442, ..Default::default() },
block_height: 603018, // Initial block height
block_time: 1573324462,
is_coinbase: false,
},
witness: array![].span(),
};

// Pass a lower block height than required by the relative locktime
let block_height = 603019;
let block_time = 1573401225;
let result = validate_relative_locktime(@input, block_height, block_time);
result.unwrap_err();
}

#[test]
fn test_relative_locktime_block_time_lt_relative_locktime() {
// txid 12fa403cb22bf08c4c5542cc00673495a0c54c9cc8181bea850a12d40d7593a2
// input 0
// note: only relevant fields are initialized
let input = TxIn {
script: @from_hex(""),
sequence: 4194311, // Time-based relative locktime (0x400007 = SEQUENCE_LOCKTIME_TYPE_FLAG + value)
previous_output: OutPoint {
txid: hex_to_hash_rev(
"0000000000000000000000000000000000000000000000000000000000000000"
),
vout: 0,
data: TxOut { value: 13671, ..Default::default() },
block_height: 603434,
block_time: 1573549241, // Initial block time
is_coinbase: false,
},
witness: array![].span(),
};

// Pass a lower block_time than required by the relative locktime
let block_height = 603450;
let block_time = 1573549250;

// This should fail because the current block time does not satisfy the relative locktime
let result = validate_relative_locktime(@input, block_height, block_time);
result.unwrap_err();
}

#[test]
fn test_relative_locktime_enabled_lt_relative_locktime() {
let input = TxIn {
script: @from_hex(""),
sequence: 144, // Relative locktime enabled (no SEQUENCE_LOCKTIME_DISABLE_FLAG)
previous_output: OutPoint {
txid: hex_to_hash_rev(
"0000000000000000000000000000000000000000000000000000000000000000"
),
vout: 0,
data: TxOut { value: 100, ..Default::default() },
block_height: 100, // Previous block height
block_time: 1600000000, // Previous block time
is_coinbase: false,
},
witness: array![].span(),
};

let current_block_height = 100;
let current_block_time = 1600000000;

// This should fail because relative locktime is enabled and the conditions do not satisfy
let result = validate_relative_locktime(@input, current_block_height, current_block_time);
result.unwrap_err();
}


#[test]
fn test_absolute_locktime_block_height() {
// Absolute locktime: block height
Expand Down

0 comments on commit e7ebebe

Please sign in to comment.