Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Aug 19, 2024
1 parent 85c23fe commit 48a8e23
Show file tree
Hide file tree
Showing 30 changed files with 683 additions and 677 deletions.
208 changes: 105 additions & 103 deletions client/docs/swagger-ui/swagger.yaml

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions client/query/zoneconcierge.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func (c *QueryClient) QueryZoneConcierge(f func(ctx context.Context, queryClient
}

// FinalizedConnectedChainsInfo queries the zoneconcierge module to get the finalization information for a connected chain
func (c *QueryClient) FinalizedConnectedChainsInfo(chainIds []string) (*zctypes.QueryFinalizedChainsInfoResponse, error) {
func (c *QueryClient) FinalizedConnectedChainsInfo(consumerIds []string) (*zctypes.QueryFinalizedChainsInfoResponse, error) {
var resp *zctypes.QueryFinalizedChainsInfoResponse
err := c.QueryZoneConcierge(func(ctx context.Context, queryClient zctypes.QueryClient) error {
var err error
req := &zctypes.QueryFinalizedChainsInfoRequest{
ChainIds: chainIds,
ConsumerIds: consumerIds,
}
resp, err = queryClient.FinalizedChainsInfo(ctx, req)
return err
Expand All @@ -36,12 +36,12 @@ func (c *QueryClient) FinalizedConnectedChainsInfo(chainIds []string) (*zctypes.
}

// ConnectedChainsInfo queries the zoneconcierge module to get information for a connected chain
func (c *QueryClient) ConnectedChainsInfo(chainIds []string) (*zctypes.QueryChainsInfoResponse, error) {
func (c *QueryClient) ConnectedChainsInfo(consumerIds []string) (*zctypes.QueryChainsInfoResponse, error) {
var resp *zctypes.QueryChainsInfoResponse
err := c.QueryZoneConcierge(func(ctx context.Context, queryClient zctypes.QueryClient) error {
var err error
req := &zctypes.QueryChainsInfoRequest{
ChainIds: chainIds,
ConsumerIds: consumerIds,
}
resp, err = queryClient.ChainsInfo(ctx, req)
return err
Expand All @@ -64,12 +64,12 @@ func (c *QueryClient) ConnectedChainList() (*zctypes.QueryChainListResponse, err
}

// ConnectedChainHeaders queries the zoneconcierge module for the headers of a connected chain
func (c *QueryClient) ConnectedChainHeaders(chainID string, pagination *sdkquerytypes.PageRequest) (*zctypes.QueryListHeadersResponse, error) {
func (c *QueryClient) ConnectedChainHeaders(consumerID string, pagination *sdkquerytypes.PageRequest) (*zctypes.QueryListHeadersResponse, error) {
var resp *zctypes.QueryListHeadersResponse
err := c.QueryZoneConcierge(func(ctx context.Context, queryClient zctypes.QueryClient) error {
var err error
req := &zctypes.QueryListHeadersRequest{
ChainId: chainID,
ConsumerId: consumerID,
Pagination: pagination,
}
resp, err = queryClient.ListHeaders(ctx, req)
Expand All @@ -80,13 +80,13 @@ func (c *QueryClient) ConnectedChainHeaders(chainID string, pagination *sdkquery
}

// ConnectedChainsEpochInfo queries the zoneconcierge module for the chain information of a connected chain at a particular epoch
func (c *QueryClient) ConnectedChainsEpochInfo(chainIds []string, epochNum uint64) (*zctypes.QueryEpochChainsInfoResponse, error) {
func (c *QueryClient) ConnectedChainsEpochInfo(consumerIds []string, epochNum uint64) (*zctypes.QueryEpochChainsInfoResponse, error) {
var resp *zctypes.QueryEpochChainsInfoResponse
err := c.QueryZoneConcierge(func(ctx context.Context, queryClient zctypes.QueryClient) error {
var err error
req := &zctypes.QueryEpochChainsInfoRequest{
ChainIds: chainIds,
EpochNum: epochNum,
ConsumerIds: consumerIds,
EpochNum: epochNum,
}
resp, err = queryClient.EpochChainsInfo(ctx, req)
return err
Expand Down
30 changes: 15 additions & 15 deletions proto/babylon/zoneconcierge/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ service Query {
// Header queries the CZ header and fork headers at a given height.
rpc Header(QueryHeaderRequest) returns (QueryHeaderResponse) {
option (google.api.http).get =
"/babylon/zoneconcierge/v1/chain_info/{chain_id}/header/{height}";
"/babylon/zoneconcierge/v1/chain_info/{consumer_id}/header/{height}";
}
// ChainList queries the list of chains that checkpoint to Babylon
rpc ChainList(QueryChainListRequest) returns (QueryChainListResponse) {
Expand All @@ -43,14 +43,14 @@ service Query {
// pagination support
rpc ListHeaders(QueryListHeadersRequest) returns (QueryListHeadersResponse) {
option (google.api.http).get =
"/babylon/zoneconcierge/v1/headers/{chain_id}";
"/babylon/zoneconcierge/v1/headers/{consumer_id}";
}
// ListEpochHeaders queries the headers of a chain timestamped in a given
// epoch of Babylon, with pagination support
rpc ListEpochHeaders(QueryListEpochHeadersRequest)
returns (QueryListEpochHeadersResponse) {
option (google.api.http).get =
"/babylon/zoneconcierge/v1/headers/{chain_id}/epochs/{epoch_num}";
"/babylon/zoneconcierge/v1/headers/{consumer_id}/epochs/{epoch_num}";
}
// FinalizedChainsInfo queries the BTC-finalised info of chains with given IDs, with proofs
rpc FinalizedChainsInfo(QueryFinalizedChainsInfoRequest)
Expand All @@ -63,7 +63,7 @@ service Query {
rpc FinalizedChainInfoUntilHeight(QueryFinalizedChainInfoUntilHeightRequest)
returns (QueryFinalizedChainInfoUntilHeightResponse) {
option (google.api.http).get =
"/babylon/zoneconcierge/v1/finalized_chain_info/{chain_id}/height/"
"/babylon/zoneconcierge/v1/finalized_chain_info/{consumer_id}/height/"
"{height}";
}
}
Expand All @@ -79,7 +79,7 @@ message QueryParamsResponse {

// QueryHeaderRequest is request type for the Query/Header RPC method.
message QueryHeaderRequest {
string chain_id = 1;
string consumer_id = 1;
uint64 height = 2;
}

Expand All @@ -97,14 +97,14 @@ message QueryChainListRequest {

// QueryChainListResponse is response type for the Query/ChainList RPC method
message QueryChainListResponse {
// chain_ids are IDs of the chains in ascending alphabetical order
repeated string chain_ids = 1;
// consumer_ids are IDs of the chains in ascending alphabetical order
repeated string consumer_ids = 1;
// pagination defines the pagination in the response
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QueryChainsInfoRequest is request type for the Query/ChainsInfo RPC method.
message QueryChainsInfoRequest { repeated string chain_ids = 1; }
message QueryChainsInfoRequest { repeated string consumer_ids = 1; }

// QueryChainsInfoResponse is response type for the Query/ChainsInfo RPC method.
message QueryChainsInfoResponse {
Expand All @@ -115,7 +115,7 @@ message QueryChainsInfoResponse {
// method.
message QueryEpochChainsInfoRequest {
uint64 epoch_num = 1;
repeated string chain_ids = 2;
repeated string consumer_ids = 2;
}

// QueryEpochChainsInfoResponse is response type for the Query/EpochChainsInfo RPC
Expand All @@ -127,7 +127,7 @@ message QueryEpochChainsInfoResponse {

// QueryListHeadersRequest is request type for the Query/ListHeaders RPC method.
message QueryListHeadersRequest {
string chain_id = 1;
string consumer_id = 1;
// pagination defines whether to have the pagination in the request
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}
Expand All @@ -145,7 +145,7 @@ message QueryListHeadersResponse {
// RPC method.
message QueryListEpochHeadersRequest {
uint64 epoch_num = 1;
string chain_id = 2;
string consumer_id = 2;
}

// QueryListEpochHeadersResponse is response type for the Query/ListEpochHeaders
Expand All @@ -158,8 +158,8 @@ message QueryListEpochHeadersResponse {
// QueryFinalizedChainsInfoRequest is request type for the
// Query/FinalizedChainsInfo RPC method.
message QueryFinalizedChainsInfoRequest {
// chain_ids is the list of ids of CZs
repeated string chain_ids = 1;
// consumer_ids is the list of ids of CZs
repeated string consumer_ids = 1;
// prove indicates whether the querier wants to get proofs of this timestamp
bool prove = 2;
}
Expand All @@ -173,8 +173,8 @@ message QueryFinalizedChainsInfoResponse {
// QueryFinalizedChainInfoUntilHeightRequest is request type for the
// Query/FinalizedChainInfoUntilHeight RPC method.
message QueryFinalizedChainInfoUntilHeightRequest {
// chain_id is the ID of the CZ
string chain_id = 1;
// consumer_id is the ID of the CZ
string consumer_id = 1;
// height is the height of the CZ chain
// such that the returned finalised chain info will be no later than this
// height
Expand Down
12 changes: 6 additions & 6 deletions proto/babylon/zoneconcierge/v1/zoneconcierge.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ option go_package = "github.com/babylonlabs-io/babylon/x/zoneconcierge/types";

// IndexedHeader is the metadata of a CZ header
message IndexedHeader {
// chain_id is the unique ID of the chain
string chain_id = 1;
// consumer_id is the unique ID of the consumer
string consumer_id = 1;
// hash is the hash of this header
bytes hash = 2;
// height is the height of this header on CZ ledger
Expand Down Expand Up @@ -60,8 +60,8 @@ message Forks {

// ChainInfo is the information of a CZ
message ChainInfo {
// chain_id is the ID of the chain
string chain_id = 1;
// consumer_id is the ID of the consumer
string consumer_id = 1;
// latest_header is the latest header in CZ's canonical chain
IndexedHeader latest_header = 2;
// latest_forks is the latest forks, formed as a series of IndexedHeader (from
Expand All @@ -84,8 +84,8 @@ message ChainInfoWithProof {

// FinalizedChainInfo is the information of a CZ that is BTC-finalised
message FinalizedChainInfo {
// chain_id is the ID of the chain
string chain_id = 1;
// consumer_id is the ID of the consumer
string consumer_id = 1;
// finalized_chain_info is the info of the CZ
babylon.zoneconcierge.v1.ChainInfo finalized_chain_info = 2;

Expand Down
6 changes: 3 additions & 3 deletions test/e2e/btc_timestamping_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (s *BTCTimestampingTestSuite) Test4IbcCheckpointing() {
// Query checkpoint chain info for opposing chain
chainsInfo, err := nonValidatorNode.QueryChainsInfo([]string{initialization.ChainBID})
s.NoError(err)
s.Equal(chainsInfo[0].ChainId, initialization.ChainBID)
s.Equal(chainsInfo[0].ConsumerId, initialization.ChainBID)

// Finalize epoch 1, 2, 3, as first headers of opposing chain are in epoch 3
var (
Expand All @@ -142,15 +142,15 @@ func (s *BTCTimestampingTestSuite) Test4IbcCheckpointing() {
// Check we have epoch info for opposing chain and some basic assertions
epochChainsInfo, err := nonValidatorNode.QueryEpochChainsInfo(endEpochNum, []string{initialization.ChainBID})
s.NoError(err)
s.Equal(epochChainsInfo[0].ChainId, initialization.ChainBID)
s.Equal(epochChainsInfo[0].ConsumerId, initialization.ChainBID)
s.Equal(epochChainsInfo[0].LatestHeader.BabylonEpoch, endEpochNum)

// Check we have finalized epoch info for opposing chain and some basic assertions
finalizedChainsInfo, err := nonValidatorNode.QueryFinalizedChainsInfo([]string{initialization.ChainBID})
s.NoError(err)

// TODO Add more assertion here. Maybe check proofs ?
s.Equal(finalizedChainsInfo[0].FinalizedChainInfo.ChainId, initialization.ChainBID)
s.Equal(finalizedChainsInfo[0].FinalizedChainInfo.ConsumerId, initialization.ChainBID)
s.Equal(finalizedChainsInfo[0].EpochInfo.EpochNumber, endEpochNum)

currEpoch, err := nonValidatorNode.QueryCurrentEpoch()
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/configurer/chain/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,13 @@ func (n *NodeConfig) QueryChains() (*[]string, error) {
if err := util.Cdc.UnmarshalJSON(bz, &chainsResponse); err != nil {
return nil, err
}
return &chainsResponse.ChainIds, nil
return &chainsResponse.ConsumerIds, nil
}

func (n *NodeConfig) QueryChainsInfo(chainIDs []string) ([]*zctypes.ChainInfo, error) {
func (n *NodeConfig) QueryChainsInfo(consumerIDs []string) ([]*zctypes.ChainInfo, error) {
queryParams := url.Values{}
for _, chainId := range chainIDs {
queryParams.Add("chain_ids", chainId)
for _, consumerId := range consumerIDs {
queryParams.Add("consumer_ids", consumerId)
}

bz, err := n.QueryGRPCGateway("/babylon/zoneconcierge/v1/chains_info", queryParams)
Expand Down
18 changes: 9 additions & 9 deletions x/zoneconcierge/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func GetQueryCmd(queryRoute string) *cobra.Command {

func CmdChainsInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "chains-info <chain-ids>",
Short: "retrieve the latest info for a given list of chains",
Use: "chains-info <consumer-ids>",
Short: "retrieve the latest info for a given list of consumers",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)
req := types.QueryChainsInfoRequest{ChainIds: args}
req := types.QueryChainsInfoRequest{ConsumerIds: args}
resp, err := queryClient.ChainsInfo(cmd.Context(), &req)
if err != nil {
return err
Expand All @@ -53,15 +53,15 @@ func CmdChainsInfo() *cobra.Command {

func CmdFinalizedChainsInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "finalized-chains-info <chain-ids>",
Short: "retrieve the finalized info for a given list of chains",
Use: "finalized-chains-info <consumer-ids>",
Short: "retrieve the finalized info for a given list of consumers",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
prove, _ := cmd.Flags().GetBool("prove")

clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)
req := types.QueryFinalizedChainsInfoRequest{ChainIds: args, Prove: prove}
req := types.QueryFinalizedChainsInfoRequest{ConsumerIds: args, Prove: prove}
resp, err := queryClient.FinalizedChainsInfo(cmd.Context(), &req)
if err != nil {
return err
Expand All @@ -79,8 +79,8 @@ func CmdFinalizedChainsInfo() *cobra.Command {

func CmdEpochChainsInfoInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "epoch-chains-info <epoch-num> <chain-ids>",
Short: "retrieve the latest info for a list of chains in a given epoch",
Use: "epoch-chains-info <epoch-num> <consumer-ids>",
Short: "retrieve the latest info for a list of consumers in a given epoch",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
Expand All @@ -90,7 +90,7 @@ func CmdEpochChainsInfoInfo() *cobra.Command {
if err != nil {
return err
}
req := types.QueryEpochChainsInfoRequest{EpochNum: epoch, ChainIds: args[1:]}
req := types.QueryEpochChainsInfoRequest{EpochNum: epoch, ConsumerIds: args[1:]}
resp, err := queryClient.EpochChainsInfo(cmd.Context(), &req)
if err != nil {
return err
Expand Down
27 changes: 14 additions & 13 deletions x/zoneconcierge/keeper/canonical_chain_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/runtime"

sdkerrors "cosmossdk.io/errors"
Expand All @@ -12,10 +13,10 @@ import (
)

// FindClosestHeader finds the IndexedHeader that is closest to (but not after) the given height
func (k Keeper) FindClosestHeader(ctx context.Context, chainID string, height uint64) (*types.IndexedHeader, error) {
chainInfo, err := k.GetChainInfo(ctx, chainID)
func (k Keeper) FindClosestHeader(ctx context.Context, consumerID string, height uint64) (*types.IndexedHeader, error) {
chainInfo, err := k.GetChainInfo(ctx, consumerID)
if err != nil {
return nil, fmt.Errorf("failed to get chain info for chain with ID %s: %w", chainID, err)
return nil, fmt.Errorf("failed to get chain info for chain with ID %s: %w", consumerID, err)
}

// if the given height is no lower than the latest header, return the latest header directly
Expand All @@ -24,13 +25,13 @@ func (k Keeper) FindClosestHeader(ctx context.Context, chainID string, height ui
}

// the requested height is lower than the latest header, trace back until finding a timestamped header
store := k.canonicalChainStore(ctx, chainID)
store := k.canonicalChainStore(ctx, consumerID)
heightBytes := sdk.Uint64ToBigEndian(height)
iter := store.ReverseIterator(nil, heightBytes)
defer iter.Close()
// if there is no key within range [0, height], return error
if !iter.Valid() {
return nil, fmt.Errorf("chain with ID %s does not have a timestamped header before height %d", chainID, height)
return nil, fmt.Errorf("chain with ID %s does not have a timestamped header before height %d", consumerID, height)
}
// find the header in bytes, decode and return
headerBytes := iter.Value()
Expand All @@ -39,8 +40,8 @@ func (k Keeper) FindClosestHeader(ctx context.Context, chainID string, height ui
return &header, nil
}

func (k Keeper) GetHeader(ctx context.Context, chainID string, height uint64) (*types.IndexedHeader, error) {
store := k.canonicalChainStore(ctx, chainID)
func (k Keeper) GetHeader(ctx context.Context, consumerID string, height uint64) (*types.IndexedHeader, error) {
store := k.canonicalChainStore(ctx, consumerID)
heightBytes := sdk.Uint64ToBigEndian(height)
if !store.Has(heightBytes) {
return nil, types.ErrHeaderNotFound
Expand All @@ -51,23 +52,23 @@ func (k Keeper) GetHeader(ctx context.Context, chainID string, height uint64) (*
return &header, nil
}

func (k Keeper) insertHeader(ctx context.Context, chainID string, header *types.IndexedHeader) error {
func (k Keeper) insertHeader(ctx context.Context, consumerID string, header *types.IndexedHeader) error {
if header == nil {
return sdkerrors.Wrapf(types.ErrInvalidHeader, "header is nil")
}
// NOTE: we can accept header without ancestor since IBC connection can be established at any height
store := k.canonicalChainStore(ctx, chainID)
store := k.canonicalChainStore(ctx, consumerID)
store.Set(sdk.Uint64ToBigEndian(header.Height), k.cdc.MustMarshal(header))
return nil
}

// canonicalChainStore stores the canonical chain of a CZ, formed as a list of IndexedHeader
// prefix: CanonicalChainKey || chainID
// prefix: CanonicalChainKey || consumerID
// key: height
// value: IndexedHeader
func (k Keeper) canonicalChainStore(ctx context.Context, chainID string) prefix.Store {
func (k Keeper) canonicalChainStore(ctx context.Context, consumerID string) prefix.Store {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
canonicalChainStore := prefix.NewStore(storeAdapter, types.CanonicalChainKey)
chainIDBytes := []byte(chainID)
return prefix.NewStore(canonicalChainStore, chainIDBytes)
consumerIDBytes := []byte(consumerID)
return prefix.NewStore(canonicalChainStore, consumerIDBytes)
}
Loading

0 comments on commit 48a8e23

Please sign in to comment.