From 4e1e1e2980e52e55c01683cbc1db1ce848eba1b3 Mon Sep 17 00:00:00 2001 From: kenta92115 Date: Fri, 22 Mar 2024 07:30:31 +0000 Subject: [PATCH] chore: remove abic file and use simapp vote extension handler --- app/abci.go | 84 ----------------------------------------------------- app/app.go | 3 +- 2 files changed, 2 insertions(+), 85 deletions(-) delete mode 100644 app/abci.go diff --git a/app/abci.go b/app/abci.go deleted file mode 100644 index 94b34cc3..00000000 --- a/app/abci.go +++ /dev/null @@ -1,84 +0,0 @@ -package app - -import ( - "bytes" - "crypto/rand" - "encoding/json" - "fmt" - - abci "github.com/cometbft/cometbft/abci/types" - - "github.com/cosmos/cosmos-sdk/baseapp" - sdk "github.com/cosmos/cosmos-sdk/types" -) - -type ( - // VoteExtensionHandler defines a dummy vote extension handler for SimApp. - // - // NOTE: This implementation is solely used for testing purposes. DO NOT use - // in a production application! - VoteExtensionHandler struct{} - - // VoteExtension defines the structure used to create a dummy vote extension. - VoteExtension struct { - Hash []byte - Height int64 - Data []byte - } -) - -func NewVoteExtensionHandler() *VoteExtensionHandler { - return &VoteExtensionHandler{} -} - -func (h *VoteExtensionHandler) SetHandlers(bApp *baseapp.BaseApp) { - bApp.SetExtendVoteHandler(h.ExtendVote()) - bApp.SetVerifyVoteExtensionHandler(h.VerifyVoteExtension()) -} - -func (h *VoteExtensionHandler) ExtendVote() sdk.ExtendVoteHandler { - return func(_ sdk.Context, req *abci.RequestExtendVote) (*abci.ResponseExtendVote, error) { - buf := make([]byte, 1024) - - _, err := rand.Read(buf) - if err != nil { - return nil, fmt.Errorf("failed to generate random vote extension data: %w", err) - } - - ve := VoteExtension{ - Hash: req.Hash, - Height: req.Height, - Data: buf, - } - - bz, err := json.Marshal(ve) - if err != nil { - return nil, fmt.Errorf("failed to encode vote extension: %w", err) - } - - return &abci.ResponseExtendVote{VoteExtension: bz}, nil - } -} - -func (h *VoteExtensionHandler) VerifyVoteExtension() sdk.VerifyVoteExtensionHandler { - return func(ctx sdk.Context, req *abci.RequestVerifyVoteExtension) (*abci.ResponseVerifyVoteExtension, error) { - var ve VoteExtension - - if err := json.Unmarshal(req.VoteExtension, &ve); err != nil { - return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil - } - - switch { - case req.Height != ve.Height: - return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil - - case !bytes.Equal(req.Hash, ve.Hash): - return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil - - case len(ve.Data) != 1024: - return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil - } - - return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_ACCEPT}, nil - } -} diff --git a/app/app.go b/app/app.go index 5bd93e7c..510e3f9f 100644 --- a/app/app.go +++ b/app/app.go @@ -139,6 +139,7 @@ import ( "swisstronik/ethereum/eip712" sdkmath "cosmossdk.io/math" + "cosmossdk.io/simapp" addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/types/mempool" "github.com/cosmos/cosmos-sdk/x/consensus" @@ -313,7 +314,7 @@ func New( eip712.SetEncodingConfig(encodingConfig) // create and set dummy vote extension handler voteExtOp := func(bApp *baseapp.BaseApp) { - voteExtHandler := NewVoteExtensionHandler() + voteExtHandler := simapp.NewVoteExtensionHandler() voteExtHandler.SetHandlers(bApp) } baseAppOptions = append(baseAppOptions, voteExtOp, baseapp.SetOptimisticExecution())