-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
refactor(genutil): Use sdk types genesis validator #21678
Changes from 41 commits
fddadd3
41c5520
219213b
794ecbc
252f50f
0033996
ed34222
17a42a3
6bb526b
f9aa48b
389b7b8
5775c9f
85a753e
5fe570c
5b31f47
dd2a634
fba3f54
05fe687
e4f1972
80a92ae
fb41419
d0f9f1c
7a7c42a
ddf3149
7c0ce57
641164f
bbb3b01
5d02a84
e9e27f6
cebc09d
a0d41c9
7075a3d
d1f59d2
528606f
b5abb09
29156ed
b1924fd
bb2d7f1
8937cb7
b4014f3
bae84ba
e1a6b0c
76ab968
33e0432
6a81119
496627d
f0c5509
e7600cd
2d40cef
7879361
ebbf3e2
9ddc128
687866d
9b397f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package codec | ||
|
||
import ( | ||
"cosmossdk.io/errors" | ||
|
||
cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys" | ||
bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" | ||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
func PubKeyToProto(pk cryptokeys.JSONPubkey) (cryptotypes.PubKey, error) { | ||
switch pk.KeyType { | ||
case ed25519.PubKeyName: | ||
return &ed25519.PubKey{ | ||
Key: pk.Value, | ||
}, nil | ||
case secp256k1.PubKeyName: | ||
return &secp256k1.PubKey{ | ||
Key: pk.Value, | ||
}, nil | ||
case bls12_381.PubKeyName: | ||
return &bls12_381.PubKey{ | ||
Key: pk.Value, | ||
}, nil | ||
default: | ||
return nil, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to proto public key", pk) | ||
} | ||
} | ||
|
||
func PubKeyFromProto(pk cryptotypes.PubKey) (cryptokeys.JSONPubkey, error) { | ||
switch pk := pk.(type) { | ||
case *ed25519.PubKey: | ||
return cryptokeys.JSONPubkey{ | ||
KeyType: ed25519.PubKeyName, | ||
Value: pk.Bytes(), | ||
}, nil | ||
case *secp256k1.PubKey: | ||
return cryptokeys.JSONPubkey{ | ||
KeyType: secp256k1.PubKeyName, | ||
Value: pk.Bytes(), | ||
}, nil | ||
case *bls12_381.PubKey: | ||
return cryptokeys.JSONPubkey{ | ||
KeyType: bls12_381.PubKeyName, | ||
Value: pk.Bytes(), | ||
}, nil | ||
default: | ||
return cryptokeys.JSONPubkey{}, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from proto public key", pk) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package keys | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" | ||
"github.com/cosmos/cosmos-sdk/crypto/types" | ||
) | ||
|
||
// JSONPubKey defines a public key that are parse from JSON file. | ||
// convert PubKey to JSONPubKey needs a in between step | ||
type JSONPubkey struct { | ||
KeyType string `json:"type"` | ||
Value []byte `json:"value"` | ||
} | ||
|
||
func (pk JSONPubkey) Address() types.Address { | ||
switch pk.KeyType { | ||
case ed25519.PubKeyName: | ||
ed25519 := ed25519.PubKey{ | ||
Key: pk.Value, | ||
} | ||
return ed25519.Address() | ||
case secp256k1.PubKeyName: | ||
secp256k1 := secp256k1.PubKey{ | ||
Key: pk.Value, | ||
} | ||
return secp256k1.Address() | ||
case bls12_381.PubKeyName: | ||
bls12_381 := bls12_381.PubKey{ | ||
Key: pk.Value, | ||
} | ||
return bls12_381.Address() | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func (pk JSONPubkey) Bytes() []byte { | ||
return pk.Value | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package runtime | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"slices" | ||
|
@@ -123,3 +124,11 @@ func (a *App[T]) GetAppManager() *appmanager.AppManager[T] { | |
func (a *App[T]) GetGPRCMethodsToMessageMap() map[string]func() gogoproto.Message { | ||
return a.GRPCMethodsToMessageMap | ||
} | ||
|
||
func (a *App[T]) Query(ctx context.Context, gasLimit, version uint64, req transaction.Msg) (transaction.Msg, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i couldn't find other way to get bonded validator directly because of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use the staking keeper directly and remove this, like in export v1. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we pass the correct context from the calling func? |
||
state, err := a.db.StateAt(version) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return a.stf.Query(ctx, state, gasLimit, req) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ import ( | |
|
||
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" | ||
cmtcrypto "github.com/cometbft/cometbft/crypto" | ||
cmttypes "github.com/cometbft/cometbft/types" | ||
"github.com/cosmos/gogoproto/grpc" | ||
|
||
"cosmossdk.io/core/server" | ||
|
@@ -18,6 +17,7 @@ import ( | |
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/server/api" | ||
"github.com/cosmos/cosmos-sdk/server/config" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
type ( | ||
|
@@ -76,7 +76,7 @@ type ( | |
// AppState is the application state as JSON. | ||
AppState json.RawMessage | ||
// Validators is the exported validator set. | ||
Validators []cmttypes.GenesisValidator | ||
Validators []sdk.GenesisValidator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification Update remaining The verification process has identified several instances where
Action items:
Analysis chainVerify compatibility with existing code. The change from However, it's important to ensure that existing code relying on the Run the following script to identify instances of If the script returns instances of Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Search for usage of `cmttypes.GenesisValidator` in Go files.
# Test: Search for the type usage. Expect: Only occurrences in test files or comments.
rg --type go $'cmttypes\.GenesisValidator'
Length of output: 287 |
||
// Height is the app's latest block height. | ||
Height int64 | ||
// ConsensusParams are the exported consensus params for ABCI. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,12 @@ package simapp | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
stakingtypes "cosmossdk.io/x/staking/types" | ||
|
||
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
v2 "github.com/cosmos/cosmos-sdk/x/genutil/v2" | ||
) | ||
|
||
|
@@ -22,8 +27,39 @@ func (app *SimApp[T]) ExportAppStateAndValidators(jailAllowedAddrs []string) (v2 | |
return v2.ExportedApp{}, err | ||
} | ||
|
||
// get the current bonded validators | ||
resp, err := app.Query(ctx, 0, latestHeight, &stakingtypes.QueryValidatorsRequest{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not using the keeper directly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmmm i tried but got |
||
Status: stakingtypes.BondStatusBonded, | ||
}) | ||
|
||
vals, ok := resp.(*stakingtypes.QueryValidatorsResponse) | ||
if !ok { | ||
return v2.ExportedApp{}, fmt.Errorf("invalid response, expected QueryValidatorsResponse") | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the input argument to this func |
||
// convert to genesis validator | ||
var genesisVals []sdk.GenesisValidator | ||
for _, val := range vals.Validators { | ||
pk, err := val.ConsPubKey() | ||
if err != nil { | ||
return v2.ExportedApp{}, err | ||
} | ||
jsonPk, err := cryptocodec.PubKeyFromProto(pk) | ||
if err != nil { | ||
return v2.ExportedApp{}, err | ||
} | ||
|
||
genesisVals = append(genesisVals, sdk.GenesisValidator{ | ||
Address: sdk.ConsAddress(pk.Address()).Bytes(), | ||
PubKey: jsonPk, | ||
Power: val.GetConsensusPower(app.StakingKeeper.PowerReduction(ctx)), | ||
Name: val.Description.Moniker, | ||
}) | ||
} | ||
|
||
return v2.ExportedApp{ | ||
AppState: genesis, | ||
Height: int64(latestHeight), | ||
}, nil | ||
AppState: genesis, | ||
Height: int64(latestHeight), | ||
Validators: genesisVals, | ||
}, err | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider adding error handling for unknown key types.
The
Address()
method implementation looks good overall and follows the Uber Golang style guide. However, it might be beneficial to add error handling for unknown key types.Consider updating the
Address()
method to return an error for unknown key types:This change would allow callers to handle unknown key types more gracefully.
📝 Committable suggestion