Test coin metadata query (#1520)

* Test coin metadata query; contract updates

* Update tests and contracts
This commit is contained in:
Alexander Peters
2023-07-19 15:12:07 +02:00
committed by GitHub
parent 551acabaac
commit fe3afd5200
13 changed files with 136 additions and 17 deletions

View File

@@ -104,7 +104,7 @@ func TestParseCodeInfoFlags(t *testing.T) {
wasmBin, err := os.ReadFile("../../keeper/testdata/hackatom.wasm.gzip") wasmBin, err := os.ReadFile("../../keeper/testdata/hackatom.wasm.gzip")
require.NoError(t, err) require.NoError(t, err)
checksumStr := "beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b" checksumStr := "5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2"
specs := map[string]struct { specs := map[string]struct {
args []string args []string

View File

@@ -26,22 +26,22 @@ func TestParseVerificationFlags(t *testing.T) {
"gov store zipped": { "gov store zipped": {
srcPath: "../../keeper/testdata/hackatom.wasm.gzip", srcPath: "../../keeper/testdata/hackatom.wasm.gzip",
args: []string{ 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", "--code-source-url=https://example.com", "--builder=cosmwasm/workspace-optimizer:0.12.11",
}, },
expBuilder: "cosmwasm/workspace-optimizer:0.12.11", expBuilder: "cosmwasm/workspace-optimizer:0.12.11",
expSource: "https://example.com", expSource: "https://example.com",
expCodeHash: "beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b", expCodeHash: "5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2",
}, },
"gov store raw": { "gov store raw": {
srcPath: "../../keeper/testdata/hackatom.wasm", srcPath: "../../keeper/testdata/hackatom.wasm",
args: []string{ 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", "--code-source-url=https://example.com", "--builder=cosmwasm/workspace-optimizer:0.12.11",
}, },
expBuilder: "cosmwasm/workspace-optimizer:0.12.11", expBuilder: "cosmwasm/workspace-optimizer:0.12.11",
expSource: "https://example.com", expSource: "https://example.com",
expCodeHash: "beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b", expCodeHash: "5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2",
}, },
"gov store checksum mismatch": { "gov store checksum mismatch": {
srcPath: "../../keeper/testdata/hackatom.wasm", srcPath: "../../keeper/testdata/hackatom.wasm",

View File

@@ -64,7 +64,7 @@ func TestCreateSuccess(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, hackatomWasm, storedCode) require.Equal(t, hackatomWasm, storedCode)
// and events emitted // 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"))} exp := sdk.Events{sdk.NewEvent("store_code", sdk.NewAttribute("code_checksum", codeHash), sdk.NewAttribute("code_id", "1"))}
assert.Equal(t, exp, em.Events()) assert.Equal(t, exp, em.Events())
} }
@@ -417,7 +417,7 @@ func TestInstantiate(t *testing.T) {
gasAfter := ctx.GasMeter().GasConsumed() gasAfter := ctx.GasMeter().GasConsumed()
if types.EnableGasVerification { if types.EnableGasVerification {
require.Equal(t, uint64(0x1b5bc), gasAfter-gasBefore) require.Equal(t, uint64(0x1b5bd), gasAfter-gasBefore)
} }
// ensure it is stored properly // ensure it is stored properly

View File

@@ -35,7 +35,7 @@ func TestStoreCodeProposal(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
gzippedWasmCode, err := os.ReadFile("./testdata/hackatom.wasm.gzip") gzippedWasmCode, err := os.ReadFile("./testdata/hackatom.wasm.gzip")
require.NoError(t, err) require.NoError(t, err)
checksum, err := hex.DecodeString("beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b") checksum, err := hex.DecodeString("5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2")
require.NoError(t, err) require.NoError(t, err)
specs := map[string]struct { specs := map[string]struct {
@@ -330,7 +330,7 @@ func TestStoreAndInstantiateContractProposal(t *testing.T) {
wasmCode, err := os.ReadFile("./testdata/hackatom.wasm") wasmCode, err := os.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err) require.NoError(t, err)
checksum, err := hex.DecodeString("beb3de5e9b93b52e514c74ce87ccddb594b9bcd33b7f1af1bb6da63fc883917b") checksum, err := hex.DecodeString("5ca46abb8e9b1b754a5c906f9c0f4eec9121ee09e3cee55ea0faba54763706e2")
require.NoError(t, err) require.NoError(t, err)
var ( var (

View File

@@ -57,8 +57,8 @@ func TestGasCostOnQuery(t *testing.T) {
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256 // 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 GasWork50 uint64 = 64_218 // this is a little shy of 50k gas - to keep an eye on the limit
GasReturnUnhashed uint64 = 32 GasReturnUnhashed uint64 = 29
GasReturnHashed uint64 = 27 GasReturnHashed uint64 = 24
) )
cases := map[string]struct { cases := map[string]struct {
@@ -208,9 +208,9 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
const ( const (
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256 // 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 // This is overhead for calling into a sub-contract
GasReturnHashed uint64 = 27 GasReturnHashed uint64 = 25
) )
cases := map[string]struct { cases := map[string]struct {
@@ -261,7 +261,7 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
expectQueriesFromContract: 10, expectQueriesFromContract: 10,
expectOutOfGas: false, expectOutOfGas: false,
expectError: "query wasm contract failed", // Error we get from the contract instance doing the failing query, not wasmd 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,
}, },
} }

View File

@@ -2,6 +2,7 @@ package keeper
import ( import (
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"strings" "strings"
"testing" "testing"
@@ -36,7 +37,10 @@ func mustParse(t *testing.T, data []byte, res interface{}) {
require.NoError(t, err) 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) { func TestReflectContractSend(t *testing.T) {
cdc := MakeEncodingConfig(t).Marshaler cdc := MakeEncodingConfig(t).Marshaler
@@ -546,7 +550,7 @@ func TestWasmRawQueryWithNil(t *testing.T) {
} }
func TestRustPanicIsHandled(t *testing.T) { func TestRustPanicIsHandled(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, ReflectFeatures) ctx, keepers := CreateTestInput(t, false, CyberpunkFeatures)
keeper := keepers.ContractKeeper keeper := keepers.ContractKeeper
creator := keepers.Faucet.NewFundedRandomAccount(ctx, sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))...) creator := keepers.Faucet.NewFundedRandomAccount(ctx, sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))...)
@@ -568,6 +572,119 @@ func TestRustPanicIsHandled(t *testing.T) {
assert.Nil(t, gotData) 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) { 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) acct := accKeeper.GetAccount(ctx, addr)
if expected == nil { if expected == nil {

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -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