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 mut hashes_hex = block.txs.iter().map(hex::encode).collect::<Vec<_>>();
let mut all_txs = vec![];
while !hashes_hex.is_empty() {
let txs: TransactionsResponse = rpc let txs: TransactionsResponse = rpc
.rpc_call( .rpc_call(
"get_transactions", "get_transactions",
Some(json!({ Some(json!({
"txs_hashes": block.txs.iter().map(hex::encode).collect::<Vec<_>>() "txs_hashes": hashes_hex.drain(.. hashes_hex.len().min(100)).collect::<Vec<_>>(),
})), })),
) )
.await .await
.expect("couldn't call get_transactions"); .expect("couldn't call get_transactions");
assert!(txs.missed_tx.is_empty()); 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,11 +198,18 @@ impl<R: RpcConnection> Rpc<R> {
return Ok(vec![]); return Ok(vec![]);
} }
let mut hashes_hex = hashes.iter().map(hex::encode).collect::<Vec<_>>();
let mut all_txs = Vec::with_capacity(hashes.len());
while !hashes_hex.is_empty() {
// Monero errors if more than 100 is requested unless using a non-restricted RPC
const TXS_PER_REQUEST: usize = 100;
let this_count = TXS_PER_REQUEST.min(hashes_hex.len());
let txs: TransactionsResponse = self let txs: TransactionsResponse = self
.rpc_call( .rpc_call(
"get_transactions", "get_transactions",
Some(json!({ Some(json!({
"txs_hashes": hashes.iter().map(hex::encode).collect::<Vec<_>>() "txs_hashes": hashes_hex.drain(.. this_count).collect::<Vec<_>>(),
})), })),
) )
.await?; .await?;
@ -213,8 +220,10 @@ impl<R: RpcConnection> Rpc<R> {
))?; ))?;
} }
txs all_txs.extend(txs.txs);
.txs }
all_txs
.iter() .iter()
.enumerate() .enumerate()
.map(|(i, res)| { .map(|(i, res)| {