Skip to content

Commit

Permalink
Merge branch 'master' into hulatown/adr-043-nft
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Jun 3, 2021
2 parents e8e6980 + 90edeb6 commit db8bd84
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 109 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

* [\#9428](https://github.com/cosmos/cosmos-sdk/pull/9428) Optimize bank InitGenesis. Removed bank keeper's `k.setBalances` and `k.clearBalances`. Added `k.initBalances`.
* [\#9231](https://github.com/cosmos/cosmos-sdk/pull/9231) Remove redundant staking errors.
* [\#9205](https://github.com/cosmos/cosmos-sdk/pull/9205) Improve readability in `abci` handleQueryP2P
* [\#9235](https://github.com/cosmos/cosmos-sdk/pull/9235) CreateMembershipProof/CreateNonMembershipProof now returns an error
Expand Down Expand Up @@ -127,7 +128,9 @@ if input key is empty, or input data contains empty key.

### Improvements

* (baseapp, types) [#\9390](https://github.com/cosmos/cosmos-sdk/pull/9390) Add current block header hash to `Context`
* (store) [\#9403](https://github.com/cosmos/cosmos-sdk/pull/9403) Add `RefundGas` function to `GasMeter` interface
* (baseapp, types) [\#9390](https://github.com/cosmos/cosmos-sdk/pull/9390) Add current block header hash to `Context`
* (x/staking) [\#9423](https://github.com/cosmos/cosmos-sdk/pull/9423) Staking delegations now returns empty list instead of rpc error when no records found.
* (x/bank) [\#8614](https://github.com/cosmos/cosmos-sdk/issues/8614) Add `Name` and `Symbol` fields to denom metadata
* (x/auth) [\#8522](https://github.com/cosmos/cosmos-sdk/pull/8522) Allow to query all stored accounts
* (crypto/types) [\#8600](https://github.com/cosmos/cosmos-sdk/pull/8600) `CompactBitArray`: optimize the `NumTrueBitsBefore` method and add an `Equal` method.
Expand Down
2 changes: 1 addition & 1 deletion client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func AddTxFlagsToCmd(cmd *cobra.Command) {
cmd.Flags().String(FlagFrom, "", "Name or address of private key with which to sign")
cmd.Flags().Uint64P(FlagAccountNumber, "a", 0, "The account number of the signing account (offline mode only)")
cmd.Flags().Uint64P(FlagSequence, "s", 0, "The sequence number of the signing account (offline mode only)")
cmd.Flags().String(FlagNote, "", "Note to add a description to the transaction (previously `--memo`)")
cmd.Flags().String(FlagNote, "", "Note to add a description to the transaction (previously --memo)")
cmd.Flags().String(FlagFees, "", "Fees to pay along with transaction; eg: 10uatom")
cmd.Flags().String(FlagGasPrices, "", "Gas prices in decimal format to determine the transaction fee (e.g. 0.1uatom)")
cmd.Flags().String(FlagNode, "tcp://localhost:26657", "<host>:<port> to tendermint rpc interface for this chain")
Expand Down
34 changes: 34 additions & 0 deletions store/types/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const (
// Gas measured by the SDK
type Gas = uint64

// ErrorNegativeGasConsumed defines an error thrown when the amount of gas refunded results in a
// negative gas consumed amount.
type ErrorNegativeGasConsumed struct {
Descriptor string
}

// ErrorOutOfGas defines an error thrown when an action results in out of gas.
type ErrorOutOfGas struct {
Descriptor string
Expand All @@ -37,6 +43,7 @@ type GasMeter interface {
GasConsumedToLimit() Gas
Limit() Gas
ConsumeGas(amount Gas, descriptor string)
RefundGas(amount Gas, descriptor string)
IsPastLimit() bool
IsOutOfGas() bool
String() string
Expand Down Expand Up @@ -91,7 +98,20 @@ func (g *basicGasMeter) ConsumeGas(amount Gas, descriptor string) {
if g.consumed > g.limit {
panic(ErrorOutOfGas{descriptor})
}
}

// RefundGas will deduct the given amount from the gas consumed. If the amount is greater than the
// gas consumed, the function will panic.
//
// Use case: This functionality enables refunding gas to the transaction or block gas pools so that
// EVM-compatible chains can fully support the go-ethereum StateDb interface.
// See https://github.com/cosmos/cosmos-sdk/pull/9403 for reference.
func (g *basicGasMeter) RefundGas(amount Gas, descriptor string) {
if g.consumed < amount {
panic(ErrorNegativeGasConsumed{Descriptor: descriptor})
}

g.consumed -= amount
}

func (g *basicGasMeter) IsPastLimit() bool {
Expand Down Expand Up @@ -138,6 +158,20 @@ func (g *infiniteGasMeter) ConsumeGas(amount Gas, descriptor string) {
}
}

// RefundGas will deduct the given amount from the gas consumed. If the amount is greater than the
// gas consumed, the function will panic.
//
// Use case: This functionality enables refunding gas to the trasaction or block gas pools so that
// EVM-compatible chains can fully support the go-ethereum StateDb interface.
// See https://github.com/cosmos/cosmos-sdk/pull/9403 for reference.
func (g *infiniteGasMeter) RefundGas(amount Gas, descriptor string) {
if g.consumed < amount {
panic(ErrorNegativeGasConsumed{Descriptor: descriptor})
}

g.consumed -= amount
}

func (g *infiniteGasMeter) IsPastLimit() bool {
return false
}
Expand Down
8 changes: 8 additions & 0 deletions store/types/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ func TestInfiniteGasMeter(t *testing.T) {
meter.ConsumeGas(10, "consume 10")
require.Equal(t, uint64(10), meter.GasConsumed())
require.Equal(t, uint64(10), meter.GasConsumedToLimit())
meter.RefundGas(1, "refund 1")
require.Equal(t, uint64(9), meter.GasConsumed())
require.False(t, meter.IsPastLimit())
require.False(t, meter.IsOutOfGas())
meter.ConsumeGas(Gas(math.MaxUint64/2), "consume half max uint64")
require.Panics(t, func() { meter.ConsumeGas(Gas(math.MaxUint64/2)+2, "panic") })
require.Panics(t, func() { meter.RefundGas(meter.GasConsumed()+1, "refund greater than consumed") })
}

func TestGasMeter(t *testing.T) {
Expand Down Expand Up @@ -57,6 +60,11 @@ func TestGasMeter(t *testing.T) {
require.Panics(t, func() { meter.ConsumeGas(1, "") }, "Exceeded but not panicked. tc #%d", tcnum)
require.Equal(t, meter.GasConsumedToLimit(), meter.Limit(), "Gas consumption (to limit) not match limit")
require.Equal(t, meter.GasConsumed(), meter.Limit()+1, "Gas consumption not match limit+1")

require.NotPanics(t, func() { meter.RefundGas(1, "refund 1") })
require.Equal(t, meter.GasConsumed(), meter.Limit(), "Gas consumption not match limit+1")
require.Panics(t, func() { meter.RefundGas(meter.GasConsumed()+1, "refund greater than consumed") })

meter2 := NewGasMeter(math.MaxUint64)
meter2.ConsumeGas(Gas(math.MaxUint64/2), "consume half max uint64")
require.Panics(t, func() { meter2.ConsumeGas(Gas(math.MaxUint64/2)+2, "panic") })
Expand Down
2 changes: 1 addition & 1 deletion x/bank/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
panic(err)
}

if err := k.setBalances(ctx, addr, balance.Coins); err != nil {
if err := k.initBalances(ctx, addr, balance.Coins); err != nil {
panic(fmt.Errorf("error on setting balances %w", err))
}

Expand Down
35 changes: 12 additions & 23 deletions x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,31 +227,20 @@ func (k BaseSendKeeper) addCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.C
return nil
}

// clearBalances removes all balances for a given account by address.
func (k BaseSendKeeper) clearBalances(ctx sdk.Context, addr sdk.AccAddress) {
keys := [][]byte{}
k.IterateAccountBalances(ctx, addr, func(balance sdk.Coin) bool {
keys = append(keys, []byte(balance.Denom))
return false
})

// initBalances sets the balance (multiple coins) for an account by address.
// An error is returned upon failure.
func (k BaseSendKeeper) initBalances(ctx sdk.Context, addr sdk.AccAddress, balances sdk.Coins) error {
accountStore := k.getAccountStore(ctx, addr)
for i := range balances {
balance := balances[i]
if !balance.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, balance.String())
}

for _, key := range keys {
accountStore.Delete(key)
}
}

// setBalances sets the balance (multiple coins) for an account by address. It will
// clear out all balances prior to setting the new coins as to set existing balances
// to zero if they don't exist in amt. An error is returned upon failure.
func (k BaseSendKeeper) setBalances(ctx sdk.Context, addr sdk.AccAddress, balances sdk.Coins) error {
k.clearBalances(ctx, addr)

for _, balance := range balances {
err := k.setBalance(ctx, addr, balance)
if err != nil {
return err
// Bank invariants require to not store zero balances.
if !balance.IsZero() {
bz := k.cdc.MustMarshal(&balance)
accountStore.Set([]byte(balance.Denom), bz)
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/bank/legacy/v043/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func migrateBalanceKeys(store sdk.KVStore) {
}
}

// MigrateStore performs in-place store migrations from v0.40 to v0.42. The
// MigrateStore performs in-place store migrations from v0.40 to v0.43. The
// migration includes:
//
// - Change addresses to be length-prefixed.
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/legacy/v043/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
v040distribution "github.com/cosmos/cosmos-sdk/x/distribution/legacy/v040"
)

// MigrateStore performs in-place store migrations from v0.40 to v0.42. The
// MigrateStore performs in-place store migrations from v0.40 to v0.43. The
// migration includes:
//
// - Change addresses to be length-prefixed.
Expand Down
2 changes: 1 addition & 1 deletion x/gov/legacy/v043/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func migrateStoreWeightedVotes(store sdk.KVStore, cdc codec.BinaryCodec) error {
return nil
}

// MigrateStore performs in-place store migrations from v0.40 to v0.42. The
// MigrateStore performs in-place store migrations from v0.40 to v0.43. The
// migration includes:
//
// - Change addresses to be length-prefixed.
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/legacy/v043/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
v040slashing "github.com/cosmos/cosmos-sdk/x/slashing/legacy/v040"
)

// MigrateStore performs in-place store migrations from v0.40 to v0.42. The
// MigrateStore performs in-place store migrations from v0.40 to v0.43. The
// migration includes:
//
// - Change addresses to be length-prefixed.
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ValidateGenesis(data GenesisState) error {

downtimeJail := data.Params.DowntimeJailDuration
if downtimeJail < 1*time.Minute {
return fmt.Errorf("downtime unblond duration must be at least 1 minute, is %s", downtimeJail.String())
return fmt.Errorf("downtime unjail duration must be at least 1 minute, is %s", downtimeJail.String())
}

signedWindow := data.Params.SignedBlocksWindow
Expand Down
45 changes: 16 additions & 29 deletions x/staking/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package rest_test

import (
"fmt"
"io/ioutil"
"net/http"
"testing"

"github.com/gogo/protobuf/proto"
Expand Down Expand Up @@ -414,39 +412,15 @@ func (s *IntegrationTestSuite) TestQueryUnbondingDelegationGRPC() {
}
}

func (s *IntegrationTestSuite) TestQueryDelegationsResponseCode() {
func (s *IntegrationTestSuite) TestQueryDelegatorDelegationsGRPC() {
val := s.network.Validators[0]
baseURL := val.APIAddress

// Create new account in the keyring.
// Create new account in the keyring for address without delegations.
info, _, err := val.ClientCtx.Keyring.NewMnemonic("test", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
s.Require().NoError(err)
newAddr := sdk.AccAddress(info.GetPubKey().Address())

s.T().Log("expect 404 error for address without delegations")
res, statusCode, err := getRequest(fmt.Sprintf("%s/cosmos/staking/v1beta1/delegations/%s", val.APIAddress, newAddr.String()))
s.Require().NoError(err)
s.Require().Contains(string(res), "\"code\": 5")
s.Require().Equal(404, statusCode)
}

func getRequest(url string) ([]byte, int, error) {
res, err := http.Get(url) // nolint:gosec
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, res.StatusCode, err
}

if err = res.Body.Close(); err != nil {
return nil, res.StatusCode, err
}

return body, res.StatusCode, nil
}

func (s *IntegrationTestSuite) TestQueryDelegatorDelegationsGRPC() {
val := s.network.Validators[0]
baseURL := val.APIAddress

testCases := []struct {
name string
url string
Expand Down Expand Up @@ -486,6 +460,19 @@ func (s *IntegrationTestSuite) TestQueryDelegatorDelegationsGRPC() {
Pagination: &query.PageResponse{Total: 1},
},
},
{
"address without delegations",
fmt.Sprintf("%s/cosmos/staking/v1beta1/delegations/%s", baseURL, newAddr.String()),
map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
},
false,
&types.QueryDelegatorDelegationsResponse{},
&types.QueryDelegatorDelegationsResponse{
DelegationResponses: types.DelegationResponses{},
Pagination: &query.PageResponse{Total: 0},
},
},
}

for _, tc := range testCases {
Expand Down
5 changes: 0 additions & 5 deletions x/staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,6 @@ func (k Querier) DelegatorDelegations(c context.Context, req *types.QueryDelegat
return nil, status.Error(codes.Internal, err.Error())
}

if delegations == nil {
return nil, status.Errorf(
codes.NotFound,
"unable to find delegations for address %s", req.DelegatorAddr)
}
delegationResps, err := DelegationsToDelegationResponses(ctx, k.Keeper, delegations)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand Down
Loading

0 comments on commit db8bd84

Please sign in to comment.