@@ -24,6 +24,7 @@ import (
2424 "github.com/ava-labs/avalanchego/graft/coreth/plugin/evm/message"
2525 "github.com/ava-labs/avalanchego/graft/coreth/sync/client/stats"
2626 "github.com/ava-labs/avalanchego/ids"
27+ "github.com/ava-labs/avalanchego/network/p2p"
2728 "github.com/ava-labs/avalanchego/version"
2829
2930 ethparams "github.com/ava-labs/libevm/params"
@@ -65,6 +66,13 @@ type Client interface {
6566
6667 // GetCode synchronously retrieves code associated with the given hashes
6768 GetCode (ctx context.Context , hashes []common.Hash ) ([][]byte , error )
69+
70+ // NewClient creates a [p2p.Client] on the underlying network with the given handlerID
71+ NewClient (handlerID uint64 ) * p2p.Client
72+
73+ // NodeIDs returns the list of node IDs used by this client for state sync
74+ // If the list is empty, any available node may be used
75+ NodeIDs () []ids.NodeID
6876}
6977
7078// parseResponseFn parses given response bytes in context of specified request
@@ -74,7 +82,7 @@ type Client interface {
7482type parseResponseFn func (codec codec.Manager , request message.Request , response []byte ) (interface {}, int , error )
7583
7684type client struct {
77- networkClient network.SyncedNetworkClient
85+ network network.Network
7886 codec codec.Manager
7987 stateSyncNodes []ids.NodeID
8088 stateSyncNodeIdx uint32
@@ -83,7 +91,7 @@ type client struct {
8391}
8492
8593type Config struct {
86- NetworkClient network.SyncedNetworkClient
94+ Network network.Network
8795 Codec codec.Manager
8896 Stats stats.ClientSyncerStats
8997 StateSyncNodeIDs []ids.NodeID
@@ -96,14 +104,18 @@ type EthBlockParser interface {
96104
97105func New (config * Config ) * client {
98106 return & client {
99- networkClient : config .NetworkClient ,
107+ network : config .Network ,
100108 codec : config .Codec ,
101109 stats : config .Stats ,
102110 stateSyncNodes : config .StateSyncNodeIDs ,
103111 blockParser : config .BlockParser ,
104112 }
105113}
106114
115+ func (c * client ) NodeIDs () []ids.NodeID {
116+ return c .stateSyncNodes
117+ }
118+
107119// GetLeafs synchronously retrieves leafs as per given [message.LeafsRequest]
108120// Retries when:
109121// - response bytes could not be unmarshalled to [message.LeafsResponse]
@@ -319,14 +331,14 @@ func (c *client) get(ctx context.Context, request message.Request, parseFn parse
319331 start = time .Now ()
320332 )
321333 if len (c .stateSyncNodes ) == 0 {
322- response , nodeID , err = c .networkClient .SendSyncedAppRequestAny (ctx , StateSyncVersion , requestBytes )
334+ response , nodeID , err = c .network .SendSyncedAppRequestAny (ctx , StateSyncVersion , requestBytes )
323335 } else {
324336 // get the next nodeID using the nodeIdx offset. If we're out of nodes, loop back to 0
325337 // we do this every attempt to ensure we get a different node each time if possible.
326338 nodeIdx := atomic .AddUint32 (& c .stateSyncNodeIdx , 1 )
327339 nodeID = c .stateSyncNodes [nodeIdx % uint32 (len (c .stateSyncNodes ))]
328340
329- response , err = c .networkClient .SendSyncedAppRequest (ctx , nodeID , requestBytes )
341+ response , err = c .network .SendSyncedAppRequest (ctx , nodeID , requestBytes )
330342 }
331343 metric .UpdateRequestLatency (time .Since (start ))
332344
@@ -338,25 +350,29 @@ func (c *client) get(ctx context.Context, request message.Request, parseFn parse
338350 ctx = append (ctx , "attempt" , attempt , "request" , request , "err" , err )
339351 log .Debug ("request failed, retrying" , ctx ... )
340352 metric .IncFailed ()
341- c .networkClient .TrackBandwidth (nodeID , 0 )
353+ c .network .TrackBandwidth (nodeID , 0 )
342354 time .Sleep (failedRequestSleepInterval )
343355 continue
344356 } else {
345357 responseIntf , numElements , err = parseFn (c .codec , request , response )
346358 if err != nil {
347359 lastErr = err
348360 log .Debug ("could not validate response, retrying" , "nodeID" , nodeID , "attempt" , attempt , "request" , request , "err" , err )
349- c .networkClient .TrackBandwidth (nodeID , 0 )
361+ c .network .TrackBandwidth (nodeID , 0 )
350362 metric .IncFailed ()
351363 metric .IncInvalidResponse ()
352364 continue
353365 }
354366
355367 bandwidth := float64 (len (response )) / (time .Since (start ).Seconds () + epsilon )
356- c .networkClient .TrackBandwidth (nodeID , bandwidth )
368+ c .network .TrackBandwidth (nodeID , bandwidth )
357369 metric .IncSucceeded ()
358370 metric .IncReceived (int64 (numElements ))
359371 return responseIntf , nil
360372 }
361373 }
362374}
375+
376+ func (c * client ) NewClient (handlerID uint64 ) * p2p.Client {
377+ return c .network .P2PNetwork ().NewClient (handlerID , c .network .P2PValidators ())
378+ }
0 commit comments