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
|
2021-04-14 14:07:49 +00:00
|
|
|
import pkg/nitro
|
2021-04-12 09:43:54 +00:00
|
|
|
import pkg/questionable
|
2021-08-30 19:25:20 +00:00
|
|
|
import ./protobuf/blockexc
|
2021-04-12 09:43:54 +00:00
|
|
|
import ./protobuf/payments
|
2021-04-26 15:11:11 +00:00
|
|
|
import ./protobuf/presence
|
2021-04-12 09:43:54 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
export payments, nitro
|
2021-04-14 14:07:49 +00:00
|
|
|
|
2021-04-12 09:43:54 +00:00
|
|
|
type
|
2021-08-30 19:25:20 +00:00
|
|
|
BlockExcPeerCtx* = ref object of RootObj
|
2021-04-12 09:43:54 +00:00
|
|
|
id*: PeerID
|
2022-04-20 03:46:44 +00:00
|
|
|
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
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
proc peerHave*(context: BlockExcPeerCtx): seq[Cid] =
|
2021-05-10 12:08:40 +00:00
|
|
|
toSeq(context.peerPrices.keys)
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
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 )
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
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)
|
2021-05-10 11:47:15 +00:00
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
func price*(context: BlockExcPeerCtx, cids: seq[Cid]): UInt256 =
|
2021-05-10 11:47:15 +00:00
|
|
|
for cid in cids:
|
|
|
|
if price =? context.peerPrices.?[cid]:
|
|
|
|
result += price
|