fixes wrong logic

This commit is contained in:
andri lim 2018-11-06 11:30:47 +07:00 committed by zah
parent e8bd614021
commit c3a49d187d
4 changed files with 34 additions and 13 deletions

View File

@ -35,7 +35,7 @@ proc newEthereumNode*(keys: KeyPair,
chain: AbstractChainDB, chain: AbstractChainDB,
clientId = "nim-eth-p2p/0.2.0", # TODO: read this value from nimble somehow clientId = "nim-eth-p2p/0.2.0", # TODO: read this value from nimble somehow
addAllCapabilities = true, addAllCapabilities = true,
useSnappyCompression: bool = false): EthereumNode = useCompression: bool = false): EthereumNode =
new result new result
result.keys = keys result.keys = keys
result.networkId = networkId result.networkId = networkId
@ -46,7 +46,7 @@ proc newEthereumNode*(keys: KeyPair,
result.connectionState = ConnectionState.None result.connectionState = ConnectionState.None
when useSnappy: when useSnappy:
if useSnappyCompression: if useCompression:
result.protocolVersion = devp2pSnappyVersion result.protocolVersion = devp2pSnappyVersion
else: else:
result.protocolVersion = devp2pVersion result.protocolVersion = devp2pVersion

View File

@ -28,6 +28,8 @@ type
expectedMsgs: Deque[ExpectedMsg] expectedMsgs: Deque[ExpectedMsg]
receivedMsgsCount: int receivedMsgsCount: int
when useSnappy:
useCompression*: bool
var var
nextUnusedMockPort = 40304 nextUnusedMockPort = 40304
@ -152,6 +154,12 @@ macro expect*(mock: MockConf, MsgType: type, handler: untyped = nil): untyped =
newCall(bindSym"makeProtoMsgPair", MsgType.getType), newCall(bindSym"makeProtoMsgPair", MsgType.getType),
newCall(bindSym"toAction", handler)) newCall(bindSym"toAction", handler))
template compression(m: MockConf): bool =
when useSnappy:
m.useCompression
else:
false
proc newMockPeer*(userConfigurator: proc (m: MockConf)): EthereumNode = proc newMockPeer*(userConfigurator: proc (m: MockConf)): EthereumNode =
var mockConf = new MockConf var mockConf = new MockConf
mockConf.keys = newKeyPair() mockConf.keys = newKeyPair()
@ -169,7 +177,8 @@ proc newMockPeer*(userConfigurator: proc (m: MockConf)): EthereumNode =
mockConf.networkId, mockConf.networkId,
mockConf.chain, mockConf.chain,
mockConf.clientId, mockConf.clientId,
addAllCapabilities = false) addAllCapabilities = false,
mockConf.compression())
mockConf.handshakes.sort do (lhs, rhs: ExpectedMsg) -> int: mockConf.handshakes.sort do (lhs, rhs: ExpectedMsg) -> int:
# this is intentially sorted in reverse order, so we # this is intentially sorted in reverse order, so we

View File

@ -261,7 +261,9 @@ proc linkSendFailureToReqFuture[S, R](sendFut: Future[S], resFut: Future[R]) =
template compressMsg(peer: Peer, data: Bytes): Bytes = template compressMsg(peer: Peer, data: Bytes): Bytes =
when useSnappy: when useSnappy:
if peer.snappyEnabled: snappy.compress(data) if peer.snappyEnabled and
peer.network.protocolVersion == devp2pSnappyVersion:
snappy.compress(data)
else: data else: data
else: else:
data data
@ -422,7 +424,8 @@ proc recvMsg*(peer: Peer): Future[tuple[msgId: int, msgData: Rlp]] {.async.} =
decryptedBytes.setLen(decryptedBytesCount) decryptedBytes.setLen(decryptedBytesCount)
when useSnappy: when useSnappy:
if peer.network.protocolVersion == devp2pSnappyVersion: if peer.network.protocolVersion == devp2pSnappyVersion and
peer.snappyEnabled:
decryptedBytes = snappy.uncompress(decryptedBytes) decryptedBytes = snappy.uncompress(decryptedBytes)
if decryptedBytes.len == 0: if decryptedBytes.len == 0:
await peer.disconnectAndRaise(BreachOfProtocol, await peer.disconnectAndRaise(BreachOfProtocol,
@ -1189,7 +1192,14 @@ template baseProtocolVersion(node: EthereumNode): untyped =
else: else:
devp2pVersion devp2pVersion
template checkPeerCap(peer: Peer, handshake: HandShake) = template baseProtocolVersion(node: EthereumNode, peer: Peer): untyped =
when useSnappy:
if peer.snappyEnabled: node.protocolVersion
else: devp2pVersion
else:
devp2pVersion
template checkPeerProtocolVersion(peer: Peer, handshake: HandShake) =
when useSnappy: when useSnappy:
peer.snappyEnabled = handshake.version >= devp2pSnappyVersion.uint peer.snappyEnabled = handshake.version >= devp2pSnappyVersion.uint
@ -1201,7 +1211,7 @@ template getVersion(handshake: HandShake): uint =
template baseProtocolVersion(peer: Peer): uint = template baseProtocolVersion(peer: Peer): uint =
when useSnappy: when useSnappy:
if result.snappyEnabled: devp2pSnappyVersion if peer.snappyEnabled: devp2pSnappyVersion
else: devp2pVersion else: devp2pVersion
else: else:
devp2pVersion devp2pVersion
@ -1215,7 +1225,6 @@ proc rlpxConnect*(node: EthereumNode, remote: Node): Future[Peer] {.async.} =
var ok = false var ok = false
try: try:
result.transport = await connect(ta) result.transport = await connect(ta)
var handshake = newHandshake({Initiator, EIP8}, int(node.baseProtocolVersion())) var handshake = newHandshake({Initiator, EIP8}, int(node.baseProtocolVersion()))
handshake.host = node.keys handshake.host = node.keys
@ -1238,7 +1247,7 @@ proc rlpxConnect*(node: EthereumNode, remote: Node): Future[Peer] {.async.} =
ret = handshake.decodeAckMessage(ackMsg) ret = handshake.decodeAckMessage(ackMsg)
check ret check ret
result.checkPeerCap(handshake) result.checkPeerProtocolVersion(handshake)
initSecretState(handshake, ^authMsg, ackMsg, result) initSecretState(handshake, ^authMsg, ackMsg, result)
# if handshake.remoteHPubkey != remote.node.pubKey: # if handshake.remoteHPubkey != remote.node.pubKey:
@ -1300,8 +1309,8 @@ proc rlpxAccept*(node: EthereumNode,
ret = handshake.decodeAuthMessage(authMsg) ret = handshake.decodeAuthMessage(authMsg)
check ret check ret
result.checkPeerCap(handshake) result.checkPeerProtocolVersion(handshake)
handshake.version = uint8(node.baseProtocolVersion()) handshake.version = uint8(node.baseProtocolVersion(result))
var ackMsg: array[AckMessageMaxEIP8, byte] var ackMsg: array[AckMessageMaxEIP8, byte]
var ackMsgLen: int var ackMsgLen: int

View File

@ -90,10 +90,10 @@ template asyncTest(name, body: untyped) =
waitFor scenario() waitFor scenario()
asyncTest "network with 3 peers using custom protocols": asyncTest "network with 3 peers using custom protocols":
const usecompression = defined(useSnappy) const useCompression = defined(useSnappy)
let localKeys = newKeyPair() let localKeys = newKeyPair()
let localAddress = localAddress(30303) let localAddress = localAddress(30303)
var localNode = newEthereumNode(localKeys, localAddress, 1, nil, useSnappyCompression = useCompression) var localNode = newEthereumNode(localKeys, localAddress, 1, nil, useCompression = useCompression)
localNode.initProtocolStates() localNode.initProtocolStates()
localNode.startListening() localNode.startListening()
@ -119,6 +119,9 @@ asyncTest "network with 3 peers using custom protocols":
echo "got xyz req" echo "got xyz req"
await peer.xyzRes("mock peer data") await peer.xyzRes("mock peer data")
when useCompression:
m.useCompression = useCompression
discard await mock1.rlpxConnect(localNode) discard await mock1.rlpxConnect(localNode)
let mock2Connection = await localNode.rlpxConnect(mock2) let mock2Connection = await localNode.rlpxConnect(mock2)