Skip to content

Commit

Permalink
style: clippy + fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
biryukovmaxim committed Jan 10, 2025
1 parent e3d92ff commit ebea677
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 54 deletions.
2 changes: 1 addition & 1 deletion consensus/core/src/acceptance_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct MergesetBlockAcceptanceDataWithTx {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionWithFee {
pub tx: Transaction,
pub fee: u64
pub fee: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion consensus/core/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub trait ConsensusApi: Send + Sync {
&self,
accepting_block_hash: Hash,
outpoints: Arc<Vec<TransactionOutpoint>>,
) -> Result<Vec<u64>, UtxoInquirerError> {
) -> Result<Vec<u64>, UtxoInquirerError> {
unimplemented!()
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ impl ConsensusApi for Consensus {
&self,
accepting_block_hash: Hash,
outpoints: Arc<Vec<TransactionOutpoint>>,
) -> Result<Vec<u64>, UtxoInquirerError> {
) -> Result<Vec<u64>, UtxoInquirerError> {
// We need consistency between the pruning_point_store, utxo_diffs_store, block_transactions_store, selected chain and headers store reads
let _guard = self.pruning_lock.blocking_read();
self.virtual_processor.get_utxo_amounts(accepting_block_hash, outpoints)
Expand Down
56 changes: 21 additions & 35 deletions consensus/src/pipeline/virtual_processor/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ use rayon::{
ThreadPool,
};
use rocksdb::WriteBatch;
use std::collections::BTreeMap;
use std::{
cmp::min,
collections::{BinaryHeap, HashMap, VecDeque},
ops::Deref,
sync::{atomic::Ordering, Arc},
};
use std::collections::BTreeMap;

pub struct VirtualStateProcessor {
// Channels
Expand Down Expand Up @@ -350,21 +350,24 @@ impl VirtualStateProcessor {
.expect("expecting an open unbounded channel");
if self.notification_root.has_subscription(EventType::VirtualChainChanged) {
// check for subscriptions before the heavy lifting
let added_chain_blocks_acceptance_data: Vec<(Hash, Arc<AcceptanceData>)> =
chain_path.added.iter().copied().map(|added| self.acceptance_data_store.get(added).map(|acceptance_data| (added, acceptance_data)).unwrap()).collect_vec();
let added_chain_blocks_acceptance_data: Vec<(Hash, Arc<AcceptanceData>)> = chain_path
.added
.iter()
.copied()
.map(|added| self.acceptance_data_store.get(added).map(|acceptance_data| (added, acceptance_data)).unwrap())
.collect_vec();
let added_chain_blocks_acceptance_data = added_chain_blocks_acceptance_data
.into_iter()
.map(|(accepting_block_hash, mergeset_data)| {
// Create maps to track values for fee calculation
let mut outpoint_to_value = BTreeMap::new();
let mut tx_id_input_to_outpoint: BTreeMap<(TransactionId, u32), (TransactionOutpoint, Option<u64>)> = BTreeMap::new();
let mut tx_id_input_to_outpoint: BTreeMap<(TransactionId, u32), (TransactionOutpoint, Option<u64>)> =
BTreeMap::new();

let acceptance_data = mergeset_data.iter()
let acceptance_data = mergeset_data
.iter()
.map(|mined_block| {
let block_transactions = self
.block_transactions_store
.get(mined_block.block_hash)
.unwrap();
let block_transactions = self.block_transactions_store.get(mined_block.block_hash).unwrap();

let accepted_transactions = block_transactions
.iter()
Expand All @@ -379,32 +382,21 @@ impl VirtualStateProcessor {
.map(|(_, tx)| {
// Collect input outpoints for later value lookup
tx.inputs.iter().enumerate().for_each(|(index, input)| {
tx_id_input_to_outpoint.insert(
(tx.id(), index as u32),
(input.previous_outpoint, None)
);
tx_id_input_to_outpoint.insert((tx.id(), index as u32), (input.previous_outpoint, None));
});

// Store output values
tx.outputs.iter().enumerate().for_each(|(index, out)| {
outpoint_to_value.insert(
TransactionOutpoint {
transaction_id: tx.id(),
index: index as u32
},
out.value
);
outpoint_to_value
.insert(TransactionOutpoint { transaction_id: tx.id(), index: index as u32 }, out.value);
});

tx
})
.collect_vec();

let block_timestamp = self
.headers_store
.get_compact_header_data(mined_block.block_hash)
.unwrap()
.timestamp;
let block_timestamp =
self.headers_store.get_compact_header_data(mined_block.block_hash).unwrap().timestamp;

MergesetBlockAcceptanceDataWithTx {
block_hash: mined_block.block_hash,
Expand All @@ -414,10 +406,7 @@ impl VirtualStateProcessor {
.map(|tx| {
// Calculate fee
let mut outpoints_requested_from_utxo = Vec::new();
let input_outpoints: Vec<_> = tx.inputs
.iter()
.map(|input| input.previous_outpoint)
.collect();
let input_outpoints: Vec<_> = tx.inputs.iter().map(|input| input.previous_outpoint).collect();

// Collect outpoints that need values from UTXO diff
let missing_outpoints: Vec<_> = input_outpoints
Expand All @@ -436,12 +425,9 @@ impl VirtualStateProcessor {
});

// Store retrieved values
outpoints_requested_from_utxo
.iter()
.zip(values)
.for_each(|(outpoint, value)| {
outpoint_to_value.insert(*outpoint, value);
});
outpoints_requested_from_utxo.iter().zip(values).for_each(|(outpoint, value)| {
outpoint_to_value.insert(*outpoint, value);
});
}

// Calculate fee as input_sum - output_sum
Expand Down
14 changes: 5 additions & 9 deletions consensus/src/pipeline/virtual_processor/utxo_inquirer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{cmp, sync::Arc};

use kaspa_consensus_core::tx::TransactionOutpoint;
use kaspa_consensus_core::{
acceptance_data::AcceptanceData,
tx::{SignableTransaction, Transaction, UtxoEntry},
utxo::{utxo_diff::ImmutableUtxoDiff, utxo_inquirer::UtxoInquirerError},
};
use kaspa_consensus_core::tx::TransactionOutpoint;
use kaspa_core::{trace, warn};
use kaspa_hashes::Hash;

Expand All @@ -19,7 +19,6 @@ use super::VirtualStateProcessor;
// todo get populated transactions by accepting block hash and by previous outpoints

impl VirtualStateProcessor {

pub fn get_utxo_amounts(
&self,
accepting_block_hash: Hash,
Expand All @@ -37,13 +36,10 @@ impl VirtualStateProcessor {
let values: Vec<u64> = outpoints
.iter()
.map(|outpoint| {
removed_diffs
.get(outpoint)
.map(|v| v.amount)
.unwrap_or_else(|| {
log::error!("Missing UTXO entry for outpoint: {:?}", outpoint);
0
})
removed_diffs.get(outpoint).map(|v| v.amount).unwrap_or_else(|| {
log::error!("Missing UTXO entry for outpoint: {:?}", outpoint);
0
})
})
.collect();

Expand Down
7 changes: 3 additions & 4 deletions rpc/core/src/convert/tx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Conversion of Transaction related types
use kaspa_consensus_core::acceptance_data::TransactionWithFee;
use crate::{RpcError, RpcResult, RpcTransaction, RpcTransactionInput, RpcTransactionOutput};
use kaspa_consensus_core::acceptance_data::TransactionWithFee;
use kaspa_consensus_core::tx::{Transaction, TransactionInput, TransactionOutput};

// ----------------------------------------------------------------------------
Expand All @@ -28,21 +28,20 @@ impl From<&Transaction> for RpcTransaction {
}

impl From<&TransactionWithFee> for RpcTransaction {
fn from(TransactionWithFee {tx,fee}: &TransactionWithFee) -> Self {
fn from(TransactionWithFee { tx, fee }: &TransactionWithFee) -> Self {
let mut rtx = RpcTransaction::from(tx);
rtx.fee = *fee;
rtx
}
}
impl From<TransactionWithFee> for RpcTransaction {
fn from(TransactionWithFee{tx,fee}: TransactionWithFee) -> Self {
fn from(TransactionWithFee { tx, fee }: TransactionWithFee) -> Self {
let mut rtx = RpcTransaction::from(&tx);
rtx.fee = fee;
rtx
}
}


impl From<&TransactionOutput> for RpcTransactionOutput {
fn from(item: &TransactionOutput) -> Self {
Self {
Expand Down
1 change: 1 addition & 0 deletions rpc/core/src/model/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ mod mockery {
payload: Hash::mock().as_bytes().to_vec(),
mass: mock(),
verbose_data: mock(),
fee: mock(),
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions rpc/service/src/converter/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ impl ConsensusConverter {
let mut tx_id_input_to_outpoint: BTreeMap<(TransactionId, u32), (TransactionOutpoint, Option<&u64>)> = BTreeMap::new();
let mut outpoint_to_value = BTreeMap::new();
let mut acceptance_data = Vec::with_capacity(mergeset_data.len());
for MergesetBlockAcceptanceData { block_hash, accepted_transactions: accepted_transaction_entries } in mergeset_data.iter() {
for MergesetBlockAcceptanceData { block_hash, accepted_transactions: accepted_transaction_entries } in mergeset_data.iter()
{
let block = consensus.async_get_block_even_if_header_only(*block_hash).await?;
let block_timestamp = block.header.timestamp;
let block_txs = block.transactions;
Expand Down Expand Up @@ -222,8 +223,11 @@ impl ConsensusConverter {
acceptance_data.iter_mut().for_each(|d| {
d.accepted_transactions.iter_mut().for_each(|rtx| {
let tx = Transaction::try_from(rtx.clone()).unwrap(); // lol
let input_sum: u64 =
tx.inputs.iter().map(|input| outpoint_to_value.get(&input.previous_outpoint).cloned().unwrap_or_default()).sum();
let input_sum: u64 = tx
.inputs
.iter()
.map(|input| outpoint_to_value.get(&input.previous_outpoint).cloned().unwrap_or_default())
.sum();
let output_sum: u64 = tx.outputs.iter().map(|o| o.value).sum();
rtx.fee = input_sum.saturating_sub(output_sum);
})
Expand Down

0 comments on commit ebea677

Please sign in to comment.