Skip to content

Commit

Permalink
chore: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
taramakage committed Jul 6, 2023
1 parent 8b17a05 commit 8b3476f
Show file tree
Hide file tree
Showing 77 changed files with 1,380 additions and 305 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ lint: golangci-lint
go mod verify

format:
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./lite/*/statik.go" -not -path "*.pb.go" -not -path "./api" | xargs gofmt -w -s
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./lite/*/statik.go" -not -path "*.pb.go" -not -path "./api" | xargs misspell -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./lite/*/statik.go" -not -path "*.pb.go" -not -path "./api" | xargs goimports -w -local github.com/bianjieai/iritamod
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./lite/*/statik.go" -not -path "*.pb.go" -not -path "*.pulsar.go" | xargs golines -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./lite/*/statik.go" -not -path "*.pb.go" -not -path "*.pulsar.go" | xargs misspell -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "./lite/*/statik.go" -not -path "*.pb.go" -not -path "*.pulsar.go" | xargs goimports -w -local github.com/bianjieai/iritamod

benchmark:
@go test -mod=readonly -bench=. ./...
16 changes: 10 additions & 6 deletions modules/genutil/client/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ const (
)

type printInfo struct {
Moniker string `json:"moniker" yaml:"moniker"`
ChainID string `json:"chain_id" yaml:"chain_id"`
NodeID string `json:"node_id" yaml:"node_id"`
GenTxsDir string `json:"gentxs_dir" yaml:"gentxs_dir"`
Moniker string `json:"moniker" yaml:"moniker"`
ChainID string `json:"chain_id" yaml:"chain_id"`
NodeID string `json:"node_id" yaml:"node_id"`
GenTxsDir string `json:"gentxs_dir" yaml:"gentxs_dir"`
AppMessage json.RawMessage `json:"app_message" yaml:"app_message"`
}

func newPrintInfo(moniker, chainID, nodeID, genTxsDir string, appMessage json.RawMessage) printInfo {
func newPrintInfo(
moniker, chainID, nodeID, genTxsDir string,
appMessage json.RawMessage,
) printInfo {
return printInfo{
Moniker: moniker,
ChainID: chainID,
Expand Down Expand Up @@ -124,7 +127,8 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command { /

cmd.Flags().String(cli.HomeFlag, defaultNodeHome, "node's home directory")
cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file")
cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")
cmd.Flags().
String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")

return cmd
}
20 changes: 16 additions & 4 deletions modules/genutil/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ func GenAppStateFromConfig(
return appState, err
}

appGenesisState, err = AddGenTxsInAppGenesisState(cdc, txEncodingConfig.TxJSONEncoder(), appGenesisState, appGenTxs)
appGenesisState, err = AddGenTxsInAppGenesisState(
cdc,
txEncodingConfig.TxJSONEncoder(),
appGenesisState,
appGenTxs,
)
if err != nil {
return appState, err
}
Expand Down Expand Up @@ -121,19 +126,26 @@ func CollectTxs(
}
nodeAddrIP := memoTx.GetMemo()
if len(nodeAddrIP) == 0 {
return appGenTxs, persistentPeers, fmt.Errorf("failed to find node's address and IP in %s", fo.Name())
return appGenTxs, persistentPeers, fmt.Errorf(
"failed to find node's address and IP in %s",
fo.Name(),
)
}

// genesis transactions must be single-message
msgs := genTx.GetMsgs()
if len(msgs) != 1 {
return appGenTxs, persistentPeers, errors.New("each genesis transaction must provide a single genesis message")
return appGenTxs, persistentPeers, errors.New(
"each genesis transaction must provide a single genesis message",
)
}

// TODO abstract out validator message validation back to validator
msg, ok := msgs[0].(*nodetypes.MsgCreateValidator)
if !ok {
return appGenTxs, persistentPeers, errors.New("each genesis transaction must provide a validator creating message")
return appGenTxs, persistentPeers, errors.New(
"each genesis transaction must provide a validator creating message",
)
}
// exclude itself from persistent peers
if msg.Name != moniker {
Expand Down
8 changes: 7 additions & 1 deletion modules/genutil/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ func InitGenesis(

var validators []abci.ValidatorUpdate
if len(genesisState.GenTxs) > 0 {
validators = DeliverGenTxs(ctx, genesisState.GenTxs, nodeKeeper, deliverTx, txEncodingConfig)
validators = DeliverGenTxs(
ctx,
genesisState.GenTxs,
nodeKeeper,
deliverTx,
txEncodingConfig,
)
}

return validators
Expand Down
18 changes: 15 additions & 3 deletions modules/genutil/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
}

// ValidateGenesis performs genesis state validation for the genutil module.
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
func (AppModuleBasic) ValidateGenesis(
cdc codec.JSONCodec,
config client.TxEncodingConfig,
bz json.RawMessage,
) error {
var data GenesisState
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err)
Expand All @@ -53,7 +57,11 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod
}

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the genutil module.
func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {}
func (a AppModuleBasic) RegisterGRPCGatewayRoutes(
clientCtx client.Context,
mux *runtime.ServeMux,
) {
}

// GetTxCmd returns no root tx command for the genutil module.
func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil }
Expand Down Expand Up @@ -96,7 +104,11 @@ func NewAppModule(

// InitGenesis performs genesis initialization for the genutil module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
func (am AppModule) InitGenesis(
ctx sdk.Context,
cdc codec.JSONCodec,
data json.RawMessage,
) []abci.ValidatorUpdate {
var genesisState GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
return InitGenesis(ctx, am.nodeKeeper, am.deliverTx, genesisState, am.txEncodingConfig)
Expand Down
13 changes: 10 additions & 3 deletions modules/genutil/types/genesis_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ func NewGenesisStateFromTx(txJSONEncoder sdk.TxEncoder, genTxs []sdk.Tx) *Genesi
}

// GetGenesisStateFromAppState gets the genutil genesis state from the expected app state
func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *GenesisState {
func GetGenesisStateFromAppState(
cdc codec.JSONCodec,
appState map[string]json.RawMessage,
) *GenesisState {
var genesisState GenesisState
if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
Expand All @@ -65,7 +68,9 @@ func SetGenesisStateInAppState(
// for the application.
//
// NOTE: The pubkey input is this machines pubkey.
func GenesisStateFromGenDoc(genDoc ctmtypes.GenesisDoc) (genesisState map[string]json.RawMessage, err error) {
func GenesisStateFromGenDoc(
genDoc ctmtypes.GenesisDoc,
) (genesisState map[string]json.RawMessage, err error) {
if err = json.Unmarshal(genDoc.AppState, &genesisState); err != nil {
return genesisState, err
}
Expand All @@ -76,7 +81,9 @@ func GenesisStateFromGenDoc(genDoc ctmtypes.GenesisDoc) (genesisState map[string
// for the application.
//
// NOTE: The pubkey input is this machines pubkey.
func GenesisStateFromGenFile(genFile string) (genesisState map[string]json.RawMessage, genDoc *ctmtypes.GenesisDoc, err error) {
func GenesisStateFromGenFile(
genFile string,
) (genesisState map[string]json.RawMessage, genDoc *ctmtypes.GenesisDoc, err error) {
if !tmos.FileExists(genFile) {
return genesisState, genDoc, fmt.Errorf("%s does not exist, run `init` first", genFile)
}
Expand Down
12 changes: 10 additions & 2 deletions modules/genutil/types/genesis_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ func TestValidateGenesisMultipleMessages(t *testing.T) {
tx := txBuilder.GetTx()
genesisState := types.NewGenesisStateFromTx(txGen.TxJSONEncoder(), []sdk.Tx{tx})

err = types.ValidateGenesis(genesisState, simappparams.MakeTestEncodingConfig().TxConfig.TxJSONDecoder(), types.DefaultMessageValidator)
err = types.ValidateGenesis(
genesisState,
simappparams.MakeTestEncodingConfig().TxConfig.TxJSONDecoder(),
types.DefaultMessageValidator,
)
require.Error(t, err)
}

Expand All @@ -101,6 +105,10 @@ func TestValidateGenesisBadMessage(t *testing.T) {
tx := txBuilder.GetTx()
genesisState := types.NewGenesisStateFromTx(txGen.TxJSONEncoder(), []sdk.Tx{tx})

err = types.ValidateGenesis(genesisState, simappparams.MakeTestEncodingConfig().TxConfig.TxJSONDecoder(), types.DefaultMessageValidator)
err = types.ValidateGenesis(
genesisState,
simappparams.MakeTestEncodingConfig().TxConfig.TxJSONDecoder(),
types.DefaultMessageValidator,
)
require.Error(t, err)
}
4 changes: 3 additions & 1 deletion modules/genutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func ExportGenesisFileWithTime(
}

// InitializeNodeValidatorFiles creates private validator and p2p configuration files.
func InitializeNodeValidatorFiles(config *cfg.Config) (nodeKey *p2p.NodeKey, pv *privval.FilePV, err error) {
func InitializeNodeValidatorFiles(
config *cfg.Config,
) (nodeKey *p2p.NodeKey, pv *privval.FilePV, err error) {
nodeKey, err = p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
return nil, nil, err
Expand Down
30 changes: 25 additions & 5 deletions modules/identity/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,36 @@ var (
)

func init() {
FsCreateIdentity.BytesHex(FlagID, nil, "ID of the identity with 32 hex characters, automatically generated if not provided")
FsCreateIdentity.BytesHex(
FlagID,
nil,
"ID of the identity with 32 hex characters, automatically generated if not provided",
)
FsCreateIdentity.BytesHex(FlagPubKey, nil, "hex encoded public key")
FsCreateIdentity.String(FlagPubKeyAlgo, "", "algorithm of the public key (rsa|dsa|ecdsa|ed25519|sm2)")
FsCreateIdentity.String(
FlagPubKeyAlgo,
"",
"algorithm of the public key (rsa|dsa|ecdsa|ed25519|sm2)",
)
FsCreateIdentity.String(FlagCertificateFile, "", "X.509 certificate file path")
FsCreateIdentity.String(FlagCredentials, "", "uri pointing to credentials of the identity")
FsCreateIdentity.String(FlagData, "", "custom data of the identity")

FsUpdateIdentity.BytesHex(FlagPubKey, nil, "hex encoded public key to be added")
FsUpdateIdentity.String(FlagPubKeyAlgo, "", "algorithm of the public key (rsa|dsa|ecdsa|ed25519|sm2)")
FsUpdateIdentity.String(FlagCertificateFile, "", "file path of the X.509 certificate to be added")
FsUpdateIdentity.String(FlagCredentials, types.DoNotModifyDesc, "uri pointing to credentials of the identity")
FsUpdateIdentity.String(
FlagPubKeyAlgo,
"",
"algorithm of the public key (rsa|dsa|ecdsa|ed25519|sm2)",
)
FsUpdateIdentity.String(
FlagCertificateFile,
"",
"file path of the X.509 certificate to be added",
)
FsUpdateIdentity.String(
FlagCredentials,
types.DoNotModifyDesc,
"uri pointing to credentials of the identity",
)
FsUpdateIdentity.String(FlagData, types.DoNotModifyDesc, "custom data of the identity")
}
5 changes: 4 additions & 1 deletion modules/identity/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func GetCmdQueryIdentity() *cobra.Command {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Identity(context.Background(), &types.QueryIdentityRequest{Id: args[0]})
res, err := queryClient.Identity(
context.Background(),
&types.QueryIdentityRequest{Id: args[0]},
)
if err != nil {
return err
}
Expand Down
26 changes: 22 additions & 4 deletions modules/identity/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func NewCreateIdentityCmd() *cobra.Command {
if len(pubKeyStr) > 0 {
pubKeyInfo = new(types.PubKeyInfo)
pubKeyInfo.PubKey = pubKeyStr
pubKeyInfo.Algorithm = types.PubKeyAlgorithmFromString(viper.GetString(FlagPubKeyAlgo))
pubKeyInfo.Algorithm = types.PubKeyAlgorithmFromString(
viper.GetString(FlagPubKeyAlgo),
)
}

certFile := viper.GetString(FlagCertificateFile)
Expand All @@ -95,7 +97,14 @@ func NewCreateIdentityCmd() *cobra.Command {
credentials := viper.GetString(FlagCredentials)

data := viper.GetString(FlagData)
msg := types.NewMsgCreateIdentity(id, pubKeyInfo, string(cert), credentials, owner, data)
msg := types.NewMsgCreateIdentity(
id,
pubKeyInfo,
string(cert),
credentials,
owner,
data,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
Expand Down Expand Up @@ -147,7 +156,9 @@ func NewUpdateIdentityCmd() *cobra.Command {
if len(pubKeyStr) > 0 {
pubKeyInfo = new(types.PubKeyInfo)
pubKeyInfo.PubKey = pubKeyStr
pubKeyInfo.Algorithm = types.PubKeyAlgorithmFromString(viper.GetString(FlagPubKeyAlgo))
pubKeyInfo.Algorithm = types.PubKeyAlgorithmFromString(
viper.GetString(FlagPubKeyAlgo),
)
}

certFile := viper.GetString(FlagCertificateFile)
Expand All @@ -162,7 +173,14 @@ func NewUpdateIdentityCmd() *cobra.Command {

credentials := viper.GetString(FlagCredentials)
data := viper.GetString(FlagData)
msg := types.NewMsgUpdateIdentity(id, pubKeyInfo, string(cert), credentials, owner, data)
msg := types.NewMsgUpdateIdentity(
id,
pubKeyInfo,
string(cert),
credentials,
owner,
data,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion modules/identity/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import (
var _ types.QueryServer = Keeper{}

// Identity queries an identity by id
func (k Keeper) Identity(c context.Context, req *types.QueryIdentityRequest) (*types.QueryIdentityResponse, error) {
func (k Keeper) Identity(
c context.Context,
req *types.QueryIdentityRequest,
) (*types.QueryIdentityResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
Expand Down
27 changes: 22 additions & 5 deletions modules/identity/keeper/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ func (k Keeper) UpdateIdentity(
}

// AddPubKey adds the given public key for the identity
func (k Keeper) AddPubKey(ctx sdk.Context, identityID ctmbytes.HexBytes, pubKey *types.PubKeyInfo) error {
func (k Keeper) AddPubKey(
ctx sdk.Context,
identityID ctmbytes.HexBytes,
pubKey *types.PubKeyInfo,
) error {
pubKeyIdentityID, found := k.GetPubKeyIdentity(ctx, pubKey)
if found {
if !bytes.Equal(pubKeyIdentityID, identityID) {
Expand All @@ -111,7 +115,11 @@ func (k Keeper) AddPubKey(ctx sdk.Context, identityID ctmbytes.HexBytes, pubKey
}

// AddCertificate adds the given certificate for the identity
func (k Keeper) AddCertificate(ctx sdk.Context, identityID ctmbytes.HexBytes, certificate string) error {
func (k Keeper) AddCertificate(
ctx sdk.Context,
identityID ctmbytes.HexBytes,
certificate string,
) error {
cert := strings.TrimSpace(certificate)
certHash := tmhash.Sum([]byte(cert))

Expand Down Expand Up @@ -152,7 +160,10 @@ func (k Keeper) SetPubKey(ctx sdk.Context, identityID ctmbytes.HexBytes, pubKey
}

// GetPubKeyIdentity gets the identity ID of the specified public key
func (k Keeper) GetPubKeyIdentity(ctx sdk.Context, pubKey *types.PubKeyInfo) (ctmbytes.HexBytes, bool) {
func (k Keeper) GetPubKeyIdentity(
ctx sdk.Context,
pubKey *types.PubKeyInfo,
) (ctmbytes.HexBytes, bool) {
store := ctx.KVStore(k.storeKey)

bz := store.Get(types.GetPubKeyIdentityKey(pubKey))
Expand Down Expand Up @@ -260,7 +271,10 @@ func (k Keeper) SetIdentity(ctx sdk.Context, identity types.Identity) error {
}

// GetIdentity retrieves the identity of the specified ID
func (k Keeper) GetIdentity(ctx sdk.Context, id ctmbytes.HexBytes) (identity types.Identity, found bool) {
func (k Keeper) GetIdentity(
ctx sdk.Context,
id ctmbytes.HexBytes,
) (identity types.Identity, found bool) {
owner, found := k.GetOwner(ctx, id)
if !found {
return identity, false
Expand Down Expand Up @@ -317,7 +331,10 @@ func (k Keeper) IteratePubKeys(
pubKeyAlgo := types.PubKeyAlgorithm(binary.BigEndian.Uint32(pubKeyInfoKey[0:4]))
pubKey := pubKeyInfoKey[4:]

pubKeyInfo := types.PubKeyInfo{PubKey: ctmbytes.HexBytes(pubKey).String(), Algorithm: pubKeyAlgo}
pubKeyInfo := types.PubKeyInfo{
PubKey: ctmbytes.HexBytes(pubKey).String(),
Algorithm: pubKeyAlgo,
}

if stop := op(pubKeyInfo); stop {
break
Expand Down
Loading

0 comments on commit 8b3476f

Please sign in to comment.