Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions execution_chain/networking/discoveryv5.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,62 @@

{.push raises: [].}

include
eth/p2p/discoveryv5/protocol
import
std/[options],
chronos,
chronicles,
results,
metrics,
eth/common/keys,
eth/p2p/discoveryv5/[protocol, encoding, messages_encoding, enr, node, sessions]

proc receiveV5*(d: Protocol, a: Address, packet: openArray[byte]): Result[void, cstring] =
export
# Core types only
protocol.Protocol,
node.Node,
node.Address,
enr.Record

# Type aliases for cleaner API
type
DiscoveryV5* = protocol.Protocol
NodeV5* = node.Node
AddressV5* = node.Address

DiscResult*[T] = Result[T, cstring]

proc newDiscoveryV5*(
privKey: PrivateKey,
enrIp: Opt[IpAddress],
enrTcpPort: Opt[Port],
enrUdpPort: Opt[Port],
bootstrapRecords: openArray[enr.Record] = [],
bindPort: Port,
bindIp = IPv6_any(),
enrAutoUpdate = true,
rng = newRng(),
): DiscoveryV5 =
## Create a new Discovery v5 protocol instance
protocol.newProtocol(
privKey = privKey,
enrIp = enrIp,
enrTcpPort = enrTcpPort,
enrUdpPort = enrUdpPort,
bootstrapRecords = bootstrapRecords,
bindPort = bindPort,
bindIp = bindIp,
enrAutoUpdate = enrAutoUpdate,
rng = rng
)

proc receiveV5*(d: Protocol, a: Address, packet: openArray[byte]): DiscResult[void] =
discv5_network_bytes.inc(packet.len.int64, labelValues = [$Direction.In])

let packet = ?d.codec.decodePacket(a, packet)
let decoded = d.codec.decodePacket(a, packet)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jangko Something like this , correct? We can reuse DiscResult from discovery v4 but will need to have some common file for them, then the ones specific to their functionalities and variables.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally we want to share from discovery V5 to V4, not the other way around.

What I mean by porting receiveV5, the one in nim-eth should be fixed, so we don't have two copies of receive proc.
And discovery v5 designed to accept external shared stream transport, and don't have to open it itself.

Same with discovery V4.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jangko ,So how should I proceed in this repository for now? update implementations in nim-eth?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having that code duplication in the current solution of discv5 integration (with discv4) is definitely a bit ugly and not great for maintainability.

I've been wanting to add some other discv5 init calls to the API to be able to pass in an already created transport. This being mostly needed for Portal, but would be useful also for this double discv4/discv5 on same UDP port use case.

if decoded.isErr():
return err("discv5: Failed to decode packet")

let packet = decoded[]

case packet.flag
of OrdinaryMessage:
Expand Down
13 changes: 7 additions & 6 deletions execution_chain/networking/eth1_discovery.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ logScope:

type
DiscV4 = discoveryv4.DiscoveryV4
DiscV5 = discoveryv5.Protocol
DiscV5 = discoveryv5.DiscoveryV5

NodeV4 = discoveryv4.Node
NodeV5 = discoveryv5.Node
NodeV5 = discoveryv5.NodeV5

AddressV4 = discoveryv4.Address
AddressV5 = discoveryv5.Address
AddressV5 = discoveryv5.AddressV5

Eth1Discovery* = ref object
discv4: DiscV4
Expand Down Expand Up @@ -84,8 +84,9 @@ proc processClient(
if discv4.isErr:
# unhandled buf will be handled by discv5
let addrv5 = raddr.to(AddressV5)
proto.discv5.receiveV5(addrv5, buf).isOkOr:
debug "Discovery receive error", discv4=discv4.error, discv5=error
let discv5 = proto.discv5.receiveV5(addrv5, buf)
if discv5.isErr:
debug "Discovery receive error", discv4=discv4.error, discv5=discv5.error

#------------------------------------------------------------------------------
# Public functions
Expand All @@ -110,7 +111,7 @@ proc new*(
bindIp = bindIp,
rng = rng
),
discv5: discoveryv5.newProtocol(
discv5: discoveryv5.newDiscoveryV5(
privKey = privKey,
enrIp = Opt.some(address.ip),
enrTcpPort = Opt.some(address.tcpPort),
Expand Down
Loading