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

refactor!: make ExportGenesis return proto.Message #12700

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
2e12fa1
initial progress
facundomedica Jul 22, 2022
4c3891f
small fix
facundomedica Jul 22, 2022
e084984
DefaultGenesis, InitGenesis
facundomedica Jul 25, 2022
aabb540
Merge branch 'main' into facu/import-export-genesis
facundomedica Jul 25, 2022
c8498f4
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Jul 25, 2022
2b6b679
fix legacy app
facundomedica Jul 25, 2022
7605079
Merge branch 'facu/import-export-genesis' of https://github.com/cosmo…
facundomedica Jul 25, 2022
15e4cf7
fix test
facundomedica Jul 25, 2022
69e8b5e
merge main
facundomedica Jul 26, 2022
4347cba
re-enabled test
facundomedica Jul 26, 2022
3e6692c
simplify type casts
facundomedica Jul 26, 2022
6d92f10
small fixes
facundomedica Jul 26, 2022
3d36566
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Jul 26, 2022
46a83e2
merge main; fix some issues
facundomedica Jul 27, 2022
735ee18
fixes
facundomedica Jul 27, 2022
5f2a5bf
fix
facundomedica Jul 27, 2022
036661c
Merge branch 'main' into facu/import-export-genesis
facundomedica Jul 27, 2022
96cb0ad
fix e2e
facundomedica Jul 27, 2022
12f18f9
Merge branch 'facu/import-export-genesis' of https://github.com/cosmo…
facundomedica Jul 27, 2022
cc81161
Merge branch 'main' into facu/import-export-genesis
facundomedica Jul 27, 2022
7475528
Merge branch 'main' into facu/import-export-genesis
facundomedica Jul 27, 2022
14d7fa6
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Jul 28, 2022
fed6baa
add marshal func
facundomedica Jul 28, 2022
c321ccd
fix test
facundomedica Jul 28, 2022
6ac9973
upgrading and changelog entry
facundomedica Jul 28, 2022
60e0995
move upgrading notes
facundomedica Jul 28, 2022
2a73e25
Merge branch 'main' into facu/import-export-genesis
facundomedica Jul 28, 2022
f031937
Merge branch 'main' into facu/import-export-genesis
julienrbrt Jul 28, 2022
4e855e3
Merge branch 'main' into facu/import-export-genesis
julienrbrt Jul 28, 2022
8561535
Merge branch 'main' into facu/import-export-genesis
julienrbrt Jul 28, 2022
5be7220
Merge branch 'main' into facu/import-export-genesis
julienrbrt Jul 28, 2022
4f5dbb7
Merge branch 'main' into facu/import-export-genesis
julienrbrt Jul 28, 2022
c3fee89
Merge branch 'main' into facu/import-export-genesis
facundomedica Jul 29, 2022
56e58ae
remove GenesisState type
facundomedica Aug 1, 2022
f87781f
don't use tmjson in utils
facundomedica Aug 1, 2022
d304999
fixes
facundomedica Aug 1, 2022
0181ccf
use reflect pkg to get genesis type
facundomedica Aug 2, 2022
161ea0e
fix conflcts
facundomedica Aug 2, 2022
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
14 changes: 13 additions & 1 deletion baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package baseapp_test

import (
"encoding/json"
"fmt"
"math"
"testing"
Expand Down Expand Up @@ -109,8 +110,19 @@ func TestBaseApp_BlockGas(t *testing.T) {
&testdata.TestMsg{},
)
genState := GenesisStateWithSingleValidator(t, cdc, appBuilder)
stateBytes, err := tmjson.MarshalIndent(genState, "", " ")

genStateJson := map[string]json.RawMessage{}
Copy link
Member

@julienrbrt julienrbrt Jul 27, 2022

Choose a reason for hiding this comment

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

It think it makes sense to have a helper for that. This logic is repeated very often.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I agree. The only difference among these is the indentation of the output. Would it make sense to have something like:
MarshalGenesisStateToJSON(gs map[string]proto.Message, cdc codec.Codec, indent bool) []byte.
Also, where would you place this helper?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah good question 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we can add such a function @facundomedica 👍

for k, v := range genState {
if v != nil {
genStateJson[k] = cdc.MustMarshalJSON(v)
} else {
genStateJson[k] = []byte("{}")
}
}

stateBytes, err := tmjson.MarshalIndent(genStateJson, "", " ")
require.NoError(t, err)

bapp.InitChain(abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: simtestutil.DefaultConsensusParams,
Expand Down
4 changes: 2 additions & 2 deletions baseapp/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package baseapp_test

import (
"context"
"encoding/json"
"testing"

"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/require"
tmtypes "github.com/tendermint/tendermint/types"

Expand Down Expand Up @@ -38,7 +38,7 @@ import (

// GenesisStateWithSingleValidator initializes GenesisState with a single validator and genesis accounts
// that also act as delegators.
func GenesisStateWithSingleValidator(t *testing.T, codec codec.Codec, builder *runtime.AppBuilder) map[string]json.RawMessage {
func GenesisStateWithSingleValidator(t *testing.T, codec codec.Codec, builder *runtime.AppBuilder) map[string]proto.Message {
t.Helper()

privVal := mock.NewPV()
Expand Down
12 changes: 11 additions & 1 deletion client/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client_test

import (
"context"
"encoding/json"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -73,7 +74,16 @@ func (s *IntegrationTestSuite) SetupSuite() {
genesisState, err := sims.GenesisStateWithValSet(cdc, appBuilder.DefaultGenesis(), valSet, []authtypes.GenesisAccount{acc}, balance)
s.NoError(err)

stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ")
genStateJson := map[string]json.RawMessage{}
for k, v := range genesisState {
if v != nil {
genStateJson[k] = cdc.MustMarshalJSON(v)
} else {
genStateJson[k] = []byte("{}")
}
}

stateBytes, err := tmjson.MarshalIndent(genStateJson, "", " ")
s.NoError(err)

// init chain will set the validator set and initialize the genesis accounts
Expand Down
1 change: 1 addition & 0 deletions runtime/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (a *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.Respo
if err := json.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}

return a.ModuleManager.InitGenesis(ctx, a.cdc, genesisState)
}

Expand Down
6 changes: 3 additions & 3 deletions runtime/builder.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package runtime

import (
"encoding/json"
"io"

"github.com/gogo/protobuf/proto"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

Expand All @@ -20,8 +20,8 @@ type AppBuilder struct {

// DefaultGenesis returns a default genesis from the registered
// AppModuleBasic's.
func (a *AppBuilder) DefaultGenesis() map[string]json.RawMessage {
return a.app.basicManager.DefaultGenesis(a.app.cdc)
func (a *AppBuilder) DefaultGenesis() map[string]proto.Message {
Copy link
Contributor

Choose a reason for hiding this comment

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

Will we break the AppModule interface again once we remove gogoproto?

How hard would it be to use the api/ genesis state today?

Copy link
Member

Choose a reason for hiding this comment

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

pulsar is not production ready from my understanding. We shouldn't force people to use it yet. the interface break is fairly small in the future, if we document then people will be fine with it

return a.app.basicManager.DefaultGenesis()
}

// Build builds an *App instance.
Expand Down
2 changes: 1 addition & 1 deletion simapp/app_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func (a *SimApp) Configurator() module.Configurator {

// InitChainer application update at chain initialization
func (app *SimApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
var genesisState GenesisState
var genesisState map[string]json.RawMessage
if err := json.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}
Expand Down
12 changes: 7 additions & 5 deletions simapp/app_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package simapp

import (
"encoding/json"
"testing"

"github.com/golang/mock/gomock"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/tests/mocks"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
Expand Down Expand Up @@ -60,7 +60,8 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) {
logger2, _ := log.NewDefaultLogger("plain", "info", false)
// Making a new app object with the db, so that initchain hasn't been called
app2 := NewSimApp(logger2, db, nil, true, encCfg, simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome))
_, err := app2.ExportAppStateAndValidators(false, []string{})
exportedApp, err := app2.ExportAppStateAndValidators(false, []string{})
println(string(exportedApp.AppState))
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}

Expand Down Expand Up @@ -214,9 +215,10 @@ func TestInitGenesisOnMigration(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)
mockModule := mocks.NewMockAppModule(mockCtrl)
mockDefaultGenesis := json.RawMessage(`{"key": "value"}`)
mockModule.EXPECT().DefaultGenesis(gomock.Eq(app.appCodec)).Times(1).Return(mockDefaultGenesis)
mockModule.EXPECT().InitGenesis(gomock.Eq(ctx), gomock.Eq(app.appCodec), gomock.Eq(mockDefaultGenesis)).Times(1).Return(nil)
mockDefaultGenesis := &testdata.AnyWithExtra{B: 111}

mockModule.EXPECT().DefaultGenesis().Times(1).Return(mockDefaultGenesis)
mockModule.EXPECT().InitGenesis(gomock.Eq(ctx), gomock.Eq(mockDefaultGenesis)).Times(1).Return(nil)
mockModule.EXPECT().ConsensusVersion().Times(1).Return(uint64(0))

app.ModuleManager.Modules["mock"] = mockModule
Expand Down
14 changes: 12 additions & 2 deletions simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,18 @@ func (app *SimApp) ExportAppStateAndValidators(
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
}

genState := app.ModuleManager.ExportGenesis(ctx, app.appCodec)
appState, err := json.MarshalIndent(genState, "", " ")
genState := app.ModuleManager.ExportGenesis(ctx)

genStateJson := map[string]json.RawMessage{}
for k, v := range genState {
if v != nil {
genStateJson[k] = app.appCodec.MustMarshalJSON(v)
} else {
genStateJson[k] = []byte("{}")
}
}

appState, err := json.MarshalIndent(genStateJson, "", " ")
if err != nil {
return servertypes.ExportedApp{}, err
}
Expand Down
7 changes: 3 additions & 4 deletions simapp/genesis.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package simapp

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/gogo/protobuf/proto"
)

// GenesisState of the blockchain is represented here as a map of raw json
Expand All @@ -13,9 +12,9 @@ import (
// Within this application default genesis information is retrieved from
// the ModuleBasicManager which populates json from each BasicModule
// object provided to it during init.
type GenesisState map[string]json.RawMessage
type GenesisState map[string]proto.Message
facundomedica marked this conversation as resolved.
Show resolved Hide resolved

// NewDefaultGenesisState generates the default state for the application.
func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState {
return ModuleBasics.DefaultGenesis(cdc)
return ModuleBasics.DefaultGenesis()
}
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 12 additions & 1 deletion simapp/integration/server/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -132,7 +133,17 @@ func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, *t
app := simapp.NewSimApp(logger, db, nil, true, encCfg, simtestutil.NewAppOptionsWithFlagHome(tempDir))

genesisState := simapp.GenesisStateWithSingleValidator(t, app)
stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ")

genStateJson := map[string]json.RawMessage{}
for k, v := range genesisState {
if v != nil {
genStateJson[k] = encCfg.Codec.MustMarshalJSON(v)
} else {
genStateJson[k] = []byte("{}")
}
}

stateBytes, err := tmjson.MarshalIndent(genStateJson, "", " ")
require.NoError(t, err)

serverCtx := server.NewDefaultContext()
Expand Down
3 changes: 2 additions & 1 deletion simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ func TestAppImportExport(t *testing.T) {
newApp := NewSimApp(log.NewNopLogger(), newDB, nil, true, MakeTestEncodingConfig(), appOptions, fauxMerkleModeOpt)
require.Equal(t, "SimApp", newApp.Name())

var genesisState GenesisState
// var genesisState GenesisState
var genesisState map[string]json.RawMessage
err = json.Unmarshal(exported.AppState, &genesisState)
require.NoError(t, err)

Expand Down
26 changes: 15 additions & 11 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,35 +354,39 @@ func initGenFiles(
genAccounts []authtypes.GenesisAccount, genBalances []banktypes.Balance,
genFiles []string, numValidators int,
) error {
appGenState := mbm.DefaultGenesis(clientCtx.Codec)
appGenState := mbm.DefaultGenesis()

// set the accounts in the genesis state
var authGenState authtypes.GenesisState
clientCtx.Codec.MustUnmarshalJSON(appGenState[authtypes.ModuleName], &authGenState)
authGenState := appGenState[authtypes.ModuleName].(*authtypes.GenesisState)

accounts, err := authtypes.PackAccounts(genAccounts)
if err != nil {
return err
}

authGenState.Accounts = accounts
appGenState[authtypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&authGenState)
appGenState[authtypes.ModuleName] = authGenState

// set the balances in the genesis state
var bankGenState banktypes.GenesisState
clientCtx.Codec.MustUnmarshalJSON(appGenState[banktypes.ModuleName], &bankGenState)
bankGenState := appGenState[banktypes.ModuleName].(*banktypes.GenesisState)

bankGenState.Balances = banktypes.SanitizeGenesisBalances(genBalances)
for _, bal := range bankGenState.Balances {
bankGenState.Supply = bankGenState.Supply.Add(bal.Coins...)
}
appGenState[banktypes.ModuleName] = clientCtx.Codec.MustMarshalJSON(&bankGenState)

appGenStateJSON, err := json.MarshalIndent(appGenState, "", " ")
if err != nil {
return err
appGenState[banktypes.ModuleName] = bankGenState

genStateJson := map[string]json.RawMessage{}
for k, v := range appGenState {
if v != nil {
genStateJson[k] = clientCtx.Codec.MustMarshalJSON(v)
} else {
genStateJson[k] = []byte("{}")
}
}

appGenStateJSON, err := json.MarshalIndent(genStateJson, "", " ")

genDoc := types.GenesisDoc{
ChainID: chainID,
AppState: appGenStateJSON,
Expand Down
4 changes: 2 additions & 2 deletions simapp/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile str
panic(err)
}

var authGenesis authtypes.GenesisState
var authGenesis *authtypes.GenesisState
if appState[authtypes.ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[authtypes.ModuleName], &authGenesis)
authGenesis = appState[authtypes.ModuleName].(*authtypes.GenesisState)
}

newAccs := make([]simtypes.Account, len(authGenesis.Accounts))
Expand Down
24 changes: 21 additions & 3 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@ func NewSimappWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptio

if !isCheckTx {
// init chain must be called to stop deliverState from being nil
stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ")
genStateJson := map[string]json.RawMessage{}
for k, v := range genesisState {
if v != nil {
genStateJson[k] = app.appCodec.MustMarshalJSON(v)
} else {
genStateJson[k] = []byte("{}")
}
}

stateBytes, err := tmjson.MarshalIndent(genStateJson, "", " ")
require.NoError(t, err)

// Initialize the chain
Expand Down Expand Up @@ -139,7 +148,16 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
genesisState, err := simtestutil.GenesisStateWithValSet(app.AppCodec(), genesisState, valSet, genAccs, balances...)
require.NoError(t, err)

stateBytes, err := json.MarshalIndent(genesisState, "", " ")
genStateJson := map[string]json.RawMessage{}
for k, v := range genesisState {
if v != nil {
genStateJson[k] = app.appCodec.MustMarshalJSON(v)
} else {
genStateJson[k] = []byte("{}")
}
}

stateBytes, err := tmjson.MarshalIndent(genStateJson, "", " ")
require.NoError(t, err)

// init chain will set the validator set and initialize the genesis accounts
Expand Down Expand Up @@ -378,7 +396,7 @@ func NewTestNetworkFixture() network.TestFixture {

return network.TestFixture{
AppConstructor: appCtr,
GenesisState: ModuleBasics.DefaultGenesis(cfg.Codec),
GenesisState: ModuleBasics.DefaultGenesis(),
EncodingConfig: cfg,
}
}
Loading