Skip to content

Commit a6e59cd

Browse files
authored
Merge pull request #1082 from oasisprotocol/ptrus/feature/core-blockmeta
analyer/consensus: Support new BlockMeta version
2 parents b608797 + fd8ee36 commit a6e59cd

File tree

4 files changed

+117
-6
lines changed

4 files changed

+117
-6
lines changed

.changelog/1082.feature.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
analyer/consensus: Support new BlockMeta version

analyzer/consensus/consensus.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/oasisprotocol/oasis-core/go/common/cbor"
1616
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
1717
"github.com/oasisprotocol/oasis-core/go/common/quantity"
18-
cometbft "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
1918
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/crypto"
2019
sdkConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
2120
sdkTypes "github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
@@ -25,6 +24,7 @@ import (
2524
beacon "github.com/oasisprotocol/nexus/coreapi/v22.2.11/beacon/api"
2625
"github.com/oasisprotocol/nexus/coreapi/v22.2.11/consensus/api/transaction"
2726
staking "github.com/oasisprotocol/nexus/coreapi/v22.2.11/staking/api"
27+
cometbft "github.com/oasisprotocol/nexus/coreapi/v24.0/consensus/cometbft/api"
2828

2929
"github.com/oasisprotocol/nexus/analyzer"
3030
"github.com/oasisprotocol/nexus/analyzer/block"
@@ -402,15 +402,13 @@ func (m *processor) queueBlockInserts(batch *storage.QueryBatch, data *consensus
402402
}
403403

404404
var cmtMeta cometbft.BlockMeta
405-
if err := cbor.Unmarshal(data.BlockHeader.Meta, &cmtMeta); err != nil {
405+
if err := cmtMeta.TryUnmarshal(data.BlockHeader.Meta); err != nil {
406406
m.logger.Warn("could not unmarshal block meta, may be incompatible version",
407407
"height", data.BlockHeader.Height,
408408
"err", err,
409409
)
410-
// We only try to unmarshal into the current version of the metadata
411-
// structure (oasis-core Eden + CometBFT at time of writing). This may
412-
// fail on blocks from an incompatible earlier version. Skip indexing
413-
// the block metadata in that case.
410+
// We just skip indexing the block metadata if we cannot unmarshal it
411+
// and don't stop indexing the rest of the block.
414412
}
415413

416414
var proposerAddr *string
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package api
2+
3+
import (
4+
"fmt"
5+
6+
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
7+
cmttypes "github.com/cometbft/cometbft/types"
8+
"github.com/oasisprotocol/oasis-core/go/common/cbor"
9+
)
10+
11+
// BlockMeta is the CometBFT-specific per-block metadata.
12+
type BlockMeta struct {
13+
// Header is the CometBFT block header.
14+
Header *cmttypes.Header `json:"header"`
15+
// LastCommit is the CometBFT last commit info.
16+
LastCommit *cmttypes.Commit `json:"last_commit"`
17+
}
18+
19+
// TryUnmarshal attempts to unmarshal the given data into a BlockMeta.
20+
//
21+
// It first tries to unmarshal into V1, and if that fails, it tries to
22+
// unmarshal into V2.
23+
//
24+
// We only try to unmarshal into the version of the metadata structure
25+
// starting at Oasis-Core Eden (V1), and into V2 (starting at #6235).
26+
// This may fail on blocks from an incompatible earlier version.
27+
func (b *BlockMeta) TryUnmarshal(data []byte) error {
28+
// Try to unmarshal into V1 first.
29+
switch err := cbor.Unmarshal(data, &b); {
30+
case err == nil:
31+
return nil
32+
default:
33+
// Continue below.
34+
}
35+
36+
// Try unmarshal into V2.
37+
var metaV2 blockMetaV2
38+
if err := cbor.Unmarshal(data, &metaV2); err != nil {
39+
return err
40+
}
41+
42+
// V2 uses protobuf encoding. Try decoding into BlockMeta.
43+
var lastCommitProto cmtproto.Commit
44+
if err := lastCommitProto.Unmarshal(metaV2.LastCommit); err != nil {
45+
return fmt.Errorf("malformed V2 block meta last commit: %w", err)
46+
}
47+
lastCommit, err := cmttypes.CommitFromProto(&lastCommitProto)
48+
if err != nil {
49+
return fmt.Errorf("malformed V2 block meta last commit: %w", err)
50+
}
51+
52+
var lastHeaderProto cmtproto.Header
53+
if err := lastHeaderProto.Unmarshal(metaV2.Header); err != nil {
54+
return fmt.Errorf("malformed V2 block meta last header: %w", err)
55+
}
56+
header, err := cmttypes.HeaderFromProto(&lastHeaderProto)
57+
if err != nil {
58+
return fmt.Errorf("malformed V2 block meta last header: %w", err)
59+
}
60+
61+
b.Header = &header
62+
b.LastCommit = lastCommit
63+
return nil
64+
}
65+
66+
// blockMetaV2 is the CometBFT-specific per-block metadata used in:
67+
// https://github.com/oasisprotocol/oasis-core/pull/6235
68+
type blockMetaV2 struct {
69+
// Header is the CometBFT block header.
70+
Header []byte `json:"header"`
71+
// LastCommit is the CometBFT last commit info.
72+
LastCommit []byte `json:"last_commit"`
73+
}

0 commit comments

Comments
 (0)