Csaba Kiraly f707e44c86
fix imports: remove pkg/
pkg/ is specific to how old nimble handles
packages. Nimble 0.14 (with lock file support)
uses pkg2/

Anyway, pkg or pkg2 are not needed, better remove.

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
2023-06-09 11:17:16 +02:00

59 lines
1.7 KiB
Nim

# 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.
{.push raises: [Defect].}
import std/sequtils
import std/strutils
import chronos
import libp2p
import datastore
import questionable
import questionable/results
import ../node
export node, results
const
ProvidersKey* = Key.init("/providers").tryGet # keys is of the form /providers/peerid = provider
CidKey* = Key.init("/cids").tryGet # keys is of the form /cids/cid/peerid/ttl = ttl
ZeroMoment* = Moment.init(0, Nanosecond) # for conversion between Duration and Moment
proc mapFailure*[T](err: T): ref CatchableError =
newException(CatchableError, $err)
proc makeProviderKey*(peerId: PeerId): ?!Key =
(ProvidersKey / $peerId)
proc makeCidKey*(cid: NodeId, peerId: PeerId): ?!Key =
(CidKey / cid.toHex / $peerId / "ttl")
proc fromCidKey*(key: Key): ?!tuple[id: NodeId, peerId: PeerId] =
let
parts = key.id.split(datastore.Separator)
if parts.len == 5:
let
peerId = ?PeerId.init(parts[3]).mapErr(mapFailure)
id = ?NodeId.fromHex(parts[2]).catch
return success (id, peerId)
return failure("Unable to extract peer id from key")
proc fromProvKey*(key: Key): ?!PeerId =
let
parts = key.id.split(datastore.Separator)
if parts.len != 3:
return failure("Can't find peer id in key")
return success ?PeerId.init(parts[^1]).mapErr(mapFailure)