From 192261b4e2b8bc200c096cd46733c1d341370a61 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Mon, 23 Oct 2023 11:28:47 +0800 Subject: [PATCH 01/22] Problem: parse chain-id from big genesis file could be slow Solution: - use streaming json parsing for that --- CHANGELOG.md | 1 + go.mod | 1 + go.sum | 2 ++ server/util.go | 9 ++++++--- types/chain_id.go | 24 ++++++++++++++++++++++++ types/chain_id_test.go | 22 ++++++++++++++++++++++ 6 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 types/chain_id.go create mode 100644 types/chain_id_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 59055fe238ab..2e7db98a0419 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [#17470](https://github.com/cosmos/cosmos-sdk/pull/17470) Avoid open 0.0.0.0 to public by default and add `listen-ip-address` argument for `testnet init-files` cmd. * (types) [#17670](https://github.com/cosmos/cosmos-sdk/pull/17670) Use `ctx.CometInfo` in place of `ctx.VoteInfos` * [#17733](https://github.com/cosmos/cosmos-sdk/pull/17733) Ensure `buf export` exports all proto dependencies +* [#]() Use streaming json parser to parse chain-id from genesis file. ### Bug Fixes diff --git a/go.mod b/go.mod index 591fe58f73c3..843786bc624a 100644 --- a/go.mod +++ b/go.mod @@ -69,6 +69,7 @@ require ( filippo.io/edwards25519 v1.0.0 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect github.com/DataDog/zstd v1.5.5 // indirect + github.com/bcicen/jstream v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/bufbuild/protocompile v0.6.0 // indirect diff --git a/go.sum b/go.sum index 6bc253effcdd..6fdcee69dddd 100644 --- a/go.sum +++ b/go.sum @@ -95,6 +95,8 @@ github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6l github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU= github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= +github.com/bcicen/jstream v1.0.1 h1:BXY7Cu4rdmc0rhyTVyT3UkxAiX3bnLpKLas9btbH5ck= +github.com/bcicen/jstream v1.0.1/go.mod h1:9ielPxqFry7Y4Tg3j4BfjPocfJ3TbsRtXOAYXYmRuAQ= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= diff --git a/server/util.go b/server/util.go index dae0a21abb7e..905292f163a4 100644 --- a/server/util.go +++ b/server/util.go @@ -37,7 +37,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/mempool" "github.com/cosmos/cosmos-sdk/version" - genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" ) // ServerContextKey defines the context key used to retrieve a server.Context from @@ -485,12 +484,16 @@ func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) { chainID := cast.ToString(appOpts.Get(flags.FlagChainID)) if chainID == "" { // fallback to genesis chain-id - appGenesis, err := genutiltypes.AppGenesisFromFile(filepath.Join(homeDir, "config", "genesis.json")) + reader, err := os.Open(filepath.Join(homeDir, "config", "genesis.json")) if err != nil { panic(err) } + defer reader.Close() - chainID = appGenesis.ChainID + chainID, err = sdk.ParseChainIDFromGenesis(reader) + if err != nil { + panic(fmt.Errorf("failed to parse chain-id from genesis file: %w", err)) + } } snapshotStore, err := GetSnapshotStore(appOpts) diff --git a/types/chain_id.go b/types/chain_id.go new file mode 100644 index 000000000000..6671fccddb24 --- /dev/null +++ b/types/chain_id.go @@ -0,0 +1,24 @@ +package types + +import ( + "io" + + "github.com/bcicen/jstream" +) + +const ChainIDFieldName = "chain-id" + +// ParseChainIDFromGenesis parses the chain-id from the genesis file using constant memory. +func ParseChainIDFromGenesis(reader io.Reader) (string, error) { + decoder := jstream.NewDecoder(reader, 1).EmitKV() + for mv := range decoder.Stream() { + kv := mv.Value.(jstream.KV) + if kv.Key == ChainIDFieldName { + if chain_id, ok := kv.Value.(string); ok { + return chain_id, nil + } + break + } + } + return "", nil +} diff --git a/types/chain_id_test.go b/types/chain_id_test.go new file mode 100644 index 000000000000..598e913595e5 --- /dev/null +++ b/types/chain_id_test.go @@ -0,0 +1,22 @@ +package types + +import ( + strings "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestParseChainIDFromGenesis(t *testing.T) { + chain_id, err := ParseChainIDFromGenesis(strings.NewReader(`{ + "chain-id":"test-chain-id", + "state": { + "accounts": [ + "abc": {}, + "efg": {}, + ], + }, + }`)) + require.NoError(t, err) + require.Equal(t, "test-chain-id", chain_id) +} From 7c59903664101e2e524c87b5ec5c4841c2888da8 Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 23 Oct 2023 11:30:36 +0800 Subject: [PATCH 02/22] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e7db98a0419..b5d28a8c3e67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,7 +67,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [#17470](https://github.com/cosmos/cosmos-sdk/pull/17470) Avoid open 0.0.0.0 to public by default and add `listen-ip-address` argument for `testnet init-files` cmd. * (types) [#17670](https://github.com/cosmos/cosmos-sdk/pull/17670) Use `ctx.CometInfo` in place of `ctx.VoteInfos` * [#17733](https://github.com/cosmos/cosmos-sdk/pull/17733) Ensure `buf export` exports all proto dependencies -* [#]() Use streaming json parser to parse chain-id from genesis file. +* [#18204](https://github.com/cosmos/cosmos-sdk/pull/18204) Use streaming json parser to parse chain-id from genesis file. ### Bug Fixes From ee9b00a5a6775afc86d1ce56382fdc4d501c46e8 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Mon, 23 Oct 2023 14:29:09 +0800 Subject: [PATCH 03/22] fix unit test --- types/chain_id.go | 14 ++++++---- types/chain_id_test.go | 63 +++++++++++++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/types/chain_id.go b/types/chain_id.go index 6671fccddb24..901b9cc28ba4 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -1,6 +1,8 @@ package types import ( + "errors" + fmt "fmt" "io" "github.com/bcicen/jstream" @@ -12,13 +14,15 @@ const ChainIDFieldName = "chain-id" func ParseChainIDFromGenesis(reader io.Reader) (string, error) { decoder := jstream.NewDecoder(reader, 1).EmitKV() for mv := range decoder.Stream() { - kv := mv.Value.(jstream.KV) - if kv.Key == ChainIDFieldName { - if chain_id, ok := kv.Value.(string); ok { + if kv, ok := mv.Value.(jstream.KV); ok { + if kv.Key == ChainIDFieldName { + chain_id, ok := kv.Value.(string) + if !ok { + return "", fmt.Errorf("chain-id field is not string") + } return chain_id, nil } - break } } - return "", nil + return "", errors.New("chain-id field not found") } diff --git a/types/chain_id_test.go b/types/chain_id_test.go index 598e913595e5..c7f64a936e69 100644 --- a/types/chain_id_test.go +++ b/types/chain_id_test.go @@ -8,15 +8,58 @@ import ( ) func TestParseChainIDFromGenesis(t *testing.T) { - chain_id, err := ParseChainIDFromGenesis(strings.NewReader(`{ - "chain-id":"test-chain-id", - "state": { - "accounts": [ - "abc": {}, - "efg": {}, - ], + testCases := []struct { + name string + json string + expChainID string + }{ + { + "success", + `{ + "state": { + "accounts": { + "a": {} + } + }, + "chain-id": "test-chain-id" + }`, + "test-chain-id", }, - }`)) - require.NoError(t, err) - require.Equal(t, "test-chain-id", chain_id) + { + "not exist", + `{ + "state": { + "accounts": { + "a": {} + } + }, + "chain_id": "test-chain-id" + }`, + "", + }, + { + "invalid type", + `{ + "chain-id":1, + }`, + "", + }, + { + "invalid json", + `[ " ': }`, + "", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + chain_id, err := ParseChainIDFromGenesis(strings.NewReader(tc.json)) + if tc.expChainID == "" { + require.Error(t, err) + } else { + require.NoError(t, err) + require.Equal(t, tc.expChainID, chain_id) + } + }) + } } From 567ee2dbd0f691e696d56980b9c0e26ae86173b6 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Mon, 23 Oct 2023 15:27:40 +0800 Subject: [PATCH 04/22] patch the library to support early abort --- go.mod | 3 ++- go.sum | 4 ++-- types/chain_id.go | 20 ++++++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 843786bc624a..2cddfba56418 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( cosmossdk.io/x/protocolpool v0.0.0-20230925135524-a1bc045b3190 cosmossdk.io/x/tx v0.11.0 github.com/99designs/keyring v1.2.1 + github.com/bcicen/jstream v1.0.1 github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 github.com/bits-and-blooms/bitset v1.10.0 github.com/chzyer/readline v1.5.1 @@ -69,7 +70,6 @@ require ( filippo.io/edwards25519 v1.0.0 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect github.com/DataDog/zstd v1.5.5 // indirect - github.com/bcicen/jstream v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/bufbuild/protocompile v0.6.0 // indirect @@ -174,6 +174,7 @@ replace cosmossdk.io/x/protocolpool => ./x/protocolpool replace ( // use cosmos fork of keyring github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0 + github.com/bcicen/jstream => github.com/yihuang/jstream v0.0.0-20231023072018-cb6eaf5ca571 // dgrijalva/jwt-go is deprecated and doesn't receive security updates. // TODO: remove it: https://github.com/cosmos/cosmos-sdk/issues/13134 github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2 diff --git a/go.sum b/go.sum index 6fdcee69dddd..e807b9cd916a 100644 --- a/go.sum +++ b/go.sum @@ -95,8 +95,6 @@ github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6l github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU= github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= -github.com/bcicen/jstream v1.0.1 h1:BXY7Cu4rdmc0rhyTVyT3UkxAiX3bnLpKLas9btbH5ck= -github.com/bcicen/jstream v1.0.1/go.mod h1:9ielPxqFry7Y4Tg3j4BfjPocfJ3TbsRtXOAYXYmRuAQ= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= @@ -794,6 +792,8 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +github.com/yihuang/jstream v0.0.0-20231023072018-cb6eaf5ca571 h1:J0wQbFhjC6ZLJlNCIcWSEpWuiBQ/wdvaDXJSBzpoeyQ= +github.com/yihuang/jstream v0.0.0-20231023072018-cb6eaf5ca571/go.mod h1:9ielPxqFry7Y4Tg3j4BfjPocfJ3TbsRtXOAYXYmRuAQ= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/types/chain_id.go b/types/chain_id.go index 901b9cc28ba4..548be9698ba1 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -2,7 +2,6 @@ package types import ( "errors" - fmt "fmt" "io" "github.com/bcicen/jstream" @@ -13,16 +12,21 @@ const ChainIDFieldName = "chain-id" // ParseChainIDFromGenesis parses the chain-id from the genesis file using constant memory. func ParseChainIDFromGenesis(reader io.Reader) (string, error) { decoder := jstream.NewDecoder(reader, 1).EmitKV() - for mv := range decoder.Stream() { + var chain_id string + err := decoder.Decode(func(mv *jstream.MetaValue) bool { if kv, ok := mv.Value.(jstream.KV); ok { if kv.Key == ChainIDFieldName { - chain_id, ok := kv.Value.(string) - if !ok { - return "", fmt.Errorf("chain-id field is not string") - } - return chain_id, nil + chain_id, _ = kv.Value.(string) + return false } } + return true + }) + if len(chain_id) > 0 { + return chain_id, nil } - return "", errors.New("chain-id field not found") + if err == nil { + return "", errors.New("chain-id not found in genesis file") + } + return "", err } From 55be3ec8a0472797a5604f22ad672ba95c05d9b3 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Mon, 23 Oct 2023 15:38:10 +0800 Subject: [PATCH 05/22] add benchmarking --- types/chain_id.go | 2 +- types/chain_id_test.go | 585 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 584 insertions(+), 3 deletions(-) diff --git a/types/chain_id.go b/types/chain_id.go index 548be9698ba1..5e15faab19a1 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -7,7 +7,7 @@ import ( "github.com/bcicen/jstream" ) -const ChainIDFieldName = "chain-id" +const ChainIDFieldName = "chain_id" // ParseChainIDFromGenesis parses the chain-id from the genesis file using constant memory. func ParseChainIDFromGenesis(reader io.Reader) (string, error) { diff --git a/types/chain_id_test.go b/types/chain_id_test.go index c7f64a936e69..9aa324392b47 100644 --- a/types/chain_id_test.go +++ b/types/chain_id_test.go @@ -4,6 +4,7 @@ import ( strings "strings" "testing" + genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" "github.com/stretchr/testify/require" ) @@ -21,7 +22,7 @@ func TestParseChainIDFromGenesis(t *testing.T) { "a": {} } }, - "chain-id": "test-chain-id" + "chain_id": "test-chain-id" }`, "test-chain-id", }, @@ -33,7 +34,7 @@ func TestParseChainIDFromGenesis(t *testing.T) { "a": {} } }, - "chain_id": "test-chain-id" + "chain-id": "test-chain-id" }`, "", }, @@ -63,3 +64,583 @@ func TestParseChainIDFromGenesis(t *testing.T) { }) } } + +func BenchmarkParseChainID(b *testing.B) { + // an arbitrary genesis file from a cronos devnet. + json := ` +{ + "genesis_time": "2023-09-27T05:35:43.213433Z", + "chain_id": "cronos_777-1", + "initial_height": "1", + "consensus_params": { + "block": { + "max_bytes": "1048576", + "max_gas": "81500000" + }, + "evidence": { + "max_age_num_blocks": "100000", + "max_age_duration": "172800000000000", + "max_bytes": "1048576" + }, + "validator": { + "pub_key_types": [ + "ed25519" + ] + }, + "version": { + "app": "0" + } + }, + "app_hash": "", + "app_state": { + "07-tendermint": null, + "auth": { + "params": { + "max_memo_characters": "256", + "tx_sig_limit": "7", + "tx_size_cost_per_byte": "10", + "sig_verify_cost_ed25519": "590", + "sig_verify_cost_secp256k1": "1000" + }, + "accounts": [ + { + "@type": "/ethermint.types.v1.EthAccount", + "base_account": { + "address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", + "pub_key": null, + "account_number": "0", + "sequence": "0" + }, + "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + }, + { + "@type": "/ethermint.types.v1.EthAccount", + "base_account": { + "address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", + "pub_key": null, + "account_number": "1", + "sequence": "0" + }, + "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + }, + { + "@type": "/ethermint.types.v1.EthAccount", + "base_account": { + "address": "crc1x7x9pkfxf33l87ftspk5aetwnkr0lvlv3346cd", + "pub_key": null, + "account_number": "2", + "sequence": "0" + }, + "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + }, + { + "@type": "/ethermint.types.v1.EthAccount", + "base_account": { + "address": "crc16z0herz998946wr659lr84c8c556da55dc34hh", + "pub_key": null, + "account_number": "3", + "sequence": "0" + }, + "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + }, + { + "@type": "/ethermint.types.v1.EthAccount", + "base_account": { + "address": "crc1q04jewhxw4xxu3vlg3rc85240h9q7ns6hglz0g", + "pub_key": null, + "account_number": "4", + "sequence": "0" + }, + "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + } + ] + }, + "authz": { + "authorization": [] + }, + "bank": { + "params": { + "send_enabled": [], + "default_send_enabled": true + }, + "balances": [ + { + "address": "crc1q04jewhxw4xxu3vlg3rc85240h9q7ns6hglz0g", + "coins": [ + { + "denom": "basetcro", + "amount": "30000000000000000000000" + } + ] + }, + { + "address": "crc1x7x9pkfxf33l87ftspk5aetwnkr0lvlv3346cd", + "coins": [ + { + "denom": "basetcro", + "amount": "10000000000000000000000" + } + ] + }, + { + "address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", + "coins": [ + { + "denom": "basetcro", + "amount": "10000000000000000000000" + }, + { + "denom": "stake", + "amount": "1000000000000000000" + } + ] + }, + { + "address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", + "coins": [ + { + "denom": "basetcro", + "amount": "10000000000000000000000" + }, + { + "denom": "stake", + "amount": "1000000000000000000" + } + ] + }, + { + "address": "crc16z0herz998946wr659lr84c8c556da55dc34hh", + "coins": [ + { + "denom": "basetcro", + "amount": "20000000000000000000000" + } + ] + } + ], + "supply": [ + { + "denom": "basetcro", + "amount": "80000000000000000000000" + }, + { + "denom": "stake", + "amount": "2000000000000000000" + } + ], + "denom_metadata": [], + "send_enabled": [] + }, + "capability": { + "index": "1", + "owners": [] + }, + "consensus": null, + "crisis": { + "constant_fee": { + "denom": "stake", + "amount": "1000" + } + }, + "cronos": { + "params": { + "ibc_cro_denom": "ibc/6411AE2ADA1E73DB59DB151A8988F9B7D5E7E233D8414DB6817F8F1A01611F86", + "ibc_timeout": "86400000000000", + "cronos_admin": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", + "enable_auto_deployment": true + }, + "external_contracts": [], + "auto_contracts": [] + }, + "distribution": { + "params": { + "community_tax": "0.020000000000000000", + "base_proposer_reward": "0.000000000000000000", + "bonus_proposer_reward": "0.000000000000000000", + "withdraw_addr_enabled": true + }, + "fee_pool": { + "community_pool": [] + }, + "delegator_withdraw_infos": [], + "previous_proposer": "", + "outstanding_rewards": [], + "validator_accumulated_commissions": [], + "validator_historical_rewards": [], + "validator_current_rewards": [], + "delegator_starting_infos": [], + "validator_slash_events": [] + }, + "evidence": { + "evidence": [] + }, + "evm": { + "accounts": [], + "params": { + "evm_denom": "basetcro", + "enable_create": true, + "enable_call": true, + "extra_eips": [], + "chain_config": { + "homestead_block": "0", + "dao_fork_block": "0", + "dao_fork_support": true, + "eip150_block": "0", + "eip150_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "eip155_block": "0", + "eip158_block": "0", + "byzantium_block": "0", + "constantinople_block": "0", + "petersburg_block": "0", + "istanbul_block": "0", + "muir_glacier_block": "0", + "berlin_block": "0", + "london_block": "0", + "arrow_glacier_block": "0", + "gray_glacier_block": "0", + "merge_netsplit_block": "0", + "shanghai_block": "0", + "cancun_block": "0" + }, + "allow_unprotected_txs": false + } + }, + "feegrant": { + "allowances": [] + }, + "feeibc": { + "identified_fees": [], + "fee_enabled_channels": [], + "registered_payees": [], + "registered_counterparty_payees": [], + "forward_relayers": [] + }, + "feemarket": { + "params": { + "no_base_fee": false, + "base_fee_change_denominator": 8, + "elasticity_multiplier": 2, + "enable_height": "0", + "base_fee": "100000000000", + "min_gas_price": "0.000000000000000000", + "min_gas_multiplier": "0.500000000000000000" + }, + "block_gas": "0" + }, + "genutil": { + "gen_txs": [ + { + "body": { + "messages": [ + { + "@type": "/cosmos.staking.v1beta1.MsgCreateValidator", + "description": { + "moniker": "node1", + "identity": "", + "website": "", + "security_contact": "", + "details": "" + }, + "commission": { + "rate": "0.100000000000000000", + "max_rate": "0.200000000000000000", + "max_change_rate": "0.010000000000000000" + }, + "min_self_delegation": "1", + "delegator_address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", + "validator_address": "crcvaloper18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjp0e0dh", + "pubkey": { + "@type": "/cosmos.crypto.ed25519.PubKey", + "key": "VYjUEP/dlIS1m5QVwzw4TwpGFXIbHwX+nXl0EilxXss=" + }, + "value": { + "denom": "stake", + "amount": "1000000000000000000" + } + } + ], + "memo": "03116e1d4dc9b09cdae5ae2b6a9512e37d1e000f@192.168.0.8:26656", + "timeout_height": "0", + "extension_options": [], + "non_critical_extension_options": [] + }, + "auth_info": { + "signer_infos": [ + { + "public_key": { + "@type": "/ethermint.crypto.v1.ethsecp256k1.PubKey", + "key": "AkJ4WnUHRFLWKmrCInD/uPsByTddC6coh66ADcYZMV0b" + }, + "mode_info": { + "single": { + "mode": "SIGN_MODE_DIRECT" + } + }, + "sequence": "0" + } + ], + "fee": { + "amount": [], + "gas_limit": "200000", + "payer": "", + "granter": "" + }, + "tip": null + }, + "signatures": [ + "vXMjRJSsXYoNEDnWKwu6P/O3PcaOAcLcbNmPraP5e1Euk7bE+0/2IiFAJV7iQsHuQYKA3lzS4Wtq9nmmjwXzYQE=" + ] + }, + { + "body": { + "messages": [ + { + "@type": "/cosmos.staking.v1beta1.MsgCreateValidator", + "description": { + "moniker": "node0", + "identity": "", + "website": "", + "security_contact": "", + "details": "" + }, + "commission": { + "rate": "0.100000000000000000", + "max_rate": "0.200000000000000000", + "max_change_rate": "0.010000000000000000" + }, + "min_self_delegation": "1", + "delegator_address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", + "validator_address": "crcvaloper12luku6uxehhak02py4rcz65zu0swh7wj6ulrlg", + "pubkey": { + "@type": "/cosmos.crypto.ed25519.PubKey", + "key": "XMFekpj1jUgxm8jx4PRiDGk8tqqmdPYEVBrHdk122Pw=" + }, + "value": { + "denom": "stake", + "amount": "1000000000000000000" + } + } + ], + "memo": "6bdd363882d10473b99c8c710661f79d38bd5ce6@192.168.0.8:26656", + "timeout_height": "0", + "extension_options": [], + "non_critical_extension_options": [] + }, + "auth_info": { + "signer_infos": [ + { + "public_key": { + "@type": "/ethermint.crypto.v1.ethsecp256k1.PubKey", + "key": "Am5xCmKjQt4O1NfEUy3Ly7r78ZZS7WeyN++rcOiyB++s" + }, + "mode_info": { + "single": { + "mode": "SIGN_MODE_DIRECT" + } + }, + "sequence": "0" + } + ], + "fee": { + "amount": [], + "gas_limit": "200000", + "payer": "", + "granter": "" + }, + "tip": null + }, + "signatures": [ + "xiDZO94CFnydjTKevKRg/lpDSAK0LO/nxrcdJRjnvaZA3znfV+YUwqBPr9GU+Jcxi+QBUg+Rp3JLq1HJR/it+gA=" + ] + } + ] + }, + "gov": { + "starting_proposal_id": "1", + "deposits": [], + "votes": [], + "proposals": [], + "deposit_params": null, + "voting_params": null, + "tally_params": null, + "params": { + "min_deposit": [ + { + "amount": "1", + "denom": "basetcro" + } + ], + "max_deposit_period": "10s", + "voting_period": "10s", + "quorum": "0.334000000000000000", + "threshold": "0.500000000000000000", + "veto_threshold": "0.334000000000000000", + "min_initial_deposit_ratio": "0.000000000000000000", + "burn_vote_quorum": false, + "burn_proposal_deposit_prevote": false, + "burn_vote_veto": true + } + }, + "gravity": { + "params": { + "gravity_id": "defaultgravityid", + "contract_source_hash": "", + "bridge_ethereum_address": "0x0000000000000000000000000000000000000000", + "bridge_chain_id": "0", + "signed_signer_set_txs_window": "10000", + "signed_batches_window": "10000", + "ethereum_signatures_window": "10000", + "target_eth_tx_timeout": "43200000", + "average_block_time": "5000", + "average_ethereum_block_time": "15000", + "slash_fraction_signer_set_tx": "0.001000000000000000", + "slash_fraction_batch": "0.001000000000000000", + "slash_fraction_ethereum_signature": "0.001000000000000000", + "slash_fraction_conflicting_ethereum_signature": "0.001000000000000000", + "unbond_slashing_signer_set_txs_window": "10000", + "bridge_active": true, + "batch_creation_period": "10", + "batch_max_element": "100", + "observe_ethereum_height_period": "50" + }, + "last_observed_event_nonce": "0", + "outgoing_txs": [], + "confirmations": [], + "ethereum_event_vote_records": [], + "delegate_keys": [], + "erc20_to_denoms": [], + "unbatched_send_to_ethereum_txs": [] + }, + "ibc": { + "client_genesis": { + "clients": [], + "clients_consensus": [], + "clients_metadata": [], + "params": { + "allowed_clients": [ + "06-solomachine", + "07-tendermint", + "09-localhost" + ] + }, + "create_localhost": false, + "next_client_sequence": "0" + }, + "connection_genesis": { + "connections": [], + "client_connection_paths": [], + "next_connection_sequence": "0", + "params": { + "max_expected_time_per_block": "30000000000" + } + }, + "channel_genesis": { + "channels": [], + "acknowledgements": [], + "commitments": [], + "receipts": [], + "send_sequences": [], + "recv_sequences": [], + "ack_sequences": [], + "next_channel_sequence": "0" + } + }, + "icaauth": { + "params": { + "min_timeout_duration": "3600s" + } + }, + "interchainaccounts": { + "controller_genesis_state": { + "active_channels": [], + "interchain_accounts": [], + "ports": [], + "params": { + "controller_enabled": true + } + }, + "host_genesis_state": { + "active_channels": [], + "interchain_accounts": [], + "port": "icahost", + "params": { + "host_enabled": true, + "allow_messages": [ + "*" + ] + } + } + }, + "mint": { + "minter": { + "inflation": "0.130000000000000000", + "annual_provisions": "0.000000000000000000" + }, + "params": { + "mint_denom": "stake", + "inflation_rate_change": "0.130000000000000000", + "inflation_max": "0.200000000000000000", + "inflation_min": "0.070000000000000000", + "goal_bonded": "0.670000000000000000", + "blocks_per_year": "6311520" + } + }, + "params": null, + "slashing": { + "params": { + "signed_blocks_window": "100", + "min_signed_per_window": "0.500000000000000000", + "downtime_jail_duration": "600s", + "slash_fraction_double_sign": "0.050000000000000000", + "slash_fraction_downtime": "0.010000000000000000" + }, + "signing_infos": [], + "missed_blocks": [] + }, + "staking": { + "params": { + "unbonding_time": "1814400s", + "max_validators": 100, + "max_entries": 7, + "historical_entries": 10000, + "bond_denom": "stake", + "min_commission_rate": "0.000000000000000000" + }, + "last_total_power": "0", + "last_validator_powers": [], + "validators": [], + "delegations": [], + "unbonding_delegations": [], + "redelegations": [], + "exported": false + }, + "transfer": { + "port_id": "transfer", + "denom_traces": [], + "params": { + "send_enabled": true, + "receive_enabled": true + }, + "total_escrowed": [] + }, + "upgrade": {}, + "vesting": {} + } +} +` + b.Run("new", func(b *testing.B) { + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, err := ParseChainIDFromGenesis(strings.NewReader(json)) + require.NoError(b, err) + } + }) + + b.Run("old", func(b *testing.B) { + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, err := genutiltypes.AppGenesisFromReader(strings.NewReader(json)) + require.NoError(b, err) + } + }) +} From 9c220f59d7d5e648163d8dce0e34442572c747d4 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Mon, 23 Oct 2023 15:41:12 +0800 Subject: [PATCH 06/22] mention encoding/json/v2 --- types/chain_id.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/chain_id.go b/types/chain_id.go index 5e15faab19a1..aa1be8bcab93 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -10,6 +10,8 @@ import ( const ChainIDFieldName = "chain_id" // ParseChainIDFromGenesis parses the chain-id from the genesis file using constant memory. +// +// TODO consider [encoding/json/v2](https://github.com/golang/go/discussions/63397) when it's ready. func ParseChainIDFromGenesis(reader io.Reader) (string, error) { decoder := jstream.NewDecoder(reader, 1).EmitKV() var chain_id string From c74ba3b7e4aa57d7c7542e9169975503f77af7b2 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Mon, 23 Oct 2023 15:45:15 +0800 Subject: [PATCH 07/22] fix error handling --- types/chain_id.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/types/chain_id.go b/types/chain_id.go index aa1be8bcab93..6841f764b8c2 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -14,11 +14,14 @@ const ChainIDFieldName = "chain_id" // TODO consider [encoding/json/v2](https://github.com/golang/go/discussions/63397) when it's ready. func ParseChainIDFromGenesis(reader io.Reader) (string, error) { decoder := jstream.NewDecoder(reader, 1).EmitKV() - var chain_id string + var ( + chain_id string + ok bool + ) err := decoder.Decode(func(mv *jstream.MetaValue) bool { if kv, ok := mv.Value.(jstream.KV); ok { if kv.Key == ChainIDFieldName { - chain_id, _ = kv.Value.(string) + chain_id, ok = kv.Value.(string) return false } } @@ -27,6 +30,9 @@ func ParseChainIDFromGenesis(reader io.Reader) (string, error) { if len(chain_id) > 0 { return chain_id, nil } + if !ok { + return "", errors.New("chain-id is not a string") + } if err == nil { return "", errors.New("chain-id not found in genesis file") } From 7680cdda04bf877fe9ca5ad4783bd0621836080b Mon Sep 17 00:00:00 2001 From: HuangYi Date: Mon, 23 Oct 2023 16:06:46 +0800 Subject: [PATCH 08/22] fix shadow var --- types/chain_id.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/chain_id.go b/types/chain_id.go index 6841f764b8c2..9351df4b477a 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -15,13 +15,13 @@ const ChainIDFieldName = "chain_id" func ParseChainIDFromGenesis(reader io.Reader) (string, error) { decoder := jstream.NewDecoder(reader, 1).EmitKV() var ( - chain_id string - ok bool + chain_id string + chain_id_ok bool ) err := decoder.Decode(func(mv *jstream.MetaValue) bool { if kv, ok := mv.Value.(jstream.KV); ok { if kv.Key == ChainIDFieldName { - chain_id, ok = kv.Value.(string) + chain_id, chain_id_ok = kv.Value.(string) return false } } @@ -30,7 +30,7 @@ func ParseChainIDFromGenesis(reader io.Reader) (string, error) { if len(chain_id) > 0 { return chain_id, nil } - if !ok { + if !chain_id_ok { return "", errors.New("chain-id is not a string") } if err == nil { From 1035e32bad3b9d7c5146c69f06068a693db644c4 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Tue, 24 Oct 2023 07:54:18 +0800 Subject: [PATCH 09/22] use simpler solution with different trade off --- go.mod | 2 - go.sum | 2 - types/chain_id.go | 76 +++- types/chain_id_test.go | 597 ++----------------------- types/testdata/streaming_chain_id.json | 549 +++++++++++++++++++++++ 5 files changed, 633 insertions(+), 593 deletions(-) create mode 100644 types/testdata/streaming_chain_id.json diff --git a/go.mod b/go.mod index 2cddfba56418..591fe58f73c3 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,6 @@ require ( cosmossdk.io/x/protocolpool v0.0.0-20230925135524-a1bc045b3190 cosmossdk.io/x/tx v0.11.0 github.com/99designs/keyring v1.2.1 - github.com/bcicen/jstream v1.0.1 github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 github.com/bits-and-blooms/bitset v1.10.0 github.com/chzyer/readline v1.5.1 @@ -174,7 +173,6 @@ replace cosmossdk.io/x/protocolpool => ./x/protocolpool replace ( // use cosmos fork of keyring github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0 - github.com/bcicen/jstream => github.com/yihuang/jstream v0.0.0-20231023072018-cb6eaf5ca571 // dgrijalva/jwt-go is deprecated and doesn't receive security updates. // TODO: remove it: https://github.com/cosmos/cosmos-sdk/issues/13134 github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2 diff --git a/go.sum b/go.sum index e807b9cd916a..6bc253effcdd 100644 --- a/go.sum +++ b/go.sum @@ -792,8 +792,6 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= -github.com/yihuang/jstream v0.0.0-20231023072018-cb6eaf5ca571 h1:J0wQbFhjC6ZLJlNCIcWSEpWuiBQ/wdvaDXJSBzpoeyQ= -github.com/yihuang/jstream v0.0.0-20231023072018-cb6eaf5ca571/go.mod h1:9ielPxqFry7Y4Tg3j4BfjPocfJ3TbsRtXOAYXYmRuAQ= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/types/chain_id.go b/types/chain_id.go index 9351df4b477a..f74e4a880f67 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -1,40 +1,68 @@ package types import ( + "encoding/json" "errors" + fmt "fmt" "io" - "github.com/bcicen/jstream" + "github.com/cometbft/cometbft/types" ) const ChainIDFieldName = "chain_id" -// ParseChainIDFromGenesis parses the chain-id from the genesis file using constant memory. -// -// TODO consider [encoding/json/v2](https://github.com/golang/go/discussions/63397) when it's ready. -func ParseChainIDFromGenesis(reader io.Reader) (string, error) { - decoder := jstream.NewDecoder(reader, 1).EmitKV() - var ( - chain_id string - chain_id_ok bool - ) - err := decoder.Decode(func(mv *jstream.MetaValue) bool { - if kv, ok := mv.Value.(jstream.KV); ok { - if kv.Key == ChainIDFieldName { - chain_id, chain_id_ok = kv.Value.(string) - return false +// ParseChainIDFromGenesis parses the `chain_id` from the genesis json file and abort early, +// it still parses the values before the `chain_id` field, particularly if the `app_state` field is +// before the `chain_id` field, it will parse the `app_state` value, user must make sure the `chain_id` +// is put before `app_state` or other big entries to enjoy the efficiency. +func ParseChainIDFromGenesis(r io.Reader) (string, error) { + dec := json.NewDecoder(r) + + var t json.Token + t, err := dec.Token() + if err != nil { + return "", err + } + if t != json.Delim('{') { + return "", fmt.Errorf("expected {, got %s", t) + } + + for dec.More() { + t, err = dec.Token() + if err != nil { + return "", err + } + key, ok := t.(string) + if !ok { + return "", fmt.Errorf("expected string, got %s", t) + } + var value interface{} + err = dec.Decode(&value) + if err != nil { + return "", err + } + if key == ChainIDFieldName { + chainId, ok := value.(string) + if !ok { + return "", fmt.Errorf("expected string chain_id, got %s", value) + } + if err := validateChainID(chainId); err != nil { + return "", err } + return chainId, nil } - return true - }) - if len(chain_id) > 0 { - return chain_id, nil } - if !chain_id_ok { - return "", errors.New("chain-id is not a string") + + return "", errors.New("missing chain-id in genesis file") +} + +func validateChainID(chainID string) error { + if chainID == "" { + return errors.New("genesis doc must include non-empty chain_id") } - if err == nil { - return "", errors.New("chain-id not found in genesis file") + if len(chainID) > types.MaxChainIDLen { + return fmt.Errorf("chain_id in genesis doc is too long (max: %d)", types.MaxChainIDLen) } - return "", err + + return nil } diff --git a/types/chain_id_test.go b/types/chain_id_test.go index 9aa324392b47..b7481948baf4 100644 --- a/types/chain_id_test.go +++ b/types/chain_id_test.go @@ -1,13 +1,17 @@ package types import ( - strings "strings" + _ "embed" + "strings" "testing" genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" "github.com/stretchr/testify/require" ) +//go:embed testdata/streaming_chain_id.json +var BenchmarkGenesis string + func TestParseChainIDFromGenesis(t *testing.T) { testCases := []struct { name string @@ -26,6 +30,18 @@ func TestParseChainIDFromGenesis(t *testing.T) { }`, "test-chain-id", }, + { + "nested", + `{ + "state": { + "accounts": { + "a": {} + }, + "chain_id": "test-chain-id" + } + }`, + "", + }, { "not exist", `{ @@ -50,6 +66,16 @@ func TestParseChainIDFromGenesis(t *testing.T) { `[ " ': }`, "", }, + { + "empty chain_id", + `{"chain_id": ""}`, + "", + }, + { + "chain_id too long", + `{"chain_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}`, + "", + }, } for _, tc := range testCases { @@ -66,581 +92,22 @@ func TestParseChainIDFromGenesis(t *testing.T) { } func BenchmarkParseChainID(b *testing.B) { - // an arbitrary genesis file from a cronos devnet. - json := ` -{ - "genesis_time": "2023-09-27T05:35:43.213433Z", - "chain_id": "cronos_777-1", - "initial_height": "1", - "consensus_params": { - "block": { - "max_bytes": "1048576", - "max_gas": "81500000" - }, - "evidence": { - "max_age_num_blocks": "100000", - "max_age_duration": "172800000000000", - "max_bytes": "1048576" - }, - "validator": { - "pub_key_types": [ - "ed25519" - ] - }, - "version": { - "app": "0" - } - }, - "app_hash": "", - "app_state": { - "07-tendermint": null, - "auth": { - "params": { - "max_memo_characters": "256", - "tx_sig_limit": "7", - "tx_size_cost_per_byte": "10", - "sig_verify_cost_ed25519": "590", - "sig_verify_cost_secp256k1": "1000" - }, - "accounts": [ - { - "@type": "/ethermint.types.v1.EthAccount", - "base_account": { - "address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", - "pub_key": null, - "account_number": "0", - "sequence": "0" - }, - "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - }, - { - "@type": "/ethermint.types.v1.EthAccount", - "base_account": { - "address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", - "pub_key": null, - "account_number": "1", - "sequence": "0" - }, - "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - }, - { - "@type": "/ethermint.types.v1.EthAccount", - "base_account": { - "address": "crc1x7x9pkfxf33l87ftspk5aetwnkr0lvlv3346cd", - "pub_key": null, - "account_number": "2", - "sequence": "0" - }, - "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - }, - { - "@type": "/ethermint.types.v1.EthAccount", - "base_account": { - "address": "crc16z0herz998946wr659lr84c8c556da55dc34hh", - "pub_key": null, - "account_number": "3", - "sequence": "0" - }, - "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - }, - { - "@type": "/ethermint.types.v1.EthAccount", - "base_account": { - "address": "crc1q04jewhxw4xxu3vlg3rc85240h9q7ns6hglz0g", - "pub_key": null, - "account_number": "4", - "sequence": "0" - }, - "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - } - ] - }, - "authz": { - "authorization": [] - }, - "bank": { - "params": { - "send_enabled": [], - "default_send_enabled": true - }, - "balances": [ - { - "address": "crc1q04jewhxw4xxu3vlg3rc85240h9q7ns6hglz0g", - "coins": [ - { - "denom": "basetcro", - "amount": "30000000000000000000000" - } - ] - }, - { - "address": "crc1x7x9pkfxf33l87ftspk5aetwnkr0lvlv3346cd", - "coins": [ - { - "denom": "basetcro", - "amount": "10000000000000000000000" - } - ] - }, - { - "address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", - "coins": [ - { - "denom": "basetcro", - "amount": "10000000000000000000000" - }, - { - "denom": "stake", - "amount": "1000000000000000000" - } - ] - }, - { - "address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", - "coins": [ - { - "denom": "basetcro", - "amount": "10000000000000000000000" - }, - { - "denom": "stake", - "amount": "1000000000000000000" - } - ] - }, - { - "address": "crc16z0herz998946wr659lr84c8c556da55dc34hh", - "coins": [ - { - "denom": "basetcro", - "amount": "20000000000000000000000" - } - ] - } - ], - "supply": [ - { - "denom": "basetcro", - "amount": "80000000000000000000000" - }, - { - "denom": "stake", - "amount": "2000000000000000000" - } - ], - "denom_metadata": [], - "send_enabled": [] - }, - "capability": { - "index": "1", - "owners": [] - }, - "consensus": null, - "crisis": { - "constant_fee": { - "denom": "stake", - "amount": "1000" - } - }, - "cronos": { - "params": { - "ibc_cro_denom": "ibc/6411AE2ADA1E73DB59DB151A8988F9B7D5E7E233D8414DB6817F8F1A01611F86", - "ibc_timeout": "86400000000000", - "cronos_admin": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", - "enable_auto_deployment": true - }, - "external_contracts": [], - "auto_contracts": [] - }, - "distribution": { - "params": { - "community_tax": "0.020000000000000000", - "base_proposer_reward": "0.000000000000000000", - "bonus_proposer_reward": "0.000000000000000000", - "withdraw_addr_enabled": true - }, - "fee_pool": { - "community_pool": [] - }, - "delegator_withdraw_infos": [], - "previous_proposer": "", - "outstanding_rewards": [], - "validator_accumulated_commissions": [], - "validator_historical_rewards": [], - "validator_current_rewards": [], - "delegator_starting_infos": [], - "validator_slash_events": [] - }, - "evidence": { - "evidence": [] - }, - "evm": { - "accounts": [], - "params": { - "evm_denom": "basetcro", - "enable_create": true, - "enable_call": true, - "extra_eips": [], - "chain_config": { - "homestead_block": "0", - "dao_fork_block": "0", - "dao_fork_support": true, - "eip150_block": "0", - "eip150_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "eip155_block": "0", - "eip158_block": "0", - "byzantium_block": "0", - "constantinople_block": "0", - "petersburg_block": "0", - "istanbul_block": "0", - "muir_glacier_block": "0", - "berlin_block": "0", - "london_block": "0", - "arrow_glacier_block": "0", - "gray_glacier_block": "0", - "merge_netsplit_block": "0", - "shanghai_block": "0", - "cancun_block": "0" - }, - "allow_unprotected_txs": false - } - }, - "feegrant": { - "allowances": [] - }, - "feeibc": { - "identified_fees": [], - "fee_enabled_channels": [], - "registered_payees": [], - "registered_counterparty_payees": [], - "forward_relayers": [] - }, - "feemarket": { - "params": { - "no_base_fee": false, - "base_fee_change_denominator": 8, - "elasticity_multiplier": 2, - "enable_height": "0", - "base_fee": "100000000000", - "min_gas_price": "0.000000000000000000", - "min_gas_multiplier": "0.500000000000000000" - }, - "block_gas": "0" - }, - "genutil": { - "gen_txs": [ - { - "body": { - "messages": [ - { - "@type": "/cosmos.staking.v1beta1.MsgCreateValidator", - "description": { - "moniker": "node1", - "identity": "", - "website": "", - "security_contact": "", - "details": "" - }, - "commission": { - "rate": "0.100000000000000000", - "max_rate": "0.200000000000000000", - "max_change_rate": "0.010000000000000000" - }, - "min_self_delegation": "1", - "delegator_address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", - "validator_address": "crcvaloper18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjp0e0dh", - "pubkey": { - "@type": "/cosmos.crypto.ed25519.PubKey", - "key": "VYjUEP/dlIS1m5QVwzw4TwpGFXIbHwX+nXl0EilxXss=" - }, - "value": { - "denom": "stake", - "amount": "1000000000000000000" - } - } - ], - "memo": "03116e1d4dc9b09cdae5ae2b6a9512e37d1e000f@192.168.0.8:26656", - "timeout_height": "0", - "extension_options": [], - "non_critical_extension_options": [] - }, - "auth_info": { - "signer_infos": [ - { - "public_key": { - "@type": "/ethermint.crypto.v1.ethsecp256k1.PubKey", - "key": "AkJ4WnUHRFLWKmrCInD/uPsByTddC6coh66ADcYZMV0b" - }, - "mode_info": { - "single": { - "mode": "SIGN_MODE_DIRECT" - } - }, - "sequence": "0" - } - ], - "fee": { - "amount": [], - "gas_limit": "200000", - "payer": "", - "granter": "" - }, - "tip": null - }, - "signatures": [ - "vXMjRJSsXYoNEDnWKwu6P/O3PcaOAcLcbNmPraP5e1Euk7bE+0/2IiFAJV7iQsHuQYKA3lzS4Wtq9nmmjwXzYQE=" - ] - }, - { - "body": { - "messages": [ - { - "@type": "/cosmos.staking.v1beta1.MsgCreateValidator", - "description": { - "moniker": "node0", - "identity": "", - "website": "", - "security_contact": "", - "details": "" - }, - "commission": { - "rate": "0.100000000000000000", - "max_rate": "0.200000000000000000", - "max_change_rate": "0.010000000000000000" - }, - "min_self_delegation": "1", - "delegator_address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", - "validator_address": "crcvaloper12luku6uxehhak02py4rcz65zu0swh7wj6ulrlg", - "pubkey": { - "@type": "/cosmos.crypto.ed25519.PubKey", - "key": "XMFekpj1jUgxm8jx4PRiDGk8tqqmdPYEVBrHdk122Pw=" - }, - "value": { - "denom": "stake", - "amount": "1000000000000000000" - } - } - ], - "memo": "6bdd363882d10473b99c8c710661f79d38bd5ce6@192.168.0.8:26656", - "timeout_height": "0", - "extension_options": [], - "non_critical_extension_options": [] - }, - "auth_info": { - "signer_infos": [ - { - "public_key": { - "@type": "/ethermint.crypto.v1.ethsecp256k1.PubKey", - "key": "Am5xCmKjQt4O1NfEUy3Ly7r78ZZS7WeyN++rcOiyB++s" - }, - "mode_info": { - "single": { - "mode": "SIGN_MODE_DIRECT" - } - }, - "sequence": "0" - } - ], - "fee": { - "amount": [], - "gas_limit": "200000", - "payer": "", - "granter": "" - }, - "tip": null - }, - "signatures": [ - "xiDZO94CFnydjTKevKRg/lpDSAK0LO/nxrcdJRjnvaZA3znfV+YUwqBPr9GU+Jcxi+QBUg+Rp3JLq1HJR/it+gA=" - ] - } - ] - }, - "gov": { - "starting_proposal_id": "1", - "deposits": [], - "votes": [], - "proposals": [], - "deposit_params": null, - "voting_params": null, - "tally_params": null, - "params": { - "min_deposit": [ - { - "amount": "1", - "denom": "basetcro" - } - ], - "max_deposit_period": "10s", - "voting_period": "10s", - "quorum": "0.334000000000000000", - "threshold": "0.500000000000000000", - "veto_threshold": "0.334000000000000000", - "min_initial_deposit_ratio": "0.000000000000000000", - "burn_vote_quorum": false, - "burn_proposal_deposit_prevote": false, - "burn_vote_veto": true - } - }, - "gravity": { - "params": { - "gravity_id": "defaultgravityid", - "contract_source_hash": "", - "bridge_ethereum_address": "0x0000000000000000000000000000000000000000", - "bridge_chain_id": "0", - "signed_signer_set_txs_window": "10000", - "signed_batches_window": "10000", - "ethereum_signatures_window": "10000", - "target_eth_tx_timeout": "43200000", - "average_block_time": "5000", - "average_ethereum_block_time": "15000", - "slash_fraction_signer_set_tx": "0.001000000000000000", - "slash_fraction_batch": "0.001000000000000000", - "slash_fraction_ethereum_signature": "0.001000000000000000", - "slash_fraction_conflicting_ethereum_signature": "0.001000000000000000", - "unbond_slashing_signer_set_txs_window": "10000", - "bridge_active": true, - "batch_creation_period": "10", - "batch_max_element": "100", - "observe_ethereum_height_period": "50" - }, - "last_observed_event_nonce": "0", - "outgoing_txs": [], - "confirmations": [], - "ethereum_event_vote_records": [], - "delegate_keys": [], - "erc20_to_denoms": [], - "unbatched_send_to_ethereum_txs": [] - }, - "ibc": { - "client_genesis": { - "clients": [], - "clients_consensus": [], - "clients_metadata": [], - "params": { - "allowed_clients": [ - "06-solomachine", - "07-tendermint", - "09-localhost" - ] - }, - "create_localhost": false, - "next_client_sequence": "0" - }, - "connection_genesis": { - "connections": [], - "client_connection_paths": [], - "next_connection_sequence": "0", - "params": { - "max_expected_time_per_block": "30000000000" - } - }, - "channel_genesis": { - "channels": [], - "acknowledgements": [], - "commitments": [], - "receipts": [], - "send_sequences": [], - "recv_sequences": [], - "ack_sequences": [], - "next_channel_sequence": "0" - } - }, - "icaauth": { - "params": { - "min_timeout_duration": "3600s" - } - }, - "interchainaccounts": { - "controller_genesis_state": { - "active_channels": [], - "interchain_accounts": [], - "ports": [], - "params": { - "controller_enabled": true - } - }, - "host_genesis_state": { - "active_channels": [], - "interchain_accounts": [], - "port": "icahost", - "params": { - "host_enabled": true, - "allow_messages": [ - "*" - ] - } - } - }, - "mint": { - "minter": { - "inflation": "0.130000000000000000", - "annual_provisions": "0.000000000000000000" - }, - "params": { - "mint_denom": "stake", - "inflation_rate_change": "0.130000000000000000", - "inflation_max": "0.200000000000000000", - "inflation_min": "0.070000000000000000", - "goal_bonded": "0.670000000000000000", - "blocks_per_year": "6311520" - } - }, - "params": null, - "slashing": { - "params": { - "signed_blocks_window": "100", - "min_signed_per_window": "0.500000000000000000", - "downtime_jail_duration": "600s", - "slash_fraction_double_sign": "0.050000000000000000", - "slash_fraction_downtime": "0.010000000000000000" - }, - "signing_infos": [], - "missed_blocks": [] - }, - "staking": { - "params": { - "unbonding_time": "1814400s", - "max_validators": 100, - "max_entries": 7, - "historical_entries": 10000, - "bond_denom": "stake", - "min_commission_rate": "0.000000000000000000" - }, - "last_total_power": "0", - "last_validator_powers": [], - "validators": [], - "delegations": [], - "unbonding_delegations": [], - "redelegations": [], - "exported": false - }, - "transfer": { - "port_id": "transfer", - "denom_traces": [], - "params": { - "send_enabled": true, - "receive_enabled": true - }, - "total_escrowed": [] - }, - "upgrade": {}, - "vesting": {} - } -} -` + b.ReportAllocs() b.Run("new", func(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - _, err := ParseChainIDFromGenesis(strings.NewReader(json)) + chainId, err := ParseChainIDFromGenesis(strings.NewReader(BenchmarkGenesis)) require.NoError(b, err) + require.Equal(b, "test_777-1", chainId) } }) b.Run("old", func(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - _, err := genutiltypes.AppGenesisFromReader(strings.NewReader(json)) + doc, err := genutiltypes.AppGenesisFromReader(strings.NewReader(BenchmarkGenesis)) require.NoError(b, err) + require.Equal(b, "test_777-1", doc.ChainID) } }) } diff --git a/types/testdata/streaming_chain_id.json b/types/testdata/streaming_chain_id.json new file mode 100644 index 000000000000..bb08a716473d --- /dev/null +++ b/types/testdata/streaming_chain_id.json @@ -0,0 +1,549 @@ +{ + "genesis_time": "2023-09-27T05:35:43.213433Z", + "chain_id": "test_777-1", + "initial_height": "1", + "consensus_params": { + "block": { + "max_bytes": "1048576", + "max_gas": "81500000" + }, + "evidence": { + "max_age_num_blocks": "100000", + "max_age_duration": "172800000000000", + "max_bytes": "1048576" + }, + "validator": { + "pub_key_types": [ + "ed25519" + ] + }, + "version": { + "app": "0" + } + }, + "app_hash": "", + "app_state": { + "07-tendermint": null, + "auth": { + "params": { + "max_memo_characters": "256", + "tx_sig_limit": "7", + "tx_size_cost_per_byte": "10", + "sig_verify_cost_ed25519": "590", + "sig_verify_cost_secp256k1": "1000" + }, + "accounts": [ + { + "@type": "/ethermint.types.v1.EthAccount", + "base_account": { + "address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", + "pub_key": null, + "account_number": "0", + "sequence": "0" + }, + "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + }, + { + "@type": "/ethermint.types.v1.EthAccount", + "base_account": { + "address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", + "pub_key": null, + "account_number": "1", + "sequence": "0" + }, + "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + }, + { + "@type": "/ethermint.types.v1.EthAccount", + "base_account": { + "address": "crc1x7x9pkfxf33l87ftspk5aetwnkr0lvlv3346cd", + "pub_key": null, + "account_number": "2", + "sequence": "0" + }, + "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + }, + { + "@type": "/ethermint.types.v1.EthAccount", + "base_account": { + "address": "crc16z0herz998946wr659lr84c8c556da55dc34hh", + "pub_key": null, + "account_number": "3", + "sequence": "0" + }, + "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + }, + { + "@type": "/ethermint.types.v1.EthAccount", + "base_account": { + "address": "crc1q04jewhxw4xxu3vlg3rc85240h9q7ns6hglz0g", + "pub_key": null, + "account_number": "4", + "sequence": "0" + }, + "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + } + ] + }, + "authz": { + "authorization": [] + }, + "bank": { + "params": { + "send_enabled": [], + "default_send_enabled": true + }, + "balances": [ + { + "address": "crc1q04jewhxw4xxu3vlg3rc85240h9q7ns6hglz0g", + "coins": [ + { + "denom": "basetcro", + "amount": "30000000000000000000000" + } + ] + }, + { + "address": "crc1x7x9pkfxf33l87ftspk5aetwnkr0lvlv3346cd", + "coins": [ + { + "denom": "basetcro", + "amount": "10000000000000000000000" + } + ] + }, + { + "address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", + "coins": [ + { + "denom": "basetcro", + "amount": "10000000000000000000000" + }, + { + "denom": "stake", + "amount": "1000000000000000000" + } + ] + }, + { + "address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", + "coins": [ + { + "denom": "basetcro", + "amount": "10000000000000000000000" + }, + { + "denom": "stake", + "amount": "1000000000000000000" + } + ] + }, + { + "address": "crc16z0herz998946wr659lr84c8c556da55dc34hh", + "coins": [ + { + "denom": "basetcro", + "amount": "20000000000000000000000" + } + ] + } + ], + "supply": [ + { + "denom": "basetcro", + "amount": "80000000000000000000000" + }, + { + "denom": "stake", + "amount": "2000000000000000000" + } + ], + "denom_metadata": [], + "send_enabled": [] + }, + "capability": { + "index": "1", + "owners": [] + }, + "consensus": null, + "crisis": { + "constant_fee": { + "denom": "stake", + "amount": "1000" + } + }, + "distribution": { + "params": { + "community_tax": "0.020000000000000000", + "base_proposer_reward": "0.000000000000000000", + "bonus_proposer_reward": "0.000000000000000000", + "withdraw_addr_enabled": true + }, + "fee_pool": { + "community_pool": [] + }, + "delegator_withdraw_infos": [], + "previous_proposer": "", + "outstanding_rewards": [], + "validator_accumulated_commissions": [], + "validator_historical_rewards": [], + "validator_current_rewards": [], + "delegator_starting_infos": [], + "validator_slash_events": [] + }, + "evidence": { + "evidence": [] + }, + "evm": { + "accounts": [], + "params": { + "evm_denom": "basetcro", + "enable_create": true, + "enable_call": true, + "extra_eips": [], + "chain_config": { + "homestead_block": "0", + "dao_fork_block": "0", + "dao_fork_support": true, + "eip150_block": "0", + "eip150_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "eip155_block": "0", + "eip158_block": "0", + "byzantium_block": "0", + "constantinople_block": "0", + "petersburg_block": "0", + "istanbul_block": "0", + "muir_glacier_block": "0", + "berlin_block": "0", + "london_block": "0", + "arrow_glacier_block": "0", + "gray_glacier_block": "0", + "merge_netsplit_block": "0", + "shanghai_block": "0", + "cancun_block": "0" + }, + "allow_unprotected_txs": false + } + }, + "feegrant": { + "allowances": [] + }, + "feeibc": { + "identified_fees": [], + "fee_enabled_channels": [], + "registered_payees": [], + "registered_counterparty_payees": [], + "forward_relayers": [] + }, + "feemarket": { + "params": { + "no_base_fee": false, + "base_fee_change_denominator": 8, + "elasticity_multiplier": 2, + "enable_height": "0", + "base_fee": "100000000000", + "min_gas_price": "0.000000000000000000", + "min_gas_multiplier": "0.500000000000000000" + }, + "block_gas": "0" + }, + "genutil": { + "gen_txs": [ + { + "body": { + "messages": [ + { + "@type": "/cosmos.staking.v1beta1.MsgCreateValidator", + "description": { + "moniker": "node1", + "identity": "", + "website": "", + "security_contact": "", + "details": "" + }, + "commission": { + "rate": "0.100000000000000000", + "max_rate": "0.200000000000000000", + "max_change_rate": "0.010000000000000000" + }, + "min_self_delegation": "1", + "delegator_address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", + "validator_address": "crcvaloper18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjp0e0dh", + "pubkey": { + "@type": "/cosmos.crypto.ed25519.PubKey", + "key": "VYjUEP/dlIS1m5QVwzw4TwpGFXIbHwX+nXl0EilxXss=" + }, + "value": { + "denom": "stake", + "amount": "1000000000000000000" + } + } + ], + "memo": "03116e1d4dc9b09cdae5ae2b6a9512e37d1e000f@192.168.0.8:26656", + "timeout_height": "0", + "extension_options": [], + "non_critical_extension_options": [] + }, + "auth_info": { + "signer_infos": [ + { + "public_key": { + "@type": "/ethermint.crypto.v1.ethsecp256k1.PubKey", + "key": "AkJ4WnUHRFLWKmrCInD/uPsByTddC6coh66ADcYZMV0b" + }, + "mode_info": { + "single": { + "mode": "SIGN_MODE_DIRECT" + } + }, + "sequence": "0" + } + ], + "fee": { + "amount": [], + "gas_limit": "200000", + "payer": "", + "granter": "" + }, + "tip": null + }, + "signatures": [ + "vXMjRJSsXYoNEDnWKwu6P/O3PcaOAcLcbNmPraP5e1Euk7bE+0/2IiFAJV7iQsHuQYKA3lzS4Wtq9nmmjwXzYQE=" + ] + }, + { + "body": { + "messages": [ + { + "@type": "/cosmos.staking.v1beta1.MsgCreateValidator", + "description": { + "moniker": "node0", + "identity": "", + "website": "", + "security_contact": "", + "details": "" + }, + "commission": { + "rate": "0.100000000000000000", + "max_rate": "0.200000000000000000", + "max_change_rate": "0.010000000000000000" + }, + "min_self_delegation": "1", + "delegator_address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", + "validator_address": "crcvaloper12luku6uxehhak02py4rcz65zu0swh7wj6ulrlg", + "pubkey": { + "@type": "/cosmos.crypto.ed25519.PubKey", + "key": "XMFekpj1jUgxm8jx4PRiDGk8tqqmdPYEVBrHdk122Pw=" + }, + "value": { + "denom": "stake", + "amount": "1000000000000000000" + } + } + ], + "memo": "6bdd363882d10473b99c8c710661f79d38bd5ce6@192.168.0.8:26656", + "timeout_height": "0", + "extension_options": [], + "non_critical_extension_options": [] + }, + "auth_info": { + "signer_infos": [ + { + "public_key": { + "@type": "/ethermint.crypto.v1.ethsecp256k1.PubKey", + "key": "Am5xCmKjQt4O1NfEUy3Ly7r78ZZS7WeyN++rcOiyB++s" + }, + "mode_info": { + "single": { + "mode": "SIGN_MODE_DIRECT" + } + }, + "sequence": "0" + } + ], + "fee": { + "amount": [], + "gas_limit": "200000", + "payer": "", + "granter": "" + }, + "tip": null + }, + "signatures": [ + "xiDZO94CFnydjTKevKRg/lpDSAK0LO/nxrcdJRjnvaZA3znfV+YUwqBPr9GU+Jcxi+QBUg+Rp3JLq1HJR/it+gA=" + ] + } + ] + }, + "gov": { + "starting_proposal_id": "1", + "deposits": [], + "votes": [], + "proposals": [], + "deposit_params": null, + "voting_params": null, + "tally_params": null, + "params": { + "min_deposit": [ + { + "amount": "1", + "denom": "basetcro" + } + ], + "max_deposit_period": "10s", + "voting_period": "10s", + "quorum": "0.334000000000000000", + "threshold": "0.500000000000000000", + "veto_threshold": "0.334000000000000000", + "min_initial_deposit_ratio": "0.000000000000000000", + "burn_vote_quorum": false, + "burn_proposal_deposit_prevote": false, + "burn_vote_veto": true + } + }, + "gravity": { + "params": { + "gravity_id": "defaultgravityid", + "contract_source_hash": "", + "bridge_ethereum_address": "0x0000000000000000000000000000000000000000", + "bridge_chain_id": "0", + "signed_signer_set_txs_window": "10000", + "signed_batches_window": "10000", + "ethereum_signatures_window": "10000", + "target_eth_tx_timeout": "43200000", + "average_block_time": "5000", + "average_ethereum_block_time": "15000", + "slash_fraction_signer_set_tx": "0.001000000000000000", + "slash_fraction_batch": "0.001000000000000000", + "slash_fraction_ethereum_signature": "0.001000000000000000", + "slash_fraction_conflicting_ethereum_signature": "0.001000000000000000", + "unbond_slashing_signer_set_txs_window": "10000", + "bridge_active": true, + "batch_creation_period": "10", + "batch_max_element": "100", + "observe_ethereum_height_period": "50" + }, + "last_observed_event_nonce": "0", + "outgoing_txs": [], + "confirmations": [], + "ethereum_event_vote_records": [], + "delegate_keys": [], + "erc20_to_denoms": [], + "unbatched_send_to_ethereum_txs": [] + }, + "ibc": { + "client_genesis": { + "clients": [], + "clients_consensus": [], + "clients_metadata": [], + "params": { + "allowed_clients": [ + "06-solomachine", + "07-tendermint", + "09-localhost" + ] + }, + "create_localhost": false, + "next_client_sequence": "0" + }, + "connection_genesis": { + "connections": [], + "client_connection_paths": [], + "next_connection_sequence": "0", + "params": { + "max_expected_time_per_block": "30000000000" + } + }, + "channel_genesis": { + "channels": [], + "acknowledgements": [], + "commitments": [], + "receipts": [], + "send_sequences": [], + "recv_sequences": [], + "ack_sequences": [], + "next_channel_sequence": "0" + } + }, + "icaauth": { + "params": { + "min_timeout_duration": "3600s" + } + }, + "interchainaccounts": { + "controller_genesis_state": { + "active_channels": [], + "interchain_accounts": [], + "ports": [], + "params": { + "controller_enabled": true + } + }, + "host_genesis_state": { + "active_channels": [], + "interchain_accounts": [], + "port": "icahost", + "params": { + "host_enabled": true, + "allow_messages": [ + "*" + ] + } + } + }, + "mint": { + "minter": { + "inflation": "0.130000000000000000", + "annual_provisions": "0.000000000000000000" + }, + "params": { + "mint_denom": "stake", + "inflation_rate_change": "0.130000000000000000", + "inflation_max": "0.200000000000000000", + "inflation_min": "0.070000000000000000", + "goal_bonded": "0.670000000000000000", + "blocks_per_year": "6311520" + } + }, + "params": null, + "slashing": { + "params": { + "signed_blocks_window": "100", + "min_signed_per_window": "0.500000000000000000", + "downtime_jail_duration": "600s", + "slash_fraction_double_sign": "0.050000000000000000", + "slash_fraction_downtime": "0.010000000000000000" + }, + "signing_infos": [], + "missed_blocks": [] + }, + "staking": { + "params": { + "unbonding_time": "1814400s", + "max_validators": 100, + "max_entries": 7, + "historical_entries": 10000, + "bond_denom": "stake", + "min_commission_rate": "0.000000000000000000" + }, + "last_total_power": "0", + "last_validator_powers": [], + "validators": [], + "delegations": [], + "unbonding_delegations": [], + "redelegations": [], + "exported": false + }, + "transfer": { + "port_id": "transfer", + "denom_traces": [], + "params": { + "send_enabled": true, + "receive_enabled": true + }, + "total_escrowed": [] + }, + "upgrade": {}, + "vesting": {} + } +} From be3a3f7a8ac7dd979a75efe3557a9357e21bae1d Mon Sep 17 00:00:00 2001 From: HuangYi Date: Tue, 24 Oct 2023 09:12:33 +0800 Subject: [PATCH 10/22] cleanup --- types/chain_id.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/chain_id.go b/types/chain_id.go index f74e4a880f67..912c64b28fbb 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -3,7 +3,7 @@ package types import ( "encoding/json" "errors" - fmt "fmt" + "fmt" "io" "github.com/cometbft/cometbft/types" From 7cd078754276671953808f9ac9a8abc838c3b129 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Tue, 24 Oct 2023 09:22:27 +0800 Subject: [PATCH 11/22] skip value more efficiently --- types/chain_id.go | 18 ++++++++++-------- types/chain_id_test.go | 2 +- ...aming_chain_id.json => parse_chain_id.json} | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) rename types/testdata/{streaming_chain_id.json => parse_chain_id.json} (100%) diff --git a/types/chain_id.go b/types/chain_id.go index 912c64b28fbb..6d1f4a994ab6 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -36,21 +36,23 @@ func ParseChainIDFromGenesis(r io.Reader) (string, error) { if !ok { return "", fmt.Errorf("expected string, got %s", t) } - var value interface{} - err = dec.Decode(&value) - if err != nil { - return "", err - } + if key == ChainIDFieldName { - chainId, ok := value.(string) - if !ok { - return "", fmt.Errorf("expected string chain_id, got %s", value) + var chainId string + if err := dec.Decode(&chainId); err != nil { + return "", err } if err := validateChainID(chainId); err != nil { return "", err } return chainId, nil } + + // skip the value + var value json.RawMessage + if err := dec.Decode(&value); err != nil { + return "", err + } } return "", errors.New("missing chain-id in genesis file") diff --git a/types/chain_id_test.go b/types/chain_id_test.go index b7481948baf4..d476823094cf 100644 --- a/types/chain_id_test.go +++ b/types/chain_id_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/require" ) -//go:embed testdata/streaming_chain_id.json +//go:embed testdata/parse_chain_id.json var BenchmarkGenesis string func TestParseChainIDFromGenesis(t *testing.T) { diff --git a/types/testdata/streaming_chain_id.json b/types/testdata/parse_chain_id.json similarity index 100% rename from types/testdata/streaming_chain_id.json rename to types/testdata/parse_chain_id.json index bb08a716473d..7b5dfbea89a2 100644 --- a/types/testdata/streaming_chain_id.json +++ b/types/testdata/parse_chain_id.json @@ -1,6 +1,6 @@ { - "genesis_time": "2023-09-27T05:35:43.213433Z", "chain_id": "test_777-1", + "genesis_time": "2023-09-27T05:35:43.213433Z", "initial_height": "1", "consensus_params": { "block": { From df3e2a393616b618546d61444bdf35f608d50949 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Wed, 25 Oct 2023 09:31:39 +0800 Subject: [PATCH 12/22] trim space --- types/chain_id.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/chain_id.go b/types/chain_id.go index 6d1f4a994ab6..9e8d43f03e14 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "strings" "github.com/cometbft/cometbft/types" ) @@ -59,7 +60,7 @@ func ParseChainIDFromGenesis(r io.Reader) (string, error) { } func validateChainID(chainID string) error { - if chainID == "" { + if strings.TrimSpace(chainID) == "" { return errors.New("genesis doc must include non-empty chain_id") } if len(chainID) > types.MaxChainIDLen { From 3d7ef1abc61e5abf59273e102b077ec28877edfe Mon Sep 17 00:00:00 2001 From: HuangYi Date: Wed, 25 Oct 2023 09:31:39 +0800 Subject: [PATCH 13/22] trim space --- types/chain_id_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/types/chain_id_test.go b/types/chain_id_test.go index d476823094cf..f25e226d80b1 100644 --- a/types/chain_id_test.go +++ b/types/chain_id_test.go @@ -71,6 +71,11 @@ func TestParseChainIDFromGenesis(t *testing.T) { `{"chain_id": ""}`, "", }, + { + "whitespace chain_id", + `{"chain_id": " "}`, + "", + }, { "chain_id too long", `{"chain_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}`, From b09130eae5e9fa4a0f663cd61b603fbdb33c8b45 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Wed, 25 Oct 2023 09:33:36 +0800 Subject: [PATCH 14/22] review suggestions --- types/chain_id.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/types/chain_id.go b/types/chain_id.go index 9e8d43f03e14..e2a5f802ac9d 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -19,7 +19,6 @@ const ChainIDFieldName = "chain_id" func ParseChainIDFromGenesis(r io.Reader) (string, error) { dec := json.NewDecoder(r) - var t json.Token t, err := dec.Token() if err != nil { return "", err @@ -35,7 +34,7 @@ func ParseChainIDFromGenesis(r io.Reader) (string, error) { } key, ok := t.(string) if !ok { - return "", fmt.Errorf("expected string, got %s", t) + return "", fmt.Errorf("expected string for the key type, got %s", t) } if key == ChainIDFieldName { From d52f3b6ad01d0b87f344e5980fd52c687de1b516 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Wed, 25 Oct 2023 09:40:15 +0800 Subject: [PATCH 15/22] assert error messages --- types/chain_id_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/types/chain_id_test.go b/types/chain_id_test.go index f25e226d80b1..fe0c6e6d61d2 100644 --- a/types/chain_id_test.go +++ b/types/chain_id_test.go @@ -17,6 +17,7 @@ func TestParseChainIDFromGenesis(t *testing.T) { name string json string expChainID string + expError string }{ { "success", @@ -29,6 +30,7 @@ func TestParseChainIDFromGenesis(t *testing.T) { "chain_id": "test-chain-id" }`, "test-chain-id", + "", }, { "nested", @@ -41,6 +43,7 @@ func TestParseChainIDFromGenesis(t *testing.T) { } }`, "", + "missing chain-id in genesis file", }, { "not exist", @@ -53,33 +56,39 @@ func TestParseChainIDFromGenesis(t *testing.T) { "chain-id": "test-chain-id" }`, "", + "missing chain-id in genesis file", }, { "invalid type", `{ - "chain-id":1, + "chain-id": 1, }`, "", + "invalid character '}' looking for beginning of object key string", }, { "invalid json", `[ " ': }`, "", + "expected {, got [", }, { "empty chain_id", `{"chain_id": ""}`, "", + "genesis doc must include non-empty chain_id", }, { "whitespace chain_id", `{"chain_id": " "}`, "", + "genesis doc must include non-empty chain_id", }, { "chain_id too long", `{"chain_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}`, "", + "chain_id in genesis doc is too long", }, } @@ -88,6 +97,7 @@ func TestParseChainIDFromGenesis(t *testing.T) { chain_id, err := ParseChainIDFromGenesis(strings.NewReader(tc.json)) if tc.expChainID == "" { require.Error(t, err) + require.Contains(t, err.Error(), tc.expError) } else { require.NoError(t, err) require.Equal(t, tc.expChainID, chain_id) From 1ad9a8febda03436f3c38a1c99d54c580651a779 Mon Sep 17 00:00:00 2001 From: yihuang Date: Wed, 25 Oct 2023 09:41:49 +0800 Subject: [PATCH 16/22] Update types/chain_id.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- types/chain_id.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/chain_id.go b/types/chain_id.go index e2a5f802ac9d..83ab9bcb3631 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -16,6 +16,8 @@ const ChainIDFieldName = "chain_id" // it still parses the values before the `chain_id` field, particularly if the `app_state` field is // before the `chain_id` field, it will parse the `app_state` value, user must make sure the `chain_id` // is put before `app_state` or other big entries to enjoy the efficiency. +// If the `chain_id` field is not found, the function will return an error. +func ParseChainIDFromGenesis(r io.Reader) (string, error) { func ParseChainIDFromGenesis(r io.Reader) (string, error) { dec := json.NewDecoder(r) From 63b604c60d0143f6cdad60543b3bc53088d70747 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Wed, 25 Oct 2023 09:43:30 +0800 Subject: [PATCH 17/22] use cronos mainnet genesis for benchmark --- types/chain_id_test.go | 5 +- types/testdata/parse_chain_id.json | 550 +---------------------------- 2 files changed, 4 insertions(+), 551 deletions(-) diff --git a/types/chain_id_test.go b/types/chain_id_test.go index fe0c6e6d61d2..141c5141d25f 100644 --- a/types/chain_id_test.go +++ b/types/chain_id_test.go @@ -107,13 +107,14 @@ func TestParseChainIDFromGenesis(t *testing.T) { } func BenchmarkParseChainID(b *testing.B) { + expChainID := "cronosmainnet_25-1" b.ReportAllocs() b.Run("new", func(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { chainId, err := ParseChainIDFromGenesis(strings.NewReader(BenchmarkGenesis)) require.NoError(b, err) - require.Equal(b, "test_777-1", chainId) + require.Equal(b, expChainID, chainId) } }) @@ -122,7 +123,7 @@ func BenchmarkParseChainID(b *testing.B) { for i := 0; i < b.N; i++ { doc, err := genutiltypes.AppGenesisFromReader(strings.NewReader(BenchmarkGenesis)) require.NoError(b, err) - require.Equal(b, "test_777-1", doc.ChainID) + require.Equal(b, expChainID, doc.ChainID) } }) } diff --git a/types/testdata/parse_chain_id.json b/types/testdata/parse_chain_id.json index 7b5dfbea89a2..be2588d2ae79 100644 --- a/types/testdata/parse_chain_id.json +++ b/types/testdata/parse_chain_id.json @@ -1,549 +1 @@ -{ - "chain_id": "test_777-1", - "genesis_time": "2023-09-27T05:35:43.213433Z", - "initial_height": "1", - "consensus_params": { - "block": { - "max_bytes": "1048576", - "max_gas": "81500000" - }, - "evidence": { - "max_age_num_blocks": "100000", - "max_age_duration": "172800000000000", - "max_bytes": "1048576" - }, - "validator": { - "pub_key_types": [ - "ed25519" - ] - }, - "version": { - "app": "0" - } - }, - "app_hash": "", - "app_state": { - "07-tendermint": null, - "auth": { - "params": { - "max_memo_characters": "256", - "tx_sig_limit": "7", - "tx_size_cost_per_byte": "10", - "sig_verify_cost_ed25519": "590", - "sig_verify_cost_secp256k1": "1000" - }, - "accounts": [ - { - "@type": "/ethermint.types.v1.EthAccount", - "base_account": { - "address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", - "pub_key": null, - "account_number": "0", - "sequence": "0" - }, - "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - }, - { - "@type": "/ethermint.types.v1.EthAccount", - "base_account": { - "address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", - "pub_key": null, - "account_number": "1", - "sequence": "0" - }, - "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - }, - { - "@type": "/ethermint.types.v1.EthAccount", - "base_account": { - "address": "crc1x7x9pkfxf33l87ftspk5aetwnkr0lvlv3346cd", - "pub_key": null, - "account_number": "2", - "sequence": "0" - }, - "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - }, - { - "@type": "/ethermint.types.v1.EthAccount", - "base_account": { - "address": "crc16z0herz998946wr659lr84c8c556da55dc34hh", - "pub_key": null, - "account_number": "3", - "sequence": "0" - }, - "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - }, - { - "@type": "/ethermint.types.v1.EthAccount", - "base_account": { - "address": "crc1q04jewhxw4xxu3vlg3rc85240h9q7ns6hglz0g", - "pub_key": null, - "account_number": "4", - "sequence": "0" - }, - "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - } - ] - }, - "authz": { - "authorization": [] - }, - "bank": { - "params": { - "send_enabled": [], - "default_send_enabled": true - }, - "balances": [ - { - "address": "crc1q04jewhxw4xxu3vlg3rc85240h9q7ns6hglz0g", - "coins": [ - { - "denom": "basetcro", - "amount": "30000000000000000000000" - } - ] - }, - { - "address": "crc1x7x9pkfxf33l87ftspk5aetwnkr0lvlv3346cd", - "coins": [ - { - "denom": "basetcro", - "amount": "10000000000000000000000" - } - ] - }, - { - "address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", - "coins": [ - { - "denom": "basetcro", - "amount": "10000000000000000000000" - }, - { - "denom": "stake", - "amount": "1000000000000000000" - } - ] - }, - { - "address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", - "coins": [ - { - "denom": "basetcro", - "amount": "10000000000000000000000" - }, - { - "denom": "stake", - "amount": "1000000000000000000" - } - ] - }, - { - "address": "crc16z0herz998946wr659lr84c8c556da55dc34hh", - "coins": [ - { - "denom": "basetcro", - "amount": "20000000000000000000000" - } - ] - } - ], - "supply": [ - { - "denom": "basetcro", - "amount": "80000000000000000000000" - }, - { - "denom": "stake", - "amount": "2000000000000000000" - } - ], - "denom_metadata": [], - "send_enabled": [] - }, - "capability": { - "index": "1", - "owners": [] - }, - "consensus": null, - "crisis": { - "constant_fee": { - "denom": "stake", - "amount": "1000" - } - }, - "distribution": { - "params": { - "community_tax": "0.020000000000000000", - "base_proposer_reward": "0.000000000000000000", - "bonus_proposer_reward": "0.000000000000000000", - "withdraw_addr_enabled": true - }, - "fee_pool": { - "community_pool": [] - }, - "delegator_withdraw_infos": [], - "previous_proposer": "", - "outstanding_rewards": [], - "validator_accumulated_commissions": [], - "validator_historical_rewards": [], - "validator_current_rewards": [], - "delegator_starting_infos": [], - "validator_slash_events": [] - }, - "evidence": { - "evidence": [] - }, - "evm": { - "accounts": [], - "params": { - "evm_denom": "basetcro", - "enable_create": true, - "enable_call": true, - "extra_eips": [], - "chain_config": { - "homestead_block": "0", - "dao_fork_block": "0", - "dao_fork_support": true, - "eip150_block": "0", - "eip150_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "eip155_block": "0", - "eip158_block": "0", - "byzantium_block": "0", - "constantinople_block": "0", - "petersburg_block": "0", - "istanbul_block": "0", - "muir_glacier_block": "0", - "berlin_block": "0", - "london_block": "0", - "arrow_glacier_block": "0", - "gray_glacier_block": "0", - "merge_netsplit_block": "0", - "shanghai_block": "0", - "cancun_block": "0" - }, - "allow_unprotected_txs": false - } - }, - "feegrant": { - "allowances": [] - }, - "feeibc": { - "identified_fees": [], - "fee_enabled_channels": [], - "registered_payees": [], - "registered_counterparty_payees": [], - "forward_relayers": [] - }, - "feemarket": { - "params": { - "no_base_fee": false, - "base_fee_change_denominator": 8, - "elasticity_multiplier": 2, - "enable_height": "0", - "base_fee": "100000000000", - "min_gas_price": "0.000000000000000000", - "min_gas_multiplier": "0.500000000000000000" - }, - "block_gas": "0" - }, - "genutil": { - "gen_txs": [ - { - "body": { - "messages": [ - { - "@type": "/cosmos.staking.v1beta1.MsgCreateValidator", - "description": { - "moniker": "node1", - "identity": "", - "website": "", - "security_contact": "", - "details": "" - }, - "commission": { - "rate": "0.100000000000000000", - "max_rate": "0.200000000000000000", - "max_change_rate": "0.010000000000000000" - }, - "min_self_delegation": "1", - "delegator_address": "crc18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjtsgrn7", - "validator_address": "crcvaloper18z6q38mhvtsvyr5mak8fj8s8g4gw7kjjp0e0dh", - "pubkey": { - "@type": "/cosmos.crypto.ed25519.PubKey", - "key": "VYjUEP/dlIS1m5QVwzw4TwpGFXIbHwX+nXl0EilxXss=" - }, - "value": { - "denom": "stake", - "amount": "1000000000000000000" - } - } - ], - "memo": "03116e1d4dc9b09cdae5ae2b6a9512e37d1e000f@192.168.0.8:26656", - "timeout_height": "0", - "extension_options": [], - "non_critical_extension_options": [] - }, - "auth_info": { - "signer_infos": [ - { - "public_key": { - "@type": "/ethermint.crypto.v1.ethsecp256k1.PubKey", - "key": "AkJ4WnUHRFLWKmrCInD/uPsByTddC6coh66ADcYZMV0b" - }, - "mode_info": { - "single": { - "mode": "SIGN_MODE_DIRECT" - } - }, - "sequence": "0" - } - ], - "fee": { - "amount": [], - "gas_limit": "200000", - "payer": "", - "granter": "" - }, - "tip": null - }, - "signatures": [ - "vXMjRJSsXYoNEDnWKwu6P/O3PcaOAcLcbNmPraP5e1Euk7bE+0/2IiFAJV7iQsHuQYKA3lzS4Wtq9nmmjwXzYQE=" - ] - }, - { - "body": { - "messages": [ - { - "@type": "/cosmos.staking.v1beta1.MsgCreateValidator", - "description": { - "moniker": "node0", - "identity": "", - "website": "", - "security_contact": "", - "details": "" - }, - "commission": { - "rate": "0.100000000000000000", - "max_rate": "0.200000000000000000", - "max_change_rate": "0.010000000000000000" - }, - "min_self_delegation": "1", - "delegator_address": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", - "validator_address": "crcvaloper12luku6uxehhak02py4rcz65zu0swh7wj6ulrlg", - "pubkey": { - "@type": "/cosmos.crypto.ed25519.PubKey", - "key": "XMFekpj1jUgxm8jx4PRiDGk8tqqmdPYEVBrHdk122Pw=" - }, - "value": { - "denom": "stake", - "amount": "1000000000000000000" - } - } - ], - "memo": "6bdd363882d10473b99c8c710661f79d38bd5ce6@192.168.0.8:26656", - "timeout_height": "0", - "extension_options": [], - "non_critical_extension_options": [] - }, - "auth_info": { - "signer_infos": [ - { - "public_key": { - "@type": "/ethermint.crypto.v1.ethsecp256k1.PubKey", - "key": "Am5xCmKjQt4O1NfEUy3Ly7r78ZZS7WeyN++rcOiyB++s" - }, - "mode_info": { - "single": { - "mode": "SIGN_MODE_DIRECT" - } - }, - "sequence": "0" - } - ], - "fee": { - "amount": [], - "gas_limit": "200000", - "payer": "", - "granter": "" - }, - "tip": null - }, - "signatures": [ - "xiDZO94CFnydjTKevKRg/lpDSAK0LO/nxrcdJRjnvaZA3znfV+YUwqBPr9GU+Jcxi+QBUg+Rp3JLq1HJR/it+gA=" - ] - } - ] - }, - "gov": { - "starting_proposal_id": "1", - "deposits": [], - "votes": [], - "proposals": [], - "deposit_params": null, - "voting_params": null, - "tally_params": null, - "params": { - "min_deposit": [ - { - "amount": "1", - "denom": "basetcro" - } - ], - "max_deposit_period": "10s", - "voting_period": "10s", - "quorum": "0.334000000000000000", - "threshold": "0.500000000000000000", - "veto_threshold": "0.334000000000000000", - "min_initial_deposit_ratio": "0.000000000000000000", - "burn_vote_quorum": false, - "burn_proposal_deposit_prevote": false, - "burn_vote_veto": true - } - }, - "gravity": { - "params": { - "gravity_id": "defaultgravityid", - "contract_source_hash": "", - "bridge_ethereum_address": "0x0000000000000000000000000000000000000000", - "bridge_chain_id": "0", - "signed_signer_set_txs_window": "10000", - "signed_batches_window": "10000", - "ethereum_signatures_window": "10000", - "target_eth_tx_timeout": "43200000", - "average_block_time": "5000", - "average_ethereum_block_time": "15000", - "slash_fraction_signer_set_tx": "0.001000000000000000", - "slash_fraction_batch": "0.001000000000000000", - "slash_fraction_ethereum_signature": "0.001000000000000000", - "slash_fraction_conflicting_ethereum_signature": "0.001000000000000000", - "unbond_slashing_signer_set_txs_window": "10000", - "bridge_active": true, - "batch_creation_period": "10", - "batch_max_element": "100", - "observe_ethereum_height_period": "50" - }, - "last_observed_event_nonce": "0", - "outgoing_txs": [], - "confirmations": [], - "ethereum_event_vote_records": [], - "delegate_keys": [], - "erc20_to_denoms": [], - "unbatched_send_to_ethereum_txs": [] - }, - "ibc": { - "client_genesis": { - "clients": [], - "clients_consensus": [], - "clients_metadata": [], - "params": { - "allowed_clients": [ - "06-solomachine", - "07-tendermint", - "09-localhost" - ] - }, - "create_localhost": false, - "next_client_sequence": "0" - }, - "connection_genesis": { - "connections": [], - "client_connection_paths": [], - "next_connection_sequence": "0", - "params": { - "max_expected_time_per_block": "30000000000" - } - }, - "channel_genesis": { - "channels": [], - "acknowledgements": [], - "commitments": [], - "receipts": [], - "send_sequences": [], - "recv_sequences": [], - "ack_sequences": [], - "next_channel_sequence": "0" - } - }, - "icaauth": { - "params": { - "min_timeout_duration": "3600s" - } - }, - "interchainaccounts": { - "controller_genesis_state": { - "active_channels": [], - "interchain_accounts": [], - "ports": [], - "params": { - "controller_enabled": true - } - }, - "host_genesis_state": { - "active_channels": [], - "interchain_accounts": [], - "port": "icahost", - "params": { - "host_enabled": true, - "allow_messages": [ - "*" - ] - } - } - }, - "mint": { - "minter": { - "inflation": "0.130000000000000000", - "annual_provisions": "0.000000000000000000" - }, - "params": { - "mint_denom": "stake", - "inflation_rate_change": "0.130000000000000000", - "inflation_max": "0.200000000000000000", - "inflation_min": "0.070000000000000000", - "goal_bonded": "0.670000000000000000", - "blocks_per_year": "6311520" - } - }, - "params": null, - "slashing": { - "params": { - "signed_blocks_window": "100", - "min_signed_per_window": "0.500000000000000000", - "downtime_jail_duration": "600s", - "slash_fraction_double_sign": "0.050000000000000000", - "slash_fraction_downtime": "0.010000000000000000" - }, - "signing_infos": [], - "missed_blocks": [] - }, - "staking": { - "params": { - "unbonding_time": "1814400s", - "max_validators": 100, - "max_entries": 7, - "historical_entries": 10000, - "bond_denom": "stake", - "min_commission_rate": "0.000000000000000000" - }, - "last_total_power": "0", - "last_validator_powers": [], - "validators": [], - "delegations": [], - "unbonding_delegations": [], - "redelegations": [], - "exported": false - }, - "transfer": { - "port_id": "transfer", - "denom_traces": [], - "params": { - "send_enabled": true, - "receive_enabled": true - }, - "total_escrowed": [] - }, - "upgrade": {}, - "vesting": {} - } -} +{"genesis_time":"2021-11-08T01:00:00Z","chain_id":"cronosmainnet_25-1","initial_height":"1","consensus_params":{"block":{"max_bytes":"1048576","max_gas":"10000000","time_iota_ms":"1000"},"evidence":{"max_age_num_blocks":"403200","max_age_duration":"2419200000000000","max_bytes":"150000"},"validator":{"pub_key_types":["ed25519"]},"version":{}},"app_hash":"","app_state":{"auth":{"params":{"max_memo_characters":"256","tx_sig_limit":"7","tx_size_cost_per_byte":"10","sig_verify_cost_ed25519":"590","sig_verify_cost_secp256k1":"1000"},"accounts":[{"@type":"/ethermint.types.v1.EthAccount","base_account":{"address":"crc1q7q2mmmcx2nlw6ptw4a9a3danlnu8z6tq32gv9","pub_key":null,"account_number":"0","sequence":"0"},"code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},{"@type":"/ethermint.types.v1.EthAccount","base_account":{"address":"crc1s8372smy0erx5k4usf84s39tpgy3kmrvjwdejw","pub_key":null,"account_number":"0","sequence":"0"},"code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},{"@type":"/ethermint.types.v1.EthAccount","base_account":{"address":"crc1efw0q0ggzxtmuf80wpcgr77h70c3avpdp9nq5k","pub_key":null,"account_number":"0","sequence":"0"},"code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},{"@type":"/ethermint.types.v1.EthAccount","base_account":{"address":"crc1f7r687vm68jc6qw7rsut07puh9n7s9kzdj0zph","pub_key":null,"account_number":"0","sequence":"0"},"code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},{"@type":"/ethermint.types.v1.EthAccount","base_account":{"address":"crc1aaxs058pksrq8cx3k0nrxv60p2a9c7nq527949","pub_key":null,"account_number":"0","sequence":"0"},"code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},{"@type":"/ethermint.types.v1.EthAccount","base_account":{"address":"crc17s50usvlr5934tr2fxsesr89k4twtm25vjl6zs","pub_key":null,"account_number":"0","sequence":"0"},"code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},{"@type":"/ethermint.types.v1.EthAccount","base_account":{"address":"crc1tasmcx3rqpglmsafdt7vylnsdkc3yjlzfv3u63","pub_key":null,"account_number":"0","sequence":"0"},"code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"},{"@type":"/ethermint.types.v1.EthAccount","base_account":{"address":"crc17m20ajc6d7mu9j34q956q5x5sw7c0wyrem3s7n","pub_key":null,"account_number":"0","sequence":"0"},"code_hash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"}]},"authz":{"authorization":[]},"bank":{"params":{"send_enabled":[{"denom":"stake","enabled":true},{"denom":"basecro","enabled":false}],"default_send_enabled":true},"balances":[{"address":"crc1q7q2mmmcx2nlw6ptw4a9a3danlnu8z6tq32gv9","coins":[{"denom":"stake","amount":"10000000000"}]},{"address":"crc1f7r687vm68jc6qw7rsut07puh9n7s9kzdj0zph","coins":[{"denom":"stake","amount":"10000000000"}]},{"address":"crc1tasmcx3rqpglmsafdt7vylnsdkc3yjlzfv3u63","coins":[{"denom":"stake","amount":"600000000000"}]},{"address":"crc1s8372smy0erx5k4usf84s39tpgy3kmrvjwdejw","coins":[{"denom":"stake","amount":"10000000000"}]},{"address":"crc1efw0q0ggzxtmuf80wpcgr77h70c3avpdp9nq5k","coins":[{"denom":"stake","amount":"10000000000"}]},{"address":"crc1aaxs058pksrq8cx3k0nrxv60p2a9c7nq527949","coins":[{"denom":"stake","amount":"10000"}]},{"address":"crc17s50usvlr5934tr2fxsesr89k4twtm25vjl6zs","coins":[{"denom":"stake","amount":"1000000000000000"}]},{"address":"crc17m20ajc6d7mu9j34q956q5x5sw7c0wyrem3s7n","coins":[{"denom":"stake","amount":"50000000"}]}],"supply":[{"denom":"stake","amount":"1000640050010000"}],"denom_metadata":[]},"capability":{"index":"1","owners":[]},"crisis":{"constant_fee":{"denom":"stake","amount":"1000"}},"cronos":{"params":{"ibc_cro_denom":"ibc/6411AE2ADA1E73DB59DB151A8988F9B7D5E7E233D8414DB6817F8F1A01611F86","ibc_timeout":"86400000000000","cronos_admin":"crc1tasmcx3rqpglmsafdt7vylnsdkc3yjlzfv3u63","enable_auto_deployment":false},"external_contracts":[],"auto_contracts":[]},"distribution":{"params":{"community_tax":"0","base_proposer_reward":"0","bonus_proposer_reward":"0","withdraw_addr_enabled":true},"fee_pool":{"community_pool":[]},"delegator_withdraw_infos":[],"previous_proposer":"","outstanding_rewards":[],"validator_accumulated_commissions":[],"validator_historical_rewards":[],"validator_current_rewards":[],"delegator_starting_infos":[],"validator_slash_events":[]},"evidence":{"evidence":[]},"evm":{"accounts":[],"params":{"evm_denom":"basecro","enable_create":true,"enable_call":true,"extra_eips":["2929","2200","1884","1344"],"chain_config":{"homestead_block":"0","dao_fork_block":"0","dao_fork_support":true,"eip150_block":"0","eip150_hash":"0x0000000000000000000000000000000000000000000000000000000000000000","eip155_block":"0","eip158_block":"0","byzantium_block":"0","constantinople_block":"0","petersburg_block":"0","istanbul_block":"0","muir_glacier_block":"0","berlin_block":"0","catalyst_block":null,"london_block":"9223372036854775808"}}},"feegrant":{"allowances":[]},"genutil":{"gen_txs":[{"body":{"messages":[{"@type":"/cosmos.staking.v1beta1.MsgCreateValidator","description":{"moniker":"Pioneer 11","identity":"BB602FB166F21C6E","website":"https://cronos.crypto.org","security_contact":"chain-security@crypto.org","details":""},"commission":{"rate":"1.000000000000000000","max_rate":"1.000000000000000000","max_change_rate":"1.000000000000000000"},"min_self_delegation":"1","delegator_address":"crc1q7q2mmmcx2nlw6ptw4a9a3danlnu8z6tq32gv9","validator_address":"crcvaloper1q7q2mmmcx2nlw6ptw4a9a3danlnu8z6t2wmyjv","pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"H8pPcYZvZYd/cI2pKuwcmyst7ZTWm5+QkXXuCoGg1P0="},"value":{"denom":"stake","amount":"10000000000"}}],"memo":"","timeout_height":0,"extension_options":[],"non_critical_extension_options":[]},"auth_info":{"signer_infos":[{"public_key":{"@type":"/ethermint.crypto.v1.ethsecp256k1.PubKey","key":"A/S9y8Yhb2FVVInVKmdPOtgIiKUlm7CE8ixT14GmNYm1"},"mode_info":{"single":{"mode":"SIGN_MODE_DIRECT"}},"sequence":0}],"fee":{"amount":[],"gas_limit":200000,"payer":"","granter":""}},"signatures":["35KHI9qc+w+2cOKwlmgXzx1QaPfsK9lxs1Z7INaiNHgHXqRHBUTgl8eKzIHTCnFJU89gpLPEqjUsLDasiHavggA="]},{"body":{"messages":[{"@type":"/cosmos.staking.v1beta1.MsgCreateValidator","description":{"moniker":"Voyager 1","identity":"BB602FB166F21C6E","website":"https://cronos.crypto.org","security_contact":"chain-security@crypto.org","details":""},"commission":{"rate":"1.000000000000000000","max_rate":"1.000000000000000000","max_change_rate":"1.000000000000000000"},"min_self_delegation":"1","delegator_address":"crc1s8372smy0erx5k4usf84s39tpgy3kmrvjwdejw","validator_address":"crcvaloper1s8372smy0erx5k4usf84s39tpgy3kmrvc3u4v8","pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"7WE80ID9TzUjIb5Lu9A+ncidsND5+sJAUL6l/NNn4KE="},"value":{"denom":"stake","amount":"10000000000"}}],"memo":"","timeout_height":0,"extension_options":[],"non_critical_extension_options":[]},"auth_info":{"signer_infos":[{"public_key":{"@type":"/ethermint.crypto.v1.ethsecp256k1.PubKey","key":"AxI/HqRT4KKIQhonXtJBe9H/wRq4BkvAZO+4TxSQSjZt"},"mode_info":{"single":{"mode":"SIGN_MODE_DIRECT"}},"sequence":0}],"fee":{"amount":[],"gas_limit":200000,"payer":"","granter":""}},"signatures":["TlS6sZBiFNI64lr13x+sr3rrwV+U7icm/V9ksIMEtlhSZlXNJhS+2hQywyGutb6JhA+Ov+Wjln4puHm/MSA3iAE="]},{"body":{"messages":[{"@type":"/cosmos.staking.v1beta1.MsgCreateValidator","description":{"moniker":"Voyager 2","identity":"BB602FB166F21C6E","website":"https://cronos.crypto.org","security_contact":"chain-security@crypto.org","details":""},"commission":{"rate":"1.000000000000000000","max_rate":"1.000000000000000000","max_change_rate":"1.000000000000000000"},"min_self_delegation":"1","delegator_address":"crc1efw0q0ggzxtmuf80wpcgr77h70c3avpdp9nq5k","validator_address":"crcvaloper1efw0q0ggzxtmuf80wpcgr77h70c3avpdt6zv2l","pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"TXH4O/GhYibZffcBo5UAERwi9T4VgPAiVCDyl4rdFDE="},"value":{"denom":"stake","amount":"10000000000"}}],"memo":"","timeout_height":0,"extension_options":[],"non_critical_extension_options":[]},"auth_info":{"signer_infos":[{"public_key":{"@type":"/ethermint.crypto.v1.ethsecp256k1.PubKey","key":"AzR06WzkRRXNNE+A4VFG18w170ZicRa/mVsLvBGlBAxz"},"mode_info":{"single":{"mode":"SIGN_MODE_DIRECT"}},"sequence":0}],"fee":{"amount":[],"gas_limit":200000,"payer":"","granter":""}},"signatures":["mx//YsSSwUP4G020FLRS0532nP2AylVPqRUXb9EdauM4OWdXF0+Ftx8jbcFinzBw8PHGof3XHcg2yw2P2qNBMAE="]},{"body":{"messages":[{"@type":"/cosmos.staking.v1beta1.MsgCreateValidator","description":{"moniker":"Huygens","identity":"BB602FB166F21C6E","website":"https://cronos.crypto.org","security_contact":"chain-security@crypto.org","details":""},"commission":{"rate":"1.000000000000000000","max_rate":"1.000000000000000000","max_change_rate":"1.000000000000000000"},"min_self_delegation":"1","delegator_address":"crc1f7r687vm68jc6qw7rsut07puh9n7s9kzdj0zph","validator_address":"crcvaloper1f7r687vm68jc6qw7rsut07puh9n7s9kz8d7wl7","pubkey":{"@type":"/cosmos.crypto.ed25519.PubKey","key":"f826QMXnfR9pEAJDEQR4t228BwQVJ7xe2EwGZN8doQY="},"value":{"denom":"stake","amount":"10000000000"}}],"memo":"","timeout_height":0,"extension_options":[],"non_critical_extension_options":[]},"auth_info":{"signer_infos":[{"public_key":{"@type":"/ethermint.crypto.v1.ethsecp256k1.PubKey","key":"AkDcLOeNUKVUS52hUPx+cSo+ohG6vW3P2RYkbW6dwdMG"},"mode_info":{"single":{"mode":"SIGN_MODE_DIRECT"}},"sequence":0}],"fee":{"amount":[],"gas_limit":200000,"payer":"","granter":""}},"signatures":["Rz89OXlfJQgasEeKxsywF/XyiYg7Xdgg/qgwtJcOMuIgJ3hfsE0KrreXlfHOGPIe9nVduffC9W3gKvw2sHe9ugE="]}]},"gov":{"starting_proposal_id":"1","deposits":[],"votes":[],"proposals":[],"deposit_params":{"min_deposit":[{"denom":"basecro","amount":"20000000000000000000000"}],"max_deposit_period":"21600000000000ns"},"voting_params":{"voting_period":"259200000000000ns"},"tally_params":{"quorum":"0.334","threshold":"0.5","veto_threshold":"0.334"}},"ibc":{"client_genesis":{"clients":[],"clients_consensus":[],"clients_metadata":[],"params":{"allowed_clients":["06-solomachine","07-tendermint"]},"create_localhost":false,"next_client_sequence":"0"},"connection_genesis":{"connections":[],"client_connection_paths":[],"next_connection_sequence":"0","params":{"max_expected_time_per_block":"30000000000"}},"channel_genesis":{"channels":[],"acknowledgements":[],"commitments":[],"receipts":[],"send_sequences":[],"recv_sequences":[],"ack_sequences":[],"next_channel_sequence":"0"}},"mint":{"minter":{"inflation":"0.000000000000000000","annual_provisions":"0.000000000000000000"},"params":{"mint_denom":"stake","inflation_rate_change":"0","inflation_max":"0","inflation_min":"0","goal_bonded":"1","blocks_per_year":"6311520"}},"params":null,"slashing":{"params":{"signed_blocks_window":"10000","min_signed_per_window":"0.5","downtime_jail_duration":"28800s","slash_fraction_double_sign":"0","slash_fraction_downtime":"0"},"signing_infos":[],"missed_blocks":[]},"staking":{"params":{"unbonding_time":"2419200000000000ns","max_validators":"50","max_entries":"7","historical_entries":"10000","bond_denom":"stake"},"last_total_power":"0","last_validator_powers":[],"validators":[],"delegations":[],"unbonding_delegations":[],"redelegations":[],"exported":false},"transfer":{"port_id":"transfer","denom_traces":[],"params":{"send_enabled":true,"receive_enabled":true}},"upgrade":{},"vesting":{}}} From a4cc48a7968546d094065d175dcd4f91e7bb07c6 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Wed, 25 Oct 2023 10:12:16 +0800 Subject: [PATCH 18/22] fix conflicts --- types/chain_id.go | 1 - 1 file changed, 1 deletion(-) diff --git a/types/chain_id.go b/types/chain_id.go index 83ab9bcb3631..7edb3aa6d8a4 100644 --- a/types/chain_id.go +++ b/types/chain_id.go @@ -17,7 +17,6 @@ const ChainIDFieldName = "chain_id" // before the `chain_id` field, it will parse the `app_state` value, user must make sure the `chain_id` // is put before `app_state` or other big entries to enjoy the efficiency. // If the `chain_id` field is not found, the function will return an error. -func ParseChainIDFromGenesis(r io.Reader) (string, error) { func ParseChainIDFromGenesis(r io.Reader) (string, error) { dec := json.NewDecoder(r) From 5147d1ca906b29827e63eeee7fac792d092005eb Mon Sep 17 00:00:00 2001 From: HuangYi Date: Wed, 25 Oct 2023 16:07:15 +0800 Subject: [PATCH 19/22] fix cyclic import --- types/chain_id_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/types/chain_id_test.go b/types/chain_id_test.go index 141c5141d25f..b98b3c36bc7f 100644 --- a/types/chain_id_test.go +++ b/types/chain_id_test.go @@ -1,10 +1,11 @@ -package types +package types_test import ( _ "embed" "strings" "testing" + sdk "github.com/cosmos/cosmos-sdk/types" genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" "github.com/stretchr/testify/require" ) @@ -94,7 +95,7 @@ func TestParseChainIDFromGenesis(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - chain_id, err := ParseChainIDFromGenesis(strings.NewReader(tc.json)) + chain_id, err := sdk.ParseChainIDFromGenesis(strings.NewReader(tc.json)) if tc.expChainID == "" { require.Error(t, err) require.Contains(t, err.Error(), tc.expError) @@ -112,7 +113,7 @@ func BenchmarkParseChainID(b *testing.B) { b.Run("new", func(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - chainId, err := ParseChainIDFromGenesis(strings.NewReader(BenchmarkGenesis)) + chainId, err := sdk.ParseChainIDFromGenesis(strings.NewReader(BenchmarkGenesis)) require.NoError(b, err) require.Equal(b, expChainID, chainId) } From ba767d91daae45b4a8b82290a6d495c07d4279af Mon Sep 17 00:00:00 2001 From: HuangYi Date: Wed, 25 Oct 2023 22:53:59 +0800 Subject: [PATCH 20/22] fix lint --- types/chain_id_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/chain_id_test.go b/types/chain_id_test.go index b98b3c36bc7f..f37bf1b1dd7a 100644 --- a/types/chain_id_test.go +++ b/types/chain_id_test.go @@ -5,9 +5,10 @@ import ( "strings" "testing" + "github.com/stretchr/testify/require" + sdk "github.com/cosmos/cosmos-sdk/types" genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" - "github.com/stretchr/testify/require" ) //go:embed testdata/parse_chain_id.json From 2d3ba07c2505ebce27b7ea8e9682206748c9d490 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Thu, 26 Oct 2023 09:51:13 +0800 Subject: [PATCH 21/22] move directory --- server/util.go | 3 ++- {types => x/genutil/types}/chain_id.go | 0 {types => x/genutil/types}/chain_id_test.go | 9 ++++----- {types => x/genutil/types}/testdata/parse_chain_id.json | 0 4 files changed, 6 insertions(+), 6 deletions(-) rename {types => x/genutil/types}/chain_id.go (100%) rename {types => x/genutil/types}/chain_id_test.go (87%) rename {types => x/genutil/types}/testdata/parse_chain_id.json (100%) diff --git a/server/util.go b/server/util.go index 905292f163a4..0c1a46a68688 100644 --- a/server/util.go +++ b/server/util.go @@ -37,6 +37,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/mempool" "github.com/cosmos/cosmos-sdk/version" + genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" ) // ServerContextKey defines the context key used to retrieve a server.Context from @@ -490,7 +491,7 @@ func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) { } defer reader.Close() - chainID, err = sdk.ParseChainIDFromGenesis(reader) + chainID, err = genutiltypes.ParseChainIDFromGenesis(reader) if err != nil { panic(fmt.Errorf("failed to parse chain-id from genesis file: %w", err)) } diff --git a/types/chain_id.go b/x/genutil/types/chain_id.go similarity index 100% rename from types/chain_id.go rename to x/genutil/types/chain_id.go diff --git a/types/chain_id_test.go b/x/genutil/types/chain_id_test.go similarity index 87% rename from types/chain_id_test.go rename to x/genutil/types/chain_id_test.go index f37bf1b1dd7a..2f558a37d99b 100644 --- a/types/chain_id_test.go +++ b/x/genutil/types/chain_id_test.go @@ -7,8 +7,7 @@ import ( "github.com/stretchr/testify/require" - sdk "github.com/cosmos/cosmos-sdk/types" - genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" + "github.com/cosmos/cosmos-sdk/x/genutil/types" ) //go:embed testdata/parse_chain_id.json @@ -96,7 +95,7 @@ func TestParseChainIDFromGenesis(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - chain_id, err := sdk.ParseChainIDFromGenesis(strings.NewReader(tc.json)) + chain_id, err := types.ParseChainIDFromGenesis(strings.NewReader(tc.json)) if tc.expChainID == "" { require.Error(t, err) require.Contains(t, err.Error(), tc.expError) @@ -114,7 +113,7 @@ func BenchmarkParseChainID(b *testing.B) { b.Run("new", func(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - chainId, err := sdk.ParseChainIDFromGenesis(strings.NewReader(BenchmarkGenesis)) + chainId, err := types.ParseChainIDFromGenesis(strings.NewReader(BenchmarkGenesis)) require.NoError(b, err) require.Equal(b, expChainID, chainId) } @@ -123,7 +122,7 @@ func BenchmarkParseChainID(b *testing.B) { b.Run("old", func(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - doc, err := genutiltypes.AppGenesisFromReader(strings.NewReader(BenchmarkGenesis)) + doc, err := types.AppGenesisFromReader(strings.NewReader(BenchmarkGenesis)) require.NoError(b, err) require.Equal(b, expChainID, doc.ChainID) } diff --git a/types/testdata/parse_chain_id.json b/x/genutil/types/testdata/parse_chain_id.json similarity index 100% rename from types/testdata/parse_chain_id.json rename to x/genutil/types/testdata/parse_chain_id.json From 5145bac181284d4f936d0ab137724f866bd421b2 Mon Sep 17 00:00:00 2001 From: yihuang Date: Thu, 26 Oct 2023 10:07:57 +0800 Subject: [PATCH 22/22] Update x/genutil/types/chain_id.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- x/genutil/types/chain_id.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/x/genutil/types/chain_id.go b/x/genutil/types/chain_id.go index 7edb3aa6d8a4..068a82fdfb56 100644 --- a/x/genutil/types/chain_id.go +++ b/x/genutil/types/chain_id.go @@ -12,11 +12,9 @@ import ( const ChainIDFieldName = "chain_id" -// ParseChainIDFromGenesis parses the `chain_id` from the genesis json file and abort early, -// it still parses the values before the `chain_id` field, particularly if the `app_state` field is -// before the `chain_id` field, it will parse the `app_state` value, user must make sure the `chain_id` -// is put before `app_state` or other big entries to enjoy the efficiency. -// If the `chain_id` field is not found, the function will return an error. +// ParseChainIDFromGenesis parses the `chain_id` from a genesis JSON file, aborting early after finding the `chain_id`. +// For efficiency, it's recommended to place the `chain_id` field before any large entries in the JSON file. +// Returns an error if the `chain_id` field is not found. func ParseChainIDFromGenesis(r io.Reader) (string, error) { dec := json.NewDecoder(r)