Skip to content

Commit

Permalink
Devnet integration tweaks:
Browse files Browse the repository at this point in the history
* Removed unnecessary fields name and color of plane
* Do not interrupt tx processing if duplicated MsgCreatePlane received
* Do not check supervisor's sequence number to allow multiple parallel transactions
  • Loading branch information
mikluke committed Nov 9, 2023
1 parent 0e927c8 commit 1d5a8f1
Show file tree
Hide file tree
Showing 26 changed files with 220 additions and 2,303 deletions.
2 changes: 1 addition & 1 deletion app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
NewSigVerificationDecorator(options.AccountKeeper, options.ScorumKeeper, options.SignModeHandler),
NewCheckAddressesDecorator(options.AccountKeeper, options.ScorumKeeper),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
}
Expand Down
104 changes: 104 additions & 0 deletions app/ante/sig_verification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package ante

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
)

// SigVerificationDecorator is a copy of cosmos original SigVerificationDecorator
// Own implementation is needed to skip signature checking for supervisors to allow
// use the same key from many integrated services in parallel.
type SigVerificationDecorator struct {
ak AccountKeeper
sk ScorumKeeper

signModeHandler authsigning.SignModeHandler
}

func NewSigVerificationDecorator(ak AccountKeeper, sk ScorumKeeper, signModeHandler authsigning.SignModeHandler) SigVerificationDecorator {
return SigVerificationDecorator{
ak: ak,
sk: sk,
signModeHandler: signModeHandler,
}
}
func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
sigTx, ok := tx.(authsigning.SigVerifiableTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type")
}

// stdSigs contains the sequence number, account number, and signatures.
// When simulating, this would just be a 0-length slice.
sigs, err := sigTx.GetSignaturesV2()
if err != nil {
return ctx, err
}

signerAddrs := sigTx.GetSigners()

// check that signer length and signature length are the same
if len(sigs) != len(signerAddrs) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "invalid number of signer; expected: %d, got %d", len(signerAddrs), len(sigs))
}

for i, sig := range sigs {
acc, err := ante.GetSignerAcc(ctx, svd.ak, signerAddrs[i])
if err != nil {
return ctx, err
}

// retrieve pubkey
pubKey := acc.GetPubKey()
if !simulate && pubKey == nil {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidPubKey, "pubkey on account is not set")
}

// Check account sequence number.
// Applicable only for not supervisors
if !svd.sk.IsSupervisor(ctx, acc.GetAddress().String()) && sig.Sequence != acc.GetSequence() {
return ctx, sdkerrors.Wrapf(
sdkerrors.ErrWrongSequence,
"account sequence mismatch, expected %d, got %d", acc.GetSequence(), sig.Sequence,
)
}

// retrieve signer data
genesis := ctx.BlockHeight() == 0
chainID := ctx.ChainID()
var accNum uint64
if !genesis {
accNum = acc.GetAccountNumber()
}
signerData := authsigning.SignerData{
Address: acc.GetAddress().String(),
ChainID: chainID,
AccountNumber: accNum,
Sequence: sig.Sequence, // acc.GetSequence() was originally here, but supervisors allowed to put any value
PubKey: pubKey,
}

// no need to verify signatures on recheck tx
if !simulate && !ctx.IsReCheckTx() {
err := authsigning.VerifySignature(pubKey, signerData, sig.Data, svd.signModeHandler, tx)
if err != nil {
var errMsg string
if ante.OnlyLegacyAminoSigners(sig.Data) {
// If all signers are using SIGN_MODE_LEGACY_AMINO, we rely on VerifySignature to check account sequence number,
// and therefore communicate sequence number as a potential cause of error.
errMsg = fmt.Sprintf("signature verification failed; please verify account number (%d), sequence (%d) and chain-id (%s)", accNum, acc.GetSequence(), chainID)
} else {
errMsg = fmt.Sprintf("signature verification failed; please verify account number (%d) and chain-id (%s)", accNum, chainID)
}
return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, errMsg)

}
}
}

return next(ctx, tx, simulate)
}
13 changes: 0 additions & 13 deletions proto/network/aviatrix/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ service Query {
rpc Params (QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/aviatrix/params";
}

// PlaneByName queries the nft with provided name.
rpc PlaneByName (QueryPlaneByNameRequest) returns (QueryPlaneByNameResponse) {
option (google.api.http).get = "/aviatrix/planeByName";
}

}
// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}
Expand All @@ -33,10 +27,3 @@ message QueryParamsResponse {
Params params = 1 [(gogoproto.nullable) = false];
}

message QueryPlaneByNameRequest {
string name = 1;
}

message QueryPlaneByNameResponse {
cosmos.nft.v1beta1.NFT nft = 1;
}
22 changes: 0 additions & 22 deletions proto/network/aviatrix/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ option go_package = "github.com/scorum/cosmos-network/x/aviatrix/types";
// Msg defines the Msg service.
service Msg {
rpc CreatePlane(MsgCreatePlane) returns (MsgCreatePlaneResponse);
rpc UpdatePlaneName(MsgUpdatePlaneName) returns (MsgUpdatePlaneNameResponse);
rpc UpdatePlaneColor(MsgUpdatePlaneColor) returns (MsgUpdatePlaneColorResponse);
rpc UpdatePlaneExperience(MsgUpdatePlaneExperience) returns (MsgUpdatePlaneExperienceResponse);
rpc AdjustPlaneExperience(MsgAdjustPlaneExperience) returns (MsgAdjustPlaneExperienceResponse);
}

message PlaneMeta {
string name = 1;
string color = 2;
uint64 experience = 3;
}

Expand All @@ -37,24 +33,6 @@ message MsgCreatePlane {
}
message MsgCreatePlaneResponse {}

message MsgUpdatePlaneName {
option (cosmos.msg.v1.signer) = "supervisor";

string supervisor = 1;
string id = 2;
string name = 3;
}
message MsgUpdatePlaneNameResponse {}

message MsgUpdatePlaneColor {
option (cosmos.msg.v1.signer) = "supervisor";

string supervisor = 1;
string id = 2;
string color = 3;
}
message MsgUpdatePlaneColorResponse {}

message MsgUpdatePlaneExperience {
option (cosmos.msg.v1.signer) = "supervisor";

Expand Down
1 change: 0 additions & 1 deletion x/aviatrix/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
}

cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdQueryPlaneByName())

return cmd
}
37 changes: 0 additions & 37 deletions x/aviatrix/client/cli/query_plane_by_nft.go

This file was deleted.

2 changes: 0 additions & 2 deletions x/aviatrix/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ func GetTxCmd() *cobra.Command {

cmd.AddCommand(
CmdCreatePlane(),
CmdUpdatePlaneName(),
CmdUpdatePlaneColor(),
CmdUpdatePlaneExperience(),
CmdAdjustPlaneExperience(),
)
Expand Down
20 changes: 14 additions & 6 deletions x/aviatrix/client/cli/tx_create_nft.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cli

import (
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -11,9 +14,9 @@ import (

func CmdCreatePlane() *cobra.Command {
cmd := &cobra.Command{
Use: "create-plane [id] [owner] [name] [color]",
Use: "create-plane [id] [owner] [experience]",
Short: "Broadcast message CreatePlane",
Args: cobra.ExactArgs(4),
Args: cobra.RangeArgs(2, 3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -22,15 +25,20 @@ func CmdCreatePlane() *cobra.Command {

argId := args[0]
argOwner := args[1]
argName := args[2]
argColor := args[3]
var argExperience uint64

if len(args) == 3 {
argExperience, err = strconv.ParseUint(args[2], 10, 64)
if err != nil {
return fmt.Errorf("invalid experience: %w", err)
}
}

msg := types.NewMsgCreatePlane(
clientCtx.GetFromAddress().String(),
argId,
argOwner,
argName,
argColor,
argExperience,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
47 changes: 0 additions & 47 deletions x/aviatrix/client/cli/tx_update_plane_color.go

This file was deleted.

47 changes: 0 additions & 47 deletions x/aviatrix/client/cli/tx_update_plane_name.go

This file was deleted.

9 changes: 2 additions & 7 deletions x/aviatrix/keeper/msg_server_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ func (k msgServer) CreatePlane(goCtx context.Context, msg *types.MsgCreatePlane)

_, exists := k.nftKeeper.GetNFT(ctx, types.NftClassID, msg.Id)
if exists {
return nil, errorsmod.Wrap(sdkerrors.ErrConflict, "plane id is busy")
}

if k.planeNameIndex(ctx).Has([]byte(msg.Meta.Name)) {
return nil, errorsmod.Wrap(sdkerrors.ErrConflict, "plane name is busy")
ctx.Logger().With("owner", msg.Owner, "id", msg.Id).Info("attempt to create plane with existing id")
return &types.MsgCreatePlaneResponse{}, nil
}

if err := k.nftKeeper.Mint(ctx, nft.NFT{
Expand All @@ -52,7 +49,5 @@ func (k msgServer) CreatePlane(goCtx context.Context, msg *types.MsgCreatePlane)
return nil, fmt.Errorf("failed to mint nft: %w", err)
}

k.planeNameIndex(ctx).Set([]byte(msg.Meta.Name), []byte(msg.Id))

return &types.MsgCreatePlaneResponse{}, nil
}
Loading

0 comments on commit 1d5a8f1

Please sign in to comment.