From 205f52c5632c84f2cfe757115eb8c00ed07cf067 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 6 Aug 2020 18:34:44 +0300 Subject: [PATCH] core: use error wrapping to provide more details --- pkg/core/blockchain.go | 13 ++++++------- pkg/core/native/policy.go | 11 ++++++----- pkg/network/server.go | 8 ++++---- pkg/rpc/server/server.go | 4 ++-- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 6d5347f49..3cd33e991 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -420,7 +420,7 @@ func (bc *Blockchain) AddBlock(block *block.Block) error { expectedHeight := bc.BlockHeight() + 1 if expectedHeight != block.Index { - return ErrInvalidBlockIndex + return fmt.Errorf("expected %d, got %d: %w", expectedHeight, block.Index, ErrInvalidBlockIndex) } headerLen := bc.headerListLen() @@ -1367,23 +1367,22 @@ func (bc *Blockchain) PoolTx(t *transaction.Transaction) error { defer bc.lock.RUnlock() if bc.HasTransaction(t.Hash()) { - return ErrAlreadyExists + return fmt.Errorf("blockchain: %w", ErrAlreadyExists) } if err := bc.verifyTx(t, nil); err != nil { return err } // Policying. - if ok, err := bc.contracts.Policy.CheckPolicy(bc.newInteropContext(trigger.Application, bc.dao, nil, t), t); err != nil { - return err - } else if !ok { - return ErrPolicy + if err := bc.contracts.Policy.CheckPolicy(bc.newInteropContext(trigger.Application, bc.dao, nil, t), t); err != nil { + // Only one %w can be used. + return fmt.Errorf("%w: %v", ErrPolicy, err) } if err := bc.memPool.Add(t, bc); err != nil { switch err { case mempool.ErrOOM: return ErrOOM case mempool.ErrConflict: - return ErrAlreadyExists + return fmt.Errorf("mempool: %w", ErrAlreadyExists) default: return err } diff --git a/pkg/core/native/policy.go b/pkg/core/native/policy.go index 5e24fc067..46abf3b40 100644 --- a/pkg/core/native/policy.go +++ b/pkg/core/native/policy.go @@ -3,6 +3,7 @@ package native import ( "encoding/binary" "errors" + "fmt" "math/big" "sort" "sync" @@ -510,21 +511,21 @@ func (p *Policy) checkValidators(ic *interop.Context) (bool, error) { // CheckPolicy checks whether transaction's script hashes for verifying are // included into blocked accounts list. -func (p *Policy) CheckPolicy(ic *interop.Context, tx *transaction.Transaction) (bool, error) { +func (p *Policy) CheckPolicy(ic *interop.Context, tx *transaction.Transaction) error { ba, err := p.GetBlockedAccountsInternal(ic.DAO) if err != nil { - return false, err + return fmt.Errorf("unable to get blocked accounts list: %w", err) } scriptHashes, err := ic.Chain.GetScriptHashesForVerifying(tx) if err != nil { - return false, err + return fmt.Errorf("unable to get tx script hashes: %w", err) } for _, acc := range ba { for _, hash := range scriptHashes { if acc.Equals(hash) { - return false, nil + return fmt.Errorf("account %s is blocked", hash.StringLE()) } } } - return true, nil + return nil } diff --git a/pkg/network/server.go b/pkg/network/server.go index 562908eaa..3a926f19c 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -843,12 +843,12 @@ func (s *Server) relayBlocksLoop() { // verifyAndPoolTX verifies the TX and adds it to the local mempool. func (s *Server) verifyAndPoolTX(t *transaction.Transaction) RelayReason { if err := s.chain.PoolTx(t); err != nil { - switch err { - case core.ErrAlreadyExists: + switch { + case errors.Is(err, core.ErrAlreadyExists): return RelayAlreadyExists - case core.ErrOOM: + case errors.Is(err, core.ErrOOM): return RelayOutOfMemory - case core.ErrPolicy: + case errors.Is(err, core.ErrPolicy): return RelayPolicyFail default: return RelayInvalid diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 22f60c3bc..c64e48e54 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -917,8 +917,8 @@ func (s *Server) submitBlock(reqParams request.Params) (interface{}, *response.E } err = s.chain.AddBlock(b) if err != nil { - switch err { - case core.ErrInvalidBlockIndex, core.ErrAlreadyExists: + switch { + case errors.Is(err, core.ErrInvalidBlockIndex) || errors.Is(err, core.ErrAlreadyExists): return nil, response.ErrAlreadyExists default: return nil, response.ErrValidationFailed