Skip to content

Commit

Permalink
[DFI-603] Disable rewards txes (#192)
Browse files Browse the repository at this point in the history
* Denied messages list (to disable rewards txs), same for CLI

* cosmos version to v0.39.1 (previous branch missed)

* Test disabled distribution txs

* fixed comment
  • Loading branch information
borispovod authored Aug 24, 2020
1 parent be4e53f commit 1256df9
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cosmos_dir=$(swagger_dir)/cosmos-sdk
dnode = ./cmd/dnode
dncli =./cmd/dncli

cosmos_version = backport/v0.39.x
cosmos_version = v0.39.1

all: install
install: go.sum install-dnode install-dncli
Expand Down
12 changes: 11 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ var (
orders.ModuleName: {supply.Burner},
gov.ModuleName: {supply.Burner},
}

// Denied messages types.
msgsDeniedList = map[string][]string{
distribution.ModuleName: {
distribution.MsgWithdrawDelegatorReward{}.Type(),
distribution.MsgWithdrawValidatorCommission{}.Type(),
distribution.TypeMsgFundCommunityPool,
distribution.MsgSetWithdrawAddress{}.Type(),
},
}
)

// DN Service App implements DN mains logic.
Expand Down Expand Up @@ -190,7 +200,7 @@ func MakeCodec() *codec.Codec {
func NewDnServiceApp(logger log.Logger, db dbm.DB, config *config.VMConfig, invCheckPeriod uint, baseAppOptions ...func(*BaseApp)) *DnServiceApp {
cdc := MakeCodec()

bApp := NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
bApp := NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), msgsDeniedList, baseAppOptions...)
bApp.SetAppVersion(version.Version)

keys := sdk.NewKVStoreKeys(
Expand Down
16 changes: 15 additions & 1 deletion app/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ type BaseApp struct { // nolint: maligned

// trace set will return full stack traces for errors in ABCI Log field
trace bool

// list of denied messages, e.g.: moduleName: {msgType},
msgsDeniedList map[string][]string
}

// NewBaseApp returns a reference to an initialized BaseApp. It accepts a
Expand All @@ -116,7 +119,7 @@ type BaseApp struct { // nolint: maligned
//
// NOTE: The db is used to store the version number for now.
func NewBaseApp(
name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecoder, options ...func(*BaseApp),
name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecoder, msgsDeniedList map[string][]string, options ...func(*BaseApp),
) *BaseApp {

app := &BaseApp{
Expand All @@ -130,6 +133,7 @@ func NewBaseApp(
txDecoder: txDecoder,
fauxMerkleMode: false,
trace: false,
msgsDeniedList: msgsDeniedList,
}
for _, option := range options {
option(app)
Expand Down Expand Up @@ -676,6 +680,16 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*s
}

msgRoute := msg.Route()

// Check if message denied.
if types, hasFound := app.msgsDeniedList[msgRoute]; hasFound {
for _, _type := range types {
if _type == msg.Type() {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "message route denied: %s/%s; message index: %d", msgRoute, msg.Type(), i)
}
}
}

handler := app.router.Route(ctx, msgRoute)
if handler == nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized message route: %s; message index: %d", msgRoute, i)
Expand Down
13 changes: 13 additions & 0 deletions app/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ func TestCurrencies_CLI(t *testing.T) {
}
}

// Check that distribution commands in CLI disabled.
func TestDisableRewards_CLI(t *testing.T) {
t.Parallel()

ct := cliTester.New(t, false)
defer ct.Close()

code, stdOut, _ := ct.TxDistributionWithoutParams().Send()

require.Equal(t, 0, code)
require.NotContains(t, string(stdOut), "distribution")
}

func TestOracle_CLI(t *testing.T) {
t.Parallel()

Expand Down
15 changes: 8 additions & 7 deletions app/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,11 @@ func NewTestDnAppDVM(t *testing.T, logOpts ...log.Option) (*DnServiceApp, string
}

// GetGenesis builds genesis state for dnode app.
func GetGenesis(app *DnServiceApp, chainID, monikerID string, accs []*auth.BaseAccount) ([]byte, error) {
func GetGenesis(app *DnServiceApp, chainID, monikerID string, nodeAccPrivKey secp256k1.PrivKeySecp256k1, accs []*auth.BaseAccount) ([]byte, error) {
// generate node validator account
var nodeAcc *auth.BaseAccount
var nodeAccPubKey crypto.PubKey
var nodeAccPrivKey secp256k1.PrivKeySecp256k1
{
nodeAccPrivKey = secp256k1.GenPrivKey()
nodeAccPubKey = nodeAccPrivKey.PubKey()

accAddr := sdk.AccAddress(nodeAccPubKey.Address())
Expand Down Expand Up @@ -454,20 +452,23 @@ func SetGenesis(app *DnServiceApp, genesisStateBz []byte) {
app.Commit()
}

// Sets genesis to DnServiceApp with default test genesis.
func CheckSetGenesisMockVM(t *testing.T, app *DnServiceApp, accs []*auth.BaseAccount) {
genesisStateBz, err := GetGenesis(app, chainID, "test-moniker", accs)
// Sets genesis to DnServiceApp with default test genesis, returns validator node private key.
func CheckSetGenesisMockVM(t *testing.T, app *DnServiceApp, accs []*auth.BaseAccount) secp256k1.PrivKeySecp256k1 {
nodeAccPrivKey := secp256k1.GenPrivKey()
genesisStateBz, err := GetGenesis(app, chainID, "test-moniker", nodeAccPrivKey, accs)
require.NoError(t, err, "GetGenesis")

SetGenesis(app, genesisStateBz)

return nodeAccPrivKey
}

// Sets genesis to DnServiceApp with default test genesis and VM genesis writeSets.
func CheckSetGenesisDVM(t *testing.T, app *DnServiceApp, accs []*auth.BaseAccount) {
// get genesis bytes
var genesisState GenesisState
{
stateBytes, err := GetGenesis(app, "", "testMoniker", accs)
stateBytes, err := GetGenesis(app, "", "testMoniker", secp256k1.GenPrivKey(), accs)
require.NoError(t, err, "GetGenesis")

app.cdc.MustUnmarshalJSON(stateBytes, &genesisState)
Expand Down
61 changes: 61 additions & 0 deletions app/unit_distribution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// +build unit

package app

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/tendermint/tendermint/crypto/secp256k1"

"github.com/dfinance/dnode/cmd/config"
)

// Check disabled distribution transactions.
func TestDistribution_MessagesNotWorking(t *testing.T) {
t.Parallel()

app, appStop := NewTestDnAppMockVM()
defer appStop()

genValidators, _, _, _ := CreateGenAccounts(9, GenDefCoins(t))
nodePrivKey := secp256k1.PrivKeySecp256k1(CheckSetGenesisMockVM(t, app, genValidators))
nodePubKey := nodePrivKey.PubKey()
nodeAddress := sdk.AccAddress(nodePubKey.Address())
valAddress := sdk.ValAddress(nodePubKey.Address())

// check withdraw rewards tx.
{
senderAcc, senderPrivKey := GetAccountCheckTx(app, nodeAddress), nodePrivKey
// need delegator and validator address.
withdrawRewardsMsg := distribution.NewMsgWithdrawDelegatorReward(nodeAddress, valAddress)
tx := GenTx([]sdk.Msg{withdrawRewardsMsg}, []uint64{senderAcc.GetAccountNumber()}, []uint64{senderAcc.GetSequence()}, senderPrivKey)
CheckDeliverSpecificErrorTx(t, app, tx, errors.ErrUnknownRequest)
}

// check withdraw validator comission tx.
{
senderAcc, senderPrivKey := GetAccountCheckTx(app, nodeAddress), nodePrivKey
withdrawComissionMsg := distribution.NewMsgWithdrawValidatorCommission(valAddress)
tx := GenTx([]sdk.Msg{withdrawComissionMsg}, []uint64{senderAcc.GetAccountNumber()}, []uint64{senderAcc.GetSequence()}, senderPrivKey)
CheckDeliverSpecificErrorTx(t, app, tx, errors.ErrUnknownRequest)
}

// check fund community pool.
{
senderAcc, senderPrivKey := GetAccountCheckTx(app, nodeAddress), nodePrivKey
withdrawComissionMsg := distribution.MsgFundCommunityPool(sdk.NewCoins(sdk.NewCoin(config.MainDenom, sdk.NewInt(1))), nodeAddress)
tx := GenTx([]sdk.Msg{withdrawComissionMsg}, []uint64{senderAcc.GetAccountNumber()}, []uint64{senderAcc.GetSequence()}, senderPrivKey)
CheckDeliverSpecificErrorTx(t, app, tx, errors.ErrUnknownRequest)
}

// check set withdraw address.
{
senderAcc, senderPrivKey := GetAccountCheckTx(app, nodeAddress), nodePrivKey
withdrawAddress := distribution.NewMsgSetWithdrawAddress(nodeAddress, sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()))
tx := GenTx([]sdk.Msg{withdrawAddress}, []uint64{senderAcc.GetAccountNumber()}, []uint64{senderAcc.GetSequence()}, senderPrivKey)
CheckDeliverSpecificErrorTx(t, app, tx, errors.ErrUnknownRequest)
}
}
13 changes: 13 additions & 0 deletions cmd/dncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
"github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/go-amino"
Expand All @@ -33,6 +34,11 @@ const (
DefaultGas = 500000
)

var (
// Denied tx commands.
txCommandsDenied = []string{distribution.ModuleName}
)

// Entry function for DN CLI.
func main() {
config := sdk.GetConfig()
Expand Down Expand Up @@ -144,6 +150,13 @@ func SetDefaultFeeForTxCmd(cmd *cobra.Command) {
}

for _, child := range cmd.Commands() {
for _, deniedCmd := range txCommandsDenied {
if deniedCmd == child.Use {
cmd.RemoveCommand(child)
continue
}
}

SetDefaultFeeForTxCmd(child)
}
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.4
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/mailru/easyjson v0.7.2 // indirect
github.com/mailru/easyjson v0.7.3 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/olekukonko/tablewriter v0.0.4
github.com/pelletier/go-toml v1.6.0
Expand All @@ -40,8 +40,8 @@ require (
github.com/tendermint/tendermint v0.33.6
github.com/tendermint/tm-db v0.5.1
github.com/urfave/cli/v2 v2.2.0 // indirect
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d // indirect
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc // indirect
golang.org/x/tools v0.0.0-20200818005847-188abfa75333 // indirect
google.golang.org/grpc v1.30.0
google.golang.org/protobuf v1.24.0 // indirect
k8s.io/apimachinery v0.18.6 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.2 h1:V9ecaZWDYm7v9uJ15RZD6DajMu5sE0hdep0aoDwT9g4=
github.com/mailru/easyjson v0.7.2/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mailru/easyjson v0.7.3 h1:M6wcO9gFHCIPynXGu4iA+NMs//FCgFUWR2jxqV3/+Xk=
github.com/mailru/easyjson v0.7.3/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
Expand Down Expand Up @@ -855,6 +857,8 @@ golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU=
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc h1:zK/HqS5bZxDptfPJNq8v7vJfXtkU7r9TLIoSr1bXaP4=
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -937,6 +941,8 @@ golang.org/x/tools v0.0.0-20200729041821-df70183b1872 h1:/U95VAvB4ZsR91rpZX2MwiK
golang.org/x/tools v0.0.0-20200729041821-df70183b1872/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d h1:szSOL78iTCl0LF1AMjhSWJj8tIM0KixlUUnBtYXsmd8=
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200818005847-188abfa75333 h1:a6ryybeZHQf5qnBc6IwRfVnI/75UmdtJo71f0//8Dqo=
golang.org/x/tools v0.0.0-20200818005847-188abfa75333/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down
8 changes: 8 additions & 0 deletions helpers/tests/clitester/cli_tester_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,11 @@ func (ct *CLITester) TxGovVote(fromAddress string, id uint64, option gov.VoteOpt

return r
}

func (ct *CLITester) TxDistributionWithoutParams() *TxRequest {
r := ct.newTxRequest()
r.cmd.AddArg("", "tx")
r.cmd.AddArg("", "distribution")

return r
}

0 comments on commit 1256df9

Please sign in to comment.