Skip to content

Commit

Permalink
Add transaction from peer
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Aug 4, 2024
1 parent 67514eb commit 08ddbd5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
30 changes: 17 additions & 13 deletions crates/subcoin-network/src/transaction_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,26 @@ impl TransactionManager {
.map(|tx_info| tx_info.transaction.clone())
}

pub fn add_transaction(&mut self, raw_tx: &[u8]) {
pub fn add_raw_transaction(&mut self, raw_tx: &[u8]) {
if let Ok(transaction) = deserialize::<Transaction>(raw_tx) {
let txid = transaction.compute_txid();
self.add_transaction(transaction);
}
}

if self.transactions.len() == Self::MAX_TRANSACTIONS {
self.transactions.shift_remove_index(0);
}
pub fn add_transaction(&mut self, transaction: Transaction) {
let txid = transaction.compute_txid();

match self.transactions.entry(txid) {
Entry::Occupied(_) => {
tracing::debug!("Tx {txid} already exists");
}
Entry::Vacant(entry) => {
entry.insert(TransactionInfo::new(transaction));
tracing::debug!("Added new tx {txid}");
}
if self.transactions.len() == Self::MAX_TRANSACTIONS {
self.transactions.shift_remove_index(0);
}

match self.transactions.entry(txid) {
Entry::Occupied(_) => {
tracing::debug!("Tx {txid} already exists");
}
Entry::Vacant(entry) => {
entry.insert(TransactionInfo::new(transaction));
tracing::debug!("Added new tx {txid}");
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/subcoin-network/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ where
let _ = result_sender.send(self.peer_manager.inbound_peers_count());
}
NetworkWorkerMessage::SendRawTransaction(raw_tx) => {
self.transaction_manager.add_transaction(&raw_tx);
self.transaction_manager.add_raw_transaction(&raw_tx);
}
}
}
Expand Down Expand Up @@ -239,7 +239,8 @@ where
Ok(SyncAction::None)
}
NetworkMessage::Tx(tx) => {
tracing::info!("======================== TODO processing {tx:?}");
// TODO: check if the peer is allowed to send tx.
self.transaction_manager.add_transaction(tx);
Ok(SyncAction::None)
}
NetworkMessage::GetData(inv) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/subcoin-rpc/src/subcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub trait SubcoinApi {
#[method(name = "subcoin_networkPeers")]
async fn network_peers(&self) -> Result<NetworkPeers, Error>;

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

0 comments on commit 08ddbd5

Please sign in to comment.