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

Use correct txids for getting mempool transactions

If block is found after "getmempoolentry()" and before "gettransactions()",
there will be more transactions at `txs` than in `entries` - causing a panic.
This commit is contained in:
Roman Zeyde 2018-07-15 09:28:50 +03:00
parent ea24d6a01f
commit d1f5c61def
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
2 changed files with 6 additions and 6 deletions

View File

@ -359,7 +359,7 @@ impl Daemon {
tx_from_value(self.request("getrawtransaction", args)?)
}
pub fn gettransactions(&self, txhashes: &[Sha256dHash]) -> Result<Vec<Transaction>> {
pub fn gettransactions(&self, txhashes: &[&Sha256dHash]) -> Result<Vec<Transaction>> {
let params_list: Vec<Value> = txhashes
.iter()
.map(|txhash| json!([txhash.be_hex_string(), /*verbose=*/ false]))
@ -370,6 +370,7 @@ impl Daemon {
for value in values {
txs.push(tx_from_value(value)?);
}
assert_eq!(txhashes.len(), txs.len());
Ok(txs)
}

View File

@ -201,9 +201,8 @@ impl Tracker {
timer.observe_duration();
let timer = self.stats.start_timer("add");
let txids_to_add: Vec<Sha256dHash> = new_txids.difference(&old_txids).cloned().collect();
let entries: Vec<(&Sha256dHash, MempoolEntry)> = txids_to_add
.iter()
let txids_iter = new_txids.difference(&old_txids);
let entries: Vec<(&Sha256dHash, MempoolEntry)> = txids_iter
.filter_map(|txid| {
match daemon.getmempoolentry(txid) {
Ok(entry) => Some((txid, entry)),
@ -214,8 +213,8 @@ impl Tracker {
}
})
.collect();
let txs = daemon.gettransactions(&txids_to_add)?;
assert_eq!(entries.len(), txs.len());
let txids: Vec<&Sha256dHash> = entries.iter().map(|(txid, _)| *txid).collect();
let txs = daemon.gettransactions(&txids)?;
for ((txid, entry), tx) in entries.into_iter().zip(txs.into_iter()) {
assert_eq!(tx.txid(), *txid);
self.add(txid, tx, entry);