Skip to content
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

Vote ext verify #167

Merged
merged 4 commits into from
Jun 6, 2024
Merged
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
27 changes: 25 additions & 2 deletions app/extend_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ type VoteExtHandler struct {
oracleKeeper OracleKeeper
bridgeKeeper BridgeKeeper
codec codec.Codec
// cosmosCtx sdk.Context
}

type OracleAttestation struct {
Expand Down Expand Up @@ -168,7 +167,31 @@ func (h *VoteExtHandler) ExtendVoteHandler(ctx sdk.Context, req *abci.RequestExt
}

func (h *VoteExtHandler) VerifyVoteExtensionHandler(ctx sdk.Context, req *abci.RequestVerifyVoteExtension) (*abci.ResponseVerifyVoteExtension, error) {
// TODO: implement the logic to verify the vote extension
var voteExt BridgeVoteExtension
err := json.Unmarshal(req.VoteExtension, &voteExt)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal vote extension: %w", err)
}
// ensure oracle attestations length is less than or equal to the number of attestation requests
attestationRequests, err := h.bridgeKeeper.GetAttestationRequestsByHeight(ctx, uint64(ctx.BlockHeight()-1))
if err != nil {
if !errors.Is(err, collections.ErrNotFound) {
return nil, err
} else if len(voteExt.OracleAttestations) > 0 {
return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil
}
} else if len(voteExt.OracleAttestations) > len(attestationRequests.Requests) {
return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil
}
// verify the initial signature size
if len(voteExt.InitialSignature.SignatureA) > 65 || len(voteExt.InitialSignature.SignatureB) > 65 {
return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil
}
// verify the valset signature size
if len(voteExt.ValsetSignature.Signature) > 65 {
return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil
}

Comment on lines +176 to +194
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks good to me 👌 Let's still perform tests and make sure that we are not "over-rejecting". For example, if there's a case in which the vote extension being malformed won't make a difference, mayyybe allow it

return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_ACCEPT}, nil
}

Expand Down
71 changes: 67 additions & 4 deletions app/proposal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"reflect"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -42,10 +43,11 @@ type OracleAttestations struct {
}

type VoteExtTx struct {
BlockHeight int64 `json:"block_height"`
OpAndEVMAddrs OperatorAndEVM `json:"op_and_evm_addrs"`
ValsetSigs ValsetSignatures `json:"valset_sigs"`
OracleAttestations OracleAttestations `json:"oracle_attestations"`
BlockHeight int64 `json:"block_height"`
OpAndEVMAddrs OperatorAndEVM `json:"op_and_evm_addrs"`
ValsetSigs ValsetSignatures `json:"valset_sigs"`
OracleAttestations OracleAttestations `json:"oracle_attestations"`
ExtendedCommitInfo abci.ExtendedCommitInfo `json:"extended_commit_info"`
}

func NewProposalHandler(logger log.Logger, valStore baseapp.ValidatorStore, appCodec codec.Codec, oracleKeeper OracleKeeper, bridgeKeeper BridgeKeeper, stakingKeeper StakingKeeper) *ProposalHandler {
Expand Down Expand Up @@ -122,6 +124,7 @@ func (h *ProposalHandler) PrepareProposalHandler(ctx sdk.Context, req *abci.Requ
OpAndEVMAddrs: operatorAndEvm,
ValsetSigs: valsetSigs,
OracleAttestations: oracleAttestations,
ExtendedCommitInfo: req.LocalLastCommit,
}

bz, err := json.Marshal(injectedVoteExtTx)
Expand All @@ -139,6 +142,66 @@ func (h *ProposalHandler) PrepareProposalHandler(ctx sdk.Context, req *abci.Requ
}

func (h *ProposalHandler) ProcessProposalHandler(ctx sdk.Context, req *abci.RequestProcessProposal) (*abci.ResponseProcessProposal, error) {
h.logger.Info("@ProcessProposalHandler", "height", req.Height, "voteExtEnableHeight", ctx.ConsensusParams().Abci.VoteExtensionsEnableHeight)
if req.Height > ctx.ConsensusParams().Abci.VoteExtensionsEnableHeight {
var injectedVoteExtTx VoteExtTx
if err := json.Unmarshal(req.Txs[0], &injectedVoteExtTx); err != nil {
h.logger.Error("failed to decode injected vote extension tx", "err", err)
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}
err := baseapp.ValidateVoteExtensions(ctx, h.valStore, req.Height, ctx.ChainID(), injectedVoteExtTx.ExtendedCommitInfo)
if err != nil {
return nil, err
}

operatorAddresses, evmAddresses, err := h.CheckInitialSignaturesFromLastCommit(ctx, injectedVoteExtTx.ExtendedCommitInfo)
if err != nil {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

if !reflect.DeepEqual(operatorAddresses, injectedVoteExtTx.OpAndEVMAddrs.OperatorAddresses) {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

if !reflect.DeepEqual(evmAddresses, injectedVoteExtTx.OpAndEVMAddrs.EVMAddresses) {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

valsetOperatorAddresses, valsetTimestamps, valsetSignatures, err := h.CheckValsetSignaturesFromLastCommit(ctx, injectedVoteExtTx.ExtendedCommitInfo)
if err != nil {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

if !reflect.DeepEqual(valsetOperatorAddresses, injectedVoteExtTx.ValsetSigs.OperatorAddresses) {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

if !reflect.DeepEqual(valsetTimestamps, injectedVoteExtTx.ValsetSigs.Timestamps) {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

if !reflect.DeepEqual(valsetSignatures, injectedVoteExtTx.ValsetSigs.Signatures) {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

oracleSigs, oracleSnapshots, oracleOperatorAddresses, err := h.CheckOracleAttestationsFromLastCommit(ctx, injectedVoteExtTx.ExtendedCommitInfo)
if err != nil {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

if !reflect.DeepEqual(oracleSigs, injectedVoteExtTx.OracleAttestations.Attestations) {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

if !reflect.DeepEqual(oracleSnapshots, injectedVoteExtTx.OracleAttestations.Snapshots) {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

if !reflect.DeepEqual(oracleOperatorAddresses, injectedVoteExtTx.OracleAttestations.OperatorAddresses) {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}
}

return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_ACCEPT}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion start_scripts/start_bill.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ echo "$VALIDATOR_JSON" > $NODE2_HOME_DIR/config/validator.json

# Stake Bill as a validator
echo "Staking bill as a validator..."
./layerd tx staking create-validator ~/.layer/bill/config/validator.json --from bill --keyring-backend $KEYRING_BACKEND --keyring-dir ~/.layer/bill --chain-id layer --home ~/.layer/bill
./layerd tx staking create-validator ~/.layer/bill/config/validator.json --from bill --keyring-backend $KEYRING_BACKEND --keyring-dir ~/.layer/bill --chain-id layer --home ~/.layer/bill --gas 300000

# Modify keyring-backend in client.toml for bill
echo "Modifying keyring-backend in client.toml for bill..."
Expand Down
Loading