From 16bc5296cbdf60f5a3debc494e0b989998064bcc Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 11 Oct 2019 11:40:54 +0300 Subject: [PATCH] block: return error from Verify Don't hide real problem behind the bool value. Makes it easier to identify problems when looking at log messages. --- pkg/core/block.go | 13 ++++++++----- pkg/core/block_test.go | 6 +++--- pkg/core/blockchain.go | 5 +++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pkg/core/block.go b/pkg/core/block.go index fddb06eba..23bc73c40 100644 --- a/pkg/core/block.go +++ b/pkg/core/block.go @@ -1,6 +1,9 @@ package core import ( + "errors" + "fmt" + "github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/crypto" "github.com/CityOfZion/neo-go/pkg/io" @@ -45,26 +48,26 @@ func (b *Block) rebuildMerkleRoot() error { } // Verify the integrity of the block. -func (b *Block) Verify(full bool) bool { +func (b *Block) Verify(full bool) error { // There has to be some transaction inside. if len(b.Transactions) == 0 { - return false + return errors.New("no transactions") } // The first TX has to be a miner transaction. if b.Transactions[0].Type != transaction.MinerType { - return false + return fmt.Errorf("the first transaction is %s", b.Transactions[0].Type) } // If the first TX is a minerTX then all others cant. for _, tx := range b.Transactions[1:] { if tx.Type == transaction.MinerType { - return false + return fmt.Errorf("miner transaction %s is not the first one", tx.Hash().ReverseString()) } } // TODO: When full is true, do a full verification. if full { log.Warn("full verification of blocks is not yet implemented") } - return true + return nil } // NewBlockFromTrimmedBytes returns a new block from trimmed data. diff --git a/pkg/core/block_test.go b/pkg/core/block_test.go index b8e390c71..bb7016826 100644 --- a/pkg/core/block_test.go +++ b/pkg/core/block_test.go @@ -87,19 +87,19 @@ func TestBlockVerify(t *testing.T) { newMinerTX(), newIssueTX(), ) - assert.True(t, block.Verify(false)) + assert.Nil(t, block.Verify(false)) block.Transactions = []*transaction.Transaction{ {Type: transaction.IssueType}, {Type: transaction.MinerType}, } - assert.False(t, block.Verify(false)) + assert.NotNil(t, block.Verify(false)) block.Transactions = []*transaction.Transaction{ {Type: transaction.MinerType}, {Type: transaction.MinerType}, } - assert.False(t, block.Verify(false)) + assert.NotNil(t, block.Verify(false)) } func TestBinBlockDecodeEncode(t *testing.T) { diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 7013e3719..bf5abadca 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -210,8 +210,9 @@ func (bc *Blockchain) AddBlock(block *Block) error { return fmt.Errorf("expected block %d, but passed block %d", expectedHeight, block.Index) } if bc.verifyBlocks { - if !block.Verify(false) { - return fmt.Errorf("block %s is invalid", block.Hash()) + err := block.Verify(false) + if err != nil { + return fmt.Errorf("block %s is invalid: %s", block.Hash().ReverseString(), err) } for _, tx := range block.Transactions { err := bc.Verify(tx)