Skip to content

Commit d40398f

Browse files
fix: sort supernode IP addresses and states by blockchain height
1 parent 234c0d0 commit d40398f

7 files changed

Lines changed: 45 additions & 15 deletions

File tree

cmd/sncli/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ require (
3333
github.com/99designs/keyring v1.2.2 // indirect
3434
github.com/DataDog/datadog-go v3.2.0+incompatible // indirect
3535
github.com/DataDog/zstd v1.5.5 // indirect
36-
github.com/LumeraProtocol/lumera v1.6.0 // indirect
36+
github.com/LumeraProtocol/lumera v1.7.0 // indirect
3737
github.com/beorn7/perks v1.0.1 // indirect
3838
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
3939
github.com/bytedance/sonic v1.12.3 // indirect

cmd/sncli/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3
6363
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
6464
github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
6565
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
66-
github.com/LumeraProtocol/lumera v1.6.0 h1:5I172U/f1Migt7tRxnywhz5aRKCpBOx/IMgOzhJfTP0=
67-
github.com/LumeraProtocol/lumera v1.6.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo=
66+
github.com/LumeraProtocol/lumera v1.7.0 h1:F5zgRBnCtgGfdMB6jz01PFWIzbS8VjQfCu1H9OYt3BU=
67+
github.com/LumeraProtocol/lumera v1.7.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo=
6868
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
6969
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
7070
github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE=

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ replace (
1111
require (
1212
cosmossdk.io/math v1.5.3
1313
github.com/AlecAivazis/survey/v2 v2.3.7
14-
github.com/LumeraProtocol/lumera v1.6.0
14+
github.com/LumeraProtocol/lumera v1.7.0
1515
github.com/LumeraProtocol/rq-go v0.2.1
1616
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
1717
github.com/cenkalti/backoff/v4 v4.3.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3
6363
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
6464
github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
6565
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
66-
github.com/LumeraProtocol/lumera v1.6.0 h1:5I172U/f1Migt7tRxnywhz5aRKCpBOx/IMgOzhJfTP0=
67-
github.com/LumeraProtocol/lumera v1.6.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo=
66+
github.com/LumeraProtocol/lumera v1.7.0 h1:F5zgRBnCtgGfdMB6jz01PFWIzbS8VjQfCu1H9OYt3BU=
67+
github.com/LumeraProtocol/lumera v1.7.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo=
6868
github.com/LumeraProtocol/rq-go v0.2.1 h1:8B3UzRChLsGMmvZ+UVbJsJj6JZzL9P9iYxbdUwGsQI4=
6969
github.com/LumeraProtocol/rq-go v0.2.1/go.mod h1:APnKCZRh1Es2Vtrd2w4kCLgAyaL5Bqrkz/BURoRJ+O8=
7070
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=

sdk/adapters/lumera/adapter.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lumera
33
import (
44
"context"
55
"fmt"
6+
"sort"
67

78
"github.com/LumeraProtocol/supernode/v2/sdk/log"
89

@@ -194,13 +195,14 @@ func toSdkSupernodes(resp *sntypes.QueryGetTopSuperNodesForBlockResponse) []Supe
194195
continue
195196
}
196197

197-
// Check if States slice has at least one element
198-
if len(sn.States) == 0 {
198+
// Get the latest state based on height
199+
latestState, err := getLatestState(sn)
200+
if err != nil {
199201
continue
200202
}
201203

202-
// Check if the first state is active
203-
if sn.States[0].State.String() != string(SUPERNODE_STATE_ACTIVE) {
204+
// Check if the latest state is active
205+
if latestState.State.String() != string(SUPERNODE_STATE_ACTIVE) {
204206
continue
205207
}
206208

@@ -213,6 +215,29 @@ func toSdkSupernodes(resp *sntypes.QueryGetTopSuperNodesForBlockResponse) []Supe
213215
return result
214216
}
215217

218+
func getLatestState(supernode *sntypes.SuperNode) (*sntypes.SuperNodeStateRecord, error) {
219+
if supernode == nil {
220+
return nil, fmt.Errorf("supernode is nil")
221+
}
222+
223+
// Check if the slice has elements before accessing it
224+
if len(supernode.States) == 0 {
225+
return nil, fmt.Errorf("no state history exists for the supernode")
226+
}
227+
228+
// Sort by height in descending order to get the latest first
229+
sort.Slice(supernode.States, func(i, j int) bool {
230+
return supernode.States[i].Height > supernode.States[j].Height
231+
})
232+
233+
// Access the latest state safely
234+
if supernode.States[0] == nil {
235+
return nil, fmt.Errorf("latest state in history is nil")
236+
}
237+
238+
return supernode.States[0], nil
239+
}
240+
216241
func getLatestIP(supernode *sntypes.SuperNode) (string, error) {
217242
if supernode == nil {
218243
return "", fmt.Errorf("supernode is nil")
@@ -223,9 +248,14 @@ func getLatestIP(supernode *sntypes.SuperNode) (string, error) {
223248
return "", fmt.Errorf("no ip history exists for the supernode")
224249
}
225250

226-
// Access the first element safely
251+
// Sort by height in descending order to get the latest first
252+
sort.Slice(supernode.PrevIpAddresses, func(i, j int) bool {
253+
return supernode.PrevIpAddresses[i].Height > supernode.PrevIpAddresses[j].Height
254+
})
255+
256+
// Access the latest IP address safely
227257
if supernode.PrevIpAddresses[0] == nil {
228-
return "", fmt.Errorf("first IP address in history is nil")
258+
return "", fmt.Errorf("latest IP address in history is nil")
229259
}
230260

231261
return supernode.PrevIpAddresses[0].Address, nil

tests/system/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ require (
2727

2828
require (
2929
cosmossdk.io/math v1.5.3
30-
github.com/LumeraProtocol/lumera v1.6.0
30+
github.com/LumeraProtocol/lumera v1.7.0
3131
github.com/LumeraProtocol/supernode/v2 v2.0.0-00010101000000-000000000000
3232
github.com/cometbft/cometbft v0.38.17
3333
github.com/cosmos/btcutil v1.0.5

tests/system/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.48
7373
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1 h1:8nn+rsCvTq9axyEh382S0PFLBeaFwNsT43IrPWzctRU=
7474
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1/go.mod h1:viRWSEhtMZqz1rhwmOVKkWl6SwmVowfL9O2YR5gI2PE=
7575
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
76-
github.com/LumeraProtocol/lumera v1.6.0 h1:5I172U/f1Migt7tRxnywhz5aRKCpBOx/IMgOzhJfTP0=
77-
github.com/LumeraProtocol/lumera v1.6.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo=
76+
github.com/LumeraProtocol/lumera v1.7.0 h1:F5zgRBnCtgGfdMB6jz01PFWIzbS8VjQfCu1H9OYt3BU=
77+
github.com/LumeraProtocol/lumera v1.7.0/go.mod h1:c1M+sjewuCvxw+pznwlspUzenDJI8Y+suKB3RFKS2Wo=
7878
github.com/LumeraProtocol/rq-go v0.2.1 h1:8B3UzRChLsGMmvZ+UVbJsJj6JZzL9P9iYxbdUwGsQI4=
7979
github.com/LumeraProtocol/rq-go v0.2.1/go.mod h1:APnKCZRh1Es2Vtrd2w4kCLgAyaL5Bqrkz/BURoRJ+O8=
8080
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=

0 commit comments

Comments
 (0)