Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed Dec 5, 2023
1 parent 3e55d0f commit 8543305
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 42 deletions.
3 changes: 3 additions & 0 deletions domain/consensus/model/testapi/test_consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type TestConsensus interface {
AddBlock(parentHashes []*externalapi.DomainHash, coinbaseData *externalapi.DomainCoinbaseData,
transactions []*externalapi.DomainTransaction) (*externalapi.DomainHash, *externalapi.VirtualChangeSet, error)

AddBlockOnTips(coinbaseData *externalapi.DomainCoinbaseData,
transactions []*externalapi.DomainTransaction) (*externalapi.DomainHash, *externalapi.VirtualChangeSet, error)

AddUTXOInvalidHeader(parentHashes []*externalapi.DomainHash) (*externalapi.DomainHash, *externalapi.VirtualChangeSet, error)

AddUTXOInvalidBlock(parentHashes []*externalapi.DomainHash) (*externalapi.DomainHash,
Expand Down
11 changes: 11 additions & 0 deletions domain/consensus/test_consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ func (tc *testConsensus) AddBlock(parentHashes []*externalapi.DomainHash, coinba
return consensushashing.BlockHash(block), virtualChangeSet, nil
}

func (tc *testConsensus) AddBlockOnTips(coinbaseData *externalapi.DomainCoinbaseData,
transactions []*externalapi.DomainTransaction) (*externalapi.DomainHash, *externalapi.VirtualChangeSet, error) {

tips, err := tc.Tips()
if err != nil {
return nil, nil, err
}

return tc.AddBlock(tips, coinbaseData, transactions)
}

func (tc *testConsensus) AddUTXOInvalidHeader(parentHashes []*externalapi.DomainHash) (*externalapi.DomainHash,
*externalapi.VirtualChangeSet, error) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
func (mp *mempool) revalidateHighPriorityTransactions() ([]*externalapi.DomainTransaction, error) {
type txNode struct {
children map[externalapi.DomainTransactionID]struct{}
isInvalid bool
nonVisitedParents int
tx *model.MempoolTransaction
visited bool
Expand All @@ -31,7 +30,6 @@ func (mp *mempool) revalidateHighPriorityTransactions() ([]*externalapi.DomainTr

node := &txNode{
children: make(map[externalapi.DomainTransactionID]struct{}),
isInvalid: false,
nonVisitedParents: 0,
tx: mp.transactionsPool.highPriorityTransactions[txID],
}
Expand All @@ -41,13 +39,7 @@ func (mp *mempool) revalidateHighPriorityTransactions() ([]*externalapi.DomainTr

queue := make([]*txNode, 0, len(mp.transactionsPool.highPriorityTransactions))
for id, transaction := range mp.transactionsPool.highPriorityTransactions {
node := &txNode{
children: make(map[externalapi.DomainTransactionID]struct{}),
isInvalid: false,
nonVisitedParents: 0,
tx: transaction,
}
txDAG[id] = node
node := maybeAddNode(id)

parents := make(map[externalapi.DomainTransactionID]struct{})
for _, input := range transaction.Transaction().Inputs {
Expand Down Expand Up @@ -76,34 +68,13 @@ func (mp *mempool) revalidateHighPriorityTransactions() ([]*externalapi.DomainTr
continue
}
node.visited = true
if node.isInvalid {
continue
}

transaction := node.tx
isValid, err := mp.revalidateTransaction(transaction)
if err != nil {
return nil, err
}

if !isValid {
// Invalidate the offspring of this transaction
invalidateQueue := []*txNode{node}
for len(invalidateQueue) > 0 {
var current *txNode
current, invalidateQueue = invalidateQueue[0], invalidateQueue[1:]

if current.isInvalid {
continue
}
current.isInvalid = true
for child := range current.children {
invalidateQueue = append(invalidateQueue, txDAG[child])
}
}
continue
}

for child := range node.children {
childNode := txDAG[child]
childNode.nonVisitedParents--
Expand All @@ -112,7 +83,9 @@ func (mp *mempool) revalidateHighPriorityTransactions() ([]*externalapi.DomainTr
}
}

validTransactions = append(validTransactions, transaction.Transaction().Clone())
if isValid {
validTransactions = append(validTransactions, transaction.Transaction().Clone())
}
}

return validTransactions, nil
Expand All @@ -134,6 +107,11 @@ func (mp *mempool) revalidateTransaction(transaction *model.MempoolTransaction)
return false, nil
}

_, err = mp.validateAndInsertTransaction(transaction.Transaction(), false, false)
if err != nil {
return false, err
}

return true, nil
}

Expand Down
110 changes: 99 additions & 11 deletions domain/miningmanager/miningmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,76 @@ func TestRevalidateHighPriorityTransactions(t *testing.T) {
})
}

func TestRevalidateHighPriorityTransactionsWithChain(t *testing.T) {
testutils.ForAllNets(t, true, func(t *testing.T, consensusConfig *consensus.Config) {
consensusConfig.BlockCoinbaseMaturity = 0
factory := consensus.NewFactory()
tc, teardown, err := factory.NewTestConsensus(consensusConfig, "TestRevalidateHighPriorityTransactions")
if err != nil {
t.Fatalf("Failed setting up TestConsensus: %+v", err)
}
defer teardown(false)

miningFactory := miningmanager.NewFactory()
mempoolConfig := mempool.DefaultConfig(&consensusConfig.Params)
tcAsConsensus := tc.(externalapi.Consensus)
tcAsConsensusPointer := &tcAsConsensus
consensusReference := consensusreference.NewConsensusReference(&tcAsConsensusPointer)
miningManager := miningFactory.NewMiningManager(consensusReference, &consensusConfig.Params, mempoolConfig)

const chainSize = 10
chain, err := createTxChain(tc, chainSize)
if err != nil {
t.Fatal(err)
}

for i, transaction := range chain {
t.Logf("chain %d %s", i, consensushashing.TransactionID(transaction))
}

_, err = miningManager.ValidateAndInsertTransaction(chain[0], true, false)
if err != nil {
t.Fatal(err)
}

blockHash, _, err := tc.AddBlockOnTips(nil, []*externalapi.DomainTransaction{chain[0].Clone()})
if err != nil {
t.Fatal(err)
}

block, _, err := tc.GetBlock(blockHash)
if err != nil {
t.Fatal(err)
}

_, err = miningManager.HandleNewBlockTransactions(block.Transactions)
if err != nil {
t.Fatal(err)
}

for _, transaction := range chain[1:] {
_, err = miningManager.ValidateAndInsertTransaction(transaction, true, false)
if err != nil {
t.Fatal(err)
}
}

_, _, err = tc.AddBlockOnTips(nil, []*externalapi.DomainTransaction{chain[1].Clone()})
if err != nil {
t.Fatal(err)
}

revalidated, err := miningManager.RevalidateHighPriorityTransactions()
if err != nil {
t.Fatal(err)
}

if len(revalidated) != chainSize-2 {
t.Fatalf("expected %d transactions to revalidate but instead only %d revalidated", chainSize-2, len(revalidated))
}
})
}

// TestModifyBlockTemplate verifies that modifying a block template changes coinbase data correctly.
func TestModifyBlockTemplate(t *testing.T) {
testutils.ForAllNets(t, true, func(t *testing.T, consensusConfig *consensus.Config) {
Expand Down Expand Up @@ -904,40 +974,58 @@ func createArraysOfParentAndChildrenTransactions(tc testapi.TestConsensus) ([]*e
func createParentAndChildrenTransactions(tc testapi.TestConsensus) (txParent *externalapi.DomainTransaction,
txChild *externalapi.DomainTransaction, err error) {

chain, err := createTxChain(tc, 2)
if err != nil {
return nil, nil, err
}

return chain[0], chain[1], nil
}

func createTxChain(tc testapi.TestConsensus, numTxs int) ([]*externalapi.DomainTransaction, error) {
// We will add two blocks by consensus before the parent transactions, in order to fund the parent transactions.
tips, err := tc.Tips()
if err != nil {
return nil, nil, err
return nil, err
}

_, _, err = tc.AddBlock(tips, nil, nil)
if err != nil {
return nil, nil, errors.Wrapf(err, "AddBlock: %v", err)
return nil, errors.Wrapf(err, "AddBlock: %v", err)
}

tips, err = tc.Tips()
if err != nil {
return nil, nil, err
return nil, err
}

fundingBlockHashForParent, _, err := tc.AddBlock(tips, nil, nil)
if err != nil {
return nil, nil, errors.Wrap(err, "AddBlock: ")
return nil, errors.Wrap(err, "AddBlock: ")
}
fundingBlockForParent, _, err := tc.GetBlock(fundingBlockHashForParent)
if err != nil {
return nil, nil, errors.Wrap(err, "GetBlock: ")
return nil, errors.Wrap(err, "GetBlock: ")
}
fundingTransactionForParent := fundingBlockForParent.Transactions[transactionhelper.CoinbaseTransactionIndex]
txParent, err = testutils.CreateTransaction(fundingTransactionForParent, 1000)

transactions := make([]*externalapi.DomainTransaction, numTxs)
transactions[0], err = testutils.CreateTransaction(fundingTransactionForParent, 1000)
if err != nil {
return nil, nil, err
return nil, err
}
txChild, err = testutils.CreateTransaction(txParent, 1000)
if err != nil {
return nil, nil, err

txParent := transactions[0]
for i := 1; i < numTxs; i++ {
transactions[i], err = testutils.CreateTransaction(txParent, 1000)
if err != nil {
return nil, err
}

txParent = transactions[i]
}
return txParent, txChild, nil

return transactions, nil
}

func createChildAndParentTxsAndAddParentToConsensus(tc testapi.TestConsensus) (*externalapi.DomainTransaction, error) {
Expand Down

0 comments on commit 8543305

Please sign in to comment.