Skip to content

Commit

Permalink
feat(x/genutil): Allow creation of AppGenesis without a file lookup (#…
Browse files Browse the repository at this point in the history
…17571)

Co-authored-by: Julien Robert <julien@rbrt.fr>
(cherry picked from commit 0a253f3)

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
chatton authored and mergify[bot] committed Aug 29, 2023
1 parent 8a0f494 commit 3a65347
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

<<<<<<< HEAD
=======
### Features

* (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

* (client) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) Add `client.Context{}.WithAddressCodec`, `WithValidatorAddressCodec`, `WithConsensusAddressCodec` to provide address codecs to the client context. See the [UPGRADING.md](./UPGRADING.md) for more details.
* (crypto/keyring) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) Simplify keyring interfaces to use `[]byte` instead of `sdk.Address` for addresses.
* (all) [#16537](https://github.com/cosmos/cosmos-sdk/pull/16537) Properly propagated `fmt.Errorf` errors and using `errors.New` where appropriate.
* (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.

>>>>>>> 0a253f3c6 (feat(x/genutil): Allow creation of AppGenesis without a file lookup (#17571))
### Bug Fixes

* (baseapp) [#17518](https://github.com/cosmos/cosmos-sdk/pull/17518) Utilizing voting power from vote extensions (CometBFT) instead of the current bonded tokens (x/staking) to determine if a set of vote extensions are valid.
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
}

appGenesis, err := AppGenesisFromReader(bufio.NewReader(file))
if err != nil {
return nil, fmt.Errorf("failed to read genesis from file %s: %w", genFile, err)
}

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

return appGenesis, nil
}

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

0 comments on commit 3a65347

Please sign in to comment.