Skip to content

Commit

Permalink
get clusters info
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBECEDA committed May 24, 2024
1 parent 5c8d57d commit 31ef676
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion blockchain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewClient(url string) (*Client, error) {
return &Client{
SubstrateAPI: substrateApi,
eventsListeners: make(map[*EventsListener]struct{}),
DdcClusters: pallets.NewDdcClustersApi(substrateApi),
DdcClusters: pallets.NewDdcClustersApi(substrateApi, meta),
DdcCustomers: pallets.NewDdcCustomersApi(substrateApi, meta),
DdcNodes: pallets.NewDdcNodesApi(substrateApi, meta),
DdcPayouts: pallets.NewDdcPayoutsApi(substrateApi, meta),
Expand Down
32 changes: 29 additions & 3 deletions blockchain/pallets/ddcclusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ type ClusterProps struct {

type DdcClustersApi interface {
GetClustersNodes(clusterId ClusterId) ([]NodePubKey, error)
GetClusterInfo(clusterId ClusterId) (types.Option[Cluster], error)
}

type ddcClustersApi struct {
substrateApi *gsrpc.SubstrateAPI

substrateApi *gsrpc.SubstrateAPI
meta *types.Metadata
clustersNodesKey []byte
}

func NewDdcClustersApi(substrateApi *gsrpc.SubstrateAPI) DdcClustersApi {
func NewDdcClustersApi(substrateApi *gsrpc.SubstrateAPI, meta *types.Metadata) DdcClustersApi {
clustersNodesKey := append(
xxhash.New128([]byte("DdcClusters")).Sum(nil),
xxhash.New128([]byte("ClustersNodes")).Sum(nil)...,
Expand All @@ -43,6 +44,7 @@ func NewDdcClustersApi(substrateApi *gsrpc.SubstrateAPI) DdcClustersApi {
return &ddcClustersApi{
substrateApi: substrateApi,
clustersNodesKey: clustersNodesKey,
meta: meta,
}
}

Expand Down Expand Up @@ -87,3 +89,27 @@ func (api *ddcClustersApi) GetClustersNodes(clusterId ClusterId) ([]NodePubKey,

return nodesKeys, nil
}

func (api *ddcClustersApi) GetClusterInfo(clusterId ClusterId) (types.Option[Cluster], error) {
mayBeCluster := types.NewEmptyOption[Cluster]()

bytes, err := codec.Encode(clusterId)
if err != nil {
return mayBeCluster, err
}

key, err := types.CreateStorageKey(api.meta, "DdcClusters", "Clusters", bytes)
if err != nil {
return mayBeCluster, err
}

var cluster Cluster
ok, err := api.substrateApi.RPC.State.GetStorageLatest(key, &cluster)
if !ok || err != nil {
return mayBeCluster, err
}

mayBeCluster.SetSome(cluster)

return mayBeCluster, nil
}

0 comments on commit 31ef676

Please sign in to comment.