Skip to content

Commit

Permalink
perf(consensus/blockstore): Remove validate basic call from LoadBlock…
Browse files Browse the repository at this point in the history
…Meta (backport cometbft#2964) (cometbft#2998) (#62) (#66)

* perf(consensus/blockstore): Remove validate basic call from LoadBlockMeta (cometbft#2998) (#62)

Co-authored-by: Anton Kaliaev <[email protected]>
(cherry picked from commit c73455f)

Co-authored-by: Dev Ojha <[email protected]>
  • Loading branch information
mergify[bot] and ValarDragon authored May 23, 2024
1 parent 622ebf9 commit ca3ee6e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[blockstore]` Remove a redundant `Header.ValidateBasic` call in `LoadBlockMeta`, 75% reducing this time.
([\#2964](https://github.com/cometbft/cometbft/pull/2964))
2 changes: 1 addition & 1 deletion store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (bs *BlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
panic(fmt.Errorf("unmarshal to cmtproto.BlockMeta: %w", err))
}

blockMeta, err := types.BlockMetaFromProto(pbbm)
blockMeta, err := types.BlockMetaFromTrustedProto(pbbm)
if err != nil {
panic(fmt.Errorf("error from proto blockMeta: %w", err))
}
Expand Down
10 changes: 9 additions & 1 deletion types/block_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func (bm *BlockMeta) ToProto() *cmtproto.BlockMeta {
}

func BlockMetaFromProto(pb *cmtproto.BlockMeta) (*BlockMeta, error) {
bm, err := BlockMetaFromTrustedProto(pb)
if err != nil {
return nil, err
}
return bm, bm.ValidateBasic()
}

func BlockMetaFromTrustedProto(pb *cmtproto.BlockMeta) (*BlockMeta, error) {
if pb == nil {
return nil, errors.New("blockmeta is empty")
}
Expand All @@ -62,7 +70,7 @@ func BlockMetaFromProto(pb *cmtproto.BlockMeta) (*BlockMeta, error) {
bm.Header = h
bm.NumTxs = int(pb.NumTxs)

return bm, bm.ValidateBasic()
return bm, nil
}

// ValidateBasic performs basic validation.
Expand Down
2 changes: 1 addition & 1 deletion types/block_meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestBlockMeta_ToProto(t *testing.T) {
t.Run(tt.testName, func(t *testing.T) {
pb := tt.bm.ToProto()

bm, err := BlockMetaFromProto(pb)
bm, err := BlockMetaFromTrustedProto(pb)

if !tt.expErr {
require.NoError(t, err, tt.testName)
Expand Down

0 comments on commit ca3ee6e

Please sign in to comment.