Merge PR #4384: allow splitting withdrawal txs
* allow splitting withdrawal txs * updating .pending * small cleanup * fixing build issue * Update x/distribution/client/cli/tx.go Co-Authored-By: Alessio Treglia <quadrispro@ubuntu.com> * updating usage text * fixing merge issue * changes based on the review * adding unit tests * Update x/distribution/client/cli/tx.go Co-Authored-By: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * adjustments from review
This commit is contained in:
parent
e53d23390d
commit
68036ec71f
|
@ -0,0 +1 @@
|
|||
#4384- Allow splitting withdrawal transaction in several chunks
|
|
@ -26,6 +26,11 @@ var (
|
|||
flagOnlyFromValidator = "only-from-validator"
|
||||
flagIsValidator = "is-validator"
|
||||
flagComission = "commission"
|
||||
flagMaxMessagesPerTx = "max-msgs"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxMessagesPerTxDefault = 5
|
||||
)
|
||||
|
||||
// GetTxCmd returns the transaction commands for this module
|
||||
|
@ -43,6 +48,38 @@ func GetTxCmd(storeKey string, cdc *amino.Codec) *cobra.Command {
|
|||
return distTxCmd
|
||||
}
|
||||
|
||||
type generateOrBroadcastFunc func(context.CLIContext, authtxb.TxBuilder, []sdk.Msg) error
|
||||
|
||||
func splitAndApply(
|
||||
generateOrBroadcast generateOrBroadcastFunc,
|
||||
cliCtx context.CLIContext,
|
||||
txBldr authtxb.TxBuilder,
|
||||
msgs []sdk.Msg,
|
||||
chunkSize int,
|
||||
) error {
|
||||
|
||||
if chunkSize == 0 {
|
||||
return generateOrBroadcast(cliCtx, txBldr, msgs)
|
||||
}
|
||||
|
||||
// split messages into slices of length chunkSize
|
||||
totalMessages := len(msgs)
|
||||
for i := 0; i < len(msgs); i += chunkSize {
|
||||
|
||||
sliceEnd := i + chunkSize
|
||||
if sliceEnd > totalMessages {
|
||||
sliceEnd = totalMessages
|
||||
}
|
||||
|
||||
msgChunk := msgs[i:sliceEnd]
|
||||
if err := generateOrBroadcast(cliCtx, txBldr, msgChunk); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// command to withdraw rewards
|
||||
func GetCmdWithdrawRewards(cdc *codec.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
|
@ -86,7 +123,7 @@ $ %s tx distr withdraw-rewards cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqh
|
|||
|
||||
// command to withdraw all rewards
|
||||
func GetCmdWithdrawAllRewards(cdc *codec.Codec, queryRoute string) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
cmd := &cobra.Command{
|
||||
Use: "withdraw-all-rewards",
|
||||
Short: "withdraw all delegations rewards for a delegator",
|
||||
Long: strings.TrimSpace(
|
||||
|
@ -112,14 +149,17 @@ $ %s tx distr withdraw-all-rewards --from mykey
|
|||
return err
|
||||
}
|
||||
|
||||
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, msgs)
|
||||
chunkSize := viper.GetInt(flagMaxMessagesPerTx)
|
||||
return splitAndApply(utils.GenerateOrBroadcastMsgs, cliCtx, txBldr, msgs, chunkSize)
|
||||
},
|
||||
}
|
||||
cmd.Flags().Int(flagMaxMessagesPerTx, MaxMessagesPerTxDefault, "Limit the number of messages per tx (0 for unlimited)")
|
||||
return cmd
|
||||
}
|
||||
|
||||
// command to replace a delegator's withdrawal address
|
||||
func GetCmdSetWithdrawAddr(cdc *codec.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
return &cobra.Command{
|
||||
Use: "set-withdraw-addr [withdraw-addr]",
|
||||
Short: "change the default withdraw address for rewards associated with an address",
|
||||
Long: strings.TrimSpace(
|
||||
|
@ -149,7 +189,6 @@ $ %s tx set-withdraw-addr cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p --from m
|
|||
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
// GetCmdSubmitProposal implements the command to submit a community-pool-spend proposal
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client/utils"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tendermint/tendermint/crypto/secp256k1"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func createFakeTxBuilder() authtxb.TxBuilder {
|
||||
cdc := codec.New()
|
||||
return authtxb.NewTxBuilder(
|
||||
utils.GetTxEncoder(cdc),
|
||||
123,
|
||||
9876,
|
||||
0,
|
||||
1.2,
|
||||
false,
|
||||
"test_chain",
|
||||
"hello",
|
||||
sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1))),
|
||||
sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.NewDecWithPrec(10000, sdk.Precision))},
|
||||
)
|
||||
}
|
||||
|
||||
func Test_splitAndCall_NoMessages(t *testing.T) {
|
||||
ctx := context.CLIContext{}
|
||||
txBldr := createFakeTxBuilder()
|
||||
|
||||
err := splitAndApply(nil, ctx, txBldr, nil, 10)
|
||||
assert.NoError(t, err, "")
|
||||
}
|
||||
|
||||
func Test_splitAndCall_Splitting(t *testing.T) {
|
||||
ctx := context.CLIContext{}
|
||||
txBldr := createFakeTxBuilder()
|
||||
|
||||
addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
|
||||
|
||||
// Add five messages
|
||||
msgs := []sdk.Msg{
|
||||
sdk.NewTestMsg(addr),
|
||||
sdk.NewTestMsg(addr),
|
||||
sdk.NewTestMsg(addr),
|
||||
sdk.NewTestMsg(addr),
|
||||
sdk.NewTestMsg(addr),
|
||||
}
|
||||
|
||||
// Keep track of number of calls
|
||||
const chunkSize = 2
|
||||
|
||||
callCount := 0
|
||||
err := splitAndApply(
|
||||
func(ctx context.CLIContext, txBldr authtxb.TxBuilder, msgs []sdk.Msg) error {
|
||||
callCount += 1
|
||||
|
||||
assert.NotNil(t, ctx)
|
||||
assert.NotNil(t, txBldr)
|
||||
assert.NotNil(t, msgs)
|
||||
|
||||
if callCount < 3 {
|
||||
assert.Equal(t, len(msgs), 2)
|
||||
} else {
|
||||
assert.Equal(t, len(msgs), 1)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
ctx, txBldr, msgs, chunkSize)
|
||||
|
||||
assert.NoError(t, err, "")
|
||||
assert.Equal(t, 3, callCount)
|
||||
}
|
Loading…
Reference in New Issue