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 authored and jsdanielh committed Dec 12, 2024
1 parent 2fb68f9 commit e908521
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 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 into the mempool 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 into the mempool and broadcast it to the network.
async fn push_high_priority_transaction(
&mut self,
raw_tx: String,
Expand Down
28 changes: 22 additions & 6 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_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,8 +38,13 @@ 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) {
Ok(_) => Ok(txid.into()),
match self.mempool.add_transaction(tx.clone(), None) {
Ok(_) => self
.consensus
.send_transaction(tx)
.await
.map(|_| txid.into())
.map_err(Error::NetworkError),
Err(e) => Err(Error::MempoolError(e)),
}
}
Expand All @@ -48,8 +56,16 @@ 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)) {
Ok(_) => Ok(txid.into()),
match self
.mempool
.add_transaction(tx.clone(), Some(TxPriority::High))
{
Ok(_) => self
.consensus
.send_transaction(tx)
.await
.map(|_| txid.into())
.map_err(Error::NetworkError),
Err(e) => Err(Error::MempoolError(e)),
}
}
Expand Down

0 comments on commit e908521

Please sign in to comment.