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: export genesis in simapp v2 #21199

Merged
merged 48 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
ebf5006
add state export
tac0turtle Jun 24, 2024
268c034
minor
tac0turtle Jun 24, 2024
babf339
Merge branch 'main' into marko/state_export
tac0turtle Jul 1, 2024
6780043
comment out export zero height
tac0turtle Jul 1, 2024
61c0343
Merge branch 'main' into marko/state_export
randygrok Jul 30, 2024
b8ac0e5
add logic for export app state and bootstrap simapp tests
randygrok Aug 6, 2024
a1b0a5f
fix bug with export genesis nil pointer
randygrok Aug 6, 2024
71d0027
some additions to continue the test
randygrok Aug 6, 2024
cadccd4
add context with value
randygrok Aug 6, 2024
7bc73f0
remove commented code
randygrok Aug 7, 2024
728313a
go mod tidy all
randygrok Aug 7, 2024
e97fda6
add version on export
randygrok Aug 7, 2024
57c9751
include export validators
randygrok Aug 8, 2024
a32d778
add validators into status
randygrok Aug 8, 2024
4bcf345
include new types mode
randygrok Aug 9, 2024
7006da5
go mod tidy all
randygrok Aug 13, 2024
91570fb
temp commit, include logic for zeroheight
randygrok Aug 13, 2024
de93866
remove zero height part
randygrok Aug 13, 2024
9fc2546
add export
randygrok Aug 13, 2024
7a1fa60
apply into simapp v2
randygrok Aug 13, 2024
6b32d53
Merge remote-tracking branch 'origin/main' into randy/export-genesis
randygrok Aug 13, 2024
db542cf
remove option to export certain modules only
randygrok Aug 13, 2024
70f1090
remove modules to export feature
randygrok Aug 13, 2024
ea0193d
remove modules to export feature part 2
randygrok Aug 13, 2024
c552beb
remove debug entry in manager
randygrok Aug 13, 2024
f67770e
delete commented code
randygrok Aug 14, 2024
2948e09
rename RunWithCtx
randygrok Aug 14, 2024
a0ffd0a
race condition not fixed yet
randygrok Aug 14, 2024
519d6b7
remove concurrent export
randygrok Aug 14, 2024
f199f06
include issue number
randygrok Aug 14, 2024
11ec8d2
fix lint
randygrok Aug 15, 2024
a634633
fix lint, last
randygrok Aug 15, 2024
e0093cf
move ExportedApp to genutil
randygrok Aug 15, 2024
5ab7c1a
Merge branch 'main' into randy/export-genesis
randygrok Aug 15, 2024
da07a4a
lint
randygrok Aug 15, 2024
264434e
Merge branch 'randy/export-genesis' of git.luolix.top-randy:cosmos/cosmos…
randygrok Aug 15, 2024
aedc7b0
remove duplicated package on import
randygrok Aug 15, 2024
8f97b21
lint
randygrok Aug 15, 2024
9156b86
linter
randygrok Aug 15, 2024
858af16
pass linter
randygrok Aug 15, 2024
a789413
remove unsafe message
randygrok Aug 15, 2024
dee4c3b
remove command
randygrok Aug 15, 2024
5051c67
review changes
randygrok Aug 15, 2024
73e839b
Revert "remove option to export certain modules only"
randygrok Aug 15, 2024
98b4397
remove comment
randygrok Aug 15, 2024
4324f42
Merge branch 'main' into randy/export-genesis
randygrok Aug 15, 2024
3ea6367
bring back viper sets
randygrok Aug 16, 2024
1874e0c
Merge branch 'randy/export-genesis' of git.luolix.top-randy:cosmos/cosmos…
randygrok Aug 16, 2024
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
5 changes: 5 additions & 0 deletions runtime/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (a *App[T]) LoadHeight(height uint64) error {
return a.db.LoadVersion(height)
}

// LoadLatestHeight loads the latest height.
func (a *App[T]) LoadLatestHeight() (uint64, error) {
return a.db.GetLatestVersion()
}

// Close is called in start cmd to gracefully cleanup resources.
func (a *App[T]) Close() error {
return nil
Expand Down
15 changes: 14 additions & 1 deletion runtime/v2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (a *AppBuilder[T]) RegisterModules(modules map[string]appmodulev2.AppModule

// RegisterStores registers the provided store keys.
// This method should only be used for registering extra stores
// wiich is necessary for modules that not registered using the app config.
// which is necessary for modules that not registered using the app config.
// To be used in combination of RegisterModules.
func (a *AppBuilder[T]) RegisterStores(keys ...string) {
a.app.storeKeys = append(a.app.storeKeys, keys...)
Expand Down Expand Up @@ -175,6 +175,19 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
}
return nil
},
ExportGenesis: func(ctx context.Context, version uint64) ([]byte, error) {
genesisJson, err := a.app.moduleManager.ExportGenesisForModules(ctx)
if err != nil {
return nil, fmt.Errorf("failed to export genesis: %w", err)
}

bz, err := json.Marshal(genesisJson)
if err != nil {
return nil, fmt.Errorf("failed to marshal genesis: %w", err)
}

return bz, nil
},
}

appManager, err := appManagerBuilder.Build()
Expand Down
8 changes: 4 additions & 4 deletions runtime/v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,9 @@ func (m *MM[T]) InitGenesisJSON(
// ExportGenesisForModules performs export genesis functionality for modules
func (m *MM[T]) ExportGenesisForModules(
ctx context.Context,
modulesToExport ...string,
randygrok marked this conversation as resolved.
Show resolved Hide resolved
) (map[string]json.RawMessage, error) {
if len(modulesToExport) == 0 {
modulesToExport = m.config.ExportGenesis
}
modulesToExport := m.config.ExportGenesis

// verify modules exists in app, so that we don't panic in the middle of an export
if err := m.checkModulesExists(modulesToExport); err != nil {
return nil, err
Expand All @@ -221,6 +219,8 @@ func (m *MM[T]) ExportGenesisForModules(
moduleI = module.(ModuleI)
} else if module, hasABCIGenesis := mod.(appmodulev2.HasGenesis); hasABCIGenesis {
moduleI = module.(ModuleI)
} else {
continue
}

channels[moduleName] = make(chan genesisResult)
Expand Down
34 changes: 33 additions & 1 deletion server/v2/appmanager/appmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,24 @@ func (a AppManager[T]) InitGenesis(

// ExportGenesis exports the genesis state of the application.
func (a AppManager[T]) ExportGenesis(ctx context.Context, version uint64) ([]byte, error) {
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
bz, err := a.exportGenesis(ctx, version)
zeroState, err := a.db.StateAt(version)
if err != nil {
return nil, fmt.Errorf("unable to get latest state: %w", err)
}

var bz = make([]byte, 0)
_, err = a.stf.RunWithCtx(ctx, zeroState, func(ctx context.Context) error {
if a.exportGenesis == nil {
return errors.New("export genesis function not set")
}

bz, err = a.exportGenesis(ctx, version)
if err != nil {
return fmt.Errorf("failed to export genesis state: %w", err)
}

return nil
})
if err != nil {
return nil, fmt.Errorf("failed to export genesis state: %w", err)
}
Expand Down Expand Up @@ -169,3 +186,18 @@ func (a AppManager[T]) QueryWithState(
) (transaction.Msg, error) {
return a.stf.Query(ctx, state, a.config.QueryGasLimit, request)
}

// UnsafeRunWithCtx is made to support genesis, if genesis was just the execution of messages instead
randygrok marked this conversation as resolved.
Show resolved Hide resolved
// of being something custom then we would not need this. PLEASE DO NOT USE.
// TODO: Remove
func (a AppManager[T]) UnsafeRunWithCtx(
ctx context.Context,
closure func(ctx context.Context) error,
) (corestore.WriterMap, error) {
_, st, err := a.db.StateLatest()
if err != nil {
return nil, fmt.Errorf("unable to get latest state: %w", err)
}

return a.stf.RunWithCtx(ctx, st, closure)
}
2 changes: 1 addition & 1 deletion server/v2/cometbft/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
cosmossdk.io/errors v1.0.1
cosmossdk.io/log v1.4.0
cosmossdk.io/server/v2 v2.0.0-00010101000000-000000000000
cosmossdk.io/server/v2/appmanager v0.0.0-00010101000000-000000000000
cosmossdk.io/server/v2/appmanager v0.0.0-20240802110823-cffeedff643d
cosmossdk.io/store/v2 v2.0.0-00010101000000-000000000000
cosmossdk.io/x/consensus v0.0.0-00010101000000-000000000000
github.com/cometbft/cometbft v1.0.0-rc1
Expand Down
15 changes: 12 additions & 3 deletions server/v2/types.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package serverv2

import (
gogoproto "github.com/cosmos/gogoproto/proto"
"github.com/spf13/viper"

coreapp "cosmossdk.io/core/app"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
"cosmossdk.io/server/v2/appmanager"
"encoding/json"
gogoproto "github.com/cosmos/gogoproto/proto"
"github.com/spf13/viper"
)

type AppCreator[T transaction.Tx] func(log.Logger, *viper.Viper) AppI[T]
Expand All @@ -20,3 +20,12 @@ type AppI[T transaction.Tx] interface {
GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message
GetStore() any
}

// ExportedApp represents an exported app state, along with
// validators, consensus params and latest app height.
type ExportedApp struct {
// AppState is the application state as JSON.
AppState json.RawMessage
// Height is the app's latest block height.
Height int64
}
3 changes: 0 additions & 3 deletions simapp/v2/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package simapp

import (
_ "embed"

"github.com/spf13/viper"

clienthelpers "cosmossdk.io/client/v2/helpers"
Expand All @@ -12,7 +11,6 @@ import (
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/runtime/v2"
serverv2 "cosmossdk.io/server/v2"
"cosmossdk.io/x/accounts"
authkeeper "cosmossdk.io/x/auth/keeper"
authzkeeper "cosmossdk.io/x/authz/keeper"
Expand Down Expand Up @@ -92,7 +90,6 @@ func NewSimApp[T transaction.Tx](
logger log.Logger,
viper *viper.Viper,
) *SimApp[T] {
viper.Set(serverv2.FlagHome, DefaultNodeHome) // TODO possibly set earlier when viper is created
randygrok marked this conversation as resolved.
Show resolved Hide resolved
var (
app = &SimApp[T]{}
appBuilder *runtime.AppBuilder[T]
Expand Down
151 changes: 151 additions & 0 deletions simapp/v2/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package simapp

import (
"context"
app2 "cosmossdk.io/core/app"
"cosmossdk.io/core/comet"
context2 "cosmossdk.io/core/context"
sdkmath "cosmossdk.io/math"
serverv2 "cosmossdk.io/server/v2"
authtypes "cosmossdk.io/x/auth/types"
banktypes "cosmossdk.io/x/bank/types"
"crypto/sha256"
"encoding/json"
"github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/testutil/mock"
sdk "github.com/cosmos/cosmos-sdk/types"
"testing"
"time"

"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
comettypes "cosmossdk.io/server/v2/cometbft/types"
"cosmossdk.io/store/v2/db"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)

func NewTestApp(t *testing.T) (*SimApp[transaction.Tx], context.Context) {
logger := log.NewTestLogger(t)

vp := viper.New()
vp.Set("store.app-db-backend", string(db.DBTypeGoLevelDB))
vp.Set(serverv2.FlagHome, t.TempDir())

app := NewSimApp[transaction.Tx](logger, vp)
genesis := app.ModuleManager().DefaultGenesis()

privVal := mock.NewPV()
pubKey, err := privVal.GetPubKey()
require.NoError(t, err)

// create validator set with single validator
validator := types.NewValidator(pubKey, 1)
valSet := types.NewValidatorSet([]*types.Validator{validator})

// generate genesis account
senderPrivKey := secp256k1.GenPrivKey()
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100000000000000))),
}

genesis, err = simtestutil.GenesisStateWithValSet(
app.AppCodec(),
genesis,
valSet,
[]authtypes.GenesisAccount{acc},
balance,
)
require.NoError(t, err)

genesisBytes, err := json.Marshal(genesis)
require.NoError(t, err)

st := app.GetStore().(comettypes.Store)
ci, err := st.LastCommitID()
require.NoError(t, err)

bz := sha256.Sum256([]byte{})

ctx := context.Background()
Copy link
Contributor

Choose a reason for hiding this comment

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

Use context with timeout or cancellation.

Using context.Background() does not allow for timeout or cancellation. Consider using context.WithTimeout or context.WithCancel to handle potential long-running operations.

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()


_, newState, err := app.InitGenesis(
ctx,
&app2.BlockRequest[transaction.Tx]{
0,
time.Now(),
bz[:],
"theChain",
ci.Hash,
nil,
true,
},
genesisBytes,
nil,
)
require.NoError(t, err)

changes, err := newState.GetStateChanges()
require.NoError(t, err)

_, err = st.Commit(&store.Changeset{changes})
require.NoError(t, err)

return app, ctx
}

func MoveNextBlock(t *testing.T, app *SimApp[transaction.Tx], ctx context.Context) {
bz := sha256.Sum256([]byte{})

st := app.GetStore().(comettypes.Store)
ci, err := st.LastCommitID()
require.NoError(t, err)

height, err := app.LoadLatestHeight()
require.NoError(t, err)

// TODO: this is a hack to set the comet info in the context for distribution module dependency.
ctx = context.WithValue(ctx, context2.CometInfoKey, comet.Info{
Evidence: nil,
ValidatorsHash: nil,
ProposerAddress: nil,
LastCommit: comet.CommitInfo{},
})

_, newState, err := app.DeliverBlock(
ctx,
&app2.BlockRequest[transaction.Tx]{
Height: height + 1,
Time: time.Now(),
Hash: bz[:],
AppHash: ci.Hash,
})
require.NoError(t, err)

changes, err := newState.GetStateChanges()
require.NoError(t, err)

_, err = st.Commit(&store.Changeset{Changes: changes})
require.NoError(t, err)
}

func TestSimAppExportAndBlockedAddrs_WithOneBlockProduced(t *testing.T) {
app, ctx := NewTestApp(t)

MoveNextBlock(t, app, ctx)

_, err := app.ExportAppStateAndValidators(nil)
require.NoError(t, err)
}

func TestSimAppExportAndBlockedAddrs_NoBlocksProduced(t *testing.T) {
app, _ := NewTestApp(t)

_, err := app.ExportAppStateAndValidators(nil)
require.NoError(t, err)
}
26 changes: 22 additions & 4 deletions simapp/v2/export.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
package simapp

import (
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"context"
serverv2 "cosmossdk.io/server/v2"
)

// ExportAppStateAndValidators exports the state of the application for a genesis file.
func (app *SimApp[T]) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAddrs, modulesToExport []string) (servertypes.ExportedApp, error) {
panic("not implemented")
// ExportAppStateAndValidators exports the state of the application for a genesis
// file.
func (app *SimApp[T]) ExportAppStateAndValidators(jailAllowedAddrs []string) (serverv2.ExportedApp, error) {
// as if they could withdraw from the start of the next block
ctx := context.Background()
Copy link
Contributor

Choose a reason for hiding this comment

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

Use context with timeout or cancellation.

Using context.Background() does not allow for timeout or cancellation. Consider using context.WithTimeout or context.WithCancel to handle potential long-running operations.

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()


latestHeight, err := app.LoadLatestHeight()
if err != nil {
return serverv2.ExportedApp{}, err
}

genesis, err := app.ExportGenesis(ctx, latestHeight)
if err != nil {
return serverv2.ExportedApp{}, err
}

return serverv2.ExportedApp{
AppState: genesis,
Height: int64(latestHeight),
}, nil
}
7 changes: 4 additions & 3 deletions simapp/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
cosmossdk.io/runtime/v2 v2.0.0-00010101000000-000000000000
cosmossdk.io/server/v2 v2.0.0-20240718121635-a877e3e8048a
cosmossdk.io/server/v2/cometbft v0.0.0-00010101000000-000000000000
cosmossdk.io/store/v2 v2.0.0 // indirect
cosmossdk.io/store/v2 v2.0.0
cosmossdk.io/tools/confix v0.0.0-00010101000000-000000000000
cosmossdk.io/x/accounts v0.0.0-20240226161501-23359a0b6d91
cosmossdk.io/x/auth v0.0.0-00010101000000-000000000000
Expand Down Expand Up @@ -44,6 +44,8 @@ require (

require cosmossdk.io/core/testing v0.0.0-00010101000000-000000000000 // indirect
randygrok marked this conversation as resolved.
Show resolved Hide resolved

require github.com/cometbft/cometbft/api v1.0.0-rc.1 // indirect

require (
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2 // indirect
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2 // indirect
Expand All @@ -57,7 +59,7 @@ require (
cosmossdk.io/errors v1.0.1 // indirect
cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5 // indirect
cosmossdk.io/schema v0.1.1 // indirect
cosmossdk.io/server/v2/appmanager v0.0.0-00010101000000-000000000000 // indirect
cosmossdk.io/server/v2/appmanager v0.0.0-20240802110823-cffeedff643d // indirect
cosmossdk.io/server/v2/stf v0.0.0-00010101000000-000000000000 // indirect
cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc // indirect
cosmossdk.io/x/accounts/defaults/lockup v0.0.0-20240417181816-5e7aae0db1f5 // indirect
Expand Down Expand Up @@ -86,7 +88,6 @@ require (
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft-db v0.12.0 // indirect
github.com/cometbft/cometbft/api v1.0.0-rc.1 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.5 // indirect
github.com/cosmos/crypto v0.1.2 // indirect
Expand Down
Loading
Loading