Re enable using custom distance function (#854)

This commit is contained in:
KonradStaniec 2021-10-05 21:16:33 +02:00 committed by GitHub
parent b58920f29f
commit c62905db25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 5 deletions

View File

@ -4,7 +4,8 @@ import
eth/p2p/discoveryv5/[protocol, node, enr], eth/p2p/discoveryv5/[protocol, node, enr],
../../content_db, ../../content_db,
../wire/portal_protocol, ../wire/portal_protocol,
./state_content ./state_content,
./custom_distance
const const
StateProtocolId* = "portal:state".toBytes() StateProtocolId* = "portal:state".toBytes()
@ -43,7 +44,7 @@ proc new*(T: type StateNetwork, baseProtocol: protocol.Protocol,
bootstrapRecords: openarray[Record] = []): T = bootstrapRecords: openarray[Record] = []): T =
let portalProtocol = PortalProtocol.new( let portalProtocol = PortalProtocol.new(
baseProtocol, StateProtocolId, getHandler(contentDB), dataRadius, baseProtocol, StateProtocolId, getHandler(contentDB), dataRadius,
bootstrapRecords) bootstrapRecords, customDistanceCalculator)
return StateNetwork(portalProtocol: portalProtocol, contentDB: contentDB) return StateNetwork(portalProtocol: portalProtocol, contentDB: contentDB)

View File

@ -194,12 +194,14 @@ proc new*(T: type PortalProtocol,
protocolId: seq[byte], protocolId: seq[byte],
contentHandler: ContentHandler, contentHandler: ContentHandler,
dataRadius = UInt256.high(), dataRadius = UInt256.high(),
bootstrapRecords: openarray[Record] = []): T = bootstrapRecords: openarray[Record] = [],
distanceCalculator: DistanceCalculator = XorDistanceCalculator
): T =
let proto = PortalProtocol( let proto = PortalProtocol(
protocolHandler: messageHandler, protocolHandler: messageHandler,
protocolId: protocolId, protocolId: protocolId,
routingTable: RoutingTable.init(baseProtocol.localNode, DefaultBitsPerHop, routingTable: RoutingTable.init(baseProtocol.localNode, DefaultBitsPerHop,
DefaultTableIpLimits, baseProtocol.rng), DefaultTableIpLimits, baseProtocol.rng, distanceCalculator),
baseProtocol: baseProtocol, baseProtocol: baseProtocol,
dataRadius: dataRadius, dataRadius: dataRadius,
handleContentRequest: contentHandler, handleContentRequest: contentHandler,

View File

@ -12,7 +12,7 @@ import
eth/p2p/discoveryv5/protocol as discv5_protocol, eth/p2p/discoveryv5/routing_table, eth/p2p/discoveryv5/protocol as discv5_protocol, eth/p2p/discoveryv5/routing_table,
../../nimbus/[genesis, chain_config, config, db/db_chain], ../../nimbus/[genesis, chain_config, config, db/db_chain],
../network/wire/portal_protocol, ../network/wire/portal_protocol,
../network/state/[state_content, state_network], ../network/state/[state_content, state_network, custom_distance],
../content_db, ../content_db,
./test_helpers ./test_helpers
@ -155,3 +155,32 @@ procSuite "State Content Network":
await node1.closeWait() await node1.closeWait()
await node2.closeWait() await node2.closeWait()
await node3.closeWait() await node3.closeWait()
asyncTest "Find other nodes in state network with correct custom distance":
let
node1 = initDiscoveryNode(
rng, PrivateKey.random(rng[]), localAddress(20302))
node2 = initDiscoveryNode(
rng, PrivateKey.random(rng[]), localAddress(20303))
proto1 = StateNetwork.new(node1, ContentDB.new("", inMemory = true))
proto2 = StateNetwork.new(node2, ContentDB.new("", inMemory = true))
check (await node1.ping(node2.localNode)).isOk()
check (await node2.ping(node1.localNode)).isOk()
proto2.portalProtocol.seedTable()
let distance = logDistance(node1.localNode.id, node2.localNode.id)
let nodes = await proto1.portalProtocol.findNode(proto2.portalProtocol.localNode,
List[uint16, 256](@[distance]))
check:
nodes.isOk()
nodes.get().total == 1'u8
nodes.get().enrs.len() == 1
await node1.closeWait()
await node2.closeWait()