Skip to content

refactor: simplify getting abci block #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions pkg/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/abci/types"
cmtcfg "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/libs/bytes"
"github.com/cometbft/cometbft/mempool"
corep2p "github.com/cometbft/cometbft/p2p"
cmtprototypes "github.com/cometbft/cometbft/proto/tendermint/types"
Expand Down Expand Up @@ -559,15 +558,15 @@ func fireEvents(

func (a *Adapter) getLastCommit(ctx context.Context, blockHeight uint64) (*cmttypes.Commit, error) {
if blockHeight > 1 {
header, data, err := a.RollkitStore.GetBlockData(ctx, blockHeight-1)
header, _, err := a.RollkitStore.GetBlockData(ctx, blockHeight-1)
if err != nil {
return nil, fmt.Errorf("get previous block data: %w", err)
}

commitForPrevBlock := &cmttypes.Commit{
Height: int64(header.Height()),
Round: 0,
BlockID: cmttypes.BlockID{Hash: bytes.HexBytes(header.Hash()), PartSetHeader: cmttypes.PartSetHeader{Total: 1, Hash: bytes.HexBytes(data.Hash())}},
BlockID: cmttypes.BlockID{},
Signatures: []cmttypes.CommitSig{
{
BlockIDFlag: cmttypes.BlockIDFlagCommit,
Expand Down Expand Up @@ -602,7 +601,7 @@ func cometCommitToABCICommitInfo(commit *cmttypes.Commit) abci.CommitInfo {
votes[i] = abci.VoteInfo{
Validator: abci.Validator{
Address: sig.ValidatorAddress,
Power: 0,
Power: 1,
},
BlockIdFlag: cmtprototypes.BlockIDFlag(sig.BlockIDFlag),
}
Expand Down
54 changes: 18 additions & 36 deletions pkg/cometcompat/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@ import (

// ToABCIBlock converts Rolkit block into block format defined by ABCI.
func ToABCIBlock(header *rlktypes.SignedHeader, data *rlktypes.Data, lastCommit *cmttypes.Commit) (*cmttypes.Block, error) {
abciHeader, err := ToABCIHeader(&header.Header)
if err != nil {
return nil, err
}

// validate have one validator
if len(header.ProposerAddress) == 0 {
return nil, errors.New("proposer address is not set")
}

// set commit hash
abciHeader.LastCommitHash = lastCommit.Hash()
abciHeader := cmttypes.Header{
Version: cmprotoversion.Consensus{
Block: cmtversion.BlockProtocol,
App: header.Version.App,
},
Height: int64(header.Height()), //nolint:gosec
Time: header.Time(),
LastBlockID: lastCommit.BlockID,
LastCommitHash: lastCommit.Hash(),
DataHash: cmbytes.HexBytes(header.DataHash),
ConsensusHash: cmbytes.HexBytes(header.ConsensusHash),
AppHash: cmbytes.HexBytes(header.AppHash),
LastResultsHash: cmbytes.HexBytes(header.LastResultsHash),
EvidenceHash: new(cmttypes.EvidenceData).Hash(),
ProposerAddress: header.ProposerAddress,
ChainID: header.ChainID(),
}

// set validator hash
if header.Signer.Address != nil {
Expand All @@ -44,6 +54,7 @@ func ToABCIBlock(header *rlktypes.SignedHeader, data *rlktypes.Data, lastCommit
},
LastCommit: lastCommit,
}

abciBlock.Txs = make([]cmttypes.Tx, len(data.Txs))
for i := range data.Txs {
abciBlock.Txs[i] = cmttypes.Tx(data.Txs[i])
Expand All @@ -68,32 +79,3 @@ func ToABCIBlockMeta(header *rlktypes.SignedHeader, data *rlktypes.Data, lastCom
NumTxs: len(cmblock.Txs),
}, nil
}

// ToABCIHeader converts Rollkit header to Header format defined in ABCI.
func ToABCIHeader(header *rlktypes.Header) (cmttypes.Header, error) {
return cmttypes.Header{
Version: cmprotoversion.Consensus{
Block: cmtversion.BlockProtocol,
App: header.Version.App,
},
Height: int64(header.Height()), //nolint:gosec
Time: header.Time(),
LastBlockID: cmttypes.BlockID{
Hash: cmbytes.HexBytes(header.LastHeaderHash),
PartSetHeader: cmttypes.PartSetHeader{
Total: 0,
Hash: nil,
},
},
LastCommitHash: cmbytes.HexBytes(header.LastCommitHash),
DataHash: cmbytes.HexBytes(header.DataHash),
ConsensusHash: cmbytes.HexBytes(header.ConsensusHash),
AppHash: cmbytes.HexBytes(header.AppHash),
LastResultsHash: cmbytes.HexBytes(header.LastResultsHash),
EvidenceHash: new(cmttypes.EvidenceData).Hash(),
ProposerAddress: header.ProposerAddress,
ChainID: header.ChainID(),
// validator hash and next validator hash are not set here
// they are set later (in ToABCIBlock)
}, nil
}
16 changes: 5 additions & 11 deletions pkg/cometcompat/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@ import (

func PayloadProvider() types.SignaturePayloadProvider {
return func(header *types.Header) ([]byte, error) {
abciHeaderForSigning, err := ToABCIHeader(header)
if err != nil {
return nil, err
}
vote := cmtproto.Vote{
Type: cmtproto.PrecommitType,
Height: int64(header.Height()), //nolint:gosec
Round: 0,
BlockID: cmtproto.BlockID{
Hash: abciHeaderForSigning.Hash(),
PartSetHeader: cmtproto.PartSetHeader{},
},
Type: cmtproto.PrecommitType,
Height: int64(header.Height()), //nolint:gosec
Round: 0,
BlockID: cmtproto.BlockID{},
Timestamp: header.Time(),
ValidatorAddress: header.ProposerAddress,
ValidatorIndex: 0,
}

chainID := header.ChainID()
consensusVoteBytes := cmttypes.VoteSignBytes(chainID, &vote)

Expand Down
63 changes: 1 addition & 62 deletions pkg/rpc/core/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

cmbytes "github.com/cometbft/cometbft/libs/bytes"
cmquery "github.com/cometbft/cometbft/libs/pubsub/query"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
rpctypes "github.com/cometbft/cometbft/rpc/jsonrpc/types"
cmttypes "github.com/cometbft/cometbft/types"
Expand Down Expand Up @@ -141,35 +140,6 @@ func Block(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlock, error)
return nil, err
}

// Then re-sign the final ABCI header if we have a signer
if env.Signer != nil {
// Create a vote for the final ABCI header
vote := cmtproto.Vote{
Type: cmtproto.PrecommitType,
Height: int64(header.Height()), //nolint:gosec
Round: 0,
BlockID: cmtproto.BlockID{
Hash: abciBlock.Header.Hash(),
PartSetHeader: cmtproto.PartSetHeader{},
},
Timestamp: abciBlock.Time,
ValidatorAddress: header.ProposerAddress,
ValidatorIndex: 0,
}
chainID := header.ChainID()
finalSignBytes := cmttypes.VoteSignBytes(chainID, &vote)

newSignature, err := env.Signer.Sign(finalSignBytes)
if err != nil {
return nil, fmt.Errorf("failed to sign final ABCI header: %w", err)
}

// Update the signature in the block
if len(abciBlock.LastCommit.Signatures) > 0 {
abciBlock.LastCommit.Signatures[0].Signature = newSignature
}
}

return &ctypes.ResultBlock{
BlockID: cmttypes.BlockID{Hash: abciBlock.Hash()},
Block: abciBlock,
Comment on lines 144 to 145

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The BlockID field is being populated with the hash of the abciBlock, but the PartSetHeader is not being set. This could lead to issues if the block needs to be reconstructed from parts. Consider populating the PartSetHeader with appropriate values, or ensuring that it's handled correctly elsewhere in the code. Leaving it empty might cause unexpected behavior in CometBFT's light client or other components that rely on the PartSetHeader.

Suggested change
BlockID: cmttypes.BlockID{Hash: abciBlock.Hash()},
Block: abciBlock,
BlockID: cmttypes.BlockID{Hash: abciBlock.Hash(), PartSetHeader: cmttypes.PartSetHeader{Total: 1, Hash: abciBlock.DataHash}},

Expand Down Expand Up @@ -236,46 +206,15 @@ func Commit(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultCommit, erro
}},
}

// First apply ToABCIBlock to get the final header with all transformations
abciBlock, err := cometcompat.ToABCIBlock(header, rollkitData, abciCommit)
if err != nil {
return nil, err
}

// Then re-sign the final ABCI header if we have a signer
if env.Signer != nil {
// Create a vote for the final ABCI header
vote := cmtproto.Vote{
Type: cmtproto.PrecommitType,
Height: int64(header.Height()), //nolint:gosec
Round: 0,
BlockID: cmtproto.BlockID{
Hash: abciBlock.Header.Hash(),
PartSetHeader: cmtproto.PartSetHeader{},
},
Timestamp: abciBlock.Time,
ValidatorAddress: header.ProposerAddress,
ValidatorIndex: 0,
}
chainID := header.ChainID()
finalSignBytes := cmttypes.VoteSignBytes(chainID, &vote)

newSignature, err := env.Signer.Sign(finalSignBytes)
if err != nil {
return nil, fmt.Errorf("failed to sign final ABCI header: %w", err)
}

// Update the commit with the new signature
abciBlock.LastCommit.Signatures[0].Signature = newSignature
}

// Update the commit's BlockID to match the final ABCI block hash
abciBlock.LastCommit.BlockID.Hash = abciBlock.Header.Hash()

return &ctypes.ResultCommit{
SignedHeader: cmttypes.SignedHeader{
Header: &abciBlock.Header,
Commit: abciBlock.LastCommit,
Commit: abciCommit,
},
CanonicalCommit: true,
}, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/rpc/core/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestCommit_VerifyCometBFTLightClientCompatibility_MultipleBlocks(t *testing
}

var trustedHeader cmttypes.SignedHeader
var isFirstBlock = true
isFirstBlock := true

// Test multiple blocks
for i := 1; i <= 3; i++ {
Expand All @@ -206,7 +206,7 @@ func TestCommit_VerifyCometBFTLightClientCompatibility_MultipleBlocks(t *testing
verifyFirstBlock(t, fixedValSet, chainID, trustedHeader)
isFirstBlock = false
} else {
verifySubsequentBlock(t, fixedValSet, &trustedHeader, &commitResult.SignedHeader, rollkitHeader, realSignature)
verifySubsequentBlock(t, fixedValSet, &trustedHeader, &commitResult.SignedHeader, rollkitHeader)
trustedHeader = commitResult.SignedHeader
}
}
Expand Down Expand Up @@ -311,7 +311,7 @@ func verifyFirstBlock(t *testing.T, valSet *cmttypes.ValidatorSet, chainID strin
}
}

func verifySubsequentBlock(t *testing.T, valSet *cmttypes.ValidatorSet, trustedHeader, newHeader *cmttypes.SignedHeader, rollkitHeader types.Header, realSignature []byte) {
func verifySubsequentBlock(t *testing.T, valSet *cmttypes.ValidatorSet, trustedHeader, newHeader *cmttypes.SignedHeader, rollkitHeader types.Header) {
require := require.New(t)

trustingPeriod := 3 * time.Hour
Expand Down
Loading