mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-28 08:11:08 +00:00
implement expireOldTransactions and expireOrphanTransactions
This commit is contained in:
parent
619b7ab8cd
commit
ff10ce145a
32
domain/miningmanager/mempool/config.go
Normal file
32
domain/miningmanager/mempool/config.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package mempool
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/dagconfig"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultTransactionExpireIntervalSeconds uint64 = 60
|
||||||
|
defaultTransactionExpireScanIntervalSeconds uint64 = 10
|
||||||
|
defaultOrphanExpireIntervalSeconds uint64 = 60
|
||||||
|
defaultOrphanExpireScanIntervalSeconds uint64 = 10
|
||||||
|
)
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
transactionExpireIntervalDAAScore uint64
|
||||||
|
transactionExpireScanIntervalDAAScore uint64
|
||||||
|
orphanExpireIntervalDAAScore uint64
|
||||||
|
orphanExpireScanIntervalDAAScore uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultConfig(dagParams *dagconfig.Params) *config {
|
||||||
|
targetBlocksPerSecond := uint64(dagParams.TargetTimePerBlock / time.Second)
|
||||||
|
|
||||||
|
return &config{
|
||||||
|
transactionExpireIntervalDAAScore: defaultTransactionExpireIntervalSeconds / targetBlocksPerSecond,
|
||||||
|
transactionExpireScanIntervalDAAScore: defaultTransactionExpireScanIntervalSeconds / targetBlocksPerSecond,
|
||||||
|
orphanExpireIntervalDAAScore: defaultOrphanExpireIntervalSeconds / targetBlocksPerSecond,
|
||||||
|
orphanExpireScanIntervalDAAScore: defaultOrphanExpireScanIntervalSeconds / targetBlocksPerSecond,
|
||||||
|
}
|
||||||
|
}
|
||||||
9
domain/miningmanager/mempool/helper_functions.go
Normal file
9
domain/miningmanager/mempool/helper_functions.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package mempool
|
||||||
|
|
||||||
|
func (mp *mempool) virtualDAAScore() (uint64, error) {
|
||||||
|
virtualInfo, err := mp.consensus.GetVirtualInfo()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return virtualInfo.DAAScore, nil
|
||||||
|
}
|
||||||
@ -1,15 +1,24 @@
|
|||||||
package mempool
|
package mempool
|
||||||
|
|
||||||
import "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
import (
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
"github.com/kaspanet/kaspad/domain/dagconfig"
|
||||||
|
)
|
||||||
|
|
||||||
type mempool struct {
|
type mempool struct {
|
||||||
|
config *config
|
||||||
|
consensus externalapi.Consensus
|
||||||
|
|
||||||
mempoolUTXOSet *mempoolUTXOSet
|
mempoolUTXOSet *mempoolUTXOSet
|
||||||
transactionsPool *transactionsPool
|
transactionsPool *transactionsPool
|
||||||
orphansPool *orphansPool
|
orphansPool *orphansPool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMempool() *mempool {
|
func newMempool(consensus externalapi.Consensus, dagParams *dagconfig.Params) *mempool {
|
||||||
mp := &mempool{}
|
mp := &mempool{
|
||||||
|
config: defaultConfig(dagParams),
|
||||||
|
consensus: consensus,
|
||||||
|
}
|
||||||
|
|
||||||
mp.mempoolUTXOSet = newMempoolUTXOSet(mp)
|
mp.mempoolUTXOSet = newMempoolUTXOSet(mp)
|
||||||
mp.transactionsPool = newTransactionsPool(mp)
|
mp.transactionsPool = newTransactionsPool(mp)
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
package mempool
|
package mempool
|
||||||
|
|
||||||
import "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
import (
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
||||||
|
)
|
||||||
|
|
||||||
type idToTransaction map[externalapi.DomainTransactionID]*mempoolTransaction
|
type idToTransaction map[externalapi.DomainTransactionID]*mempoolTransaction
|
||||||
|
|
||||||
@ -11,8 +14,16 @@ type mempoolTransaction struct {
|
|||||||
addAtDAAScore uint64
|
addAtDAAScore uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mt *mempoolTransaction) transactionID() *externalapi.DomainTransactionID {
|
||||||
|
return consensushashing.TransactionID(mt.transaction)
|
||||||
|
}
|
||||||
|
|
||||||
type orphanTransaction struct {
|
type orphanTransaction struct {
|
||||||
transaction externalapi.DomainTransaction
|
transaction *externalapi.DomainTransaction
|
||||||
isHighPriority bool
|
isHighPriority bool
|
||||||
addedAtDAAScore uint64
|
addedAtDAAScore uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ot *orphanTransaction) transactionID() *externalapi.DomainTransactionID {
|
||||||
|
return consensushashing.TransactionID(ot.transaction)
|
||||||
|
}
|
||||||
|
|||||||
@ -8,7 +8,7 @@ type orphansPool struct {
|
|||||||
mempool *mempool
|
mempool *mempool
|
||||||
allOrphans idToTransaction
|
allOrphans idToTransaction
|
||||||
orphanByPreviousOutpoint previousOutpointToOrphans
|
orphanByPreviousOutpoint previousOutpointToOrphans
|
||||||
previousExpireScan uint64
|
lastExpireScan uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func newOrphansPool(mp *mempool) *orphansPool {
|
func newOrphansPool(mp *mempool) *orphansPool {
|
||||||
@ -16,7 +16,7 @@ func newOrphansPool(mp *mempool) *orphansPool {
|
|||||||
mempool: mp,
|
mempool: mp,
|
||||||
allOrphans: idToTransaction{},
|
allOrphans: idToTransaction{},
|
||||||
orphanByPreviousOutpoint: previousOutpointToOrphans{},
|
orphanByPreviousOutpoint: previousOutpointToOrphans{},
|
||||||
previousExpireScan: 0,
|
lastExpireScan: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,5 +41,30 @@ func (op *orphansPool) removeOrphan(orphanTransactionID *externalapi.DomainTrans
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (op *orphansPool) expireOrphanTransactions() error {
|
func (op *orphansPool) expireOrphanTransactions() error {
|
||||||
panic("orphansPool.expireOrphanTransactions not implemented") // TODO (Mike)
|
virtualDAAScore, err := op.mempool.virtualDAAScore()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if virtualDAAScore-op.lastExpireScan < op.mempool.config.orphanExpireScanIntervalDAAScore {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, orphanTransaction := range op.allOrphans {
|
||||||
|
// Never expire high priority transactions
|
||||||
|
if orphanTransaction.isHighPriority {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove all transactions whose addedAtDAAScore is older then transactionExpireIntervalDAAScore
|
||||||
|
if virtualDAAScore-orphanTransaction.addAtDAAScore > op.mempool.config.orphanExpireIntervalDAAScore {
|
||||||
|
err = op.removeOrphan(orphanTransaction.transactionID())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
op.lastExpireScan = virtualDAAScore
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
package mempool
|
package mempool
|
||||||
|
|
||||||
import "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
import (
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
)
|
||||||
|
|
||||||
type outpointToTransaction map[externalapi.DomainOutpoint]*mempoolTransaction
|
type outpointToTransaction map[externalapi.DomainOutpoint]*mempoolTransaction
|
||||||
|
|
||||||
@ -12,6 +14,7 @@ type transactionsPool struct {
|
|||||||
highPriorityTransactions idToTransaction
|
highPriorityTransactions idToTransaction
|
||||||
chainedTransactionsByPreviousOutpoint outpointToTransaction
|
chainedTransactionsByPreviousOutpoint outpointToTransaction
|
||||||
transactionsByFeeRate transactionsByFeeHeap
|
transactionsByFeeRate transactionsByFeeHeap
|
||||||
|
lastExpireScan uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTransactionsPool(mp *mempool) *transactionsPool {
|
func newTransactionsPool(mp *mempool) *transactionsPool {
|
||||||
@ -21,6 +24,7 @@ func newTransactionsPool(mp *mempool) *transactionsPool {
|
|||||||
highPriorityTransactions: idToTransaction{},
|
highPriorityTransactions: idToTransaction{},
|
||||||
chainedTransactionsByPreviousOutpoint: outpointToTransaction{},
|
chainedTransactionsByPreviousOutpoint: outpointToTransaction{},
|
||||||
transactionsByFeeRate: transactionsByFeeHeap{},
|
transactionsByFeeRate: transactionsByFeeHeap{},
|
||||||
|
lastExpireScan: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +37,32 @@ func (tp *transactionsPool) addMempoolTransaction(transaction mempoolTransaction
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tp *transactionsPool) expireOldTransactions() error {
|
func (tp *transactionsPool) expireOldTransactions() error {
|
||||||
panic("transactionsPool.expireOldTransactions not implemented") // TODO (Mike)
|
virtualDAAScore, err := tp.mempool.virtualDAAScore()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if virtualDAAScore-tp.lastExpireScan < tp.mempool.config.transactionExpireScanIntervalDAAScore {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, mempoolTransaction := range tp.allTransactions {
|
||||||
|
// Never expire high priority transactions
|
||||||
|
if mempoolTransaction.isHighPriority {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove all transactions whose addedAtDAAScore is older then transactionExpireIntervalDAAScore
|
||||||
|
if virtualDAAScore-mempoolTransaction.addAtDAAScore > tp.mempool.config.transactionExpireIntervalDAAScore {
|
||||||
|
err = tp.mempool.RemoveTransaction(mempoolTransaction.transactionID())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tp.lastExpireScan = virtualDAAScore
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tp *transactionsPool) allReadyTransactions() []*externalapi.DomainTransaction {
|
func (tp *transactionsPool) allReadyTransactions() []*externalapi.DomainTransaction {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user