2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2021-02-26 00:23:22 +00: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.
|
|
|
|
|
2022-11-02 17:40:28 +00:00
|
|
|
import pkg/upraises
|
|
|
|
push: {.upraises: [].}
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
|
2022-05-19 22:28:53 +00:00
|
|
|
import ../protobuf/blockexc
|
2023-07-19 13:10:14 +00:00
|
|
|
import ../protobuf/message
|
2022-10-13 23:58:57 +00: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 07:35:03 +00:00
|
|
|
import ../../logutils
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
logScope:
|
2022-11-15 15:46:21 +00:00
|
|
|
topics = "codex blockexcnetworkpeer"
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
type
|
2022-11-02 17:40:28 +00:00
|
|
|
ConnProvider* = proc(): Future[Connection] {.gcsafe, closure.}
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
RPCHandler* = proc(peer: NetworkPeer, msg: Message): Future[void] {.gcsafe.}
|
|
|
|
|
|
|
|
NetworkPeer* = ref object of RootObj
|
|
|
|
id*: PeerId
|
|
|
|
handler*: RPCHandler
|
|
|
|
sendConn: Connection
|
|
|
|
getConn: ConnProvider
|
|
|
|
|
|
|
|
proc connected*(b: NetworkPeer): bool =
|
|
|
|
not(isNil(b.sendConn)) and
|
|
|
|
not(b.sendConn.closed or b.sendConn.atEof)
|
|
|
|
|
|
|
|
proc readLoop*(b: NetworkPeer, conn: Connection) {.async.} =
|
|
|
|
if isNil(conn):
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2022-05-20 16:53:34 +00:00
|
|
|
while not conn.atEof or not conn.closed:
|
2022-01-10 15:32:56 +00:00
|
|
|
let
|
2023-07-19 13:10:14 +00:00
|
|
|
data = await conn.readLp(MaxMessageSize.int)
|
2023-03-10 07:02:54 +00:00
|
|
|
msg = Message.protobufDecode(data).mapFailure().tryGet()
|
2021-02-26 00:23:22 +00:00
|
|
|
await b.handler(b, msg)
|
2023-07-19 13:10:14 +00:00
|
|
|
except CatchableError as err:
|
|
|
|
warn "Exception in blockexc read loop", msg = err.msg
|
2021-02-26 00:23:22 +00:00
|
|
|
finally:
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
proc connect*(b: NetworkPeer): Future[Connection] {.async.} =
|
|
|
|
if b.connected:
|
|
|
|
return b.sendConn
|
|
|
|
|
|
|
|
b.sendConn = await b.getConn()
|
|
|
|
asyncSpawn b.readLoop(b.sendConn)
|
|
|
|
return b.sendConn
|
|
|
|
|
|
|
|
proc send*(b: NetworkPeer, msg: Message) {.async.} =
|
|
|
|
let conn = await b.connect()
|
|
|
|
|
|
|
|
if isNil(conn):
|
2024-05-16 17:06:12 +00:00
|
|
|
warn "Unable to get send connection for peer message not sent", peer = b.id
|
2021-02-26 00:23:22 +00:00
|
|
|
return
|
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
await conn.writeLp(protobufEncode(msg))
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc broadcast*(b: NetworkPeer, msg: Message) =
|
|
|
|
proc sendAwaiter() {.async.} =
|
|
|
|
try:
|
|
|
|
await b.send(msg)
|
|
|
|
except CatchableError as exc:
|
2024-05-16 17:06:12 +00:00
|
|
|
warn "Exception broadcasting message to peer", peer = b.id, exc = exc.msg
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
asyncSpawn sendAwaiter()
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
func new*(
|
2021-02-26 00:23:22 +00:00
|
|
|
T: type NetworkPeer,
|
|
|
|
peer: PeerId,
|
|
|
|
connProvider: ConnProvider,
|
2023-11-27 18:25:53 +00:00
|
|
|
rpcHandler: RPCHandler): NetworkPeer =
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
doAssert(not isNil(connProvider),
|
|
|
|
"should supply connection provider")
|
|
|
|
|
|
|
|
NetworkPeer(
|
|
|
|
id: peer,
|
|
|
|
getConn: connProvider,
|
|
|
|
handler: rpcHandler)
|