Move clientId from constant.
This commit is contained in:
parent
7b1fc33d59
commit
28759422bc
|
@ -17,6 +17,7 @@ type
|
||||||
keyPair: KeyPair
|
keyPair: KeyPair
|
||||||
networkId: int
|
networkId: int
|
||||||
minPeers: int
|
minPeers: int
|
||||||
|
clientId: string
|
||||||
discovery: DiscoveryProtocol
|
discovery: DiscoveryProtocol
|
||||||
lastLookupTime: float
|
lastLookupTime: float
|
||||||
connectedNodes: Table[Node, Peer]
|
connectedNodes: Table[Node, Peer]
|
||||||
|
@ -33,14 +34,15 @@ const
|
||||||
connectLoopSleepMs = 2000
|
connectLoopSleepMs = 2000
|
||||||
|
|
||||||
proc newPeerPool*(chainDb: AsyncChainDb, networkId: int, keyPair: KeyPair,
|
proc newPeerPool*(chainDb: AsyncChainDb, networkId: int, keyPair: KeyPair,
|
||||||
discovery: DiscoveryProtocol, minPeers = 10): PeerPool =
|
discovery: DiscoveryProtocol, clientId: string,
|
||||||
|
listenPort = Port(30303), minPeers = 10): PeerPool =
|
||||||
result.new()
|
result.new()
|
||||||
result.keyPair = keyPair
|
result.keyPair = keyPair
|
||||||
result.minPeers = minPeers
|
result.minPeers = minPeers
|
||||||
result.networkId = networkId
|
result.networkId = networkId
|
||||||
result.discovery = discovery
|
result.discovery = discovery
|
||||||
result.connectedNodes = initTable[Node, Peer]()
|
result.connectedNodes = initTable[Node, Peer]()
|
||||||
result.listenPort = Port(30303)
|
result.listenPort = listenPort
|
||||||
|
|
||||||
template ensureFuture(f: untyped) = asyncCheck f
|
template ensureFuture(f: untyped) = asyncCheck f
|
||||||
|
|
||||||
|
@ -73,7 +75,7 @@ proc connect(p: PeerPool, remote: Node): Future[Peer] {.async.} =
|
||||||
debug "Skipping ", remote, "; already connected to it"
|
debug "Skipping ", remote, "; already connected to it"
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
result = await remote.rlpxConnect(p.keyPair, p.listenPort)
|
result = await remote.rlpxConnect(p.keyPair, p.listenPort, p.clientId)
|
||||||
|
|
||||||
# expected_exceptions = (
|
# expected_exceptions = (
|
||||||
# UnreachablePeer, TimeoutError, PeerConnectionLost, HandshakeFailure)
|
# UnreachablePeer, TimeoutError, PeerConnectionLost, HandshakeFailure)
|
||||||
|
|
|
@ -73,7 +73,6 @@ type
|
||||||
|
|
||||||
const
|
const
|
||||||
baseProtocolVersion = 4
|
baseProtocolVersion = 4
|
||||||
clienId = "Nimbus 0.1.0"
|
|
||||||
|
|
||||||
# TODO: Usage of this variables causes GCSAFE problems.
|
# TODO: Usage of this variables causes GCSAFE problems.
|
||||||
var
|
var
|
||||||
|
@ -536,8 +535,8 @@ proc initSecretState(hs: var Handshake, authMsg, ackMsg: openarray[byte],
|
||||||
initSecretState(secrets, p.secretsState)
|
initSecretState(secrets, p.secretsState)
|
||||||
burnMem(secrets)
|
burnMem(secrets)
|
||||||
|
|
||||||
proc rlpxConnect*(remote: Node, myKeys: KeyPair,
|
proc rlpxConnect*(remote: Node, myKeys: KeyPair, listenPort: Port,
|
||||||
listenPort: Port): Future[Peer] {.async.} =
|
clientId: string): Future[Peer] {.async.} =
|
||||||
new result
|
new result
|
||||||
result.remote = remote
|
result.remote = remote
|
||||||
let ta = initTAddress(remote.node.address.ip, remote.node.address.tcpPort)
|
let ta = initTAddress(remote.node.address.ip, remote.node.address.tcpPort)
|
||||||
|
@ -571,7 +570,7 @@ proc rlpxConnect*(remote: Node, myKeys: KeyPair,
|
||||||
# if handshake.remoteHPubkey != remote.node.pubKey:
|
# if handshake.remoteHPubkey != remote.node.pubKey:
|
||||||
# raise newException(Exception, "Remote pubkey is wrong")
|
# raise newException(Exception, "Remote pubkey is wrong")
|
||||||
|
|
||||||
discard result.hello(baseProtocolVersion, clienId, rlpxCapabilities,
|
discard result.hello(baseProtocolVersion, clientId, rlpxCapabilities,
|
||||||
uint(listenPort), myKeys.pubkey.getRaw())
|
uint(listenPort), myKeys.pubkey.getRaw())
|
||||||
|
|
||||||
var response = await result.nextMsg(p2p.hello, discardOthers = true)
|
var response = await result.nextMsg(p2p.hello, discardOthers = true)
|
||||||
|
@ -584,8 +583,8 @@ proc rlpxConnect*(remote: Node, myKeys: KeyPair,
|
||||||
if not isNil(result.transp):
|
if not isNil(result.transp):
|
||||||
result.transp.close()
|
result.transp.close()
|
||||||
|
|
||||||
proc rlpxAccept*(transp: StreamTransport,
|
proc rlpxAccept*(transp: StreamTransport, myKeys: KeyPair,
|
||||||
myKeys: KeyPair): Future[Peer] {.async.} =
|
clientId: string): Future[Peer] {.async.} =
|
||||||
new result
|
new result
|
||||||
result.transp = transp
|
result.transp = transp
|
||||||
var handshake = newHandshake({Responder})
|
var handshake = newHandshake({Responder})
|
||||||
|
@ -613,8 +612,9 @@ proc rlpxAccept*(transp: StreamTransport,
|
||||||
|
|
||||||
var response = await result.nextMsg(p2p.hello, discardOthers = true)
|
var response = await result.nextMsg(p2p.hello, discardOthers = true)
|
||||||
let listenPort = transp.localAddress().port
|
let listenPort = transp.localAddress().port
|
||||||
discard result.hello(baseProtocolVersion, clienId,
|
discard result.hello(baseProtocolVersion, clientId,
|
||||||
rlpxCapabilities, listenPort.uint, myKeys.pubkey.getRaw())
|
rlpxCapabilities, listenPort.uint,
|
||||||
|
myKeys.pubkey.getRaw())
|
||||||
|
|
||||||
if validatePubKeyInHello(response, handshake.remoteHPubkey):
|
if validatePubKeyInHello(response, handshake.remoteHPubkey):
|
||||||
warn "Remote nodeId is not its public key" # XXX: Do we care?
|
warn "Remote nodeId is not its public key" # XXX: Do we care?
|
||||||
|
@ -669,4 +669,3 @@ when isMainModule:
|
||||||
dispatchMsgPtr = dispatchMsg
|
dispatchMsgPtr = dispatchMsg
|
||||||
recvMsgPtr: GcSafeRecvMsg = recvMsg
|
recvMsgPtr: GcSafeRecvMsg = recvMsg
|
||||||
acceptPtr: GcSafeAccept = rlpxAccept
|
acceptPtr: GcSafeAccept = rlpxAccept
|
||||||
|
|
||||||
|
|
|
@ -18,13 +18,14 @@ type
|
||||||
keyPair: KeyPair
|
keyPair: KeyPair
|
||||||
address: Address
|
address: Address
|
||||||
networkId: int
|
networkId: int
|
||||||
|
clientId: string
|
||||||
discovery: DiscoveryProtocol
|
discovery: DiscoveryProtocol
|
||||||
peerPool: PeerPool
|
peerPool: PeerPool
|
||||||
|
|
||||||
proc processIncoming(server: StreamServer,
|
proc processIncoming(server: StreamServer,
|
||||||
remote: StreamTransport): Future[void] {.async, gcsafe.} =
|
remote: StreamTransport): Future[void] {.async, gcsafe.} =
|
||||||
var p2p = getUserData[P2PServer](server)
|
var p2p = getUserData[P2PServer](server)
|
||||||
let peerfut = remote.rlpxAccept(p2p.keyPair)
|
let peerfut = remote.rlpxAccept(p2p.keyPair, p2p.clientId)
|
||||||
yield peerfut
|
yield peerfut
|
||||||
if not peerfut.failed:
|
if not peerfut.failed:
|
||||||
let peer = peerfut.read()
|
let peer = peerfut.read()
|
||||||
|
@ -35,16 +36,18 @@ proc processIncoming(server: StreamServer,
|
||||||
remote.close()
|
remote.close()
|
||||||
|
|
||||||
proc newP2PServer*(keyPair: KeyPair, address: Address, chainDb: AsyncChainDB,
|
proc newP2PServer*(keyPair: KeyPair, address: Address, chainDb: AsyncChainDB,
|
||||||
bootstrapNodes: openarray[ENode],
|
bootstrapNodes: openarray[ENode], clientId: string,
|
||||||
networkId: int): P2PServer =
|
networkId: int): P2PServer =
|
||||||
result.new()
|
result.new()
|
||||||
result.chainDb = chainDb
|
result.chainDb = chainDb
|
||||||
result.keyPair = keyPair
|
result.keyPair = keyPair
|
||||||
result.address = address
|
result.address = address
|
||||||
|
result.clientId = clientId
|
||||||
result.networkId = networkId
|
result.networkId = networkId
|
||||||
result.discovery = newDiscoveryProtocol(keyPair.seckey, address,
|
result.discovery = newDiscoveryProtocol(keyPair.seckey, address,
|
||||||
bootstrapNodes)
|
bootstrapNodes)
|
||||||
result.peerPool = newPeerPool(chainDb, networkId, keyPair, result.discovery)
|
result.peerPool = newPeerPool(chainDb, networkId, keyPair, result.discovery,
|
||||||
|
clientId, address.tcpPort)
|
||||||
|
|
||||||
let ta = initTAddress(address.ip, address.tcpPort)
|
let ta = initTAddress(address.ip, address.tcpPort)
|
||||||
result.server = createStreamServer(ta, processIncoming, {ReuseAddr},
|
result.server = createStreamServer(ta, processIncoming, {ReuseAddr},
|
||||||
|
|
|
@ -11,6 +11,8 @@ import sequtils, logging
|
||||||
import eth_keys, asyncdispatch2, byteutils
|
import eth_keys, asyncdispatch2, byteutils
|
||||||
import eth_p2p/[discovery, kademlia, peer_pool, enode]
|
import eth_p2p/[discovery, kademlia, peer_pool, enode]
|
||||||
|
|
||||||
|
const clientId = "nim-eth-p2p/0.0.1"
|
||||||
|
|
||||||
addHandler(newConsoleLogger())
|
addHandler(newConsoleLogger())
|
||||||
|
|
||||||
proc startDiscoveryNode(privKey: PrivateKey, address: Address, bootnodes: seq[ENode]): Future[DiscoveryProtocol] {.async.} =
|
proc startDiscoveryNode(privKey: PrivateKey, address: Address, bootnodes: seq[ENode]): Future[DiscoveryProtocol] {.async.} =
|
||||||
|
@ -42,7 +44,9 @@ proc test() {.async.} =
|
||||||
var nodeAddrs = newSeqOfCap[Address](nodeKeys.len)
|
var nodeAddrs = newSeqOfCap[Address](nodeKeys.len)
|
||||||
for i in 0 ..< nodeKeys.len: nodeAddrs.add(localAddress(20302 + i))
|
for i in 0 ..< nodeKeys.len: nodeAddrs.add(localAddress(20302 + i))
|
||||||
|
|
||||||
var nodes = await all(zip(nodeKeys, nodeAddrs).mapIt(startDiscoveryNode(it.a, it.b, @[bootENode])))
|
var nodes = await all(zip(nodeKeys, nodeAddrs).mapIt(
|
||||||
|
startDiscoveryNode(it.a, it.b, @[bootENode]))
|
||||||
|
)
|
||||||
nodes.add(bootNode)
|
nodes.add(bootNode)
|
||||||
|
|
||||||
for i in nodes:
|
for i in nodes:
|
||||||
|
|
|
@ -11,6 +11,8 @@ import sequtils
|
||||||
import eth_keys, asyncdispatch2
|
import eth_keys, asyncdispatch2
|
||||||
import eth_p2p/[discovery, kademlia, peer_pool, enode, server, rlpx]
|
import eth_p2p/[discovery, kademlia, peer_pool, enode, server, rlpx]
|
||||||
|
|
||||||
|
const clientId = "nim-eth-p2p/0.0.1"
|
||||||
|
|
||||||
proc localAddress(port: int): Address =
|
proc localAddress(port: int): Address =
|
||||||
let port = Port(port)
|
let port = Port(port)
|
||||||
result = Address(udpPort: port, tcpPort: port, ip: parseIpAddress("127.0.0.1"))
|
result = Address(udpPort: port, tcpPort: port, ip: parseIpAddress("127.0.0.1"))
|
||||||
|
@ -19,11 +21,11 @@ proc test() {.async.} =
|
||||||
let kp = newKeyPair()
|
let kp = newKeyPair()
|
||||||
let address = localAddress(20301)
|
let address = localAddress(20301)
|
||||||
|
|
||||||
let s = newP2PServer(kp, address, nil, [], 1)
|
let s = newP2PServer(kp, address, nil, [], clientId, 1)
|
||||||
s.start()
|
s.start()
|
||||||
|
|
||||||
let n = newNode(initENode(kp.pubKey, address))
|
let n = newNode(initENode(kp.pubKey, address))
|
||||||
let peer = await rlpxConnect(n, newKeyPair(), Port(1234))
|
let peer = await rlpxConnect(n, newKeyPair(), Port(1234), clientId)
|
||||||
|
|
||||||
doAssert(not peer.isNil)
|
doAssert(not peer.isNil)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue