2022-09-29 23:49:55 +00:00
|
|
|
# codex-dht - Codex DHT
|
|
|
|
# Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import std/sequtils
|
|
|
|
import std/strutils
|
|
|
|
|
|
|
|
import pkg/datastore
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/stew/results as rs
|
|
|
|
import pkg/stew/byteutils
|
2022-10-01 16:05:03 +00:00
|
|
|
import pkg/questionable
|
2022-09-29 23:49:55 +00:00
|
|
|
import pkg/questionable/results
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
import ./maintenance
|
|
|
|
import ./cache
|
|
|
|
import ./common
|
|
|
|
import ../spr
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
export cache, datastore
|
2022-09-29 23:49:55 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "discv5 providers manager"
|
|
|
|
|
|
|
|
const
|
2022-10-01 16:05:03 +00:00
|
|
|
DefaultProviderTTL* = 24.hours
|
2022-09-29 23:49:55 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
ProvidersManager* = ref object of RootObj
|
|
|
|
store*: Datastore
|
2022-10-01 16:05:03 +00:00
|
|
|
cache*: ProvidersCache
|
2022-09-29 23:49:55 +00:00
|
|
|
ttl*: Duration
|
|
|
|
maxItems*: uint
|
|
|
|
maxProviders*: uint
|
|
|
|
disableCache*: bool
|
2022-10-01 16:05:03 +00:00
|
|
|
expiredLoop*: Future[void]
|
|
|
|
orphanedLoop*: Future[void]
|
|
|
|
started*: bool
|
|
|
|
batchSize*: int
|
|
|
|
cleanupInterval*: Duration
|
2022-09-29 23:49:55 +00:00
|
|
|
|
|
|
|
proc getProvByKey*(self: ProvidersManager, key: Key): Future[?!SignedPeerRecord] {.async.} =
|
|
|
|
|
|
|
|
without bytes =? (await self.store.get(key)) and bytes.len <= 0:
|
|
|
|
trace "No provider in store"
|
|
|
|
return failure("No no provider in store")
|
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
return SignedPeerRecord.decode(bytes).mapErr(mapFailure)
|
2022-09-29 23:49:55 +00:00
|
|
|
|
|
|
|
proc add*(
|
|
|
|
self: ProvidersManager,
|
|
|
|
cid: NodeId,
|
|
|
|
provider: SignedPeerRecord,
|
|
|
|
ttl = ZeroDuration): Future[?!void] {.async.} =
|
|
|
|
|
|
|
|
let
|
|
|
|
peerId = provider.data.peerId
|
|
|
|
|
|
|
|
trace "Adding provider to persistent store", cid, peerId
|
|
|
|
without provKey =? makeProviderKey(peerId), err:
|
|
|
|
trace "Error creating key from provider record", err = err.msg
|
|
|
|
return failure err.msg
|
|
|
|
|
|
|
|
without cidKey =? makeCidKey(cid, peerId), err:
|
|
|
|
trace "Error creating key from content id", err = err.msg
|
|
|
|
return failure err.msg
|
|
|
|
|
|
|
|
let
|
|
|
|
expires =
|
|
|
|
if ttl > ZeroDuration:
|
|
|
|
ttl
|
|
|
|
else:
|
|
|
|
Moment.fromNow(self.ttl) - ZeroMoment
|
|
|
|
|
|
|
|
ttl = expires.microseconds.uint64.toBytesBE
|
|
|
|
|
|
|
|
bytes: seq[byte] =
|
|
|
|
if existing =? (await self.getProvByKey(provKey)) and
|
|
|
|
existing.data.seqNo >= provider.data.seqNo:
|
|
|
|
trace "Provider with same seqNo already exist", seqNo = $provider.data.seqNo
|
|
|
|
@[]
|
|
|
|
else:
|
|
|
|
without bytes =? provider.envelope.encode:
|
|
|
|
trace "Enable to encode provider"
|
|
|
|
return failure "Unable to encode provider"
|
|
|
|
bytes
|
|
|
|
|
|
|
|
if bytes.len > 0:
|
|
|
|
trace "Adding or updating provider record", cid, peerId
|
|
|
|
if err =? (await self.store.put(provKey, bytes)).errorOption:
|
|
|
|
trace "Unable to store provider with key", key = provKey, err = err.msg
|
|
|
|
|
|
|
|
trace "Adding or updating cid", cid, key = cidKey, ttl = expires.minutes
|
|
|
|
if err =? (await self.store.put(cidKey, @ttl)).errorOption:
|
|
|
|
trace "Unable to store provider with key", key = cidKey, err = err.msg
|
|
|
|
return
|
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
self.cache.add(cid, provider)
|
2022-09-29 23:49:55 +00:00
|
|
|
trace "Provider for cid added", cidKey, provKey
|
|
|
|
return success()
|
|
|
|
|
|
|
|
proc get*(
|
|
|
|
self: ProvidersManager,
|
|
|
|
id: NodeId,
|
2022-10-01 16:05:03 +00:00
|
|
|
start = 0,
|
|
|
|
stop = MaxProvidersPerEntry.int): Future[?!seq[SignedPeerRecord]] {.async.} =
|
|
|
|
trace "Retrieving providers from persistent store", id
|
|
|
|
|
|
|
|
let
|
|
|
|
provs = self.cache.get(id, start = start, stop = stop)
|
2022-09-29 23:49:55 +00:00
|
|
|
|
|
|
|
if provs.len > 0:
|
|
|
|
return success provs
|
|
|
|
|
|
|
|
without cidKey =? (CidKey / id.toHex), err:
|
|
|
|
return failure err.msg
|
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
trace "Querying providers from persistent store", id, key = cidKey
|
2022-09-29 23:49:55 +00:00
|
|
|
var
|
|
|
|
providers: seq[SignedPeerRecord]
|
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
block:
|
|
|
|
without cidIter =?
|
|
|
|
(await self.store.query(Query.init(cidKey, offset = start, limit = stop))), err:
|
|
|
|
return failure err.msg
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
defer:
|
|
|
|
if not isNil(cidIter):
|
|
|
|
trace "Cleaning up query iterator"
|
|
|
|
discard (await cidIter.dispose())
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
for item in cidIter:
|
|
|
|
# TODO: =? doesn't support tuples
|
|
|
|
if pair =? (await item) and pair.key.isSome:
|
|
|
|
let
|
|
|
|
(key, val) = (pair.key.get, pair.data)
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
without pairs =? key.fromCidKey() and
|
|
|
|
provKey =? makeProviderKey(pairs.peerId), err:
|
|
|
|
trace "Error creating key from provider record", err = err.msg
|
|
|
|
continue
|
|
|
|
|
|
|
|
trace "Querying provider key", key = provKey
|
|
|
|
without data =? (await self.store.get(provKey)):
|
|
|
|
trace "Error getting provider", key = provKey
|
|
|
|
continue
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
without provider =? SignedPeerRecord.decode(data).mapErr(mapFailure), err:
|
|
|
|
trace "Unable to decode provider from store", err = err.msg
|
|
|
|
continue
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
trace "Retrieved provider with key", key = provKey
|
|
|
|
providers.add(provider)
|
|
|
|
self.cache.add(id, provider)
|
|
|
|
|
|
|
|
trace "Retrieved providers from persistent store", cid = id, len = providers.len
|
2022-09-29 23:49:55 +00:00
|
|
|
return success providers
|
|
|
|
|
|
|
|
proc contains*(
|
|
|
|
self: ProvidersManager,
|
|
|
|
id: NodeId,
|
|
|
|
peerId: PeerId): Future[bool] {.async.} =
|
|
|
|
without key =? makeCidKey(id, peerId), err:
|
|
|
|
return false
|
|
|
|
|
2022-12-02 22:47:16 +00:00
|
|
|
return (await self.store.has(key)) |? false
|
2022-09-29 23:49:55 +00:00
|
|
|
|
|
|
|
proc contains*(self: ProvidersManager, peerId: PeerId): Future[bool] {.async.} =
|
|
|
|
without provKey =? makeProviderKey(peerId), err:
|
|
|
|
return false
|
|
|
|
|
2022-12-02 22:47:16 +00:00
|
|
|
return (await self.store.has(provKey)) |? false
|
2022-09-29 23:49:55 +00:00
|
|
|
|
|
|
|
proc contains*(self: ProvidersManager, cid: NodeId): Future[bool] {.async.} =
|
|
|
|
without cidKey =? (CidKey / $cid), err:
|
|
|
|
return false
|
|
|
|
|
|
|
|
let
|
|
|
|
q = Query.init(cidKey, limit = 1)
|
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
block:
|
|
|
|
without iter =? (await self.store.query(q)), err:
|
|
|
|
trace "Unable to obtain record for key", key = cidKey
|
|
|
|
return false
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
defer:
|
|
|
|
if not isNil(iter):
|
|
|
|
trace "Cleaning up query iterator"
|
|
|
|
discard (await iter.dispose())
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
for item in iter:
|
|
|
|
if pair =? (await item) and pair.key.isSome:
|
|
|
|
return true
|
2022-09-29 23:49:55 +00:00
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
proc remove*(self: ProvidersManager, cid: NodeId): Future[?!void] {.async.} =
|
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
self.cache.drop(cid)
|
2022-09-29 23:49:55 +00:00
|
|
|
without cidKey =? (CidKey / $cid), err:
|
|
|
|
return failure(err.msg)
|
|
|
|
|
|
|
|
let
|
|
|
|
q = Query.init(cidKey)
|
|
|
|
|
|
|
|
block:
|
2022-10-01 16:05:03 +00:00
|
|
|
without iter =? (await self.store.query(q)), err:
|
|
|
|
trace "Unable to obtain record for key", key = cidKey
|
2022-10-03 21:14:47 +00:00
|
|
|
return failure err
|
2022-10-01 16:05:03 +00:00
|
|
|
|
2022-09-29 23:49:55 +00:00
|
|
|
defer:
|
2022-10-01 16:05:03 +00:00
|
|
|
if not isNil(iter):
|
|
|
|
trace "Cleaning up query iterator"
|
|
|
|
discard (await iter.dispose())
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-03 21:14:47 +00:00
|
|
|
var
|
|
|
|
keys: seq[Key]
|
|
|
|
|
2022-09-29 23:49:55 +00:00
|
|
|
for item in iter:
|
|
|
|
if pair =? (await item) and pair.key.isSome:
|
2022-10-03 21:14:47 +00:00
|
|
|
let
|
|
|
|
key = pair.key.get()
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-03 21:14:47 +00:00
|
|
|
keys.add(key)
|
2022-10-01 16:05:03 +00:00
|
|
|
without pairs =? key.fromCidKey, err:
|
2022-09-29 23:49:55 +00:00
|
|
|
trace "Unable to parse peer id from key", key
|
2022-10-03 21:14:47 +00:00
|
|
|
return failure err
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
self.cache.remove(cid, pairs.peerId)
|
2022-09-29 23:49:55 +00:00
|
|
|
trace "Deleted record from store", key
|
|
|
|
|
2022-10-03 21:14:47 +00:00
|
|
|
if keys.len > 0 and err =? (await self.store.delete(keys)).errorOption:
|
|
|
|
trace "Error deleting record from persistent store", err = err.msg
|
|
|
|
return failure err
|
|
|
|
|
2022-09-29 23:49:55 +00:00
|
|
|
return success()
|
|
|
|
|
|
|
|
proc remove*(self: ProvidersManager, peerId: PeerId): Future[?!void] {.async.} =
|
|
|
|
without cidKey =? (CidKey / "*" / $peerId), err:
|
2022-10-03 21:14:47 +00:00
|
|
|
return failure err
|
2022-09-29 23:49:55 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
q = Query.init(cidKey)
|
|
|
|
|
|
|
|
block:
|
2022-10-01 16:05:03 +00:00
|
|
|
without iter =? (await self.store.query(q)), err:
|
|
|
|
trace "Unable to obtain record for key", key = cidKey
|
2022-10-03 21:14:47 +00:00
|
|
|
return failure err
|
2022-10-01 16:05:03 +00:00
|
|
|
|
2022-09-29 23:49:55 +00:00
|
|
|
defer:
|
2022-10-01 16:05:03 +00:00
|
|
|
if not isNil(iter):
|
|
|
|
trace "Cleaning up query iterator"
|
|
|
|
discard (await iter.dispose())
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-03 21:14:47 +00:00
|
|
|
var
|
|
|
|
keys: seq[Key]
|
|
|
|
|
2022-09-29 23:49:55 +00:00
|
|
|
for item in iter:
|
|
|
|
if pair =? (await item) and pair.key.isSome:
|
|
|
|
let
|
|
|
|
key = pair.key.get()
|
|
|
|
|
2022-10-03 21:14:47 +00:00
|
|
|
keys.add(key)
|
2022-09-29 23:49:55 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
parts = key.id.split(datastore.Separator)
|
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
self.cache.remove(NodeId.fromHex(parts[2]), peerId)
|
2022-09-29 23:49:55 +00:00
|
|
|
|
2022-10-03 21:14:47 +00:00
|
|
|
if keys.len > 0 and err =? (await self.store.delete(keys)).errorOption:
|
|
|
|
trace "Error deleting record from persistent store", err = err.msg
|
|
|
|
return failure err
|
|
|
|
|
|
|
|
trace "Deleted records from store"
|
|
|
|
|
2022-09-29 23:49:55 +00:00
|
|
|
without provKey =? makeProviderKey(peerId), err:
|
2022-10-03 21:14:47 +00:00
|
|
|
return failure err
|
2022-09-29 23:49:55 +00:00
|
|
|
|
|
|
|
trace "Removing provider record", key = provKey
|
|
|
|
return (await self.store.delete(provKey))
|
|
|
|
|
|
|
|
proc remove*(
|
|
|
|
self: ProvidersManager,
|
|
|
|
cid: NodeId,
|
|
|
|
peerId: PeerId): Future[?!void] {.async.} =
|
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
self.cache.remove(cid, peerId)
|
2022-09-29 23:49:55 +00:00
|
|
|
without cidKey =? makeCidKey(cid, peerId), err:
|
|
|
|
trace "Error creating key from content id", err = err.msg
|
|
|
|
return failure err.msg
|
|
|
|
|
|
|
|
return (await self.store.delete(cidKey))
|
|
|
|
|
2022-10-01 16:05:03 +00:00
|
|
|
proc cleanupExpiredLoop(self: ProvidersManager) {.async.} =
|
|
|
|
try:
|
|
|
|
while self.started:
|
|
|
|
await self.store.cleanupExpired(self.batchSize)
|
|
|
|
await sleepAsync(self.cleanupInterval)
|
|
|
|
except CancelledError as exc:
|
|
|
|
trace "Cancelled expired cleanup job", err = exc.msg
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Exception in expired cleanup job", err = exc.msg
|
|
|
|
raiseAssert "Exception in expired cleanup job"
|
|
|
|
|
|
|
|
proc cleanupOrphanedLoop(self: ProvidersManager) {.async.} =
|
|
|
|
try:
|
|
|
|
while self.started:
|
|
|
|
await self.store.cleanupOrphaned(self.batchSize)
|
|
|
|
await sleepAsync(self.cleanupInterval)
|
|
|
|
except CancelledError as exc:
|
|
|
|
trace "Cancelled orphaned cleanup job", err = exc.msg
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Exception in orphaned cleanup job", err = exc.msg
|
|
|
|
raiseAssert "Exception in orphaned cleanup job"
|
|
|
|
|
|
|
|
proc start*(self: ProvidersManager) {.async.} =
|
|
|
|
self.started = true
|
|
|
|
self.expiredLoop = self.cleanupExpiredLoop
|
|
|
|
self.orphanedLoop = self.cleanupOrphanedLoop
|
|
|
|
|
|
|
|
proc stop*(self: ProvidersManager) {.async.} =
|
|
|
|
await self.expiredLoop.cancelAndWait()
|
|
|
|
await self.orphanedLoop.cancelAndWait()
|
|
|
|
self.started = false
|
|
|
|
|
2022-09-29 23:49:55 +00:00
|
|
|
func new*(
|
|
|
|
T: type ProvidersManager,
|
|
|
|
store: Datastore,
|
|
|
|
disableCache = false,
|
|
|
|
ttl = DefaultProviderTTL,
|
|
|
|
maxItems = MaxProvidersEntries,
|
2022-10-01 16:05:03 +00:00
|
|
|
maxProviders = MaxProvidersPerEntry,
|
|
|
|
batchSize = ExpiredCleanupBatch,
|
|
|
|
cleanupInterval = CleanupInterval): T =
|
|
|
|
|
|
|
|
T(
|
|
|
|
store: store,
|
|
|
|
ttl: ttl,
|
|
|
|
maxItems: maxItems,
|
|
|
|
maxProviders: maxProviders,
|
|
|
|
disableCache: disableCache,
|
|
|
|
batchSize: batchSize,
|
|
|
|
cleanupInterval: cleanupInterval,
|
|
|
|
cache: ProvidersCache.init(
|
|
|
|
size = maxItems,
|
|
|
|
maxProviders = maxProviders,
|
|
|
|
disable = disableCache))
|