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

feat: added MsgUpsertSequencer to the admission handler #352

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
127 changes: 106 additions & 21 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"os"
"path/filepath"

"github.com/dymensionxyz/rollapp-evm/app/ante"

"github.com/cosmos/cosmos-sdk/x/authz"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/dymensionxyz/dymension-rdk/server/consensus"
"github.com/gogo/protobuf/proto"
prototypes "github.com/gogo/protobuf/types"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/spf13/cast"
Expand Down Expand Up @@ -110,6 +111,7 @@ import (

srvflags "github.com/evmos/evmos/v12/server/flags"

"github.com/dymensionxyz/rollapp-evm/app/ante"
rollappevmparams "github.com/dymensionxyz/rollapp-evm/app/params"

// unnamed import of statik for swagger UI support
Expand Down Expand Up @@ -301,24 +303,24 @@ type App struct {
memKeys map[string]*storetypes.MemoryStoreKey

// keepers
AccountKeeper authkeeper.AccountKeeper
AuthzKeeper authzkeeper.Keeper
BankKeeper bankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper stakingkeeper.Keeper
SequencersKeeper seqkeeper.Keeper
MintKeeper mintkeeper.Keeper
EpochsKeeper epochskeeper.Keeper
DistrKeeper distrkeeper.Keeper
GovKeeper govkeeper.Keeper
HubKeeper hubkeeper.Keeper
HubGenesisKeeper hubgenkeeper.Keeper
UpgradeKeeper upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
TransferKeeper transferkeeper.Keeper
FeeGrantKeeper feegrantkeeper.Keeper
TimeUpgradeKeeper timeupgradekeeper.Keeper
AccountKeeper authkeeper.AccountKeeper
AuthzKeeper authzkeeper.Keeper
BankKeeper bankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper stakingkeeper.Keeper
SequencersKeeper seqkeeper.Keeper
MintKeeper mintkeeper.Keeper
EpochsKeeper epochskeeper.Keeper
DistrKeeper distrkeeper.Keeper
GovKeeper govkeeper.Keeper
HubKeeper hubkeeper.Keeper
HubGenesisKeeper hubgenkeeper.Keeper
UpgradeKeeper upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
TransferKeeper transferkeeper.Keeper
FeeGrantKeeper feegrantkeeper.Keeper
TimeUpgradeKeeper timeupgradekeeper.Keeper
RollappParamsKeeper rollappparamskeeper.Keeper

// make scoped keepers public for test purposes
Expand All @@ -341,6 +343,8 @@ type App struct {

// module configurator
configurator module.Configurator

consensusMessageAdmissionHandler consensus.AdmissionHandler
}

// NewRollapp returns a reference to an initialized blockchain app
Expand Down Expand Up @@ -833,6 +837,12 @@ func NewRollapp(
app.SetAnteHandler(h)
app.setPostHandler()

// Admission handler for consensus messages
app.setAdmissionHandler(consensus.AllowedMessagesHandler([]string{
// proto.MessageName(&banktypes.MsgSend{}), // Example of message allowed as consensus message
Copy link
Contributor

Choose a reason for hiding this comment

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

you can probably remove this comment

proto.MessageName(&seqtypes.MsgUpsertSequencer{}),
}))

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
Expand All @@ -856,12 +866,87 @@ func (app *App) setPostHandler() {
app.SetPostHandler(postHandler)
}

func (app *App) setAdmissionHandler(handler consensus.AdmissionHandler) {
app.consensusMessageAdmissionHandler = handler
}

// Name returns the name of the App
func (app *App) Name() string { return app.BaseApp.Name() }

// BeginBlocker application updates every begin block
func (app *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
return app.mm.BeginBlock(ctx, req)
consensusResponses := app.processConsensusMessage(ctx, req.ConsensusMessages)

resp := app.mm.BeginBlock(ctx, req)
resp.ConsensusMessagesResponses = consensusResponses

return resp
}

func (app *App) processConsensusMessage(ctx sdk.Context, consensusMsgs []*prototypes.Any) []*abci.ConsensusMessageResponse {
var responses []*abci.ConsensusMessageResponse

for _, anyMsg := range consensusMsgs {
sdkAny := &types.Any{
TypeUrl: "/" + anyMsg.TypeUrl,
Value: anyMsg.Value,
}

var msg sdk.Msg
err := app.appCodec.UnpackAny(sdkAny, &msg)
if err != nil {
responses = append(responses, &abci.ConsensusMessageResponse{
Response: &abci.ConsensusMessageResponse_Error{
Error: fmt.Errorf("failed to unpack consensus message: %w", err).Error(),
},
})

continue
}

cacheCtx, writeCache := ctx.CacheContext()
err = app.consensusMessageAdmissionHandler(cacheCtx, msg)
if err != nil {
responses = append(responses, &abci.ConsensusMessageResponse{
Response: &abci.ConsensusMessageResponse_Error{
Error: fmt.Errorf("consensus message admission failed: %w", err).Error(),
},
})

continue
}

resp, err := app.MsgServiceRouter().Handler(msg)(ctx, msg)
if err != nil {
responses = append(responses, &abci.ConsensusMessageResponse{
Response: &abci.ConsensusMessageResponse_Error{
Error: fmt.Errorf("failed to execute consensus message: %w", err).Error(),
},
})

continue
}

theType, err := proto.Marshal(resp)
if err != nil {
return nil
}

anyResp := &prototypes.Any{
TypeUrl: proto.MessageName(resp),
Value: theType,
}

responses = append(responses, &abci.ConsensusMessageResponse{
Response: &abci.ConsensusMessageResponse_Ok{
Ok: anyResp,
},
})

writeCache()
}

return responses
}

// EndBlocker application updates every end block
Expand Down
100 changes: 100 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package app

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/gogo/protobuf/proto"
prototypes "github.com/gogo/protobuf/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/proto/tendermint/types"
)

func TestBeginBlocker(t *testing.T) {
app, valAccount := SetupWithOneValidator(t)
ctx := app.NewUncachedContext(true, types.Header{
Height: 1,
ChainID: "testchain_9000-1",
})

bankSend := &banktypes.MsgSend{
FromAddress: valAccount.GetAddress().String(),
ToAddress: valAccount.GetAddress().String(),
Amount: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100))),
}
msgBz, err := proto.Marshal(bankSend)
require.NoError(t, err)

goodMessage := &prototypes.Any{
TypeUrl: proto.MessageName(&banktypes.MsgSend{}),
Value: msgBz,
}

testCases := []struct {
name string
consensusMsgs []*prototypes.Any
expectError bool
}{
{
name: "ValidConsensusMessage",
consensusMsgs: []*prototypes.Any{
goodMessage,
},
expectError: false,
},
{
name: "InvalidUnpackMessage",
consensusMsgs: []*prototypes.Any{
{
TypeUrl: "/path.to.InvalidMsg",
Value: []byte("invalid unpack data"),
},
},
expectError: true,
},
{
name: "InvalidExecutionMessage",
consensusMsgs: []*prototypes.Any{
{
TypeUrl: "/path.to.ExecErrorMsg",
Value: []byte("execution error data"),
},
},
expectError: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := abci.RequestBeginBlock{
Header: types.Header{
Height: 1,
Time: ctx.BlockTime(),
ChainID: "testchain_9000-1",
},
LastCommitInfo: abci.LastCommitInfo{},
ByzantineValidators: []abci.Evidence{},
ConsensusMessages: tc.consensusMsgs,
}

res := app.BeginBlocker(ctx, req)
require.NotNil(t, res)

if tc.expectError {
require.NotEmpty(t, res.ConsensusMessagesResponses)
for _, response := range res.ConsensusMessagesResponses {
_, isError := response.Response.(*abci.ConsensusMessageResponse_Error)
require.True(t, isError, "Expected an error response but got a success")
}
} else {
require.NotEmpty(t, res.ConsensusMessagesResponses)
for _, response := range res.ConsensusMessagesResponses {
_, isOk := response.Response.(*abci.ConsensusMessageResponse_Ok)
require.True(t, isOk, "Expected a success response but got an error")
}
}
})
}
}
Loading
Loading