2024-01-13 01:41:57 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-01-19 15:06:23 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-01-19 15:06:23 +00:00
|
|
|
|
|
|
|
import
|
2024-01-24 15:28:03 +00:00
|
|
|
std/[hashes, tables, net],
|
2024-02-28 17:31:45 +00:00
|
|
|
chronos,
|
|
|
|
chronicles,
|
|
|
|
confutils,
|
2022-01-19 15:06:23 +00:00
|
|
|
confutils/std/net as confNet,
|
2024-06-19 01:57:45 +00:00
|
|
|
stew/byteutils,
|
2022-01-19 15:06:23 +00:00
|
|
|
json_rpc/servers/httpserver,
|
|
|
|
eth/p2p/discoveryv5/protocol,
|
|
|
|
eth/p2p/discoveryv5/enr,
|
|
|
|
eth/utp/[utp_discv5_protocol, utp_router],
|
2024-10-16 12:18:02 +00:00
|
|
|
eth/common/keys,
|
2024-01-13 01:41:57 +00:00
|
|
|
../../rpc/rpc_discovery_api,
|
|
|
|
./utp_rpc_types
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
const defaultListenAddress* = (static parseIpAddress("127.0.0.1"))
|
2022-01-19 15:06:23 +00:00
|
|
|
|
|
|
|
type AppConf* = object
|
2024-02-28 17:31:45 +00:00
|
|
|
rpcPort* {.defaultValue: 7041, desc: "Json rpc port", name: "rpc-port".}: Port
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
udpPort* {.defaultValue: 7042, desc: "UDP listening port", name: "udp-port".}: Port
|
2022-01-19 15:06:23 +00:00
|
|
|
|
|
|
|
udpListenAddress* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
defaultValue: defaultListenAddress,
|
|
|
|
desc: "UDP listening address",
|
|
|
|
name: "udp-listen-address"
|
|
|
|
.}: IpAddress
|
2022-01-19 15:06:23 +00:00
|
|
|
|
|
|
|
rpcListenAddress* {.
|
2024-02-28 17:31:45 +00:00
|
|
|
defaultValue: defaultListenAddress,
|
|
|
|
desc: "RPC listening address",
|
|
|
|
name: "rpc-listen-address"
|
|
|
|
.}: IpAddress
|
2022-12-13 18:22:36 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc writeValue*(w: var JsonWriter[JrpcConv], v: Record) {.gcsafe, raises: [IOError].} =
|
2024-01-13 01:41:57 +00:00
|
|
|
w.writeValue(v.toURI())
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc readValue*(
|
|
|
|
r: var JsonReader[JrpcConv], val: var Record
|
|
|
|
) {.gcsafe, raises: [IOError, JsonReaderError].} =
|
2024-10-16 12:18:02 +00:00
|
|
|
val = enr.Record.fromURI(r.parseString()).valueOr:
|
2024-01-13 01:41:57 +00:00
|
|
|
r.raiseUnexpectedValue("Invalid ENR")
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2022-01-31 17:40:00 +00:00
|
|
|
proc installUtpHandlers(
|
2024-02-28 17:31:45 +00:00
|
|
|
srv: RpcHttpServer,
|
|
|
|
d: protocol.Protocol,
|
|
|
|
s: UtpDiscv5Protocol,
|
|
|
|
t: ref Table[SKey, UtpSocket[NodeAddress]],
|
2024-06-19 01:57:45 +00:00
|
|
|
) {.raises: [].} =
|
2022-01-31 17:40:00 +00:00
|
|
|
srv.rpc("utp_connect") do(r: enr.Record) -> SKey:
|
2024-06-27 15:59:08 +00:00
|
|
|
let node = Node.fromRecord(r)
|
|
|
|
let nodeAddress = NodeAddress.init(node).unsafeGet()
|
|
|
|
discard d.addNode(node)
|
|
|
|
let connResult = await s.connectTo(nodeAddress)
|
|
|
|
if (connResult.isOk()):
|
|
|
|
let socket = connResult.get()
|
|
|
|
let sKey = socket.socketKey.toSKey()
|
|
|
|
t[sKey] = socket
|
|
|
|
return sKey
|
2022-01-19 15:06:23 +00:00
|
|
|
else:
|
2024-06-27 15:59:08 +00:00
|
|
|
raise newException(ValueError, "Connection to node Failed.")
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2022-01-31 17:40:00 +00:00
|
|
|
srv.rpc("utp_write") do(k: SKey, b: string) -> bool:
|
2022-01-19 15:06:23 +00:00
|
|
|
let sock = t.getOrDefault(k)
|
|
|
|
let bytes = hexToSeqByte(b)
|
|
|
|
if sock != nil:
|
2022-01-31 17:40:00 +00:00
|
|
|
# TODO consider doing it async to avoid json-rpc timeouts in case of large writes
|
2022-01-19 15:06:23 +00:00
|
|
|
let res = await sock.write(bytes)
|
|
|
|
if res.isOk():
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
# TODO return correct errors instead of just true/false
|
|
|
|
return false
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Socket with provided key is missing")
|
|
|
|
|
2022-01-31 17:40:00 +00:00
|
|
|
srv.rpc("utp_get_connections") do() -> seq[SKey]:
|
2022-01-19 15:06:23 +00:00
|
|
|
var keys = newSeq[SKey]()
|
|
|
|
|
|
|
|
for k in t.keys:
|
|
|
|
keys.add(k)
|
2022-12-13 18:22:36 +00:00
|
|
|
|
2022-01-19 15:06:23 +00:00
|
|
|
return keys
|
|
|
|
|
2022-01-31 17:40:00 +00:00
|
|
|
srv.rpc("utp_read") do(k: SKey, n: int) -> string:
|
2022-01-19 15:06:23 +00:00
|
|
|
let sock = t.getOrDefault(k)
|
|
|
|
if sock != nil:
|
|
|
|
let res = await sock.read(n)
|
|
|
|
let asHex = res.toHex()
|
|
|
|
return asHex
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Socket with provided key is missing")
|
|
|
|
|
2022-01-31 17:40:00 +00:00
|
|
|
srv.rpc("utp_close") do(k: SKey) -> bool:
|
2022-01-19 15:06:23 +00:00
|
|
|
let sock = t.getOrDefault(k)
|
|
|
|
if sock != nil:
|
|
|
|
await sock.closeWait()
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "Socket with provided key is missing")
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
proc buildAcceptConnection(
|
|
|
|
t: ref Table[SKey, UtpSocket[NodeAddress]]
|
|
|
|
): AcceptConnectionCallback[NodeAddress] =
|
2022-01-19 15:06:23 +00:00
|
|
|
return (
|
2024-06-14 07:31:08 +00:00
|
|
|
proc(
|
|
|
|
server: UtpRouter[NodeAddress], client: UtpSocket[NodeAddress]
|
|
|
|
): Future[void] {.async: (raw: true, raises: []).} =
|
|
|
|
let fut = noCancel Future[void].Raising([CancelledError]).init(
|
|
|
|
"utp_test_app.AcceptConnectionCallback"
|
|
|
|
)
|
2022-01-19 15:06:23 +00:00
|
|
|
let key = client.socketKey.toSKey()
|
|
|
|
t[key] = client
|
|
|
|
fut.complete()
|
|
|
|
return fut
|
|
|
|
)
|
|
|
|
|
|
|
|
when isMainModule:
|
2022-01-25 09:19:16 +00:00
|
|
|
var table = newTable[SKey, UtpSocket[NodeAddress]]()
|
2022-01-19 15:06:23 +00:00
|
|
|
|
|
|
|
let rng = newRng()
|
|
|
|
|
|
|
|
{.pop.}
|
|
|
|
let conf = AppConf.load()
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
let
|
2022-01-19 15:06:23 +00:00
|
|
|
protName = "test-utp".toBytes()
|
|
|
|
la = initTAddress(conf.rpcListenAddress, conf.rpcPort)
|
|
|
|
key = PrivateKey.random(rng[])
|
|
|
|
discAddress = conf.udpListenAddress
|
|
|
|
srv = newRpcHttpServer(@[la], RpcRouter.init())
|
|
|
|
|
|
|
|
srv.start()
|
|
|
|
|
|
|
|
let d = newProtocol(
|
|
|
|
key,
|
2024-06-19 01:57:45 +00:00
|
|
|
Opt.some(discAddress),
|
|
|
|
Opt.none(Port),
|
|
|
|
Opt.some(conf.udpPort),
|
2022-01-19 15:06:23 +00:00
|
|
|
bootstrapRecords = @[],
|
2024-02-28 17:31:45 +00:00
|
|
|
bindIp = discAddress,
|
|
|
|
bindPort = conf.udpPort,
|
2022-01-19 15:06:23 +00:00
|
|
|
enrAutoUpdate = true,
|
2024-02-28 17:31:45 +00:00
|
|
|
rng = rng,
|
|
|
|
)
|
2022-01-19 15:06:23 +00:00
|
|
|
|
|
|
|
d.open()
|
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
let
|
2022-01-19 15:06:23 +00:00
|
|
|
cfg = SocketConfig.init(incomingSocketReceiveTimeout = none[Duration]())
|
2024-02-28 17:31:45 +00:00
|
|
|
utp = UtpDiscv5Protocol.new(
|
|
|
|
d, protName, buildAcceptConnection(table), socketConfig = cfg
|
|
|
|
)
|
2022-01-19 15:06:23 +00:00
|
|
|
|
2022-01-31 17:40:00 +00:00
|
|
|
# needed for some of the discovery api: nodeInfo, setEnr, ping
|
|
|
|
srv.installDiscoveryApiHandlers(d)
|
|
|
|
|
|
|
|
srv.installUtpHandlers(d, utp, table)
|
2022-01-19 15:06:23 +00:00
|
|
|
|
|
|
|
runForever()
|