From d9e5bc934f0be89c82d948dfa6862577a2b0adb9 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 12 Apr 2021 11:43:54 +0200 Subject: [PATCH] Move peer context into its own module --- dagger/bitswap/engine.nim | 25 +++---------------------- dagger/bitswap/peercontext.nim | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 dagger/bitswap/peercontext.nim diff --git a/dagger/bitswap/engine.nim b/dagger/bitswap/engine.nim index 36f17b25..39353a5b 100644 --- a/dagger/bitswap/engine.nim +++ b/dagger/bitswap/engine.nim @@ -22,6 +22,9 @@ import ../utils/asyncheapqueue import ./network import ./pendingblocks +import ./peercontext + +export peercontext logScope: topics = "dagger bitswap engine" @@ -34,16 +37,6 @@ type TaskHandler* = proc(task: BitswapPeerCtx): Future[void] {.gcsafe.} TaskScheduler* = proc(task: BitswapPeerCtx): bool {.gcsafe.} - BitswapPeerCtx* = ref object of RootObj - id*: PeerID - peerHave*: seq[Cid] # remote peers have lists - peerWants*: seq[Entry] # remote peers want lists - bytesSent*: int # bytes sent to remote - bytesRecv*: int # bytes received from remote - exchanged*: int # times peer has exchanged with us - lastExchange*: Moment # last time peer has exchanged with us - pricing*: ?Pricing # optional bandwidth price for this peer - BitswapEngine* = ref object of RootObj localStore*: BlockStore # where we localStore blocks for this instance peers*: seq[BitswapPeerCtx] # peers we're currently actively exchanging with @@ -61,18 +54,6 @@ proc contains*(a: AsyncHeapQueue[Entry], b: Cid): bool = a.anyIt( it.cid == b ) -proc contains*(a: openArray[BitswapPeerCtx], b: PeerID): bool = - ## Convenience method to check for peer prepense - ## - - a.anyIt( it.id == b ) - -proc debtRatio*(b: BitswapPeerCtx): float = - b.bytesSent / (b.bytesRecv + 1) - -proc `<`*(a, b: BitswapPeerCtx): bool = - a.debtRatio < b.debtRatio - proc getPeerCtx*(b: BitswapEngine, peerId: PeerID): BitswapPeerCtx = ## Get the peer's context ## diff --git a/dagger/bitswap/peercontext.nim b/dagger/bitswap/peercontext.nim new file mode 100644 index 00000000..5685688d --- /dev/null +++ b/dagger/bitswap/peercontext.nim @@ -0,0 +1,30 @@ +import std/sequtils +import pkg/libp2p +import pkg/chronos +import pkg/questionable +import ./protobuf/bitswap +import ./protobuf/payments + +type + BitswapPeerCtx* = ref object of RootObj + id*: PeerID + peerHave*: seq[Cid] # remote peers have lists + peerWants*: seq[Entry] # remote peers want lists + bytesSent*: int # bytes sent to remote + bytesRecv*: int # bytes received from remote + exchanged*: int # times peer has exchanged with us + lastExchange*: Moment # last time peer has exchanged with us + pricing*: ?Pricing # optional bandwidth price for this peer + +proc contains*(a: openArray[BitswapPeerCtx], b: PeerID): bool = + ## Convenience method to check for peer prepense + ## + + a.anyIt( it.id == b ) + +proc debtRatio*(b: BitswapPeerCtx): float = + b.bytesSent / (b.bytesRecv + 1) + +proc `<`*(a, b: BitswapPeerCtx): bool = + a.debtRatio < b.debtRatio +