access both pools in the same state via parameters

This commit is contained in:
D-Stacks 2022-07-06 00:31:02 +02:00
parent ff95dd0ea6
commit 3ce7c1853d
12 changed files with 195 additions and 176 deletions

View File

@ -102,7 +102,7 @@ func (flow *handleRelayedTransactionsFlow) requestInvTransactions(
func (flow *handleRelayedTransactionsFlow) isKnownTransaction(txID *externalapi.DomainTransactionID) bool { func (flow *handleRelayedTransactionsFlow) isKnownTransaction(txID *externalapi.DomainTransactionID) bool {
// Ask the transaction memory pool if the transaction is known // Ask the transaction memory pool if the transaction is known
// to it in any form (main pool or orphan). // to it in any form (main pool or orphan).
if _, ok := flow.Domain().MiningManager().GetTransaction(txID); ok { if _, _, ok := flow.Domain().MiningManager().GetTransaction(txID, true, true); ok {
return true return true
} }

View File

@ -30,7 +30,11 @@ func (flow *handleRequestedTransactionsFlow) start() error {
} }
for _, transactionID := range msgRequestTransactions.IDs { for _, transactionID := range msgRequestTransactions.IDs {
tx, ok := flow.Domain().MiningManager().GetTransaction(transactionID) //note: below ignores orphan txs that are requested
//find out if this is good or bad practice
//only reference i found to this, is that nodes don't do this in btc
//source: https://arxiv.org/abs/1912.11541 (2nd sentence in abstract)
tx, _, ok := flow.Domain().MiningManager().GetTransaction(transactionID, true, false)
if !ok { if !ok {
msgTransactionNotFound := appmessage.NewMsgTransactionNotFound(transactionID) msgTransactionNotFound := appmessage.NewMsgTransactionNotFound(transactionID)
@ -40,7 +44,6 @@ func (flow *handleRequestedTransactionsFlow) start() error {
} }
continue continue
} }
err := flow.outgoingRoute.Enqueue(appmessage.DomainTransactionToMsgTx(tx)) err := flow.outgoingRoute.Enqueue(appmessage.DomainTransactionToMsgTx(tx))
if err != nil { if err != nil {
return err return err

View File

@ -16,7 +16,7 @@ func HandleGetInfo(context *rpccontext.Context, _ *router.Router, _ appmessage.M
response := appmessage.NewGetInfoResponseMessage( response := appmessage.NewGetInfoResponseMessage(
context.NetAdapter.ID().String(), context.NetAdapter.ID().String(),
uint64(context.Domain.MiningManager().TransactionCount()), uint64(context.Domain.MiningManager().TransactionCount(true, false)),
version.Version(), version.Version(),
context.Config.UTXOIndex, context.Config.UTXOIndex,
context.ProtocolManager.Context().HasPeers() && isNearlySynced, context.ProtocolManager.Context().HasPeers() && isNearlySynced,

View File

@ -12,30 +12,10 @@ func HandleGetMempoolEntries(context *rpccontext.Context, _ *router.Router, requ
entries := make([]*appmessage.MempoolEntry, 0) entries := make([]*appmessage.MempoolEntry, 0)
transactionPoolTransactions, orphanPoolTransactions := context.Domain.MiningManager().AllTransactions(!getMempoolEntriesRequest.FilterTransactionPool, getMempoolEntriesRequest.IncludeOrphanPool)
if !getMempoolEntriesRequest.FilterTransactionPool { if !getMempoolEntriesRequest.FilterTransactionPool {
transactionPoolEntries, err := getTransactionPoolMempoolEntries(context) for _, transaction := range transactionPoolTransactions {
if err != nil {
return nil, err
}
entries = append(entries, transactionPoolEntries...)
}
if getMempoolEntriesRequest.IncludeOrphanPool {
orphanPoolEntries, err := getOrphanPoolMempoolEntries(context)
if err != nil {
return nil, err
}
entries = append(entries, orphanPoolEntries...)
}
return appmessage.NewGetMempoolEntriesResponseMessage(entries), nil
}
func getTransactionPoolMempoolEntries(context *rpccontext.Context) ([]*appmessage.MempoolEntry, error) {
transactions := context.Domain.MiningManager().AllTransactions()
entries := make([]*appmessage.MempoolEntry, 0, len(transactions))
for _, transaction := range transactions {
rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction) rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction)
err := context.PopulateTransactionWithVerboseData(rpcTransaction, nil) err := context.PopulateTransactionWithVerboseData(rpcTransaction, nil)
if err != nil { if err != nil {
@ -47,23 +27,21 @@ func getTransactionPoolMempoolEntries(context *rpccontext.Context) ([]*appmessag
IsOrphan: false, IsOrphan: false,
}) })
} }
return entries, nil }
} if getMempoolEntriesRequest.IncludeOrphanPool {
for _, transaction := range orphanPoolTransactions {
func getOrphanPoolMempoolEntries(context *rpccontext.Context) ([]*appmessage.MempoolEntry, error) { rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction)
orphanTransactions := context.Domain.MiningManager().AllOrphanTransactions()
entries := make([]*appmessage.MempoolEntry, 0, len(orphanTransactions))
for _, orphanTransaction := range orphanTransactions {
rpcTransaction := appmessage.DomainTransactionToRPCTransaction(orphanTransaction)
err := context.PopulateTransactionWithVerboseData(rpcTransaction, nil) err := context.PopulateTransactionWithVerboseData(rpcTransaction, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
entries = append(entries, &appmessage.MempoolEntry{ entries = append(entries, &appmessage.MempoolEntry{
Fee: orphanTransaction.Fee, Fee: transaction.Fee,
Transaction: rpcTransaction, Transaction: rpcTransaction,
IsOrphan: true, IsOrphan: true,
}) })
} }
return entries, nil }
return appmessage.NewGetMempoolEntriesResponseMessage(entries), nil
} }

View File

@ -15,6 +15,11 @@ func HandleGetMempoolEntriesByAddresses(context *rpccontext.Context, _ *router.R
mempoolEntriesByAddresses := make([]*appmessage.MempoolEntryByAddress, 0) mempoolEntriesByAddresses := make([]*appmessage.MempoolEntryByAddress, 0)
sendingInTransactionPool, receivingInTransactionPool, sendingInOrphanPool, receivingInOrphanPool, err := context.Domain.MiningManager().GetTransactionsByAddresses(!getMempoolEntriesByAddressesRequest.FilterTransactionPool, getMempoolEntriesByAddressesRequest.IncludeOrphanPool)
if err != nil {
return nil, err
}
for _, addressString := range getMempoolEntriesByAddressesRequest.Addresses { for _, addressString := range getMempoolEntriesByAddressesRequest.Addresses {
address, err := util.DecodeAddress(addressString, context.Config.NetParams().Prefix) address, err := util.DecodeAddress(addressString, context.Config.NetParams().Prefix)
@ -29,12 +34,7 @@ func HandleGetMempoolEntriesByAddresses(context *rpccontext.Context, _ *router.R
if !getMempoolEntriesByAddressesRequest.FilterTransactionPool { if !getMempoolEntriesByAddressesRequest.FilterTransactionPool {
sendingInMempool, receivingInMempool, err := context.Domain.MiningManager().GetTransactionsByAddresses() if transaction, found := sendingInTransactionPool[address.String()]; found {
if err != nil {
return nil, err
}
if transaction, found := sendingInMempool[address.String()]; found {
rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction) rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction)
err := context.PopulateTransactionWithVerboseData(rpcTransaction, nil) err := context.PopulateTransactionWithVerboseData(rpcTransaction, nil)
if err != nil { if err != nil {
@ -49,7 +49,7 @@ func HandleGetMempoolEntriesByAddresses(context *rpccontext.Context, _ *router.R
) )
} }
if transaction, found := receivingInMempool[address.String()]; found { if transaction, found := receivingInTransactionPool[address.String()]; found {
rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction) rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction)
err := context.PopulateTransactionWithVerboseData(rpcTransaction, nil) err := context.PopulateTransactionWithVerboseData(rpcTransaction, nil)
if err != nil { if err != nil {
@ -66,11 +66,6 @@ func HandleGetMempoolEntriesByAddresses(context *rpccontext.Context, _ *router.R
} }
if getMempoolEntriesByAddressesRequest.IncludeOrphanPool { if getMempoolEntriesByAddressesRequest.IncludeOrphanPool {
sendingInOrphanPool, receivingInOrphanPool, err := context.Domain.MiningManager().GetOrphanTransactionsByAddresses()
if err != nil {
return nil, err
}
if transaction, found := sendingInOrphanPool[address.String()]; found { if transaction, found := sendingInOrphanPool[address.String()]; found {
rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction) rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction)
err := context.PopulateTransactionWithVerboseData(rpcTransaction, nil) err := context.PopulateTransactionWithVerboseData(rpcTransaction, nil)

View File

@ -6,6 +6,7 @@ import (
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi" "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionid" "github.com/kaspanet/kaspad/domain/consensus/utils/transactionid"
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/router" "github.com/kaspanet/kaspad/infrastructure/network/netadapter/router"
"github.com/pkg/errors"
) )
// HandleGetMempoolEntry handles the respectively named RPC command // HandleGetMempoolEntry handles the respectively named RPC command
@ -24,14 +25,7 @@ func HandleGetMempoolEntry(context *rpccontext.Context, _ *router.Router, reques
return errorMessage, nil return errorMessage, nil
} }
if !getMempoolEntryRequest.FilterTransactionPool { transactionPoolTransaction, orphanPoolTransaction, found := context.Domain.MiningManager().GetTransaction(transactionID, !getMempoolEntryRequest.FilterTransactionPool, getMempoolEntryRequest.IncludeOrphanPool)
transaction, found = context.Domain.MiningManager().GetTransaction(transactionID)
}
if getMempoolEntryRequest.IncludeOrphanPool && !found {
transaction, found = context.Domain.MiningManager().GetOrphanTransaction(transactionID)
isOrphan = true
}
if !found { if !found {
errorMessage := &appmessage.GetMempoolEntryResponseMessage{} errorMessage := &appmessage.GetMempoolEntryResponseMessage{}
@ -39,6 +33,16 @@ func HandleGetMempoolEntry(context *rpccontext.Context, _ *router.Router, reques
return errorMessage, nil return errorMessage, nil
} }
if transactionPoolTransaction != nil && orphanPoolTransaction != nil {
return nil, errors.Errorf("Transaction %s is both an orphan, and not. This should never not happen", transactionID)
} else if transactionPoolTransaction != nil {
transaction = transactionPoolTransaction
isOrphan = false
} else if orphanPoolTransaction != nil {
transaction = orphanPoolTransaction
isOrphan = true
}
rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction) rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction)
err = context.PopulateTransactionWithVerboseData(rpcTransaction, nil) err = context.PopulateTransactionWithVerboseData(rpcTransaction, nil)
if err != nil { if err != nil {

View File

@ -46,61 +46,87 @@ func (mp *mempool) ValidateAndInsertTransaction(transaction *externalapi.DomainT
return mp.validateAndInsertTransaction(transaction, isHighPriority, allowOrphan, true) return mp.validateAndInsertTransaction(transaction, isHighPriority, allowOrphan, true)
} }
func (mp *mempool) GetTransaction(transactionID *externalapi.DomainTransactionID) (*externalapi.DomainTransaction, bool) { func (mp *mempool) GetTransaction(transactionID *externalapi.DomainTransactionID,
includeTransactionPool bool,
includeOrphanPool bool) (
transactionPoolTransaction *externalapi.DomainTransaction,
orphanPoolTransaction *externalapi.DomainTransaction,
found bool) {
mp.mtx.RLock() mp.mtx.RLock()
defer mp.mtx.RUnlock() defer mp.mtx.RUnlock()
return mp.transactionsPool.getTransaction(transactionID, true) var transactionfound bool
var orphanTransactionFound bool
if includeTransactionPool {
transactionPoolTransaction, transactionfound = mp.transactionsPool.getTransaction(transactionID, true)
}
if includeOrphanPool {
orphanPoolTransaction, orphanTransactionFound = mp.orphansPool.getOrphanTransaction(transactionID, true)
}
return transactionPoolTransaction, orphanPoolTransaction, transactionfound || orphanTransactionFound
} }
func (mp *mempool) GetTransactionsByAddresses() ( func (mp *mempool) GetTransactionsByAddresses(includeTransactionPool bool, includeOrphanPool bool) (
sending map[string]*externalapi.DomainTransaction, sendingInTransactionPool map[string]*externalapi.DomainTransaction,
receiving map[string]*externalapi.DomainTransaction, receivingInTransactionPool map[string]*externalapi.DomainTransaction,
err error, sendingInOrphanPool map[string]*externalapi.DomainTransaction,
) { receivingInOrphanPool map[string]*externalapi.DomainTransaction,
err error) {
mp.mtx.RLock() mp.mtx.RLock()
defer mp.mtx.RUnlock() defer mp.mtx.RUnlock()
return mp.transactionsPool.getTransactionsByAddresses(true) if includeTransactionPool {
sendingInTransactionPool, receivingInTransactionPool, err = mp.transactionsPool.getTransactionsByAddresses(true)
if err != nil {
return nil, nil, nil, nil, err
}
}
if includeOrphanPool {
sendingInTransactionPool, receivingInOrphanPool, err = mp.orphansPool.getOrphanTransactionsByAddresses(true)
if err != nil {
return nil, nil, nil, nil, err
}
}
return sendingInTransactionPool, receivingInTransactionPool, sendingInTransactionPool, receivingInOrphanPool, nil
} }
func (mp *mempool) AllTransactions() []*externalapi.DomainTransaction { func (mp *mempool) AllTransactions(includeTransactionPool bool, includeOrphanPool bool) (
transactionPoolTransactions []*externalapi.DomainTransaction,
orphanPoolTransactions []*externalapi.DomainTransaction) {
mp.mtx.RLock() mp.mtx.RLock()
defer mp.mtx.RUnlock() defer mp.mtx.RUnlock()
return mp.transactionsPool.getAllTransactions(true) if includeTransactionPool {
transactionPoolTransactions = mp.transactionsPool.getAllTransactions(true)
}
if includeOrphanPool {
orphanPoolTransactions = mp.orphansPool.getAllOrphanTransactions(true)
}
return transactionPoolTransactions, orphanPoolTransactions
} }
func (mp *mempool) GetOrphanTransaction(transactionID *externalapi.DomainTransactionID) (*externalapi.DomainTransaction, bool) { func (mp *mempool) TransactionCount(includeTransactionPool bool, includeOrphanPool bool) int {
mp.mtx.RLock() mp.mtx.RLock()
defer mp.mtx.RUnlock() defer mp.mtx.RUnlock()
return mp.orphansPool.getOrphanTransaction(transactionID, true) transactionCount := 0
}
func (mp *mempool) GetOrphanTransactionsByAddresses() ( if includeOrphanPool {
sending map[string]*externalapi.DomainTransaction, transactionCount += mp.orphansPool.orphanTransactionCount()
receiving map[string]*externalapi.DomainTransaction, }
err error, if includeTransactionPool {
) { transactionCount += mp.transactionsPool.transactionCount()
mp.mtx.RLock() }
defer mp.mtx.RUnlock()
return mp.orphansPool.getOrphanTransactionsByAddresses(true) return transactionCount
}
func (mp *mempool) AllOrphanTransactions() []*externalapi.DomainTransaction {
mp.mtx.RLock()
defer mp.mtx.RUnlock()
return mp.orphansPool.getAllOrphanTransactions(true)
}
func (mp *mempool) TransactionCount() int {
mp.mtx.RLock()
defer mp.mtx.RUnlock()
return mp.transactionsPool.transactionCount()
} }
func (mp *mempool) HandleNewBlockTransactions(transactions []*externalapi.DomainTransaction) ( func (mp *mempool) HandleNewBlockTransactions(transactions []*externalapi.DomainTransaction) (

View File

@ -398,3 +398,7 @@ func (op *orphansPool) getAllOrphanTransactions(clone bool) []*externalapi.Domai
} }
return allOrphanTransactions return allOrphanTransactions
} }
func (op *orphansPool) orphanTransactionCount() int {
return len(op.allOrphans)
}

View File

@ -15,19 +15,20 @@ type MiningManager interface {
GetBlockTemplate(coinbaseData *externalapi.DomainCoinbaseData) (block *externalapi.DomainBlock, isNearlySynced bool, err error) GetBlockTemplate(coinbaseData *externalapi.DomainCoinbaseData) (block *externalapi.DomainBlock, isNearlySynced bool, err error)
ClearBlockTemplate() ClearBlockTemplate()
GetBlockTemplateBuilder() miningmanagermodel.BlockTemplateBuilder GetBlockTemplateBuilder() miningmanagermodel.BlockTemplateBuilder
GetTransaction(transactionID *externalapi.DomainTransactionID) (*externalapi.DomainTransaction, bool) GetTransaction(transactionID *externalapi.DomainTransactionID, includeTransactionPool bool, includeOrphanPool bool) (
GetTransactionsByAddresses() ( transactionPoolTransaction *externalapi.DomainTransaction,
sending map[string]*externalapi.DomainTransaction, orphanPoolTransaction *externalapi.DomainTransaction,
receiving map[string]*externalapi.DomainTransaction, found bool)
GetTransactionsByAddresses(includeTransactionPool bool, includeOrphanPool bool) (
sendingInTransactionPool map[string]*externalapi.DomainTransaction,
receivingInTransactionPool map[string]*externalapi.DomainTransaction,
sendingInOrphanPool map[string]*externalapi.DomainTransaction,
receivingInOrphanPool map[string]*externalapi.DomainTransaction,
err error) err error)
AllTransactions() []*externalapi.DomainTransaction AllTransactions(includeTransactionPool bool, includeOrphanPool bool) (
GetOrphanTransaction(transactionID *externalapi.DomainTransactionID) (*externalapi.DomainTransaction, bool) transactionPoolTransactions []*externalapi.DomainTransaction,
GetOrphanTransactionsByAddresses() ( orphanPoolTransactions []*externalapi.DomainTransaction)
sending map[string]*externalapi.DomainTransaction, TransactionCount(includeTransactionPool bool, includeOrphanPool bool) int
receiving map[string]*externalapi.DomainTransaction,
err error)
AllOrphanTransactions() []*externalapi.DomainTransaction
TransactionCount() int
HandleNewBlockTransactions(txs []*externalapi.DomainTransaction) ([]*externalapi.DomainTransaction, error) HandleNewBlockTransactions(txs []*externalapi.DomainTransaction) ([]*externalapi.DomainTransaction, error)
ValidateAndInsertTransaction(transaction *externalapi.DomainTransaction, isHighPriority bool, allowOrphan bool) ( ValidateAndInsertTransaction(transaction *externalapi.DomainTransaction, isHighPriority bool, allowOrphan bool) (
acceptedTransactions []*externalapi.DomainTransaction, err error) acceptedTransactions []*externalapi.DomainTransaction, err error)
@ -117,42 +118,35 @@ func (mm *miningManager) ValidateAndInsertTransaction(transaction *externalapi.D
} }
func (mm *miningManager) GetTransaction( func (mm *miningManager) GetTransaction(
transactionID *externalapi.DomainTransactionID) (*externalapi.DomainTransaction, bool) { transactionID *externalapi.DomainTransactionID,
includeTransactionPool bool,
includeOrphanPool bool) (
transactionPoolTransaction *externalapi.DomainTransaction,
orphanPoolTransaction *externalapi.DomainTransaction,
found bool) {
return mm.mempool.GetTransaction(transactionID) return mm.mempool.GetTransaction(transactionID, includeTransactionPool, includeOrphanPool)
} }
func (mm *miningManager) AllTransactions() []*externalapi.DomainTransaction { func (mm *miningManager) AllTransactions(includeTransactionPool bool, includeOrphanPool bool) (
return mm.mempool.AllTransactions() transactionPoolTransactions []*externalapi.DomainTransaction,
orphanPoolTransactions []*externalapi.DomainTransaction) {
return mm.mempool.AllTransactions(includeTransactionPool, includeOrphanPool)
} }
func (mm *miningManager) GetTransactionsByAddresses() ( func (mm *miningManager) GetTransactionsByAddresses(includeTransactionPool bool, includeOrphanPool bool) (
sending map[string]*externalapi.DomainTransaction, sendingInTransactionPool map[string]*externalapi.DomainTransaction,
receiving map[string]*externalapi.DomainTransaction, receivingInTransactionPool map[string]*externalapi.DomainTransaction,
sendingInOrphanPool map[string]*externalapi.DomainTransaction,
receivingInOrphanPool map[string]*externalapi.DomainTransaction,
err error) { err error) {
return mm.mempool.GetTransactionsByAddresses()
return mm.mempool.GetTransactionsByAddresses(includeTransactionPool, includeOrphanPool)
} }
func (mm *miningManager) GetOrphanTransaction( func (mm *miningManager) TransactionCount(includeTransactionPool bool, includeOrphanPool bool) int {
transactionID *externalapi.DomainTransactionID) (*externalapi.DomainTransaction, bool) { return mm.mempool.TransactionCount(includeTransactionPool, includeOrphanPool)
return mm.mempool.GetOrphanTransaction(transactionID)
}
func (mm *miningManager) GetOrphanTransactionsByAddresses() (
sending map[string]*externalapi.DomainTransaction,
receiving map[string]*externalapi.DomainTransaction,
err error) {
return mm.mempool.GetTransactionsByAddresses()
}
func (mm *miningManager) AllOrphanTransactions() []*externalapi.DomainTransaction {
return mm.mempool.AllOrphanTransactions()
}
func (mm *miningManager) TransactionCount() int {
return mm.mempool.TransactionCount()
} }
func (mm *miningManager) RevalidateHighPriorityTransactions() ( func (mm *miningManager) RevalidateHighPriorityTransactions() (

View File

@ -52,7 +52,7 @@ func TestValidateAndInsertTransaction(t *testing.T) {
} }
// The UTXOEntry was filled manually for those transactions, so the transactions won't be considered orphans. // The UTXOEntry was filled manually for those transactions, so the transactions won't be considered orphans.
// Therefore, all the transactions expected to be contained in the mempool. // Therefore, all the transactions expected to be contained in the mempool.
transactionsFromMempool := miningManager.AllTransactions() transactionsFromMempool, _ := miningManager.AllTransactions(true, false)
if len(transactionsToInsert) != len(transactionsFromMempool) { if len(transactionsToInsert) != len(transactionsFromMempool) {
t.Fatalf("Wrong number of transactions in mempool: expected: %d, got: %d", len(transactionsToInsert), len(transactionsFromMempool)) t.Fatalf("Wrong number of transactions in mempool: expected: %d, got: %d", len(transactionsToInsert), len(transactionsFromMempool))
} }
@ -72,7 +72,7 @@ func TestValidateAndInsertTransaction(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("ValidateAndInsertTransaction: %v", err) t.Fatalf("ValidateAndInsertTransaction: %v", err)
} }
transactionsFromMempool = miningManager.AllTransactions() transactionsFromMempool, _ = miningManager.AllTransactions(true, false)
if !contains(transactionNotAnOrphan, transactionsFromMempool) { if !contains(transactionNotAnOrphan, transactionsFromMempool) {
t.Fatalf("Missing transaction %s in the mempool", consensushashing.TransactionID(transactionNotAnOrphan)) t.Fatalf("Missing transaction %s in the mempool", consensushashing.TransactionID(transactionNotAnOrphan))
} }
@ -99,7 +99,7 @@ func TestImmatureSpend(t *testing.T) {
if !errors.As(err, txRuleError) || txRuleError.RejectCode != mempool.RejectImmatureSpend { if !errors.As(err, txRuleError) || txRuleError.RejectCode != mempool.RejectImmatureSpend {
t.Fatalf("Unexpected error %+v", err) t.Fatalf("Unexpected error %+v", err)
} }
transactionsFromMempool := miningManager.AllTransactions() transactionsFromMempool, _ := miningManager.AllTransactions(true, false)
if contains(tx, transactionsFromMempool) { if contains(tx, transactionsFromMempool) {
t.Fatalf("Mempool contains a transaction with immature coinbase") t.Fatalf("Mempool contains a transaction with immature coinbase")
} }
@ -205,7 +205,7 @@ func TestHandleNewBlockTransactions(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("HandleNewBlockTransactions: %v", err) t.Fatalf("HandleNewBlockTransactions: %v", err)
} }
mempoolTransactions := miningManager.AllTransactions() mempoolTransactions, _ := miningManager.AllTransactions(true, false)
for _, removedTransaction := range blockWithFirstPartOfTheTransactions { for _, removedTransaction := range blockWithFirstPartOfTheTransactions {
if contains(removedTransaction, mempoolTransactions) { if contains(removedTransaction, mempoolTransactions) {
t.Fatalf("This transaction shouldnt be in mempool: %s", consensushashing.TransactionID(removedTransaction)) t.Fatalf("This transaction shouldnt be in mempool: %s", consensushashing.TransactionID(removedTransaction))
@ -214,7 +214,7 @@ func TestHandleNewBlockTransactions(t *testing.T) {
// There are no chained/double-spends transactions, and hence it is expected that all the other // There are no chained/double-spends transactions, and hence it is expected that all the other
// transactions, will still be included in the mempool. // transactions, will still be included in the mempool.
mempoolTransactions = miningManager.AllTransactions() mempoolTransactions, _ = miningManager.AllTransactions(true, false)
for _, transaction := range blockWithRestOfTheTransactions[transactionhelper.CoinbaseTransactionIndex+1:] { for _, transaction := range blockWithRestOfTheTransactions[transactionhelper.CoinbaseTransactionIndex+1:] {
if !contains(transaction, mempoolTransactions) { if !contains(transaction, mempoolTransactions) {
t.Fatalf("This transaction %s should be in mempool.", consensushashing.TransactionID(transaction)) t.Fatalf("This transaction %s should be in mempool.", consensushashing.TransactionID(transaction))
@ -225,8 +225,10 @@ func TestHandleNewBlockTransactions(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("HandleNewBlockTransactions: %v", err) t.Fatalf("HandleNewBlockTransactions: %v", err)
} }
if len(miningManager.AllTransactions()) != 0 { mempoolTransactions, _ = miningManager.AllTransactions(true, false)
blockIDs := domainBlocksToBlockIds(miningManager.AllTransactions()) if len(mempoolTransactions) != 0 {
mempoolTransactions, _ = miningManager.AllTransactions(true, false)
blockIDs := domainBlocksToBlockIds(mempoolTransactions)
t.Fatalf("The mempool contains unexpected transactions: %s", blockIDs) t.Fatalf("The mempool contains unexpected transactions: %s", blockIDs)
} }
}) })
@ -269,7 +271,8 @@ func TestDoubleSpendWithBlock(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("HandleNewBlockTransactions: %v", err) t.Fatalf("HandleNewBlockTransactions: %v", err)
} }
if contains(transactionInTheMempool, miningManager.AllTransactions()) { mempoolTransactions, _ := miningManager.AllTransactions(true, false)
if contains(transactionInTheMempool, mempoolTransactions) {
t.Fatalf("The transaction %s, shouldn't be in the mempool, since at least one "+ t.Fatalf("The transaction %s, shouldn't be in the mempool, since at least one "+
"output was already spent.", consensushashing.TransactionID(transactionInTheMempool)) "output was already spent.", consensushashing.TransactionID(transactionInTheMempool))
} }
@ -303,7 +306,7 @@ func TestOrphanTransactions(t *testing.T) {
t.Fatalf("ValidateAndInsertTransaction: %v", err) t.Fatalf("ValidateAndInsertTransaction: %v", err)
} }
} }
transactionsMempool := miningManager.AllTransactions() transactionsMempool, _ := miningManager.AllTransactions(true, false)
for _, transaction := range transactionsMempool { for _, transaction := range transactionsMempool {
if contains(transaction, childTransactions) { if contains(transaction, childTransactions) {
t.Fatalf("Error: an orphan transaction is exist in the mempool") t.Fatalf("Error: an orphan transaction is exist in the mempool")
@ -345,7 +348,7 @@ func TestOrphanTransactions(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("HandleNewBlockTransactions: %+v", err) t.Fatalf("HandleNewBlockTransactions: %+v", err)
} }
transactionsMempool = miningManager.AllTransactions() transactionsMempool, _ = miningManager.AllTransactions(true, false)
if len(transactionsMempool) != len(childTransactions) { if len(transactionsMempool) != len(childTransactions) {
t.Fatalf("Expected %d transactions in the mempool but got %d", len(childTransactions), len(transactionsMempool)) t.Fatalf("Expected %d transactions in the mempool but got %d", len(childTransactions), len(transactionsMempool))
} }
@ -553,8 +556,8 @@ func TestRevalidateHighPriorityTransactions(t *testing.T) {
} }
// Make sure spendingTransaction is still in mempool // Make sure spendingTransaction is still in mempool
allTransactions := miningManager.AllTransactions() mempoolTransactions, _ := miningManager.AllTransactions(true, false)
if len(allTransactions) != 1 || !allTransactions[0].Equal(spendingTransaction) { if len(mempoolTransactions) != 1 || !mempoolTransactions[0].Equal(spendingTransaction) {
t.Fatalf("Expected to have spendingTransaction as only validTransaction returned from "+ t.Fatalf("Expected to have spendingTransaction as only validTransaction returned from "+
"RevalidateHighPriorityTransactions, but got %v instead", validTransactions) "RevalidateHighPriorityTransactions, but got %v instead", validTransactions)
} }
@ -568,9 +571,9 @@ func TestRevalidateHighPriorityTransactions(t *testing.T) {
t.Fatalf("Expected to have empty validTransactions, but got %v instead", validTransactions) t.Fatalf("Expected to have empty validTransactions, but got %v instead", validTransactions)
} }
// And also AllTransactions should be empty as well // And also AllTransactions should be empty as well
allTransactions = miningManager.AllTransactions() mempoolTransactions, _ = miningManager.AllTransactions(true, false)
if len(allTransactions) != 0 { if len(mempoolTransactions) != 0 {
t.Fatalf("Expected to have empty allTransactions, but got %v instead", allTransactions) t.Fatalf("Expected to have empty allTransactions, but got %v instead", mempoolTransactions)
} }
}) })
} }
@ -605,7 +608,7 @@ func TestModifyBlockTemplate(t *testing.T) {
t.Fatalf("ValidateAndInsertTransaction: %v", err) t.Fatalf("ValidateAndInsertTransaction: %v", err)
} }
} }
transactionsMempool := miningManager.AllTransactions() transactionsMempool, _ := miningManager.AllTransactions(true, true)
for _, transaction := range transactionsMempool { for _, transaction := range transactionsMempool {
if contains(transaction, childTransactions) { if contains(transaction, childTransactions) {
t.Fatalf("Error: an orphan transaction is exist in the mempool") t.Fatalf("Error: an orphan transaction is exist in the mempool")
@ -654,7 +657,7 @@ func TestModifyBlockTemplate(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("HandleNewBlockTransactions: %+v", err) t.Fatalf("HandleNewBlockTransactions: %+v", err)
} }
transactionsMempool = miningManager.AllTransactions() transactionsMempool, _ = miningManager.AllTransactions(true, true)
if len(transactionsMempool) != len(childTransactions) { if len(transactionsMempool) != len(childTransactions) {
t.Fatalf("Expected %d transactions in the mempool but got %d", len(childTransactions), len(transactionsMempool)) t.Fatalf("Expected %d transactions in the mempool but got %d", len(childTransactions), len(transactionsMempool))
} }

View File

@ -12,19 +12,31 @@ type Mempool interface {
ValidateAndInsertTransaction(transaction *externalapi.DomainTransaction, isHighPriority bool, allowOrphan bool) ( ValidateAndInsertTransaction(transaction *externalapi.DomainTransaction, isHighPriority bool, allowOrphan bool) (
acceptedTransactions []*externalapi.DomainTransaction, err error) acceptedTransactions []*externalapi.DomainTransaction, err error)
RemoveTransactions(txs []*externalapi.DomainTransaction, removeRedeemers bool) error RemoveTransactions(txs []*externalapi.DomainTransaction, removeRedeemers bool) error
GetTransaction(transactionID *externalapi.DomainTransactionID) (*externalapi.DomainTransaction, bool) GetTransaction(
GetTransactionsByAddresses() ( transactionID *externalapi.DomainTransactionID,
sending map[string]*externalapi.DomainTransaction, includeTransactionPool bool,
receiving map[string]*externalapi.DomainTransaction, includeOrphanPool bool,
) (
transactionPoolTransaction *externalapi.DomainTransaction,
orphanPoolTransaction *externalapi.DomainTransaction,
found bool)
GetTransactionsByAddresses(
includeTransactionPool bool,
includeOrphanPool bool) (
sendingInTransactionPool map[string]*externalapi.DomainTransaction,
receivingInTransactionPool map[string]*externalapi.DomainTransaction,
sendingInOrphanPool map[string]*externalapi.DomainTransaction,
receivingInOrphanPool map[string]*externalapi.DomainTransaction,
err error) err error)
AllTransactions() []*externalapi.DomainTransaction AllTransactions(
GetOrphanTransaction(transactionID *externalapi.DomainTransactionID) (*externalapi.DomainTransaction, bool) includeTransactionPool bool,
GetOrphanTransactionsByAddresses() ( includeOrphanPool bool,
sending map[string]*externalapi.DomainTransaction, ) (
receiving map[string]*externalapi.DomainTransaction, transactionPoolTransactions []*externalapi.DomainTransaction,
err error) orphanPoolTransactions []*externalapi.DomainTransaction)
AllOrphanTransactions() []*externalapi.DomainTransaction TransactionCount(
TransactionCount() int includeTransactionPool bool,
includeOrphanPool bool) int
RevalidateHighPriorityTransactions() (validTransactions []*externalapi.DomainTransaction, err error) RevalidateHighPriorityTransactions() (validTransactions []*externalapi.DomainTransaction, err error)
IsTransactionOutputDust(output *externalapi.DomainTransactionOutput) bool IsTransactionOutputDust(output *externalapi.DomainTransactionOutput) bool
} }

View File

@ -105,7 +105,7 @@ func TestTxRelay(t *testing.T) {
} }
//TO DO: find out if failing the below is a bug or not //TO DO: find out if failing the below is a bug or not
/* for _, mempoolEntry := range mempoolEntryByAddress.Receiving { for _, mempoolEntry := range mempoolEntryByAddress.Receiving {
if mempoolEntry.IsOrphan { if mempoolEntry.IsOrphan {
t.Fatalf("transaction %s is an orphan, although it shouldn't be", mempoolEntry.Transaction.VerboseData.TransactionID) t.Fatalf("transaction %s is an orphan, although it shouldn't be", mempoolEntry.Transaction.VerboseData.TransactionID)
} }
@ -114,7 +114,7 @@ func TestTxRelay(t *testing.T) {
if mempoolEntry.IsOrphan { if mempoolEntry.IsOrphan {
t.Fatalf("transaction %s is an orphan, although it shouldn't be", mempoolEntry.Transaction.VerboseData.TransactionID) t.Fatalf("transaction %s is an orphan, although it shouldn't be", mempoolEntry.Transaction.VerboseData.TransactionID)
} }
}*/ }
} }
close(txAddedToMempoolChan) close(txAddedToMempoolChan)