diff --git a/zebra-state/src/lib.rs b/zebra-state/src/lib.rs index 9e2da095..ffda8645 100644 --- a/zebra-state/src/lib.rs +++ b/zebra-state/src/lib.rs @@ -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; diff --git a/zebrad/src/commands/start.rs b/zebrad/src/commands/start.rs index f956ad5f..afa80bb6 100644 --- a/zebrad/src/commands/start.rs +++ b/zebrad/src/commands/start.rs @@ -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); diff --git a/zebrad/src/components/inbound/tests.rs b/zebrad/src/components/inbound/tests.rs index 2c1b4b2b..3d5a3d51 100644 --- a/zebrad/src/components/inbound/tests.rs +++ b/zebrad/src/components/inbound/tests.rs @@ -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); diff --git a/zebrad/src/components/mempool.rs b/zebrad/src/components/mempool.rs index f6826d1b..a75f799b 100644 --- a/zebrad/src/components/mempool.rs +++ b/zebrad/src/components/mempool.rs @@ -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 diff --git a/zebrad/src/components/mempool/storage.rs b/zebrad/src/components/mempool/storage.rs index 8d1d4d20..29c9d063 100644 --- a/zebrad/src/components/mempool/storage.rs +++ b/zebrad/src/components/mempool/storage.rs @@ -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, diff --git a/zebrad/src/components/mempool/tests.rs b/zebrad/src/components/mempool/tests.rs index 844eaf0e..08c317a6 100644 --- a/zebrad/src/components/mempool/tests.rs +++ b/zebrad/src/components/mempool/tests.rs @@ -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())?;