Move inRange check to the Portal protocol + related simplifications (#2602)

This commit is contained in:
Kim De Mey 2024-09-09 17:52:11 +02:00 committed by GitHub
parent 71e466d173
commit 0869a27462
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 36 additions and 46 deletions

View File

@ -490,18 +490,11 @@ proc createGetHandler*(db: ContentDB): DbGetHandler =
ok(content)
)
proc createStoreHandler*(
db: ContentDB, cfg: RadiusConfig, p: PortalProtocol
): DbStoreHandler =
proc createStoreHandler*(db: ContentDB, cfg: RadiusConfig): DbStoreHandler =
return (
proc(
contentKey: ContentKeyByteList, contentId: ContentId, content: seq[byte]
) {.raises: [], gcsafe.} =
# always re-check that the key is in the node range to make sure only
# content in range is stored.
# TODO: current silent assumption is that both ContentDB and PortalProtocol
# are using the same xor distance function
if p.inRange(contentId):
case cfg.kind
of Dynamic:
# In case of dynamic radius, the radius gets adjusted based on the

View File

@ -201,14 +201,13 @@ proc new*(
getProtocolId(portalNetwork, PortalSubnetwork.beacon),
toContentIdHandler,
createGetHandler(beaconDb),
createStoreHandler(beaconDb),
createRadiusHandler(beaconDb),
stream,
bootstrapRecords,
config = portalConfig,
)
portalProtocol.dbPut = createStoreHandler(beaconDb)
BeaconNetwork(
portalProtocol: portalProtocol,
beaconDb: beaconDb,

View File

@ -692,15 +692,13 @@ proc new*(
getProtocolId(portalNetwork, PortalSubnetwork.history),
toContentIdHandler,
createGetHandler(contentDB),
createStoreHandler(contentDB, portalConfig.radiusConfig),
createRadiusHandler(contentDB),
stream,
bootstrapRecords,
config = portalConfig,
)
portalProtocol.dbPut =
createStoreHandler(contentDB, portalConfig.radiusConfig, portalProtocol)
HistoryNetwork(
portalProtocol: portalProtocol,
contentDB: contentDB,

View File

@ -57,15 +57,13 @@ proc new*(
getProtocolId(portalNetwork, PortalSubnetwork.state),
toContentIdHandler,
createGetHandler(contentDB),
createStoreHandler(contentDB, portalConfig.radiusConfig),
createRadiusHandler(contentDB),
s,
bootstrapRecords,
config = portalConfig,
)
portalProtocol.dbPut =
createStoreHandler(contentDB, portalConfig.radiusConfig, portalProtocol)
return StateNetwork(
portalProtocol: portalProtocol,
contentDB: contentDB,

View File

@ -561,6 +561,7 @@ proc new*(
protocolId: PortalProtocolId,
toContentId: ToContentIdHandler,
dbGet: DbGetHandler,
dbPut: DbStoreHandler,
dbRadius: DbRadiusHandler,
stream: PortalStream,
bootstrapRecords: openArray[Record] = [],
@ -577,6 +578,7 @@ proc new*(
baseProtocol: baseProtocol,
toContentId: toContentId,
dbGet: dbGet,
dbPut: dbPut,
dataRadius: dbRadius,
bootstrapRecords: @bootstrapRecords,
stream: stream,
@ -1595,6 +1597,9 @@ proc storeContent*(
contentId: ContentId,
content: seq[byte],
) =
# Always re-check that the key is still in the node range to make sure only
# content in range is stored.
if p.inRange(contentId):
doAssert(p.dbPut != nil)
p.dbPut(contentKey, contentId, content)

View File

@ -49,13 +49,12 @@ proc initPortalProtocol(
protocolId,
toContentId,
createGetHandler(db),
createStoreHandler(db, defaultRadiusConfig),
createRadiusHandler(db),
stream,
bootstrapRecords = bootstrapRecords,
)
proto.dbPut = createStoreHandler(db, defaultRadiusConfig, proto)
return proto
proc stopPortalProtocol(proto: PortalProtocol) {.async.} =
@ -346,12 +345,11 @@ procSuite "Portal Wire Protocol Tests":
protocolId,
toContentId,
createGetHandler(db),
createStoreHandler(db, defaultRadiusConfig),
createRadiusHandler(db),
stream,
)
proto1.dbPut = createStoreHandler(db, defaultRadiusConfig, proto1)
let item = genByteSeq(10_000)
var distances: seq[UInt256] = @[]

View File

@ -254,13 +254,12 @@ proc run(config: PortalCliConf) =
config.protocolId,
testContentIdHandler,
createGetHandler(db),
createStoreHandler(db, defaultRadiusConfig),
createRadiusHandler(db),
stream,
bootstrapRecords = bootstrapRecords,
)
portal.dbPut = createStoreHandler(db, defaultRadiusConfig, portal)
if config.metricsEnabled:
let
address = config.metricsAddress