diff --git a/rs/bitcoin/adapter/benches/e2e.rs b/rs/bitcoin/adapter/benches/e2e.rs index cb4070648b6..6702e2657b7 100644 --- a/rs/bitcoin/adapter/benches/e2e.rs +++ b/rs/bitcoin/adapter/benches/e2e.rs @@ -1,8 +1,8 @@ -use bitcoin::{Block, BlockHash, BlockHeader, Network}; +use bitcoin::{blockdata::constants::genesis_block, BlockHash, BlockHeader, Network}; use criterion::{criterion_group, criterion_main, Criterion}; +use ic_btc_adapter::config::Config; use ic_btc_adapter::config::IncomingSource; use ic_btc_adapter::start_server; -use ic_btc_adapter::{config::Config, BlockchainState}; use ic_btc_adapter_client::setup_bitcoin_adapter_clients; use ic_btc_adapter_test_utils::generate_headers; use ic_btc_replica_types::BitcoinAdapterRequestWrapper; @@ -37,7 +37,6 @@ async fn start_client(uds_path: &Path) -> BitcoinAdapterClient { } fn prepare( - blockchain_state: &mut BlockchainState, processed_block_hashes: &mut Vec, genesis: BlockHeader, forks_num: usize, @@ -59,16 +58,6 @@ fn prepare( .map(|h| h.block_hash()) .collect::>(), ); - blockchain_state.add_headers(&fork); - for header in fork { - let block = Block { - header, - txdata: vec![], - }; - blockchain_state - .add_block(block) - .expect("Failed to add block"); - } } } @@ -78,18 +67,10 @@ fn e2e(criterion: &mut Criterion) { ..Default::default() }; - let mut blockchain_state = BlockchainState::new(&config, &MetricsRegistry::default()); let mut processed_block_hashes = vec![]; - let genesis = *blockchain_state.genesis(); + let genesis = genesis_block(config.network).header; - prepare( - &mut blockchain_state, - &mut processed_block_hashes, - genesis, - 4, - 2000, - 1975, - ); + prepare(&mut processed_block_hashes, genesis, 4, 2000, 1975); let rt = tokio::runtime::Runtime::new().unwrap(); diff --git a/rs/bitcoin/adapter/src/lib.rs b/rs/bitcoin/adapter/src/lib.rs index 245217f5cb0..ffec373df3f 100644 --- a/rs/bitcoin/adapter/src/lib.rs +++ b/rs/bitcoin/adapter/src/lib.rs @@ -52,10 +52,10 @@ mod transaction_store; // malicious fork can be prioritized by a DFS, thus potentially ignoring honest forks). mod get_successors_handler; -use crate::{router::start_main_event_loop, stream::StreamEvent}; -pub use blockchainstate::BlockchainState; -pub use get_successors_handler::GetSuccessorsHandler; -pub use rpc_server::start_grpc_server; +use crate::{ + blockchainstate::BlockchainState, get_successors_handler::GetSuccessorsHandler, + router::start_main_event_loop, rpc_server::start_grpc_server, stream::StreamEvent, +}; /// This struct is used to represent commands given to the adapter in order to interact /// with BTC nodes. @@ -121,7 +121,7 @@ trait ProcessBitcoinNetworkMessage { /// Commands sent back to the router in order perform actions on the blockchain state. #[derive(Debug)] -pub enum BlockchainManagerRequest { +pub(crate) enum BlockchainManagerRequest { /// Inform the adapter to enqueue the next block headers into the syncing queue. EnqueueNewBlocksToDownload(Vec), /// Inform the adapter to prune the following block hashes from the cache. @@ -131,7 +131,7 @@ pub enum BlockchainManagerRequest { /// The transaction manager is owned by a single thread which listens on a channel /// for TransactionManagerRequest messages and executes the corresponding method. #[derive(Debug)] -pub enum TransactionManagerRequest { +pub(crate) enum TransactionManagerRequest { /// Command for executing send_transaction SendTransaction(Vec), } @@ -139,7 +139,7 @@ pub enum TransactionManagerRequest { /// The type tracks when then adapter should become idle. The type is /// thread-safe. #[derive(Clone)] -pub struct AdapterState { +pub(crate) struct AdapterState { /// The field contains how long the adapter should wait before becoming idle. idle_seconds: u64, @@ -209,22 +209,14 @@ pub fn start_server( let (blockchain_manager_tx, blockchain_manager_rx) = channel(100); let blockchain_state = Arc::new(Mutex::new(BlockchainState::new(&config, metrics_registry))); - let get_successors_handler = GetSuccessorsHandler::new( - &config, - // The get successor handler should be low latency, and instead of not sharing state and - // offloading the computation to an event loop here we directly access the shared state. - blockchain_state.clone(), - blockchain_manager_tx, - metrics_registry, - ); - let (transaction_manager_tx, transaction_manager_rx) = channel(100); start_grpc_server( config.clone(), log.clone(), tx, - get_successors_handler, + blockchain_state.clone(), + blockchain_manager_tx, transaction_manager_tx, metrics_registry, ); diff --git a/rs/bitcoin/adapter/src/rpc_server.rs b/rs/bitcoin/adapter/src/rpc_server.rs index 5833504038f..1478e6f8fe4 100644 --- a/rs/bitcoin/adapter/src/rpc_server.rs +++ b/rs/bitcoin/adapter/src/rpc_server.rs @@ -1,8 +1,9 @@ use crate::{ + blockchainstate::BlockchainState, config::{Config, IncomingSource}, get_successors_handler::{GetSuccessorsRequest, GetSuccessorsResponse}, metrics::{ServiceMetrics, LABEL_GET_SUCCESSOR, LABEL_SEND_TRANSACTION}, - GetSuccessorsHandler, TransactionManagerRequest, + BlockchainManagerRequest, GetSuccessorsHandler, TransactionManagerRequest, }; use bitcoin::{consensus::Encodable, hashes::Hash, BlockHash}; use ic_async_utils::{incoming_from_nth_systemd_socket, incoming_from_path}; @@ -14,6 +15,7 @@ use ic_btc_service::{ use ic_logger::{debug, ReplicaLogger}; use ic_metrics::MetricsRegistry; use std::convert::{TryFrom, TryInto}; +use std::sync::{Arc, Mutex}; use std::time::Instant; use tokio::sync::mpsc; use tokio::sync::watch; @@ -127,10 +129,20 @@ pub fn start_grpc_server( config: Config, logger: ReplicaLogger, last_received_tx: watch::Sender>, - get_successors_handler: GetSuccessorsHandler, + blockchain_state: Arc>, + blockchain_manager_tx: mpsc::Sender, transaction_manager_tx: mpsc::Sender, metrics_registry: &MetricsRegistry, ) { + let get_successors_handler = GetSuccessorsHandler::new( + &config, + // The get successor handler should be low latency, and instead of not sharing state and + // offloading the computation to an event loop here we directly access the shared state. + blockchain_state, + blockchain_manager_tx, + metrics_registry, + ); + let btc_adapter_impl = BtcServiceImpl { last_received_tx, get_successors_handler,