From 14d06c3023f05643a371bef2967570693b29aa7d Mon Sep 17 00:00:00 2001 From: lumtis Date: Thu, 30 May 2024 13:45:57 +0200 Subject: [PATCH] add unit test for parse type in cli --- .../zetacored/zetacored_query_authority.md | 1 + ...tacored_query_authority_show-chain-info.md | 34 +++++++++++++ docs/openapi/openapi.swagger.yaml | 23 +++++++++ docs/spec/authority/messages.md | 6 +-- pkg/testdata/testdata.go | 5 ++ pkg/testdata/types/chain_info.json | 24 +++++++++ pkg/testdata/types/policies.json | 16 ++++++ .../zetacore/authority/query_pb.d.ts | 50 +++++++++++++++++++ .../zetachain/zetacore/authority/tx_pb.d.ts | 8 +-- .../client/cli/tx_update_chain_info.go | 16 +++--- .../client/cli/tx_update_chain_info_test.go | 40 +++++++++++++++ x/authority/client/cli/tx_update_policies.go | 15 +++--- .../client/cli/tx_update_policies_test.go | 32 ++++++++++++ 13 files changed, 251 insertions(+), 19 deletions(-) create mode 100644 docs/cli/zetacored/zetacored_query_authority_show-chain-info.md create mode 100644 pkg/testdata/types/chain_info.json create mode 100644 pkg/testdata/types/policies.json create mode 100644 x/authority/client/cli/tx_update_chain_info_test.go create mode 100644 x/authority/client/cli/tx_update_policies_test.go diff --git a/docs/cli/zetacored/zetacored_query_authority.md b/docs/cli/zetacored/zetacored_query_authority.md index 1685db07ca..f1e6bd9b69 100644 --- a/docs/cli/zetacored/zetacored_query_authority.md +++ b/docs/cli/zetacored/zetacored_query_authority.md @@ -26,5 +26,6 @@ zetacored query authority [flags] ### SEE ALSO * [zetacored query](zetacored_query.md) - Querying subcommands +* [zetacored query authority show-chain-info](zetacored_query_authority_show-chain-info.md) - show the chain info * [zetacored query authority show-policies](zetacored_query_authority_show-policies.md) - show the policies diff --git a/docs/cli/zetacored/zetacored_query_authority_show-chain-info.md b/docs/cli/zetacored/zetacored_query_authority_show-chain-info.md new file mode 100644 index 0000000000..440cfc470f --- /dev/null +++ b/docs/cli/zetacored/zetacored_query_authority_show-chain-info.md @@ -0,0 +1,34 @@ +# query authority show-chain-info + +show the chain info + +``` +zetacored query authority show-chain-info [flags] +``` + +### Options + +``` + --grpc-addr string the gRPC endpoint to use for this chain + --grpc-insecure allow gRPC over insecure channels, if not TLS the server must use TLS + --height int Use a specific height to query state at (this can error if the node is pruning state) + -h, --help help for show-chain-info + --node string [host]:[port] to Tendermint RPC interface for this chain + -o, --output string Output format (text|json) +``` + +### Options inherited from parent commands + +``` + --chain-id string The network chain ID + --home string directory for config and data + --log_format string The logging format (json|plain) + --log_level string The logging level (trace|debug|info|warn|error|fatal|panic) + --log_no_color Disable colored logs + --trace print out full stack trace on errors +``` + +### SEE ALSO + +* [zetacored query authority](zetacored_query_authority.md) - Querying commands for the authority module + diff --git a/docs/openapi/openapi.swagger.yaml b/docs/openapi/openapi.swagger.yaml index 565c98b6bc..c9e758227f 100644 --- a/docs/openapi/openapi.swagger.yaml +++ b/docs/openapi/openapi.swagger.yaml @@ -28254,6 +28254,21 @@ paths: type: boolean tags: - Query + /zeta-chain/authority/chainInfo: + get: + summary: Queries ChainInfo + operationId: Query_ChainInfo + responses: + "200": + description: A successful response. + schema: + $ref: '#/definitions/authorityQueryGetChainInfoResponse' + default: + description: An unexpected error response. + schema: + $ref: '#/definitions/googlerpcStatus' + tags: + - Query /zeta-chain/authority/policies: get: summary: Queries Policies @@ -56751,6 +56766,14 @@ definitions: Used for administrative tasks like changing sensitive title: PolicyType defines the type of policy + authorityQueryGetChainInfoResponse: + type: object + properties: + chain_info: + $ref: '#/definitions/authorityChainInfo' + description: |- + QueryGetChainInfoResponse is the response type for the Query/ChainInfo RPC + method. authorityQueryGetPoliciesResponse: type: object properties: diff --git a/docs/spec/authority/messages.md b/docs/spec/authority/messages.md index 5159734edd..899a95b989 100644 --- a/docs/spec/authority/messages.md +++ b/docs/spec/authority/messages.md @@ -6,19 +6,19 @@ UpdatePolicies updates policies ```proto message MsgUpdatePolicies { - string signer = 1; + string creator = 1; Policies policies = 2; } ``` ## MsgUpdateChainInfo -UpdateChainInfo updates the chain inffo structure that adds new static chain info or overwrite existing chain info +UpdateChainInfo updates the chain info structure that adds new static chain info or overwrite existing chain info on the hard-coded chain info ```proto message MsgUpdateChainInfo { - string signer = 1; + string creator = 1; ChainInfo chain_info = 2; } ``` diff --git a/pkg/testdata/testdata.go b/pkg/testdata/testdata.go index 37c63c4131..50f08b1451 100644 --- a/pkg/testdata/testdata.go +++ b/pkg/testdata/testdata.go @@ -17,6 +17,11 @@ const ( TxsCount = 81 ) +// TypesFiles contains the embedded files of different types in ZetaChain +// +//go:embed types/* +var TypesFiles embed.FS + //go:embed ethereum/* var ethFiles embed.FS diff --git a/pkg/testdata/types/chain_info.json b/pkg/testdata/types/chain_info.json new file mode 100644 index 0000000000..e346dac20a --- /dev/null +++ b/pkg/testdata/types/chain_info.json @@ -0,0 +1,24 @@ +{ + "chains": [ + { + "chain_id": 42, + "chain_name": 0, + "network": 0, + "network_type": 0, + "vm": 0, + "consensus": 0, + "is_external": false, + "cctx_gateway": 0 + }, + { + "chain_id": 84, + "chain_name": 1, + "network": 1, + "network_type": 1, + "vm": 1, + "consensus": 1, + "is_external": true, + "cctx_gateway": 1 + } + ] +} \ No newline at end of file diff --git a/pkg/testdata/types/policies.json b/pkg/testdata/types/policies.json new file mode 100644 index 0000000000..bbfdc2783f --- /dev/null +++ b/pkg/testdata/types/policies.json @@ -0,0 +1,16 @@ +{ + "items": [ + { + "address": "zeta1nl7550unvzyswx5ts9m338ufmfydjsz2g0xt74", + "policy_type": 0 + }, + { + "address": "zeta1n0rn6sne54hv7w2uu93fl48ncyqz97d3kty6sh", + "policy_type": 1 + }, + { + "address": "zeta1srsq755t654agc0grpxj4y3w0znktrpr9tcdgk", + "policy_type": 2 + } + ] +} \ No newline at end of file diff --git a/typescript/zetachain/zetacore/authority/query_pb.d.ts b/typescript/zetachain/zetacore/authority/query_pb.d.ts index 103de61b97..13192f4592 100644 --- a/typescript/zetachain/zetacore/authority/query_pb.d.ts +++ b/typescript/zetachain/zetacore/authority/query_pb.d.ts @@ -6,6 +6,7 @@ import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf"; import { Message, proto3 } from "@bufbuild/protobuf"; import type { Policies } from "./policies_pb.js"; +import type { ChainInfo } from "./chain_info_pb.js"; /** * QueryGetPoliciesRequest is the request type for the Query/Policies RPC @@ -56,3 +57,52 @@ export declare class QueryGetPoliciesResponse extends Message | undefined, b: QueryGetPoliciesResponse | PlainMessage | undefined): boolean; } +/** + * QueryGetChainInfoRequest is the request type for the Query/ChainInfo RPC + * method. + * + * @generated from message zetachain.zetacore.authority.QueryGetChainInfoRequest + */ +export declare class QueryGetChainInfoRequest extends Message { + constructor(data?: PartialMessage); + + static readonly runtime: typeof proto3; + static readonly typeName = "zetachain.zetacore.authority.QueryGetChainInfoRequest"; + static readonly fields: FieldList; + + static fromBinary(bytes: Uint8Array, options?: Partial): QueryGetChainInfoRequest; + + static fromJson(jsonValue: JsonValue, options?: Partial): QueryGetChainInfoRequest; + + static fromJsonString(jsonString: string, options?: Partial): QueryGetChainInfoRequest; + + static equals(a: QueryGetChainInfoRequest | PlainMessage | undefined, b: QueryGetChainInfoRequest | PlainMessage | undefined): boolean; +} + +/** + * QueryGetChainInfoResponse is the response type for the Query/ChainInfo RPC + * method. + * + * @generated from message zetachain.zetacore.authority.QueryGetChainInfoResponse + */ +export declare class QueryGetChainInfoResponse extends Message { + /** + * @generated from field: zetachain.zetacore.authority.ChainInfo chain_info = 1; + */ + chainInfo?: ChainInfo; + + constructor(data?: PartialMessage); + + static readonly runtime: typeof proto3; + static readonly typeName = "zetachain.zetacore.authority.QueryGetChainInfoResponse"; + static readonly fields: FieldList; + + static fromBinary(bytes: Uint8Array, options?: Partial): QueryGetChainInfoResponse; + + static fromJson(jsonValue: JsonValue, options?: Partial): QueryGetChainInfoResponse; + + static fromJsonString(jsonString: string, options?: Partial): QueryGetChainInfoResponse; + + static equals(a: QueryGetChainInfoResponse | PlainMessage | undefined, b: QueryGetChainInfoResponse | PlainMessage | undefined): boolean; +} + diff --git a/typescript/zetachain/zetacore/authority/tx_pb.d.ts b/typescript/zetachain/zetacore/authority/tx_pb.d.ts index c0fc313de9..bd531ac2e5 100644 --- a/typescript/zetachain/zetacore/authority/tx_pb.d.ts +++ b/typescript/zetachain/zetacore/authority/tx_pb.d.ts @@ -15,9 +15,9 @@ import type { ChainInfo } from "./chain_info_pb.js"; */ export declare class MsgUpdatePolicies extends Message { /** - * @generated from field: string signer = 1; + * @generated from field: string creator = 1; */ - signer: string; + creator: string; /** * @generated from field: zetachain.zetacore.authority.Policies policies = 2; @@ -67,9 +67,9 @@ export declare class MsgUpdatePoliciesResponse extends Message { /** - * @generated from field: string signer = 1; + * @generated from field: string creator = 1; */ - signer: string; + creator: string; /** * @generated from field: zetachain.zetacore.authority.ChainInfo chain_info = 2; diff --git a/x/authority/client/cli/tx_update_chain_info.go b/x/authority/client/cli/tx_update_chain_info.go index 9272cc78ef..4a2f4b93eb 100644 --- a/x/authority/client/cli/tx_update_chain_info.go +++ b/x/authority/client/cli/tx_update_chain_info.go @@ -1,6 +1,9 @@ package cli import ( + "encoding/json" + "fmt" + "io/fs" "os" "github.com/cosmos/cosmos-sdk/client" @@ -17,7 +20,7 @@ func CmdUpdateChainInfo() *cobra.Command { Short: "Update the chain info", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) (err error) { - chainInfo, err := readChainInfoFromFile(args[0]) + chainInfo, err := ReadChainInfoFromFile(os.DirFS("."), args[0]) if err != nil { return err } @@ -39,13 +42,14 @@ func CmdUpdateChainInfo() *cobra.Command { return cmd } -// readChainInfoFromFile read the chain info from the file using os package and unmarshal it into the chain info variable -func readChainInfoFromFile(filePath string) (types.ChainInfo, error) { +// ReadChainInfoFromFile read the chain info from the file using os package and unmarshal it into the chain info variable +func ReadChainInfoFromFile(fsys fs.FS, filePath string) (types.ChainInfo, error) { var chainInfo types.ChainInfo - chainInfoBytes, err := os.ReadFile(filePath) + chainInfoBytes, err := fs.ReadFile(fsys, filePath) if err != nil { - return chainInfo, err + return chainInfo, fmt.Errorf("failed to read file: %w", err) } - err = chainInfo.Unmarshal(chainInfoBytes) + + err = json.Unmarshal(chainInfoBytes, &chainInfo) return chainInfo, err } diff --git a/x/authority/client/cli/tx_update_chain_info_test.go b/x/authority/client/cli/tx_update_chain_info_test.go new file mode 100644 index 0000000000..95b033777f --- /dev/null +++ b/x/authority/client/cli/tx_update_chain_info_test.go @@ -0,0 +1,40 @@ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/zeta-chain/zetacore/pkg/chains" + "github.com/zeta-chain/zetacore/pkg/testdata" + "github.com/zeta-chain/zetacore/x/authority/client/cli" +) + +func TestReadChainInfoFromFile(t *testing.T) { + fs := testdata.TypesFiles + + chainInfo, err := cli.ReadChainInfoFromFile(fs, "types/chain_info.json") + require.NoError(t, err) + + require.Len(t, chainInfo.Chains, 2) + require.EqualValues(t, chains.Chain{ + ChainId: 42, + ChainName: chains.ChainName_empty, + Network: chains.Network_eth, + NetworkType: chains.NetworkType_mainnet, + Vm: chains.Vm_no_vm, + Consensus: chains.Consensus_ethereum, + IsExternal: false, + CctxGateway: chains.CCTXGateway_zevm, + }, chainInfo.Chains[0]) + require.EqualValues(t, chains.Chain{ + ChainId: 84, + ChainName: chains.ChainName_eth_mainnet, + Network: chains.Network_zeta, + NetworkType: chains.NetworkType_testnet, + Vm: chains.Vm_evm, + Consensus: chains.Consensus_tendermint, + IsExternal: true, + CctxGateway: chains.CCTXGateway_observers, + }, chainInfo.Chains[1]) +} diff --git a/x/authority/client/cli/tx_update_policies.go b/x/authority/client/cli/tx_update_policies.go index 193a5c063b..19bfe6ab4f 100644 --- a/x/authority/client/cli/tx_update_policies.go +++ b/x/authority/client/cli/tx_update_policies.go @@ -1,6 +1,9 @@ package cli import ( + "encoding/json" + "fmt" + "io/fs" "os" "github.com/cosmos/cosmos-sdk/client" @@ -17,7 +20,7 @@ func CmdUpdatePolices() *cobra.Command { Short: "Update the policies", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) (err error) { - policies, err := readPoliciesFromFile(args[0]) + policies, err := ReadPoliciesFromFile(os.DirFS("."), args[0]) if err != nil { return err } @@ -39,14 +42,14 @@ func CmdUpdatePolices() *cobra.Command { return cmd } -// readPoliciesFromFile read the policies from the file using os package and unmarshal it into the policies variable -func readPoliciesFromFile(filePath string) (types.Policies, error) { +// ReadPoliciesFromFile read the policies from the file using os package and unmarshal it into the policies variable +func ReadPoliciesFromFile(fsys fs.FS, filePath string) (types.Policies, error) { var policies types.Policies - policiesBytes, err := os.ReadFile(filePath) + policiesBytes, err := fs.ReadFile(fsys, filePath) if err != nil { - return policies, err + return policies, fmt.Errorf("failed to read file: %w", err) } - err = policies.Unmarshal(policiesBytes) + err = json.Unmarshal(policiesBytes, &policies) return policies, err } diff --git a/x/authority/client/cli/tx_update_policies_test.go b/x/authority/client/cli/tx_update_policies_test.go new file mode 100644 index 0000000000..45e6813fcb --- /dev/null +++ b/x/authority/client/cli/tx_update_policies_test.go @@ -0,0 +1,32 @@ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/zeta-chain/zetacore/pkg/testdata" + "github.com/zeta-chain/zetacore/x/authority/client/cli" + authoritytypes "github.com/zeta-chain/zetacore/x/authority/types" +) + +func TestReadPoliciesFromFile(t *testing.T) { + fs := testdata.TypesFiles + + policies, err := cli.ReadPoliciesFromFile(fs, "types/policies.json") + require.NoError(t, err) + + require.Len(t, policies.Items, 3) + require.EqualValues(t, &authoritytypes.Policy{ + PolicyType: authoritytypes.PolicyType_groupEmergency, + Address: "zeta1nl7550unvzyswx5ts9m338ufmfydjsz2g0xt74", + }, policies.Items[0]) + require.EqualValues(t, &authoritytypes.Policy{ + PolicyType: authoritytypes.PolicyType_groupOperational, + Address: "zeta1n0rn6sne54hv7w2uu93fl48ncyqz97d3kty6sh", + }, policies.Items[1]) + require.EqualValues(t, &authoritytypes.Policy{ + PolicyType: authoritytypes.PolicyType_groupAdmin, + Address: "zeta1srsq755t654agc0grpxj4y3w0znktrpr9tcdgk", + }, policies.Items[2]) +}