diff --git a/crates/subcoin-network/src/lib.rs b/crates/subcoin-network/src/lib.rs index fb5f09f3..59894ff2 100644 --- a/crates/subcoin-network/src/lib.rs +++ b/crates/subcoin-network/src/lib.rs @@ -227,6 +227,8 @@ enum NetworkWorkerMessage { SyncPeers(oneshot::Sender>), /// Retrieve the number of inbound connected peers. InboundPeersCount(oneshot::Sender), + /// Add transaction to the transaction manager. + SendRawTransaction(Vec), } /// A handle for interacting with the network worker. @@ -269,6 +271,17 @@ impl NetworkHandle { receiver.await.unwrap_or_default() } + pub fn send_transaction(&self, raw_tx: Vec) { + let tx_size = raw_tx.len(); + if self + .worker_msg_sender + .unbounded_send(NetworkWorkerMessage::SendRawTransaction(raw_tx)) + .is_err() + { + tracing::error!("Failed to send raw tx ({tx_size} bytes) to worker"); + } + } + /// Returns a flag indicating whether the node is actively performing a major sync. pub fn is_major_syncing(&self) -> Arc { self.is_major_syncing.clone() diff --git a/crates/subcoin-network/src/worker.rs b/crates/subcoin-network/src/worker.rs index 71a6c910..023d683c 100644 --- a/crates/subcoin-network/src/worker.rs +++ b/crates/subcoin-network/src/worker.rs @@ -177,7 +177,7 @@ where } } - fn process_worker_message(&self, worker_msg: NetworkWorkerMessage, bandwidth: &Bandwidth) { + fn process_worker_message(&mut self, worker_msg: NetworkWorkerMessage, bandwidth: &Bandwidth) { match worker_msg { NetworkWorkerMessage::NetworkStatus(result_sender) => { let net_status = NetworkStatus { @@ -195,6 +195,9 @@ where NetworkWorkerMessage::InboundPeersCount(result_sender) => { let _ = result_sender.send(self.peer_manager.inbound_peers_count()); } + NetworkWorkerMessage::SendRawTransaction(raw_tx) => { + self.transaction_manager.add_transaction(&raw_tx); + } } } diff --git a/crates/subcoin-rpc/src/subcoin.rs b/crates/subcoin-rpc/src/subcoin.rs index a3fa985c..0e9a6eef 100644 --- a/crates/subcoin-rpc/src/subcoin.rs +++ b/crates/subcoin-rpc/src/subcoin.rs @@ -25,6 +25,10 @@ pub trait SubcoinApi { /// Get the sync peers. #[method(name = "subcoin_networkPeers")] async fn network_peers(&self) -> Result; + + /// Get the sync peers. + #[method(name = "subcoin_sendTransaction", blocking)] + fn send_transaction(&self, raw_tx: Vec) -> Result<(), Error>; } /// This struct provides the Subcoin API. @@ -87,4 +91,9 @@ where sync_peers, }) } + + fn send_transaction(&self, raw_tx: Vec) -> Result<(), Error> { + self.network_handle.send_transaction(raw_tx); + Ok(()) + } }