Skip to content

Commit

Permalink
Fix type detection in RemoveInvalidTransactions (#2252)
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 authored Dec 12, 2023
1 parent 8e71f79 commit 5a3b8a0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
5 changes: 3 additions & 2 deletions domain/consensus/processes/blockbuilder/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ func (bb *blockBuilder) validateTransactions(stagingArea *model.StagingArea,
for _, transaction := range transactions {
err := bb.validateTransaction(stagingArea, transaction)
if err != nil {
if !errors.As(err, &ruleerrors.RuleError{}) {
ruleError := ruleerrors.RuleError{}
if !errors.As(err, &ruleError) {
return err
}
invalidTransactions = append(invalidTransactions,
ruleerrors.InvalidTransaction{Transaction: transaction, Error: err})
ruleerrors.InvalidTransaction{Transaction: transaction, Error: &ruleError})
}
}

Expand Down
2 changes: 1 addition & 1 deletion domain/consensus/ruleerrors/rule_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func NewErrMissingParents(missingParentHashes []*externalapi.DomainHash) error {
// InvalidTransaction is a struct containing an invalid transaction, and the error explaining why it's invalid.
type InvalidTransaction struct {
Transaction *externalapi.DomainTransaction
Error error
Error *RuleError
}

func (invalid InvalidTransaction) String() string {
Expand Down
7 changes: 4 additions & 3 deletions domain/consensus/ruleerrors/rule_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package ruleerrors
import (
"errors"
"fmt"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"testing"

"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"

"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func TestNewErrMissingTxOut(t *testing.T) {
func TestNewErrInvalidTransactionsInNewBlock(t *testing.T) {
tx := &externalapi.DomainTransaction{Fee: 1337}
txID := consensushashing.TransactionID(tx)
outer := NewErrInvalidTransactionsInNewBlock([]InvalidTransaction{{tx, ErrNoTxInputs}})
outer := NewErrInvalidTransactionsInNewBlock([]InvalidTransaction{{tx, &ErrNoTxInputs}})
//TODO: Implement Stringer for `DomainTransaction`
expectedOuterErr := fmt.Sprintf("ErrInvalidTransactionsInNewBlock: [(%s: ErrNoTxInputs)]", txID)
inner := &ErrInvalidTransactionsInNewBlock{}
Expand All @@ -60,7 +61,7 @@ func TestNewErrInvalidTransactionsInNewBlock(t *testing.T) {
if len(inner.InvalidTransactions) != 1 {
t.Fatalf("TestNewErrInvalidTransactionsInNewBlock: Expected len(inner.MissingOutpoints) 1, found: %d", len(inner.InvalidTransactions))
}
if inner.InvalidTransactions[0].Error != ErrNoTxInputs {
if *inner.InvalidTransactions[0].Error != ErrNoTxInputs {
t.Fatalf("TestNewErrInvalidTransactionsInNewBlock: Expected ErrNoTxInputs. found: %v", inner.InvalidTransactions[0].Error)
}
if inner.InvalidTransactions[0].Transaction.Fee != 1337 {
Expand Down
16 changes: 4 additions & 12 deletions domain/miningmanager/mempool/mempool.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package mempool

import (
"sync"

"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
"sync"
"github.com/pkg/errors"

"github.com/kaspanet/kaspad/domain/consensusreference"

Expand Down Expand Up @@ -209,17 +211,7 @@ func (mp *mempool) RemoveInvalidTransactions(err *ruleerrors.ErrInvalidTransacti
defer mp.mtx.Unlock()

for _, tx := range err.InvalidTransactions {
ruleErr, success := tx.Error.(ruleerrors.RuleError)
if !success {
continue
}

inner := ruleErr.Unwrap()
removeRedeemers := true
if _, ok := inner.(ruleerrors.ErrMissingTxOut); ok {
removeRedeemers = false
}

removeRedeemers := !errors.As(tx.Error, &ruleerrors.ErrMissingTxOut{})
err := mp.removeTransaction(consensushashing.TransactionID(tx.Transaction), removeRedeemers)
if err != nil {
return err
Expand Down

0 comments on commit 5a3b8a0

Please sign in to comment.