Sneak chain_tip_change into mempool (#2785)

* Pass ChainTipChange to the mempool

* Fix nits
This commit is contained in:
Marek 2021-09-21 19:06:52 +02:00 committed by GitHub
parent de589f6b31
commit 061ad55144
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 4 deletions

View File

@ -35,7 +35,10 @@ pub use constants::MAX_BLOCK_REORG_HEIGHT;
pub use error::{BoxError, CloneError, CommitBlockError, ValidateContextError};
pub use request::{FinalizedBlock, HashOrHeight, PreparedBlock, Request};
pub use response::Response;
pub use service::{chain_tip::LatestChainTip, init};
pub use service::{
chain_tip::{ChainTipChange, LatestChainTip, TipAction},
init,
};
#[cfg(any(test, feature = "proptest-impl"))]
pub use service::init_test;

View File

@ -55,7 +55,7 @@ impl StartCmd {
info!("initializing node state");
// TODO: use ChainTipChange to get tip changes (#2374, #2710, #2711, #2712, #2713, #2714)
let (state_service, latest_chain_tip, _chain_tip_change) =
let (state_service, latest_chain_tip, chain_tip_change) =
zebra_state::init(config.state.clone(), config.network.network);
let state = ServiceBuilder::new().buffer(20).service(state_service);
@ -96,6 +96,7 @@ impl StartCmd {
state,
tx_verifier,
sync_status.clone(),
chain_tip_change.clone(),
));
let mempool = ServiceBuilder::new().buffer(20).service(mempool_service);

View File

@ -24,6 +24,8 @@ async fn mempool_requests_for_transactions() {
let address_book = AddressBook::new(SocketAddr::from_str("0.0.0.0:0").unwrap(), Span::none());
let address_book = Arc::new(std::sync::Mutex::new(address_book));
let (sync_status, _recent_syncs) = SyncStatus::new();
let (_state_service, _latest_chain_tip, chain_tip_change) =
zebra_state::init(state_config.clone(), network);
let (state, _, _) = zebra_state::init(state_config, network);
let state_service = ServiceBuilder::new().buffer(1).service(state);
@ -38,6 +40,7 @@ async fn mempool_requests_for_transactions() {
state_service.clone(),
transaction_verifier,
sync_status,
chain_tip_change,
);
let added_transactions = add_some_stuff_to_mempool(&mut mempool_service, network);

View File

@ -17,6 +17,7 @@ use zebra_chain::{
use zebra_consensus::{error::TransactionError, transaction};
use zebra_network as zn;
use zebra_state as zs;
use zs::ChainTipChange;
pub use crate::BoxError;
@ -82,6 +83,10 @@ pub struct Mempool {
/// Allows checking if we are near the tip to enable/disable the mempool.
#[allow(dead_code)]
sync_status: SyncStatus,
/// Allows the detection of chain tip resets.
#[allow(dead_code)]
chain_tip_change: ChainTipChange,
}
impl Mempool {
@ -92,20 +97,23 @@ impl Mempool {
state: State,
tx_verifier: TxVerifier,
sync_status: SyncStatus,
chain_tip_change: ChainTipChange,
) -> Self {
let tx_downloads = Box::pin(TxDownloads::new(
Timeout::new(outbound, TRANSACTION_DOWNLOAD_TIMEOUT),
Timeout::new(tx_verifier, TRANSACTION_VERIFY_TIMEOUT),
state,
));
Mempool {
storage: Default::default(),
tx_downloads,
sync_status,
chain_tip_change,
}
}
/// Get the storage field of the mempool for testing purposes.
/// Get the storage field of the mempool for testing purposes.
#[cfg(test)]
pub fn storage(&mut self) -> &mut storage::Storage {
&mut self.storage

View File

@ -22,7 +22,8 @@ pub enum State {
/// no longer belongs in the mempool.
Confirmed(block::Hash),
/// Stayed in mempool for too long without being mined.
// TODO(2021-08-20): set expiration at 2 weeks? This is what Bitcoin does.
// TODO(2021-09-09): Implement ZIP-203: Validate Transaction Expiry Height.
// TODO(2021-09-09): https://github.com/ZcashFoundation/zebra/issues/2387
Expired,
/// Transaction fee is too low for the current mempool state.
LowFee,

View File

@ -17,6 +17,8 @@ async fn mempool_service_basic() -> Result<(), Report> {
let state_config = StateConfig::ephemeral();
let (peer_set, _) = mock_peer_set();
let (sync_status, _recent_syncs) = SyncStatus::new();
let (_state_service, _latest_chain_tip, chain_tip_change) =
zebra_state::init(state_config.clone(), network);
let (state, _, _) = zebra_state::init(state_config, network);
let state_service = ServiceBuilder::new().buffer(1).service(state);
@ -33,6 +35,7 @@ async fn mempool_service_basic() -> Result<(), Report> {
state_service.clone(),
tx_verifier,
sync_status,
chain_tip_change,
);
// Insert the genesis block coinbase transaction into the mempool storage.
service.storage.insert(genesis_transactions.1[0].clone())?;