diff --git a/.changeset/fix_panic_when_fetching_block_with_empty_block_id_from_chainmanager.md b/.changeset/fix_panic_when_fetching_block_with_empty_block_id_from_chainmanager.md new file mode 100644 index 0000000..ba2f304 --- /dev/null +++ b/.changeset/fix_panic_when_fetching_block_with_empty_block_id_from_chainmanager.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +# Fix panic when fetching block with empty block id from ChainManager diff --git a/chain/db.go b/chain/db.go index 466acfa..eb78fa9 100644 --- a/chain/db.go +++ b/chain/db.go @@ -295,12 +295,13 @@ func (db *DBStore) putState(cs consensus.State) { func (db *DBStore) getBlock(id types.BlockID) (bh types.BlockHeader, b *types.Block, bs *consensus.V1BlockSupplement, _ bool) { var sb supplementedBlock - ok := db.bucket(bBlocks).get(id[:], &sb) - if sb.Header == nil { + if ok := db.bucket(bBlocks).get(id[:], &sb); !ok { + return types.BlockHeader{}, nil, nil, false + } else if sb.Header == nil { sb.Header = new(types.BlockHeader) *sb.Header = sb.Block.Header() } - return *sb.Header, sb.Block, sb.Supplement, ok + return *sb.Header, sb.Block, sb.Supplement, true } func (db *DBStore) putBlock(bh types.BlockHeader, b *types.Block, bs *consensus.V1BlockSupplement) { diff --git a/chain/db_test.go b/chain/db_test.go new file mode 100644 index 0000000..f30f6de --- /dev/null +++ b/chain/db_test.go @@ -0,0 +1,19 @@ +package chain_test + +import ( + "testing" + + "go.sia.tech/core/types" + "go.sia.tech/coreutils/chain" + "go.sia.tech/coreutils/testutil" +) + +func TestGetEmptyBlockID(t *testing.T) { + n, genesisBlock := testutil.V2Network() + store, tipState, err := chain.NewDBStore(chain.NewMemDB(), n, genesisBlock) + if err != nil { + t.Fatal(err) + } + cm := chain.NewManager(store, tipState) + _, _ = cm.Block(types.BlockID{}) +}