Skip to content

Commit

Permalink
RPC Server: pushTransaction and pushHighPriorityTransaction metho…
Browse files Browse the repository at this point in the history
…ds broadcast the transaction instead of only pushing it into the local mempool

By only pushing the transaction into the local mempool, the transaction never gets adopted if the node itself isn't an elected block producer.
  • Loading branch information
Eligioo committed Dec 9, 2024
1 parent c801676 commit b350d94
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/src/extras/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn initialize_rpc_server(
));
dispatcher.add(NetworkDispatcher::new(client.network()));
if let Some(mempool) = client.mempool() {
dispatcher.add(MempoolDispatcher::new(mempool));
dispatcher.add(MempoolDispatcher::new(client.consensus_proxy(), mempool));
}
dispatcher.add(PolicyDispatcher {});
if let Some(validator_proxy) = client.validator_proxy() {
Expand Down
2 changes: 1 addition & 1 deletion mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ impl Mempool {
(txs, size)
}

/// Adds a transaction to the Mempool.
/// Adds a transaction to the local mempool without broadcasting it over the network.
pub fn add_transaction(
&self,
transaction: Transaction,
Expand Down
4 changes: 2 additions & 2 deletions rpc-interface/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use crate::types::{HashOrTx, MempoolInfo, RPCResult};
pub trait MempoolInterface {
type Error;

/// Pushes a raw transaction into the mempool, it will be assigned a default priority.
/// Pushes a raw transaction with a default priority assigned and broadcast it to the network.
async fn push_transaction(&mut self, raw_tx: String)
-> RPCResult<Blake2bHash, (), Self::Error>;

/// Pushes a raw transaction into the mempool with high priority.
/// Pushes a raw transaction with a high priority assigned and broadcast it to the network.
async fn push_high_priority_transaction(
&mut self,
raw_tx: String,
Expand Down
17 changes: 10 additions & 7 deletions rpc-server/src/dispatchers/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::sync::Arc;

use async_trait::async_trait;
use nimiq_consensus::ConsensusProxy;
use nimiq_hash::{Blake2bHash, Hash};
use nimiq_mempool::{mempool::Mempool, mempool_transactions::TxPriority};
use nimiq_mempool::mempool::Mempool;
use nimiq_network_libp2p::Network;
use nimiq_rpc_interface::{
mempool::MempoolInterface,
types::{HashOrTx, MempoolInfo, RPCResult},
Expand All @@ -14,12 +16,13 @@ use crate::error::Error;

#[allow(dead_code)]
pub struct MempoolDispatcher {
consensus: ConsensusProxy<Network>,
mempool: Arc<Mempool>,
}

impl MempoolDispatcher {
pub fn new(mempool: Arc<Mempool>) -> Self {
MempoolDispatcher { mempool }
pub fn new(consensus: ConsensusProxy<Network>, mempool: Arc<Mempool>) -> Self {
MempoolDispatcher { consensus, mempool }
}
}

Expand All @@ -35,9 +38,9 @@ impl MempoolInterface for MempoolDispatcher {
let tx = Transaction::deserialize_from_vec(&hex::decode(&raw_tx)?)?;
let txid = tx.hash::<Blake2bHash>();

match self.mempool.add_transaction(tx, None) {
match self.consensus.send_transaction(tx).await {
Ok(_) => Ok(txid.into()),
Err(e) => Err(Error::MempoolError(e)),
Err(e) => Err(Error::NetworkError(e)),
}
}

Expand All @@ -48,9 +51,9 @@ impl MempoolInterface for MempoolDispatcher {
let tx = Transaction::deserialize_from_vec(&hex::decode(&raw_tx)?)?;
let txid = tx.hash::<Blake2bHash>();

match self.mempool.add_transaction(tx, Some(TxPriority::High)) {
match self.consensus.send_transaction(tx).await {
Ok(_) => Ok(txid.into()),
Err(e) => Err(Error::MempoolError(e)),
Err(e) => Err(Error::NetworkError(e)),
}
}

Expand Down

0 comments on commit b350d94

Please sign in to comment.