mirror of https://github.com/status-im/nim-eth.git
[discv5] Allow specifying the advertised IP address; Code style improvements
This commit is contained in:
parent
5aebab3f41
commit
1369cf3a30
|
@ -11,20 +11,20 @@ type
|
|||
proc toNodeId*(pk: PublicKey): NodeId =
|
||||
readUintBE[256](keccak256.digest(pk.getRaw()).data)
|
||||
|
||||
proc newNode*(pk: PublicKey, address: Address): Node =
|
||||
result.new()
|
||||
result.node = initENode(pk, address)
|
||||
result.id = pk.toNodeId()
|
||||
proc newNode*(enode: ENode): Node =
|
||||
Node(node: enode,
|
||||
id: enode.pubkey.toNodeId())
|
||||
|
||||
proc newNode*(enode: ENode, r: Record): Node =
|
||||
Node(node: enode,
|
||||
id: enode.pubkey.toNodeId(),
|
||||
record: r)
|
||||
|
||||
proc newNode*(uriString: string): Node =
|
||||
result.new()
|
||||
result.node = initENode(uriString)
|
||||
result.id = result.node.pubkey.toNodeId()
|
||||
newNode initENode(uriString)
|
||||
|
||||
proc newNode*(enode: ENode): Node =
|
||||
result.new()
|
||||
result.node = enode
|
||||
result.id = result.node.pubkey.toNodeId()
|
||||
proc newNode*(pk: PublicKey, address: Address): Node =
|
||||
newNode initENode(pk, address)
|
||||
|
||||
proc newNode*(r: Record): Node =
|
||||
# TODO: Handle IPv6
|
||||
|
|
|
@ -36,22 +36,23 @@ proc whoareyouMagic(toNode: NodeId): array[32, byte] =
|
|||
for i, c in prefix: data[sizeof(toNode) + i] = byte(c)
|
||||
sha256.digest(data).data
|
||||
|
||||
proc newProtocol*(privKey: PrivateKey,
|
||||
db: Database,
|
||||
tcpPort, udpPort: Port): Protocol =
|
||||
result = Protocol(privateKey: privKey, db: db)
|
||||
let a = Address(ip: parseIpAddress("127.0.0.1"),
|
||||
tcpPort: tcpPort, udpPort: udpPort)
|
||||
proc newProtocol*(privKey: PrivateKey, db: Database,
|
||||
ip: IpAddress, tcpPort, udpPort: Port): Protocol =
|
||||
let
|
||||
a = Address(ip: ip, tcpPort: tcpPort, udpPort: udpPort)
|
||||
enode = initENode(privKey.getPublicKey(), a)
|
||||
enrRec = enr.Record.init(12, privKey, a)
|
||||
node = newNode(enode, enrRec)
|
||||
|
||||
result.localNode = newNode(initENode(result.privateKey.getPublicKey(), a))
|
||||
result.localNode.record = enr.Record.init(12, result.privateKey, a)
|
||||
result = Protocol(
|
||||
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.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)
|
||||
result.routingTable.init(node)
|
||||
|
||||
proc start*(p: Protocol) =
|
||||
discard
|
||||
|
@ -376,7 +377,7 @@ when isMainModule:
|
|||
pk = newPrivateKey()
|
||||
|
||||
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()
|
||||
result.add(d)
|
||||
|
||||
|
|
|
@ -8,7 +8,9 @@ import
|
|||
proc startDiscoveryv5Node*(privKey: PrivateKey, address: Address,
|
||||
bootnodes: seq[Record]): discv5_protocol.Protocol =
|
||||
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:
|
||||
result.addNode(node)
|
||||
|
|
Loading…
Reference in New Issue