Class TransactionMempool
TransactionPrioritizer (FR-MEMPOOL-01 through FR-MEMPOOL-05).
Milestone 8 wiring (T-066)
When constructed with a BlockchainEventBus the mempool automatically:
- Publishes a
BlockchainEvent.TransactionSubmittedEventon every successfulsubmit(Transaction)call. - Registers an internal
BlockchainEventListenerthat listens forBlockchainEvent.BlockAddedEventand removes all transactions that were confirmed in that block (FR-MEMPOOL-05).
Thread safety
All public methods are protected by a ReentrantLock. The event listener
callback onEvent() also acquires the same lock before modifying the pool,
so concurrent submissions and block-confirmations are always safe.
Usage
TransactionMempool mempool = new TransactionMempool(
new FeeBasedPrioritizer(), eventBus);
mempool.start(Duration.ofMinutes(10)); // enable TTL eviction
MempoolSubmissionResult result = mempool.submitWithValidation(tx, blockchain);
if (result.isAccepted()) {
// tx is in the pool and TransactionSubmittedEvent has been published
}
- Since:
- 1.0.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionTransactionMempool(TransactionPrioritizer prioritizer) Creates a mempool with a prioritizer and no event-bus integration.TransactionMempool(TransactionPrioritizer prioritizer, int maxSize) Creates a mempool with a prioritizer, no validator, and bounded capacity.TransactionMempool(TransactionPrioritizer prioritizer, BlockchainEventBus eventBus) Creates a mempool with a prioritizer, event-bus integration, and the default unbounded capacity.TransactionMempool(TransactionPrioritizer prioritizer, TransactionValidator validator) Creates a mempool with a prioritizer, optional validator, and unbounded capacity.TransactionMempool(TransactionPrioritizer prioritizer, TransactionValidator validator, int maxSize) Creates a mempool with a prioritizer, optional validator, bounded capacity, and no event-bus integration.TransactionMempool(TransactionPrioritizer prioritizer, TransactionValidator validator, int maxSize, BlockchainEventBus eventBus) Full constructor wiring all dependencies. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all transactions from the pool.booleanReturnstrueif the pool contains a transaction with the given ID.voidevictExpired(Duration ttl) Evicts transactions older than the given TTL (FR-MEMPOOL-04).getTopN(int n) Returns the topntransactions ordered by the configured prioritizer.booleanRemoves a specific transaction from the pool.intsize()Returns the current number of transactions in the pool.voidStarts the background TTL eviction scheduler.voidstop()Stops the background eviction scheduler and clears the pool.booleansubmit(Transaction transaction) Submits a transaction to the pool without pre-validation.submitWithValidation(Transaction transaction, Blockchain chain) Validates and submits a transaction using the configuredTransactionValidator.
-
Constructor Details
-
TransactionMempool
Creates a mempool with a prioritizer and no event-bus integration.- Parameters:
prioritizer- ordering strategy for pending transactions (non-null)- Throws:
NullPointerException- if prioritizer is null
-
TransactionMempool
Creates a mempool with a prioritizer, event-bus integration, and the default unbounded capacity.When
eventBusis non-null the mempool:- Publishes
BlockchainEvent.TransactionSubmittedEventonsubmit(com.privatechain.core.model.Transaction). - Registers a listener that removes confirmed transactions on
BlockchainEvent.BlockAddedEvent.
- Parameters:
prioritizer- ordering strategy for pending transactions (non-null)eventBus- event bus to wire into; may benullfor standalone mode- Throws:
NullPointerException- if prioritizer is null
- Publishes
-
TransactionMempool
Creates a mempool with a prioritizer, optional validator, and unbounded capacity.- Parameters:
prioritizer- ordering strategy (non-null)validator- pre-submit validator; may benull- Throws:
NullPointerException- if prioritizer is null
-
TransactionMempool
Creates a mempool with a prioritizer, no validator, and bounded capacity.- Parameters:
prioritizer- ordering strategy (non-null)maxSize- maximum pool capacity (≥ 1)- Throws:
NullPointerException- if prioritizer is nullIllegalArgumentException- if maxSize < 1
-
TransactionMempool
public TransactionMempool(TransactionPrioritizer prioritizer, TransactionValidator validator, int maxSize) Creates a mempool with a prioritizer, optional validator, bounded capacity, and no event-bus integration.- Parameters:
prioritizer- ordering strategy (non-null)validator- pre-submit validator; may benullmaxSize- maximum pool capacity (≥ 1)- Throws:
NullPointerException- if prioritizer is nullIllegalArgumentException- if maxSize < 1
-
TransactionMempool
public TransactionMempool(TransactionPrioritizer prioritizer, TransactionValidator validator, int maxSize, BlockchainEventBus eventBus) Full constructor wiring all dependencies.- Parameters:
prioritizer- ordering strategy (non-null)validator- pre-submit validator; may benullmaxSize- maximum pool capacity (≥ 1)eventBus- event bus; may benull- Throws:
NullPointerException- if prioritizer is nullIllegalArgumentException- if maxSize < 1
-
-
Method Details
-
submit
Submits a transaction to the pool without pre-validation.The transaction is rejected if:
- The pool already contains a transaction with the same ID (duplicate)
- The pool is at capacity and the new transaction has lower priority than all existing transactions (lowest-priority eviction applies)
On acceptance, if an event bus is wired, a
BlockchainEvent.TransactionSubmittedEventis published asynchronously.- Parameters:
transaction- the transaction to submit (non-null)- Returns:
trueif the transaction was accepted;falseif rejected- Throws:
NullPointerException- if transaction is null
-
submitWithValidation
Validates and submits a transaction using the configuredTransactionValidator.If no validator was provided at construction time, this behaves identically to
submit(Transaction).- Parameters:
transaction- the transaction to validate and submit (non-null)chain- the current blockchain state used during validation (non-null)- Returns:
- a
MempoolSubmissionResultdescribing the outcome - Throws:
NullPointerException- if transaction or chain is null
-
getTopN
Returns the topntransactions ordered by the configured prioritizer.The returned list is a snapshot; modifications to the pool do not affect it.
- Parameters:
n- maximum number of transactions to return (≥ 0)- Returns:
- ordered list with at most
nentries - Throws:
IllegalArgumentException- if n is negative
-
size
public int size()Returns the current number of transactions in the pool.- Returns:
- pool size (≥ 0)
-
contains
Returnstrueif the pool contains a transaction with the given ID.- Parameters:
transactionId- the ID to look up (non-null)- Returns:
trueif present- Throws:
NullPointerException- if transactionId is null
-
remove
Removes a specific transaction from the pool.- Parameters:
transactionId- the ID of the transaction to remove (non-null)- Returns:
trueif the transaction was found and removed- Throws:
NullPointerException- if transactionId is null
-
clear
public void clear()Removes all transactions from the pool. -
evictExpired
Evicts transactions older than the given TTL (FR-MEMPOOL-04).Called automatically by the background scheduler after
start(java.time.Duration)is invoked. May also be called manually for an immediate eviction pass.- Parameters:
ttl- the time-to-live duration (non-null, positive)- Throws:
NullPointerException- if ttl is null
-
start
Starts the background TTL eviction scheduler.After this call,
evictExpired(Duration)is called at a fixed interval equal tottl / 2(minimum 10 seconds). The scheduler runs as a daemon thread and stops whenstop()is called.- Parameters:
ttl- the time-to-live for unconfirmed transactions (non-null, positive)- Throws:
NullPointerException- if ttl is nullIllegalStateException- if the scheduler is already running
-
stop
public void stop()Stops the background eviction scheduler and clears the pool.After this call, no further automatic eviction occurs. The pool is cleared so that stale references are not retained after node shutdown.
-