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!: Define Genesis state transition #15999

Merged
merged 8 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions baseapp/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package baseapp

import (
"github.com/cometbft/cometbft/abci/types"
)

// GenesisState allows modules to define a set of state transitions
// that will initialize the chain's state at genesis.
type GenesisState interface {
// SetState sets the genesis state.
// This should be called in a order define by the application developer
SetState([]byte) types.ResponseDeliverTx
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
}

var _ GenesisState = (*BaseApp)(nil)

// SetState implements genesis.GenesisState from
// cosmossdk.io/core/genesis to set initial state in genesis
func (ba BaseApp) SetState(tx []byte) types.ResponseDeliverTx {
return ba.DeliverTx(types.RequestDeliverTx{Tx: tx})
}
10 changes: 3 additions & 7 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoregistry"

abci "github.com/cometbft/cometbft/abci/types"

runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
"cosmossdk.io/core/appmodule"
Expand Down Expand Up @@ -62,7 +60,7 @@ func init() {
ProvideKVStoreKey,
ProvideTransientStoreKey,
ProvideMemoryStoreKey,
ProvideDeliverTx,
ProvideGenesisState,
ProvideKVStoreService,
ProvideMemoryStoreService,
ProvideTransientStoreService,
Expand Down Expand Up @@ -204,10 +202,8 @@ func ProvideMemoryStoreKey(key depinject.ModuleKey, app *AppBuilder) *storetypes
return storeKey
}

func ProvideDeliverTx(appBuilder *AppBuilder) func(abci.RequestDeliverTx) abci.ResponseDeliverTx {
return func(tx abci.RequestDeliverTx) abci.ResponseDeliverTx {
return appBuilder.app.BaseApp.DeliverTx(tx)
}
func ProvideGenesisState(appBuilder *AppBuilder) baseapp.GenesisState {
return appBuilder.app.BaseApp
}

func ProvideKVStoreService(config *runtimev1alpha1.Module, key depinject.ModuleKey, app *AppBuilder) store.KVStoreService {
Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func NewSimApp(
// must be passed by reference here.
app.ModuleManager = module.NewManager(
genutil.NewAppModule(
app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx,
app.AccountKeeper, app.StakingKeeper, app.BaseApp,
encodingConfig.TxConfig,
),
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
Expand Down
3 changes: 2 additions & 1 deletion x/genutil/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package genutil
import (
abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
Expand All @@ -11,7 +12,7 @@ import (
// InitGenesis - initialize accounts and deliver genesis transactions
func InitGenesis(
ctx sdk.Context, stakingKeeper types.StakingKeeper,
deliverTx deliverTxfn, genesisState types.GenesisState,
deliverTx baseapp.GenesisState, genesisState types.GenesisState,
txEncodingConfig client.TxEncodingConfig,
) (validators []abci.ValidatorUpdate, err error) {
if len(genesisState.GenTxs) > 0 {
Expand Down
7 changes: 3 additions & 4 deletions x/genutil/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -84,14 +85,12 @@ func ValidateAccountInGenesis(
return nil
}

type deliverTxfn func(abci.RequestDeliverTx) abci.ResponseDeliverTx

// DeliverGenTxs iterates over all genesis txs, decodes each into a Tx and
// invokes the provided deliverTxfn with the decoded Tx. It returns the result
// of the staking module's ApplyAndReturnValidatorSetUpdates.
func DeliverGenTxs(
ctx sdk.Context, genTxs []json.RawMessage,
stakingKeeper types.StakingKeeper, deliverTx deliverTxfn,
stakingKeeper types.StakingKeeper, deliverTx baseapp.GenesisState,
txEncodingConfig client.TxEncodingConfig,
) ([]abci.ValidatorUpdate, error) {
for _, genTx := range genTxs {
Expand All @@ -105,7 +104,7 @@ func DeliverGenTxs(
return nil, fmt.Errorf("failed to encode GenTx '%s': %s", genTx, err)
}

res := deliverTx(abci.RequestDeliverTx{Tx: bz})
res := deliverTx.SetState(bz)
if !res.IsOK() {
return nil, fmt.Errorf("failed to execute DeliverTx for '%s': %s", genTx, res.Log)
}
Expand Down
45 changes: 27 additions & 18 deletions x/genutil/gentx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand Down Expand Up @@ -246,7 +247,7 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() {
testCases := []struct {
msg string
malleate func()
deliverTxFn func(abci.RequestDeliverTx) abci.ResponseDeliverTx
deliverTxFn baseapp.GenesisState
expPass bool
}{
{
Expand All @@ -260,14 +261,7 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() {
suite.Require().NoError(err)
genTxs[0] = tx
},
func(_ abci.RequestDeliverTx) abci.ResponseDeliverTx {
return abci.ResponseDeliverTx{
Code: sdkerrors.ErrNoSignatures.ABCICode(),
GasWanted: int64(10000000),
GasUsed: int64(41913),
Log: "no signatures supplied",
}
},
GenesisState1{},
false,
},
{
Expand All @@ -293,15 +287,7 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() {
suite.Require().NoError(err)
genTxs[0] = genTx
},
func(tx abci.RequestDeliverTx) abci.ResponseDeliverTx {
return abci.ResponseDeliverTx{
Code: sdkerrors.ErrUnauthorized.ABCICode(),
GasWanted: int64(10000000),
GasUsed: int64(41353),
Log: "signature verification failed; please verify account number (4) and chain-id (): unauthorized",
Codespace: "sdk",
}
},
GenesisState2{},
true,
},
}
Expand Down Expand Up @@ -334,3 +320,26 @@ func (suite *GenTxTestSuite) TestDeliverGenTxs() {
func TestGenTxTestSuite(t *testing.T) {
suite.Run(t, new(GenTxTestSuite))
}

type GenesisState1 struct{}

func (GenesisState1) SetState(_ []byte) abci.ResponseDeliverTx {
return abci.ResponseDeliverTx{
Code: sdkerrors.ErrNoSignatures.ABCICode(),
GasWanted: int64(10000000),
GasUsed: int64(41913),
Log: "no signatures supplied",
}
}

type GenesisState2 struct{}

func (GenesisState2) SetState(tx []byte) abci.ResponseDeliverTx {
return abci.ResponseDeliverTx{
Code: sdkerrors.ErrUnauthorized.ABCICode(),
GasWanted: int64(10000000),
GasUsed: int64(41353),
Log: "signature verification failed; please verify account number (4) and chain-id (): unauthorized",
Codespace: "sdk",
}
}
7 changes: 4 additions & 3 deletions x/genutil/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"cosmossdk.io/depinject"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -80,13 +81,13 @@ type AppModule struct {

accountKeeper types.AccountKeeper
stakingKeeper types.StakingKeeper
deliverTx deliverTxfn
deliverTx baseapp.GenesisState
txEncodingConfig client.TxEncodingConfig
}

// NewAppModule creates a new AppModule object
func NewAppModule(accountKeeper types.AccountKeeper,
stakingKeeper types.StakingKeeper, deliverTx deliverTxfn,
stakingKeeper types.StakingKeeper, deliverTx baseapp.GenesisState,
txEncodingConfig client.TxEncodingConfig,
) module.GenesisOnlyAppModule {
return module.NewGenesisOnlyAppModule(AppModule{
Expand Down Expand Up @@ -138,7 +139,7 @@ type ModuleInputs struct {

AccountKeeper types.AccountKeeper
StakingKeeper types.StakingKeeper
DeliverTx func(abci.RequestDeliverTx) abci.ResponseDeliverTx
DeliverTx baseapp.GenesisState
Config client.TxConfig
}

Expand Down