From b533dfceba492afedef984e633777cdc981bb7c2 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 21 Oct 2019 08:37:01 +0300 Subject: [PATCH] core: don't panic on init when there are less than 2000 hashes If you're to sync less than 2000 headers no batched header key-value is gonna be written into the DB and init() would panic because bc.headerList.Len() would return 0. Use genesis block as a target in this case. --- pkg/core/blockchain.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index a20a6a5f1..263606ee1 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -137,7 +137,16 @@ func (bc *Blockchain) init() error { // that with stored blocks. if currHeaderHeight > bc.storedHeaderCount { hash := currHeaderHash - targetHash := bc.headerList.Get(bc.headerList.Len() - 1) + var targetHash util.Uint256 + if bc.headerList.Len() > 0 { + targetHash = bc.headerList.Get(bc.headerList.Len() - 1) + } else { + genesisBlock, err := createGenesisBlock(bc.config) + if err != nil { + return err + } + targetHash = genesisBlock.Hash() + } headers := make([]*Header, 0) for hash != targetHash {