Skip to content

Commit

Permalink
Add rpc subcoin_sendTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Aug 4, 2024
1 parent 514235a commit 67514eb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions crates/subcoin-network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ enum NetworkWorkerMessage {
SyncPeers(oneshot::Sender<Vec<PeerSync>>),
/// Retrieve the number of inbound connected peers.
InboundPeersCount(oneshot::Sender<usize>),
/// Add transaction to the transaction manager.
SendRawTransaction(Vec<u8>),
}

/// A handle for interacting with the network worker.
Expand Down Expand Up @@ -269,6 +271,17 @@ impl NetworkHandle {
receiver.await.unwrap_or_default()
}

pub fn send_transaction(&self, raw_tx: Vec<u8>) {
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<AtomicBool> {
self.is_major_syncing.clone()
Expand Down
5 changes: 4 additions & 1 deletion crates/subcoin-network/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions crates/subcoin-rpc/src/subcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub trait SubcoinApi {
/// Get the sync peers.
#[method(name = "subcoin_networkPeers")]
async fn network_peers(&self) -> Result<NetworkPeers, Error>;

/// Get the sync peers.
#[method(name = "subcoin_sendTransaction", blocking)]
fn send_transaction(&self, raw_tx: Vec<u8>) -> Result<(), Error>;
}

/// This struct provides the Subcoin API.
Expand Down Expand Up @@ -87,4 +91,9 @@ where
sync_peers,
})
}

fn send_transaction(&self, raw_tx: Vec<u8>) -> Result<(), Error> {
self.network_handle.send_transaction(raw_tx);
Ok(())
}
}

0 comments on commit 67514eb

Please sign in to comment.