diff --git a/libp2p/routing_record.nim b/libp2p/routing_record.nim index 81475274c..de39891ef 100644 --- a/libp2p/routing_record.nim +++ b/libp2p/routing_record.nim @@ -51,10 +51,12 @@ proc decode*( for address in addressInfos: var addressInfo = AddressInfo() let subProto = initProtoBuffer(address) - if ? subProto.getField(1, addressInfo.address) == false: - return err(ProtoError.RequiredFieldMissing) + let f = subProto.getField(1, addressInfo.address) + if f.isOk() and f.get(): + record.addresses &= addressInfo - record.addresses &= addressInfo + if record.addresses.len == 0: + return err(ProtoError.RequiredFieldMissing) ok(record) diff --git a/tests/testrouting_record.nim b/tests/testrouting_record.nim index 65bb81e95..5d720ffd4 100644 --- a/tests/testrouting_record.nim +++ b/tests/testrouting_record.nim @@ -77,3 +77,41 @@ suite "Signed Routing Record": buffer = routingRecord.envelope.encode().tryGet() check SignedPeerRecord.decode(buffer).error == EnvelopeInvalidSignature + + test "Decode doesn't fail if some addresses are invalid": + let + rng = newRng() + privKey = PrivateKey.random(rng[]).tryGet() + peerId = PeerId.init(privKey).tryGet() + multiAddresses = @[MultiAddress(), MultiAddress.init("/ip4/0.0.0.0/tcp/25").tryGet()] + routingRecord = PeerRecord.init(peerId, multiAddresses, 42) + + buffer = routingRecord.encode() + parsedRR = PeerRecord.decode(buffer).tryGet() + + check parsedRR.addresses.len == 1 + + test "Decode doesn't fail if there are no addresses": + let + rng = newRng() + privKey = PrivateKey.random(rng[]).tryGet() + peerId = PeerId.init(privKey).tryGet() + multiAddresses = newSeq[MultiAddress]() + routingRecord = PeerRecord.init(peerId, multiAddresses, 42) + + buffer = routingRecord.encode() + parsedRR = PeerRecord.decode(buffer).tryGet() + + check parsedRR.addresses.len == 0 + + test "Decode fails if all addresses are invalid": + let + rng = newRng() + privKey = PrivateKey.random(rng[]).tryGet() + peerId = PeerId.init(privKey).tryGet() + multiAddresses = @[MultiAddress(), MultiAddress()] + routingRecord = PeerRecord.init(peerId, multiAddresses, 42) + + buffer = routingRecord.encode() + + check PeerRecord.decode(buffer).isErr