From e49dbe4d32c9f49744ae7614f8915161222f9cd4 Mon Sep 17 00:00:00 2001 From: chatton Date: Tue, 29 Aug 2023 12:44:49 +0100 Subject: [PATCH 1/5] feat: adding alternative AppGenesisFromReader function to read genesis file --- x/genutil/types/genesis.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index 8cc486cd55b..8153fd5c3f5 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -1,10 +1,12 @@ package types import ( + "bufio" "bytes" "encoding/json" "errors" "fmt" + "io" "os" "time" @@ -85,11 +87,11 @@ func (ag *AppGenesis) SaveAs(file string) error { return os.WriteFile(file, appGenesisBytes, 0o600) } -// AppGenesisFromFile reads the AppGenesis from the provided file. -func AppGenesisFromFile(genFile string) (*AppGenesis, error) { - jsonBlob, err := os.ReadFile(genFile) +// AppGenesisFromReader reads the AppGenesis from the reader. +func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) { + jsonBlob, err := io.ReadAll(reader) if err != nil { - return nil, fmt.Errorf("couldn't read AppGenesis file (%s): %w", genFile, err) + return nil, err } var appGenesis AppGenesis @@ -97,7 +99,7 @@ func AppGenesisFromFile(genFile string) (*AppGenesis, error) { // fallback to CometBFT genesis var ctmGenesis cmttypes.GenesisDoc if err2 := cmtjson.Unmarshal(jsonBlob, &ctmGenesis); err2 != nil { - return nil, fmt.Errorf("error unmarshalling AppGenesis at %s: %w\n failed fallback to CometBFT GenDoc: %w", genFile, err, err2) + return nil, fmt.Errorf("error unmarshalling AppGenesis: %w\n failed fallback to CometBFT GenDoc: %w", err, err2) } appGenesis = AppGenesis{ @@ -118,6 +120,25 @@ func AppGenesisFromFile(genFile string) (*AppGenesis, error) { return &appGenesis, nil } +// AppGenesisFromFile reads the AppGenesis from the provided file. +func AppGenesisFromFile(genFile string) (*AppGenesis, error) { + file, err := os.Open(genFile) + if err != nil { + return nil, err + } + + genesisFile, err := AppGenesisFromReader(bufio.NewReader(file)) + if err != nil { + return nil, err + } + + if err := file.Close(); err != nil { + return nil, err + } + + return genesisFile, nil +} + // -------------------------- // CometBFT Genesis Handling // -------------------------- From b0bfb2a10d2c7279f7c139427dd9272d55ead9b5 Mon Sep 17 00:00:00 2001 From: chatton Date: Tue, 29 Aug 2023 13:20:29 +0100 Subject: [PATCH 2/5] chore: adding CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a543132244..2d91fe09313 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (client) [#17513](https://github.com/cosmos/cosmos-sdk/pull/17513) Allow overwritting `client.toml`. Use `client.CreateClientConfig` in place of `client.ReadFromClientConfig` and provide a custom template and a custom config. * (x/bank) [#14224](https://github.com/cosmos/cosmos-sdk/pull/14224) Allow injection of restrictions on transfers using `AppendSendRestriction` or `PrependSendRestriction`. +* (genutil) [#17571](https://github.com/cosmos/cosmos-sdk/pull/17571) Allow creation of `AppGenesis` without a file lookup. ### Improvements From 329b088df68d30c1e2fb11d936298646e9249b3b Mon Sep 17 00:00:00 2001 From: chatton Date: Tue, 29 Aug 2023 13:24:48 +0100 Subject: [PATCH 3/5] chore: fix linter warnings --- x/genutil/types/genesis.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index 8153fd5c3f5..8cf1d3c4ec3 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "os" + "path/filepath" "time" cmtjson "github.com/cometbft/cometbft/libs/json" @@ -122,7 +123,7 @@ func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) { // AppGenesisFromFile reads the AppGenesis from the provided file. func AppGenesisFromFile(genFile string) (*AppGenesis, error) { - file, err := os.Open(genFile) + file, err := os.Open(filepath.Clean(genFile)) if err != nil { return nil, err } From dc7a9ea38cfe48f147974b9c9f4a5ee0ee56bde4 Mon Sep 17 00:00:00 2001 From: chatton Date: Tue, 29 Aug 2023 14:54:04 +0100 Subject: [PATCH 4/5] chore: addressing PR feedback --- x/genutil/types/genesis.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index 8cf1d3c4ec3..ac330fdb4e6 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -128,16 +128,16 @@ func AppGenesisFromFile(genFile string) (*AppGenesis, error) { return nil, err } - genesisFile, err := AppGenesisFromReader(bufio.NewReader(file)) + appGenesis, err := AppGenesisFromReader(bufio.NewReader(file)) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to read genesis from file %s: %s", genFile, err) } if err := file.Close(); err != nil { return nil, err } - return genesisFile, nil + return appGenesis, nil } // -------------------------- From e1489d1a0860684d80487a82c520ce619c83703b Mon Sep 17 00:00:00 2001 From: Cian Hatton Date: Tue, 29 Aug 2023 15:22:28 +0100 Subject: [PATCH 5/5] Update x/genutil/types/genesis.go Co-authored-by: Julien Robert --- x/genutil/types/genesis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index ac330fdb4e6..3c322fa086f 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -130,7 +130,7 @@ func AppGenesisFromFile(genFile string) (*AppGenesis, error) { appGenesis, err := AppGenesisFromReader(bufio.NewReader(file)) if err != nil { - return nil, fmt.Errorf("failed to read genesis from file %s: %s", genFile, err) + return nil, fmt.Errorf("failed to read genesis from file %s: %w", genFile, err) } if err := file.Close(); err != nil {