Skip to content

Commit 1256df9

Browse files
authored
[DFI-603] Disable rewards txes (#192)
* 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
1 parent be4e53f commit 1256df9

File tree

10 files changed

+139
-13
lines changed

10 files changed

+139
-13
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cosmos_dir=$(swagger_dir)/cosmos-sdk
1414
dnode = ./cmd/dnode
1515
dncli =./cmd/dncli
1616

17-
cosmos_version = backport/v0.39.x
17+
cosmos_version = v0.39.1
1818

1919
all: install
2020
install: go.sum install-dnode install-dncli

app/app.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ var (
100100
orders.ModuleName: {supply.Burner},
101101
gov.ModuleName: {supply.Burner},
102102
}
103+
104+
// Denied messages types.
105+
msgsDeniedList = map[string][]string{
106+
distribution.ModuleName: {
107+
distribution.MsgWithdrawDelegatorReward{}.Type(),
108+
distribution.MsgWithdrawValidatorCommission{}.Type(),
109+
distribution.TypeMsgFundCommunityPool,
110+
distribution.MsgSetWithdrawAddress{}.Type(),
111+
},
112+
}
103113
)
104114

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

193-
bApp := NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
203+
bApp := NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), msgsDeniedList, baseAppOptions...)
194204
bApp.SetAppVersion(version.Version)
195205

196206
keys := sdk.NewKVStoreKeys(

app/baseapp.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ type BaseApp struct { // nolint: maligned
108108

109109
// trace set will return full stack traces for errors in ABCI Log field
110110
trace bool
111+
112+
// list of denied messages, e.g.: moduleName: {msgType},
113+
msgsDeniedList map[string][]string
111114
}
112115

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

122125
app := &BaseApp{
@@ -130,6 +133,7 @@ func NewBaseApp(
130133
txDecoder: txDecoder,
131134
fauxMerkleMode: false,
132135
trace: false,
136+
msgsDeniedList: msgsDeniedList,
133137
}
134138
for _, option := range options {
135139
option(app)
@@ -676,6 +680,16 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*s
676680
}
677681

678682
msgRoute := msg.Route()
683+
684+
// Check if message denied.
685+
if types, hasFound := app.msgsDeniedList[msgRoute]; hasFound {
686+
for _, _type := range types {
687+
if _type == msg.Type() {
688+
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "message route denied: %s/%s; message index: %d", msgRoute, msg.Type(), i)
689+
}
690+
}
691+
}
692+
679693
handler := app.router.Route(ctx, msgRoute)
680694
if handler == nil {
681695
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized message route: %s; message index: %d", msgRoute, i)

app/cli_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ func TestCurrencies_CLI(t *testing.T) {
230230
}
231231
}
232232

233+
// Check that distribution commands in CLI disabled.
234+
func TestDisableRewards_CLI(t *testing.T) {
235+
t.Parallel()
236+
237+
ct := cliTester.New(t, false)
238+
defer ct.Close()
239+
240+
code, stdOut, _ := ct.TxDistributionWithoutParams().Send()
241+
242+
require.Equal(t, 0, code)
243+
require.NotContains(t, string(stdOut), "distribution")
244+
}
245+
233246
func TestOracle_CLI(t *testing.T) {
234247
t.Parallel()
235248

app/common_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,11 @@ func NewTestDnAppDVM(t *testing.T, logOpts ...log.Option) (*DnServiceApp, string
269269
}
270270

271271
// GetGenesis builds genesis state for dnode app.
272-
func GetGenesis(app *DnServiceApp, chainID, monikerID string, accs []*auth.BaseAccount) ([]byte, error) {
272+
func GetGenesis(app *DnServiceApp, chainID, monikerID string, nodeAccPrivKey secp256k1.PrivKeySecp256k1, accs []*auth.BaseAccount) ([]byte, error) {
273273
// generate node validator account
274274
var nodeAcc *auth.BaseAccount
275275
var nodeAccPubKey crypto.PubKey
276-
var nodeAccPrivKey secp256k1.PrivKeySecp256k1
277276
{
278-
nodeAccPrivKey = secp256k1.GenPrivKey()
279277
nodeAccPubKey = nodeAccPrivKey.PubKey()
280278

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

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

462461
SetGenesis(app, genesisStateBz)
462+
463+
return nodeAccPrivKey
463464
}
464465

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

473474
app.cdc.MustUnmarshalJSON(stateBytes, &genesisState)

app/unit_distribution_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// +build unit
2+
3+
package app
4+
5+
import (
6+
"testing"
7+
8+
sdk "github.com/cosmos/cosmos-sdk/types"
9+
"github.com/cosmos/cosmos-sdk/types/errors"
10+
"github.com/cosmos/cosmos-sdk/x/distribution"
11+
"github.com/tendermint/tendermint/crypto/secp256k1"
12+
13+
"github.com/dfinance/dnode/cmd/config"
14+
)
15+
16+
// Check disabled distribution transactions.
17+
func TestDistribution_MessagesNotWorking(t *testing.T) {
18+
t.Parallel()
19+
20+
app, appStop := NewTestDnAppMockVM()
21+
defer appStop()
22+
23+
genValidators, _, _, _ := CreateGenAccounts(9, GenDefCoins(t))
24+
nodePrivKey := secp256k1.PrivKeySecp256k1(CheckSetGenesisMockVM(t, app, genValidators))
25+
nodePubKey := nodePrivKey.PubKey()
26+
nodeAddress := sdk.AccAddress(nodePubKey.Address())
27+
valAddress := sdk.ValAddress(nodePubKey.Address())
28+
29+
// check withdraw rewards tx.
30+
{
31+
senderAcc, senderPrivKey := GetAccountCheckTx(app, nodeAddress), nodePrivKey
32+
// need delegator and validator address.
33+
withdrawRewardsMsg := distribution.NewMsgWithdrawDelegatorReward(nodeAddress, valAddress)
34+
tx := GenTx([]sdk.Msg{withdrawRewardsMsg}, []uint64{senderAcc.GetAccountNumber()}, []uint64{senderAcc.GetSequence()}, senderPrivKey)
35+
CheckDeliverSpecificErrorTx(t, app, tx, errors.ErrUnknownRequest)
36+
}
37+
38+
// check withdraw validator comission tx.
39+
{
40+
senderAcc, senderPrivKey := GetAccountCheckTx(app, nodeAddress), nodePrivKey
41+
withdrawComissionMsg := distribution.NewMsgWithdrawValidatorCommission(valAddress)
42+
tx := GenTx([]sdk.Msg{withdrawComissionMsg}, []uint64{senderAcc.GetAccountNumber()}, []uint64{senderAcc.GetSequence()}, senderPrivKey)
43+
CheckDeliverSpecificErrorTx(t, app, tx, errors.ErrUnknownRequest)
44+
}
45+
46+
// check fund community pool.
47+
{
48+
senderAcc, senderPrivKey := GetAccountCheckTx(app, nodeAddress), nodePrivKey
49+
withdrawComissionMsg := distribution.MsgFundCommunityPool(sdk.NewCoins(sdk.NewCoin(config.MainDenom, sdk.NewInt(1))), nodeAddress)
50+
tx := GenTx([]sdk.Msg{withdrawComissionMsg}, []uint64{senderAcc.GetAccountNumber()}, []uint64{senderAcc.GetSequence()}, senderPrivKey)
51+
CheckDeliverSpecificErrorTx(t, app, tx, errors.ErrUnknownRequest)
52+
}
53+
54+
// check set withdraw address.
55+
{
56+
senderAcc, senderPrivKey := GetAccountCheckTx(app, nodeAddress), nodePrivKey
57+
withdrawAddress := distribution.NewMsgSetWithdrawAddress(nodeAddress, sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()))
58+
tx := GenTx([]sdk.Msg{withdrawAddress}, []uint64{senderAcc.GetAccountNumber()}, []uint64{senderAcc.GetSequence()}, senderPrivKey)
59+
CheckDeliverSpecificErrorTx(t, app, tx, errors.ErrUnknownRequest)
60+
}
61+
}

cmd/dncli/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
1717
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
1818
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
19+
"github.com/cosmos/cosmos-sdk/x/distribution"
1920
"github.com/spf13/cobra"
2021
"github.com/spf13/viper"
2122
"github.com/tendermint/go-amino"
@@ -33,6 +34,11 @@ const (
3334
DefaultGas = 500000
3435
)
3536

37+
var (
38+
// Denied tx commands.
39+
txCommandsDenied = []string{distribution.ModuleName}
40+
)
41+
3642
// Entry function for DN CLI.
3743
func main() {
3844
config := sdk.GetConfig()
@@ -144,6 +150,13 @@ func SetDefaultFeeForTxCmd(cmd *cobra.Command) {
144150
}
145151

146152
for _, child := range cmd.Commands() {
153+
for _, deniedCmd := range txCommandsDenied {
154+
if deniedCmd == child.Use {
155+
cmd.RemoveCommand(child)
156+
continue
157+
}
158+
}
159+
147160
SetDefaultFeeForTxCmd(child)
148161
}
149162
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
github.com/gorilla/handlers v1.4.2
2424
github.com/gorilla/mux v1.7.4
2525
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
26-
github.com/mailru/easyjson v0.7.2 // indirect
26+
github.com/mailru/easyjson v0.7.3 // indirect
2727
github.com/morikuni/aec v1.0.0 // indirect
2828
github.com/olekukonko/tablewriter v0.0.4
2929
github.com/pelletier/go-toml v1.6.0
@@ -40,8 +40,8 @@ require (
4040
github.com/tendermint/tendermint v0.33.6
4141
github.com/tendermint/tm-db v0.5.1
4242
github.com/urfave/cli/v2 v2.2.0 // indirect
43-
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
44-
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d // indirect
43+
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc // indirect
44+
golang.org/x/tools v0.0.0-20200818005847-188abfa75333 // indirect
4545
google.golang.org/grpc v1.30.0
4646
google.golang.org/protobuf v1.24.0 // indirect
4747
k8s.io/apimachinery v0.18.6 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN
469469
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
470470
github.com/mailru/easyjson v0.7.2 h1:V9ecaZWDYm7v9uJ15RZD6DajMu5sE0hdep0aoDwT9g4=
471471
github.com/mailru/easyjson v0.7.2/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
472+
github.com/mailru/easyjson v0.7.3 h1:M6wcO9gFHCIPynXGu4iA+NMs//FCgFUWR2jxqV3/+Xk=
473+
github.com/mailru/easyjson v0.7.3/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
472474
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
473475
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
474476
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
@@ -855,6 +857,8 @@ golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/
855857
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
856858
golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU=
857859
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
860+
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc h1:zK/HqS5bZxDptfPJNq8v7vJfXtkU7r9TLIoSr1bXaP4=
861+
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
858862
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
859863
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
860864
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -937,6 +941,8 @@ golang.org/x/tools v0.0.0-20200729041821-df70183b1872 h1:/U95VAvB4ZsR91rpZX2MwiK
937941
golang.org/x/tools v0.0.0-20200729041821-df70183b1872/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
938942
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d h1:szSOL78iTCl0LF1AMjhSWJj8tIM0KixlUUnBtYXsmd8=
939943
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
944+
golang.org/x/tools v0.0.0-20200818005847-188abfa75333 h1:a6ryybeZHQf5qnBc6IwRfVnI/75UmdtJo71f0//8Dqo=
945+
golang.org/x/tools v0.0.0-20200818005847-188abfa75333/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
940946
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
941947
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
942948
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=

helpers/tests/clitester/cli_tester_txs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,11 @@ func (ct *CLITester) TxGovVote(fromAddress string, id uint64, option gov.VoteOpt
317317

318318
return r
319319
}
320+
321+
func (ct *CLITester) TxDistributionWithoutParams() *TxRequest {
322+
r := ct.newTxRequest()
323+
r.cmd.AddArg("", "tx")
324+
r.cmd.AddArg("", "distribution")
325+
326+
return r
327+
}

0 commit comments

Comments
 (0)