From 2b685ee41d34f2f4488efa90bc0008e54939b510 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 10 Mar 2021 16:31:51 +0000 Subject: [PATCH 1/5] Revert "ci: revert cleanup action's upstream latest changes (#8834)" (#8840) This reverts commit 7a4903842fe286a9e52da6ad51b37f3f11efd277. --- .github/workflows/release-sims.yml | 2 +- .github/workflows/sims.yml | 2 +- .github/workflows/test.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release-sims.yml b/.github/workflows/release-sims.yml index b9272b176c4d..82e68ff41b07 100644 --- a/.github/workflows/release-sims.yml +++ b/.github/workflows/release-sims.yml @@ -10,7 +10,7 @@ jobs: cleanup-runs: runs-on: ubuntu-latest steps: - - uses: tendermint/workflow-run-cleanup-action@master + - uses: rokroskar/workflow-run-cleanup-action@master env: GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'" diff --git a/.github/workflows/sims.yml b/.github/workflows/sims.yml index c1fd10f0c350..a64e808b3874 100644 --- a/.github/workflows/sims.yml +++ b/.github/workflows/sims.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'" steps: - - uses: tendermint/workflow-run-cleanup-action@master + - uses: rokroskar/workflow-run-cleanup-action@master env: GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 872eaff7c939..fb25c58fe190 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ jobs: cleanup-runs: runs-on: ubuntu-latest steps: - - uses: tendermint/workflow-run-cleanup-action@master + - uses: rokroskar/workflow-run-cleanup-action@master env: GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'" From 55fc465dce737f35f245932152198847f2b96f4f Mon Sep 17 00:00:00 2001 From: Charly Date: Wed, 10 Mar 2021 20:14:12 +0100 Subject: [PATCH 2/5] fixes: permalinks for docs (#8838) * fixed broken links, typos * Update docs/ibc/overview.md Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> * Update docs/intro/sdk-app-architecture.md Co-authored-by: Marko * Update docs/building-modules/simulator.md Co-authored-by: Marko * build(deps): bump JamesIves/github-pages-deploy-action from 4.0.0 to 4.1.0 (#8792) Bumps [JamesIves/github-pages-deploy-action](https://github.com/JamesIves/github-pages-deploy-action) from 4.0.0 to 4.1.0. - [Release notes](https://github.com/JamesIves/github-pages-deploy-action/releases) - [Commits](https://github.com/JamesIves/github-pages-deploy-action/compare/4.0.0...3dbacc7e69578703f91f077118b3475862cb09b8) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * fix multisig account pubkeys migration (#8794) closes: #8776 * Update mergify (#8784) * Update mergify Prep for the v0.42 release series. * retire v0.41, the hub can upgrade to v0.42 smoothly * perf change (#8796) Co-authored-by: Alessio Treglia Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> * Supply by denom Migrations (#8780) * Add back supply proto * Add migration for supply * Fix lint * Update x/bank/spec/01_state.md * Fix test * Proto gen * Update x/bank/spec/01_state.md * Make proto gen Co-authored-by: Jonathan Gimeno * fix make protoc error (#8799) * reduce gas costs by 10x for transient store operations (#8790) * reduce gas costs by 10x for transient store operations * fix TestTransientGasConfig for ReadCostFlat * added changelog entry * fix changelog * fix changelog Co-authored-by: Alessio Treglia * x/gov: fix NormalizeProposalType() return values (#8808) Closes: #8806 * store/cachekv: use typed types/kv.List instead of container/list.List (#8811) Reduces CPU burn by using a typed List to avoid the expensive type assertions from using an interface. This is the only option for now until Go makes generics generally available. The change brings time spent on the time assertion cummulatively to: 580ms down from 6.88s Explanation: The type assertions were showing up heavily in profiles: * Before this commit ```shell Total: 42.18s ROUTINE ======================== github.com/cosmos/cosmos-sdk/store/cachekv.newMemIterator in /Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/store/cachekv/memiterator.go 14.01s 18.87s (flat, cum) 44.74% of Total . . 17: items []*kv.Pair . . 18: ascending bool . . 19:} . . 20: . . 21:func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator { . 620ms 22: itemsInDomain := make([]*kv.Pair, 0, items.Len()) . . 23: . . 24: var entered bool . . 25: 510ms 870ms 26: for e := items.Front(); e != nil; e = e.Next() { 6.85s 6.88s 27: item := e.Value.(*kv.Pair) 5.71s 8.19s 28: if !dbm.IsKeyInDomain(item.Key, start, end) { 120ms 120ms 29: if entered { . . 30: break . . 31: } . . 32: . . 33: continue . . 34: } . . 35: 820ms 980ms 36: itemsInDomain = append(itemsInDomain, item) . . 37: entered = true . . 38: } . . 39: . 1.21s 40: return &memIterator{ . . 41: start: start, . . 42: end: end, . . 43: items: itemsInDomain, . . 44: ascending: ascending, . . 45: } ``` and given that the list only uses that type, it is only right to lift the code from container/list.List, and only modify Element.Value's type. For emphasis, the code is basically just a retrofit of container/list/list.go but with a typing, and we'll keep it as is until perhaps Go1.17 or Go1.18 or when everyone uses Go1.17+ after generics have landed. * After this commit ```shell Total: 45.25s ROUTINE ======================== github.com/cosmos/cosmos-sdk/store/cachekv.newMemIterator in /Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/store/cachekv/memiterator.go 4.84s 6.77s (flat, cum) 14.96% of Total . . 16: items []*kv.Pair . . 17: ascending bool . . 18:} . . 19: . . 20:func newMemIterator(start, end []byte, items *kv.List, ascending bool) *memIterator { . 330ms 21: itemsInDomain := make([]*kv.Pair, 0, items.Len()) . . 22: . . 23: var entered bool . . 24: 60ms 160ms 25: for e := items.Front(); e != nil; e = e.Next() { 580ms 580ms 26: item := e.Value 3.68s 4.78s 27: if !dbm.IsKeyInDomain(item.Key, start, end) { 80ms 80ms 28: if entered { . . 29: break . . 30: } . . 31: . . 32: continue . . 33: } . . 34: 440ms 580ms 35: itemsInDomain = append(itemsInDomain, item) . . 36: entered = true . . 37: } . . 38: . 260ms 39: return &memIterator{ . . 40: start: start, . . 41: end: end, . . 42: items: itemsInDomain, . . 43: ascending: ascending, . . 44: } ``` Fixes #8810 * Move all migration scripts to v043 (#8814) * Move all migration scripts to v043 * Fix permaling * Fix test * Fix test again Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> * permalinks Co-authored-by: chrly Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> Co-authored-by: Jonathan Gimeno Co-authored-by: Marko Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Akhil Kumar P <36399231+akhilkumarpilli@users.noreply.github.com> Co-authored-by: Alessio Treglia Co-authored-by: Marko Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Albert Chon Co-authored-by: Emmanuel T Odeke --- docs/building-modules/simulator.md | 6 +++--- docs/intro/sdk-app-architecture.md | 10 +++++----- docs/intro/why-app-specific.md | 3 ++- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/docs/building-modules/simulator.md b/docs/building-modules/simulator.md index 4a7e9ba9714c..586119a331cc 100644 --- a/docs/building-modules/simulator.md +++ b/docs/building-modules/simulator.md @@ -33,7 +33,7 @@ for the key-value pairs from the stores to be decoded (_i.e_ unmarshalled) to their corresponding types. In particular, it matches the key to a concrete type and then unmarshals the value from the `KVPair` to the type provided. -You can use the example [here](https://github.com/cosmos/cosmos-sdk/blob/master/x/distribution/simulation/decoder.go) from the distribution module to implement your store decoders. +You can use the example [here](https://github.com/cosmos/cosmos-sdk/blob/v0.42.0/x/distribution/simulation/decoder.go) from the distribution module to implement your store decoders. ### Randomized genesis @@ -50,7 +50,7 @@ You can check an example on how to create the randomized genesis [here](https:// The simulator is able to test parameter changes at random. The simulator package from each module must contain a `RandomizedParams` func that will simulate parameter changes of the module throughout the simulations lifespan. -You can see how an example of what is needed to fully test parameter changes [here](https://github.com/cosmos/cosmos-sdk/blob/master/x/staking/simulation/params.go) +You can see how an example of what is needed to fully test parameter changes [here](https://github.com/cosmos/cosmos-sdk/blob/v0.42.0/x/staking/simulation/params.go) ### Random weighted operations @@ -83,7 +83,7 @@ them to be used on the parameters. Now that all the required functions are defined, we need to integrate them into the module pattern within the `module.go`: -+++ https://github.com/cosmos/cosmos-sdk/blob/master/x/distribution/module.go ++++ https://github.com/cosmos/cosmos-sdk/blob/v0.42.0/x/distribution/module.go ## App Simulator manager diff --git a/docs/intro/sdk-app-architecture.md b/docs/intro/sdk-app-architecture.md index 9e61cf4c0902..ccd17118574c 100644 --- a/docs/intro/sdk-app-architecture.md +++ b/docs/intro/sdk-app-architecture.md @@ -58,11 +58,11 @@ Blockchain node | | Consensus | | [Tendermint](https://docs.tendermint.com/v0.34/introduction/what-is-tendermint.html) is an application-agnostic engine that is responsible for handling the *networking* and *consensus* layers of a blockchain. In practice, this means that Tendermint is responsible for propagating and ordering transaction bytes. Tendermint Core relies on an eponymous Byzantine-Fault-Tolerant (BFT) algorithm to reach consensus on the order of transactions. -The Tendermint [consensus algorithm](https://docs.tendermint.com/master/introduction/what-is-tendermint.html#consensus-overview) works with a set of special nodes called *Validators*. Validators are responsible for adding blocks of transactions to the blockchain. At any given block, there is a validator set V. A validator in V is chosen by the algorithm to be the proposer of the next block. This block is considered valid if more than two thirds of V signed a *[prevote](https://docs.tendermint.com/master/spec/consensus/consensus.html#prevote-step-height-h-round-r)* and a *[precommit](https://docs.tendermint.com/master/spec/consensus/consensus.html#precommit-step-height-h-round-r)* on it, and if all the transactions that it contains are valid. The validator set can be changed by rules written in the state-machine. +The Tendermint [consensus algorithm](https://docs.tendermint.com/v0.34/introduction/what-is-tendermint.html#consensus-overview) works with a set of special nodes called *Validators*. Validators are responsible for adding blocks of transactions to the blockchain. At any given block, there is a validator set V. A validator in V is chosen by the algorithm to be the proposer of the next block. This block is considered valid if more than two thirds of V signed a *[prevote](https://docs.tendermint.com/v0.34/spec/consensus/consensus.html#prevote-step-height-h-round-r)* and a *[precommit](https://docs.tendermint.com/v0.34/spec/consensus/consensus.html#precommit-step-height-h-round-r)* on it, and if all the transactions that it contains are valid. The validator set can be changed by rules written in the state-machine. ## ABCI -Tendermint passes transactions to the application through an interface called the [ABCI](https://docs.tendermint.com/master/spec/abci/), which the application must implement. +Tendermint passes transactions to the application through an interface called the [ABCI](https://docs.tendermint.com/v0.34/spec/abci/), which the application must implement. ``` +---------------------+ @@ -86,11 +86,11 @@ Note that **Tendermint only handles transaction bytes**. It has no knowledge of Here are the most important messages of the ABCI: -- `CheckTx`: When a transaction is received by Tendermint Core, it is passed to the application to check if a few basic requirements are met. `CheckTx` is used to protect the mempool of full-nodes against spam transactions. A special handler called the [`AnteHandler`](../basics/gas-fees.md#antehandler) is used to execute a series of validation steps such as checking for sufficient fees and validating the signatures. If the checks are valid, the transaction is added to the [mempool](https://docs.tendermint.com/master/tendermint-core/mempool.html#mempool) and relayed to peer nodes. Note that transactions are not processed (i.e. no modification of the state occurs) with `CheckTx` since they have not been included in a block yet. -- `DeliverTx`: When a [valid block](https://docs.tendermint.com/master/spec/blockchain/blockchain.html#validation) is received by Tendermint Core, each transaction in the block is passed to the application via `DeliverTx` in order to be processed. It is during this stage that the state transitions occur. The `AnteHandler` executes again along with the actual [`Msg` service methods](../building-modules/msg-services.md) for each message in the transaction. +- `CheckTx`: When a transaction is received by Tendermint Core, it is passed to the application to check if a few basic requirements are met. `CheckTx` is used to protect the mempool of full-nodes against spam transactions. A special handler called the [`AnteHandler`](../basics/gas-fees.md#antehandler) is used to execute a series of validation steps such as checking for sufficient fees and validating the signatures. If the checks are valid, the transaction is added to the [mempool](https://docs.tendermint.com/v0.34/tendermint-core/mempool.html#mempool) and relayed to peer nodes. Note that transactions are not processed (i.e. no modification of the state occurs) with `CheckTx` since they have not been included in a block yet. +- `DeliverTx`: When a [valid block](https://docs.tendermint.com/v0.34/spec/blockchain/blockchain.html#validation) is received by Tendermint Core, each transaction in the block is passed to the application via `DeliverTx` in order to be processed. It is during this stage that the state transitions occur. The `AnteHandler` executes again along with the actual [`Msg` service methods](../building-modules/msg-services.md) for each message in the transaction. - `BeginBlock`/`EndBlock`: These messages are executed at the beginning and the end of each block, whether the block contains transaction or not. It is useful to trigger automatic execution of logic. Proceed with caution though, as computationally expensive loops could slow down your blockchain, or even freeze it if the loop is infinite. -Find a more detailed view of the ABCI methods from the [Tendermint docs](https://docs.tendermint.com/master/spec/abci/abci.html#overview). +Find a more detailed view of the ABCI methods from the [Tendermint docs](https://docs.tendermint.com/v0.34/spec/abci/abci.html#overview). Any application built on Tendermint needs to implement the ABCI interface in order to communicate with the underlying local Tendermint engine. Fortunately, you do not have to implement the ABCI interface. The Cosmos SDK provides a boilerplate implementation of it in the form of [baseapp](./sdk-design.md#baseapp). diff --git a/docs/intro/why-app-specific.md b/docs/intro/why-app-specific.md index 99ce398baef6..14f6eea03d7c 100644 --- a/docs/intro/why-app-specific.md +++ b/docs/intro/why-app-specific.md @@ -44,7 +44,8 @@ Application-Specific Blockchains are designed to address these shortcomings. Application-specific blockchains give maximum flexibility to developers: -- In Cosmos blockchains, the state-machine is typically connected to the underlying consensus engine via an interface called the [ABCI](https://docs.tendermint.com/master/spec/abci/). This interface can be wrapped in any programming language, meaning developers can build their state-machine in the programming language of their choice. +- In Cosmos blockchains, the state-machine is typically connected to the underlying consensus engine via an interface called the [ABCI](https://docs.tendermint.com/v0.34/spec/abci/). This interface can be wrapped in any programming language, meaning developers can build their state-machine in the programming language of their choice. + - Developers can choose among multiple frameworks to build their state-machine. The most widely used today is the Cosmos SDK, but others exist (e.g. [Lotion](https://github.com/nomic-io/lotion), [Weave](https://github.com/iov-one/weave), ...). The choice will most of the time be done based on the programming language they want to use (Cosmos SDK and Weave are in Golang, Lotion is in Javascript, ...). - The ABCI also allows developers to swap the consensus engine of their application-specific blockchain. Today, only Tendermint is production-ready, but in the future other consensus engines are expected to emerge. - Even when they settle for a framework and consensus engine, developers still have the freedom to tweak them if they don't perfectly match their requirements in their pristine forms. From 0e3ff45380d8a544ce0d29d90fc376705d7bd00a Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Wed, 10 Mar 2021 15:50:30 -0600 Subject: [PATCH 3/5] Fix typo (#8848) --- x/auth/spec/03_antehandlers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/auth/spec/03_antehandlers.md b/x/auth/spec/03_antehandlers.md index 709cae3789f4..7e0ffdba21e2 100644 --- a/x/auth/spec/03_antehandlers.md +++ b/x/auth/spec/03_antehandlers.md @@ -2,7 +2,7 @@ order: 3 --> -# AnthHandlers +# AnteHandlers ## Handlers From e73cf2b410793070c5cec48014dbac21281185f5 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 10 Mar 2021 17:31:21 -0500 Subject: [PATCH 4/5] Merge pull request from GHSA-mvm6-gfm2-89xj Co-authored-by: Alessio Treglia --- simapp/app.go | 18 +----------------- simapp/app_test.go | 7 +++++-- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 000d5234fd37..77c75c8f9ca3 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -130,11 +130,6 @@ var ( stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking}, govtypes.ModuleName: {authtypes.Burner}, } - - // module accounts that are allowed to receive tokens - allowedReceivingModAcc = map[string]bool{ - distrtypes.ModuleName: true, - } ) var ( @@ -242,7 +237,7 @@ func NewSimApp( appCodec, keys[authtypes.StoreKey], app.GetSubspace(authtypes.ModuleName), authtypes.ProtoBaseAccount, maccPerms, ) app.BankKeeper = bankkeeper.NewBaseKeeper( - appCodec, keys[banktypes.StoreKey], app.AccountKeeper, app.GetSubspace(banktypes.ModuleName), app.BlockedAddrs(), + appCodec, keys[banktypes.StoreKey], app.AccountKeeper, app.GetSubspace(banktypes.ModuleName), app.ModuleAccountAddrs(), ) stakingKeeper := stakingkeeper.NewKeeper( appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName), @@ -444,17 +439,6 @@ func (app *SimApp) ModuleAccountAddrs() map[string]bool { return modAccAddrs } -// BlockedAddrs returns all the app's module account addresses that are not -// allowed to receive external tokens. -func (app *SimApp) BlockedAddrs() map[string]bool { - blockedAddrs := make(map[string]bool) - for acc := range maccPerms { - blockedAddrs[authtypes.NewModuleAddress(acc).String()] = !allowedReceivingModAcc[acc] - } - - return blockedAddrs -} - // LegacyAmino returns SimApp's amino codec. // // NOTE: This is solely to be used for testing purposes as it may be desirable diff --git a/simapp/app_test.go b/simapp/app_test.go index 7e14ab062b79..55963732655f 100644 --- a/simapp/app_test.go +++ b/simapp/app_test.go @@ -38,8 +38,11 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) { app := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) for acc := range maccPerms { - require.Equal(t, !allowedReceivingModAcc[acc], app.BankKeeper.BlockedAddr(app.AccountKeeper.GetModuleAddress(acc)), - "ensure that blocked addresses are properly set in bank keeper") + require.True( + t, + app.BankKeeper.BlockedAddr(app.AccountKeeper.GetModuleAddress(acc)), + "ensure that blocked addresses are properly set in bank keeper", + ) } genesisState := NewDefaultGenesisState(encCfg.Marshaler) From 280ee4f15e46117a5061ab3a374d9b695ed082bf Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Thu, 11 Mar 2021 15:20:32 +0100 Subject: [PATCH 5/5] Fix SendToModuleAccountTest (#8857) --- x/bank/app_test.go | 80 ------------------------------------------ x/bank/handler_test.go | 68 +++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 82 deletions(-) diff --git a/x/bank/app_test.go b/x/bank/app_test.go index 6cb3655cc829..5007b087454a 100644 --- a/x/bank/app_test.go +++ b/x/bank/app_test.go @@ -12,7 +12,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/bank/types" - distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" ) type ( @@ -121,85 +120,6 @@ func TestSendNotEnoughBalance(t *testing.T) { require.Equal(t, res2.GetSequence(), origSeq+1) } -// A module account cannot be the recipient of bank sends unless it has been marked as such -func TestSendToModuleAcc(t *testing.T) { - tests := []struct { - name string - fromBalance sdk.Coins - msg *types.MsgSend - expSimPass bool - expPass bool - expFromBalance sdk.Coins - expToBalance sdk.Coins - }{ - { - name: "Normal module account cannot be the recipient of bank sends", - fromBalance: coins, - msg: types.NewMsgSend(addr1, moduleAccAddr, coins), - expSimPass: false, - expPass: false, - expFromBalance: coins, - expToBalance: sdk.NewCoins(), - }, - { - name: "Allowed module account can be the recipient of bank sends", - fromBalance: coins, - msg: types.NewMsgSend(addr1, authtypes.NewModuleAddress(distrtypes.ModuleName), coins), - expPass: true, - expSimPass: true, - expFromBalance: sdk.NewCoins(), - expToBalance: coins, - }, - } - - for _, test := range tests { - test := test - t.Run(test.name, func(t *testing.T) { - acc := &authtypes.BaseAccount{ - Address: test.msg.FromAddress, - } - - genAccs := []authtypes.GenesisAccount{acc} - app := simapp.SetupWithGenesisAccounts(genAccs) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - - fromAddr, err := sdk.AccAddressFromBech32(test.msg.FromAddress) - require.NoError(t, err) - toAddr, err := sdk.AccAddressFromBech32(test.msg.ToAddress) - require.NoError(t, err) - - require.NoError(t, simapp.FundAccount(app, ctx, fromAddr, test.fromBalance)) - - app.Commit() - - res1 := app.AccountKeeper.GetAccount(ctx, fromAddr) - require.NotNil(t, res1) - require.Equal(t, acc, res1.(*authtypes.BaseAccount)) - - origAccNum := res1.GetAccountNumber() - origSeq := res1.GetSequence() - - header := tmproto.Header{Height: app.LastBlockHeight() + 1} - txGen := simapp.MakeTestEncodingConfig().TxConfig - _, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{test.msg}, "", []uint64{origAccNum}, []uint64{origSeq}, test.expSimPass, test.expPass, priv1) - if test.expPass { - require.NoError(t, err) - } else { - require.Error(t, err) - } - - simapp.CheckBalance(t, app, fromAddr, test.expFromBalance) - simapp.CheckBalance(t, app, toAddr, test.expToBalance) - - res2 := app.AccountKeeper.GetAccount(app.NewContext(true, tmproto.Header{}), addr1) - require.NotNil(t, res2) - - require.Equal(t, res2.GetAccountNumber(), origAccNum) - require.Equal(t, res2.GetSequence(), origSeq+1) - }) - } -} - func TestMsgMultiSendWithAccounts(t *testing.T) { acc := &authtypes.BaseAccount{ Address: addr1.String(), diff --git a/x/bank/handler_test.go b/x/bank/handler_test.go index e16e8590a14e..5fcdf02f1a93 100644 --- a/x/bank/handler_test.go +++ b/x/bank/handler_test.go @@ -1,4 +1,4 @@ -package bank +package bank_test import ( "strings" @@ -7,13 +7,20 @@ import ( "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/bank" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) func TestInvalidMsg(t *testing.T) { - h := NewHandler(nil) + h := bank.NewHandler(nil) res, err := h(sdk.NewContext(nil, tmproto.Header{}, false, nil), testdata.NewTestMsg()) require.Error(t, err) @@ -22,3 +29,60 @@ func TestInvalidMsg(t *testing.T) { _, _, log := sdkerrors.ABCIInfo(err, false) require.True(t, strings.Contains(log, "unrecognized bank message type")) } + +// A module account cannot be the recipient of bank sends unless it has been marked as such +func TestSendToModuleAccount(t *testing.T) { + priv1 := secp256k1.GenPrivKey() + addr1 := sdk.AccAddress(priv1.PubKey().Address()) + moduleAccAddr := authtypes.NewModuleAddress(stakingtypes.BondedPoolName) + coins := sdk.Coins{sdk.NewInt64Coin("foocoin", 10)} + + tests := []struct { + name string + expectedError error + msg *types.MsgSend + }{ + { + name: "not allowed module account", + msg: types.NewMsgSend(addr1, moduleAccAddr, coins), + expectedError: sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", moduleAccAddr), + }, + { + name: "allowed module account", + msg: types.NewMsgSend(addr1, authtypes.NewModuleAddress(stakingtypes.ModuleName), coins), + expectedError: nil, + }, + } + + acc1 := &authtypes.BaseAccount{ + Address: addr1.String(), + } + accs := authtypes.GenesisAccounts{acc1} + balances := []types.Balance{ + { + Address: addr1.String(), + Coins: coins, + }, + } + + app := simapp.SetupWithGenesisAccounts(accs, balances...) + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + + app.BankKeeper = bankkeeper.NewBaseKeeper( + app.AppCodec(), app.GetKey(types.StoreKey), app.AccountKeeper, app.GetSubspace(types.ModuleName), map[string]bool{ + moduleAccAddr.String(): true, + }, + ) + handler := bank.NewHandler(app.BankKeeper) + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + _, err := handler(ctx, tc.msg) + if tc.expectedError != nil { + require.EqualError(t, err, tc.expectedError.Error()) + } else { + require.NoError(t, err) + } + }) + } +}