1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 01:43:29 +01:00

immediately fetch TX from mempool after broadcast

This commit is contained in:
conduition 2023-12-17 22:32:15 +00:00
parent 10998f9f72
commit 2d50ebe1fc
No known key found for this signature in database
GPG Key ID: 78296CED84F6B3C5
4 changed files with 102 additions and 18 deletions

View File

@ -220,6 +220,12 @@ impl Daemon {
.context("failed to get mempool txids") .context("failed to get mempool txids")
} }
pub(crate) fn get_mempool_entry(&self, txid: &Txid) -> Result<json::GetMempoolEntryResult> {
self.rpc
.get_mempool_entry(txid)
.context("failed to get mempool entry")
}
pub(crate) fn get_mempool_entries( pub(crate) fn get_mempool_entries(
&self, &self,
txids: &[Txid], txids: &[Txid],

View File

@ -19,7 +19,7 @@ use crate::{
cache::Cache, cache::Cache,
config::{Config, ELECTRS_VERSION}, config::{Config, ELECTRS_VERSION},
daemon::{self, extract_bitcoind_error, Daemon}, daemon::{self, extract_bitcoind_error, Daemon},
mempool::MempoolSyncUpdate, mempool::{self, MempoolSyncUpdate},
merkle::Proof, merkle::Proof,
metrics::{self, Histogram, Metrics}, metrics::{self, Histogram, Metrics},
signals::Signal, signals::Signal,
@ -49,6 +49,20 @@ struct Request {
params: Value, params: Value,
} }
struct CallResult {
response: Value,
mempool_update: MempoolSyncUpdate,
}
impl CallResult {
fn new<T: serde::Serialize>(response: T) -> CallResult {
CallResult {
response: json!(response),
mempool_update: MempoolSyncUpdate::default(),
}
}
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(untagged)] #[serde(untagged)]
enum Requests { enum Requests {
@ -372,11 +386,20 @@ impl Rpc {
Ok(status) Ok(status)
} }
fn transaction_broadcast(&self, (tx_hex,): &(String,)) -> Result<Value> { fn transaction_broadcast(&self, (tx_hex,): &(String,)) -> Result<(Value, MempoolSyncUpdate)> {
let tx_bytes = Vec::from_hex(tx_hex).context("non-hex transaction")?; let tx_bytes = Vec::from_hex(tx_hex).context("non-hex transaction")?;
let tx = deserialize(&tx_bytes).context("invalid transaction")?; let tx = deserialize(&tx_bytes).context("invalid transaction")?;
let txid = self.daemon.broadcast(&tx)?; let txid = self.daemon.broadcast(&tx)?;
Ok(json!(txid))
// Try to fetch the mempool entry immediately, so we can return an update
// to be applied to the mempool.
let mut mempool_update = MempoolSyncUpdate::default();
if let Ok(rpc_entry) = self.daemon.get_mempool_entry(&txid) {
let entry = mempool::Entry::new(txid, tx, rpc_entry);
mempool_update.new_entries.push(entry);
}
Ok((json!(txid), mempool_update))
} }
fn transaction_get(&self, args: &TxGetArgs) -> Result<Value> { fn transaction_get(&self, args: &TxGetArgs) -> Result<Value> {
@ -475,7 +498,7 @@ impl Rpc {
})) }))
} }
pub fn handle_requests(&self, client: &mut Client, lines: &[String]) -> Vec<String> { pub fn handle_requests(&mut self, client: &mut Client, lines: &[String]) -> Vec<String> {
lines lines
.iter() .iter()
.map(|line| { .map(|line| {
@ -487,7 +510,7 @@ impl Rpc {
.collect() .collect()
} }
fn handle_calls(&self, client: &mut Client, calls: Result<Calls, Value>) -> Value { fn handle_calls(&mut self, client: &mut Client, calls: Result<Calls, Value>) -> Value {
let calls: Calls = match calls { let calls: Calls = match calls {
Ok(calls) => calls, Ok(calls) => calls,
Err(response) => return response, // JSON parsing failed - the response does not contain request id Err(response) => return response, // JSON parsing failed - the response does not contain request id
@ -498,12 +521,33 @@ impl Rpc {
if let Some(result) = self.try_multi_call(client, &batch) { if let Some(result) = self.try_multi_call(client, &batch) {
return json!(result); return json!(result);
} }
json!(batch let responses = batch
.into_iter() .into_iter()
.map(|result| self.single_call(client, result)) .map(|call| {
.collect::<Vec<Value>>()) let CallResult {
response,
mempool_update,
} = self.single_call(client, call);
// Apply the mempool update immediately, so that the next
// response will reflect the updated mempool state.
self.mempool_apply(mempool_update);
response
})
.collect::<Vec<Value>>();
json!(responses)
}
Calls::Single(call) => {
let CallResult {
response,
mempool_update,
} = self.single_call(client, call);
self.mempool_apply(mempool_update);
response
} }
Calls::Single(result) => self.single_call(client, result),
} }
} }
@ -538,10 +582,10 @@ impl Rpc {
) )
} }
fn single_call(&self, client: &mut Client, call: Result<Call, Value>) -> Value { fn single_call(&self, client: &mut Client, call: Result<Call, Value>) -> CallResult {
let call = match call { let call = match call {
Ok(call) => call, Ok(call) => call,
Err(response) => return response, // params parsing may fail - the response contains request id Err(response) => return CallResult::new(response), // params parsing may fail - the response contains request id
}; };
self.rpc_duration.observe_duration(&call.method, || { self.rpc_duration.observe_duration(&call.method, || {
if self.tracker.status().is_err() { if self.tracker.status().is_err() {
@ -551,9 +595,11 @@ impl Rpc {
| Params::BlockHeaders(_) | Params::BlockHeaders(_)
| Params::HeadersSubscribe | Params::HeadersSubscribe
| Params::Version(_) => (), | Params::Version(_) => (),
_ => return error_msg(&call.id, RpcError::UnavailableIndex), _ => return CallResult::new(error_msg(&call.id, RpcError::UnavailableIndex)),
}; };
} }
let mut mempool_update = MempoolSyncUpdate::default();
let result = match &call.params { let result = match &call.params {
Params::Banner => Ok(json!(self.banner)), Params::Banner => Ok(json!(self.banner)),
Params::BlockHeader(args) => self.block_header(*args), Params::BlockHeader(args) => self.block_header(*args),
@ -571,13 +617,23 @@ impl Rpc {
Params::ScriptHashListUnspent(args) => self.scripthash_list_unspent(client, args), Params::ScriptHashListUnspent(args) => self.scripthash_list_unspent(client, args),
Params::ScriptHashSubscribe(args) => self.scripthash_subscribe(client, args), Params::ScriptHashSubscribe(args) => self.scripthash_subscribe(client, args),
Params::ScriptHashUnsubscribe(args) => self.scripthash_unsubscribe(client, args), Params::ScriptHashUnsubscribe(args) => self.scripthash_unsubscribe(client, args),
Params::TransactionBroadcast(args) => self.transaction_broadcast(args), Params::TransactionBroadcast(args) => {
self.transaction_broadcast(args)
.map(|(result, sync_update)| {
mempool_update = sync_update; // extract the mempool sync update
result
})
}
Params::TransactionGet(args) => self.transaction_get(args), Params::TransactionGet(args) => self.transaction_get(args),
Params::TransactionGetMerkle(args) => self.transaction_get_merkle(args), Params::TransactionGetMerkle(args) => self.transaction_get_merkle(args),
Params::TransactionFromPosition(args) => self.transaction_from_pos(*args), Params::TransactionFromPosition(args) => self.transaction_from_pos(*args),
Params::Version(args) => self.version(args), Params::Version(args) => self.version(args),
}; };
call.response(result)
CallResult {
response: call.response(result),
mempool_update,
}
}) })
} }
} }

View File

@ -24,6 +24,22 @@ pub(crate) struct Entry {
pub has_unconfirmed_inputs: bool, pub has_unconfirmed_inputs: bool,
} }
impl Entry {
pub fn new(
txid: Txid,
tx: Transaction,
rpc_entry: bitcoincore_rpc::json::GetMempoolEntryResult,
) -> Entry {
Entry {
txid,
tx,
vsize: rpc_entry.vsize,
fee: rpc_entry.fees.base,
has_unconfirmed_inputs: !rpc_entry.depends.is_empty(),
}
}
}
/// Mempool current state /// Mempool current state
pub(crate) struct Mempool { pub(crate) struct Mempool {
entries: HashMap<Txid, Entry>, entries: HashMap<Txid, Entry>,
@ -38,8 +54,9 @@ pub(crate) struct Mempool {
/// An update to [`Mempool`]'s internal state. This can be fetched /// An update to [`Mempool`]'s internal state. This can be fetched
/// asynchronously using [`MempoolSyncUpdate::poll`], and applied /// asynchronously using [`MempoolSyncUpdate::poll`], and applied
/// using [`Mempool::apply_sync_update`]. /// using [`Mempool::apply_sync_update`].
#[derive(Default)]
pub(crate) struct MempoolSyncUpdate { pub(crate) struct MempoolSyncUpdate {
new_entries: Vec<Entry>, pub(crate) new_entries: Vec<Entry>,
removed_entries: HashSet<Txid>, removed_entries: HashSet<Txid>,
} }
@ -174,6 +191,11 @@ impl Mempool {
let removed = update.removed_entries.len(); let removed = update.removed_entries.len();
let added = update.new_entries.len(); let added = update.new_entries.len();
// Return early to avoid spurious logs.
if added == 0 && removed == 0 {
return;
}
for txid_to_remove in update.removed_entries { for txid_to_remove in update.removed_entries {
self.remove_entry(txid_to_remove); self.remove_entry(txid_to_remove);
} }

View File

@ -164,7 +164,7 @@ fn serve() -> Result<()> {
let rest = server_rx.iter().take(server_rx.len()); let rest = server_rx.iter().take(server_rx.len());
let events: Vec<Event> = first.chain(rest).collect(); let events: Vec<Event> = first.chain(rest).collect();
server_batch_size.observe("recv", events.len() as f64); server_batch_size.observe("recv", events.len() as f64);
duration.observe_duration("handle", || handle_events(&rpc, &mut peers, events)); duration.observe_duration("handle", || handle_events(&mut rpc, &mut peers, events));
}, },
default(config.wait_duration) => (), // sync and update default(config.wait_duration) => (), // sync and update
}; };
@ -206,7 +206,7 @@ enum Message {
Done, Done,
} }
fn handle_events(rpc: &Rpc, peers: &mut HashMap<usize, Peer>, events: Vec<Event>) { fn handle_events(rpc: &mut Rpc, peers: &mut HashMap<usize, Peer>, events: Vec<Event>) {
let mut events_by_peer = HashMap::<usize, Vec<Message>>::new(); let mut events_by_peer = HashMap::<usize, Vec<Message>>::new();
events events
.into_iter() .into_iter()
@ -217,7 +217,7 @@ fn handle_events(rpc: &Rpc, peers: &mut HashMap<usize, Peer>, events: Vec<Event>
} }
fn handle_peer_events( fn handle_peer_events(
rpc: &Rpc, rpc: &mut Rpc,
peers: &mut HashMap<usize, Peer>, peers: &mut HashMap<usize, Peer>,
peer_id: usize, peer_id: usize,
messages: Vec<Message>, messages: Vec<Message>,