Skip to content

Commit 0a253f3

Browse files
chattonjulienrbrt
andauthored
feat(x/genutil): Allow creation of AppGenesis without a file lookup (#17571)
Co-authored-by: Julien Robert <julien@rbrt.fr>
1 parent aabcfb2 commit 0a253f3

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4242

4343
* (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.
4444
* (x/bank) [#14224](https://github.com/cosmos/cosmos-sdk/pull/14224) Allow injection of restrictions on transfers using `AppendSendRestriction` or `PrependSendRestriction`.
45+
* (genutil) [#17571](https://github.com/cosmos/cosmos-sdk/pull/17571) Allow creation of `AppGenesis` without a file lookup.
4546

4647
### Improvements
4748

x/genutil/types/genesis.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package types
22

33
import (
4+
"bufio"
45
"bytes"
56
"encoding/json"
67
"errors"
78
"fmt"
9+
"io"
810
"os"
11+
"path/filepath"
912
"time"
1013

1114
cmtjson "github.com/cometbft/cometbft/libs/json"
@@ -85,19 +88,19 @@ func (ag *AppGenesis) SaveAs(file string) error {
8588
return os.WriteFile(file, appGenesisBytes, 0o600)
8689
}
8790

88-
// AppGenesisFromFile reads the AppGenesis from the provided file.
89-
func AppGenesisFromFile(genFile string) (*AppGenesis, error) {
90-
jsonBlob, err := os.ReadFile(genFile)
91+
// AppGenesisFromReader reads the AppGenesis from the reader.
92+
func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) {
93+
jsonBlob, err := io.ReadAll(reader)
9194
if err != nil {
92-
return nil, fmt.Errorf("couldn't read AppGenesis file (%s): %w", genFile, err)
95+
return nil, err
9396
}
9497

9598
var appGenesis AppGenesis
9699
if err := json.Unmarshal(jsonBlob, &appGenesis); err != nil {
97100
// fallback to CometBFT genesis
98101
var ctmGenesis cmttypes.GenesisDoc
99102
if err2 := cmtjson.Unmarshal(jsonBlob, &ctmGenesis); err2 != nil {
100-
return nil, fmt.Errorf("error unmarshalling AppGenesis at %s: %w\n failed fallback to CometBFT GenDoc: %w", genFile, err, err2)
103+
return nil, fmt.Errorf("error unmarshalling AppGenesis: %w\n failed fallback to CometBFT GenDoc: %w", err, err2)
101104
}
102105

103106
appGenesis = AppGenesis{
@@ -118,6 +121,25 @@ func AppGenesisFromFile(genFile string) (*AppGenesis, error) {
118121
return &appGenesis, nil
119122
}
120123

124+
// AppGenesisFromFile reads the AppGenesis from the provided file.
125+
func AppGenesisFromFile(genFile string) (*AppGenesis, error) {
126+
file, err := os.Open(filepath.Clean(genFile))
127+
if err != nil {
128+
return nil, err
129+
}
130+
131+
appGenesis, err := AppGenesisFromReader(bufio.NewReader(file))
132+
if err != nil {
133+
return nil, fmt.Errorf("failed to read genesis from file %s: %w", genFile, err)
134+
}
135+
136+
if err := file.Close(); err != nil {
137+
return nil, err
138+
}
139+
140+
return appGenesis, nil
141+
}
142+
121143
// --------------------------
122144
// CometBFT Genesis Handling
123145
// --------------------------

0 commit comments

Comments
 (0)