Skip to content

Commit

Permalink
chore: the adapter lib should expose just a simple function that star…
Browse files Browse the repository at this point in the history
…ts the server (#3144)

Removed unused code from the bench as well.
  • Loading branch information
rumenov authored Dec 13, 2024
1 parent ad35390 commit 7d1825b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
27 changes: 4 additions & 23 deletions rs/bitcoin/adapter/benches/e2e.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,7 +37,6 @@ async fn start_client(uds_path: &Path) -> BitcoinAdapterClient {
}

fn prepare(
blockchain_state: &mut BlockchainState,
processed_block_hashes: &mut Vec<BlockHash>,
genesis: BlockHeader,
forks_num: usize,
Expand All @@ -59,16 +58,6 @@ fn prepare(
.map(|h| h.block_hash())
.collect::<Vec<_>>(),
);
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");
}
}
}

Expand All @@ -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();

Expand Down
26 changes: 9 additions & 17 deletions rs/bitcoin/adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<BlockHeader>),
/// Inform the adapter to prune the following block hashes from the cache.
Expand All @@ -131,15 +131,15 @@ 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<u8>),
}

/// 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,

Expand Down Expand Up @@ -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,
);
Expand Down
16 changes: 14 additions & 2 deletions rs/bitcoin/adapter/src/rpc_server.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;
Expand Down Expand Up @@ -127,10 +129,20 @@ pub fn start_grpc_server(
config: Config,
logger: ReplicaLogger,
last_received_tx: watch::Sender<Option<Instant>>,
get_successors_handler: GetSuccessorsHandler,
blockchain_state: Arc<Mutex<BlockchainState>>,
blockchain_manager_tx: mpsc::Sender<BlockchainManagerRequest>,
transaction_manager_tx: mpsc::Sender<TransactionManagerRequest>,
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,
Expand Down

0 comments on commit 7d1825b

Please sign in to comment.