Skip to content

Commit

Permalink
Fix 'newBlocks.current' getter for binary blocks. (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeskov authored Apr 21, 2024
1 parent 23fbe57 commit 74aaeb1
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,30 +350,47 @@ func (n *newBlocks) unmarshalBlock(block *proto.Block, blockBytes []byte) error
return nil
}

func getBlockDataWithOptionalSnapshot[T interface{ []byte | *proto.Block }](
curPos int,
blocks []T,
snapshots []*proto.BlockSnapshot,
) (T, *proto.BlockSnapshot, error) {
if curPos > len(blocks) || curPos < 1 {
var zero T
return zero, nil, errors.New("bad current position")
}
var (
pos = curPos - 1
block = blocks[pos]
optionalSnapshot *proto.BlockSnapshot
)
if sl := len(snapshots); sl != 0 { // snapshots aren't empty
if bl := len(blocks); sl != bl { // if snapshots are present, they must have the same length as blocks
var zero T
return zero, nil, errors.Errorf("snapshots and blocks slices have different lengths %d and %d", sl, bl)
}
optionalSnapshot = snapshots[pos] // blocks and snapshots have the same length
}
return block, optionalSnapshot, nil
}

func (n *newBlocks) current() (*proto.Block, *proto.BlockSnapshot, error) {
if !n.binary {
if n.curPos > len(n.blocks) || n.curPos < 1 {
return nil, nil, errors.New("bad current position")
}
var (
pos = n.curPos - 1
block = n.blocks[pos]
optionalSnapshot *proto.BlockSnapshot
)
if len(n.snapshots) == len(n.blocks) { // return block with snapshot if it is set
optionalSnapshot = n.snapshots[pos]
block, optionalSnapshot, err := getBlockDataWithOptionalSnapshot(n.curPos, n.blocks, n.snapshots)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get current deserialized block")
}
return block, optionalSnapshot, nil
}
if n.curPos > len(n.binBlocks) || n.curPos < 1 {
return nil, nil, errors.New("bad current position")
blockBytes, optionalSnapshot, err := getBlockDataWithOptionalSnapshot(n.curPos, n.binBlocks, n.snapshots)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get current binary block")
}
blockBytes := n.binBlocks[n.curPos-1]
b := &proto.Block{}
if err := n.unmarshalBlock(b, blockBytes); err != nil {
return nil, nil, err
block := &proto.Block{}
if unmErr := n.unmarshalBlock(block, blockBytes); unmErr != nil {
return nil, nil, unmErr
}
return b, nil, nil
return block, optionalSnapshot, nil
}

func (n *newBlocks) reset() {
Expand Down

0 comments on commit 74aaeb1

Please sign in to comment.