diff --git a/fluffy/database/content_db.nim b/fluffy/database/content_db.nim index 42806465f..7a80e3f5c 100644 --- a/fluffy/database/content_db.nim +++ b/fluffy/database/content_db.nim @@ -490,41 +490,34 @@ 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 - # 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) + case cfg.kind + of Dynamic: + # In case of dynamic radius, the radius gets adjusted based on the + # 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: - db.adjustRadius(res.deletedFraction, res.distanceOfFurthestElement) - else: - # Note: - # This can occur when the furthest content is bigger than the fraction - # size. This is unlikely to happen as it would require either very - # small storage capacity or a very small `contentDeletionFraction` - # combined with some big content. - info "Database pruning attempt resulted in no content deleted" - return - of Static: - # If the radius is static, it may never be adjusted, database capacity - # is disabled and no pruning is ever done. - db.put(contentId, content) + if res.deletedFraction > 0.0: + db.adjustRadius(res.deletedFraction, res.distanceOfFurthestElement) + else: + # Note: + # This can occur when the furthest content is bigger than the fraction + # size. This is unlikely to happen as it would require either very + # small storage capacity or a very small `contentDeletionFraction` + # combined with some big content. + info "Database pruning attempt resulted in no content deleted" + return + of Static: + # If the radius is static, it may never be adjusted, database capacity + # is disabled and no pruning is ever done. + db.put(contentId, content) ) proc createRadiusHandler*(db: ContentDB): DbRadiusHandler = diff --git a/fluffy/network/beacon/beacon_network.nim b/fluffy/network/beacon/beacon_network.nim index 6a3be2690..5ef2be4c2 100644 --- a/fluffy/network/beacon/beacon_network.nim +++ b/fluffy/network/beacon/beacon_network.nim @@ -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, diff --git a/fluffy/network/history/history_network.nim b/fluffy/network/history/history_network.nim index 5b28cab9b..fe55ba3c5 100644 --- a/fluffy/network/history/history_network.nim +++ b/fluffy/network/history/history_network.nim @@ -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, diff --git a/fluffy/network/state/state_network.nim b/fluffy/network/state/state_network.nim index a76f038ed..110076ea1 100644 --- a/fluffy/network/state/state_network.nim +++ b/fluffy/network/state/state_network.nim @@ -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, diff --git a/fluffy/network/wire/portal_protocol.nim b/fluffy/network/wire/portal_protocol.nim index beb431695..55e727555 100644 --- a/fluffy/network/wire/portal_protocol.nim +++ b/fluffy/network/wire/portal_protocol.nim @@ -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,8 +1597,11 @@ proc storeContent*( contentId: ContentId, content: seq[byte], ) = - doAssert(p.dbPut != nil) - p.dbPut(contentKey, contentId, content) + # 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) proc seedTable*(p: PortalProtocol) = ## Seed the table with specifically provided Portal bootstrap nodes. These are diff --git a/fluffy/tests/test_portal_wire_protocol.nim b/fluffy/tests/test_portal_wire_protocol.nim index 272001d14..2f224d2b3 100644 --- a/fluffy/tests/test_portal_wire_protocol.nim +++ b/fluffy/tests/test_portal_wire_protocol.nim @@ -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] = @[] diff --git a/fluffy/tools/portalcli.nim b/fluffy/tools/portalcli.nim index d7ce10f46..dd620adb9 100644 --- a/fluffy/tools/portalcli.nim +++ b/fluffy/tools/portalcli.nim @@ -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