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,41 +490,34 @@ proc createGetHandler*(db: ContentDB): DbGetHandler =
ok(content) ok(content)
) )
proc createStoreHandler*( proc createStoreHandler*(db: ContentDB, cfg: RadiusConfig): DbStoreHandler =
db: ContentDB, cfg: RadiusConfig, p: PortalProtocol
): DbStoreHandler =
return ( return (
proc( proc(
contentKey: ContentKeyByteList, contentId: ContentId, content: seq[byte] contentKey: ContentKeyByteList, contentId: ContentId, content: seq[byte]
) {.raises: [], gcsafe.} = ) {.raises: [], gcsafe.} =
# always re-check that the key is in the node range to make sure only case cfg.kind
# content in range is stored. of Dynamic:
# TODO: current silent assumption is that both ContentDB and PortalProtocol # In case of dynamic radius, the radius gets adjusted based on the
# are using the same xor distance function # to storage capacity and content gets pruned accordingly.
if p.inRange(contentId): let res = db.putAndPrune(contentId, content)
case cfg.kind if res.kind == DbPruned:
of Dynamic: portal_pruning_counter.inc()
# In case of dynamic radius, the radius gets adjusted based on the portal_pruning_deleted_elements.set(res.deletedElements.int64)
# to storage capacity and content gets pruned accordingly.
let res = db.putAndPrune(contentId, content)
if res.kind == DbPruned:
portal_pruning_counter.inc()
portal_pruning_deleted_elements.set(res.deletedElements.int64)
if res.deletedFraction > 0.0: if res.deletedFraction > 0.0:
db.adjustRadius(res.deletedFraction, res.distanceOfFurthestElement) db.adjustRadius(res.deletedFraction, res.distanceOfFurthestElement)
else: else:
# Note: # Note:
# This can occur when the furthest content is bigger than the fraction # This can occur when the furthest content is bigger than the fraction
# size. This is unlikely to happen as it would require either very # size. This is unlikely to happen as it would require either very
# small storage capacity or a very small `contentDeletionFraction` # small storage capacity or a very small `contentDeletionFraction`
# combined with some big content. # combined with some big content.
info "Database pruning attempt resulted in no content deleted" info "Database pruning attempt resulted in no content deleted"
return return
of Static: of Static:
# If the radius is static, it may never be adjusted, database capacity # If the radius is static, it may never be adjusted, database capacity
# is disabled and no pruning is ever done. # is disabled and no pruning is ever done.
db.put(contentId, content) db.put(contentId, content)
) )
proc createRadiusHandler*(db: ContentDB): DbRadiusHandler = proc createRadiusHandler*(db: ContentDB): DbRadiusHandler =

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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