Skip to content

Commit

Permalink
chore: add missing proto files and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Aug 22, 2024
1 parent f217136 commit 500b322
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 43 deletions.
20 changes: 20 additions & 0 deletions proto/sedachain/pubkey/v1/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
syntax = "proto3";
package sedachain.pubkey.v1;

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "sedachain/pubkey/v1/pubkey.proto";

option go_package = "github.com/sedaprotocol/seda-chain/x/pubkey/types";

// GenesisState defines pubkey module's genesis state.
message GenesisState {
repeated ValidatorPubKeys validator_pub_keys = 1 [(gogoproto.nullable) = false];
}

// ValidatorPubKeys defines a validator's list of registered public keys
// primarily used in the x/pubkey genesis state.
message ValidatorPubKeys {
string validator_addr = 1 [(cosmos_proto.scalar) = "cosmos.ValidatorAddressString"];
repeated IndexedPubKey indexed_pub_keys = 2 [(gogoproto.nullable) = false];
}
14 changes: 14 additions & 0 deletions proto/sedachain/pubkey/v1/pubkey.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";
package sedachain.pubkey.v1;

import "gogoproto/gogo.proto";
import "google/protobuf/any.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/sedaprotocol/seda-chain/x/pubkey/types";

// IndexPubKeyPair defines an index - public key pair.
message IndexedPubKey {
uint32 index = 1;
google.protobuf.Any pub_key = 2 [(cosmos_proto.accepts_interface) = "cosmos.crypto.PubKey"];
}
28 changes: 28 additions & 0 deletions proto/sedachain/pubkey/v1/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
syntax = "proto3";
package sedachain.pubkey.v1;

import "google/api/annotations.proto";
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "sedachain/pubkey/v1/genesis.proto";

option go_package = "github.com/sedaprotocol/seda-chain/x/pubkey/types";

// Query defines the gRPC querier service.
service Query {
// ValidatorKeys returns a given validator's registered keys.
rpc ValidatorKeys(QueryValidatorKeysRequest)
returns (QueryValidatorKeysResponse) {
option (google.api.http).get = "/seda-chain/pubkey/validator_keys/{validator_addr}";
}
}

// QueryValidatorKeysRequest is request type for the Query/ValidatorKeys RPC method.
message QueryValidatorKeysRequest {
string validator_addr = 1 [(cosmos_proto.scalar) = "cosmos.ValidatorAddressString"];
}

// QueryValidatorKeysResponse is response type for the Query/ValidatorKeys RPC method.
message QueryValidatorKeysResponse {
ValidatorPubKeys validator_pub_keys = 1 [(gogoproto.nullable) = false];
}
30 changes: 30 additions & 0 deletions proto/sedachain/pubkey/v1/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
syntax = "proto3";
package sedachain.pubkey.v1;

import "gogoproto/gogo.proto";
import "google/protobuf/any.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/msg/v1/msg.proto";
import "sedachain/pubkey/v1/pubkey.proto";

option go_package = "github.com/sedaprotocol/seda-chain/x/pubkey/types";

// Msg defines the pubkey Msg service.
service Msg {
option (cosmos.msg.v1.service) = true;

// AddKey defines a method for registering a new public key.
rpc AddKey(MsgAddKey)
returns (MsgAddKeyResponse);
}

// MsgAddKey defines a message for registering a new public key.
message MsgAddKey {
option (cosmos.msg.v1.signer) = "validator_addr";

string validator_addr = 1 [(cosmos_proto.scalar) = "cosmos.ValidatorAddressString"];
repeated IndexedPubKey indexed_pub_keys = 2 [(gogoproto.nullable) = false];
}

// MsgAddKeyResponse defines the Msg/MsgAddKey response type.
message MsgAddKeyResponse {}
71 changes: 36 additions & 35 deletions x/pubkey/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/spf13/cobra"

cfg "github.com/cometbft/cometbft/config"
cmtjson "github.com/cometbft/cometbft/libs/json"
cmtos "github.com/cometbft/cometbft/libs/os"

Expand Down Expand Up @@ -72,8 +71,8 @@ func AddKey(ac address.Codec) *cobra.Command {
}
} else {
pks, err = generateSEDAKeys(
serverCfg,
[]privKeyGenerator{secp256k1GenPrivKey},
filepath.Dir(serverCfg.PrivValidatorKeyFile()),
)
if err != nil {
return err
Expand All @@ -99,19 +98,6 @@ type IndexKey struct {
PrivKey crypto.PrivKey `json:"priv_key"`
}

// saveSEDAKeys saves a given list of IndexKeys at a given path.
func saveSEDAKeys(keys []IndexKey, savePath string) error {
jsonBytes, err := cmtjson.MarshalIndent(keys, "", " ") // TODO use simple json.Marshal?
if err != nil {
return fmt.Errorf("failed to marshal SEDA keys: %v", err)
}
err = os.WriteFile(savePath, jsonBytes, 0o600)
if err != nil {
return fmt.Errorf("failed to write SEDA key file: %v", err)
}
return nil
}

// loadSEDAPubKeys loads the SEDA key file from the given path and
// returns a list of index-public key pairs.
func loadSEDAPubKeys(loadPath string) ([]types.IndexedPubKey, error) {
Expand All @@ -125,20 +111,41 @@ func loadSEDAPubKeys(loadPath string) ([]types.IndexedPubKey, error) {
return nil, fmt.Errorf("failed to unmarshal SEDA keys from %v: %v", loadPath, err)
}

var result []types.IndexedPubKey
for _, key := range keys {
result := make([]types.IndexedPubKey, len(keys))
for i, key := range keys {
pkAny, err := codectypes.NewAnyWithValue(key.PubKey)
if err != nil {
return nil, err
}
result = append(result, types.IndexedPubKey{
result[i] = types.IndexedPubKey{
Index: key.Index,
PubKey: pkAny,
})
}
}
return result, nil
}

// saveSEDAKeys saves a given list of IndexKeys in the directory at dirPath.
func saveSEDAKeys(keys []IndexKey, dirPath string) error {
savePath := filepath.Join(dirPath, SEDAKeyFileName)
if cmtos.FileExists(savePath) {
return fmt.Errorf("SEDA key file already exists at %s", savePath)
}
err := cmtos.EnsureDir(filepath.Dir(savePath), 0o700)
if err != nil {
return err
}
jsonBytes, err := cmtjson.MarshalIndent(keys, "", " ") // TODO use simple json.Marshal?
if err != nil {
return fmt.Errorf("failed to marshal SEDA keys: %v", err)
}
err = os.WriteFile(savePath, jsonBytes, 0o600)
if err != nil {
return fmt.Errorf("failed to write SEDA key file: %v", err)
}
return nil
}

type privKeyGenerator func() crypto.PrivKey

func secp256k1GenPrivKey() crypto.PrivKey {
Expand All @@ -148,39 +155,33 @@ func secp256k1GenPrivKey() crypto.PrivKey {
// generateSEDAKeys generates SEDA keys given a list of private key
// generators, saves them to the SEDA key file, and returns the resulting
// index-public key pairs. Index is assigned incrementally in the order
// of the given private key generators.
func generateSEDAKeys(config *cfg.Config, generators []privKeyGenerator) ([]types.IndexedPubKey, error) {
var keys []IndexKey
var result []types.IndexedPubKey
// of the given private key generators. The key file is stored in the
// directory given by dirPath.
func generateSEDAKeys(generators []privKeyGenerator, dirPath string) ([]types.IndexedPubKey, error) {
keys := make([]IndexKey, len(generators))
result := make([]types.IndexedPubKey, len(generators))
for i, generator := range generators {
privKey := generator()
keys = append(keys, IndexKey{
keys[i] = IndexKey{
Index: uint32(i),
PrivKey: privKey,
PubKey: privKey.PubKey(),
})
}

pkAny, err := codectypes.NewAnyWithValue(privKey.PubKey())
if err != nil {
return nil, err
}
result = append(result, types.IndexedPubKey{
result[i] = types.IndexedPubKey{
Index: uint32(i),
PubKey: pkAny,
})
}
}

// The key file is placed in the same directory as the validator key file.
pvKeyFile := config.PrivValidatorKeyFile()
savePath := filepath.Join(filepath.Dir(pvKeyFile), SEDAKeyFileName)
if cmtos.FileExists(savePath) {
return nil, fmt.Errorf("SEDA key file already exists at %s", savePath)
}
err := cmtos.EnsureDir(filepath.Dir(pvKeyFile), 0o700)
err := saveSEDAKeys(keys, dirPath)
if err != nil {
return nil, err
}
saveSEDAKeys(keys, savePath)

return result, nil
}
12 changes: 6 additions & 6 deletions x/pubkey/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import (
// generatePubKeysAndValAddrs randomly generates a given number of
// public keys encoded in codectypes.Any type and their validator
// addresses.
func (s *KeeperTestSuite) generatePubKeysAndValAddrs(num uint) ([]*codectypes.Any, []sdk.ValAddress) {
var pubKeys []*codectypes.Any
func (s *KeeperTestSuite) generatePubKeysAndValAddrs(num int) ([]*codectypes.Any, []sdk.ValAddress) {
var pkAnys []*codectypes.Any
var valAddrs []sdk.ValAddress
for i := 0; i < 10; i++ {
for i := 0; i < num; i++ {
privKey := secp256k1.GenPrivKey()
pubKey, err := cryptocodec.FromCmtPubKeyInterface(privKey.PubKey())
s.Require().NoError(err)

any, err := codectypes.NewAnyWithValue(pubKey)
pkAny, err := codectypes.NewAnyWithValue(pubKey)
s.Require().NoError(err)
pubKeys = append(pubKeys, any)
pkAnys = append(pkAnys, pkAny)

valAddrs = append(valAddrs, sdk.ValAddress(privKey.PubKey().Address()))
}
return pubKeys, valAddrs
return pkAnys, valAddrs
}

func (s *KeeperTestSuite) TestImportExportGenesis() {
Expand Down
4 changes: 2 additions & 2 deletions x/pubkey/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func (s *KeeperTestSuite) TestMsgServer_AddKey() {
{
Index: 0,
PubKey: func() *codectypes.Any {
any, err := codectypes.NewAnyWithValue(&stakingtypes.Commission{})
wrongAny, err := codectypes.NewAnyWithValue(&stakingtypes.Commission{})
s.Require().NoError(err)
return any
return wrongAny
}(),
},
},
Expand Down

0 comments on commit 500b322

Please sign in to comment.