From 8cdf2d3464286371bf3a54c02db0d2c81a4a05b2 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 20 Nov 2020 13:25:19 +0300 Subject: [PATCH] core: unify `verifyTxAttributes` errors We already have pretty ErrInvalidAttribute error, so I think that all other `verifyTxAttributes` errors should be wrappers around ErrInvalidAttr. --- pkg/core/blockchain.go | 11 +++++------ pkg/core/blockchain_test.go | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index d8a53a2e4..3d9c68d1a 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1254,7 +1254,6 @@ func (bc *Blockchain) verifyHeader(currHeader, prevHeader *block.Header) error { // Various errors that could be returned upon verification. var ( - ErrTxNotYetValid = errors.New("transaction is not yet valid") ErrTxExpired = errors.New("transaction has expired") ErrInsufficientFunds = errors.New("insufficient funds") ErrTxSmallNetworkFee = errors.New("too small network fee") @@ -1365,23 +1364,23 @@ func (bc *Blockchain) verifyTxAttributes(tx *transaction.Transaction) error { } case transaction.NotValidBeforeT: if !bc.config.P2PSigExtensions { - return errors.New("NotValidBefore attribute was found, but P2PSigExtensions are disabled") + return fmt.Errorf("%w: NotValidBefore attribute was found, but P2PSigExtensions are disabled", ErrInvalidAttribute) } nvb := tx.Attributes[i].Value.(*transaction.NotValidBefore) if height := bc.BlockHeight(); height < nvb.Height { - return fmt.Errorf("%w: NotValidBefore = %d, current height = %d", ErrTxNotYetValid, nvb.Height, height) + return fmt.Errorf("%w: transaction is not yet valid: NotValidBefore = %d, current height = %d", ErrInvalidAttribute, nvb.Height, height) } case transaction.ConflictsT: if !bc.config.P2PSigExtensions { - return errors.New("Conflicts attribute was found, but P2PSigExtensions are disabled") + return fmt.Errorf("%w: Conflicts attribute was found, but P2PSigExtensions are disabled", ErrInvalidAttribute) } conflicts := tx.Attributes[i].Value.(*transaction.Conflicts) if err := bc.dao.HasTransaction(conflicts.Hash); errors.Is(err, dao.ErrAlreadyExists) { - return fmt.Errorf("conflicting transaction %s is already on chain", conflicts.Hash.StringLE()) + return fmt.Errorf("%w: conflicting transaction %s is already on chain", ErrInvalidAttribute, conflicts.Hash.StringLE()) } default: if !bc.config.ReservedAttributes && attrType >= transaction.ReservedLowerBound && attrType <= transaction.ReservedUpperBound { - return errors.New("attribute of reserved type was found, but ReservedAttributes are disabled") + return fmt.Errorf("%w: attribute of reserved type was found, but ReservedAttributes are disabled", ErrInvalidAttribute) } } } diff --git a/pkg/core/blockchain_test.go b/pkg/core/blockchain_test.go index d9dfa0e85..1a0b208a5 100644 --- a/pkg/core/blockchain_test.go +++ b/pkg/core/blockchain_test.go @@ -621,7 +621,7 @@ func TestVerifyTx(t *testing.T) { bc.config.P2PSigExtensions = true t.Run("NotYetValid", func(t *testing.T) { tx := getNVBTx(bc.blockHeight + 1) - require.True(t, errors.Is(bc.VerifyTx(tx), ErrTxNotYetValid)) + require.True(t, errors.Is(bc.VerifyTx(tx), ErrInvalidAttribute)) }) t.Run("positive", func(t *testing.T) { tx := getNVBTx(bc.blockHeight)