[discv5] Allow specifying the advertised IP address; Code style improvements

This commit is contained in:
Zahary Karadjov 2020-02-22 01:55:37 +02:00 committed by zah
parent 5aebab3f41
commit 1369cf3a30
3 changed files with 30 additions and 27 deletions

View File

@ -11,20 +11,20 @@ type
proc toNodeId*(pk: PublicKey): NodeId = proc toNodeId*(pk: PublicKey): NodeId =
readUintBE[256](keccak256.digest(pk.getRaw()).data) readUintBE[256](keccak256.digest(pk.getRaw()).data)
proc newNode*(pk: PublicKey, address: Address): Node = proc newNode*(enode: ENode): Node =
result.new() Node(node: enode,
result.node = initENode(pk, address) id: enode.pubkey.toNodeId())
result.id = pk.toNodeId()
proc newNode*(enode: ENode, r: Record): Node =
Node(node: enode,
id: enode.pubkey.toNodeId(),
record: r)
proc newNode*(uriString: string): Node = proc newNode*(uriString: string): Node =
result.new() newNode initENode(uriString)
result.node = initENode(uriString)
result.id = result.node.pubkey.toNodeId()
proc newNode*(enode: ENode): Node = proc newNode*(pk: PublicKey, address: Address): Node =
result.new() newNode initENode(pk, address)
result.node = enode
result.id = result.node.pubkey.toNodeId()
proc newNode*(r: Record): Node = proc newNode*(r: Record): Node =
# TODO: Handle IPv6 # TODO: Handle IPv6

View File

@ -36,22 +36,23 @@ proc whoareyouMagic(toNode: NodeId): array[32, byte] =
for i, c in prefix: data[sizeof(toNode) + i] = byte(c) for i, c in prefix: data[sizeof(toNode) + i] = byte(c)
sha256.digest(data).data sha256.digest(data).data
proc newProtocol*(privKey: PrivateKey, proc newProtocol*(privKey: PrivateKey, db: Database,
db: Database, ip: IpAddress, tcpPort, udpPort: Port): Protocol =
tcpPort, udpPort: Port): Protocol = let
result = Protocol(privateKey: privKey, db: db) a = Address(ip: ip, tcpPort: tcpPort, udpPort: udpPort)
let a = Address(ip: parseIpAddress("127.0.0.1"), enode = initENode(privKey.getPublicKey(), a)
tcpPort: tcpPort, udpPort: udpPort) enrRec = enr.Record.init(12, privKey, a)
node = newNode(enode, enrRec)
result.localNode = newNode(initENode(result.privateKey.getPublicKey(), a)) result = Protocol(
result.localNode.record = enr.Record.init(12, result.privateKey, a) privateKey: privKey,
db: db,
localNode: node,
whoareyouMagic: whoareyouMagic(node.id),
idHash: sha256.digest(node.id.toByteArrayBE).data,
codec: Codec(localNode: node, privKey: privKey, db: db))
result.whoareyouMagic = whoareyouMagic(result.localNode.id) result.routingTable.init(node)
result.idHash = sha256.digest(result.localNode.id.toByteArrayBE).data
result.routingTable.init(result.localNode)
result.codec = Codec(localNode: result.localNode, privKey: result.privateKey, db: result.db)
proc start*(p: Protocol) = proc start*(p: Protocol) =
discard discard
@ -376,7 +377,7 @@ when isMainModule:
pk = newPrivateKey() pk = newPrivateKey()
let d = newProtocol(pk, DiscoveryDB.init(newMemoryDB()), let d = newProtocol(pk, DiscoveryDB.init(newMemoryDB()),
Port(12001 + i), Port(12001 + i)) parseIpAddress("127.0.0.1"), Port(12001 + i), Port(12001 + i))
d.open() d.open()
result.add(d) result.add(d)

View File

@ -8,7 +8,9 @@ import
proc startDiscoveryv5Node*(privKey: PrivateKey, address: Address, proc startDiscoveryv5Node*(privKey: PrivateKey, address: Address,
bootnodes: seq[Record]): discv5_protocol.Protocol = bootnodes: seq[Record]): discv5_protocol.Protocol =
var db = DiscoveryDB.init(newMemoryDB()) var db = DiscoveryDB.init(newMemoryDB())
result = newProtocol(privKey, db, address.tcpPort, address.udpPort) result = newProtocol(privKey, db,
parseIpAddress("127.0.0.1"),
address.tcpPort, address.udpPort)
for node in bootnodes: for node in bootnodes:
result.addNode(node) result.addNode(node)