mirror of https://github.com/vacp2p/nim-libp2p.git
Change PeerID type from distinct type.
Fix MultiAddress compilation error. Fix chat example.
This commit is contained in:
parent
a96718cf9e
commit
d7a7f8102d
|
@ -1,6 +1,9 @@
|
||||||
import chronos, nimcrypto, strutils
|
import chronos, nimcrypto, strutils
|
||||||
import ../libp2p/daemon/daemonapi
|
import ../libp2p/daemon/daemonapi
|
||||||
|
|
||||||
|
when not(compileOption("threads")):
|
||||||
|
{.fatal: "Please, compile this program with the --threads:on option!".}
|
||||||
|
|
||||||
const
|
const
|
||||||
ConsoleAddress = "/tmp/console-chat.sock"
|
ConsoleAddress = "/tmp/console-chat.sock"
|
||||||
ServerAddress = "/tmp/remote-chat.sock"
|
ServerAddress = "/tmp/remote-chat.sock"
|
||||||
|
@ -94,7 +97,7 @@ proc serveThread(server: StreamServer,
|
||||||
if len(pending) > 0:
|
if len(pending) > 0:
|
||||||
var results = await all(pending)
|
var results = await all(pending)
|
||||||
except:
|
except:
|
||||||
break
|
echo getCurrentException().msg
|
||||||
|
|
||||||
proc main() {.async.} =
|
proc main() {.async.} =
|
||||||
var data = new CustomData
|
var data = new CustomData
|
||||||
|
|
|
@ -571,7 +571,7 @@ proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec,
|
||||||
if proto.kind == None:
|
if proto.kind == None:
|
||||||
raise newException(MultiAddressError, "Protocol not found")
|
raise newException(MultiAddressError, "Protocol not found")
|
||||||
result.data = initVBuffer()
|
result.data = initVBuffer()
|
||||||
result.data.writeVarint(cast[uint64](proto.code))
|
result.data.writeVarint(cast[uint64](proto.mcodec))
|
||||||
if proto.kind in {Fixed, Length, Path}:
|
if proto.kind in {Fixed, Length, Path}:
|
||||||
if len(value) == 0:
|
if len(value) == 0:
|
||||||
raise newException(MultiAddressError, "Value must not be empty array")
|
raise newException(MultiAddressError, "Value must not be empty array")
|
||||||
|
|
141
libp2p/peer.nim
141
libp2p/peer.nim
|
@ -17,35 +17,35 @@ const
|
||||||
maxInlineKeyLength* = 42
|
maxInlineKeyLength* = 42
|
||||||
|
|
||||||
type
|
type
|
||||||
PeerID* = distinct seq[byte]
|
PeerID* = object
|
||||||
|
data*: seq[byte]
|
||||||
|
|
||||||
PeerIDError* = object of Exception
|
PeerIDError* = object of Exception
|
||||||
|
|
||||||
proc pretty*(peerid: PeerID): string {.inline.} =
|
proc pretty*(pid: PeerID): string {.inline.} =
|
||||||
## Return base58 encoded ``peerid`` representation.
|
## Return base58 encoded ``pid`` representation.
|
||||||
Base58.encode(cast[seq[byte]](peerid))
|
result = Base58.encode(pid.data)
|
||||||
|
|
||||||
proc toBytes*(peerid: PeerID, data: var openarray[byte]): int =
|
proc toBytes*(pid: PeerID, data: var openarray[byte]): int =
|
||||||
## Store PeerID ``peerid`` to array of bytes ``data``.
|
## Store PeerID ``pid`` to array of bytes ``data``.
|
||||||
##
|
##
|
||||||
## Returns number of bytes needed to store ``peerid``.
|
## Returns number of bytes needed to store ``pid``.
|
||||||
var p = cast[seq[byte]](peerid)
|
result = len(pid.data)
|
||||||
result = len(p)
|
|
||||||
if len(data) >= result and result > 0:
|
if len(data) >= result and result > 0:
|
||||||
copyMem(addr data[0], addr p[0], result)
|
copyMem(addr data[0], unsafeAddr pid.data[0], result)
|
||||||
|
|
||||||
proc getBytes*(peerid: PeerID): seq[byte] {.inline.} =
|
proc getBytes*(pid: PeerID): seq[byte] {.inline.} =
|
||||||
## Return PeerID as array of bytes.
|
## Return PeerID ``pid`` as array of bytes.
|
||||||
var p = cast[seq[byte]](peerid)
|
result = pid.data
|
||||||
result = @p
|
|
||||||
|
|
||||||
proc hex*(peerid: PeerID): string {.inline.} =
|
proc hex*(pid: PeerID): string {.inline.} =
|
||||||
## Returns hexadecimal string representation of ``peerid``.
|
## Returns hexadecimal string representation of ``pid``.
|
||||||
var p = cast[seq[byte]](peerid)
|
if len(pid.data) > 0:
|
||||||
if len(p) > 0:
|
result = toHex(pid.data)
|
||||||
result = toHex(p)
|
|
||||||
|
|
||||||
proc len*(a: PeerID): int {.borrow.}
|
proc len*(pid: PeerID): int {.inline.} =
|
||||||
|
## Returns length of ``pid`` binary representation.
|
||||||
|
result = len(pid.data)
|
||||||
|
|
||||||
proc cmp*(a, b: PeerID): int =
|
proc cmp*(a, b: PeerID): int =
|
||||||
## Compares two peer ids ``a`` and ``b``.
|
## Compares two peer ids ``a`` and ``b``.
|
||||||
|
@ -54,15 +54,13 @@ proc cmp*(a, b: PeerID): int =
|
||||||
## | 0 iff a == b
|
## | 0 iff a == b
|
||||||
## | < 0 iff a < b
|
## | < 0 iff a < b
|
||||||
## | > 0 iff a > b
|
## | > 0 iff a > b
|
||||||
var ab = cast[seq[byte]](a)
|
|
||||||
var bb = cast[seq[byte]](b)
|
|
||||||
var i = 0
|
var i = 0
|
||||||
var m = min(len(ab), len(bb))
|
var m = min(len(a.data), len(b.data))
|
||||||
while i < m:
|
while i < m:
|
||||||
result = ord(ab[i]) - ord(bb[i])
|
result = ord(a.data[i]) - ord(b.data[i])
|
||||||
if result != 0: return
|
if result != 0: return
|
||||||
inc(i)
|
inc(i)
|
||||||
result = len(ab) - len(bb)
|
result = len(a.data) - len(b.data)
|
||||||
|
|
||||||
proc `<=`*(a, b: PeerID): bool {.inline.} =
|
proc `<=`*(a, b: PeerID): bool {.inline.} =
|
||||||
(cmp(a, b) <= 0)
|
(cmp(a, b) <= 0)
|
||||||
|
@ -79,57 +77,55 @@ proc `>`*(a, b: PeerID): bool {.inline.} =
|
||||||
proc `==`*(a, b: PeerID): bool {.inline.} =
|
proc `==`*(a, b: PeerID): bool {.inline.} =
|
||||||
(cmp(a, b) == 0)
|
(cmp(a, b) == 0)
|
||||||
|
|
||||||
proc hash*(peerid: PeerID): Hash {.inline.} =
|
proc hash*(pid: PeerID): Hash {.inline.} =
|
||||||
var p = cast[seq[byte]](peerid)
|
result = hash(pid.data)
|
||||||
result = hash(p)
|
|
||||||
|
|
||||||
proc validate*(peerid: PeerID): bool =
|
proc validate*(pid: PeerID): bool =
|
||||||
## Validate check if ``peerid`` is empty or not.
|
## Validate check if ``pid`` is empty or not.
|
||||||
var p = cast[seq[byte]](peerid)
|
if len(pid.data) > 0:
|
||||||
if len(p) > 0:
|
result = MultiHash.validate(pid.data)
|
||||||
result = MultiHash.validate(p)
|
|
||||||
|
|
||||||
proc hasPublicKey*(peerid: PeerID): bool =
|
proc hasPublicKey*(pid: PeerID): bool =
|
||||||
## Returns ``true`` if ``peerid`` is small enough to hold public key inside.
|
## Returns ``true`` if ``pid`` is small enough to hold public key inside.
|
||||||
|
if len(pid.data) > 0:
|
||||||
var mh: MultiHash
|
var mh: MultiHash
|
||||||
var p = cast[seq[byte]](peerid)
|
if MultiHash.decode(pid.data, mh) > 0:
|
||||||
if len(p) > 0:
|
|
||||||
if MultiHash.decode(p, mh) > 0:
|
|
||||||
if mh.mcodec == multiCodec("identity"):
|
if mh.mcodec == multiCodec("identity"):
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
proc extractPublicKey*(peerid: PeerID, pubkey: var PublicKey): bool =
|
proc extractPublicKey*(pid: PeerID, pubkey: var PublicKey): bool =
|
||||||
## Returns ``true`` if public key was successfully decoded and stored
|
## Returns ``true`` if public key was successfully decoded from PeerID
|
||||||
## in ``pubkey``.
|
## ``pid``and stored to ``pubkey``.
|
||||||
##
|
##
|
||||||
## Returns ``false`` otherwise
|
## Returns ``false`` otherwise.
|
||||||
var mh: MultiHash
|
var mh: MultiHash
|
||||||
var p = cast[seq[byte]](peerid)
|
if len(pid.data) > 0:
|
||||||
if len(p) > 0:
|
if MultiHash.decode(pid.data, mh) > 0:
|
||||||
if MultiHash.decode(p, mh) > 0:
|
|
||||||
if mh.mcodec == multiCodec("identity"):
|
if mh.mcodec == multiCodec("identity"):
|
||||||
let length = len(mh.data.buffer)
|
let length = len(mh.data.buffer)
|
||||||
result = pubkey.init(mh.data.buffer.toOpenArray(mh.dpos, length - 1))
|
result = pubkey.init(mh.data.buffer.toOpenArray(mh.dpos, length - 1))
|
||||||
|
|
||||||
proc `$`*(peerid: PeerID): string =
|
proc `$`*(pid: PeerID): string =
|
||||||
## Returns compact string representation of ``peerid``.
|
## Returns compact string representation of ``pid``.
|
||||||
var pid = peerid.pretty()
|
var spid = pid.pretty()
|
||||||
if len(pid) <= 10:
|
if len(spid) <= 10:
|
||||||
result = pid
|
result = spid
|
||||||
else:
|
else:
|
||||||
|
result = newStringOfCap(10)
|
||||||
for i in 0..<2:
|
for i in 0..<2:
|
||||||
result.add(pid[i])
|
result.add(spid[i])
|
||||||
result.add("*")
|
result.add("*")
|
||||||
for i in (len(pid) - 6)..(len(pid) - 1):
|
for i in (len(spid) - 6)..(len(spid) - 1):
|
||||||
result.add(pid[i])
|
result.add(spid[i])
|
||||||
|
|
||||||
proc init*(pid: var PeerID, data: openarray[byte]): bool =
|
proc init*(pid: var PeerID, data: openarray[byte]): bool =
|
||||||
## Initialize peer id from raw binary representation ``data``.
|
## Initialize peer id from raw binary representation ``data``.
|
||||||
##
|
##
|
||||||
## Returns ``true`` if peer was successfully initialiazed.
|
## Returns ``true`` if peer was successfully initialiazed.
|
||||||
var p = cast[PeerID](@data)
|
var p = PeerID(data: @data)
|
||||||
if p.validate():
|
if p.validate():
|
||||||
pid = p
|
pid = p
|
||||||
|
result = true
|
||||||
|
|
||||||
proc init*(pid: var PeerID, data: string): bool =
|
proc init*(pid: var PeerID, data: string): bool =
|
||||||
## Initialize peer id from base58 encoded string representation.
|
## Initialize peer id from base58 encoded string representation.
|
||||||
|
@ -139,9 +135,11 @@ proc init*(pid: var PeerID, data: string): bool =
|
||||||
var length = 0
|
var length = 0
|
||||||
if Base58.decode(data, p, length) == Base58Status.Success:
|
if Base58.decode(data, p, length) == Base58Status.Success:
|
||||||
p.setLen(length)
|
p.setLen(length)
|
||||||
var opid = cast[PeerID](p)
|
var opid: PeerID
|
||||||
|
shallowCopy(opid.data, p)
|
||||||
if opid.validate():
|
if opid.validate():
|
||||||
pid = opid
|
pid = opid
|
||||||
|
result = true
|
||||||
|
|
||||||
proc init*(t: typedesc[PeerID], data: openarray[byte]): PeerID {.inline.} =
|
proc init*(t: typedesc[PeerID], data: openarray[byte]): PeerID {.inline.} =
|
||||||
## Create new peer id from raw binary representation ``data``.
|
## Create new peer id from raw binary representation ``data``.
|
||||||
|
@ -162,37 +160,36 @@ proc init*(t: typedesc[PeerID], pubkey: PublicKey): PeerID =
|
||||||
mh = MultiHash.digest("identity", pubraw)
|
mh = MultiHash.digest("identity", pubraw)
|
||||||
else:
|
else:
|
||||||
mh = MultiHash.digest("sha2-256", pubraw)
|
mh = MultiHash.digest("sha2-256", pubraw)
|
||||||
result = cast[PeerID](mh.data.buffer)
|
result.data = mh.data.buffer
|
||||||
|
|
||||||
proc init*(t: typedesc[PeerID], seckey: PrivateKey): PeerID {.inline.} =
|
proc init*(t: typedesc[PeerID], seckey: PrivateKey): PeerID {.inline.} =
|
||||||
## Create new peer id from private key ``seckey``.
|
## Create new peer id from private key ``seckey``.
|
||||||
result = PeerID.init(seckey.getKey())
|
result = PeerID.init(seckey.getKey())
|
||||||
|
|
||||||
proc match*(peerid: PeerID, pubkey: PublicKey): bool {.inline.} =
|
proc match*(pid: PeerID, pubkey: PublicKey): bool {.inline.} =
|
||||||
## Returns ``true`` if ``peerid`` matches public key ``pubkey``.
|
## Returns ``true`` if ``pid`` matches public key ``pubkey``.
|
||||||
result = (peerid == PeerID.init(pubkey))
|
result = (pid == PeerID.init(pubkey))
|
||||||
|
|
||||||
proc match*(peerid: PeerID, seckey: PrivateKey): bool {.inline.} =
|
proc match*(pid: PeerID, seckey: PrivateKey): bool {.inline.} =
|
||||||
## Returns ``true`` if ``peerid`` matches private key ``seckey``.
|
## Returns ``true`` if ``pid`` matches private key ``seckey``.
|
||||||
result = (peerid == PeerID.init(seckey))
|
result = (pid == PeerID.init(seckey))
|
||||||
|
|
||||||
## Serialization/Deserialization helpers
|
## Serialization/Deserialization helpers
|
||||||
|
|
||||||
proc write*(vb: var VBuffer, peerid: PeerID) {.inline.} =
|
proc write*(vb: var VBuffer, pid: PeerID) {.inline.} =
|
||||||
## Write PeerID value ``peerid`` to buffer ``vb``.
|
## Write PeerID value ``peerid`` to buffer ``vb``.
|
||||||
var p = cast[seq[byte]](peerid)
|
vb.writeSeq(pid.data)
|
||||||
vb.writeSeq(p)
|
|
||||||
|
|
||||||
proc initProtoField*(index: int, peerid: PeerID): ProtoField =
|
proc initProtoField*(index: int, pid: PeerID): ProtoField =
|
||||||
## Initialize ProtoField with PeerID ``value``.
|
## Initialize ProtoField with PeerID ``value``.
|
||||||
var p = cast[seq[byte]](peerid)
|
result = initProtoField(index, pid.data)
|
||||||
result = initProtoField(index, p)
|
|
||||||
|
|
||||||
proc getValue*(data: var ProtoBuffer, field: int, value: var PeerID): int =
|
proc getValue*(data: var ProtoBuffer, field: int, value: var PeerID): int =
|
||||||
## Read ``PeerID`` from ProtoBuf's message and validate it.
|
## Read ``PeerID`` from ProtoBuf's message and validate it.
|
||||||
var buffer: seq[byte]
|
var pid: PeerID
|
||||||
result = getLengthValue(data, field, buffer)
|
result = getLengthValue(data, field, pid.data)
|
||||||
if result > 0:
|
if result > 0:
|
||||||
value = cast[PeerID](buffer)
|
if not pid.validate():
|
||||||
if not value.validate():
|
|
||||||
result = -1
|
result = -1
|
||||||
|
else:
|
||||||
|
value = pid
|
||||||
|
|
Loading…
Reference in New Issue