diff --git a/proto/babylon/checkpointing/v1/bls_key.proto b/proto/babylon/checkpointing/v1/bls_key.proto index 494dbb048..4ea1363c5 100644 --- a/proto/babylon/checkpointing/v1/bls_key.proto +++ b/proto/babylon/checkpointing/v1/bls_key.proto @@ -50,7 +50,7 @@ message VoteExtension { // validator_address is the address of the validator string validator_address = 2; // block_hash is the hash of the block that the vote extension is signed over - bytes block_hash = 3; + bytes block_hash = 3 [ (gogoproto.customtype) = "BlockHash" ]; // epoch_num is the epoch number of the vote extension uint64 epoch_num = 4; // height is the height of the vote extension diff --git a/testutil/datagen/vote_ext.go b/testutil/datagen/vote_ext.go index 6b880a133..1f0e35f8d 100644 --- a/testutil/datagen/vote_ext.go +++ b/testutil/datagen/vote_ext.go @@ -20,7 +20,7 @@ func GenRandomVoteExtension( sig := GenRandomBlsMultiSig(r) ve := checkpointingtypes.VoteExtension{ Signer: genesisKeys[i].ValidatorAddress, - BlockHash: blockHash, + BlockHash: &blockHash, EpochNum: epochNum, Height: height, BlsSig: &sig, diff --git a/testutil/helper/helper.go b/testutil/helper/helper.go index a59093b5d..be98fa3d5 100644 --- a/testutil/helper/helper.go +++ b/testutil/helper/helper.go @@ -105,7 +105,7 @@ func (h *Helper) getExtendedVotesFromValSet(epochNum, height uint64, blockHash c sig := bls12381.Sign(sk, signBytes) ve := checkpointingtypes.VoteExtension{ Signer: genesisKeys[i].ValidatorAddress, - BlockHash: blockHash, + BlockHash: &blockHash, EpochNum: epochNum, Height: height, BlsSig: &sig, diff --git a/x/checkpointing/proposal.go b/x/checkpointing/proposal.go index bb0b3200a..6be0001a9 100644 --- a/x/checkpointing/proposal.go +++ b/x/checkpointing/proposal.go @@ -1,6 +1,7 @@ package checkpointing import ( + "encoding/hex" "fmt" "slices" @@ -180,15 +181,54 @@ func (h *ProposalHandler) getValidBlsSigs(ctx sdk.Context, extendedVotes []abci. return validBLSSigs } -// findLastBlockHash finds the last block hash from the first vote extension -// this is a workaround that the last block hash can't be obtained from the context -// this is also safe as the BLS sig is already verified by VerifyVoteExtension +// findLastBlockHash iterates over all vote extensions and finds the block hash +// that consensus has agreed upon. +// We need to iterate over all block hashes to find the one that has achieved consensus +// as CometBFT does not verify vote extensions once it has achieved >2/3 voting power in a block. +// Contract: This function should only be called for Vote Extensions +// that have been included in a previous block. func (h *ProposalHandler) findLastBlockHash(extendedVotes []abci.ExtendedVoteInfo) ([]byte, error) { - var ve ckpttypes.VoteExtension - if err := ve.Unmarshal(extendedVotes[0].VoteExtension); err != nil { + // Mapping between block hashes and voting power committed to them + blockHashes := make(map[string]int64, 0) + // Iterate over vote extensions and if they have a valid structure + // increase the voting power of the block hash they commit to + for _, vote := range extendedVotes { + var ve ckpttypes.VoteExtension + if err := ve.Unmarshal(vote.VoteExtension); err != nil { + continue + } + // Encode the block hash using hex + blockHashes[hex.EncodeToString(ve.BlockHash.MustMarshal())] += vote.Validator.Power + } + var ( + maxPower int64 = 0 + totalPower int64 = 0 + resBlockHash string + ) + // Find the block hash that has the maximum voting power committed to it + for blockHash, power := range blockHashes { + if power > maxPower { + resBlockHash = blockHash + maxPower = power + } + totalPower += power + } + if len(resBlockHash) == 0 { + return nil, fmt.Errorf("could not find the block hash") + } + // Verify that the voting power committed to the found block hash is + // more than 2/3 of the total voting power. + if requiredVP := ((totalPower * 2) / 3) + 1; maxPower < requiredVP { + return nil, fmt.Errorf( + "insufficient cumulative voting power received to verify vote extensions; got: %d, expected: >=%d", + maxPower, requiredVP, + ) + } + decoded, err := hex.DecodeString(resBlockHash) + if err != nil { return nil, err } - return ve.ToBLSSig().BlockHash.MustMarshal(), nil + return decoded, nil } // ProcessProposal examines the checkpoint in the injected tx of the proposal diff --git a/x/checkpointing/types/bls_key.pb.go b/x/checkpointing/types/bls_key.pb.go index dd52170dc..cc2cd6b74 100644 --- a/x/checkpointing/types/bls_key.pb.go +++ b/x/checkpointing/types/bls_key.pb.go @@ -240,7 +240,7 @@ type VoteExtension struct { // validator_address is the address of the validator ValidatorAddress string `protobuf:"bytes,2,opt,name=validator_address,json=validatorAddress,proto3" json:"validator_address,omitempty"` // block_hash is the hash of the block that the vote extension is signed over - BlockHash []byte `protobuf:"bytes,3,opt,name=block_hash,json=blockHash,proto3" json:"block_hash,omitempty"` + BlockHash *BlockHash `protobuf:"bytes,3,opt,name=block_hash,json=blockHash,proto3,customtype=BlockHash" json:"block_hash,omitempty"` // epoch_num is the epoch number of the vote extension EpochNum uint64 `protobuf:"varint,4,opt,name=epoch_num,json=epochNum,proto3" json:"epoch_num,omitempty"` // height is the height of the vote extension @@ -296,13 +296,6 @@ func (m *VoteExtension) GetValidatorAddress() string { return "" } -func (m *VoteExtension) GetBlockHash() []byte { - if m != nil { - return m.BlockHash - } - return nil -} - func (m *VoteExtension) GetEpochNum() uint64 { if m != nil { return m.EpochNum @@ -330,40 +323,41 @@ func init() { } var fileDescriptor_3a8c0d37ce63f038 = []byte{ - // 524 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x93, 0x4f, 0x8b, 0xd3, 0x40, - 0x18, 0xc6, 0x77, 0xda, 0x35, 0x6b, 0xa7, 0x2b, 0xb8, 0x51, 0x96, 0xa0, 0x98, 0xad, 0x3d, 0x48, - 0x61, 0x31, 0xa1, 0x5d, 0x0a, 0xee, 0x61, 0x0f, 0x16, 0x14, 0x61, 0xc1, 0x2d, 0x29, 0x56, 0xf0, - 0x12, 0x33, 0xe9, 0x6c, 0x32, 0x74, 0x9a, 0x77, 0xc8, 0x4c, 0xe2, 0xe6, 0xe6, 0xc5, 0x9b, 0x07, - 0x3f, 0x81, 0x9f, 0xc7, 0xe3, 0x1e, 0xc5, 0x83, 0x48, 0xfb, 0x45, 0x64, 0x92, 0xb8, 0xf8, 0xa7, - 0x45, 0x10, 0x6f, 0x99, 0xe7, 0x79, 0xf3, 0xcc, 0xef, 0x7d, 0x67, 0x06, 0x3f, 0x20, 0x01, 0x29, - 0x38, 0x24, 0x6e, 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af, - 0x7e, 0x3e, 0x6c, 0xe3, 0x3f, 0x1e, 0xf6, 0xe8, 0xec, 0xd3, 0xd2, 0x46, 0x97, 0x4b, 0x1b, 0x7d, - 0x5b, 0xda, 0xe8, 0xc3, 0xca, 0xde, 0xba, 0x5c, 0xd9, 0x5b, 0x9f, 0x57, 0xf6, 0xd6, 0xab, 0xe1, - 0xdf, 0xc2, 0x2f, 0x7e, 0x7b, 0xa8, 0xaa, 0x10, 0x54, 0x12, 0xa3, 0x7c, 0x74, 0x47, 0xdf, 0x03, - 0x00, 0x00, 0xff, 0xff, 0xd2, 0x99, 0x6b, 0x42, 0xce, 0x03, 0x00, 0x00, + // 534 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x93, 0x4f, 0x8b, 0xd3, 0x4e, + 0x18, 0xc7, 0x3b, 0xed, 0xfe, 0xb2, 0xbf, 0x4e, 0xbb, 0xe0, 0x46, 0x59, 0x82, 0x42, 0x5a, 0x7b, + 0x90, 0xc2, 0x6a, 0x42, 0xbb, 0x14, 0xdc, 0xc3, 0x1e, 0x2c, 0x28, 0xc2, 0x82, 0x5b, 0x52, 0xac, + 0xe0, 0x25, 0x66, 0xd2, 0xd9, 0xcc, 0xd0, 0x69, 0x66, 0xc8, 0x4c, 0xe2, 0xe6, 0x05, 0x78, 0x13, + 0xf4, 0x15, 0xf8, 0x7a, 0x3c, 0xee, 0x51, 0xf6, 0xb0, 0x48, 0xfb, 0x46, 0x64, 0x92, 0xb8, 0xf8, + 0xa7, 0x45, 0x10, 0x6f, 0x33, 0xdf, 0xef, 0x93, 0xef, 0x7c, 0x9e, 0x67, 0x32, 0xf0, 0x01, 0x0a, + 0x50, 0xce, 0x78, 0xec, 0x86, 0x04, 0x87, 0x0b, 0xc1, 0x69, 0xac, 0x68, 0x1c, 0xb9, 0xd9, 0xc0, + 0x45, 0x4c, 0xfa, 0x0b, 0x9c, 0x3b,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, 0xec, 0xf3, 0xca, 0x06, 0x97, 0x2b, 0x1b, 0x7c, 0x5d, 0xd9, 0xe0, 0xe3, 0xda, 0xae, + 0x5d, 0xae, 0xed, 0xda, 0x97, 0xb5, 0x5d, 0x7b, 0x3d, 0xfa, 0x53, 0xf8, 0xc5, 0x2f, 0xef, 0x56, + 0xe5, 0x02, 0x4b, 0x64, 0x14, 0x6f, 0xf0, 0xe8, 0x5b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xf6, 0xf8, + 0x77, 0x30, 0xdd, 0x03, 0x00, 0x00, } func (m *BlsKey) Marshal() (dAtA []byte, err error) { @@ -576,10 +570,15 @@ func (m *VoteExtension) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x20 } - if len(m.BlockHash) > 0 { - i -= len(m.BlockHash) - copy(dAtA[i:], m.BlockHash) - i = encodeVarintBlsKey(dAtA, i, uint64(len(m.BlockHash))) + if m.BlockHash != nil { + { + size := m.BlockHash.Size() + i -= size + if _, err := m.BlockHash.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintBlsKey(dAtA, i, uint64(size)) + } i-- dAtA[i] = 0x1a } @@ -694,8 +693,8 @@ func (m *VoteExtension) Size() (n int) { if l > 0 { n += 1 + l + sovBlsKey(uint64(l)) } - l = len(m.BlockHash) - if l > 0 { + if m.BlockHash != nil { + l = m.BlockHash.Size() n += 1 + l + sovBlsKey(uint64(l)) } if m.EpochNum != 0 { @@ -1298,9 +1297,10 @@ func (m *VoteExtension) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.BlockHash = append(m.BlockHash[:0], dAtA[iNdEx:postIndex]...) - if m.BlockHash == nil { - m.BlockHash = []byte{} + var v BlockHash + m.BlockHash = &v + if err := m.BlockHash.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err } iNdEx = postIndex case 4: diff --git a/x/checkpointing/types/types.go b/x/checkpointing/types/types.go index 49bb68f62..e43e57bd8 100644 --- a/x/checkpointing/types/types.go +++ b/x/checkpointing/types/types.go @@ -202,10 +202,9 @@ func BytesToCkptWithMeta(cdc codec.BinaryCodec, bz []byte) (*RawCheckpointWithMe } func (ve *VoteExtension) ToBLSSig() *BlsSig { - blockHash := BlockHash(ve.BlockHash) return &BlsSig{ EpochNum: ve.EpochNum, - BlockHash: &blockHash, + BlockHash: ve.BlockHash, BlsSig: ve.BlsSig, SignerAddress: ve.Signer, ValidatorAddress: ve.ValidatorAddress, diff --git a/x/checkpointing/vote_ext.go b/x/checkpointing/vote_ext.go index 749714124..a0a157e9e 100644 --- a/x/checkpointing/vote_ext.go +++ b/x/checkpointing/vote_ext.go @@ -62,11 +62,16 @@ func (h *VoteExtensionHandler) ExtendVote() sdk.ExtendVoteHandler { epoch.EpochNumber, req.Height) } + var bhash ckpttypes.BlockHash + if err := bhash.Unmarshal(req.Hash); err != nil { + return emptyRes, fmt.Errorf("invalid CometBFT hash") + } + // 3. build vote extension ve := &ckpttypes.VoteExtension{ Signer: signer.String(), ValidatorAddress: k.GetValidatorAddress().String(), - BlockHash: req.Hash, + BlockHash: &bhash, EpochNum: epoch.EpochNumber, Height: uint64(req.Height), BlsSig: &blsSig,