From d40398fb51fe2fa54df99fbba4c7bd54c70f4c46 Mon Sep 17 00:00:00 2001 From: Matee Ullah Malik Date: Thu, 21 Aug 2025 00:20:08 +0500 Subject: [PATCH] fix: sort supernode IP addresses and states by blockchain height --- cmd/sncli/go.mod | 2 +- cmd/sncli/go.sum | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- sdk/adapters/lumera/adapter.go | 42 +++++++++++++++++++++++++++++----- tests/system/go.mod | 2 +- tests/system/go.sum | 4 ++-- 7 files changed, 45 insertions(+), 15 deletions(-) diff --git a/cmd/sncli/go.mod b/cmd/sncli/go.mod index d01ee894..0f3a0c23 100644 --- a/cmd/sncli/go.mod +++ b/cmd/sncli/go.mod @@ -33,7 +33,7 @@ require ( github.com/99designs/keyring v1.2.2 // indirect github.com/DataDog/datadog-go v3.2.0+incompatible // indirect github.com/DataDog/zstd v1.5.5 // indirect - github.com/LumeraProtocol/lumera v1.6.0 // indirect + github.com/LumeraProtocol/lumera v1.7.0 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect github.com/bytedance/sonic v1.12.3 // indirect diff --git a/cmd/sncli/go.sum b/cmd/sncli/go.sum index e9023b9b..62b166f6 100644 --- a/cmd/sncli/go.sum +++ b/cmd/sncli/go.sum @@ -63,8 +63,8 @@ github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3 github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ= github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= -github.com/LumeraProtocol/lumera v1.6.0 h1:5I172U/f1Migt7tRxnywhz5aRKCpBOx/IMgOzhJfTP0= -github.com/LumeraProtocol/lumera v1.6.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo= +github.com/LumeraProtocol/lumera v1.7.0 h1:F5zgRBnCtgGfdMB6jz01PFWIzbS8VjQfCu1H9OYt3BU= +github.com/LumeraProtocol/lumera v1.7.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE= diff --git a/go.mod b/go.mod index e09b5998..b7f53ec1 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ replace ( require ( cosmossdk.io/math v1.5.3 github.com/AlecAivazis/survey/v2 v2.3.7 - github.com/LumeraProtocol/lumera v1.6.0 + github.com/LumeraProtocol/lumera v1.7.0 github.com/LumeraProtocol/rq-go v0.2.1 github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce github.com/cenkalti/backoff/v4 v4.3.0 diff --git a/go.sum b/go.sum index e7b93fe8..d8170371 100644 --- a/go.sum +++ b/go.sum @@ -63,8 +63,8 @@ github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3 github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ= github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= -github.com/LumeraProtocol/lumera v1.6.0 h1:5I172U/f1Migt7tRxnywhz5aRKCpBOx/IMgOzhJfTP0= -github.com/LumeraProtocol/lumera v1.6.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo= +github.com/LumeraProtocol/lumera v1.7.0 h1:F5zgRBnCtgGfdMB6jz01PFWIzbS8VjQfCu1H9OYt3BU= +github.com/LumeraProtocol/lumera v1.7.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo= github.com/LumeraProtocol/rq-go v0.2.1 h1:8B3UzRChLsGMmvZ+UVbJsJj6JZzL9P9iYxbdUwGsQI4= github.com/LumeraProtocol/rq-go v0.2.1/go.mod h1:APnKCZRh1Es2Vtrd2w4kCLgAyaL5Bqrkz/BURoRJ+O8= github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= diff --git a/sdk/adapters/lumera/adapter.go b/sdk/adapters/lumera/adapter.go index f77d8213..826dc2f1 100644 --- a/sdk/adapters/lumera/adapter.go +++ b/sdk/adapters/lumera/adapter.go @@ -3,6 +3,7 @@ package lumera import ( "context" "fmt" + "sort" "github.com/LumeraProtocol/supernode/v2/sdk/log" @@ -194,13 +195,14 @@ func toSdkSupernodes(resp *sntypes.QueryGetTopSuperNodesForBlockResponse) []Supe continue } - // Check if States slice has at least one element - if len(sn.States) == 0 { + // Get the latest state based on height + latestState, err := getLatestState(sn) + if err != nil { continue } - // Check if the first state is active - if sn.States[0].State.String() != string(SUPERNODE_STATE_ACTIVE) { + // Check if the latest state is active + if latestState.State.String() != string(SUPERNODE_STATE_ACTIVE) { continue } @@ -213,6 +215,29 @@ func toSdkSupernodes(resp *sntypes.QueryGetTopSuperNodesForBlockResponse) []Supe return result } +func getLatestState(supernode *sntypes.SuperNode) (*sntypes.SuperNodeStateRecord, error) { + if supernode == nil { + return nil, fmt.Errorf("supernode is nil") + } + + // Check if the slice has elements before accessing it + if len(supernode.States) == 0 { + return nil, fmt.Errorf("no state history exists for the supernode") + } + + // Sort by height in descending order to get the latest first + sort.Slice(supernode.States, func(i, j int) bool { + return supernode.States[i].Height > supernode.States[j].Height + }) + + // Access the latest state safely + if supernode.States[0] == nil { + return nil, fmt.Errorf("latest state in history is nil") + } + + return supernode.States[0], nil +} + func getLatestIP(supernode *sntypes.SuperNode) (string, error) { if supernode == nil { return "", fmt.Errorf("supernode is nil") @@ -223,9 +248,14 @@ func getLatestIP(supernode *sntypes.SuperNode) (string, error) { return "", fmt.Errorf("no ip history exists for the supernode") } - // Access the first element safely + // Sort by height in descending order to get the latest first + sort.Slice(supernode.PrevIpAddresses, func(i, j int) bool { + return supernode.PrevIpAddresses[i].Height > supernode.PrevIpAddresses[j].Height + }) + + // Access the latest IP address safely if supernode.PrevIpAddresses[0] == nil { - return "", fmt.Errorf("first IP address in history is nil") + return "", fmt.Errorf("latest IP address in history is nil") } return supernode.PrevIpAddresses[0].Address, nil diff --git a/tests/system/go.mod b/tests/system/go.mod index be7e7182..00960f9e 100644 --- a/tests/system/go.mod +++ b/tests/system/go.mod @@ -27,7 +27,7 @@ require ( require ( cosmossdk.io/math v1.5.3 - github.com/LumeraProtocol/lumera v1.6.0 + github.com/LumeraProtocol/lumera v1.7.0 github.com/LumeraProtocol/supernode/v2 v2.0.0-00010101000000-000000000000 github.com/cometbft/cometbft v0.38.17 github.com/cosmos/btcutil v1.0.5 diff --git a/tests/system/go.sum b/tests/system/go.sum index f8d3f9c5..afece699 100644 --- a/tests/system/go.sum +++ b/tests/system/go.sum @@ -73,8 +73,8 @@ github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.48 github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1 h1:8nn+rsCvTq9axyEh382S0PFLBeaFwNsT43IrPWzctRU= github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1/go.mod h1:viRWSEhtMZqz1rhwmOVKkWl6SwmVowfL9O2YR5gI2PE= github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= -github.com/LumeraProtocol/lumera v1.6.0 h1:5I172U/f1Migt7tRxnywhz5aRKCpBOx/IMgOzhJfTP0= -github.com/LumeraProtocol/lumera v1.6.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo= +github.com/LumeraProtocol/lumera v1.7.0 h1:F5zgRBnCtgGfdMB6jz01PFWIzbS8VjQfCu1H9OYt3BU= +github.com/LumeraProtocol/lumera v1.7.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo= github.com/LumeraProtocol/rq-go v0.2.1 h1:8B3UzRChLsGMmvZ+UVbJsJj6JZzL9P9iYxbdUwGsQI4= github.com/LumeraProtocol/rq-go v0.2.1/go.mod h1:APnKCZRh1Es2Vtrd2w4kCLgAyaL5Bqrkz/BURoRJ+O8= github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=