cover missing case in MultiAddress.init (#198)

* cover missing case in MultiAddress.init

* raise assert on marker in protocol

* unify inits for markers / non-markers

* fix string
This commit is contained in:
Jacek Sieka 2020-06-01 14:41:45 +02:00 committed by GitHub
parent 88dbeebf17
commit 7e31210455
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 19 deletions

View File

@ -692,7 +692,9 @@ proc validate*(ma: MultiAddress): bool =
discard
result = true
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: openarray[byte]): MaResult[MultiAddress] =
proc init*(
mtype: typedesc[MultiAddress], protocol: MultiCodec,
value: openarray[byte] = []): MaResult[MultiAddress] =
## Initialize MultiAddress object from protocol id ``protocol`` and array
## of bytes ``value``.
let proto = CodeAddresses.getOrDefault(protocol)
@ -702,38 +704,31 @@ proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: openarray
var res: MultiAddress
res.data = initVBuffer()
res.data.writeVarint(cast[uint64](proto.mcodec))
if proto.kind in {Fixed, Length, Path}:
case proto.kind
of Fixed, Length, Path:
if len(value) == 0:
err("multiaddress: Value must not be empty array")
else:
if proto.kind == Fixed:
res.data.writeArray(value)
else:
var data = newSeq[byte](len(value))
copyMem(addr data[0], unsafeAddr value[0], len(value))
res.data.writeSeq(data)
res.data.writeSeq(value)
res.data.finish()
ok(res)
of Marker:
if len(value) != 0:
err("multiaddress: Value must be empty for markers")
else:
res.data.finish()
ok(res)
of None:
raiseAssert "None checked above"
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: PeerID): MaResult[MultiAddress] {.inline.} =
## Initialize MultiAddress object from protocol id ``protocol`` and peer id
## ``value``.
init(mtype, protocol, value.data)
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec): MaResult[MultiAddress] =
## Initialize MultiAddress object from protocol id ``protocol``.
let proto = CodeAddresses.getOrDefault(protocol)
if proto.kind == None:
err("multiaddress: Protocol not found")
else:
var res: MultiAddress
res.data = initVBuffer()
if proto.kind != Marker:
raise newException(MultiAddressError, "Protocol missing value")
res.data.writeVarint(cast[uint64](proto.mcodec))
res.data.finish()
ok(res)
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: int): MaResult[MultiAddress] =
## Initialize MultiAddress object from protocol id ``protocol`` and integer
## ``value``. This procedure can be used to instantiate ``tcp``, ``udp``,