2022-05-19 14:56:03 -05:00
|
|
|
## Nim-Codex
|
2021-02-25 18:23:22 -06:00
|
|
|
## 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.
|
|
|
|
|
2025-02-25 16:42:44 -06:00
|
|
|
{.push raises: [].}
|
2022-11-02 12:40:28 -05:00
|
|
|
|
2021-02-25 18:23:22 -06:00
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
|
2022-05-19 16:28:53 -06:00
|
|
|
import ../protobuf/blockexc
|
2023-07-19 15:10:14 +02:00
|
|
|
import ../protobuf/message
|
2022-10-14 02:58:57 +03:00
|
|
|
import ../../errors
|
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 18:35:03 +11:00
|
|
|
import ../../logutils
|
2025-02-25 16:42:44 -06:00
|
|
|
import ../../utils/trackedfutures
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
logScope:
|
2022-11-15 09:46:21 -06:00
|
|
|
topics = "codex blockexcnetworkpeer"
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2025-02-24 15:01:23 -06:00
|
|
|
const DefaultYieldInterval = 50.millis
|
|
|
|
|
2021-02-25 18:23:22 -06:00
|
|
|
type
|
2025-02-25 16:42:44 -06:00
|
|
|
ConnProvider* =
|
|
|
|
proc(): Future[Connection] {.gcsafe, async: (raises: [CancelledError]).}
|
2022-11-02 12:40:28 -05:00
|
|
|
|
2025-02-25 16:42:44 -06:00
|
|
|
RPCHandler* = proc(peer: NetworkPeer, msg: Message) {.gcsafe, async: (raises: []).}
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
NetworkPeer* = ref object of RootObj
|
|
|
|
id*: PeerId
|
|
|
|
handler*: RPCHandler
|
|
|
|
sendConn: Connection
|
|
|
|
getConn: ConnProvider
|
2025-02-24 15:01:23 -06:00
|
|
|
yieldInterval*: Duration = DefaultYieldInterval
|
2025-02-25 16:42:44 -06:00
|
|
|
trackedFutures: TrackedFutures
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2025-02-25 16:42:44 -06:00
|
|
|
proc connected*(self: NetworkPeer): bool =
|
|
|
|
not (isNil(self.sendConn)) and not (self.sendConn.closed or self.sendConn.atEof)
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2025-02-25 16:42:44 -06:00
|
|
|
proc readLoop*(self: NetworkPeer, conn: Connection) {.async: (raises: []).} =
|
2021-02-25 18:23:22 -06:00
|
|
|
if isNil(conn):
|
2025-02-25 16:42:44 -06:00
|
|
|
trace "No connection to read from", peer = self.id
|
2021-02-25 18:23:22 -06:00
|
|
|
return
|
|
|
|
|
2025-02-25 16:42:44 -06:00
|
|
|
trace "Attaching read loop", peer = self.id, connId = conn.oid
|
2021-02-25 18:23:22 -06:00
|
|
|
try:
|
2025-02-25 16:42:44 -06:00
|
|
|
var nextYield = Moment.now() + self.yieldInterval
|
2022-05-20 10:53:34 -06:00
|
|
|
while not conn.atEof or not conn.closed:
|
2025-02-24 15:01:23 -06:00
|
|
|
if Moment.now() > nextYield:
|
2025-02-25 16:42:44 -06:00
|
|
|
nextYield = Moment.now() + self.yieldInterval
|
2025-02-24 15:01:23 -06:00
|
|
|
trace "Yielding in read loop",
|
2025-02-25 16:42:44 -06:00
|
|
|
peer = self.id, nextYield = nextYield, interval = self.yieldInterval
|
2025-02-24 15:01:23 -06:00
|
|
|
await sleepAsync(10.millis)
|
|
|
|
|
2022-01-10 09:32:56 -06:00
|
|
|
let
|
2023-07-19 15:10:14 +02:00
|
|
|
data = await conn.readLp(MaxMessageSize.int)
|
2023-03-10 08:02:54 +01:00
|
|
|
msg = Message.protobufDecode(data).mapFailure().tryGet()
|
2025-02-25 16:42:44 -06:00
|
|
|
trace "Received message", peer = self.id, connId = conn.oid
|
|
|
|
await self.handler(self, msg)
|
2024-05-23 17:29:30 +02:00
|
|
|
except CancelledError:
|
|
|
|
trace "Read loop cancelled"
|
2023-07-19 15:10:14 +02:00
|
|
|
except CatchableError as err:
|
|
|
|
warn "Exception in blockexc read loop", msg = err.msg
|
2021-02-25 18:23:22 -06:00
|
|
|
finally:
|
2025-02-25 16:42:44 -06:00
|
|
|
trace "Detaching read loop", peer = self.id, connId = conn.oid
|
2021-02-25 18:23:22 -06:00
|
|
|
await conn.close()
|
|
|
|
|
2025-02-25 16:42:44 -06:00
|
|
|
proc connect*(
|
|
|
|
self: NetworkPeer
|
|
|
|
): Future[Connection] {.async: (raises: [CancelledError]).} =
|
|
|
|
if self.connected:
|
|
|
|
trace "Already connected", peer = self.id, connId = self.sendConn.oid
|
|
|
|
return self.sendConn
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2025-02-25 16:42:44 -06:00
|
|
|
self.sendConn = await self.getConn()
|
|
|
|
self.trackedFutures.track(self.readLoop(self.sendConn))
|
|
|
|
return self.sendConn
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2025-02-25 16:42:44 -06:00
|
|
|
proc send*(
|
|
|
|
self: NetworkPeer, msg: Message
|
|
|
|
) {.async: (raises: [CancelledError, LPStreamError]).} =
|
|
|
|
let conn = await self.connect()
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
if isNil(conn):
|
2025-02-25 16:42:44 -06:00
|
|
|
warn "Unable to get send connection for peer message not sent", peer = self.id
|
2021-02-25 18:23:22 -06:00
|
|
|
return
|
|
|
|
|
2025-02-25 16:42:44 -06:00
|
|
|
trace "Sending message", peer = self.id, connId = conn.oid
|
2023-03-10 08:02:54 +01:00
|
|
|
await conn.writeLp(protobufEncode(msg))
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2021-08-30 13:25:20 -06:00
|
|
|
func new*(
|
2021-02-25 18:23:22 -06:00
|
|
|
T: type NetworkPeer,
|
|
|
|
peer: PeerId,
|
|
|
|
connProvider: ConnProvider,
|
2023-11-27 12:25:53 -06:00
|
|
|
rpcHandler: RPCHandler,
|
|
|
|
): NetworkPeer =
|
2021-02-25 18:23:22 -06:00
|
|
|
doAssert(not isNil(connProvider), "should supply connection provider")
|
2025-01-21 21:54:46 +01:00
|
|
|
|
2025-02-25 16:42:44 -06:00
|
|
|
NetworkPeer(
|
|
|
|
id: peer,
|
|
|
|
getConn: connProvider,
|
|
|
|
handler: rpcHandler,
|
|
|
|
trackedFutures: TrackedFutures(),
|
|
|
|
)
|