2022-05-19 22:28:53 +00:00
|
|
|
## 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
|
2022-11-15 15:46:21 +00:00
|
|
|
|
|
|
|
import pkg/chronicles
|
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
|
2022-05-19 02:29:15 +00:00
|
|
|
|
2022-05-19 22:28:53 +00:00
|
|
|
import ../protobuf/blockexc
|
|
|
|
import ../protobuf/payments
|
|
|
|
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
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
logScope:
|
|
|
|
topics = "codex peercontext"
|
|
|
|
|
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-11-15 15:46:21 +00:00
|
|
|
blocks*: Table[Cid, Presence] # remote peer have list including price
|
2022-05-12 20:23:05 +00:00
|
|
|
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
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
proc peerHave*(self: BlockExcPeerCtx): seq[Cid] =
|
|
|
|
toSeq(self.blocks.keys)
|
2021-05-10 12:08:40 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
proc contains*(self: BlockExcPeerCtx, cid: Cid): bool =
|
|
|
|
cid in self.blocks
|
2021-04-12 09:43:54 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
func setPresence*(self: BlockExcPeerCtx, presence: Presence) =
|
|
|
|
self.blocks[presence.cid] = presence
|
2021-04-12 09:43:54 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
func cleanPresence*(self: BlockExcPeerCtx, cids: seq[Cid]) =
|
|
|
|
for cid in cids:
|
|
|
|
self.blocks.del(cid)
|
2021-04-26 15:11:11 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
func cleanPresence*(self: BlockExcPeerCtx, cid: Cid) =
|
|
|
|
self.cleanPresence(@[cid])
|
2021-05-10 11:47:15 +00:00
|
|
|
|
2022-11-15 15:46:21 +00:00
|
|
|
func price*(self: BlockExcPeerCtx, cids: seq[Cid]): UInt256 =
|
|
|
|
var price = 0.u256
|
2021-05-10 11:47:15 +00:00
|
|
|
for cid in cids:
|
2022-11-15 15:46:21 +00:00
|
|
|
self.blocks.withValue(cid, precense):
|
|
|
|
price += precense[].price
|
|
|
|
|
|
|
|
trace "Blocks price", price
|
|
|
|
price
|