Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: parse chain-id from big genesis file could be slow #18204

Merged
merged 28 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
192261b
Problem: parse chain-id from big genesis file could be slow
yihuang Oct 23, 2023
7c59903
Update CHANGELOG.md
yihuang Oct 23, 2023
ee9b00a
fix unit test
yihuang Oct 23, 2023
567ee2d
patch the library to support early abort
yihuang Oct 23, 2023
55be3ec
add benchmarking
yihuang Oct 23, 2023
9c220f5
mention encoding/json/v2
yihuang Oct 23, 2023
c74ba3b
fix error handling
yihuang Oct 23, 2023
7680cdd
fix shadow var
yihuang Oct 23, 2023
1035e32
use simpler solution with different trade off
yihuang Oct 23, 2023
4677221
Merge branch 'main' into parse-chain-id
yihuang Oct 24, 2023
be3a3f7
cleanup
yihuang Oct 24, 2023
7cd0787
skip value more efficiently
yihuang Oct 24, 2023
c84599c
Merge branch 'main' into parse-chain-id
yihuang Oct 24, 2023
df3e2a3
trim space
yihuang Oct 25, 2023
3d7ef1a
trim space
yihuang Oct 25, 2023
b09130e
review suggestions
yihuang Oct 25, 2023
d52f3b6
assert error messages
yihuang Oct 25, 2023
1ad9a8f
Update types/chain_id.go
yihuang Oct 25, 2023
63b604c
use cronos mainnet genesis for benchmark
yihuang Oct 25, 2023
a4cc48a
fix conflicts
yihuang Oct 25, 2023
7a58af1
Merge branch 'main' into parse-chain-id
yihuang Oct 25, 2023
5147d1c
fix cyclic import
yihuang Oct 25, 2023
5d99b23
Merge branch 'main' into parse-chain-id
yihuang Oct 25, 2023
ba767d9
fix lint
yihuang Oct 25, 2023
2d3ba07
move directory
yihuang Oct 26, 2023
310b580
Merge branch 'main' into parse-chain-id
yihuang Oct 26, 2023
5145bac
Update x/genutil/types/chain_id.go
yihuang Oct 26, 2023
1bb078f
Merge branch 'main' into parse-chain-id
julienrbrt Oct 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
* [#18204](https://github.com/cosmos/cosmos-sdk/pull/18204) Use streaming json parser to parse chain-id from genesis file.

### Bug Fixes

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -173,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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -792,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=
Expand Down
9 changes: 6 additions & 3 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
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
Expand Down Expand Up @@ -485,12 +484,16 @@
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"))
Dismissed Show dismissed Hide dismissed
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))
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

snapshotStore, err := GetSnapshotStore(appOpts)
Expand Down
34 changes: 34 additions & 0 deletions types/chain_id.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we maybe move this to genutil? Imho this makes more sense to have it there as its genesis related than in types.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package types

import (
"errors"
"io"

"github.com/bcicen/jstream"

Check failure on line 7 in types/chain_id.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing go.sum entry for module providing package github.com/bcicen/jstream (imported by github.com/cosmos/cosmos-sdk/types); to add:

Check failure on line 7 in types/chain_id.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing go.sum entry for module providing package github.com/bcicen/jstream (imported by github.com/cosmos/cosmos-sdk/types); to add:

Check failure on line 7 in types/chain_id.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing go.sum entry for module providing package github.com/bcicen/jstream (imported by github.com/cosmos/cosmos-sdk/types); to add:

Check failure on line 7 in types/chain_id.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing go.sum entry for module providing package github.com/bcicen/jstream (imported by github.com/cosmos/cosmos-sdk/types); to add:

Check failure on line 7 in types/chain_id.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing go.sum entry for module providing package github.com/bcicen/jstream (imported by github.com/cosmos/cosmos-sdk/types); to add:

Check failure on line 7 in types/chain_id.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing go.sum entry for module providing package github.com/bcicen/jstream (imported by github.com/cosmos/cosmos-sdk/types); to add:

Check failure on line 7 in types/chain_id.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing go.sum entry for module providing package github.com/bcicen/jstream (imported by github.com/cosmos/cosmos-sdk/types); to add:

Check failure on line 7 in types/chain_id.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing go.sum entry for module providing package github.com/bcicen/jstream (imported by github.com/cosmos/cosmos-sdk/types); to add:

Check failure on line 7 in types/chain_id.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing go.sum entry for module providing package github.com/bcicen/jstream (imported by github.com/cosmos/cosmos-sdk/types); to add:
yihuang marked this conversation as resolved.
Show resolved Hide resolved
)

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
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)
return false
}
}
return true
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function ParseChainIDFromGenesis uses the jstream library to parse the JSON genesis file. It's a good approach to use a streaming JSON parser for large files as it uses constant memory. However, there's a potential issue with error handling. In line 19, the error from the type assertion kv.Value.(string) is ignored. If kv.Value is not a string, this will not cause a panic due to the use of the "comma ok" idiom, but chain_id will be an empty string and no error will be reported at this stage. This could lead to misleading errors, as the function will later return an error stating that the "chain-id was not found in the genesis file", even though it was found but was not a string. Consider handling this error to provide more accurate feedback.

- chain_id, _ = kv.Value.(string)
+ chain_id, ok = kv.Value.(string)
+ if !ok {
+     return errors.New("chain-id is not a string")
+ }

if len(chain_id) > 0 {
return chain_id, nil
}
if err == nil {
return "", errors.New("chain-id not found in genesis file")
}
return "", err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling here could be improved for clarity. If chain_id is empty and err is not nil, the function will return an error stating that the "chain-id was not found in the genesis file", even though the error might be unrelated to the presence of the chain-id. Consider checking err first and returning it if it's not nil, before checking if chain_id is empty.

- if len(chain_id) > 0 {
-     return chain_id, nil
- }
- if err == nil {
-     return "", errors.New("chain-id not found in genesis file")
- }
- return "", err
+ if err != nil {
+     return "", err
+ }
+ if len(chain_id) == 0 {
+     return "", errors.New("chain-id not found in genesis file")
+ }
+ return chain_id, nil

}
Loading
Loading