core: add custom error to blockhain.go

Add InvalidBlockIndex error to AddBlock func
This commit is contained in:
Anna Shaleva 2020-03-04 13:06:18 +03:00
parent f72c321426
commit d3063c26e1

View file

@ -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()