Skip to content

Commit

Permalink
Provide block height for tx disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
azarovh committed Feb 26, 2025
1 parent 317a14b commit b1787fa
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 112 deletions.
1 change: 1 addition & 0 deletions chainstate/src/detail/chainstateref/in_memory_reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl<'a, S: BlockchainStorageRead, V: TransactionVerificationStrategy> Chainstat
TransactionVerifier::new,
&tx_verifier,
self.chain_config,
&cur_index,
&block.into(),
)?
.consume()?;
Expand Down
9 changes: 7 additions & 2 deletions chainstate/src/detail/chainstateref/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,11 +1206,16 @@ impl<S: BlockchainStorageWrite, V: TransactionVerificationStrategy> ChainstateRe
}

#[log_error]
fn disconnect_transactions(&mut self, block: &WithId<Block>) -> Result<(), BlockError> {
fn disconnect_transactions(
&mut self,
block_index: &BlockIndex,
block: &WithId<Block>,
) -> Result<(), BlockError> {
let cached_inputs = self.tx_verification_strategy.disconnect_block(
TransactionVerifier::new,
&*self,
self.chain_config,
block_index,
block,
)?;
let cached_inputs = cached_inputs.consume()?;
Expand Down Expand Up @@ -1302,7 +1307,7 @@ impl<S: BlockchainStorageWrite, V: TransactionVerificationStrategy> ChainstateRe
.expect("Best block index not present in the database");
let block = self.get_block_from_index(&block_index)?.expect("Inconsistent DB");
// Disconnect transactions
self.disconnect_transactions(&block.into())?;
self.disconnect_transactions(&block_index, &block.into())?;
self.db_tx.set_best_block_id(block_index.prev_block_id())?;
// Disconnect block
self.db_tx.del_block_id_at_height(&block_index.block_height())?;
Expand Down
19 changes: 11 additions & 8 deletions chainstate/src/detail/tx_verification_strategy/default_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ use constraints_value_accumulator::AccumulatedFee;
use orders_accounting::OrdersAccountingView;
use pos_accounting::PoSAccountingView;
use tokens_accounting::TokensAccountingView;
use tx_verifier::{
transaction_verifier::{
error::ConnectTransactionError, storage::TransactionVerifierStorageRef,
TransactionSourceForConnect, TransactionVerifier,
},
TransactionSource,
use tx_verifier::transaction_verifier::{
error::ConnectTransactionError, storage::TransactionVerifierStorageRef,
TransactionSourceWithHeight, TransactionVerifier,
};
use utils::{shallow_clone::ShallowClone, tap_log::TapLog};
use utxo::UtxosView;
Expand Down Expand Up @@ -76,7 +73,7 @@ impl TransactionVerificationStrategy for DefaultTransactionVerificationStrategy
.try_fold(AccumulatedFee::new(), |total, tx| {
let fee = tx_verifier
.connect_transaction(
&TransactionSourceForConnect::Chain {
&TransactionSourceWithHeight::Chain {
new_block_index: block_index,
},
tx,
Expand Down Expand Up @@ -120,6 +117,7 @@ impl TransactionVerificationStrategy for DefaultTransactionVerificationStrategy
tx_verifier_maker: M,
storage_backend: S,
chain_config: C,
block_index: &BlockIndex,
block: &WithId<Block>,
) -> Result<TransactionVerifier<C, S, U, A, T, O>, ConnectTransactionError>
where
Expand All @@ -141,7 +139,12 @@ impl TransactionVerificationStrategy for DefaultTransactionVerificationStrategy
.iter()
.rev()
.try_for_each(|tx| {
tx_verifier.disconnect_transaction(&TransactionSource::Chain(block.get_id()), tx)
tx_verifier.disconnect_transaction(
&TransactionSourceWithHeight::Chain {
new_block_index: block_index,
},
tx,
)
})
.log_err()?;

Expand Down
1 change: 1 addition & 0 deletions chainstate/src/detail/tx_verification_strategy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub trait TransactionVerificationStrategy: Sized + Send {
tx_verifier_maker: M,
storage_backend: S,
chain_config: C,
block_index: &BlockIndex,
block: &WithId<Block>,
) -> Result<TransactionVerifier<C, S, U, A, T, O>, ConnectTransactionError>
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ use constraints_value_accumulator::AccumulatedFee;
use orders_accounting::OrdersAccountingView;
use pos_accounting::PoSAccountingView;
use tokens_accounting::TokensAccountingView;
use tx_verifier::{
transaction_verifier::{
error::ConnectTransactionError, flush::flush_to_storage,
storage::TransactionVerifierStorageRef, TransactionSourceForConnect, TransactionVerifier,
},
TransactionSource,
use tx_verifier::transaction_verifier::{
error::ConnectTransactionError, flush::flush_to_storage,
storage::TransactionVerifierStorageRef, TransactionSourceWithHeight, TransactionVerifier,
};
use utils::{shallow_clone::ShallowClone, tap_log::TapLog};
use utxo::UtxosView;
Expand Down Expand Up @@ -81,7 +78,7 @@ impl TransactionVerificationStrategy for DisposableTransactionVerificationStrate
let mut tx_verifier = base_tx_verifier.derive_child();
let fee = tx_verifier
.connect_transaction(
&TransactionSourceForConnect::Chain {
&TransactionSourceWithHeight::Chain {
new_block_index: block_index,
},
tx,
Expand Down Expand Up @@ -128,6 +125,7 @@ impl TransactionVerificationStrategy for DisposableTransactionVerificationStrate
tx_verifier_maker: M,
storage_backend: S,
chain_config: C,
block_index: &BlockIndex,
block: &WithId<Block>,
) -> Result<TransactionVerifier<C, S, U, A, T, O>, ConnectTransactionError>
where
Expand Down Expand Up @@ -155,7 +153,12 @@ impl TransactionVerificationStrategy for DisposableTransactionVerificationStrate
let mut tx_verifier = base_tx_verifier.derive_child();

tx_verifier
.disconnect_transaction(&TransactionSource::Chain(block.get_id()), tx)
.disconnect_transaction(
&TransactionSourceWithHeight::Chain {
new_block_index: block_index,
},
tx,
)
.log_err()?;

let consumed_cache = tx_verifier.consume()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ use pos_accounting::PoSAccountingView;
use randomness::{Rng, RngCore};
use test_utils::random::{make_seedable_rng, Seed};
use tokens_accounting::TokensAccountingView;
use tx_verifier::{
transaction_verifier::{
error::ConnectTransactionError, flush::flush_to_storage,
storage::TransactionVerifierStorageRef, TransactionSourceForConnect, TransactionVerifier,
TransactionVerifierDelta,
},
TransactionSource,
use tx_verifier::transaction_verifier::{
error::ConnectTransactionError, flush::flush_to_storage,
storage::TransactionVerifierStorageRef, TransactionSourceWithHeight, TransactionVerifier,
TransactionVerifierDelta,
};
use utils::{shallow_clone::ShallowClone, tap_log::TapLog};
use utxo::UtxosView;
Expand Down Expand Up @@ -105,6 +102,7 @@ impl TransactionVerificationStrategy for RandomizedTransactionVerificationStrate
tx_verifier_maker: M,
storage_backend: S,
chain_config: C,
block_index: &BlockIndex,
block: &WithId<Block>,
) -> Result<TransactionVerifier<C, S, U, A, T, O>, ConnectTransactionError>
where
Expand All @@ -117,8 +115,13 @@ impl TransactionVerificationStrategy for RandomizedTransactionVerificationStrate
M: TransactionVerifierMakerFn<C, S, U, A, T, O>,
<S as utxo::UtxosStorageRead>::Error: From<U::Error>,
{
let mut tx_verifier =
self.disconnect_with_base(tx_verifier_maker, storage_backend, chain_config, block)?;
let mut tx_verifier = self.disconnect_with_base(
tx_verifier_maker,
storage_backend,
chain_config,
block_index,
block,
)?;

tx_verifier.set_best_block(block.prev_block_id());

Expand Down Expand Up @@ -173,7 +176,7 @@ impl RandomizedTransactionVerificationStrategy {
// connect transactable using current verifier

let fee = tx_verifier.connect_transaction(
&TransactionSourceForConnect::Chain {
&TransactionSourceWithHeight::Chain {
new_block_index: block_index,
},
&block.transactions()[tx_num],
Expand Down Expand Up @@ -239,7 +242,7 @@ impl RandomizedTransactionVerificationStrategy {
} else {
// connect transactable using current verifier
let fee = tx_verifier.connect_transaction(
&TransactionSourceForConnect::Chain {
&TransactionSourceWithHeight::Chain {
new_block_index: block_index,
},
&block.transactions()[tx_num],
Expand All @@ -261,6 +264,7 @@ impl RandomizedTransactionVerificationStrategy {
tx_verifier_maker: M,
storage_backend: S,
chain_config: C,
block_index: &BlockIndex,
block: &WithId<Block>,
) -> Result<TransactionVerifier<C, S, U, A, T, O>, ConnectTransactionError>
where
Expand All @@ -282,7 +286,7 @@ impl RandomizedTransactionVerificationStrategy {
if self.rng.lock().unwrap().gen::<bool>() {
// derive a new cache
let (consumed_cache, new_tx_index) =
self.disconnect_with_derived(&tx_verifier, block, tx_num)?;
self.disconnect_with_derived(&tx_verifier, block_index, block, tx_num)?;

flush_to_storage(&mut tx_verifier, consumed_cache)
.map_err(ConnectTransactionError::from)?;
Expand All @@ -291,7 +295,9 @@ impl RandomizedTransactionVerificationStrategy {
// disconnect transactable using current verifier
tx_verifier
.disconnect_transaction(
&TransactionSource::Chain(block.get_id()),
&TransactionSourceWithHeight::Chain {
new_block_index: block_index,
},
&block.transactions()[tx_num as usize],
)
.log_err()?;
Expand All @@ -305,6 +311,7 @@ impl RandomizedTransactionVerificationStrategy {
fn disconnect_with_derived<C, S, U, A, T, O>(
&self,
base_tx_verifier: &TransactionVerifier<C, S, U, A, T, O>,
block_index: &BlockIndex,
block: &WithId<Block>,
mut tx_num: i32,
) -> Result<(TransactionVerifierDelta, i32), ConnectTransactionError>
Expand All @@ -325,7 +332,9 @@ impl RandomizedTransactionVerificationStrategy {
} else {
// disconnect transactable using current verifier
tx_verifier.disconnect_transaction(
&TransactionSource::Chain(block.get_id()),
&TransactionSourceWithHeight::Chain {
new_block_index: block_index,
},
&block.transactions()[tx_num as usize],
)?;
tx_num -= 1;
Expand Down
18 changes: 9 additions & 9 deletions chainstate/test-suite/src/tests/mempool_output_timelock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use rstest::rstest;
use test_utils::random::{make_seedable_rng, Seed};
use tx_verifier::{
error::{InputCheckError, ScriptError, TimelockError},
transaction_verifier::{TransactionSourceForConnect, TransactionVerifier},
transaction_verifier::{TransactionSourceWithHeight, TransactionVerifier},
};

fn setup(rng: &mut (impl Rng + CryptoRng)) -> (ChainConfig, InMemoryStorageWrapper, TestFramework) {
Expand Down Expand Up @@ -73,7 +73,7 @@ fn output_lock_until_height(#[case] seed: Seed) {
let best_block_index = tf.best_block_index();
assert_eq!(
verifier.connect_transaction(
&TransactionSourceForConnect::for_mempool(&best_block_index),
&TransactionSourceWithHeight::for_mempool(&best_block_index),
&spend_locked_tx,
&BlockTimestamp::from_time(tf.current_time()),
),
Expand All @@ -97,7 +97,7 @@ fn output_lock_until_height(#[case] seed: Seed) {
let best_block_index = tf.best_block_index();
verifier
.connect_transaction(
&TransactionSourceForConnect::for_mempool(&best_block_index),
&TransactionSourceWithHeight::for_mempool(&best_block_index),
&spend_locked_tx,
&BlockTimestamp::from_time(tf.current_time()),
)
Expand Down Expand Up @@ -138,7 +138,7 @@ fn output_lock_for_block_count(#[case] seed: Seed) {
let best_block_index = tf.best_block_index();
assert_eq!(
verifier.connect_transaction(
&TransactionSourceForConnect::for_mempool(&best_block_index),
&TransactionSourceWithHeight::for_mempool(&best_block_index),
&spend_locked_tx,
&BlockTimestamp::from_time(tf.current_time()),
),
Expand All @@ -165,7 +165,7 @@ fn output_lock_for_block_count(#[case] seed: Seed) {
let best_block_index = tf.best_block_index();
verifier
.connect_transaction(
&TransactionSourceForConnect::for_mempool(&best_block_index),
&TransactionSourceWithHeight::for_mempool(&best_block_index),
&spend_locked_tx,
&BlockTimestamp::from_time(tf.current_time()),
)
Expand Down Expand Up @@ -226,7 +226,7 @@ fn output_lock_until_time(#[case] seed: Seed) {
let best_block_index = tf.best_block_index();
assert_eq!(
verifier.connect_transaction(
&TransactionSourceForConnect::for_mempool(&best_block_index),
&TransactionSourceWithHeight::for_mempool(&best_block_index),
&spend_locked_tx,
&mtp,
),
Expand Down Expand Up @@ -254,7 +254,7 @@ fn output_lock_until_time(#[case] seed: Seed) {
let best_block_index = tf.best_block_index();
verifier
.connect_transaction(
&TransactionSourceForConnect::for_mempool(&best_block_index),
&TransactionSourceWithHeight::for_mempool(&best_block_index),
&spend_locked_tx,
&BlockTimestamp::from_time(tf.current_time()),
)
Expand Down Expand Up @@ -316,7 +316,7 @@ fn output_lock_for_seconds(#[case] seed: Seed) {
let best_block_index = tf.best_block_index();
assert_eq!(
verifier.connect_transaction(
&TransactionSourceForConnect::for_mempool(&best_block_index),
&TransactionSourceWithHeight::for_mempool(&best_block_index),
&spend_locked_tx,
&mtp,
),
Expand Down Expand Up @@ -349,7 +349,7 @@ fn output_lock_for_seconds(#[case] seed: Seed) {
let best_block_index = tf.best_block_index();
verifier
.connect_transaction(
&TransactionSourceForConnect::for_mempool(&best_block_index),
&TransactionSourceWithHeight::for_mempool(&best_block_index),
&spend_locked_tx,
&BlockTimestamp::from_time(time::get_time()),
)
Expand Down
Loading

0 comments on commit b1787fa

Please sign in to comment.