Skip to content

Commit

Permalink
Merge pull request #345 from PeggyJV/bolten/reject-empty-batches
Browse files Browse the repository at this point in the history
  • Loading branch information
zmanian authored Jan 28, 2022
2 parents 16bf556 + 3864937 commit 5924851
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
5 changes: 5 additions & 0 deletions module/x/gravity/keeper/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func (k Keeper) BuildBatchTx(ctx sdk.Context, contractAddress common.Address, ma
return len(selectedStes) == maxElements
})

// do not create batches that would contain no transactions, even if they are requested
if len(selectedStes) == 0 {
return nil
}

batch := &types.BatchTx{
BatchNonce: k.incrementLastOutgoingBatchNonce(ctx),
Timeout: k.getBatchTimeoutHeight(ctx),
Expand Down
26 changes: 26 additions & 0 deletions module/x/gravity/keeper/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,29 @@ func TestPoolTxRefund(t *testing.T) {
balances := input.BankKeeper.GetAllBalances(ctx, mySender)
require.Equal(t, sdk.NewInt(104), balances.AmountOf(myDenom))
}

func TestEmptyBatch(t *testing.T) {
input := CreateTestEnv(t)
ctx := input.Context

var (
now = time.Now().UTC()
mySender, _ = sdk.AccAddressFromBech32("cosmos1ahx7f8wyertuus9r20284ej0asrs085case3kn")
myTokenContractAddr = common.HexToAddress("0x429881672B9AE42b8EbA0E26cD9C73711b891Ca5") // Pickle
allVouchers = sdk.NewCoins(
types.NewERC20Token(99999, myTokenContractAddr.Hex()).GravityCoin(),
)
)

// mint some voucher first
require.NoError(t, input.BankKeeper.MintCoins(ctx, types.ModuleName, allVouchers))
// set senders balance
input.AccountKeeper.NewAccountWithAddress(ctx, mySender)
require.NoError(t, fundAccount(ctx, input.BankKeeper, mySender, allVouchers))

// no transactions should be included in this batch
ctx = ctx.WithBlockTime(now)
batchTx := input.GravityKeeper.BuildBatchTx(ctx, myTokenContractAddr, 2)

require.Nil(t, batchTx)
}
3 changes: 3 additions & 0 deletions module/x/gravity/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ func (k msgServer) RequestBatchTx(c context.Context, msg *types.MsgRequestBatchT
}

batchID := k.BuildBatchTx(ctx, tokenContract, BatchTxSize)
if batchID == nil {
return nil, fmt.Errorf("no suitable batch to create")
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
Expand Down
76 changes: 74 additions & 2 deletions module/x/gravity/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,85 @@ func TestMsgServer_CancelSendToEthereum(t *testing.T) {
}

func TestMsgServer_RequestBatchTx(t *testing.T) {
ethPrivKey, err := ethCrypto.GenerateKey()
require.NoError(t, err)

var (
env = CreateTestEnv(t)
ctx = env.Context
gk = env.GravityKeeper

orcAddr1, _ = sdk.AccAddressFromBech32("cosmos1dg55rtevlfxh46w88yjpdd08sqhh5cc3xhkcej")
valAddr1 = sdk.ValAddress(orcAddr1)
ethAddr1 = crypto.PubkeyToAddress(ethPrivKey.PublicKey)

orcAddr2, _ = sdk.AccAddressFromBech32("cosmos164knshrzuuurf05qxf3q5ewpfnwzl4gj4m4dfy")
valAddr2 = sdk.ValAddress(orcAddr2)

orcAddr3, _ = sdk.AccAddressFromBech32("cosmos193fw83ynn76328pty4yl7473vg9x86alq2cft7")
valAddr3 = sdk.ValAddress(orcAddr3)

testDenom = "stake"

balance = sdk.Coin{
Denom: testDenom,
Amount: sdk.NewInt(10000),
}
amount = sdk.Coin{
Denom: testDenom,
Amount: sdk.NewInt(1000),
}
fee = sdk.Coin{
Denom: testDenom,
Amount: sdk.NewInt(10),
}
)

{ // setup for getSignerValidator
gk.StakingKeeper = NewStakingKeeperMock(valAddr1, valAddr2, valAddr3)
gk.SetOrchestratorValidatorAddress(ctx, valAddr1, orcAddr1)
}

{ // add balance to bank
err = env.AddBalanceToBank(ctx, orcAddr1, sdk.Coins{balance})
require.NoError(t, err)
}

// create denom in keeper
gk.setCosmosOriginatedDenomToERC20(ctx, testDenom, "testcontractstring")

// setup for GetValidatorEthereumAddress
gk.setValidatorEthereumAddress(ctx, valAddr1, ethAddr1)

msgServer := NewMsgServerImpl(gk)

msg := &types.MsgSendToEthereum{
Sender: orcAddr1.String(),
EthereumRecipient: ethAddr1.String(),
Amount: amount,
BridgeFee: fee,
}

_, err = msgServer.SendToEthereum(sdk.WrapSDKContext(ctx), msg)
require.NoError(t, err)

requestMsg := &types.MsgRequestBatchTx{
Signer: orcAddr1.String(),
Denom: testDenom,
}

_, err = msgServer.RequestBatchTx(sdk.WrapSDKContext(ctx), requestMsg)
require.NoError(t, err)
}

func TestMsgServer_RequestEmptyBatchTx(t *testing.T) {
var (
env = CreateTestEnv(t)
ctx = env.Context
gk = env.GravityKeeper

orcAddr1, _ = sdk.AccAddressFromBech32("cosmos1dg55rtevlfxh46w88yjpdd08sqhh5cc3xhkcej")
valAddr1 = sdk.ValAddress(orcAddr1)
//ethAddr1 = crypto.PubkeyToAddress(ethPrivKey.PublicKey)

orcAddr2, _ = sdk.AccAddressFromBech32("cosmos164knshrzuuurf05qxf3q5ewpfnwzl4gj4m4dfy")
valAddr2 = sdk.ValAddress(orcAddr2)
Expand All @@ -244,7 +315,8 @@ func TestMsgServer_RequestBatchTx(t *testing.T) {
}

_, err := msgServer.RequestBatchTx(sdk.WrapSDKContext(ctx), msg)
require.NoError(t, err)

require.Error(t, err)
}

func TestMsgServer_SubmitEthereumEvent(t *testing.T) {
Expand Down

0 comments on commit 5924851

Please sign in to comment.