Add isWire() procedure for MultiAddress.
This commit is contained in:
parent
e9785bb509
commit
7e2d3e213f
|
@ -400,7 +400,7 @@ proc protoName*(ma: MultiAddress): string =
|
||||||
|
|
||||||
proc protoArgument*(ma: MultiAddress, value: var openarray[byte]): int =
|
proc protoArgument*(ma: MultiAddress, value: var openarray[byte]): int =
|
||||||
## Returns MultiAddress ``ma`` protocol argument value.
|
## Returns MultiAddress ``ma`` protocol argument value.
|
||||||
##
|
##
|
||||||
## If current MultiAddress do not have argument value, then result will be
|
## If current MultiAddress do not have argument value, then result will be
|
||||||
## ``0``.
|
## ``0``.
|
||||||
var header: uint64
|
var header: uint64
|
||||||
|
@ -672,3 +672,36 @@ proc `&=`*(m1: var MultiAddress, m2: MultiAddress) =
|
||||||
m1.data.buffer &= m2.data.buffer
|
m1.data.buffer &= m2.data.buffer
|
||||||
if not m1.validate():
|
if not m1.validate():
|
||||||
raise newException(MultiAddressError, "Incorrect MultiAddress!")
|
raise newException(MultiAddressError, "Incorrect MultiAddress!")
|
||||||
|
|
||||||
|
proc isWire*(ma: MultiAddress): bool =
|
||||||
|
## Returns ``true`` if MultiAddress ``ma`` is one of:
|
||||||
|
## - {IP4}/{TCP, UDP}
|
||||||
|
## - {IP6}/{TCP, UDP}
|
||||||
|
## - {UNIX}/{PATH}
|
||||||
|
var state = 0
|
||||||
|
try:
|
||||||
|
for part in ma.items():
|
||||||
|
if state == 0:
|
||||||
|
let code = part.protoCode()
|
||||||
|
if code == multiCodec("ip4") or code == multiCodec("ip6"):
|
||||||
|
inc(state)
|
||||||
|
continue
|
||||||
|
elif code == multiCodec("unix"):
|
||||||
|
result = true
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
result = false
|
||||||
|
break
|
||||||
|
elif state == 1:
|
||||||
|
if part.protoCode == multiCodec("tcp") or
|
||||||
|
part.protoCode == multiCodec("udp"):
|
||||||
|
inc(state)
|
||||||
|
result = true
|
||||||
|
else:
|
||||||
|
result = false
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
result = false
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
result = false
|
||||||
|
|
|
@ -170,6 +170,29 @@ const
|
||||||
"/p2p-circuit/50"
|
"/p2p-circuit/50"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
UtilitySuccessVectors = [
|
||||||
|
"/ip4/127.0.0.1/tcp/1024",
|
||||||
|
"/ip4/127.0.0.1/udp/1024",
|
||||||
|
"/ip4/0.0.0.0/tcp/1024",
|
||||||
|
"/ip4/0.0.0.0/udp/1024",
|
||||||
|
"/ip4/255.255.255.255/tcp/65535",
|
||||||
|
"/ip4/255.255.255.255/udp/65535",
|
||||||
|
"/ip6/::1/tcp/1024",
|
||||||
|
"/ip6/::1/udp/1024",
|
||||||
|
"/ip6/::/tcp/65535",
|
||||||
|
"/ip6/::/udp/65535",
|
||||||
|
"/ip6/::/udp/65535",
|
||||||
|
"/unix/tmp/test.socket"
|
||||||
|
]
|
||||||
|
|
||||||
|
UtilityFailVectors = [
|
||||||
|
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
|
||||||
|
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
|
||||||
|
"/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/8000/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
|
||||||
|
"/p2p-webrtc-star/ip4/127.0.0.1/tcp/9090/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
|
||||||
|
"/ip4/127.0.0.1/udp/1234/quic"
|
||||||
|
]
|
||||||
|
|
||||||
suite "MultiAddress test suite":
|
suite "MultiAddress test suite":
|
||||||
|
|
||||||
test "go-multiaddr success test vectors":
|
test "go-multiaddr success test vectors":
|
||||||
|
@ -216,3 +239,11 @@ suite "MultiAddress test suite":
|
||||||
check:
|
check:
|
||||||
$cma == "/ip4/127.0.0.1/udp/30000/p2p-circuit"
|
$cma == "/ip4/127.0.0.1/udp/30000/p2p-circuit"
|
||||||
$ma2 == "/ip4/127.0.0.1/udp/30000/p2p-circuit"
|
$ma2 == "/ip4/127.0.0.1/udp/30000/p2p-circuit"
|
||||||
|
|
||||||
|
test "isWire() test":
|
||||||
|
for item in UtilitySuccessVectors:
|
||||||
|
var a = MultiAddress.init(item)
|
||||||
|
check a.isWire() == true
|
||||||
|
for item in UtilityFailVectors:
|
||||||
|
var a = MultiAddress.init(item)
|
||||||
|
check a.isWire() == false
|
||||||
|
|
Loading…
Reference in New Issue