Skip to content

Commit

Permalink
Merge branch 'gd-optimization' into gd-tn11-uni-2
Browse files Browse the repository at this point in the history
  • Loading branch information
coderofstuff committed Aug 24, 2024
2 parents aad317d + 46dbac3 commit d0484df
Show file tree
Hide file tree
Showing 111 changed files with 4,958 additions and 772 deletions.
19 changes: 15 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ kaspa-utxoindex = { version = "0.14.2", path = "indexes/utxoindex" }
kaspa-wallet = { version = "0.14.2", path = "wallet/native" }
kaspa-wallet-cli-wasm = { version = "0.14.2", path = "wallet/wasm" }
kaspa-wallet-keys = { version = "0.14.2", path = "wallet/keys" }
kaspa-wallet-pskt = { version = "0.14.1", path = "wallet/pskt" }
kaspa-wallet-pskt = { version = "0.14.2", path = "wallet/pskt" }
kaspa-wallet-core = { version = "0.14.2", path = "wallet/core" }
kaspa-wallet-macros = { version = "0.14.2", path = "wallet/macros" }
kaspa-wasm = { version = "0.14.2", path = "wasm" }
Expand Down
9 changes: 9 additions & 0 deletions cli/src/modules/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ impl Rpc {
}
}
}
RpcApiOps::GetFeeEstimate => {
let result = rpc.get_fee_estimate_call(GetFeeEstimateRequest {}).await?;
self.println(&ctx, result);
}
RpcApiOps::GetFeeEstimateExperimental => {
let verbose = if argv.is_empty() { false } else { argv.remove(0).parse().unwrap_or(false) };
let result = rpc.get_fee_estimate_experimental_call(GetFeeEstimateExperimentalRequest { verbose }).await?;
self.println(&ctx, result);
}
_ => {
tprintln!(ctx, "rpc method exists but is not supported by the cli: '{op_str}'\r\n");
return Ok(());
Expand Down
1 change: 1 addition & 0 deletions components/addressmanager/src/stores/address_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Entry {
impl MemSizeEstimator for Entry {}

pub trait AddressesStoreReader {
#[allow(dead_code)]
fn get(&self, key: AddressKey) -> Result<Entry, StoreError>;
}

Expand Down
1 change: 1 addition & 0 deletions consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ kaspa-muhash.workspace = true
kaspa-notify.workspace = true
kaspa-pow.workspace = true
kaspa-txscript.workspace = true
kaspa-txscript-errors.workspace = true
kaspa-utils.workspace = true
log.workspace = true
once_cell.workspace = true
Expand Down
47 changes: 47 additions & 0 deletions consensus/core/src/api/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::collections::HashMap;

use crate::tx::TransactionId;

/// A struct provided to consensus for transaction validation processing calls
#[derive(Clone, Debug, Default)]
pub struct TransactionValidationArgs {
/// Optional fee/mass threshold above which a bound transaction in not rejected
pub feerate_threshold: Option<f64>,
}

impl TransactionValidationArgs {
pub fn new(feerate_threshold: Option<f64>) -> Self {
Self { feerate_threshold }
}
}

/// A struct provided to consensus for transactions validation batch processing calls
pub struct TransactionValidationBatchArgs {
tx_args: HashMap<TransactionId, TransactionValidationArgs>,
}

impl TransactionValidationBatchArgs {
const DEFAULT_ARGS: TransactionValidationArgs = TransactionValidationArgs { feerate_threshold: None };

pub fn new() -> Self {
Self { tx_args: HashMap::new() }
}

/// Set some fee/mass threshold for transaction `transaction_id`.
pub fn set_feerate_threshold(&mut self, transaction_id: TransactionId, feerate_threshold: f64) {
self.tx_args
.entry(transaction_id)
.and_modify(|x| x.feerate_threshold = Some(feerate_threshold))
.or_insert(TransactionValidationArgs::new(Some(feerate_threshold)));
}

pub fn get(&self, transaction_id: &TransactionId) -> &TransactionValidationArgs {
self.tx_args.get(transaction_id).unwrap_or(&Self::DEFAULT_ARGS)
}
}

impl Default for TransactionValidationBatchArgs {
fn default() -> Self {
Self::new()
}
}
18 changes: 14 additions & 4 deletions consensus/core/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::Arc;

use crate::{
acceptance_data::AcceptanceData,
api::args::{TransactionValidationArgs, TransactionValidationBatchArgs},
block::{Block, BlockTemplate, TemplateBuildMode, TemplateTransactionSelector, VirtualStateApproxId},
blockstatus::BlockStatus,
coinbase::MinerData,
Expand All @@ -25,6 +26,7 @@ use kaspa_hashes::Hash;

pub use self::stats::{BlockCount, ConsensusStats};

pub mod args;
pub mod counters;
pub mod stats;

Expand Down Expand Up @@ -62,14 +64,18 @@ pub trait ConsensusApi: Send + Sync {
}

/// Populates the mempool transaction with maximally found UTXO entry data and proceeds to full transaction
/// validation if all are found. If validation is successful, also [`transaction.calculated_fee`] is expected to be populated.
fn validate_mempool_transaction(&self, transaction: &mut MutableTransaction) -> TxResult<()> {
/// validation if all are found. If validation is successful, also `transaction.calculated_fee` is expected to be populated.
fn validate_mempool_transaction(&self, transaction: &mut MutableTransaction, args: &TransactionValidationArgs) -> TxResult<()> {
unimplemented!()
}

/// Populates the mempool transactions with maximally found UTXO entry data and proceeds to full transactions
/// validation if all are found. If validation is successful, also [`transaction.calculated_fee`] is expected to be populated.
fn validate_mempool_transactions_in_parallel(&self, transactions: &mut [MutableTransaction]) -> Vec<TxResult<()>> {
/// validation if all are found. If validation is successful, also `transaction.calculated_fee` is expected to be populated.
fn validate_mempool_transactions_in_parallel(
&self,
transactions: &mut [MutableTransaction],
args: &TransactionValidationBatchArgs,
) -> Vec<TxResult<()>> {
unimplemented!()
}

Expand Down Expand Up @@ -184,6 +190,10 @@ pub trait ConsensusApi: Send + Sync {
unimplemented!()
}

fn calc_transaction_hash_merkle_root(&self, txs: &[Transaction], pov_daa_score: u64) -> Hash {
unimplemented!()
}

fn validate_pruning_proof(&self, proof: &PruningPointProof) -> PruningImportResult<()> {
unimplemented!()
}
Expand Down
28 changes: 27 additions & 1 deletion consensus/core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
BlueWorkType,
};
use kaspa_hashes::Hash;
use kaspa_utils::mem_size::MemSizeEstimator;
use std::sync::Arc;

/// A mutable block structure where header and transactions within can still be mutated.
Expand Down Expand Up @@ -66,6 +67,20 @@ impl Block {
pub fn from_precomputed_hash(hash: Hash, parents: Vec<Hash>) -> Block {
Block::from_header(Header::from_precomputed_hash(hash, parents))
}

pub fn asses_for_cache(&self) -> Option<()> {
(self.estimate_mem_bytes() < 1_000_000).then_some(())
}
}

impl MemSizeEstimator for Block {
fn estimate_mem_bytes(&self) -> usize {
// Calculates mem bytes of the block (for cache tracking purposes)
size_of::<Self>()
+ self.header.estimate_mem_bytes()
+ size_of::<Vec<Transaction>>()
+ self.transactions.iter().map(Transaction::estimate_mem_bytes).sum::<usize>()
}
}

/// An abstraction for a recallable transaction selector with persistent state
Expand Down Expand Up @@ -105,6 +120,8 @@ pub struct BlockTemplate {
pub selected_parent_timestamp: u64,
pub selected_parent_daa_score: u64,
pub selected_parent_hash: Hash,
/// Expected length is one less than txs length due to lack of coinbase transaction
pub calculated_fees: Vec<u64>,
}

impl BlockTemplate {
Expand All @@ -115,8 +132,17 @@ impl BlockTemplate {
selected_parent_timestamp: u64,
selected_parent_daa_score: u64,
selected_parent_hash: Hash,
calculated_fees: Vec<u64>,
) -> Self {
Self { block, miner_data, coinbase_has_red_reward, selected_parent_timestamp, selected_parent_daa_score, selected_parent_hash }
Self {
block,
miner_data,
coinbase_has_red_reward,
selected_parent_timestamp,
selected_parent_daa_score,
selected_parent_hash,
calculated_fees,
}
}

pub fn to_virtual_state_approx_id(&self) -> VirtualStateApproxId {
Expand Down
2 changes: 1 addition & 1 deletion consensus/core/src/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ mod tests {
fn test_genesis_hashes() {
[GENESIS, TESTNET_GENESIS, TESTNET11_GENESIS, SIMNET_GENESIS, DEVNET_GENESIS].into_iter().for_each(|genesis| {
let block: Block = (&genesis).into();
assert_hashes_eq(calc_hash_merkle_root(block.transactions.iter()), block.header.hash_merkle_root);
assert_hashes_eq(calc_hash_merkle_root(block.transactions.iter(), false), block.header.hash_merkle_root);
assert_hashes_eq(block.hash(), genesis.hash);
});
}
Expand Down
3 changes: 2 additions & 1 deletion consensus/core/src/config/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ pub const SIMNET_PARAMS: Params = Params {
target_time_per_block: Testnet11Bps::target_time_per_block(),
past_median_time_sample_rate: Testnet11Bps::past_median_time_sample_rate(),
difficulty_sample_rate: Testnet11Bps::difficulty_adjustment_sample_rate(),
max_block_parents: Testnet11Bps::max_block_parents(),
// For simnet, we deviate from TN11 configuration and allow at least 64 parents in order to support mempool benchmarks out of the box
max_block_parents: if Testnet11Bps::max_block_parents() > 64 { Testnet11Bps::max_block_parents() } else { 64 },
mergeset_size_limit: Testnet11Bps::mergeset_size_limit(),
merge_depth: Testnet11Bps::merge_depth_bound(),
finality_depth: Testnet11Bps::finality_depth(),
Expand Down
12 changes: 12 additions & 0 deletions consensus/core/src/errors/tx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::constants::MAX_SOMPI;
use crate::subnets::SubnetworkId;
use crate::tx::TransactionOutpoint;
use kaspa_txscript_errors::TxScriptError;
use thiserror::Error;
Expand Down Expand Up @@ -80,6 +81,9 @@ pub enum TxRuleError {
#[error("failed to verify the signature script: {0}")]
SignatureInvalid(TxScriptError),

#[error("failed to verify empty signature script. Inner error: {0}")]
SignatureEmpty(TxScriptError),

#[error("input {0} sig op count is {1}, but the calculated value is {2}")]
WrongSigOpCount(usize, u64, u64),

Expand All @@ -88,6 +92,14 @@ pub enum TxRuleError {

#[error("calculated contextual mass (including storage mass) {0} is not equal to the committed mass field {1}")]
WrongMass(u64, u64),

#[error("transaction subnetwork id {0} is neither native nor coinbase")]
SubnetworksDisabled(SubnetworkId),

/// [`TxRuleError::FeerateTooLow`] is not a consensus error but a mempool error triggered by the
/// fee/mass RBF validation rule
#[error("fee rate per contextual mass gram is not greater than the fee rate of the replaced transaction")]
FeerateTooLow,
}

pub type TxResult<T> = std::result::Result<T, TxRuleError>;
7 changes: 7 additions & 0 deletions consensus/core/src/header.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{hashing, BlueWorkType};
use borsh::{BorshDeserialize, BorshSerialize};
use kaspa_hashes::Hash;
use kaspa_utils::mem_size::MemSizeEstimator;
use serde::{Deserialize, Serialize};

/// @category Consensus
Expand Down Expand Up @@ -92,6 +93,12 @@ impl Header {
}
}

impl MemSizeEstimator for Header {
fn estimate_mem_bytes(&self) -> usize {
size_of::<Self>() + self.parents_by_level.iter().map(|l| l.len()).sum::<usize>() * size_of::<Hash>()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 2 additions & 6 deletions consensus/core/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ use crate::{hashing, tx::Transaction};
use kaspa_hashes::Hash;
use kaspa_merkle::calc_merkle_root;

pub fn calc_hash_merkle_root_with_options<'a>(txs: impl ExactSizeIterator<Item = &'a Transaction>, include_mass_field: bool) -> Hash {
pub fn calc_hash_merkle_root<'a>(txs: impl ExactSizeIterator<Item = &'a Transaction>, include_mass_field: bool) -> Hash {
calc_merkle_root(txs.map(|tx| hashing::tx::hash(tx, include_mass_field)))
}

pub fn calc_hash_merkle_root<'a>(txs: impl ExactSizeIterator<Item = &'a Transaction>) -> Hash {
calc_merkle_root(txs.map(|tx| hashing::tx::hash(tx, false)))
}

#[cfg(test)]
mod tests {
use crate::merkle::calc_hash_merkle_root;
Expand Down Expand Up @@ -242,7 +238,7 @@ mod tests {
),
];
assert_eq!(
calc_hash_merkle_root(txs.iter()),
calc_hash_merkle_root(txs.iter(), false),
Hash::from_slice(&[
0x46, 0xec, 0xf4, 0x5b, 0xe3, 0xba, 0xca, 0x34, 0x9d, 0xfe, 0x8a, 0x78, 0xde, 0xaf, 0x05, 0x3b, 0x0a, 0xa6, 0xd5,
0x38, 0x97, 0x4d, 0xa5, 0x0f, 0xd6, 0xef, 0xb4, 0xd2, 0x66, 0xbc, 0x8d, 0x21,
Expand Down
Loading

0 comments on commit d0484df

Please sign in to comment.