Move peer context into its own module

This commit is contained in:
Mark Spanbroek 2021-04-12 11:43:54 +02:00 committed by markspanbroek
parent 4ce3f6d3da
commit d9e5bc934f
2 changed files with 33 additions and 22 deletions

View File

@ -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
##

View File

@ -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