Skip to content

Commit

Permalink
update bank
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Oct 14, 2021
1 parent f00e7a4 commit 241b9be
Show file tree
Hide file tree
Showing 60 changed files with 1,076 additions and 2,427 deletions.
27 changes: 13 additions & 14 deletions x/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand Down Expand Up @@ -92,10 +91,10 @@ func TestSendNotEnoughBalance(t *testing.T) {
}

genAccs := []authtypes.GenesisAccount{acc}
app := simapp.SetupWithGenesisAccounts(t, genAccs)
app := simapp.SetupWithGenesisAccounts(genAccs)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67))))
require.NoError(t, simapp.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67))))

app.Commit()

Expand Down Expand Up @@ -127,10 +126,10 @@ func TestMsgMultiSendWithAccounts(t *testing.T) {
}

genAccs := []authtypes.GenesisAccount{acc}
app := simapp.SetupWithGenesisAccounts(t, genAccs)
app := simapp.SetupWithGenesisAccounts(genAccs)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67))))
require.NoError(t, simapp.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 67))))

app.Commit()

Expand Down Expand Up @@ -197,12 +196,12 @@ func TestMsgMultiSendMultipleOut(t *testing.T) {
}

genAccs := []authtypes.GenesisAccount{acc1, acc2}
app := simapp.SetupWithGenesisAccounts(t, genAccs)
app := simapp.SetupWithGenesisAccounts(genAccs)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))
require.NoError(t, simapp.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))

require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))
require.NoError(t, simapp.FundAccount(app.BankKeeper, ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))

app.Commit()

Expand Down Expand Up @@ -246,14 +245,14 @@ func TestMsgMultiSendMultipleInOut(t *testing.T) {
}

genAccs := []authtypes.GenesisAccount{acc1, acc2, acc4}
app := simapp.SetupWithGenesisAccounts(t, genAccs)
app := simapp.SetupWithGenesisAccounts(genAccs)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))
require.NoError(t, simapp.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))

require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))
require.NoError(t, simapp.FundAccount(app.BankKeeper, ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))

require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr4, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))
require.NoError(t, simapp.FundAccount(app.BankKeeper, ctx, addr4, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))

app.Commit()

Expand Down Expand Up @@ -293,10 +292,10 @@ func TestMsgMultiSendDependent(t *testing.T) {
require.NoError(t, err)

genAccs := []authtypes.GenesisAccount{acc1, acc2}
app := simapp.SetupWithGenesisAccounts(t, genAccs)
app := simapp.SetupWithGenesisAccounts(genAccs)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))
require.NoError(t, simapp.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42))))

app.Commit()

Expand Down
21 changes: 8 additions & 13 deletions x/bank/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

var moduleAccAddr = authtypes.NewModuleAddress(stakingtypes.BondedPoolName)

func BenchmarkOneBankSendTxPerBlock(b *testing.B) {
b.Skip("Skipping benchmark with buggy code reported at https://github.com/cosmos/cosmos-sdk/issues/10023")

b.ReportAllocs()
// Add an account at genesis
acc := authtypes.BaseAccount{
Expand All @@ -29,11 +26,11 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) {

// construct genesis state
genAccs := []types.GenesisAccount{&acc}
benchmarkApp := simapp.SetupWithGenesisAccounts(&testing.T{}, genAccs)
benchmarkApp := simapp.SetupWithGenesisAccounts(genAccs)
ctx := benchmarkApp.BaseApp.NewContext(false, tmproto.Header{})

// some value conceivably higher than the benchmarks would ever go
require.NoError(b, testutil.FundAccount(benchmarkApp.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000))))
require.NoError(b, simapp.FundAccount(benchmarkApp.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000))))

benchmarkApp.Commit()
txGen := simappparams.MakeTestEncodingConfig().TxConfig
Expand All @@ -49,12 +46,12 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) {
// Committing, and what time comes from Check/Deliver Tx.
for i := 0; i < b.N; i++ {
benchmarkApp.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: height}})
_, _, err := benchmarkApp.SimCheck(txGen.TxEncoder(), txs[i])
_, _, err := benchmarkApp.Check(txGen.TxEncoder(), txs[i])
if err != nil {
panic("something is broken in checking transaction")
}

_, _, err = benchmarkApp.SimDeliver(txGen.TxEncoder(), txs[i])
_, _, err = benchmarkApp.Deliver(txGen.TxEncoder(), txs[i])
require.NoError(b, err)
benchmarkApp.EndBlock(abci.RequestEndBlock{Height: height})
benchmarkApp.Commit()
Expand All @@ -63,8 +60,6 @@ func BenchmarkOneBankSendTxPerBlock(b *testing.B) {
}

func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) {
b.Skip("Skipping benchmark with buggy code reported at https://github.com/cosmos/cosmos-sdk/issues/10023")

b.ReportAllocs()
// Add an account at genesis
acc := authtypes.BaseAccount{
Expand All @@ -73,11 +68,11 @@ func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) {

// Construct genesis state
genAccs := []authtypes.GenesisAccount{&acc}
benchmarkApp := simapp.SetupWithGenesisAccounts(&testing.T{}, genAccs)
benchmarkApp := simapp.SetupWithGenesisAccounts(genAccs)
ctx := benchmarkApp.BaseApp.NewContext(false, tmproto.Header{})

// some value conceivably higher than the benchmarks would ever go
require.NoError(b, testutil.FundAccount(benchmarkApp.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000))))
require.NoError(b, simapp.FundAccount(benchmarkApp.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000))))

benchmarkApp.Commit()
txGen := simappparams.MakeTestEncodingConfig().TxConfig
Expand All @@ -93,12 +88,12 @@ func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) {
// Committing, and what time comes from Check/Deliver Tx.
for i := 0; i < b.N; i++ {
benchmarkApp.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: height}})
_, _, err := benchmarkApp.SimCheck(txGen.TxEncoder(), txs[i])
_, _, err := benchmarkApp.Check(txGen.TxEncoder(), txs[i])
if err != nil {
panic("something is broken in checking transaction")
}

_, _, err = benchmarkApp.SimDeliver(txGen.TxEncoder(), txs[i])
_, _, err = benchmarkApp.Deliver(txGen.TxEncoder(), txs[i])
require.NoError(b, err)
benchmarkApp.EndBlock(abci.RequestEndBlock{Height: height})
benchmarkApp.Commit()
Expand Down
1 change: 0 additions & 1 deletion x/bank/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ func GetCmdQueryTotalSupply() *cobra.Command {
cmd := &cobra.Command{
Use: "total",
Short: "Query the total supply of coins of the chain",
Args: cobra.NoArgs,
Long: strings.TrimSpace(
fmt.Sprintf(`Query total supply of coins that are held by accounts in the chain.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package testutil
// +build norace

package rest_test

import (
"fmt"

"github.com/gogo/protobuf/proto"

"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/rest"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand Down Expand Up @@ -140,26 +142,8 @@ func (s *IntegrationTestSuite) TestDenomMetadataGRPCHandler() {
Base: "uatom",
Display: "atom",
},
{
Name: "Ethereum",
Symbol: "ETH",
Description: "Ethereum mainnet token",
DenomUnits: []*types.DenomUnit{
{
Denom: "wei",
Exponent: 0,
},
{
Denom: "eth",
Exponent: 6,
Aliases: []string{"ETH"},
},
},
Base: "wei",
Display: "eth",
},
},
Pagination: &query.PageResponse{Total: 2},
Pagination: &query.PageResponse{Total: 1},
},
},
{
Expand Down
118 changes: 118 additions & 0 deletions x/bank/client/rest/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package rest

import (
"fmt"
"net/http"

"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

// QueryBalancesRequestHandlerFn returns a REST handler that queries for all
// account balances or a specific balance by denomination.
func QueryBalancesRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

vars := mux.Vars(r)
bech32addr := vars["address"]

addr, err := sdk.AccAddressFromBech32(bech32addr)
if rest.CheckInternalServerError(w, err) {
return
}

ctx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
if !ok {
return
}

var (
params interface{}
route string
)

denom := r.FormValue("denom")
if denom == "" {
params = types.NewQueryAllBalancesRequest(addr, nil)
route = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAllBalances)
} else {
params = types.NewQueryBalanceRequest(addr, denom)
route = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryBalance)
}

bz, err := ctx.LegacyAmino.MarshalJSON(params)
if rest.CheckBadRequestError(w, err) {
return
}

res, height, err := ctx.QueryWithData(route, bz)
if rest.CheckInternalServerError(w, err) {
return
}

ctx = ctx.WithHeight(height)
rest.PostProcessResponse(w, ctx, res)
}
}

// HTTP request handler to query the total supply of coins
func totalSupplyHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
if rest.CheckBadRequestError(w, err) {
return
}

clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
if !ok {
return
}

params := types.NewQueryTotalSupplyParams(page, limit)
bz, err := clientCtx.LegacyAmino.MarshalJSON(params)

if rest.CheckBadRequestError(w, err) {
return
}

res, height, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply), bz)

if rest.CheckInternalServerError(w, err) {
return
}

clientCtx = clientCtx.WithHeight(height)
rest.PostProcessResponse(w, clientCtx, res)
}
}

// HTTP request handler to query the supply of a single denom
func supplyOfHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
denom := mux.Vars(r)["denom"]
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
if !ok {
return
}

params := types.NewQuerySupplyOfParams(denom)
bz, err := clientCtx.LegacyAmino.MarshalJSON(params)

if rest.CheckBadRequestError(w, err) {
return
}

res, height, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySupplyOf), bz)
if rest.CheckInternalServerError(w, err) {
return
}

clientCtx = clientCtx.WithHeight(height)
rest.PostProcessResponse(w, clientCtx, res)
}
}
Loading

0 comments on commit 241b9be

Please sign in to comment.