Skip to content

Commit c3a6f35

Browse files
mergify[bot]sontrinh16coderabbitai[bot]kocubinskijulienrbrt
authored
refactor(genutil): Use sdk types genesis validator (backport #21678) (#22183)
Co-authored-by: son trinh <[email protected]> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Matt Kocubinski <[email protected]> Co-authored-by: Julien Robert <[email protected]>
1 parent 1780d7d commit c3a6f35

File tree

21 files changed

+462
-69
lines changed

21 files changed

+462
-69
lines changed

crypto/codec/pubkey.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package codec
2+
3+
import (
4+
"cosmossdk.io/errors"
5+
6+
cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys"
7+
bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
8+
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
9+
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
10+
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
11+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
12+
)
13+
14+
func PubKeyToProto(pk cryptokeys.JSONPubkey) (cryptotypes.PubKey, error) {
15+
switch pk.KeyType {
16+
case ed25519.PubKeyName:
17+
return &ed25519.PubKey{
18+
Key: pk.Value,
19+
}, nil
20+
case secp256k1.PubKeyName:
21+
return &secp256k1.PubKey{
22+
Key: pk.Value,
23+
}, nil
24+
case bls12_381.PubKeyName:
25+
return &bls12_381.PubKey{
26+
Key: pk.Value,
27+
}, nil
28+
default:
29+
return nil, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to proto public key", pk)
30+
}
31+
}
32+
33+
func PubKeyFromProto(pk cryptotypes.PubKey) (cryptokeys.JSONPubkey, error) {
34+
switch pk := pk.(type) {
35+
case *ed25519.PubKey:
36+
return cryptokeys.JSONPubkey{
37+
KeyType: ed25519.PubKeyName,
38+
Value: pk.Bytes(),
39+
}, nil
40+
case *secp256k1.PubKey:
41+
return cryptokeys.JSONPubkey{
42+
KeyType: secp256k1.PubKeyName,
43+
Value: pk.Bytes(),
44+
}, nil
45+
case *bls12_381.PubKey:
46+
return cryptokeys.JSONPubkey{
47+
KeyType: bls12_381.PubKeyName,
48+
Value: pk.Bytes(),
49+
}, nil
50+
default:
51+
return cryptokeys.JSONPubkey{}, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from proto public key", pk)
52+
}
53+
}

crypto/keys/jsonkey.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package keys
2+
3+
import (
4+
"github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
5+
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
6+
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
7+
"github.com/cosmos/cosmos-sdk/crypto/types"
8+
)
9+
10+
// JSONPubKey defines a public key that are parse from JSON file.
11+
// convert PubKey to JSONPubKey needs a in between step
12+
type JSONPubkey struct {
13+
KeyType string `json:"type"`
14+
Value []byte `json:"value"`
15+
}
16+
17+
func (pk JSONPubkey) Address() types.Address {
18+
switch pk.KeyType {
19+
case ed25519.PubKeyName:
20+
ed25519 := ed25519.PubKey{
21+
Key: pk.Value,
22+
}
23+
return ed25519.Address()
24+
case secp256k1.PubKeyName:
25+
secp256k1 := secp256k1.PubKey{
26+
Key: pk.Value,
27+
}
28+
return secp256k1.Address()
29+
case bls12_381.PubKeyName:
30+
bls12_381 := bls12_381.PubKey{
31+
Key: pk.Value,
32+
}
33+
return bls12_381.Address()
34+
default:
35+
return nil
36+
}
37+
}
38+
39+
func (pk JSONPubkey) Bytes() []byte {
40+
return pk.Value
41+
}

server/types/app.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
88
cmtcrypto "github.com/cometbft/cometbft/crypto"
9-
cmttypes "github.com/cometbft/cometbft/types"
109
"github.com/cosmos/gogoproto/grpc"
1110

1211
"cosmossdk.io/core/server"
@@ -18,6 +17,7 @@ import (
1817
"github.com/cosmos/cosmos-sdk/client"
1918
"github.com/cosmos/cosmos-sdk/server/api"
2019
"github.com/cosmos/cosmos-sdk/server/config"
20+
sdk "github.com/cosmos/cosmos-sdk/types"
2121
)
2222

2323
type (
@@ -76,7 +76,7 @@ type (
7676
// AppState is the application state as JSON.
7777
AppState json.RawMessage
7878
// Validators is the exported validator set.
79-
Validators []cmttypes.GenesisValidator
79+
Validators []sdk.GenesisValidator
8080
// Height is the app's latest block height.
8181
Height int64
8282
// ConsensusParams are the exported consensus params for ABCI.

simapp/export.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import (
55
"fmt"
66

77
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
8-
cmttypes "github.com/cometbft/cometbft/types"
98

109
"cosmossdk.io/collections"
1110
storetypes "cosmossdk.io/store/types"
1211
slashingtypes "cosmossdk.io/x/slashing/types"
1312
"cosmossdk.io/x/staking"
1413
stakingtypes "cosmossdk.io/x/staking/types"
1514

16-
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
1715
servertypes "github.com/cosmos/cosmos-sdk/server/types"
1816
sdk "github.com/cosmos/cosmos-sdk/types"
1917
)
@@ -43,25 +41,10 @@ func (app *SimApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAd
4341
}
4442

4543
validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
46-
cmtValidators := []cmttypes.GenesisValidator{}
47-
for _, val := range validators {
48-
cmtPk, err := cryptocodec.ToCmtPubKeyInterface(val.PubKey)
49-
if err != nil {
50-
return servertypes.ExportedApp{}, err
51-
}
52-
cmtVal := cmttypes.GenesisValidator{
53-
Address: val.Address.Bytes(),
54-
PubKey: cmtPk,
55-
Power: val.Power,
56-
Name: val.Name,
57-
}
58-
59-
cmtValidators = append(cmtValidators, cmtVal)
60-
}
6144

6245
return servertypes.ExportedApp{
6346
AppState: appState,
64-
Validators: cmtValidators,
47+
Validators: validators,
6548
Height: height,
6649
ConsensusParams: app.BaseApp.GetConsensusParams(ctx),
6750
}, err

simapp/v2/app_di.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import (
1313
"cosmossdk.io/log"
1414
"cosmossdk.io/runtime/v2"
1515
serverstore "cosmossdk.io/server/v2/store"
16+
"cosmossdk.io/store/v2"
1617
"cosmossdk.io/store/v2/root"
1718
basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject"
1819
lockupdepinject "cosmossdk.io/x/accounts/defaults/lockup/depinject"
1920
multisigdepinject "cosmossdk.io/x/accounts/defaults/multisig/depinject"
21+
stakingkeeper "cosmossdk.io/x/staking/keeper"
2022
upgradekeeper "cosmossdk.io/x/upgrade/keeper"
2123

2224
"github.com/cosmos/cosmos-sdk/client"
@@ -38,10 +40,12 @@ type SimApp[T transaction.Tx] struct {
3840
appCodec codec.Codec
3941
txConfig client.TxConfig
4042
interfaceRegistry codectypes.InterfaceRegistry
43+
store store.RootStore
4144

4245
// required keepers during wiring
4346
// others keepers are all in the app
4447
UpgradeKeeper *upgradekeeper.Keeper
48+
StakingKeeper *stakingkeeper.Keeper
4549
}
4650

4751
func init() {
@@ -161,6 +165,7 @@ func NewSimApp[T transaction.Tx](
161165
&app.txConfig,
162166
&app.interfaceRegistry,
163167
&app.UpgradeKeeper,
168+
&app.StakingKeeper,
164169
); err != nil {
165170
panic(err)
166171
}
@@ -170,7 +175,8 @@ func NewSimApp[T transaction.Tx](
170175
if err != nil {
171176
panic(err)
172177
}
173-
_, err = storeBuilder.Build(logger, storeConfig)
178+
179+
app.store, err = storeBuilder.Build(logger, storeConfig)
174180
if err != nil {
175181
panic(err)
176182
}
@@ -213,7 +219,6 @@ func (app *SimApp[T]) TxConfig() client.TxConfig {
213219
return app.txConfig
214220
}
215221

216-
// GetStore gets the app store.
217-
func (app *SimApp[T]) GetStore() any {
218-
return app.App.GetStore()
222+
func (app *SimApp[T]) GetStore() store.RootStore {
223+
return app.store
219224
}

simapp/v2/app_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
sdkmath "cosmossdk.io/math"
2121
"cosmossdk.io/runtime/v2"
2222
serverv2 "cosmossdk.io/server/v2"
23-
comettypes "cosmossdk.io/server/v2/cometbft/types"
2423
"cosmossdk.io/store/v2/db"
2524
banktypes "cosmossdk.io/x/bank/types"
2625

@@ -74,7 +73,7 @@ func NewTestApp(t *testing.T) (*SimApp[transaction.Tx], context.Context) {
7473
genesisBytes, err := json.Marshal(genesis)
7574
require.NoError(t, err)
7675

77-
st := app.GetStore().(comettypes.Store)
76+
st := app.GetStore()
7877
ci, err := st.LastCommitID()
7978
require.NoError(t, err)
8079

@@ -110,7 +109,7 @@ func MoveNextBlock(t *testing.T, app *SimApp[transaction.Tx], ctx context.Contex
110109

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

113-
st := app.GetStore().(comettypes.Store)
112+
st := app.GetStore()
114113
ci, err := st.LastCommitID()
115114
require.NoError(t, err)
116115

simapp/v2/export.go

+27-8
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,46 @@ package simapp
33
import (
44
"context"
55

6+
"cosmossdk.io/runtime/v2/services"
7+
"cosmossdk.io/x/staking"
8+
69
v2 "github.com/cosmos/cosmos-sdk/x/genutil/v2"
710
)
811

912
// ExportAppStateAndValidators exports the state of the application for a genesis
1013
// file.
11-
func (app *SimApp[T]) ExportAppStateAndValidators(jailAllowedAddrs []string) (v2.ExportedApp, error) {
12-
// as if they could withdraw from the start of the next block
14+
// This is a demonstation of how to export a genesis file. Export may need extended at
15+
// the user discretion for cleaning the genesis state at the end provided with jailAllowedAddrs
16+
func (app *SimApp[T]) ExportAppStateAndValidators(
17+
jailAllowedAddrs []string,
18+
) (v2.ExportedApp, error) {
1319
ctx := context.Background()
20+
var exportedApp v2.ExportedApp
1421

1522
latestHeight, err := app.LoadLatestHeight()
1623
if err != nil {
17-
return v2.ExportedApp{}, err
24+
return exportedApp, err
1825
}
1926

2027
genesis, err := app.ExportGenesis(ctx, latestHeight)
2128
if err != nil {
22-
return v2.ExportedApp{}, err
29+
return exportedApp, err
30+
}
31+
32+
readerMap, err := app.GetStore().StateAt(latestHeight)
33+
if err != nil {
34+
return exportedApp, err
35+
}
36+
genesisCtx := services.NewGenesisContext(readerMap)
37+
err = genesisCtx.Read(ctx, func(ctx context.Context) error {
38+
exportedApp.Validators, err = staking.WriteValidators(ctx, app.StakingKeeper)
39+
return err
40+
})
41+
if err != nil {
42+
return exportedApp, err
2343
}
2444

25-
return v2.ExportedApp{
26-
AppState: genesis,
27-
Height: int64(latestHeight),
28-
}, nil
45+
exportedApp.AppState = genesis
46+
exportedApp.Height = int64(latestHeight)
47+
return exportedApp, nil
2948
}

simapp/v2/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
cosmossdk.io/depinject v1.0.0
1111
cosmossdk.io/log v1.4.1
1212
cosmossdk.io/math v1.3.0
13-
cosmossdk.io/runtime/v2 v2.0.0-20241008175849-325728a9fd6c // main
13+
cosmossdk.io/runtime/v2 v2.0.0-20241009051805-4aeb0539252b // main
1414
cosmossdk.io/server/v2 v2.0.0-20241008175849-325728a9fd6c // main
1515
cosmossdk.io/server/v2/cometbft v0.0.0-00010101000000-000000000000
1616
cosmossdk.io/store/v2 v2.0.0-20241008175849-325728a9fd6c // main

simapp/v2/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ cosmossdk.io/log v1.4.1 h1:wKdjfDRbDyZRuWa8M+9nuvpVYxrEOwbD/CA8hvhU8QM=
210210
cosmossdk.io/log v1.4.1/go.mod h1:k08v0Pyq+gCP6phvdI6RCGhLf/r425UT6Rk/m+o74rU=
211211
cosmossdk.io/math v1.3.0 h1:RC+jryuKeytIiictDslBP9i1fhkVm6ZDmZEoNP316zE=
212212
cosmossdk.io/math v1.3.0/go.mod h1:vnRTxewy+M7BtXBNFybkuhSH4WfedVAAnERHgVFhp3k=
213-
cosmossdk.io/runtime/v2 v2.0.0-20241008175849-325728a9fd6c h1:+msBeQ6/bmJOhJPV7fye1bYf86LTaRMJezwTc5LSBUs=
214-
cosmossdk.io/runtime/v2 v2.0.0-20241008175849-325728a9fd6c/go.mod h1:pmne/XLLzEbQ6JeM/nQoZVvK19ZtMvvEMq6qOMp9MHs=
213+
cosmossdk.io/runtime/v2 v2.0.0-20241009051805-4aeb0539252b h1:ncc1ce9iZgUeTNVKkTeJ4tiVEgGGpPRk7I28PhzFqqY=
214+
cosmossdk.io/runtime/v2 v2.0.0-20241009051805-4aeb0539252b/go.mod h1:pmne/XLLzEbQ6JeM/nQoZVvK19ZtMvvEMq6qOMp9MHs=
215215
cosmossdk.io/schema v0.3.1-0.20240930054013-7c6e0388a3f9 h1:DmOoS/1PeY6Ih0hAVlJ69kLMUrLV+TCbfICrZtB1vdU=
216216
cosmossdk.io/schema v0.3.1-0.20240930054013-7c6e0388a3f9/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
217217
cosmossdk.io/server/v2 v2.0.0-20241008175849-325728a9fd6c h1:SobvpV3A/LPlmQ+mM6YrJHt7rRP6Cp7aVOB2ulwEBEc=

tests/e2e/genutil/export_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/cosmos/cosmos-sdk/server/types"
3232
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
3333
gentestutil "github.com/cosmos/cosmos-sdk/testutil/x/genutil"
34+
sdk "github.com/cosmos/cosmos-sdk/types"
3435
"github.com/cosmos/cosmos-sdk/x/genutil"
3536
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
3637
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
@@ -184,7 +185,7 @@ func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, ge
184185
ChainID: "theChainId",
185186
AppState: stateBytes,
186187
Consensus: &genutiltypes.ConsensusGenesis{
187-
Validators: nil,
188+
Validators: []sdk.GenesisValidator{},
188189
},
189190
}
190191

types/staking.go

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package types
33
import (
44
"cosmossdk.io/math"
55

6+
cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys"
67
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
78
)
89

@@ -98,3 +99,11 @@ type ValidatorI interface {
9899
SharesFromTokens(amt math.Int) (math.LegacyDec, error) // shares worth of delegator's bond
99100
SharesFromTokensTruncated(amt math.Int) (math.LegacyDec, error) // truncated shares worth of delegator's bond
100101
}
102+
103+
// GenesisValidator is an initial validator.
104+
type GenesisValidator struct {
105+
Address ConsAddress `json:"address"`
106+
PubKey cryptokeys.JSONPubkey `json:"pub_key"`
107+
Power int64 `json:"power"`
108+
Name string `json:"name"`
109+
}

0 commit comments

Comments
 (0)