block: check for Transaction length before messing with txes

Fixes panic two lines below. Blocks without transactions are invalid by
definition, so there is a need to adjust tests accordingly.
This commit is contained in:
Roman Khimov 2019-09-30 16:55:02 +03:00
parent c3591d8897
commit 4ae18e8ffc
2 changed files with 9 additions and 4 deletions

View file

@ -46,6 +46,10 @@ func (b *Block) rebuildMerkleRoot() error {
// Verify the integrity of the block. // Verify the integrity of the block.
func (b *Block) Verify(full bool) bool { func (b *Block) Verify(full bool) bool {
// There has to be some transaction inside.
if len(b.Transactions) == 0 {
return false
}
// The first TX has to be a miner transaction. // The first TX has to be a miner transaction.
if b.Transactions[0].Type != transaction.MinerType { if b.Transactions[0].Type != transaction.MinerType {
return false return false

View file

@ -6,6 +6,7 @@ import (
"github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/config"
"github.com/CityOfZion/neo-go/pkg/core/storage" "github.com/CityOfZion/neo-go/pkg/core/storage"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -38,9 +39,9 @@ func TestAddHeaders(t *testing.T) {
func TestAddBlock(t *testing.T) { func TestAddBlock(t *testing.T) {
bc := newTestChain(t) bc := newTestChain(t)
blocks := []*Block{ blocks := []*Block{
newBlock(1), newBlock(1, newTX(transaction.MinerType)),
newBlock(2), newBlock(2, newTX(transaction.MinerType)),
newBlock(3), newBlock(3, newTX(transaction.MinerType)),
} }
for i := 0; i < len(blocks); i++ { for i := 0; i < len(blocks); i++ {
@ -69,7 +70,7 @@ func TestAddBlock(t *testing.T) {
func TestGetHeader(t *testing.T) { func TestGetHeader(t *testing.T) {
bc := newTestChain(t) bc := newTestChain(t)
block := newBlock(1) block := newBlock(1, newTX(transaction.MinerType))
err := bc.AddBlock(block) err := bc.AddBlock(block)
assert.Nil(t, err) assert.Nil(t, err)