Skip to content

Commit

Permalink
Handle decodeBlock error in blocks.Layer method (#5951)
Browse files Browse the repository at this point in the history
## Motivation

When an error occurs while decoding a block, it is not handled properly and nil is added to the slice.
  • Loading branch information
kacpersaw committed May 17, 2024
1 parent 021b693 commit 7082a3c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
15 changes: 9 additions & 6 deletions sql/blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,25 @@ func GetLayer(db sql.Executor, id types.BlockID) (types.LayerID, error) {
// Layer returns full body blocks for layer.
func Layer(db sql.Executor, lid types.LayerID) ([]*types.Block, error) {
var (
blk *types.Block
rst []*types.Block
err error
blk *types.Block
rst []*types.Block
derr error
)
if _, err = db.Exec("select id, block from blocks where layer = ?1;", func(stmt *sql.Statement) {
if _, err := db.Exec("select id, block from blocks where layer = ?1;", func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid.Uint32()))
}, func(stmt *sql.Statement) bool {
id := types.BlockID{}
stmt.ColumnBytes(0, id[:])
blk, err = decodeBlock(stmt.ColumnReader(1), id)
blk, derr = decodeBlock(stmt.ColumnReader(1), id)
if derr != nil {
return false
}
rst = append(rst, blk)
return true
}); err != nil {
return nil, fmt.Errorf("select blocks in layer %s: %w", lid, err)
}
return rst, nil
return rst, derr
}

// IDsInLayer returns list of block ids in the layer.
Expand Down
15 changes: 15 additions & 0 deletions sql/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,18 @@ func TestLoadBlob(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []int{len(blob1.Bytes), len(blob2.Bytes), -1}, sizes)
}

func TestLayerForMangledBlock(t *testing.T) {
db := sql.InMemory()
if _, err := db.Exec("insert into blocks (id, layer, block) values (?1, ?2, ?3);",
func(stmt *sql.Statement) {
stmt.BindBytes(1, []byte(`mangled-block-id`))
stmt.BindInt64(2, 1010101)
stmt.BindBytes(3, []byte(`mangled-block`)) // this is actually should encode block
}, nil); err != nil {
require.NoError(t, err)
}
rst, err := Layer(db, types.LayerID(1010101))
require.Empty(t, rst, 0)
require.Error(t, err)
}

0 comments on commit 7082a3c

Please sign in to comment.