Modify get_transactions to split requests as to not hit the restricted RPC limits

This commit is contained in:
Luke Parker 2023-07-05 22:12:34 -04:00
parent 93fe8a52dd
commit 3c6cc42c23
No known key found for this signature in database
2 changed files with 39 additions and 25 deletions

View file

@ -44,18 +44,23 @@ async fn check_block(rpc: Arc<Rpc<HttpRpc>>, block_i: usize) {
txs: Vec<TransactionResponse>, txs: Vec<TransactionResponse>,
} }
let txs: TransactionsResponse = rpc let mut hashes_hex = block.txs.iter().map(hex::encode).collect::<Vec<_>>();
.rpc_call( let mut all_txs = vec![];
"get_transactions", while !hashes_hex.is_empty() {
Some(json!({ let txs: TransactionsResponse = rpc
"txs_hashes": block.txs.iter().map(hex::encode).collect::<Vec<_>>() .rpc_call(
})), "get_transactions",
) Some(json!({
.await "txs_hashes": hashes_hex.drain(.. hashes_hex.len().min(100)).collect::<Vec<_>>(),
.expect("couldn't call get_transactions"); })),
assert!(txs.missed_tx.is_empty()); )
.await
.expect("couldn't call get_transactions");
assert!(txs.missed_tx.is_empty());
all_txs.extend(txs.txs);
}
for (tx_hash, tx_res) in block.txs.into_iter().zip(txs.txs.into_iter()) { for (tx_hash, tx_res) in block.txs.into_iter().zip(all_txs.into_iter()) {
assert_eq!( assert_eq!(
tx_res.tx_hash, tx_res.tx_hash,
hex::encode(tx_hash), hex::encode(tx_hash),

View file

@ -198,23 +198,32 @@ impl<R: RpcConnection> Rpc<R> {
return Ok(vec![]); return Ok(vec![]);
} }
let txs: TransactionsResponse = self let mut hashes_hex = hashes.iter().map(hex::encode).collect::<Vec<_>>();
.rpc_call( let mut all_txs = Vec::with_capacity(hashes.len());
"get_transactions", while !hashes_hex.is_empty() {
Some(json!({ // Monero errors if more than 100 is requested unless using a non-restricted RPC
"txs_hashes": hashes.iter().map(hex::encode).collect::<Vec<_>>() const TXS_PER_REQUEST: usize = 100;
})), let this_count = TXS_PER_REQUEST.min(hashes_hex.len());
)
.await?;
if !txs.missed_tx.is_empty() { let txs: TransactionsResponse = self
Err(RpcError::TransactionsNotFound( .rpc_call(
txs.missed_tx.iter().map(|hash| hash_hex(hash)).collect::<Result<_, _>>()?, "get_transactions",
))?; Some(json!({
"txs_hashes": hashes_hex.drain(.. this_count).collect::<Vec<_>>(),
})),
)
.await?;
if !txs.missed_tx.is_empty() {
Err(RpcError::TransactionsNotFound(
txs.missed_tx.iter().map(|hash| hash_hex(hash)).collect::<Result<_, _>>()?,
))?;
}
all_txs.extend(txs.txs);
} }
txs all_txs
.txs
.iter() .iter()
.enumerate() .enumerate()
.map(|(i, res)| { .map(|(i, res)| {