From d3063c26e1cb2033b51ec5c699a3466c7a8467a0 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 4 Mar 2020 13:06:18 +0300 Subject: [PATCH] core: add custom error to blockhain.go Add InvalidBlockIndex error to AddBlock func --- pkg/core/blockchain.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 6c6118284..22e39f2f3 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -52,6 +52,9 @@ var ( // ErrPolicy is returned on attempt to add transaction that doesn't // comply with node's configured policy into the mempool. ErrPolicy = errors.New("not allowed by policy") + // ErrInvalidBlockIndex is returned when trying to add block with index + // other than expected height of the blockchain. + ErrInvalidBlockIndex error = errors.New("invalid block index") ) var ( genAmount = []int{8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} @@ -307,7 +310,7 @@ func (bc *Blockchain) AddBlock(block *block.Block) error { expectedHeight := bc.BlockHeight() + 1 if expectedHeight != block.Index { - return fmt.Errorf("expected block %d, but passed block %d", expectedHeight, block.Index) + return ErrInvalidBlockIndex } headerLen := bc.headerListLen()