Skip to content

Commit

Permalink
Merge branch 'main' into lucas/add-cons-address-to-validator
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslopezf authored May 21, 2024
2 parents 625865e + a2dd2a0 commit c09ed0a
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 50 deletions.
4 changes: 2 additions & 2 deletions docs/learn/advanced/00-baseapp.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ The response contains:
https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-alpha.0/x/auth/ante/basic.go#L102
```

* `Events ([]cmn.KVPair)`: Key-Value tags for filtering and indexing transactions (eg. by account). See [`event`s](./08-events.md) for more.
* `Events ([]cmn.KVPair)`: Key-Value tags for filtering and indexing transactions (eg. by account). See [`events`](./08-events.md) for more.
* `Codespace (string)`: Namespace for the Code.

#### RecheckTx
Expand Down Expand Up @@ -495,7 +495,7 @@ Each transaction returns a response to the underlying consensus engine of type [
* `Info (string):` Additional information. May be non-deterministic.
* `GasWanted (int64)`: Amount of gas requested for transaction. It is provided by users when they generate the transaction.
* `GasUsed (int64)`: Amount of gas consumed by transaction. During transaction execution, this value is computed by multiplying the standard cost of a transaction byte by the size of the raw transaction, and by adding gas each time a read/write to the store occurs.
* `Events ([]cmn.KVPair)`: Key-Value tags for filtering and indexing transactions (eg. by account). See [`event`s](./08-events.md) for more.
* `Events ([]cmn.KVPair)`: Key-Value tags for filtering and indexing transactions (eg. by account). See [`events`](./08-events.md) for more.
* `Codespace (string)`: Namespace for the Code.

#### EndBlock
Expand Down
2 changes: 1 addition & 1 deletion docs/learn/advanced/08-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_position: 1
# Events

:::note Synopsis
`Event`s are objects that contain information about the execution of the application. They are mainly used by service providers like block explorers and wallet to track the execution of various messages and index transactions.
`Events` are objects that contain information about the execution of the application. They are mainly used by service providers like block explorers and wallet to track the execution of various messages and index transactions.
:::

:::note Pre-requisite Readings
Expand Down
6 changes: 6 additions & 0 deletions store/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Ref: https://keepachangelog.com/en/1.0.0/

# Changelog

## [Unreleased]

### Bug Fixes

* (store) [#20425](https://github.com/cosmos/cosmos-sdk/pull/20425) Fix nil pointer panic when query historical state where a new store don't exist.

## v1.1.0 (March 20, 2024)

### Improvements
Expand Down
2 changes: 1 addition & 1 deletion store/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type (
func NewCommitKVStoreCache(store types.CommitKVStore, size uint) *CommitKVStoreCache {
cache, err := lru.NewARC(int(size))
if err != nil {
panic(fmt.Errorf("failed to create KVStore cache: %s", err))
panic(fmt.Errorf("failed to create KVStore cache: %w", err))
}

return &CommitKVStoreCache{
Expand Down
10 changes: 7 additions & 3 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"
"io"
"math"
"math"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -607,6 +607,10 @@ func (rs *Store) CacheMultiStoreWithVersion(version int64) (types.CacheMultiStor
if storeInfos[key.Name()] {
return nil, err
}

// If the store donesn't exist at this version, create a dummy one to prevent
// nil pointer panic in newer query APIs.
cacheStore = dbadapter.Store{DB: dbm.NewMemDB()}
}

default:
Expand Down Expand Up @@ -830,7 +834,7 @@ func (rs *Store) Snapshot(height uint64, protoWriter protoio.Writer) error {
keys := keysFromStoreKeyMap(rs.stores)
for _, key := range keys {
switch store := rs.GetCommitKVStore(key).(type) {
case *iavl.Store:
case *iavl.Store:
stores = append(stores, namedStore{name: key.Name(), Store: store})
case *transient.Store, *mem.Store:
// Non-persisted stores shouldn't be snapshotted
Expand All @@ -850,7 +854,7 @@ func (rs *Store) Snapshot(height uint64, protoWriter protoio.Writer) error {
// are demarcated by new SnapshotStore items.
for _, store := range stores {
rs.logger.Debug("starting snapshot", "store", store.name, "height", height)
exporter, err := store.Export(int64(height))
exporter, err := store.Export(int64(height))
if err != nil {
rs.logger.Error("snapshot failed; exporter error", "store", store.name, "err", err)
return err
Expand Down
9 changes: 6 additions & 3 deletions store/rootmulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,19 @@ func TestCacheMultiStoreWithVersion(t *testing.T) {
require.Equal(t, kvStore.Get(k), v)

// add new module stores (store4 and store5) to multi stores and commit
ms.MountStoreWithDB(types.NewKVStoreKey("store4"), types.StoreTypeIAVL, nil)
ms.MountStoreWithDB(types.NewKVStoreKey("store5"), types.StoreTypeIAVL, nil)
key4, key5 := types.NewKVStoreKey("store4"), types.NewKVStoreKey("store5")
ms.MountStoreWithDB(key4, types.StoreTypeIAVL, nil)
ms.MountStoreWithDB(key5, types.StoreTypeIAVL, nil)
err = ms.LoadLatestVersionAndUpgrade(&types.StoreUpgrades{Added: []string{"store4", "store5"}})
require.NoError(t, err)
ms.Commit()

// cache multistore of version before adding store4 should works
_, err = ms.CacheMultiStoreWithVersion(1)
cms2, err := ms.CacheMultiStoreWithVersion(1)
require.NoError(t, err)

require.Empty(t, cms2.GetKVStore(key4).Get([]byte("key")))

// require we cannot commit (write) to a cache-versioned multi-store
require.Panics(t, func() {
kvStore.Set(k, []byte("newValue"))
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/accounts/base_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@ func bechify(t *testing.T, app *simapp.SimApp, addr []byte) string {

func fundAccount(t *testing.T, app *simapp.SimApp, ctx sdk.Context, addr sdk.AccAddress, amt string) {
require.NoError(t, testutil.FundAccount(ctx, app.BankKeeper, addr, coins(t, amt)))

}
3 changes: 2 additions & 1 deletion testutil/testdata/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package testdata
import (
"context"
"fmt"
"github.com/cosmos/gogoproto/types/any/test"
"testing"

"github.com/cosmos/gogoproto/types/any/test"

"github.com/cosmos/gogoproto/proto"
"google.golang.org/grpc"
"gotest.tools/v3/assert"
Expand Down
17 changes: 16 additions & 1 deletion x/accounts/defaults/multisig/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ func (a *Account) Init(ctx context.Context, msg *v1.MsgInit) (*v1.MsgInitRespons
return nil, err
}

totalWeight += msg.Members[i].Weight
totalWeight, err = safeAdd(totalWeight, msg.Members[i].Weight)
if err != nil {
return nil, err
}
}

if err := validateConfig(*msg.Config, totalWeight); err != nil {
Expand Down Expand Up @@ -279,6 +282,7 @@ func (a Account) ExecuteProposal(ctx context.Context, msg *v1.MsgExecuteProposal
}

totalWeight := yesVotes + noVotes + abstainVotes

var (
rejectErr error
execErr error
Expand Down Expand Up @@ -387,3 +391,14 @@ func (a *Account) RegisterQueryHandlers(builder *accountstd.QueryBuilder) {
accountstd.RegisterQueryHandler(builder, a.QueryProposal)
accountstd.RegisterQueryHandler(builder, a.QueryConfig)
}

func safeAdd(nums ...uint64) (uint64, error) {
var sum uint64
for _, num := range nums {
if sum+num < sum {
return 0, errors.New("overflow")
}
sum += num
}
return sum, nil
}
54 changes: 54 additions & 0 deletions x/accounts/defaults/multisig/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package multisig

import (
"context"
"math"
"testing"
"time"

Expand Down Expand Up @@ -312,6 +313,29 @@ func TestUpdateConfig(t *testing.T) {
},
},
},
{
"change members, invalid weights",
&v1.MsgUpdateConfig{
UpdateMembers: []*v1.Member{
{
Address: "addr1",
Weight: math.MaxUint64,
},
{
Address: "addr2",
Weight: 1,
},
},
Config: &v1.Config{
Threshold: 666,
Quorum: 400,
VotingPeriod: 60,
},
},
"overflow",
nil,
nil,
},
}

for _, tc := range testcases {
Expand Down Expand Up @@ -658,3 +682,33 @@ func TestProposalPassing(t *testing.T) {

require.Equal(t, expectedMembers, cfg.Members)
}

func TestWeightOverflow(t *testing.T) {
ctx, ss := newMockContext(t)
acc := setup(t, ctx, ss, nil)

startAcc := &v1.MsgInit{
Config: &v1.Config{
Threshold: 2640,
Quorum: 2000,
VotingPeriod: 60,
},
Members: []*v1.Member{
{
Address: "addr1",
Weight: math.MaxUint64,
},
},
}

_, err := acc.Init(ctx, startAcc)
require.NoError(t, err)

// add another member with weight 1 to trigger an overflow
startAcc.Members = append(startAcc.Members, &v1.Member{
Address: "addr2",
Weight: 1,
})
_, err = acc.Init(ctx, startAcc)
require.ErrorContains(t, err, "overflow")
}
6 changes: 5 additions & 1 deletion x/accounts/defaults/multisig/update_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func (a Account) UpdateConfig(ctx context.Context, msg *v1.MsgUpdateConfig) (*v1
// get the weight from the stored members
totalWeight := uint64(0)
err := a.Members.Walk(ctx, nil, func(_ []byte, value uint64) (stop bool, err error) {
totalWeight += value
var adderr error
totalWeight, adderr = safeAdd(totalWeight, value)
if adderr != nil {
return true, adderr
}
return false, nil
})
if err != nil {
Expand Down
21 changes: 11 additions & 10 deletions x/auth/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// TxFeeChecker check if the provided fee is enough and returns the effective fee and tx priority,
// the effective fee should be deducted later, and the priority should be returned in abci response.
// TxFeeChecker checks if the provided fee is enough and returns the effective fee and tx priority.
// The effective fee should be deducted later, and the priority should be returned in the ABCI response.
type TxFeeChecker func(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error)

// DeductFeeDecorator deducts fees from the fee payer. The fee payer is the fee granter (if specified) or first signer of the tx.
// If the fee payer does not have the funds to pay for the fees, return an InsufficientFunds error.
// Call next AnteHandler if fees successfully deducted.
// CONTRACT: Tx must implement FeeTx interface to use DeductFeeDecorator
// Call next AnteHandler if fees are successfully deducted.
// CONTRACT: The Tx must implement the FeeTx interface to use DeductFeeDecorator.
type DeductFeeDecorator struct {
accountKeeper AccountKeeper
bankKeeper types.BankKeeper
Expand All @@ -43,7 +43,7 @@ func NewDeductFeeDecorator(ak AccountKeeper, bk types.BankKeeper, fk FeegrantKee
func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, next sdk.AnteHandler) (sdk.Context, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must implement the FeeTx interface")
}

txService := dfd.accountKeeper.GetEnvironment().TransactionService
Expand Down Expand Up @@ -76,19 +76,20 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, nex
func (dfd DeductFeeDecorator) checkDeductFee(ctx sdk.Context, sdkTx sdk.Tx, fee sdk.Coins) error {
feeTx, ok := sdkTx.(sdk.FeeTx)
if !ok {
return errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
return errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must implement the FeeTx interface")
}

if addr := dfd.accountKeeper.GetModuleAddress(types.FeeCollectorName); addr == nil {
addr := dfd.accountKeeper.GetModuleAddress(types.FeeCollectorName)
if len(addr) == 0 {
return fmt.Errorf("fee collector module account (%s) has not been set", types.FeeCollectorName)
}

feePayer := feeTx.FeePayer()
feeGranter := feeTx.FeeGranter()
deductFeesFrom := feePayer

// if feegranter set deduct fee from feegranter account.
// this works with only when feegrant enabled.
// if feegranter set, deduct fee from feegranter account.
// this works only when feegrant is enabled.
if feeGranter != nil {
feeGranterAddr := sdk.AccAddress(feeGranter)

Expand Down Expand Up @@ -132,7 +133,7 @@ func DeductFees(bankKeeper types.BankKeeper, ctx sdk.Context, acc []byte, fees s

err := bankKeeper.SendCoinsFromAccountToModule(ctx, sdk.AccAddress(acc), types.FeeCollectorName, fees)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
return fmt.Errorf("failed to deduct fees: %w", err)
}

return nil
Expand Down
1 change: 0 additions & 1 deletion x/bank/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Consensus Breaking Changes

* [#19188](https://github.com/cosmos/cosmos-sdk/pull/19188) Remove creation of `BaseAccount` when sending a message to an account that does not exist
* [#20343](https://github.com/cosmos/cosmos-sdk/pull/20343) Add a check in send moduleaccount to account to prevent module accounts from sending disabled tokens to accounts
7 changes: 0 additions & 7 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,6 @@ func (k BaseKeeper) SendCoinsFromModuleToAccount(
return errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", senderModule)
}

for _, coin := range amt {
sendEnabled, found := k.getSendEnabled(ctx, coin.Denom)
if found && !sendEnabled {
return fmt.Errorf("denom: %s, is prohibited from being sent at this time", coin.Denom)
}
}

if k.BlockedAddr(recipientAddr) {
return errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", recipientAddr)
}
Expand Down
18 changes: 0 additions & 18 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,24 +389,6 @@ func (suite *KeeperTestSuite) TestSendCoinsFromModuleToAccount_Blocklist() {
))
}

func (suite *KeeperTestSuite) TestSendCoinsFromModuleToAccount_CoinSendDisabled() {
ctx := suite.ctx
require := suite.Require()
keeper := suite.bankKeeper

suite.mockMintCoins(mintAcc)
require.NoError(keeper.MintCoins(ctx, banktypes.MintModuleName, initCoins))

keeper.SetSendEnabled(ctx, sdk.DefaultBondDenom, false)

suite.authKeeper.EXPECT().GetModuleAddress(mintAcc.Name).Return(mintAcc.GetAddress())
err := keeper.SendCoinsFromModuleToAccount(
ctx, banktypes.MintModuleName, accAddrs[2], initCoins,
)
require.Contains(err.Error(), "stake, is prohibited from being sent at this time")
keeper.SetSendEnabled(ctx, sdk.DefaultBondDenom, true)
}

func (suite *KeeperTestSuite) TestSupply_DelegateUndelegateCoins() {
ctx := suite.ctx
require := suite.Require()
Expand Down

0 comments on commit c09ed0a

Please sign in to comment.