Skip to content

Commit

Permalink
add unit test for parse type in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed May 30, 2024
1 parent 2c4902a commit 14d06c3
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/cli/zetacored/zetacored_query_authority.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

34 changes: 34 additions & 0 deletions docs/cli/zetacored/zetacored_query_authority_show-chain-info.md
Original file line number Diff line number Diff line change
@@ -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

23 changes: 23 additions & 0 deletions docs/openapi/openapi.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions docs/spec/authority/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```
Expand Down
5 changes: 5 additions & 0 deletions pkg/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions pkg/testdata/types/chain_info.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
16 changes: 16 additions & 0 deletions pkg/testdata/types/policies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"items": [
{
"address": "zeta1nl7550unvzyswx5ts9m338ufmfydjsz2g0xt74",
"policy_type": 0
},
{
"address": "zeta1n0rn6sne54hv7w2uu93fl48ncyqz97d3kty6sh",
"policy_type": 1
},
{
"address": "zeta1srsq755t654agc0grpxj4y3w0znktrpr9tcdgk",
"policy_type": 2
}
]
}
50 changes: 50 additions & 0 deletions typescript/zetachain/zetacore/authority/query_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,3 +57,52 @@ export declare class QueryGetPoliciesResponse extends Message<QueryGetPoliciesRe
static equals(a: QueryGetPoliciesResponse | PlainMessage<QueryGetPoliciesResponse> | undefined, b: QueryGetPoliciesResponse | PlainMessage<QueryGetPoliciesResponse> | 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<QueryGetChainInfoRequest> {
constructor(data?: PartialMessage<QueryGetChainInfoRequest>);

static readonly runtime: typeof proto3;
static readonly typeName = "zetachain.zetacore.authority.QueryGetChainInfoRequest";
static readonly fields: FieldList;

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): QueryGetChainInfoRequest;

static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): QueryGetChainInfoRequest;

static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): QueryGetChainInfoRequest;

static equals(a: QueryGetChainInfoRequest | PlainMessage<QueryGetChainInfoRequest> | undefined, b: QueryGetChainInfoRequest | PlainMessage<QueryGetChainInfoRequest> | 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<QueryGetChainInfoResponse> {
/**
* @generated from field: zetachain.zetacore.authority.ChainInfo chain_info = 1;
*/
chainInfo?: ChainInfo;

constructor(data?: PartialMessage<QueryGetChainInfoResponse>);

static readonly runtime: typeof proto3;
static readonly typeName = "zetachain.zetacore.authority.QueryGetChainInfoResponse";
static readonly fields: FieldList;

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): QueryGetChainInfoResponse;

static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): QueryGetChainInfoResponse;

static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): QueryGetChainInfoResponse;

static equals(a: QueryGetChainInfoResponse | PlainMessage<QueryGetChainInfoResponse> | undefined, b: QueryGetChainInfoResponse | PlainMessage<QueryGetChainInfoResponse> | undefined): boolean;
}

8 changes: 4 additions & 4 deletions typescript/zetachain/zetacore/authority/tx_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import type { ChainInfo } from "./chain_info_pb.js";
*/
export declare class MsgUpdatePolicies extends Message<MsgUpdatePolicies> {
/**
* @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;
Expand Down Expand Up @@ -67,9 +67,9 @@ export declare class MsgUpdatePoliciesResponse extends Message<MsgUpdatePolicies
*/
export declare class MsgUpdateChainInfo extends Message<MsgUpdateChainInfo> {
/**
* @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;
Expand Down
16 changes: 10 additions & 6 deletions x/authority/client/cli/tx_update_chain_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cli

import (
"encoding/json"
"fmt"
"io/fs"
"os"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -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
}
Expand All @@ -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
}
40 changes: 40 additions & 0 deletions x/authority/client/cli/tx_update_chain_info_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
15 changes: 9 additions & 6 deletions x/authority/client/cli/tx_update_policies.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cli

import (
"encoding/json"
"fmt"
"io/fs"
"os"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -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
}
Expand All @@ -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
}
32 changes: 32 additions & 0 deletions x/authority/client/cli/tx_update_policies_test.go
Original file line number Diff line number Diff line change
@@ -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])
}

0 comments on commit 14d06c3

Please sign in to comment.