mirror of
https://github.com/status-im/nim-eth-p2p.git
synced 2025-02-19 18:18:23 +00:00
Rlpx fixes
This commit is contained in:
parent
42ad1e7fdd
commit
d0b1ef6a7c
@ -76,8 +76,8 @@ const
|
|||||||
clienId = "Nimbus 0.1.0"
|
clienId = "Nimbus 0.1.0"
|
||||||
|
|
||||||
var
|
var
|
||||||
gProtocols = newSeq[ProtocolInfo](0)
|
gProtocols: seq[ProtocolInfo]
|
||||||
gCapabilities = newSeq[Capability](0)
|
gCapabilities: seq[Capability]
|
||||||
gDispatchers = initSet[Dispatcher]()
|
gDispatchers = initSet[Dispatcher]()
|
||||||
devp2p: ProtocolInfo
|
devp2p: ProtocolInfo
|
||||||
|
|
||||||
@ -113,7 +113,7 @@ proc getDispatcher(otherPeerCapabilities: openarray[Capability]): Dispatcher =
|
|||||||
new(result)
|
new(result)
|
||||||
newSeq(result.protocolOffsets, gProtocols.len)
|
newSeq(result.protocolOffsets, gProtocols.len)
|
||||||
|
|
||||||
var nextUserMsgId = 0x10 + 1
|
var nextUserMsgId = 0x10
|
||||||
|
|
||||||
for i in 0 .. <gProtocols.len:
|
for i in 0 .. <gProtocols.len:
|
||||||
let localProtocol = gProtocols[i]
|
let localProtocol = gProtocols[i]
|
||||||
@ -173,6 +173,8 @@ proc registerMsg(protocol: var ProtocolInfo,
|
|||||||
proc registerProtocol(protocol: ProtocolInfo) =
|
proc registerProtocol(protocol: ProtocolInfo) =
|
||||||
# XXX: This can be done at compile-time in the future
|
# XXX: This can be done at compile-time in the future
|
||||||
if protocol.version > 0:
|
if protocol.version > 0:
|
||||||
|
if gProtocols.isNil: gProtocols = @[]
|
||||||
|
if gCapabilities.isNil: gCapabilities = @[]
|
||||||
let pos = lowerBound(gProtocols, protocol)
|
let pos = lowerBound(gProtocols, protocol)
|
||||||
gProtocols.insert(protocol, pos)
|
gProtocols.insert(protocol, pos)
|
||||||
gCapabilities.insert(Capability(name: protocol.name, version: protocol.version), pos)
|
gCapabilities.insert(Capability(name: protocol.name, version: protocol.version), pos)
|
||||||
@ -246,7 +248,7 @@ proc recvMsg*(peer: Peer): Future[tuple[msgId: int, msgData: Rlp]] {.async.} =
|
|||||||
let remainingBytes = encryptedLength(msgSize) - 32
|
let remainingBytes = encryptedLength(msgSize) - 32
|
||||||
# XXX: Migrate this to a thread-local seq
|
# XXX: Migrate this to a thread-local seq
|
||||||
var encryptedBytes = newSeq[byte](remainingBytes)
|
var encryptedBytes = newSeq[byte](remainingBytes)
|
||||||
await peer.socket.fullRecvInto(encryptedBytes.baseAddr, remainingBytes)
|
await peer.socket.fullRecvInto(encryptedBytes)
|
||||||
|
|
||||||
let decryptedMaxLength = decryptedLength(msgSize)
|
let decryptedMaxLength = decryptedLength(msgSize)
|
||||||
var
|
var
|
||||||
@ -273,9 +275,12 @@ proc nextMsg*(peer: Peer, MsgType: typedesc,
|
|||||||
|
|
||||||
while true:
|
while true:
|
||||||
var (nextMsgId, nextMsgData) = await peer.recvMsg()
|
var (nextMsgId, nextMsgData) = await peer.recvMsg()
|
||||||
|
# echo "got msg(", nextMsgId, "): ", nextMsgData.inspect
|
||||||
if nextMsgId == wantedId:
|
if nextMsgId == wantedId:
|
||||||
return nextMsgData.read(MsgType)
|
return nextMsgData.read(MsgType)
|
||||||
elif not discardOthers:
|
elif not discardOthers:
|
||||||
|
if nextMsgData.listLen != 0:
|
||||||
|
nextMsgData = nextMsgData.listElem(0)
|
||||||
peer.dispatchMsg(nextMsgId, nextMsgData)
|
peer.dispatchMsg(nextMsgId, nextMsgData)
|
||||||
|
|
||||||
iterator typedParams(n: NimNode, skip = 0): (NimNode, NimNode) =
|
iterator typedParams(n: NimNode, skip = 0): (NimNode, NimNode) =
|
||||||
@ -327,6 +332,7 @@ macro rlpxProtocol*(protoIdentifier: untyped,
|
|||||||
writeMsgId = bindSym "writeMsgId"
|
writeMsgId = bindSym "writeMsgId"
|
||||||
isSubprotocol = version > 0
|
isSubprotocol = version > 0
|
||||||
stateType: NimNode = nil
|
stateType: NimNode = nil
|
||||||
|
msgThunksAndRegistrations = newNimNode(nnkStmtList)
|
||||||
|
|
||||||
# By convention, all Ethereum protocol names must be abbreviated to 3 letters
|
# By convention, all Ethereum protocol names must be abbreviated to 3 letters
|
||||||
assert protoName.len == 3
|
assert protoName.len == 3
|
||||||
@ -415,7 +421,7 @@ macro rlpxProtocol*(protoIdentifier: untyped,
|
|||||||
|
|
||||||
nCopy.body.insert 0, localStateAccessor
|
nCopy.body.insert 0, localStateAccessor
|
||||||
|
|
||||||
result.add nCopy, thunk
|
msgThunksAndRegistrations.add(nCopy, thunk)
|
||||||
|
|
||||||
var
|
var
|
||||||
msgType = genSym(nskType, msgName & "Obj")
|
msgType = genSym(nskType, msgName & "Obj")
|
||||||
@ -467,7 +473,7 @@ macro rlpxProtocol*(protoIdentifier: untyped,
|
|||||||
return `send`(`peer`, `finish`(`rlpWriter`))
|
return `send`(`peer`, `finish`(`rlpWriter`))
|
||||||
|
|
||||||
result.add n
|
result.add n
|
||||||
result.add newCall(bindSym("registerMsg"),
|
msgThunksAndRegistrations.add newCall(bindSym("registerMsg"),
|
||||||
protocol,
|
protocol,
|
||||||
newIntLitNode(nextId),
|
newIntLitNode(nextId),
|
||||||
newStrLitNode($n.name),
|
newStrLitNode($n.name),
|
||||||
@ -477,6 +483,7 @@ macro rlpxProtocol*(protoIdentifier: untyped,
|
|||||||
else:
|
else:
|
||||||
error("illegal syntax in a RLPx protocol definition", n)
|
error("illegal syntax in a RLPx protocol definition", n)
|
||||||
|
|
||||||
|
result.add(msgThunksAndRegistrations)
|
||||||
result.add newCall(bindSym("registerProtocol"), protocol)
|
result.add newCall(bindSym("registerProtocol"), protocol)
|
||||||
when isMainModule: echo repr(result)
|
when isMainModule: echo repr(result)
|
||||||
|
|
||||||
@ -508,7 +515,8 @@ rlpxProtocol p2p, 0:
|
|||||||
|
|
||||||
proc disconnect(peer: Peer, reason: DisconnectionReason)
|
proc disconnect(peer: Peer, reason: DisconnectionReason)
|
||||||
|
|
||||||
proc ping(peer: Peer)
|
proc ping(peer: Peer) =
|
||||||
|
discard peer.pong()
|
||||||
|
|
||||||
proc pong(peer: Peer) =
|
proc pong(peer: Peer) =
|
||||||
discard
|
discard
|
||||||
|
Loading…
x
Reference in New Issue
Block a user