nim-eth/eth/p2p/discoveryv5/node.nim

68 lines
1.8 KiB
Nim
Raw Normal View History

import
std/hashes,
nimcrypto, stint, chronos, stew/shims/net,
eth/keys, enr
2019-12-16 19:38:45 +00:00
{.push raises: [Defect].}
2019-12-16 19:38:45 +00:00
type
NodeId* = UInt256
Address* = object
ip*: ValidIpAddress
port*: Port
2019-12-16 19:38:45 +00:00
Node* = ref object
id*: NodeId
pubkey*: PublicKey
address*: Option[Address]
2019-12-16 19:38:45 +00:00
record*: Record
seen*: bool ## Indicates if there was at least one successful
## request-response with this node.
2019-12-16 19:38:45 +00:00
proc toNodeId*(pk: PublicKey): NodeId =
## Convert public key to a node identifier.
readUintBE[256](keccak256.digest(pk.toRaw()).data)
2019-12-16 19:38:45 +00:00
proc newNode*(r: Record): Result[Node, cstring] =
## Create a new `Node` from a `Record`.
2019-12-16 19:38:45 +00:00
# TODO: Handle IPv6
let pk = r.get(PublicKey)
# This check is redundant for a properly created record as the deserialization
# of a record will fail at `verifySignature` if there is no public key.
if pk.isNone():
return err("Could not recover public key from ENR")
# Also this can not fail for a properly created record as id is checked upon
# deserialization.
let tr = ? r.toTypedRecord()
if tr.ip.isSome() and tr.udp.isSome():
let a = Address(ip: ipv4(tr.ip.get()), port: Port(tr.udp.get()))
ok(Node(id: pk.get().toNodeId(), pubkey: pk.get() , record: r,
address: some(a)))
else:
ok(Node(id: pk.get().toNodeId(), pubkey: pk.get(), record: r,
address: none(Address)))
2019-12-16 19:38:45 +00:00
proc hash*(n: Node): hashes.Hash = hash(n.pubkey.toRaw)
2020-05-01 20:34:26 +00:00
proc `==`*(a, b: Node): bool =
(a.isNil and b.isNil) or
(not a.isNil and not b.isNil and a.pubkey == b.pubkey)
2020-07-15 13:27:22 +00:00
proc `$`*(id: NodeId): string =
id.toHex()
proc `$`*(a: Address): string =
result.add($a.ip)
result.add(":" & $a.port)
2020-05-01 20:34:26 +00:00
proc `$`*(n: Node): string =
2019-12-16 19:38:45 +00:00
if n == nil:
"Node[uninitialized]"
elif n.address.isNone():
"Node[unaddressable]"
2019-12-16 19:38:45 +00:00
else:
"Node[" & $n.address.get().ip & ":" & $n.address.get().port & "]"