nim-codex/codex/blockexchange/peers/peercontext.nim

55 lines
1.7 KiB
Nim
Raw Normal View History

## Nim-Codex
## Copyright (c) 2021 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
2021-04-12 09:43:54 +00:00
import std/sequtils
2021-04-26 15:11:11 +00:00
import std/tables
2021-04-12 09:43:54 +00:00
import pkg/libp2p
import pkg/chronos
import pkg/nitro
2021-04-12 09:43:54 +00:00
import pkg/questionable
import ../protobuf/blockexc
import ../protobuf/payments
import ../protobuf/presence
2021-04-12 09:43:54 +00:00
export payments, nitro
2021-04-12 09:43:54 +00:00
type
BlockExcPeerCtx* = ref object of RootObj
2021-04-12 09:43:54 +00:00
id*: PeerID
peerPrices*: Table[Cid, UInt256] # remote peer have list including price
peerWants*: seq[Entry] # remote peers want lists
exchanged*: int # times peer has exchanged with us
lastExchange*: Moment # last time peer has exchanged with us
account*: ?Account # ethereum account of this peer
paymentChannel*: ?ChannelId # payment channel id
2021-04-12 09:43:54 +00:00
proc peerHave*(context: BlockExcPeerCtx): seq[Cid] =
toSeq(context.peerPrices.keys)
proc contains*(a: openArray[BlockExcPeerCtx], b: PeerID): bool =
2021-04-12 09:43:54 +00:00
## Convenience method to check for peer prepense
##
a.anyIt( it.id == b )
func updatePresence*(context: BlockExcPeerCtx, presence: Presence) =
2021-04-26 15:11:11 +00:00
let cid = presence.cid
let price = presence.price
if cid notin context.peerHave and presence.have:
context.peerPrices[cid] = price
elif cid in context.peerHave and not presence.have:
context.peerPrices.del(cid)
func price*(context: BlockExcPeerCtx, cids: seq[Cid]): UInt256 =
for cid in cids:
if price =? context.peerPrices.?[cid]:
result += price