8000 chain: Make sure we validate taproot by Davidson-Souza · Pull Request #418 · vinteumorg/Floresta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chain: Make sure we validate taproot #418

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
10 changes: 8 additions & 2 deletions crates/floresta-chain/src/pruned_utreexo/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ impl<PersistedState: ChainStore> ChainState<PersistedState> {
}
Ok(())
}

#[cfg(feature = "bitcoinconsensus")]
/// Returns the validation flags, given the current block height
fn get_validation_flags(&self, height: u32, hash: BlockHash) -> c_uint {
let chain_params = &read_lock!(self).consensus.parameters;

if let Some(flag) = chain_params.exceptions.get(&hash) {
return *flag;
}
Expand All @@ -179,20 +179,26 @@ impl<PersistedState: ChainStore> ChainState<PersistedState> {
// mainnet.
// For simplicity, always leave P2SH+WITNESS+TAPROOT on except for the two
// violating blocks.
let mut flags = bitcoinconsensus::VERIFY_P2SH | bitcoinconsensus::VERIFY_WITNESS;
let mut flags = bitcoinconsensus::VERIFY_P2SH
| bitcoinconsensus::VERIFY_WITNESS
| bitcoinconsensus::VERIFY_TAPROOT;

if height >= chain_params.params.bip65_height {
flags |= bitcoinconsensus::VERIFY_CHECKLOCKTIMEVERIFY;
}

if height >= chain_params.params.bip66_height {
flags |= bitcoinconsensus::VERIFY_DERSIG;
}

if height >= chain_params.csv_activation_height {
flags |= bitcoinconsensus::VERIFY_CHECKSEQUENCEVERIFY;
}

if height >= chain_params.segwit_activation_height {
flags |= bitcoinconsensus::VERIFY_NULLDUMMY;
}

flags
}

Expand Down
59 changes: 57 additions & 2 deletions crates/floresta-chain/src/pruned_utreexo/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ extern crate alloc;
use core::ffi::c_uint;

use bitcoin::block::Header as BlockHeader;
#[cfg(feature = "bitcoinconsensus")]
use bitcoin::consensus::serialize;
use bitcoin::hashes::sha256;
use bitcoin::hashes::Hash;
use bitcoin::Block;
Expand All @@ -18,6 +20,8 @@ use bitcoin::Transaction;
use bitcoin::TxIn;
use bitcoin::TxOut;
use bitcoin::Txid;
#[cfg(feature = "bitcoinconsensus")]
use bitcoinconsensus::Utxo;
use floresta_common::prelude::*;
use rustreexo::accumulator::proof::Proof;
use rustreexo::accumulator::stump::Stump;
Expand Down Expand Up @@ -168,9 +172,35 @@ impl Consensus {
// Verify the tx script
#[cfg(feature = "bitcoinconsensus")]
if _verify_script {
transaction
.verify_with_flags(|outpoint| utxos.remove(outpoint), _flags)
let tx = serialize(&transaction);

let mut spent_utxos = Vec::new();
let mut spent_scripts = Vec::new();

for input in transaction.input.iter() {
let spent_output = utxos
.remove(&input.previous_output)
.ok_or_else(|| tx_err!(txid, UtxoNotFound, input.previous_output))?;

spent_scripts.push((spent_output.script_pubkey, spent_output.value.to_sat()));
spent_utxos.push(Utxo {
script_pubkey_len: spent_scripts.last().unwrap().0.len() as u32,
script_pubkey: spent_scripts.last().unwrap().0.as_bytes().as_ptr(),
value: spent_output.value.to_sat() as i64,
});
}

for (input_index, (script, amount)) in spent_scripts.iter().enumerate() {
bitcoinconsensus::verify_with_flags(
script.as_bytes(),
*amount,
tx.as_slice(),
Some(&spent_utxos),
input_index,
_flags,
)
.map_err(|e| tx_err!(txid, ScriptValidationError, e.to_string()))?;
}
};

Ok((in_value, out_value))
Expand Down Expand Up @@ -543,4 +573,29 @@ mod tests {
e => panic!("Expected a TransactionError, but got: {:?}", e),
}
}

#[cfg(feature = "bitcoinconsensus")]
#[test]
fn test_taproot_transaction() {
let tx: Transaction = deserialize_hex("0200000000010133adf094f0f0038a11d9246fd1dfc7587740c49b8eabc1a67d33b82072553d330000000000ffffffff085802000000000000225120d43ef57ea573e2f44d62b4b3a50b9ce7100e82da6a45c032037b03187a6666bb5802000000000000225120d43ef57ea573e2f44d62b4b3a50b9ce7100e82da6a45c032037b03187a6666bb5802000000000000225120d43ef57ea573e2f44d62b4b3a50b9ce7100e82da6a45c032037b03187a6666bb5802000000000000225120d43ef57ea573e2f44d62b4b3a50b9ce7100e82da6a45c032037b03187a6666bb5802000000000000225120d43ef57ea573e2f44d62b4b3a50b9ce7100e82da6a45c032037b03187a6666bb5802000000000000225120d43ef57ea573e2f44d62b4b3a50b9ce7100e82da6a45c032037b03187a6666bb5802000000000000225120d43ef57ea573e2f44d62b4b3a50b9ce7100e82da6a45c032037b03187a6666bb08b3fa0200000000225120d43ef57ea573e2f44d62b4b3a50b9ce7100e82da6a45c032037b03187a6666bb0140f117ed319bcb2c554b494bf59a331389e9b2da8db7524e5984cfdd19b375be3e5321f6be671d371a6b54e23c88e208fb80a5f8cd79719a3b1d62dbb79758b7b100000000").unwrap();

let prevout = TxOut {
value: Amount::from_sat(50000000),
script_pubkey: deserialize_hex(
"225120d43ef57ea573e2f44d62b4b3a50b9ce7100e82da6a45c032037b03187a6666bb",
)
.unwrap(),
};

let mut utxos = HashMap::new();
utxos.insert(tx.input[0].previous_output, prevout);

Consensus::verify_transaction(
&tx,
&mut utxos,
true,
bitcoinconsensus::VERIFY_TAPROOT | bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT,
)
.unwrap();
}
}
Loading
0