mirror of
https://github.com/logos-storage/logos-storage-nim-validator.git
synced 2026-01-04 22:43:10 +00:00
open and close network connections
This commit is contained in:
parent
dbdc837e0f
commit
7a987a57cd
@ -7,12 +7,21 @@ license = "MIT"
|
||||
requires "stew#a6e198132097fb544d04959aeb3b839e1408f942"
|
||||
requires "faststreams#cf8d4d22636b8e514caf17e49f9c786ac56b0e85"
|
||||
requires "serialization#2086c99608b4bf472e1ef5fe063710f280243396"
|
||||
requires "httputils#8bb1acbaa4b86eb866145b0d468eff64a57d1897"
|
||||
|
||||
# pinning non-versioned dependencies
|
||||
requires "stint#ae665d6546c57b4acaf194e9a8e33ebb6aab5213"
|
||||
requires "protobuf_serialization#5a31137a82c2b6a989c9ed979bb636c7a49f570e"
|
||||
requires "blscurve#de2d3c79264bba18dbea469c8c5c4b3bb3c8bc55"
|
||||
|
||||
# versioned dependencies of dependencies
|
||||
requires "results >= 0.5.1 & < 0.6.0"
|
||||
requires "bearssl >= 0.2.5 & < 0.3.0"
|
||||
|
||||
# versioned dependencies
|
||||
requires "https://github.com/codex-storage/nim-mysticeti >= 0.1.0 & < 0.2.0"
|
||||
requires "nimcrypto >= 0.6.2 & < 0.7.0"
|
||||
requires "chronos >= 4.0.3 & < 5.0.0"
|
||||
|
||||
# test dependencies
|
||||
taskRequires "test", "asynctest >= 0.5.2 & < 0.6.0"
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
import std/sequtils
|
||||
import pkg/stint
|
||||
import pkg/chronos
|
||||
import pkg/questionable
|
||||
import pkg/questionable/results
|
||||
|
||||
export sequtils
|
||||
export stint
|
||||
export chronos
|
||||
export questionable
|
||||
export results
|
||||
|
||||
7
codexvalidator/network.nim
Normal file
7
codexvalidator/network.nim
Normal file
@ -0,0 +1,7 @@
|
||||
import ./network/address
|
||||
import ./network/server
|
||||
import ./network/connection
|
||||
|
||||
export address
|
||||
export server
|
||||
export connection
|
||||
9
codexvalidator/network/address.nim
Normal file
9
codexvalidator/network/address.nim
Normal file
@ -0,0 +1,9 @@
|
||||
import ../basics
|
||||
|
||||
type NetworkAddress* = distinct TransportAddress
|
||||
|
||||
func init*(_: type NetworkAddress, address: string): ?!NetworkAddress =
|
||||
NetworkAddress(initTAddress(address)).catch()
|
||||
|
||||
func `==`*(a, b: NetworkAddress): bool {.borrow.}
|
||||
func `$`*(address: NetworkAddress): string {.borrow.}
|
||||
14
codexvalidator/network/connection.nim
Normal file
14
codexvalidator/network/connection.nim
Normal file
@ -0,0 +1,14 @@
|
||||
import ../basics
|
||||
import ./address
|
||||
|
||||
type NetworkConnection* = distinct StreamTransport
|
||||
|
||||
proc connect*(
|
||||
_: type NetworkConnection,
|
||||
address: NetworkAddress
|
||||
): Future[?!NetworkConnection] {.async:(raises:[]).} =
|
||||
NetworkConnection(await TransportAddress(address).connect()).catch()
|
||||
|
||||
proc close*(connection: NetworkConnection) {.async:(raises:[]).} =
|
||||
StreamTransport(connection).close()
|
||||
await noCancel StreamTransport(connection).join()
|
||||
14
codexvalidator/network/server.nim
Normal file
14
codexvalidator/network/server.nim
Normal file
@ -0,0 +1,14 @@
|
||||
import ../basics
|
||||
import ./address
|
||||
|
||||
type NetworkServer* = distinct StreamServer
|
||||
|
||||
proc open*(_: type NetworkServer): Future[?!NetworkServer] {.async:(raises:[]).} =
|
||||
NetworkServer(createStreamServer(Port(0))).catch()
|
||||
|
||||
proc address*(server: NetworkServer): ?!NetworkAddress =
|
||||
NetworkAddress(StreamServer(server).localAddress()).catch()
|
||||
|
||||
proc close*(server: NetworkServer) {.async:(raises:[]).} =
|
||||
StreamServer(server).close()
|
||||
await noCancel StreamServer(server).join()
|
||||
@ -1,15 +1,21 @@
|
||||
import std/unittest
|
||||
import std/sequtils
|
||||
export unittest
|
||||
import std/strutils
|
||||
export sequtils
|
||||
export strutils
|
||||
|
||||
import pkg/stint
|
||||
export stint
|
||||
|
||||
import pkg/chronos
|
||||
export chronos
|
||||
|
||||
import pkg/questionable
|
||||
import pkg/questionable/results
|
||||
export questionable
|
||||
export results
|
||||
|
||||
import pkg/asynctest/chronos/unittest
|
||||
export unittest
|
||||
|
||||
import ./examples
|
||||
export examples
|
||||
|
||||
17
tests/codexvalidator/testNetwork.nim
Normal file
17
tests/codexvalidator/testNetwork.nim
Normal file
@ -0,0 +1,17 @@
|
||||
import ./basics
|
||||
import codexvalidator/network
|
||||
|
||||
suite "Network communication":
|
||||
|
||||
test "a connection can be made to a server":
|
||||
let server = !await NetworkServer.open()
|
||||
let address = !server.address
|
||||
let connection = !await NetworkConnection.connect(address)
|
||||
await connection.close()
|
||||
await server.close()
|
||||
|
||||
test "connect can fail":
|
||||
let address = !NetworkAddress.init("127.0.0.1:1011") # port reserved by IANA
|
||||
let connection = await NetworkConnection.connect(address)
|
||||
check connection.isFailure
|
||||
check connection.error.msg.contains("Connection refused")
|
||||
@ -6,5 +6,6 @@ import ./codexvalidator/transaction/testHashing
|
||||
import ./codexvalidator/transaction/testSigning
|
||||
import ./codexvalidator/blocks/testBlock
|
||||
import ./codexvalidator/blocks/testSerialization
|
||||
import ./codexvalidator/testNetwork
|
||||
|
||||
{.warning[UnusedImport]:off.}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user