@@ -11,7 +11,6 @@ import (
1111 "github.com/LumeraProtocol/supernode/pkg/errors"
1212
1313 "github.com/LumeraProtocol/supernode/pkg/log"
14- "github.com/LumeraProtocol/supernode/pkg/lumera"
1514 ltc "github.com/LumeraProtocol/supernode/pkg/net/credentials"
1615)
1716
@@ -77,7 +76,7 @@ func (s *DHT) setBootstrapNodesFromConfigVar(ctx context.Context, bootstrapNodes
7776 }
7877
7978 nodes = append (nodes , & Node {
80- ID : []byte (lumeraAddress .Identity ),
79+ ID : []byte (lumeraAddress .Identity ),
8180 IP : lumeraAddress .Host ,
8281 Port : lumeraAddress .Port ,
8382 })
@@ -88,6 +87,7 @@ func (s *DHT) setBootstrapNodesFromConfigVar(ctx context.Context, bootstrapNodes
8887 return nil
8988}
9089
90+ // ConfigureBootstrapNodes connects with lumera client & gets p2p boostrap ip & port
9191// ConfigureBootstrapNodes connects with lumera client & gets p2p boostrap ip & port
9292func (s * DHT ) ConfigureBootstrapNodes (ctx context.Context , bootstrapNodes string ) error {
9393 if bootstrapNodes != "" {
@@ -100,53 +100,77 @@ func (s *DHT) ConfigureBootstrapNodes(ctx context.Context, bootstrapNodes string
100100 }
101101 selfAddress = fmt .Sprintf ("%s:%d" , selfAddress , s .options .Port )
102102
103- get := func (ctx context.Context , f func (context.Context ) (lumera.SuperNodeAddressInfos , error )) ([]* Node , error ) {
104- mns , err := f (ctx )
103+ var boostrapNodes []* Node
104+
105+ if s .options .LumeraClient != nil {
106+ // Get the latest block to determine height
107+ latestBlockResp , err := s .options .LumeraClient .Node ().GetLatestBlock (ctx )
108+ if err != nil {
109+ return fmt .Errorf ("failed to get latest block: %w" , err )
110+ }
111+
112+ // Get the block height
113+ blockHeight := uint64 (latestBlockResp .SdkBlock .Header .Height )
114+
115+ // Get top supernodes for this block
116+ supernodeResp , err := s .options .LumeraClient .SuperNode ().GetTopSuperNodesForBlock (ctx , blockHeight )
105117 if err != nil {
106- return [] * Node {} , err
118+ return fmt . Errorf ( "failed to get top supernodes: %w" , err )
107119 }
108120
109121 mapNodes := map [string ]* Node {}
110- for _ , mn := range mns {
111- node , err := s .parseNode (mn .ExtP2P , selfAddress )
122+
123+ for _ , supernode := range supernodeResp .Supernodes {
124+ // Find the latest IP address (with highest block height)
125+ var latestIP string
126+ var maxHeight int64 = - 1
127+
128+ for _ , ipHistory := range supernode .PrevIpAddresses {
129+ if ipHistory .Height > maxHeight {
130+ maxHeight = ipHistory .Height
131+ latestIP = ipHistory .Address
132+ }
133+ }
134+
135+ if latestIP == "" {
136+ log .P2P ().WithContext (ctx ).
137+ WithField ("supernode" , supernode .SupernodeAccount ).
138+ Warn ("No valid IP address found for supernode" )
139+ continue
140+ }
141+
142+ // Parse the node from the IP address
143+ node , err := s .parseNode (latestIP , selfAddress )
112144 if err != nil {
113- log .P2P ().WithContext (ctx ).WithError (err ).WithField ("extP2P" , mn .ExtP2P ).Warn ("Skip Bad Boostrap Address" )
145+ log .P2P ().WithContext (ctx ).WithError (err ).
146+ WithField ("address" , latestIP ).
147+ WithField ("supernode" , supernode .SupernodeAccount ).
148+ Warn ("Skip Bad Bootstrap Address" )
114149 continue
115150 }
116151
117- mapNodes [mn .ExtP2P ] = node
152+ // Store the supernode account as the node ID
153+ node .ID = []byte (supernode .SupernodeAccount )
154+ mapNodes [latestIP ] = node
118155 }
119156
120- nodes := [] * Node {}
157+ // Convert the map to a slice
121158 for _ , node := range mapNodes {
122- nodes = append (nodes , node )
159+ boostrapNodes = append (boostrapNodes , node )
123160 }
124-
125- return nodes , nil
126161 }
127162
128- var boostrapNodes []* Node
129- if s .options .LumeraNetwork != nil {
130- boostrapNodes , err := get (ctx , s .options .LumeraNetwork .MasterNodesExtra )
131- if err != nil {
132- return fmt .Errorf ("masternodesTop failed: %s" , err )
133- } else if len (boostrapNodes ) == 0 {
134- boostrapNodes , err = get (ctx , s .options .LumeraNetwork .MasterNodesTop )
135- if err != nil {
136- return fmt .Errorf ("masternodesExtra failed: %s" , err )
137- } else if len (boostrapNodes ) == 0 {
138- log .P2P ().WithContext (ctx ).Error ("unable to fetch bootstrap ip. Missing extP2P" )
139-
140- return nil
141- }
142- }
163+ if len (boostrapNodes ) == 0 {
164+ log .P2P ().WithContext (ctx ).Error ("unable to fetch bootstrap IP addresses. No valid supernodes found." )
165+ return nil
143166 }
144167
145168 for _ , node := range boostrapNodes {
146169 log .P2P ().WithContext (ctx ).WithFields (log.Fields {
147170 "bootstap_ip" : node .IP ,
148171 "bootstrap_port" : node .Port ,
149- }).Info ("adding p2p bootstap node" )
172+ "node_id" : string (node .ID ),
173+ }).Info ("adding p2p bootstrap node" )
150174 }
151175
152176 s .options .BootstrapNodes = append (s .options .BootstrapNodes , boostrapNodes ... )
0 commit comments