Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions lib/validator/task/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ pub(in crate::validator) enum HandleM5M6 {
#[error("Old Ctip for sidechain {} is unspent", .sidechain_number.0)]
#[fatal(false)]
OldCtipUnspent { sidechain_number: SidechainNumber },
/// BIP 300: a transaction that spends a treasury UTXO as one of its inputs
/// and does not create a new treasury UTXO as one of its outputs is
/// invalid.
#[error(
"treasury UTXO for sidechain {} is spent without creating a new treasury UTXO",
.sidechain_number.0
)]
#[fatal(false)]
TreasurySpentWithoutNewCtip { sidechain_number: SidechainNumber },
#[error("cannot deposit or withdraw zero sats from sidechain")]
#[fatal(false)]
ZeroDiff,
Expand Down
60 changes: 60 additions & 0 deletions lib/validator/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,17 @@ impl BlockHandler<'_> {
new_ctips
};
let n_new_ctips = new_ctips.len();
// BIP 300: a tx that spends a treasury UTXO must also create a new
// treasury UTXO for that sidechain. Without this check, a spend of the
// anyone-can-spend OP_DRIVECHAIN treasury that creates no replacement
// output is silently ignored and drains the slot.
for sidechain_number in ctip_spends.keys() {
if !new_ctips.contains_key(sidechain_number) {
return Err(error::HandleM5M6::TreasurySpentWithoutNewCtip {
sidechain_number: *sidechain_number,
});
}
}
// Check that old ctips are spent
let mut res = Option::<DepositsOrSuccessfulWithdrawal>::None;
for (sidechain_number, new_ctip) in new_ctips {
Expand Down Expand Up @@ -2169,6 +2180,55 @@ mod tests {
Ok(())
}

/// BIP 300: a tx that spends a treasury UTXO without creating a new
/// treasury UTXO is invalid. OP_DRIVECHAIN is anyone-can-spend, so without
/// this rule anyone could drain the treasury.
#[test]
fn handle_m5_m6_treasury_spent_without_new_ctip_is_invalid() -> Result<()> {
let (_dir, dbs) = create_test_dbs()?;
let mut rwtxn = dbs.write_txn().into_diagnostic()?;
let sc = SidechainNumber(1);
let treasury_outpoint = OutPoint {
txid: Txid::from_byte_array([0x11; 32]),
vout: 0,
};
dbs.active_sidechains
.put_ctip(
&mut rwtxn,
sc,
&Ctip {
outpoint: treasury_outpoint,
value: Amount::from_sat(5_000),
},
)
.into_diagnostic()?;

// Spend the treasury UTXO and pay it all to a non-treasury output.
let theft_tx = Transaction {
version: bitcoin::transaction::Version::TWO,
lock_time: bitcoin::locktime::absolute::LockTime::ZERO,
input: vec![TxIn {
previous_output: treasury_outpoint,
..TxIn::default()
}],
output: vec![TxOut {
script_pubkey: ScriptBuf::new(),
value: Amount::from_sat(5_000),
}],
};

let err = test_handler(&dbs)
.handle_m5_m6(&rwtxn, Cow::Borrowed(&theft_tx))
.expect_err("spending a treasury without creating a new one must be invalid");
assert!(matches!(
err,
error::HandleM5M6::TreasurySpentWithoutNewCtip { sidechain_number }
if sidechain_number == sc
));
assert!(!err.is_fatal());
Ok(())
}

// ── handle_m1 ──

#[test]
Expand Down
Loading