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 8 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
7 changes: 7 additions & 0 deletions runtime/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ require (
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/errors v1.0.1 // indirect
cosmossdk.io/math v1.3.0 // indirect
cosmossdk.io/x/accounts v0.0.0-00010101000000-000000000000 // 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 Expand Up @@ -105,9 +108,11 @@ require (
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/orderedcode v0.0.1 // indirect
github.com/gorilla/handlers v1.5.2 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
Expand All @@ -130,12 +135,14 @@ require (
github.com/klauspost/compress v1.17.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.8.14 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions runtime/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/zondax/hid v0.9.2 h1:WCJFnEDMiqGF64nlZz28E9qLVZ0KSJ7xpc5DLEyma2U=
github.com/zondax/hid v0.9.2/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM=
github.com/zondax/ledger-go v0.14.3 h1:wEpJt2CEcBJ428md/5MgSLsXLBos98sBOyxNmCjfUCw=
Expand Down Expand Up @@ -747,6 +748,7 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -788,6 +790,7 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -799,6 +802,7 @@ golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -879,6 +883,7 @@ golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapK
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
68 changes: 65 additions & 3 deletions runtime/v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"sort"

"cosmossdk.io/core/genesis"

"github.com/golang/protobuf/proto"
"golang.org/x/exp/maps"
"google.golang.org/grpc"
Expand All @@ -26,6 +28,7 @@ import (

"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,9 +143,68 @@ func (m *MM) ValidateGenesis(genesisData map[string]json.RawMessage) error {
return nil
}

// InitGenesis performs init genesis functionality for modules.
func (m *MM) InitGenesis() {
panic("implement me")
// 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 / Golang security checks by gosec

Errors unhandled. Warning

Errors unhandled.
}
continue
}

// we might get an adapted module, a native core API module or a legacy module
if module, ok := mod.(appmodule.HasGenesisAuto); ok {
Copy link
Member

Choose a reason for hiding this comment

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

we shouldn't support appmodule.HasGenesisAuto here, imho only appmodulev2 logic.

m.logger.Debug("running initialization for module", "module", moduleName)
// core API genesis
source, err := genesis.SourceFromRawJSON(genesisData[moduleName])
if err != nil {
return err
}

err = module.InitGenesis(ctx, source)
if err != nil {
return err
}
} 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
}

// ExportGenesis performs export genesis functionality for modules
Expand Down
15 changes: 9 additions & 6 deletions runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func init() {
ProvideGenesisTxHandler,
ProvideAppVersionModifier,
),
appconfig.Invoke(SetupAppBuilder),
)
}

Expand Down Expand Up @@ -166,7 +167,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 All @@ -178,11 +179,13 @@ func SetupAppBuilder(inputs AppInputs) {
app.moduleManager.RegisterInterfaces(inputs.InterfaceRegistry)
app.moduleManager.RegisterLegacyAminoCodec(inputs.LegacyAmino)

// TODO: this is a bit of a hack, but it's the only way to get the store keys into the app
// registerStoreKey could instead set this on StoreOptions directly
inputs.AppBuilder.storeOptions = inputs.StoreOptions
for _, sk := range inputs.AppBuilder.app.storeKeys {
inputs.AppBuilder.storeOptions.StoreKeys = append(inputs.AppBuilder.storeOptions.StoreKeys, sk.String())
if inputs.StoreOptions != nil {
// TODO: this is a bit of a hack, but it's the only way to get the store keys into the app
// registerStoreKey could instead set this on StoreOptions directly
inputs.AppBuilder.storeOptions = inputs.StoreOptions
for _, sk := range inputs.AppBuilder.app.storeKeys {
inputs.AppBuilder.storeOptions.StoreKeys = append(inputs.AppBuilder.storeOptions.StoreKeys, sk.String())
}
}
}

Expand Down
40 changes: 32 additions & 8 deletions server/v2/appmanager/appmanager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package appmanager

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

Expand All @@ -22,6 +24,8 @@ 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]
}

Expand All @@ -30,30 +34,50 @@ func (a AppManager[T]) InitGenesis(
headerInfo header.Info,
consensusMessages []transaction.Type,
initGenesisJSON []byte,
txDecoder transaction.Codec[T],
) (corestore.WriterMap, error) {
v, zeroState, err := a.db.StateLatest()
if err != nil {
return nil, fmt.Errorf("unable to get latest state: %w", err)
}
if v != 0 {
return 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, 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),
Height: uint64(0),
Copy link
Contributor

Choose a reason for hiding this comment

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

just curious why we hardcode run at block 0 here?

Copy link
Member

@tac0turtle tac0turtle Apr 25, 2024

Choose a reason for hiding this comment

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

note from call: use height from request, check if storage has any value, if empty then assume genesis

Time: headerInfo.Time,
Hash: headerInfo.Hash,
ChainId: headerInfo.ChainID,
AppHash: headerInfo.AppHash,
Txs: nil,
Txs: genTxs,
ConsensusMessages: consensusMessages,
}

_, genesisState, err := a.DeliverBlock(ctx, block0)
_, genesisState, err := a.stf.DeliverBlock(ctx, block0, zeroState)
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
return 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 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
}
Loading
Loading