Skip to content

Commit d0484df

Browse files
committed
Merge branch 'gd-optimization' into gd-tn11-uni-2
2 parents aad317d + 46dbac3 commit d0484df

File tree

111 files changed

+4958
-772
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+4958
-772
lines changed

Cargo.lock

Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ kaspa-utxoindex = { version = "0.14.2", path = "indexes/utxoindex" }
124124
kaspa-wallet = { version = "0.14.2", path = "wallet/native" }
125125
kaspa-wallet-cli-wasm = { version = "0.14.2", path = "wallet/wasm" }
126126
kaspa-wallet-keys = { version = "0.14.2", path = "wallet/keys" }
127-
kaspa-wallet-pskt = { version = "0.14.1", path = "wallet/pskt" }
127+
kaspa-wallet-pskt = { version = "0.14.2", path = "wallet/pskt" }
128128
kaspa-wallet-core = { version = "0.14.2", path = "wallet/core" }
129129
kaspa-wallet-macros = { version = "0.14.2", path = "wallet/macros" }
130130
kaspa-wasm = { version = "0.14.2", path = "wasm" }

cli/src/modules/rpc.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ impl Rpc {
229229
}
230230
}
231231
}
232+
RpcApiOps::GetFeeEstimate => {
233+
let result = rpc.get_fee_estimate_call(GetFeeEstimateRequest {}).await?;
234+
self.println(&ctx, result);
235+
}
236+
RpcApiOps::GetFeeEstimateExperimental => {
237+
let verbose = if argv.is_empty() { false } else { argv.remove(0).parse().unwrap_or(false) };
238+
let result = rpc.get_fee_estimate_experimental_call(GetFeeEstimateExperimentalRequest { verbose }).await?;
239+
self.println(&ctx, result);
240+
}
232241
_ => {
233242
tprintln!(ctx, "rpc method exists but is not supported by the cli: '{op_str}'\r\n");
234243
return Ok(());

components/addressmanager/src/stores/address_store.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct Entry {
2121
impl MemSizeEstimator for Entry {}
2222

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

consensus/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ kaspa-muhash.workspace = true
3030
kaspa-notify.workspace = true
3131
kaspa-pow.workspace = true
3232
kaspa-txscript.workspace = true
33+
kaspa-txscript-errors.workspace = true
3334
kaspa-utils.workspace = true
3435
log.workspace = true
3536
once_cell.workspace = true

consensus/core/src/api/args.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::collections::HashMap;
2+
3+
use crate::tx::TransactionId;
4+
5+
/// A struct provided to consensus for transaction validation processing calls
6+
#[derive(Clone, Debug, Default)]
7+
pub struct TransactionValidationArgs {
8+
/// Optional fee/mass threshold above which a bound transaction in not rejected
9+
pub feerate_threshold: Option<f64>,
10+
}
11+
12+
impl TransactionValidationArgs {
13+
pub fn new(feerate_threshold: Option<f64>) -> Self {
14+
Self { feerate_threshold }
15+
}
16+
}
17+
18+
/// A struct provided to consensus for transactions validation batch processing calls
19+
pub struct TransactionValidationBatchArgs {
20+
tx_args: HashMap<TransactionId, TransactionValidationArgs>,
21+
}
22+
23+
impl TransactionValidationBatchArgs {
24+
const DEFAULT_ARGS: TransactionValidationArgs = TransactionValidationArgs { feerate_threshold: None };
25+
26+
pub fn new() -> Self {
27+
Self { tx_args: HashMap::new() }
28+
}
29+
30+
/// Set some fee/mass threshold for transaction `transaction_id`.
31+
pub fn set_feerate_threshold(&mut self, transaction_id: TransactionId, feerate_threshold: f64) {
32+
self.tx_args
33+
.entry(transaction_id)
34+
.and_modify(|x| x.feerate_threshold = Some(feerate_threshold))
35+
.or_insert(TransactionValidationArgs::new(Some(feerate_threshold)));
36+
}
37+
38+
pub fn get(&self, transaction_id: &TransactionId) -> &TransactionValidationArgs {
39+
self.tx_args.get(transaction_id).unwrap_or(&Self::DEFAULT_ARGS)
40+
}
41+
}
42+
43+
impl Default for TransactionValidationBatchArgs {
44+
fn default() -> Self {
45+
Self::new()
46+
}
47+
}

consensus/core/src/api/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44

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

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

29+
pub mod args;
2830
pub mod counters;
2931
pub mod stats;
3032

@@ -62,14 +64,18 @@ pub trait ConsensusApi: Send + Sync {
6264
}
6365

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

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

@@ -184,6 +190,10 @@ pub trait ConsensusApi: Send + Sync {
184190
unimplemented!()
185191
}
186192

193+
fn calc_transaction_hash_merkle_root(&self, txs: &[Transaction], pov_daa_score: u64) -> Hash {
194+
unimplemented!()
195+
}
196+
187197
fn validate_pruning_proof(&self, proof: &PruningPointProof) -> PruningImportResult<()> {
188198
unimplemented!()
189199
}

consensus/core/src/block.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
BlueWorkType,
66
};
77
use kaspa_hashes::Hash;
8+
use kaspa_utils::mem_size::MemSizeEstimator;
89
use std::sync::Arc;
910

1011
/// A mutable block structure where header and transactions within can still be mutated.
@@ -66,6 +67,20 @@ impl Block {
6667
pub fn from_precomputed_hash(hash: Hash, parents: Vec<Hash>) -> Block {
6768
Block::from_header(Header::from_precomputed_hash(hash, parents))
6869
}
70+
71+
pub fn asses_for_cache(&self) -> Option<()> {
72+
(self.estimate_mem_bytes() < 1_000_000).then_some(())
73+
}
74+
}
75+
76+
impl MemSizeEstimator for Block {
77+
fn estimate_mem_bytes(&self) -> usize {
78+
// Calculates mem bytes of the block (for cache tracking purposes)
79+
size_of::<Self>()
80+
+ self.header.estimate_mem_bytes()
81+
+ size_of::<Vec<Transaction>>()
82+
+ self.transactions.iter().map(Transaction::estimate_mem_bytes).sum::<usize>()
83+
}
6984
}
7085

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

110127
impl BlockTemplate {
@@ -115,8 +132,17 @@ impl BlockTemplate {
115132
selected_parent_timestamp: u64,
116133
selected_parent_daa_score: u64,
117134
selected_parent_hash: Hash,
135+
calculated_fees: Vec<u64>,
118136
) -> Self {
119-
Self { block, miner_data, coinbase_has_red_reward, selected_parent_timestamp, selected_parent_daa_score, selected_parent_hash }
137+
Self {
138+
block,
139+
miner_data,
140+
coinbase_has_red_reward,
141+
selected_parent_timestamp,
142+
selected_parent_daa_score,
143+
selected_parent_hash,
144+
calculated_fees,
145+
}
120146
}
121147

122148
pub fn to_virtual_state_approx_id(&self) -> VirtualStateApproxId {

consensus/core/src/config/genesis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ mod tests {
231231
fn test_genesis_hashes() {
232232
[GENESIS, TESTNET_GENESIS, TESTNET11_GENESIS, SIMNET_GENESIS, DEVNET_GENESIS].into_iter().for_each(|genesis| {
233233
let block: Block = (&genesis).into();
234-
assert_hashes_eq(calc_hash_merkle_root(block.transactions.iter()), block.header.hash_merkle_root);
234+
assert_hashes_eq(calc_hash_merkle_root(block.transactions.iter(), false), block.header.hash_merkle_root);
235235
assert_hashes_eq(block.hash(), genesis.hash);
236236
});
237237
}

consensus/core/src/config/params.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ pub const SIMNET_PARAMS: Params = Params {
501501
target_time_per_block: Testnet11Bps::target_time_per_block(),
502502
past_median_time_sample_rate: Testnet11Bps::past_median_time_sample_rate(),
503503
difficulty_sample_rate: Testnet11Bps::difficulty_adjustment_sample_rate(),
504-
max_block_parents: Testnet11Bps::max_block_parents(),
504+
// For simnet, we deviate from TN11 configuration and allow at least 64 parents in order to support mempool benchmarks out of the box
505+
max_block_parents: if Testnet11Bps::max_block_parents() > 64 { Testnet11Bps::max_block_parents() } else { 64 },
505506
mergeset_size_limit: Testnet11Bps::mergeset_size_limit(),
506507
merge_depth: Testnet11Bps::merge_depth_bound(),
507508
finality_depth: Testnet11Bps::finality_depth(),

consensus/core/src/errors/tx.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::constants::MAX_SOMPI;
2+
use crate::subnets::SubnetworkId;
23
use crate::tx::TransactionOutpoint;
34
use kaspa_txscript_errors::TxScriptError;
45
use thiserror::Error;
@@ -80,6 +81,9 @@ pub enum TxRuleError {
8081
#[error("failed to verify the signature script: {0}")]
8182
SignatureInvalid(TxScriptError),
8283

84+
#[error("failed to verify empty signature script. Inner error: {0}")]
85+
SignatureEmpty(TxScriptError),
86+
8387
#[error("input {0} sig op count is {1}, but the calculated value is {2}")]
8488
WrongSigOpCount(usize, u64, u64),
8589

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

8993
#[error("calculated contextual mass (including storage mass) {0} is not equal to the committed mass field {1}")]
9094
WrongMass(u64, u64),
95+
96+
#[error("transaction subnetwork id {0} is neither native nor coinbase")]
97+
SubnetworksDisabled(SubnetworkId),
98+
99+
/// [`TxRuleError::FeerateTooLow`] is not a consensus error but a mempool error triggered by the
100+
/// fee/mass RBF validation rule
101+
#[error("fee rate per contextual mass gram is not greater than the fee rate of the replaced transaction")]
102+
FeerateTooLow,
91103
}
92104

93105
pub type TxResult<T> = std::result::Result<T, TxRuleError>;

consensus/core/src/header.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{hashing, BlueWorkType};
22
use borsh::{BorshDeserialize, BorshSerialize};
33
use kaspa_hashes::Hash;
4+
use kaspa_utils::mem_size::MemSizeEstimator;
45
use serde::{Deserialize, Serialize};
56

67
/// @category Consensus
@@ -92,6 +93,12 @@ impl Header {
9293
}
9394
}
9495

96+
impl MemSizeEstimator for Header {
97+
fn estimate_mem_bytes(&self) -> usize {
98+
size_of::<Self>() + self.parents_by_level.iter().map(|l| l.len()).sum::<usize>() * size_of::<Hash>()
99+
}
100+
}
101+
95102
#[cfg(test)]
96103
mod tests {
97104
use super::*;

consensus/core/src/merkle.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ use crate::{hashing, tx::Transaction};
22
use kaspa_hashes::Hash;
33
use kaspa_merkle::calc_merkle_root;
44

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

9-
pub fn calc_hash_merkle_root<'a>(txs: impl ExactSizeIterator<Item = &'a Transaction>) -> Hash {
10-
calc_merkle_root(txs.map(|tx| hashing::tx::hash(tx, false)))
11-
}
12-
139
#[cfg(test)]
1410
mod tests {
1511
use crate::merkle::calc_hash_merkle_root;
@@ -242,7 +238,7 @@ mod tests {
242238
),
243239
];
244240
assert_eq!(
245-
calc_hash_merkle_root(txs.iter()),
241+
calc_hash_merkle_root(txs.iter(), false),
246242
Hash::from_slice(&[
247243
0x46, 0xec, 0xf4, 0x5b, 0xe3, 0xba, 0xca, 0x34, 0x9d, 0xfe, 0x8a, 0x78, 0xde, 0xaf, 0x05, 0x3b, 0x0a, 0xa6, 0xd5,
248244
0x38, 0x97, 0x4d, 0xa5, 0x0f, 0xd6, 0xef, 0xb4, 0xd2, 0x66, 0xbc, 0x8d, 0x21,

0 commit comments

Comments
 (0)