Skip to content

Commit cca338f

Browse files
A few small mempool fixes (#212)
* use `request.allow_orphan` when passing the rpc tx to the mempool * minor * include the original accepted transaction as well * for now keep the original timestamp deviation tolerance
1 parent 3d2f000 commit cca338f

File tree

11 files changed

+41
-28
lines changed

11 files changed

+41
-28
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

consensus/core/src/config/bps.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ impl<const BPS: u64> Bps<BPS> {
8585
}
8686

8787
pub const fn pruning_proof_m() -> u64 {
88-
// Since the important levels remain logarithmically long, it seems that this
89-
// constant does not need to scale with BPS.
90-
// TODO: finalize this
88+
// No need to scale this constant with BPS since the important block levels (higher) remain logarithmically long
9189
PRUNING_PROOF_M
9290
}
9391

@@ -120,10 +118,6 @@ impl<const BPS: u64> Bps<BPS> {
120118
pub const fn pre_deflationary_phase_base_subsidy() -> u64 {
121119
50000000000 / BPS
122120
}
123-
124-
// TODO: we might need to increase max_block_level (at least for mainnet) as a function of BPS
125-
// since higher BPS means easier difficulty puzzles -> less zeros in pow hash
126-
// pub const fn max_block_level() -> u64 { }
127121
}
128122

129123
#[cfg(test)]

consensus/core/src/config/constants.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub mod consensus {
2727
pub const LEGACY_TIMESTAMP_DEVIATION_TOLERANCE: u64 = 132;
2828

2929
/// **New** timestamp deviation tolerance (seconds).
30-
/// KIP-0004: 605 (~10 minutes)
31-
pub const NEW_TIMESTAMP_DEVIATION_TOLERANCE: u64 = 605;
30+
/// TODO: KIP-0004: 605 (~10 minutes)
31+
pub const NEW_TIMESTAMP_DEVIATION_TOLERANCE: u64 = 132;
3232

3333
/// The desired interval between samples of the median time window (seconds).
3434
/// KIP-0004: 10 seconds

mining/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ kaspa-txscript.workspace = true
1515
kaspa-core.workspace = true
1616
kaspa-mining-errors.workspace = true
1717
kaspa-consensusmanager.workspace = true
18+
kaspa-utils.workspace = true
1819
thiserror.workspace = true
1920
serde.workspace = true
2021
log.workspace = true

mining/src/manager_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,9 @@ mod tests {
476476
let unorphaned_txs = result.unwrap();
477477
let (populated_txs, orphans) = mining_manager.get_all_transactions(true, true);
478478
assert_eq!(
479-
unorphaned_txs.len(), SKIPPED_TXS,
479+
unorphaned_txs.len(), SKIPPED_TXS + 1,
480480
"the mempool is expected to have unorphaned the remaining child transaction after the matching parent transaction was inserted into the mempool: expected: {}, got: {}",
481-
unorphaned_txs.len(), SKIPPED_TXS
481+
SKIPPED_TXS + 1, unorphaned_txs.len()
482482
);
483483
assert_eq!(
484484
SKIPPED_TXS + SKIPPED_TXS,
@@ -632,13 +632,13 @@ mod tests {
632632
let unorphaned_txs = result.as_ref().unwrap();
633633
assert_eq!(
634634
test.should_unorphan,
635-
!unorphaned_txs.is_empty(),
635+
unorphaned_txs.len() > 1,
636636
"{}: child transaction should have been {} the orphan pool",
637637
test.name,
638638
test.parent_insert_result()
639639
);
640-
if !unorphaned_txs.is_empty() {
641-
assert_eq!(unorphaned_txs[0].id(), child_txs[i].id(), "the unorphaned transaction should match the inserted parent");
640+
if unorphaned_txs.len() > 1 {
641+
assert_eq!(unorphaned_txs[1].id(), child_txs[i].id(), "the unorphaned transaction should match the inserted parent");
642642
}
643643
}
644644
}

mining/src/mempool/validate_and_insert_transaction.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use kaspa_consensus_core::{
1111
tx::{MutableTransaction, Transaction, TransactionId, TransactionOutpoint, UtxoEntry},
1212
};
1313
use kaspa_core::info;
14+
use kaspa_utils::vec::VecExtensions;
1415

1516
use super::tx::{Orphan, Priority};
1617

@@ -60,8 +61,10 @@ impl Mempool {
6061
// transaction reference and mutably for the call to process_orphans_after_accepted_transaction
6162
let accepted_transaction =
6263
self.transaction_pool.add_transaction(transaction, consensus.get_virtual_daa_score(), priority)?.mtx.tx.clone();
63-
let accepted_orphans = self.process_orphans_after_accepted_transaction(consensus, &accepted_transaction)?;
64-
Ok(accepted_orphans)
64+
let mut accepted_transactions = self.process_orphans_after_accepted_transaction(consensus, &accepted_transaction)?;
65+
// We include the original accepted transaction as well
66+
accepted_transactions.swap_insert(0, accepted_transaction);
67+
Ok(accepted_transactions)
6568
}
6669

6770
fn validate_transaction_pre_utxo_entry(&self, transaction: &MutableTransaction) -> RuleResult<()> {
@@ -98,16 +101,12 @@ impl Mempool {
98101
) -> RuleResult<Vec<Arc<Transaction>>> {
99102
// Rust rewrite:
100103
// - The function is relocated from OrphanPool into Mempool
101-
let mut added_transactions = Vec::new();
102-
let mut unorphaned_transactions =
103-
self.get_unorphaned_transactions_after_accepted_transaction(consensus, accepted_transaction)?;
104-
while !unorphaned_transactions.is_empty() {
105-
let transaction = unorphaned_transactions.pop().unwrap();
106-
104+
let unorphaned_transactions = self.get_unorphaned_transactions_after_accepted_transaction(consensus, accepted_transaction)?;
105+
let mut added_transactions = Vec::with_capacity(unorphaned_transactions.len() + 1); // +1 since some callers add the accepted tx itself
106+
for transaction in unorphaned_transactions {
107107
// The returned transactions are leaving the mempool but must also be added to
108108
// the transaction pool so we clone.
109109
added_transactions.push(transaction.mtx.tx.clone());
110-
111110
self.transaction_pool.add_mempool_transaction(transaction)?;
112111
}
113112
Ok(added_transactions)

protocol/flows/src/flow_context.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,12 @@ impl FlowContext {
404404
// TODO: call a handler function or a predefined registered service
405405
}
406406

407-
pub async fn add_transaction(
407+
/// Adds the rpc-submitted transaction to the mempool and propagates it to peers.
408+
///
409+
/// Transactions submitted through rpc are considered high priority. This definition does not affect the tx selection algorithm
410+
/// but only changes how we manage the lifetime of the tx. A high-priority tx does not expire and is repeatedly rebroadcasted to
411+
/// peers
412+
pub async fn submit_rpc_transaction(
408413
&self,
409414
consensus: &ConsensusProxy,
410415
transaction: Transaction,

protocol/p2p/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ path = "./src/bin/server.rs"
2020
[dependencies]
2121
kaspa-core.workspace = true
2222
kaspa-consensus-core.workspace = true
23-
kaspa-mining.workspace = true
23+
kaspa-mining-errors.workspace = true
2424
kaspa-hashes.workspace = true
2525
kaspa-math.workspace = true
2626
kaspa-utils.workspace = true

protocol/p2p/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{convert::error::ConversionError, core::peer::PeerKey, KaspadMessagePayloadType};
22
use kaspa_consensus_core::errors::{block::RuleError, consensus::ConsensusError, pruning::PruningImportError};
3-
use kaspa_mining::errors::MiningManagerError;
3+
use kaspa_mining_errors::manager::MiningManagerError;
44
use std::time::Duration;
55
use thiserror::Error;
66

rpc/service/src/service.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,11 @@ impl RpcApi for RpcCoreService {
384384
let transaction: Transaction = (&request.transaction).try_into()?;
385385
let transaction_id = transaction.id();
386386
let session = self.consensus_manager.consensus().session().await;
387-
self.flow_context.add_transaction(&session, transaction, Orphan::Allowed).await.map_err(|err| {
387+
let orphan = match request.allow_orphan {
388+
true => Orphan::Allowed,
389+
false => Orphan::Forbidden,
390+
};
391+
self.flow_context.submit_rpc_transaction(&session, transaction, orphan).await.map_err(|err| {
388392
let err = RpcError::RejectedTransaction(transaction_id, err.to_string());
389393
debug!("{err}");
390394
err

0 commit comments

Comments
 (0)