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

v2 genesis #20076

Merged
merged 19 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
"mode": "debug",
"program": "simapp/v2/simdv2",
"args": ["start"],
// "args": ["genesis", "add-genesis-account", "cosmos130ch823d2pwh9wpfm335plg6ktatzw7j427qgs", "1000000000stake"],
},
{
"name": "simapp v1",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "simapp/simd",
//"args": ["start"],
"args": ["genesis", "add-genesis-account", "cosmos1r8prwezs37hvxq4kc520pfhg67lgz42m4mdvrf", "1000000000stake"],
}
]
}
6 changes: 4 additions & 2 deletions core/app/identity.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package app

var RuntimeIdentity = []byte("runtime")
var ConsensusIdentity = []byte("consensus")
var (
RuntimeIdentity = []byte("runtime")
ConsensusIdentity = []byte("consensus")
)
4 changes: 4 additions & 0 deletions core/appmodule/v2/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ type HasGenesis interface {
InitGenesis(ctx context.Context, data json.RawMessage) error
ExportGenesis(ctx context.Context) (json.RawMessage, error)
}

type HasABCIGenesis interface {
InitGenesis(ctx context.Context, data json.RawMessage) ([]ValidatorUpdate, error)
}
6 changes: 6 additions & 0 deletions core/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ const (
ExecModeSimulate
ExecModeFinalize
)

// TODO: remove
type ContextKey string

// TODO: remove
const CometInfoKey ContextKey = "comet-info"
Copy link
Member Author

Choose a reason for hiding this comment

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

For usage see: https://github.com/cosmos/cosmos-sdk/blob/kocu/genesis-v2/x/distribution/keeper/abci.go#L27
I think this can be resolved with #19602 which adds a message to retrieve CometInfo from the RouterService, or optionally including a keeper.

1 change: 1 addition & 0 deletions core/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Codec[T Tx] interface {
// Decode decodes the tx bytes into a DecodedTx, containing
// both concrete and bytes representation of the tx.
Decode([]byte) (T, error)
DecodeJSON([]byte) (T, error)
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
}

type Tx interface {
Expand Down
16 changes: 16 additions & 0 deletions runtime/v2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"

appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/store"
Expand Down Expand Up @@ -120,6 +121,21 @@ func (a *AppBuilder) Build(opts ...AppBuilderOption) (*App, error) {
ValidateTxGasLimit: a.app.config.GasConfig.ValidateTxGasLimit,
QueryGasLimit: a.app.config.GasConfig.QueryGasLimit,
SimulationGasLimit: a.app.config.GasConfig.SimulationGasLimit,
InitGenesis: func(ctx context.Context, src io.Reader, txHandler func(json.RawMessage) error) error {
// this implementation assumes that the state is a JSON object
bz, err := io.ReadAll(src)
if err != nil {
return fmt.Errorf("failed to read import state: %w", err)
}
var genesisState map[string]json.RawMessage
if err = json.Unmarshal(bz, &genesisState); err != nil {
return err
}
if err = a.app.moduleManager.InitGenesisJSON(ctx, genesisState, txHandler); err != nil {
return fmt.Errorf("failed to init genesis: %w", err)
}
return nil
},
}

appManager, err := appManagerBuilder.Build()
Expand Down
2 changes: 2 additions & 0 deletions runtime/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ require (
cosmossdk.io/math v1.3.0 // indirect
cosmossdk.io/store v1.1.0 // indirect
cosmossdk.io/x/auth v0.0.0-00010101000000-000000000000 // indirect
cosmossdk.io/x/bank v0.0.0-00010101000000-000000000000 // indirect
cosmossdk.io/x/staking v0.0.0-00010101000000-000000000000 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.2 // indirect
Expand Down
58 changes: 52 additions & 6 deletions runtime/v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

"github.com/cosmos/cosmos-sdk/codec"
sdkmodule "github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
Copy link
Member Author

Choose a reason for hiding this comment

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

To remove this dependency a genesis themed extension interface in x/genutil could be used to return []json.RawMessage for server v2.

)

type MM struct {
Expand Down Expand Up @@ -140,13 +141,58 @@
return nil
}

// ExportGenesis performs export genesis functionality for modules
func (m *MM) ExportGenesis(ctx context.Context, modulesToExport ...string) (map[string]json.RawMessage, error) {
if len(modulesToExport) == 0 {
modulesToExport = m.config.ExportGenesis
}
// InitGenesisJSON performs init genesis functionality for modules from genesis data in JSON format
func (m *MM) InitGenesisJSON(ctx context.Context, genesisData map[string]json.RawMessage, txHandler func(json.RawMessage) error) error {
m.logger.Info("initializing blockchain state from genesis.json", "order", m.config.InitGenesis)
var seenValUpdates bool
for _, moduleName := range m.config.InitGenesis {
if genesisData[moduleName] == nil {
continue
}

mod := m.modules[moduleName]

// skip genutil as it's a special module that handles gentxs
// TODO: should this be an empty extension interface on genutil for server v2?
if moduleName == "genutil" {
var genesisState types.GenesisState
err := m.cdc.UnmarshalJSON(genesisData[moduleName], &genesisState)
if err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", moduleName, err)
}
for _, jsonTx := range genesisState.GenTxs {
txHandler(jsonTx)
Fixed Show fixed Hide fixed

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}
continue
}

return m.ExportGenesisForModules(ctx, modulesToExport...)
// we might get an adapted module, a native core API module or a legacy module
if _, ok := mod.(appmodule.HasGenesisAuto); ok {
panic(fmt.Sprintf("module %s isn't server/v2 compatible", moduleName))
} else if module, ok := mod.(appmodulev2.HasGenesis); ok {
m.logger.Debug("running initialization for module", "module", moduleName)
if err := module.InitGenesis(ctx, genesisData[moduleName]); err != nil {
return err
}
} else if module, ok := mod.(appmodulev2.HasABCIGenesis); ok {
m.logger.Debug("running initialization for module", "module", moduleName)
moduleValUpdates, err := module.InitGenesis(ctx, genesisData[moduleName])
if err != nil {
return err
}

// use these validator updates if provided, the module manager assumes
// only one module will update the validator set
if len(moduleValUpdates) > 0 {
if seenValUpdates {
return errors.New("validator InitGenesis updates already set by a previous module")
} else {
seenValUpdates = true
}
}
}
}
return nil
}

// ExportGenesisForModules performs export genesis functionality for modules
Expand Down
3 changes: 2 additions & 1 deletion runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func init() {
ProvideGenesisTxHandler,
ProvideAppVersionModifier,
),
appconfig.Invoke(SetupAppBuilder),
)
}

Expand Down Expand Up @@ -163,7 +164,7 @@ type AppInputs struct {
InterfaceRegistry codectypes.InterfaceRegistry
LegacyAmino *codec.LegacyAmino
Logger log.Logger
StoreOptions *rootstorev2.FactoryOptions
StoreOptions *rootstorev2.FactoryOptions `optional:"true"`
}

func SetupAppBuilder(inputs AppInputs) {
Expand Down
52 changes: 33 additions & 19 deletions server/v2/appmanager/appmanager.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package appmanager

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"

appmanager "cosmossdk.io/core/app"
"cosmossdk.io/core/header"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/server/v2/appmanager/store"
Expand All @@ -22,38 +23,51 @@ type AppManager[T transaction.Tx] struct {
exportState func(ctx context.Context, dst map[string]io.Writer) error
importState func(ctx context.Context, src map[string]io.Reader) error

initGenesis func(ctx context.Context, state io.Reader, txHandler func(tx json.RawMessage) error) error

stf *stf.STF[T]
}

func (a AppManager[T]) InitGenesis(
ctx context.Context,
headerInfo header.Info,
consensusMessages []transaction.Type,
blockRequest *appmanager.BlockRequest[T],
initGenesisJSON []byte,
) (corestore.WriterMap, error) {
txDecoder transaction.Codec[T],
) (*appmanager.BlockResponse, corestore.WriterMap, error) {
v, zeroState, err := a.db.StateLatest()
if err != nil {
return nil, nil, fmt.Errorf("unable to get latest state: %w", err)
}
if v != 0 {
return nil, nil, fmt.Errorf("cannot init genesis on non-zero state")
}

var genTxs []T
zeroState, err = a.stf.RunWithCtx(ctx, zeroState, func(ctx context.Context) error {
return a.initGenesis(ctx, bytes.NewBuffer(initGenesisJSON), func(jsonTx json.RawMessage) error {
genTx, err := txDecoder.DecodeJSON(jsonTx)
if err != nil {
return fmt.Errorf("failed to decode genesis transaction: %w", err)
}
genTxs = append(genTxs, genTx)
return nil
})
})
if err != nil {
return nil, nil, fmt.Errorf("failed to import genesis state: %w", err)
}
// run block 0
// TODO: in an ideal world, genesis state is simply an initial state being applied
// unaware of what that state means in relation to every other, so here we can
// chain genesis
block0 := &appmanager.BlockRequest[T]{
Height: uint64(headerInfo.Height),
Time: headerInfo.Time,
Hash: headerInfo.Hash,
ChainId: headerInfo.ChainID,
AppHash: headerInfo.AppHash,
Txs: nil,
ConsensusMessages: consensusMessages,
}
blockRequest.Txs = genTxs

_, genesisState, err := a.DeliverBlock(ctx, block0)
blockresponse, genesisState, err := a.stf.DeliverBlock(ctx, blockRequest, zeroState)
if err != nil {
return nil, err
return blockresponse, nil, fmt.Errorf("failed to deliver block 0: %w", err)
}

// TODO: ok so the problem we have now, the genesis is a mix of initial state
// then followed by txs from the genutil module.

return genesisState, nil
return blockresponse, genesisState, err
}

func (a AppManager[T]) DeliverBlock(
Expand Down
7 changes: 7 additions & 0 deletions server/v2/appmanager/appmanager_builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package appmanager

import (
"context"
"encoding/json"
"io"

"cosmossdk.io/core/transaction"
"cosmossdk.io/server/v2/appmanager/store"
"cosmossdk.io/server/v2/stf"
Expand All @@ -12,6 +16,8 @@ type Builder[T transaction.Tx] struct {
ValidateTxGasLimit,
QueryGasLimit,
SimulationGasLimit uint64

InitGenesis func(ctx context.Context, src io.Reader, txHandler func(json.RawMessage) error) error
}

func (b Builder[T]) Build() (*AppManager[T], error) {
Expand All @@ -24,6 +30,7 @@ func (b Builder[T]) Build() (*AppManager[T], error) {
db: b.DB,
exportState: nil,
importState: nil,
initGenesis: b.InitGenesis,
stf: b.STF,
}, nil
}
56 changes: 41 additions & 15 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"fmt"
"sync/atomic"

"cosmossdk.io/core/header"
sdktypes "github.com/cosmos/cosmos-sdk/types"

"cosmossdk.io/core/comet"
consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types"

consensusv1 "cosmossdk.io/api/cosmos/consensus/v1"
coreappmgr "cosmossdk.io/core/app"
corecontext "cosmossdk.io/core/context"
"cosmossdk.io/core/event"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
Expand Down Expand Up @@ -234,6 +236,7 @@ func (c *Consensus[T]) Query(ctx context.Context, req *abci.RequestQuery) (*abci
func (c *Consensus[T]) InitChain(ctx context.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) {
c.logger.Info("InitChain", "initialHeight", req.InitialHeight, "chainID", req.ChainId)

// store chainID to be used later on in execution
c.chainID = req.ChainId

// On a new chain, we consider the init chain block height as 0, even though
Expand All @@ -251,26 +254,41 @@ func (c *Consensus[T]) InitChain(ctx context.Context, req *abci.RequestInitChain
})
}

genesisHeaderInfo := header.Info{
Height: req.InitialHeight,
Hash: nil,
Time: req.Time,
ChainID: req.ChainId,
AppHash: nil,
br := &coreappmgr.BlockRequest[T]{
Height: uint64(req.InitialHeight),
Time: req.Time,
Hash: nil,
AppHash: nil,
ChainId: req.ChainId,
ConsensusMessages: consMessages,
}

genesisState, err := c.app.InitGenesis(ctx, genesisHeaderInfo, consMessages, req.AppStateBytes)
blockresponse, genesisState, err := c.app.InitGenesis(
ctx,
br,
req.AppStateBytes,
c.txCodec)
if err != nil {
return nil, fmt.Errorf("genesis state init failure: %w", err)
}

println(genesisState) // TODO: this needs to be committed to store as height 0.
validatorUpdates := intoABCIValidatorUpdates(blockresponse.ValidatorUpdates)

stateChanges, err := genesisState.GetStateChanges()
if err != nil {
return nil, err
}
stateRoot, err := c.store.Commit(&store.Changeset{
Changes: stateChanges,
})
if err != nil {
return nil, fmt.Errorf("unable to commit the changeset: %w", err)
}

// TODO: populate
return &abci.ResponseInitChain{
ConsensusParams: req.ConsensusParams,
Validators: req.Validators,
AppHash: []byte{},
Validators: validatorUpdates,
AppHash: stateRoot,
}, nil
}

Expand Down Expand Up @@ -355,15 +373,23 @@ func (c *Consensus[T]) FinalizeBlock(
}

// for passing consensus info as a consensus message
cometInfo := &consensusv1.ConsensusMsgCometInfoRequest{
Info: &consensusv1.CometInfo{
cometInfo := &consensustypes.ConsensusMsgCometInfoRequest{
Info: &consensustypes.CometInfo{
Evidence: ToSDKEvidence(req.Misbehavior),
ValidatorsHash: req.NextValidatorsHash,
ProposerAddress: req.ProposerAddress,
LastCommit: ToSDKCommitInfo(req.DecidedLastCommit),
},
}

// TODO remove this once we have a better way to pass consensus info
ctx = context.WithValue(ctx, corecontext.CometInfoKey, &comet.Info{
Evidence: sdktypes.ToSDKEvidence(req.Misbehavior),
ValidatorsHash: req.NextValidatorsHash,
ProposerAddress: req.ProposerAddress,
LastCommit: sdktypes.ToSDKCommitInfo(req.DecidedLastCommit),
})

// TODO(tip): can we expect some txs to not decode? if so, what we do in this case? this does not seem to be the case,
// considering that prepare and process always decode txs, assuming they're the ones providing txs we should never
// have a tx that fails decoding.
Expand Down
Loading
Loading