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
|
2023-11-14 12:02:17 +00:00
|
|
|
import std/sets
|
2022-11-15 15:46:21 +00:00
|
|
|
|
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
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
import ../../blocktype
|
feat: create logging proxy (#663)
* implement a logging proxy
The logging proxy:
- prevents the need to import chronicles (as well as export except toJson),
- prevents the need to override `writeValue` or use or import nim-json-seralization elsewhere in the codebase, allowing for sole use of utils/json for de/serialization,
- and handles json formatting correctly in chronicles json sinks
* Rename logging -> logutils to avoid ambiguity with common names
* clean up
* add setProperty for JsonRecord, remove nim-json-serialization conflict
* Allow specifying textlines and json format separately
Not specifying a LogFormat will apply the formatting to both textlines and json sinks.
Specifying a LogFormat will apply the formatting to only that sink.
* remove unneeded usages of std/json
We only need to import utils/json instead of std/json
* move serialization from rest/json to utils/json so it can be shared
* fix NoColors ambiguity
Was causing unit tests to fail on Windows.
* Remove nre usage to fix Windows error
Windows was erroring with `could not load: pcre64.dll`. Instead of fixing that error, remove the pcre usage :)
* Add logutils module doc
* Shorten logutils.formatIt for `NBytes`
Both json and textlines formatIt were not needed, and could be combined into one formatIt
* remove debug integration test config
debug output and logformat of json for integration test logs
* Use ## module doc to support docgen
* bump nim-poseidon2 to export fromBytes
Before the changes in this branch, fromBytes was likely being resolved by nim-stew, or other dependency. With the changes in this branch, that dependency was removed and fromBytes could no longer be resolved. By exporting fromBytes from nim-poseidon, the correct resolution is now happening.
* fixes to get compiling after rebasing master
* Add support for Result types being logged using formatIt
2024-01-23 07:35:03 +00:00
|
|
|
import ../../logutils
|
2023-11-14 12:02:17 +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
|
2023-03-10 07:02:54 +00:00
|
|
|
id*: PeerId
|
2023-12-22 12:04:01 +00:00
|
|
|
blocks*: Table[BlockAddress, Presence] # remote peer have list including price
|
|
|
|
peerWants*: seq[WantListEntry] # 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
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
proc peerHave*(self: BlockExcPeerCtx): seq[BlockAddress] =
|
2022-11-15 15:46:21 +00:00
|
|
|
toSeq(self.blocks.keys)
|
2021-05-10 12:08:40 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
proc peerHaveCids*(self: BlockExcPeerCtx): HashSet[Cid] =
|
|
|
|
self.blocks.keys.toSeq.mapIt(it.cidOrTreeCid).toHashSet
|
|
|
|
|
|
|
|
proc peerWantsCids*(self: BlockExcPeerCtx): HashSet[Cid] =
|
|
|
|
self.peerWants.mapIt(it.address.cidOrTreeCid).toHashSet
|
|
|
|
|
|
|
|
proc contains*(self: BlockExcPeerCtx, address: BlockAddress): bool =
|
|
|
|
address 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) =
|
2023-11-14 12:02:17 +00:00
|
|
|
self.blocks[presence.address] = presence
|
2021-04-12 09:43:54 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
func cleanPresence*(self: BlockExcPeerCtx, addresses: seq[BlockAddress]) =
|
|
|
|
for a in addresses:
|
|
|
|
self.blocks.del(a)
|
2021-04-26 15:11:11 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
func cleanPresence*(self: BlockExcPeerCtx, address: BlockAddress) =
|
|
|
|
self.cleanPresence(@[address])
|
2021-05-10 11:47:15 +00:00
|
|
|
|
2023-11-14 12:02:17 +00:00
|
|
|
func price*(self: BlockExcPeerCtx, addresses: seq[BlockAddress]): UInt256 =
|
2022-11-15 15:46:21 +00:00
|
|
|
var price = 0.u256
|
2023-11-14 12:02:17 +00:00
|
|
|
for a in addresses:
|
|
|
|
self.blocks.withValue(a, precense):
|
2022-11-15 15:46:21 +00:00
|
|
|
price += precense[].price
|
|
|
|
|
|
|
|
price
|