Skip to content

Commit

Permalink
Merge PR #3428: SendMsg and MultiSendMsg seperation
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 authored and jackzampolin committed Feb 4, 2019
1 parent 0822951 commit 18eee0a
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 81 deletions.
4 changes: 2 additions & 2 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func TestCoinSendAccAuto(t *testing.T) {
require.Equal(t, expectedBalance.Amount.SubRaw(1), coins[0].Amount)
}

func TestCoinSendGenerateOnly(t *testing.T) {
func TestCoinMultiSendGenerateOnly(t *testing.T) {
addr, seed := CreateAddr(t, name1, pw, GetKeyBase(t))
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()
Expand All @@ -277,7 +277,7 @@ func TestCoinSendGenerateOnly(t *testing.T) {
require.Equal(t, memo, stdTx.Memo)
require.NotZero(t, stdTx.Fee.Gas)
require.IsType(t, stdTx.GetMsgs()[0], bank.MsgSend{})
require.Equal(t, addr, stdTx.GetMsgs()[0].(bank.MsgSend).Inputs[0].Address)
require.Equal(t, addr, stdTx.GetMsgs()[0].(bank.MsgSend).FromAddress)
}

func TestCoinSendGenerateSignAndBroadcast(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/app/benchmarks/txsize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ExampleTxSendSize() {
priv2 := secp256k1.GenPrivKeySecp256k1([]byte{1})
addr2 := sdk.AccAddress(priv2.PubKey().Address())
coins := sdk.Coins{sdk.NewCoin("denom", sdk.NewInt(10))}
msg1 := bank.MsgSend{
msg1 := bank.MsgMultiSend{
Inputs: []bank.Input{bank.NewInput(addr1, coins)},
Outputs: []bank.Output{bank.NewOutput(addr2, coins)},
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/gaia/app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ func randIntBetween(r *rand.Rand, min, max int) int {
func testAndRunTxs(app *GaiaApp) []simulation.WeightedOperation {
return []simulation.WeightedOperation{
{5, authsim.SimulateDeductFee(app.accountKeeper, app.feeCollectionKeeper)},
{100, banksim.SingleInputSendMsg(app.accountKeeper, app.bankKeeper)},
{100, banksim.SendMsg(app.accountKeeper, app.bankKeeper)},
{10, banksim.SingleInputMsgMultiSend(app.accountKeeper, app.bankKeeper)},
{50, distrsim.SimulateMsgSetWithdrawAddress(app.accountKeeper, app.distrKeeper)},
{50, distrsim.SimulateMsgWithdrawDelegatorReward(app.accountKeeper, app.distrKeeper)},
{50, distrsim.SimulateMsgWithdrawValidatorCommission(app.accountKeeper, app.distrKeeper)},
Expand Down
32 changes: 17 additions & 15 deletions x/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ var (
manyCoins = sdk.Coins{sdk.NewInt64Coin("foocoin", 1), sdk.NewInt64Coin("barcoin", 1)}
freeFee = auth.NewStdFee(100000, sdk.Coins{sdk.NewInt64Coin("foocoin", 0)})

sendMsg1 = MsgSend{
sendMsg1 = NewMsgSend(addr1, addr2, coins)

multiSendMsg1 = MsgMultiSend{
Inputs: []Input{NewInput(addr1, coins)},
Outputs: []Output{NewOutput(addr2, coins)},
}
sendMsg2 = MsgSend{
multiSendMsg2 = MsgMultiSend{
Inputs: []Input{NewInput(addr1, coins)},
Outputs: []Output{
NewOutput(addr2, halfCoins),
NewOutput(addr3, halfCoins),
},
}
sendMsg3 = MsgSend{
multiSendMsg3 = MsgMultiSend{
Inputs: []Input{
NewInput(addr1, coins),
NewInput(addr4, coins),
Expand All @@ -66,15 +68,15 @@ var (
NewOutput(addr3, coins),
},
}
sendMsg4 = MsgSend{
multiSendMsg4 = MsgMultiSend{
Inputs: []Input{
NewInput(addr2, coins),
},
Outputs: []Output{
NewOutput(addr1, coins),
},
}
sendMsg5 = MsgSend{
multiSendMsg5 = MsgMultiSend{
Inputs: []Input{
NewInput(addr1, manyCoins),
},
Expand Down Expand Up @@ -102,7 +104,7 @@ func getInitChainer(mapp *mock.App, keeper BaseKeeper) sdk.InitChainer {
}
}

func TestMsgSendWithAccounts(t *testing.T) {
func TestMsgMultiSendWithAccounts(t *testing.T) {
mapp := getMockApp(t)
acc := &auth.BaseAccount{
Address: addr1,
Expand All @@ -119,7 +121,7 @@ func TestMsgSendWithAccounts(t *testing.T) {

testCases := []appTestCase{
{
msgs: []sdk.Msg{sendMsg1},
msgs: []sdk.Msg{multiSendMsg1},
accNums: []uint64{0},
accSeqs: []uint64{0},
expSimPass: true,
Expand All @@ -131,7 +133,7 @@ func TestMsgSendWithAccounts(t *testing.T) {
},
},
{
msgs: []sdk.Msg{sendMsg1, sendMsg2},
msgs: []sdk.Msg{multiSendMsg1, multiSendMsg2},
accNums: []uint64{0},
accSeqs: []uint64{0},
expSimPass: true, // doesn't check signature
Expand All @@ -149,7 +151,7 @@ func TestMsgSendWithAccounts(t *testing.T) {
}
}

func TestMsgSendMultipleOut(t *testing.T) {
func TestMsgMultiSendMultipleOut(t *testing.T) {
mapp := getMockApp(t)

acc1 := &auth.BaseAccount{
Expand All @@ -165,7 +167,7 @@ func TestMsgSendMultipleOut(t *testing.T) {

testCases := []appTestCase{
{
msgs: []sdk.Msg{sendMsg2},
msgs: []sdk.Msg{multiSendMsg2},
accNums: []uint64{0},
accSeqs: []uint64{0},
expSimPass: true,
Expand All @@ -188,7 +190,7 @@ func TestMsgSendMultipleOut(t *testing.T) {
}
}

func TestSengMsgMultipleInOut(t *testing.T) {
func TestMsgMultiSendMultipleInOut(t *testing.T) {
mapp := getMockApp(t)

acc1 := &auth.BaseAccount{
Expand All @@ -208,7 +210,7 @@ func TestSengMsgMultipleInOut(t *testing.T) {

testCases := []appTestCase{
{
msgs: []sdk.Msg{sendMsg3},
msgs: []sdk.Msg{multiSendMsg3},
accNums: []uint64{0, 0},
accSeqs: []uint64{0, 0},
expSimPass: true,
Expand All @@ -232,7 +234,7 @@ func TestSengMsgMultipleInOut(t *testing.T) {
}
}

func TestMsgSendDependent(t *testing.T) {
func TestMsgMultiSendDependent(t *testing.T) {
mapp := getMockApp(t)

acc1 := &auth.BaseAccount{
Expand All @@ -244,7 +246,7 @@ func TestMsgSendDependent(t *testing.T) {

testCases := []appTestCase{
{
msgs: []sdk.Msg{sendMsg1},
msgs: []sdk.Msg{multiSendMsg1},
accNums: []uint64{0},
accSeqs: []uint64{0},
expSimPass: true,
Expand All @@ -256,7 +258,7 @@ func TestMsgSendDependent(t *testing.T) {
},
},
{
msgs: []sdk.Msg{sendMsg4},
msgs: []sdk.Msg{multiSendMsg4},
accNums: []uint64{0},
accSeqs: []uint64{0},
expSimPass: true,
Expand Down
30 changes: 30 additions & 0 deletions x/bank/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,33 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) {
benchmarkApp.Commit()
}
}

func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) {
benchmarkApp, _ := getBenchmarkMockApp()

// Add an account at genesis
acc := &auth.BaseAccount{
Address: addr1,
// Some value conceivably higher than the benchmarks would ever go
Coins: sdk.Coins{sdk.NewInt64Coin("foocoin", 100000000000)},
}
accs := []auth.Account{acc}

// Construct genesis state
mock.SetGenesis(benchmarkApp, accs)
// Precompute all txs
txs := mock.GenSequenceOfTxs([]sdk.Msg{multiSendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
b.ResetTimer()
// Run this with a profiler, so its easy to distinguish what time comes from
// Committing, and what time comes from Check/Deliver Tx.
for i := 0; i < b.N; i++ {
benchmarkApp.BeginBlock(abci.RequestBeginBlock{})
x := benchmarkApp.Check(txs[i])
if !x.IsOK() {
panic("something is broken in checking transaction")
}
benchmarkApp.Deliver(txs[i])
benchmarkApp.EndBlock(abci.RequestEndBlock{})
benchmarkApp.Commit()
}
}
4 changes: 2 additions & 2 deletions x/bank/client/cli/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
bankClient "github.com/cosmos/cosmos-sdk/x/bank/client"
"github.com/cosmos/cosmos-sdk/x/bank"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -62,7 +62,7 @@ func SendTxCmd(cdc *codec.Codec) *cobra.Command {
}

// build and sign the transaction, then broadcast to Tendermint
msg := bankClient.CreateMsg(from, to, coins)
msg := bank.NewMsgSend(from, to, coins)
if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(os.Stdout, txBldr, cliCtx, []sdk.Msg{msg}, false)
}
Expand Down
5 changes: 2 additions & 3 deletions x/bank/client/rest/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank"
bankclient "github.com/cosmos/cosmos-sdk/x/bank/client"

"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -64,7 +63,7 @@ func SendRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIC
return
}

msg := bankclient.CreateMsg(fromAddr, toAddr, req.Amount)
msg := bank.NewMsgSend(fromAddr, toAddr, req.Amount)
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
Expand All @@ -77,7 +76,7 @@ func SendRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.CLIC
}

cliCtx = cliCtx.WithFromName(fromName).WithFromAddress(fromAddress)
msg := bankclient.CreateMsg(cliCtx.GetFromAddress(), toAddr, req.Amount)
msg := bank.NewMsgSend(cliCtx.GetFromAddress(), toAddr, req.Amount)

rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
Expand Down
14 changes: 0 additions & 14 deletions x/bank/client/util.go

This file was deleted.

1 change: 1 addition & 0 deletions x/bank/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
// Register concrete types on codec codec
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(MsgSend{}, "cosmos-sdk/Send", nil)
cdc.RegisterConcrete(MsgMultiSend{}, "cosmos-sdk/MultiSend", nil)
}

var msgCdc = codec.New()
Expand Down
17 changes: 17 additions & 0 deletions x/bank/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ func NewHandler(k Keeper) sdk.Handler {
switch msg := msg.(type) {
case MsgSend:
return handleMsgSend(ctx, k, msg)
case MsgMultiSend:
return handleMsgMultiSend(ctx, k, msg)
default:
errMsg := "Unrecognized bank Msg type: %s" + msg.Type()
return sdk.ErrUnknownRequest(errMsg).Result()
Expand All @@ -19,6 +21,21 @@ func NewHandler(k Keeper) sdk.Handler {

// Handle MsgSend.
func handleMsgSend(ctx sdk.Context, k Keeper, msg MsgSend) sdk.Result {
if !k.GetSendEnabled(ctx) {
return ErrSendDisabled(k.Codespace()).Result()
}
tags, err := k.SendCoins(ctx, msg.FromAddress, msg.ToAddress, msg.Amount)
if err != nil {
return err.Result()
}

return sdk.Result{
Tags: tags,
}
}

// Handle MsgMultiSend.
func handleMsgMultiSend(ctx sdk.Context, k Keeper, msg MsgMultiSend) sdk.Result {
// NOTE: totalIn == totalOut should already have been checked
if !k.GetSendEnabled(ctx) {
return ErrSendDisabled(k.Codespace()).Result()
Expand Down
Loading

0 comments on commit 18eee0a

Please sign in to comment.