Optimize getting v1 txs

This commit is contained in:
Artur
2025-05-23 12:49:15 -03:00
parent b0fb8aec6e
commit 02e3fc0db9

View File

@@ -1,5 +1,3 @@
use core::fmt;
use cuprate_blockchain::{config::ConfigBuilder, ops, tables::{OpenTables, Tables}, types::PreRctOutputId} ;
use cuprate_database::{ConcreteEnv, DatabaseRo, Env, EnvInner};
use cuprate_types::json::tx::Transaction;
@@ -62,37 +60,51 @@ async fn get_tx(
let response: TransactionResponse = match tx.clone().into() {
Transaction::V1 { prefix } => {
let mut inputs: Vec<TransactionInput> = Vec::with_capacity(prefix.vin.len());
let inputs= prefix.vin.par_iter().map(|input| {
let mixins = input
.key
.key_offsets
.clone()
.par_iter()
.enumerate()
.map(|(key_offset_i, key_offset)| {
let new_tx_ro = env_inner.tx_ro().unwrap();
let new_tables = env_inner.open_tables(&new_tx_ro).unwrap();
for input in &prefix.vin {
let mut mixins: Vec<TransactionInputMixin> = Vec::with_capacity(
input.key.key_offsets.len()
);
let mut key_offset_sum: u64 = input.key.key_offsets[0..key_offset_i].iter().copied().sum();
key_offset_sum += key_offset;
for key_offset in &input.key.key_offsets {
let output = tables.outputs().get(
&PreRctOutputId {
amount: input.key.amount,
amount_index: key_offset.clone()
let output = new_tables.outputs().get(
&PreRctOutputId {
amount: input.key.amount,
amount_index: key_offset_sum
}
).unwrap();
let mixin_tx_block = new_tables.block_infos().get(&(output.height as usize)).unwrap();
let mixin_tx_hash: [u8; 32] = if output.tx_idx == mixin_tx_block.mining_tx_index {
let tx_blob = new_tables.tx_blobs().get(&output.tx_idx).unwrap();
monero_serai::transaction::Transaction::read(&mut tx_blob.0.as_slice()).unwrap().hash()
} else {
let block_tx_index = (output.tx_idx - mixin_tx_block.mining_tx_index - 1) as usize;
new_tables.block_txs_hashes().get(&(output.height as usize)).unwrap()[block_tx_index]
};
TransactionInputMixin {
height: output.height,
public_key: hex::encode(output.key),
tx_hash: hex::encode(mixin_tx_hash),
}
).unwrap();
let mixin_tx_blob = tables.tx_blobs().get(&output.tx_idx).unwrap();
let mixin_tx_hash = monero_serai::transaction::Transaction::read(&mut mixin_tx_blob.0.as_slice()).unwrap().hash();
mixins.push(TransactionInputMixin {
height: output.height,
public_key: hex::encode(output.key),
tx_hash: hex::encode(mixin_tx_hash),
});
}
})
.collect();
inputs.push(TransactionInput {
TransactionInput {
amount: input.key.amount,
key_image: hex::encode(*input.key.k_image),
mixins: mixins
});
}
}
})
.collect();
let mut outputs: Vec<TransactionOutput> = Vec::with_capacity(prefix.vout.len());
@@ -129,7 +141,7 @@ async fn get_tx(
let inputs = prefix.vin
.par_iter()
.map(|input| {
let mixins: Vec<TransactionInputMixin> = input
let mixins = input
.key
.key_offsets
.clone()
@@ -142,7 +154,6 @@ async fn get_tx(
let mut key_offset_sum: u64 = input.key.key_offsets[0..key_offset_i].iter().copied().sum();
key_offset_sum += key_offset;
let rct_output = new_tables.rct_outputs().get(&key_offset_sum).unwrap();
// let mixin_tx_blob = new_tables.tx_blobs().get(&rct_output.tx_idx).unwrap();
let mixin_tx_block = new_tables.block_infos().get(&(rct_output.height as usize)).unwrap();
let mixin_tx_hash: [u8; 32] = if rct_output.tx_idx == mixin_tx_block.mining_tx_index {
let tx_blob = new_tables.tx_blobs().get(&rct_output.tx_idx).unwrap();