Skip to content

Commit

Permalink
feat: support multiple messages in single tx for EIP712 (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed May 25, 2023
1 parent b3ded60 commit f0853ab
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 140 deletions.
14 changes: 0 additions & 14 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
SingleMessageDecorator{}, // Only one msg is allowed in EIP712 tx
NewSignModeDecorator(options.SignModeHandler), // Only SignMode_SIGN_MODE_EIP_712 is allowed
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(),
Expand All @@ -61,19 +60,6 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
return sdk.ChainAnteDecorators(anteDecorators...), nil
}

// SingleMessageDecorator prevents tx with multi msgs from being executed
type SingleMessageDecorator struct{}

// AnteHandle rejects txs that includes more than one msgs
func (smd SingleMessageDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if msgs := tx.GetMsgs(); len(msgs) != 1 {
return ctx, errors.Wrapf(
sdkerrors.ErrInvalidType, "Only one msg is allowed",
)
}
return next(ctx, tx, simulate)
}

// SignModeDecorator only allow EIP712 tx to be executed
type SignModeDecorator struct {
signModeHandler authsigning.SignModeHandler
Expand Down
2 changes: 1 addition & 1 deletion deployment/localup/.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ STAKING_BOND_AMOUNT=10000000000000000000000000
SNAPSHOT_INTERVAL=10
SNAPSHOT_KEEP_RECENT=0
KPASS=12345678
GENESIS_ACCOUNT_BALANCE=100000000000000000000000000
GENESIS_ACCOUNT_BALANCE=1000000000000000000000000000
COMMISSION_MAX_CHANGE_RATE=0.01
COMMISSION_MAX_RATE=1.0
COMMISSION_RATE=0.07
Expand Down
6 changes: 3 additions & 3 deletions e2e/core/basesuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ func (s *BaseSuite) SetupSuite() {
}
}

func (s *BaseSuite) SendTxBlock(msg sdk.Msg, from keys.KeyManager) *sdk.TxResponse {
func (s *BaseSuite) SendTxBlock(from keys.KeyManager, msg ...sdk.Msg) *sdk.TxResponse {
mode := tx.BroadcastMode_BROADCAST_MODE_SYNC
txOpt := &types.TxOption{
Mode: &mode,
Memo: "",
}
s.Client.SetKeyManager(from)
response, err := s.Client.BroadcastTx(context.Background(), []sdk.Msg{msg}, txOpt)
response, err := s.Client.BroadcastTx(context.Background(), append([]sdk.Msg{}, msg...), txOpt)
s.Require().NoError(err)

s.Require().NoError(s.CheckTxCode(response.TxResponse.TxHash, uint32(0)), "tx failed")
Expand Down Expand Up @@ -150,7 +150,7 @@ func (s *BaseSuite) GenAndChargeAccounts(n int, balance int64) (accounts []keys.
Inputs: []banktypes.Input{in},
Outputs: outputs,
}
_ = s.SendTxBlock(&msg, s.Validator)
_ = s.SendTxBlock(s.Validator, &msg)
return accounts
}

Expand Down
2 changes: 1 addition & 1 deletion e2e/tests/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *BridgeTestSuite) TestTransferOut() {

s.T().Logf("balance before: %s %s", from.GetAddr().String(), moduleBalanceBefore.Balance.String())

txRes := s.SendTxBlock(msgTransferOut, from)
txRes := s.SendTxBlock(from, msgTransferOut)
s.T().Log(txRes.RawLog)

moduleBalanceAfter, err := s.Client.Balance(ctx, &banktypes.QueryBalanceRequest{
Expand Down
18 changes: 9 additions & 9 deletions e2e/tests/challenge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *ChallengeTestSuite) createObject() (string, string, sdk.AccAddress, []s
nil, math.MaxUint, nil, 0)
msgCreateBucket.PrimarySpApproval.Sig, err = sp.ApprovalKey.Sign(msgCreateBucket.GetApprovalBytes())
s.Require().NoError(err)
s.SendTxBlock(msgCreateBucket, user)
s.SendTxBlock(user, msgCreateBucket)

// HeadBucket
ctx := context.Background()
Expand Down Expand Up @@ -85,7 +85,7 @@ func (s *ChallengeTestSuite) createObject() (string, string, sdk.AccAddress, []s
msgCreateObject := storagetypes.NewMsgCreateObject(user.GetAddr(), bucketName, objectName, uint64(payloadSize), storagetypes.VISIBILITY_TYPE_PRIVATE, expectChecksum, contextType, storagetypes.REDUNDANCY_EC_TYPE, math.MaxUint, nil, nil)
msgCreateObject.PrimarySpApproval.Sig, err = sp.ApprovalKey.Sign(msgCreateObject.GetApprovalBytes())
s.Require().NoError(err)
s.SendTxBlock(msgCreateObject, user)
s.SendTxBlock(user, msgCreateObject)

// HeadObject
queryHeadObjectRequest := storagetypes.QueryHeadObjectRequest{
Expand Down Expand Up @@ -114,7 +114,7 @@ func (s *ChallengeTestSuite) createObject() (string, string, sdk.AccAddress, []s

secondarySigs := [][]byte{secondarySig, secondarySig, secondarySig, secondarySig, secondarySig, secondarySig}
msgSealObject.SecondarySpSignatures = secondarySigs
s.SendTxBlock(msgSealObject, sp.SealKey)
s.SendTxBlock(sp.SealKey, msgSealObject)

queryHeadObjectResponse, err = s.Client.HeadObject(ctx, &queryHeadObjectRequest)
s.Require().NoError(err)
Expand All @@ -130,15 +130,15 @@ func (s *ChallengeTestSuite) TestSubmit() {

bucketName, objectName, primarySp, _ := s.createObject()
msgSubmit := challengetypes.NewMsgSubmit(user.GetAddr(), primarySp, bucketName, objectName, true, 1000)
txRes := s.SendTxBlock(msgSubmit, user)
txRes := s.SendTxBlock(user, msgSubmit)
event := filterChallengeEventFromTx(txRes) // secondary sps are faked with primary sp, redundancy check is meaningless here
s.Require().GreaterOrEqual(event.ChallengeId, uint64(0))
s.Require().NotEqual(event.SegmentIndex, uint32(100))
s.Require().Equal(event.SpOperatorAddress, primarySp.String())

bucketName, objectName, _, secondarySps := s.createObject()
msgSubmit = challengetypes.NewMsgSubmit(user.GetAddr(), secondarySps[0], bucketName, objectName, false, 0)
txRes = s.SendTxBlock(msgSubmit, user)
txRes = s.SendTxBlock(user, msgSubmit)
event = filterChallengeEventFromTx(txRes)
s.Require().GreaterOrEqual(event.ChallengeId, uint64(0))
s.Require().Equal(event.SegmentIndex, uint32(0))
Expand Down Expand Up @@ -169,7 +169,7 @@ func (s *ChallengeTestSuite) TestNormalAttest() {

bucketName, objectName, primarySp, _ := s.createObject()
msgSubmit := challengetypes.NewMsgSubmit(user.GetAddr(), primarySp, bucketName, objectName, true, 1000)
txRes := s.SendTxBlock(msgSubmit, user)
txRes := s.SendTxBlock(user, msgSubmit)
event := filterChallengeEventFromTx(txRes)

statusRes, err := s.TmClient.TmClient.Status(context.Background())
Expand Down Expand Up @@ -202,7 +202,7 @@ func (s *ChallengeTestSuite) TestNormalAttest() {
}

// submit attest
txRes = s.SendTxBlock(msgAttest, s.Challenger)
txRes = s.SendTxBlock(s.Challenger, msgAttest)
s.Require().True(txRes.Code == 0)

queryRes, err := s.Client.ChallengeQueryClient.LatestAttestedChallenges(context.Background(), &challengetypes.QueryLatestAttestedChallengesRequest{})
Expand Down Expand Up @@ -280,7 +280,7 @@ func (s *ChallengeTestSuite) TestHeartbeatAttest() {
}

// submit attest
txRes := s.SendTxBlock(msgAttest, s.Challenger)
txRes := s.SendTxBlock(s.Challenger, msgAttest)
s.Require().True(txRes.Code == 0)

queryRes, err := s.Client.ChallengeQueryClient.LatestAttestedChallenges(context.Background(), &challengetypes.QueryLatestAttestedChallengesRequest{})
Expand All @@ -300,7 +300,7 @@ func (s *ChallengeTestSuite) TestFailedAttest_ChallengeExpired() {

bucketName, objectName, primarySp, _ := s.createObject()
msgSubmit := challengetypes.NewMsgSubmit(user.GetAddr(), primarySp, bucketName, objectName, true, 1000)
txRes := s.SendTxBlock(msgSubmit, user)
txRes := s.SendTxBlock(user, msgSubmit)
event := filterChallengeEventFromTx(txRes)

statusRes, err := s.TmClient.TmClient.Status(context.Background())
Expand Down
58 changes: 58 additions & 0 deletions e2e/tests/eip712_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tests

import (
"context"
"math"
"testing"

"github.com/bnb-chain/greenfield/e2e/core"
storageutils "github.com/bnb-chain/greenfield/testutil/storage"
storagetypes "github.com/bnb-chain/greenfield/x/storage/types"
"github.com/stretchr/testify/suite"
)

type Eip712TestSuite struct {
core.BaseSuite
}

func (s *Eip712TestSuite) SetupSuite() {
s.BaseSuite.SetupSuite()
}

func (s *Eip712TestSuite) SetupTest() {
}

func TestEip712TestSuite(t *testing.T) {
suite.Run(t, new(Eip712TestSuite))
}

func (s *Eip712TestSuite) TestMultiMessages() {
var err error
sp := s.StorageProviders[0]
user := s.GenAndChargeAccounts(1, 1000000)[0]

// CreateBucket
bucketName := storageutils.GenRandomBucketName()
msgCreateBucket := storagetypes.NewMsgCreateBucket(
user.GetAddr(), bucketName, storagetypes.VISIBILITY_TYPE_PUBLIC_READ, sp.OperatorKey.GetAddr(),
nil, math.MaxUint, nil, 0)
msgCreateBucket.PrimarySpApproval.Sig, err = sp.ApprovalKey.Sign(msgCreateBucket.GetApprovalBytes())
s.Require().NoError(err)

// UpdateBucketInfo
msgUpdateBucketInfo := storagetypes.NewMsgUpdateBucketInfo(
user.GetAddr(), bucketName, nil, user.GetAddr(), storagetypes.VISIBILITY_TYPE_PRIVATE)
s.Require().NoError(err)

// send two messages together without error
s.SendTxBlock(user, msgCreateBucket, msgUpdateBucketInfo)

// verify modified bucketinfo
ctx := context.Background()
queryHeadBucketRequest := storagetypes.QueryHeadBucketRequest{
BucketName: bucketName,
}
queryHeadBucketResponseAfterUpdateBucket, err := s.Client.HeadBucket(ctx, &queryHeadBucketRequest)
s.Require().NoError(err)
s.Require().Equal(queryHeadBucketResponseAfterUpdateBucket.BucketInfo.Visibility, storagetypes.VISIBILITY_TYPE_PRIVATE)
}
4 changes: 2 additions & 2 deletions e2e/tests/gashub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *GashubTestSuite) TestUpdateParams() {
)
s.Require().NoError(err)

txRes := s.SendTxBlock(msgProposal, s.Validator)
txRes := s.SendTxBlock(s.Validator, msgProposal)
s.Require().Equal(txRes.Code, uint32(0))

// 2. query proposal
Expand All @@ -71,7 +71,7 @@ func (s *GashubTestSuite) TestUpdateParams() {

// 3. submit MsgVote and wait the proposal exec
msgVote := govtypesv1.NewMsgVote(validator, proposalId, govtypesv1.OptionYes, "test")
txRes = s.SendTxBlock(msgVote, s.Validator)
txRes = s.SendTxBlock(s.Validator, msgVote)
s.Require().Equal(txRes.Code, uint32(0))

for {
Expand Down
4 changes: 2 additions & 2 deletions e2e/tests/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (s *PaymentTestSuite) TestPaymentAccount() {
msgCreatePaymentAccount := &paymenttypes.MsgCreatePaymentAccount{
Creator: user.GetAddr().String(),
}
_ = s.SendTxBlock(msgCreatePaymentAccount, user)
_ = s.SendTxBlock(user, msgCreatePaymentAccount)
// query user's payment accounts
queryGetPaymentAccountsByOwnerRequest := paymenttypes.QueryGetPaymentAccountsByOwnerRequest{
Owner: user.GetAddr().String(),
Expand All @@ -51,7 +51,7 @@ func (s *PaymentTestSuite) TestPaymentAccount() {
Owner: user.GetAddr().String(),
Addr: paymentAccountAddr,
}
_ = s.SendTxBlock(msgDisableRefund, user)
_ = s.SendTxBlock(user, msgDisableRefund)
// query this payment account
paymentAccount, err = s.Client.PaymentAccount(ctx, &queryGetPaymentAccountRequest)
s.Require().NoError(err)
Expand Down
Loading

0 comments on commit f0853ab

Please sign in to comment.