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/chronicles
|
|
|
|
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
|
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()
|
2022-01-10 15:32:56 +00:00
|
|
|
trace "Got message for peer", peer = b.id
|
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):
|
|
|
|
trace "Unable to get send connection for peer message not sent", peer = b.id
|
|
|
|
return
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
trace "Sending message to remote", peer = b.id
|
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:
|
|
|
|
trace "Exception broadcasting message to peer", peer = b.id, exc = exc.msg
|
|
|
|
|
|
|
|
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)
|