Usage

This section provides guidance on how to setup and interact with the Discv5 protocol in a .NET project.

Setting Up

The Discv5Protocol class exposes functions for interacting with the Node Discovery Protocol V5. Start by creating an instance of Discv5Protocol:

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);

The CreateDefault method of Discv5Builder is used to instantiate the Discv5 protocol with a list of bootstrap ENR nodes.

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:

Discv5Protocol discv5 = new Discv5Builder()
             .WithConnectionOptions(connectionOptions)
             .WithSessionOptions(sessionOptions)
             .WithTableOptions(tableOptions)
             .WithBootstrapEnrs(bootstrapEnrs)
             .WithEnrBuilder(enrBuilder)
             .WithEnrEntryRegistry(enrEntryRegistry)
             .WithLoggerFactory(loggerFactory)
             .Build();

How To Use

Once Discv5Protocol has been instantiated, the protocol is ready to be interacted by using the following functionalities:

Start

A method to start the discovery protocol.

await discv5.StartProtocolAsync()
Stop

A method that can be used to stop the discovery protocol when it's no longer needed.

await discv5.StopProtocolAsync();
Self Enr

Returns the Ethereum Node Record (ENR) for the local node.

Enr selfRecord = discv5.SelfEnr;
Nodes Count

Returns the total number of nodes that are stored in the routing table.

int totalNodes = discv5.NodesCount;
Peer Count

Returns the total number of nodes that have Live status in the routing table.

int activePeers = discv5.PeerCount;
Active Session Count

Returns the total number of active sessions maintained by the local node.

int activeSession = discv5.ActiveSessionCount;
Get Node From Id

Returns the corrresponding NodeTableEntry stored in the routing table based on the provided nodeId.

NodeTableEntry node = discv5.GetNodeFromId(nodeId);
Get All Nodes

Returns all the nodes stored in the routing table as an array of NodeTableEntry objects.

NodeTableEntry[] nodes = discv5.GetAllNodes();
Perform Lookup

To query the network for nodes closest to a given targetNodeId, you can use the PerformLookupAsync method. It will perform a recursive lookup to find nodes that are closest to the targetNodeId.

List<NodeTableEntry>? closestNodes = await discv5.PerformLookupAsync(targetNodeId);

This method will return list of NodeTableEntry objects, or null if no nodes were found or an error occurred.

Send PING message

A PING message can be sent to a connected node by supplying its Enr using the following method.

Task<bool> request = await discv5.SendPingAsync(destinationNode);

Returns a Task<bool> object, indicating if the request was successfully sent or not.

Send FINDNODE message

A FINDNODE request can be sent to a connected node to update the routing table with newer nodes.

Task<bool> request = discv5.SendFindNodeAsync(destinationNode, targetNodeId);

Returns a Task<bool> object, indicating if the request was successful or not.

Send TALKREQ message

To send an application-level request, this method can be used by providing the destination node's Enr, the supported protocol, and the request.

Task<bool> request = await discv5.SendTalkReqAsync(destinationEnrRecord, protocol, request);

Returns a Task<bool> object, indicating if the request was successful or not.

Last updated