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

feat(x/genutil): Allow creation of AppGenesis without a file lookup #17571

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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 @@ -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

Expand Down
32 changes: 27 additions & 5 deletions x/genutil/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package types

import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"time"

cmtjson "github.com/cometbft/cometbft/libs/json"
Expand Down Expand Up @@ -85,19 +88,19 @@ 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
if err := json.Unmarshal(jsonBlob, &appGenesis); err != nil {
// 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{
Expand All @@ -118,6 +121,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(filepath.Clean(genFile))
if err != nil {
return nil, err
}

genesisFile, err := AppGenesisFromReader(bufio.NewReader(file))
if err != nil {
return nil, err
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 wrap the error to still include the file name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

}

if err := file.Close(); err != nil {
return nil, err
}

return genesisFile, nil
}

// --------------------------
// CometBFT Genesis Handling
// --------------------------
Expand Down