compare peer string ids, rather than keys

This commit is contained in:
Dmitriy Ryajov 2019-10-04 15:20:19 -06:00
parent 3194e3ac5b
commit 018da65708
3 changed files with 50 additions and 30 deletions

View File

@ -3,23 +3,24 @@ when not(compileOption("threads")):
import tables, options, sequtils, algorithm, strformat, os, strutils
import chronos
import ../libp2p/switch,
../libp2p/multistream,
../libp2p/crypto/crypto,
../libp2p/protocols/identify,
../libp2p/connection,
../libp2p/transports/[transport, tcptransport],
../libp2p/multiaddress,
../libp2p/peerinfo,
../libp2p/peer,
../libp2p/protocols/protocol,
../libp2p/protocols/secure/secure,
../libp2p/protocols/secure/secio,
../libp2p/protocols/pubsub/pubsub,
../libp2p/protocols/pubsub/floodsub,
../libp2p/muxers/muxer,
../libp2p/muxers/mplex/mplex,
../libp2p/muxers/mplex/types
import ../libp2p/[switch,
multistream,
crypto/crypto,
protocols/identify,
connection,
transports/transport,
transports/tcptransport,
multiaddress,
peerinfo,
peer,
protocols/protocol,
protocols/secure/secure,
protocols/secure/secio,
protocols/pubsub/pubsub,
protocols/pubsub/floodsub,
muxers/muxer,
muxers/mplex/mplex,
muxers/mplex/types]
const ChatCodec = "/nim-libp2p/chat/1.0.0"
const DefaultAddr = "/ip4/127.0.0.1/tcp/55505"
@ -127,7 +128,7 @@ proc writeAndPrint(p: ChatProto) {.async, gcsafe.} =
if line.startsWith("/") and "ipfs" in line:
await p.dialPeer(line)
except:
echo &"unable to dial {line}"
echo &"unable to dial remote peer {line}"
# echo getCurrentExceptionMsg()
proc readWriteLoop(p: ChatProto) {.async, gcsafe.} =
@ -191,7 +192,11 @@ proc serveThread(customData: CustomData) {.async.} =
var muxers = [(MplexCodec, mplexProvider)].toTable()
var identify = newIdentify(peerInfo)
var secureManagers = [(SecioCodec, Secure(newSecio(seckey)))].toTable()
var switch = newSwitch(peerInfo, transports, identify, muxers, secureManagers = secureManagers)
var switch = newSwitch(peerInfo,
transports,
identify,
muxers,
secureManagers = secureManagers)
var chatProto = newChatProto(switch, transp)
switch.mount(chatProto)

View File

@ -70,6 +70,7 @@ proc decodeMsg*(buf: seq[byte]): IdentifyInfo =
result.pubKey = none(PublicKey)
var pubKey: PublicKey
if pb.getValue(1, pubKey) > 0:
trace "read public key from message", pubKey = pubKey
result.pubKey = some(pubKey)
result.addrs = newSeq[MultiAddress]()
@ -77,24 +78,31 @@ proc decodeMsg*(buf: seq[byte]): IdentifyInfo =
while pb.getBytes(2, address) > 0:
if len(address) != 0:
var copyaddr = address
result.addrs.add(MultiAddress.init(copyaddr))
var ma = MultiAddress.init(copyaddr)
result.addrs.add(ma)
trace "read address bytes from message", address = ma
address.setLen(0)
var proto = ""
while pb.getString(3, proto) > 0:
trace "read proto from message", proto = proto
result.protos.add(proto)
proto = ""
var observableAddr = newSeq[byte]()
if pb.getBytes(4, observableAddr) > 0: # attempt to read the observed addr
result.observedAddr = some(MultiAddress.init(observableAddr))
var ma = MultiAddress.init(observableAddr)
trace "read observedAddr from message", address = ma
result.observedAddr = some(ma)
var protoVersion = ""
if pb.getString(5, protoVersion) > 0:
trace "read protoVersion from message", protoVersion = protoVersion
result.protoVersion = some(protoVersion)
var agentVersion = ""
if pb.getString(6, agentVersion) > 0:
trace "read agentVersion from message", agentVersion = agentVersion
result.agentVersion = some(protoVersion)
proc newIdentify*(peerInfo: PeerInfo): Identify =
@ -122,14 +130,21 @@ proc identify*(p: Identify,
"Invalid or empty message received!")
result = decodeMsg(message)
trace "Identify for remote peer succeded"
if remotePeerInfo.peerId.isSome and
result.pubKey.isSome and
result.pubKey != remotePeerInfo.peerId.get().publicKey:
trace "identify: Peer's remote public key doesn't match"
raise newException(IdentityNoMatchError,
"Peer's remote public key doesn't match")
if remotePeerInfo.peerId.isSome and result.pubKey.isSome:
let peer = PeerID.init(result.pubKey.get())
# do a string comaprison of the ids,
# because that is the only thing we have in most cases
if peer.pretty() != remotePeerInfo.peerId.get().pretty():
trace "Peer ids don't match",
remote = peer.pretty(),
local = remotePeerInfo.peerId.get().pretty()
raise newException(IdentityNoMatchError,
"Peer ids don't match")
trace "Identify for remote peer succeded"
proc push*(p: Identify, conn: Connection) {.async.} =
await conn.write(IdentifyPushCodec)

View File

@ -67,7 +67,7 @@ method listen*(t: TcpTransport,
# always get the resolved address in case we're bound to 0.0.0.0:0
t.ma = t.server.sock.getLocalAddress().toMultiAddr()
result = t.server.join()
trace "started node on", address = t.ma
debug "started node on", address = t.ma
method dial*(t: TcpTransport,
address: MultiAddress):