Skip to content

Commit

Permalink
Test coin metadata query (#1520)
Browse files Browse the repository at this point in the history
* Test coin metadata query; contract updates

* Update tests and contracts
  • Loading branch information
alpe committed Jul 19, 2023
1 parent 551acab commit fe3afd5
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 17 deletions.
2 changes: 1 addition & 1 deletion x/wasm/client/cli/gov_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestParseCodeInfoFlags(t *testing.T) {
wasmBin, err := os.ReadFile("../../keeper/testdata/hackatom.wasm.gzip")
require.NoError(t, err)

checksumStr := "beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b"
checksumStr := "5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2"

specs := map[string]struct {
args []string
Expand Down
8 changes: 4 additions & 4 deletions x/wasm/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ func TestParseVerificationFlags(t *testing.T) {
"gov store zipped": {
srcPath: "../../keeper/testdata/hackatom.wasm.gzip",
args: []string{
"--instantiate-everybody=true", "--code-hash=beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b",
"--instantiate-everybody=true", "--code-hash=5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2",
"--code-source-url=https://example.com", "--builder=cosmwasm/workspace-optimizer:0.12.11",
},
expBuilder: "cosmwasm/workspace-optimizer:0.12.11",
expSource: "https://example.com",
expCodeHash: "beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b",
expCodeHash: "5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2",
},
"gov store raw": {
srcPath: "../../keeper/testdata/hackatom.wasm",
args: []string{
"--instantiate-everybody=true", "--code-hash=beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b",
"--instantiate-everybody=true", "--code-hash=5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2",
"--code-source-url=https://example.com", "--builder=cosmwasm/workspace-optimizer:0.12.11",
},
expBuilder: "cosmwasm/workspace-optimizer:0.12.11",
expSource: "https://example.com",
expCodeHash: "beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b",
expCodeHash: "5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2",
},
"gov store checksum mismatch": {
srcPath: "../../keeper/testdata/hackatom.wasm",
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestCreateSuccess(t *testing.T) {
require.NoError(t, err)
require.Equal(t, hackatomWasm, storedCode)
// and events emitted
codeHash := strings.ToLower("beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b")
codeHash := strings.ToLower("5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2")
exp := sdk.Events{sdk.NewEvent("store_code", sdk.NewAttribute("code_checksum", codeHash), sdk.NewAttribute("code_id", "1"))}
assert.Equal(t, exp, em.Events())
}
Expand Down Expand Up @@ -417,7 +417,7 @@ func TestInstantiate(t *testing.T) {

gasAfter := ctx.GasMeter().GasConsumed()
if types.EnableGasVerification {
require.Equal(t, uint64(0x1b5bc), gasAfter-gasBefore)
require.Equal(t, uint64(0x1b5bd), gasAfter-gasBefore)
}

// ensure it is stored properly
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/keeper/proposal_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestStoreCodeProposal(t *testing.T) {
require.NoError(t, err)
gzippedWasmCode, err := os.ReadFile("./testdata/hackatom.wasm.gzip")
require.NoError(t, err)
checksum, err := hex.DecodeString("beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b")
checksum, err := hex.DecodeString("5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2")
require.NoError(t, err)

specs := map[string]struct {
Expand Down Expand Up @@ -330,7 +330,7 @@ func TestStoreAndInstantiateContractProposal(t *testing.T) {
wasmCode, err := os.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)

checksum, err := hex.DecodeString("beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b")
checksum, err := hex.DecodeString("5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2")
require.NoError(t, err)

var (
Expand Down
10 changes: 5 additions & 5 deletions x/wasm/keeper/recurse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func TestGasCostOnQuery(t *testing.T) {
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256
GasWork50 uint64 = 64_218 // this is a little shy of 50k gas - to keep an eye on the limit

GasReturnUnhashed uint64 = 32
GasReturnHashed uint64 = 27
GasReturnUnhashed uint64 = 29
GasReturnHashed uint64 = 24
)

cases := map[string]struct {
Expand Down Expand Up @@ -208,9 +208,9 @@ func TestLimitRecursiveQueryGas(t *testing.T) {

const (
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256
GasWork2k uint64 = 77_206 // = NewContractInstanceCosts + x // we have 6x gas used in cpu than in the instance
GasWork2k uint64 = 77_161 // = NewContractInstanceCosts + x // we have 6x gas used in cpu than in the instance
// This is overhead for calling into a sub-contract
GasReturnHashed uint64 = 27
GasReturnHashed uint64 = 25
)

cases := map[string]struct {
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
expectQueriesFromContract: 10,
expectOutOfGas: false,
expectError: "query wasm contract failed", // Error we get from the contract instance doing the failing query, not wasmd
expectedGas: 10*(GasWork2k+GasReturnHashed) - 247,
expectedGas: 10*(GasWork2k+GasReturnHashed) - 229,
},
}

Expand Down
121 changes: 119 additions & 2 deletions x/wasm/keeper/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -36,7 +37,10 @@ func mustParse(t *testing.T, data []byte, res interface{}) {
require.NoError(t, err)
}

const ReflectFeatures = "staking,mask,stargate,cosmwasm_1_1"
const (
ReflectFeatures = "staking,mask,stargate,cosmwasm_1_1"
CyberpunkFeatures = "staking,mask,stargate,cosmwasm_1_1,cosmwasm_1_2,cosmwasm_1_3"
)

func TestReflectContractSend(t *testing.T) {
cdc := MakeEncodingConfig(t).Marshaler
Expand Down Expand Up @@ -546,7 +550,7 @@ func TestWasmRawQueryWithNil(t *testing.T) {
}

func TestRustPanicIsHandled(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, ReflectFeatures)
ctx, keepers := CreateTestInput(t, false, CyberpunkFeatures)
keeper := keepers.ContractKeeper

creator := keepers.Faucet.NewFundedRandomAccount(ctx, sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))...)
Expand All @@ -568,6 +572,119 @@ func TestRustPanicIsHandled(t *testing.T) {
assert.Nil(t, gotData)
}

func TestQueryDenomsIntegration(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, CyberpunkFeatures)
ck, k := keepers.ContractKeeper, keepers.WasmKeeper
creator := keepers.Faucet.NewFundedRandomAccount(ctx, sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))...)

// upload code
codeID, _, err := ck.Create(ctx, creator, testdata.CyberpunkContractWasm(), nil)
require.NoError(t, err)

contractAddr, _, err := ck.Instantiate(ctx, codeID, creator, nil, []byte("{}"), "cyberpunk contract", nil)
require.NoError(t, err)

var (
metadata1 = banktypes.Metadata{
Description: "testing",
DenomUnits: []*banktypes.DenomUnit{
{Denom: "ualx", Exponent: 0, Aliases: []string{"microalx"}},
{Denom: "alx", Exponent: 6, Aliases: []string{"ALX"}},
},
Base: "ualx",
Display: "alx",
Name: "my test denom",
Symbol: "XALX",
URI: "https://example.com/ualx",
URIHash: "my_hash",
}
metadata2 = banktypes.Metadata{
Description: "testing2",
DenomUnits: []*banktypes.DenomUnit{
{Denom: "ublx", Exponent: 0, Aliases: []string{"microblx"}},
{Denom: "blx", Exponent: 6, Aliases: []string{"BLX"}},
},
Base: "ublx",
Display: "blx",
Name: "my other test denom",
Symbol: "XBLX",
}
)
type dict map[string]any

keepers.BankKeeper.SetDenomMetaData(ctx, metadata1)
keepers.BankKeeper.SetDenomMetaData(ctx, metadata2)

specs := map[string]struct {
query string
exp []byte
expErr *errorsmod.Error
}{
"all denoms": {
query: `{"denoms":{}}`,
exp: mustMarshal(t, []dict{
{
"description": "testing",
"denom_units": []dict{
{"denom": "ualx", "exponent": 0, "aliases": []string{"microalx"}},
{"denom": "alx", "exponent": 6, "aliases": []string{"ALX"}},
},
"base": "ualx",
"display": "alx",
"name": "my test denom",
"symbol": "XALX",
"uri": "https://example.com/ualx",
"uri_hash": "my_hash",
}, {
"description": "testing2",
"denom_units": []dict{
{"denom": "ublx", "exponent": 0, "aliases": []string{"microblx"}},
{"denom": "blx", "exponent": 6, "aliases": []string{"BLX"}},
},
"base": "ublx",
"display": "blx",
"name": "my other test denom",
"symbol": "XBLX",
"uri": "",
"uri_hash": "",
},
}),
},
"single denom": {
query: `{"denom":{"denom":"ublx"}}`,
exp: mustMarshal(t, dict{
"description": "testing2",
"denom_units": []dict{
{"denom": "ublx", "exponent": 0, "aliases": []string{"microblx"}},
{"denom": "blx", "exponent": 6, "aliases": []string{"BLX"}},
},
"base": "ublx",
"display": "blx",
"name": "my other test denom",
"symbol": "XBLX",
"uri": "",
"uri_hash": "",
}),
},
"unknown denom": {
query: `{"denom":{"denom":"unknown"}}`,
expErr: sdkerrors.ErrNotFound,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
gotData, gotErr := k.QuerySmart(ctx, contractAddr, []byte(spec.query))
if spec.expErr != nil {
require.Error(t, gotErr)
assert.Contains(t, gotErr.Error(), fmt.Sprintf("codespace: %s, code: %d:", spec.expErr.Codespace(), spec.expErr.ABCICode()))
return
}
require.NoError(t, gotErr)
assert.JSONEq(t, string(spec.exp), string(gotData), string(gotData))
})
}
}

func checkAccount(t *testing.T, ctx sdk.Context, accKeeper authkeeper.AccountKeeper, bankKeeper bankkeeper.Keeper, addr sdk.AccAddress, expected sdk.Coins) {
acct := accKeeper.GetAccount(ctx, addr)
if expected == nil {
Expand Down
Binary file modified x/wasm/keeper/testdata/cyberpunk.wasm
Binary file not shown.
Binary file modified x/wasm/keeper/testdata/hackatom.wasm
Binary file not shown.
Binary file modified x/wasm/keeper/testdata/hackatom.wasm.gzip
Binary file not shown.
Binary file modified x/wasm/keeper/testdata/ibc_reflect_send.wasm
Binary file not shown.
Binary file modified x/wasm/keeper/testdata/reflect.wasm
Binary file not shown.
Binary file modified x/wasm/keeper/testdata/staking.wasm
Binary file not shown.
4 changes: 3 additions & 1 deletion x/wasm/keeper/testdata/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
v1.2.0
v1.3.0
burner.wasm: v1.2.0
ibc-reflect.wasm: custom build, see: https://github.com/CosmWasm/cosmwasm/pull/1690

0 comments on commit fe3afd5

Please sign in to comment.