Copy using Lantern . Discv5 . WireProtocol ;
string [] bootstrapEnrs = new []
{
"enr:-Ku4QImhMc1z8yCiNJ1TyUxdcfNucje3BGwEHzodEZUan8PherEo4sF7pPHPSIB1NNuSg5fZy7qFsjmUKs2ea1Whi0EBh2F0dG5ldHOIAAAAAAAAAACEZXRoMpD1pf1CAAAAAP__________gmlkgnY0gmlwhBLf22SJc2VjcDI1NmsxoQOVphkDqal4QzPMksc5wnpuC3gvSC8AfbFOnZY_On34wIN1ZHCCIyg",
"enr:-KG4QOtcP9X1FbIMOe17QNMKqDxCpm14jcX5tiOE4_TyMrFqbmhPZHK_ZPG2Gxb1GE2xdtodOfx9-cgvNtxnRyHEmC0ghGV0aDKQ9aX9QgAAAAD__________4JpZIJ2NIJpcIQDE8KdiXNlY3AyNTZrMaEDhpehBDbZjM_L9ek699Y7vhUJ-eAdMyQW_Fil522Y0fODdGNwgiMog3VkcIIjKA"
};
Discv5Protocol discv5 = Discv5Builder . CreateDefault (bootstrapEnrs);
If more granular control is needed over the protocol's configuration, you can create an instance of Discv5Protocol
using the builder pattern provided by Discv5Builder
:
Copy Discv5Protocol discv5 = new Discv5Builder ()
. WithConnectionOptions (connectionOptions)
. WithSessionOptions (sessionOptions)
. WithTableOptions (tableOptions)
. WithBootstrapEnrs (bootstrapEnrs)
. WithEnrBuilder (enrBuilder)
. WithEnrEntryRegistry (enrEntryRegistry)
. WithLoggerFactory (loggerFactory)
. Build ();
StartA method to start the discovery protocol.
Copy await discv5 . StartProtocolAsync ()
StopA method that can be used to stop the discovery protocol when it's no longer needed.
Copy await discv5 . StopProtocolAsync ();
Self EnrReturns the Ethereum Node Record (ENR) for the local node.
Copy Enr selfRecord = discv5 . SelfEnr ;
Nodes CountReturns the total number of nodes that are stored in the routing table.
Copy int totalNodes = discv5 . NodesCount ;
Peer CountReturns the total number of nodes that have Live
status in the routing table.
Copy int activePeers = discv5 . PeerCount ;
Active Session CountReturns the total number of active sessions maintained by the local node.
Copy int activeSession = discv5 . ActiveSessionCount ;
Get Node From IdReturns the corrresponding NodeTableEntry
stored in the routing table based on the provided nodeId
.
Copy NodeTableEntry node = discv5 . GetNodeFromId (nodeId);
Get All NodesReturns all the nodes stored in the routing table as an array of NodeTableEntry
objects.
Copy NodeTableEntry [] nodes = discv5 . GetAllNodes ();
Send PING messageA PING
message can be sent to a connected node by supplying its Enr
using the following method.
Copy Task < bool > request = await discv5 . SendPingAsync (destinationNode);
Returns a Task<bool>
object, indicating if the request was successfully sent or not.
Send FINDNODE messageA FINDNODE
request can be sent to a connected node to update the routing table with newer nodes.
Copy Task < bool > request = discv5 . SendFindNodeAsync (destinationNode , targetNodeId);
Returns a Task<bool>
object, indicating if the request was successful or not.
Send TALKREQ messageTo send an application-level request, this method can be used by providing the destination node's Enr
, the supported protocol
, and the request
.
Copy Task < bool > request = await discv5 . SendTalkReqAsync (destinationEnrRecord , protocol , request);
Returns a Task<bool>
object, indicating if the request was successful or not.