From 4f0141dc5b9649b842931185852358d57d446c46 Mon Sep 17 00:00:00 2001 From: Christopher Brumm <97845034+christopherbrumm@users.noreply.github.com> Date: Wed, 27 Dec 2023 11:32:04 +0100 Subject: [PATCH] update: support latest Source-Registry. (#35) --- cmd/ksync/commands/info.go | 21 +++++++++++--- sources/helpers/helpers.go | 56 ++++++++++++++++++++------------------ sources/sources.go | 16 +++++------ types/types.go | 39 ++++++++++++++++++-------- utils/constants.go | 2 +- 5 files changed, 84 insertions(+), 50 deletions(-) diff --git a/cmd/ksync/commands/info.go b/cmd/ksync/commands/info.go index 560db84..8e0ed09 100644 --- a/cmd/ksync/commands/info.go +++ b/cmd/ksync/commands/info.go @@ -43,7 +43,7 @@ var infoCmd = &cobra.Command{ // Sort SourceRegistry sortFunc := func(keys []string) { sort.Slice(keys, func(i, j int) bool { - return sourceRegistry.Entries[keys[i]].Source.ChainID < sourceRegistry.Entries[keys[j]].Source.ChainID + return sourceRegistry.Entries[keys[i]].SourceID < sourceRegistry.Entries[keys[j]].SourceID }) } keys := make([]string, 0, len(sourceRegistry.Entries)) @@ -59,19 +59,32 @@ var infoCmd = &cobra.Command{ for _, key := range keys { entry := sourceRegistry.Entries[key] + var title string + if chainId == utils.ChainIdMainnet { - if (entry.Kyve.StatePoolID == nil) && (entry.Kyve.BlockPoolID == nil) { + if entry.Networks.Kyve != nil { + if entry.Networks.Kyve.Integrations.KSYNC == nil { + continue + } + title = entry.Networks.Kyve.SourceMetadata.Title + } else { continue } } else if chainId == utils.ChainIdKaon { - if (entry.Kaon.StatePoolID == nil) && (entry.Kaon.BlockPoolID == nil) { + if entry.Networks.Kaon != nil { + if entry.Networks.Kaon.Integrations.KSYNC == nil { + continue + } + title = entry.Networks.Kaon.SourceMetadata.Title + } else { continue } } + blockSync, stateSync, heightSync := sources.FormatOutput(&entry, chainId) t.AppendRows([]table.Row{ { - entry.Source.Title, + title, blockSync, stateSync, heightSync, diff --git a/sources/helpers/helpers.go b/sources/helpers/helpers.go index 63b8141..0dd2c3b 100644 --- a/sources/helpers/helpers.go +++ b/sources/helpers/helpers.go @@ -16,37 +16,41 @@ const ( func LoadLatestPoolData(sourceRegistry types.SourceRegistry) (*types.SourceRegistry, error) { for _, entry := range sourceRegistry.Entries { - if entry.Kyve.BlockPoolID != nil { - poolResponse, err := pool.GetPoolInfo(utils.RestEndpointMainnet, int64(*entry.Kyve.BlockPoolID)) - if err != nil { - return nil, err + if entry.Networks.Kyve != nil { + if entry.Networks.Kyve.Integrations.KSYNC.BlockSyncPool != nil { + poolResponse, err := pool.GetPoolInfo(utils.RestEndpointMainnet, int64(*entry.Networks.Kyve.Integrations.KSYNC.BlockSyncPool)) + if err != nil { + return nil, err + } + entry.Networks.Kyve.BlockStartKey = &poolResponse.Pool.Data.StartKey + entry.Networks.Kyve.LatestBlockKey = &poolResponse.Pool.Data.CurrentKey } - entry.Kyve.BlockStartKey = &poolResponse.Pool.Data.StartKey - entry.Kyve.LatestBlockKey = &poolResponse.Pool.Data.CurrentKey - } - if entry.Kyve.StatePoolID != nil { - poolResponse, err := pool.GetPoolInfo(utils.RestEndpointMainnet, int64(*entry.Kyve.StatePoolID)) - if err != nil { - return nil, err + if entry.Networks.Kyve.Integrations.KSYNC.StateSyncPool != nil { + poolResponse, err := pool.GetPoolInfo(utils.RestEndpointMainnet, int64(*entry.Networks.Kyve.Integrations.KSYNC.StateSyncPool)) + if err != nil { + return nil, err + } + entry.Networks.Kyve.StateStartKey = &poolResponse.Pool.Data.StartKey + entry.Networks.Kyve.LatestStateKey = &poolResponse.Pool.Data.CurrentKey } - entry.Kyve.StateStartKey = &poolResponse.Pool.Data.StartKey - entry.Kyve.LatestStateKey = &poolResponse.Pool.Data.CurrentKey } - if entry.Kaon.BlockPoolID != nil { - poolResponse, err := pool.GetPoolInfo(utils.RestEndpointKaon, int64(*entry.Kaon.BlockPoolID)) - if err != nil { - return nil, err + if entry.Networks.Kaon != nil { + if entry.Networks.Kaon.Integrations.KSYNC.BlockSyncPool != nil { + poolResponse, err := pool.GetPoolInfo(utils.RestEndpointKaon, int64(*entry.Networks.Kaon.Integrations.KSYNC.BlockSyncPool)) + if err != nil { + return nil, err + } + entry.Networks.Kaon.BlockStartKey = &poolResponse.Pool.Data.StartKey + entry.Networks.Kaon.LatestBlockKey = &poolResponse.Pool.Data.CurrentKey } - entry.Kaon.BlockStartKey = &poolResponse.Pool.Data.StartKey - entry.Kaon.LatestBlockKey = &poolResponse.Pool.Data.CurrentKey - } - if entry.Kaon.StatePoolID != nil { - poolResponse, err := pool.GetPoolInfo(utils.RestEndpointKaon, int64(*entry.Kaon.StatePoolID)) - if err != nil { - return nil, err + if entry.Networks.Kaon.Integrations.KSYNC.StateSyncPool != nil { + poolResponse, err := pool.GetPoolInfo(utils.RestEndpointKaon, int64(*entry.Networks.Kaon.Integrations.KSYNC.StateSyncPool)) + if err != nil { + return nil, err + } + entry.Networks.Kaon.StateStartKey = &poolResponse.Pool.Data.StartKey + entry.Networks.Kaon.LatestStateKey = &poolResponse.Pool.Data.CurrentKey } - entry.Kaon.StateStartKey = &poolResponse.Pool.Data.StartKey - entry.Kaon.LatestStateKey = &poolResponse.Pool.Data.CurrentKey } } return &sourceRegistry, nil diff --git a/sources/sources.go b/sources/sources.go index dc1e1d4..6a9bc5c 100644 --- a/sources/sources.go +++ b/sources/sources.go @@ -15,10 +15,10 @@ import ( func FormatOutput(entry *types.Entry, chainId string) (string, string, string) { var blockKey, stateKey, heightKey string - if chainId == utils.ChainIdMainnet { - blockKey, stateKey, heightKey = helpers.FormatKeys(entry.Kyve.BlockStartKey, entry.Kyve.LatestBlockKey, entry.Kyve.StateStartKey, entry.Kyve.LatestStateKey) - } else if chainId == utils.ChainIdKaon { - blockKey, stateKey, heightKey = helpers.FormatKeys(entry.Kaon.BlockStartKey, entry.Kaon.LatestBlockKey, entry.Kaon.StateStartKey, entry.Kaon.LatestStateKey) + if chainId == utils.ChainIdMainnet && entry.Networks.Kyve != nil { + blockKey, stateKey, heightKey = helpers.FormatKeys(entry.Networks.Kyve.BlockStartKey, entry.Networks.Kyve.LatestBlockKey, entry.Networks.Kyve.StateStartKey, entry.Networks.Kyve.LatestStateKey) + } else if chainId == utils.ChainIdKaon && entry.Networks.Kaon != nil { + blockKey, stateKey, heightKey = helpers.FormatKeys(entry.Networks.Kaon.BlockStartKey, entry.Networks.Kaon.LatestBlockKey, entry.Networks.Kaon.StateStartKey, entry.Networks.Kaon.LatestStateKey) } return blockKey, stateKey, heightKey } @@ -77,12 +77,12 @@ func getPoolsBySource(chainId, source, registryUrl string) (*int, *int, error) { } for _, entry := range sourceRegistry.Entries { - if strings.ToLower(entry.Source.Title) == strings.ToLower(source) || - strings.ToLower(entry.Source.ChainID) == strings.ToLower(source) { + if strings.ToLower(entry.Networks.Kyve.SourceMetadata.Title) == strings.ToLower(source) || + strings.ToLower(entry.SourceID) == strings.ToLower(source) { if chainId == utils.ChainIdMainnet { - return entry.Kyve.BlockPoolID, entry.Kyve.StatePoolID, nil + return entry.Networks.Kyve.Integrations.KSYNC.BlockSyncPool, entry.Networks.Kyve.Integrations.KSYNC.StateSyncPool, nil } else if chainId == utils.ChainIdKaon { - return entry.Kaon.BlockPoolID, entry.Kaon.StatePoolID, nil + return entry.Networks.Kaon.Integrations.KSYNC.BlockSyncPool, entry.Networks.Kaon.Integrations.KSYNC.StateSyncPool, nil } } } diff --git a/types/types.go b/types/types.go index 7c082a0..c20adbf 100644 --- a/types/types.go +++ b/types/types.go @@ -95,28 +95,45 @@ type Metrics struct { CatchingUp bool `json:"catching_up"` } -type SourceMetadata struct { - ChainID string `yaml:"chain_id"` - Hex string `yaml:"hex"` - Title string `yaml:"title"` +type Networks struct { + Kaon *NetworkProperties `yaml:"kaon-1,omitempty"` + Kyve *NetworkProperties `yaml:"kyve-1,omitempty"` } -type KYVEInfo struct { - BlockPoolID *int `yaml:"block_pool_id,omitempty"` - StatePoolID *int `yaml:"state_pool_id,omitempty"` +type NetworkProperties struct { LatestBlockKey *string LatestStateKey *string BlockStartKey *string StateStartKey *string + Integrations *Integrations `yaml:"integrations,omitempty"` + Pools *[]Pool `yaml:"pools,omitempty"` + SourceMetadata *SourceMetadata `yaml:"properties,omitempty"` +} + +type Integrations struct { + KSYNC *KSYNCIntegration `yaml:"ksync,omitempty"` +} + +type KSYNCIntegration struct { + BlockSyncPool *int `yaml:"block-sync-pool"` + StateSyncPool *int `yaml:"state-sync-pool"` +} + +type SourceMetadata struct { + Title string `yaml:"title"` +} + +type Pool struct { + Id *int `yaml:"id"` + Runtime string `yaml:"runtime"` } type Entry struct { - Source SourceMetadata `yaml:"source"` - Kaon *KYVEInfo `yaml:"kaon-1,omitempty"` - Kyve *KYVEInfo `yaml:"kyve-1,omitempty"` + ConfigVersion *int `yaml:"config-version"` + Networks Networks `yaml:"networks"` + SourceID string `yaml:"source-id"` } type SourceRegistry struct { Entries map[string]Entry `yaml:",inline"` - Version string `yaml:"version"` } diff --git a/utils/constants.go b/utils/constants.go index 3cb0803..5173293 100644 --- a/utils/constants.go +++ b/utils/constants.go @@ -59,5 +59,5 @@ const ( ) const ( - DefaultRegistryURL = "https://github.com/KYVENetwork/source-registry/releases/latest/download/registry.yml" + DefaultRegistryURL = "https://raw.githubusercontent.com/KYVENetwork/source-registry/main/.github/registry.yml" )