Skip to content

Commit

Permalink
batch antehandle unwrap message
Browse files Browse the repository at this point in the history
  • Loading branch information
haiyizxx committed Mar 19, 2024
1 parent 1ff535f commit 7878c6b
Show file tree
Hide file tree
Showing 19 changed files with 240 additions and 266 deletions.
20 changes: 9 additions & 11 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ import (
axelarnetKeeper "github.com/axelarnetwork/axelar-core/x/axelarnet/keeper"
axelarnetTypes "github.com/axelarnetwork/axelar-core/x/axelarnet/types"
axelarbankkeeper "github.com/axelarnetwork/axelar-core/x/bank/keeper"
"github.com/axelarnetwork/axelar-core/x/batcher"
batchertypes "github.com/axelarnetwork/axelar-core/x/batcher/types"
"github.com/axelarnetwork/axelar-core/x/batch"
batchtypes "github.com/axelarnetwork/axelar-core/x/batch/types"
"github.com/axelarnetwork/axelar-core/x/evm"
evmKeeper "github.com/axelarnetwork/axelar-core/x/evm/keeper"
evmTypes "github.com/axelarnetwork/axelar-core/x/evm/types"
Expand Down Expand Up @@ -659,7 +659,7 @@ func initAppModules(keepers *KeeperCache, bApp *bam.BaseApp, encodingConfig axel
bApp.Router(),
),
permission.NewAppModule(*getKeeper[permissionKeeper.Keeper](keepers)),
batcher.NewAppModule(encodingConfig.Codec, bApp.MsgServiceRouter()),
batch.NewAppModule(encodingConfig.Codec, bApp.MsgServiceRouter()),
)

return appModules
Expand Down Expand Up @@ -690,6 +690,7 @@ func initAnteHandlers(encodingConfig axelarParams.EncodingConfig, keys map[strin

anteDecorators := []sdk.AnteDecorator{
ante.NewAnteHandlerDecorator(baseAnteHandler),
ante.NewBatchDecorator(encodingConfig.Codec), // unwrap batch messages, must be done before any other decorators
}

// enforce wasm limits earlier in the ante handler chain
Expand All @@ -716,9 +717,6 @@ func initAnteHandlers(encodingConfig axelarParams.EncodingConfig, keys map[strin
initMessageAnteDecorators(encodingConfig, keepers).ToAnteHandler()),
)

batchAntehandler := sdk.ChainAnteDecorators(anteDecorators...)
anteDecorators = append(anteDecorators, ante.NewBatchDecorator(encodingConfig.Codec, batchAntehandler))

return sdk.ChainAnteDecorators(anteDecorators...)
}

Expand Down Expand Up @@ -796,7 +794,7 @@ func orderMigrations() []string {
permissionTypes.ModuleName,
snapTypes.ModuleName,
axelarnetTypes.ModuleName,
batchertypes.ModuleName,
batchtypes.ModuleName,
)
return migrationOrder
}
Expand Down Expand Up @@ -847,7 +845,7 @@ func orderBeginBlockers() []string {
snapTypes.ModuleName,
axelarnetTypes.ModuleName,
voteTypes.ModuleName,
batchertypes.ModuleName,
batchtypes.ModuleName,
)
return beginBlockerOrder
}
Expand Down Expand Up @@ -893,7 +891,7 @@ func orderEndBlockers() []string {
axelarnetTypes.ModuleName,
permissionTypes.ModuleName,
voteTypes.ModuleName,
batchertypes.ModuleName,
batchtypes.ModuleName,
)
return endBlockerOrder
}
Expand Down Expand Up @@ -942,7 +940,7 @@ func orderModulesForGenesis() []string {
axelarnetTypes.ModuleName,
rewardTypes.ModuleName,
permissionTypes.ModuleName,
batchertypes.ModuleName,
batchtypes.ModuleName,
)
return genesisOrder
}
Expand Down Expand Up @@ -1113,7 +1111,7 @@ func GetModuleBasics() module.BasicManager {
axelarnet.AppModuleBasic{},
reward.AppModuleBasic{},
permission.AppModuleBasic{},
batcher.AppModuleBasic{},
batch.AppModuleBasic{},
}

if IsWasmEnabled() {
Expand Down
52 changes: 26 additions & 26 deletions docs/proto/proto-docs.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
syntax = "proto3";
package axelar.batcher.v1beta1;
package axelar.batch.v1beta1;

option go_package = "github.com/axelarnetwork/axelar-core/x/batcher/types";
option go_package = "github.com/axelarnetwork/axelar-core/x/batch/types";
option (gogoproto.messagename_all) = true;

import "gogoproto/gogo.proto";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
syntax = "proto3";
package axelar.batcher.v1beta1;
package axelar.batch.v1beta1;

option go_package = "github.com/axelarnetwork/axelar-core/x/batcher/types";
option go_package = "github.com/axelarnetwork/axelar-core/x/batch/types";

import "gogoproto/gogo.proto";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
syntax = "proto3";
package axelar.batcher.v1beta1;
package axelar.batch.v1beta1;

option go_package = "github.com/axelarnetwork/axelar-core/x/batcher/types";
option go_package = "github.com/axelarnetwork/axelar-core/x/batch/types";

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "axelar/batcher/v1beta1/tx.proto";
import "axelar/batch/v1beta1/tx.proto";

option (gogoproto.goproto_registration) = true;

// Msg defines the nexus Msg service.
service MsgService {
rpc Batch(BatchRequest) returns (BatchResponse) {
option (google.api.http) = {
post : "/axelar/batcher/batch"
post : "/axelar/batch/batch"
body : "*"
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
syntax = "proto3";
package axelar.batcher.v1beta1;
package axelar.batch.v1beta1;

import "google/protobuf/any.proto";
import "gogoproto/gogo.proto";

import "axelar/permission/exported/v1beta1/types.proto";
import "cosmos/base/abci/v1beta1/abci.proto";

option go_package = "github.com/axelarnetwork/axelar-core/x/batcher/types";
option go_package = "github.com/axelarnetwork/axelar-core/x/batch/types";

message BatchRequest {
option (permission.exported.v1beta1.permission_role) = ROLE_UNRESTRICTED;
Expand Down
68 changes: 41 additions & 27 deletions x/ante/batch.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package ante

import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

batchertypes "github.com/axelarnetwork/axelar-core/x/batcher/types"
batchtypes "github.com/axelarnetwork/axelar-core/x/batch/types"
)

var _ sdk.FeeTx = (*BatchDecorator)(nil)

// BatchDecorator implements the Tx interface and runs anteHandler on the inner messages of a batch request
type BatchDecorator struct {
anteHandler sdk.AnteHandler
cdc codec.Codec
messages []sdk.Msg
sdk.FeeTx

cdc codec.Codec
messages []sdk.Msg
}

// NewBatchDecorator is the constructor for BatchDecorator
func NewBatchDecorator(cdc codec.Codec, anteHandler sdk.AnteHandler) BatchDecorator {
func NewBatchDecorator(cdc codec.Codec) BatchDecorator {
return BatchDecorator{
anteHandler,
nil,
cdc,
[]sdk.Msg{},
}
Expand All @@ -30,35 +31,34 @@ func NewBatchDecorator(cdc codec.Codec, anteHandler sdk.AnteHandler) BatchDecora
func (b BatchDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
msgs := tx.GetMsgs()

for _, msg := range msgs {
switch req := msg.(type) {
case *batchertypes.BatchRequest:
var err error
if !anyBatch(msgs) {
return next(ctx, tx, simulate)
}

for _, m := range req.Messages {
var sdkMsg sdk.Msg
if err = b.cdc.UnpackAny(&m, &sdkMsg); err != nil {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, fmt.Sprintf("unpack failed: %s", err))
}
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx must be a FeeTx")
}

if !msg.GetSigners()[0].Equals(req.Sender) {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, fmt.Sprintf("message signer mismatch"))
}
b.FeeTx = feeTx

b.messages = append(b.messages, sdkMsg)
for _, msg := range msgs {
switch req := msg.(type) {
case *batchtypes.BatchRequest:
innerMsgs := req.UnwrapMessages()
if anyBatch(innerMsgs) {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "nested batch requests are not allowed")
}

ctx, err = b.anteHandler(ctx, b, simulate)
if err != nil {
return ctx, err
}
b.messages = append(b.messages, innerMsgs...)

default:
continue
b.messages = append(b.messages, msg)
}

}

return next(ctx, tx, simulate)
return next(ctx, b, simulate)
}

func (b BatchDecorator) ValidateBasic() error {
Expand All @@ -74,3 +74,17 @@ func (b BatchDecorator) ValidateBasic() error {
func (b BatchDecorator) GetMsgs() []sdk.Msg {
return b.messages
}

func anyBatch(msgs []sdk.Msg) bool {
if len(msgs) == 0 {
return false
}

for _, msg := range msgs {
switch msg.(type) {
case *batchtypes.BatchRequest:
return true
}
}
return false
}
Loading

0 comments on commit 7878c6b

Please sign in to comment.