Skip to content

Commit

Permalink
Fix panic when fetching block with empty block id from ChainManager (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl authored Dec 11, 2024
1 parent a28e868 commit 6881993
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

# Fix panic when fetching block with empty block id from ChainManager
7 changes: 4 additions & 3 deletions chain/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions chain/db_test.go
Original file line number Diff line number Diff line change
@@ -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{})
}

0 comments on commit 6881993

Please sign in to comment.