2020-05-12 22:35:40 +00:00
|
|
|
type
|
|
|
|
LibP2PInputStream = ref object of InputStream
|
|
|
|
conn: Connection
|
|
|
|
|
|
|
|
const
|
|
|
|
closingErrMsg = "Failed to close LibP2P stream"
|
|
|
|
readingErrMsg = "Failed to read from LibP2P stream"
|
|
|
|
|
|
|
|
proc libp2pReadOnce(s: LibP2PInputStream,
|
|
|
|
dst: pointer, dstLen: Natural): Future[Natural] {.async.} =
|
|
|
|
fsTranslateErrors readingErrMsg:
|
|
|
|
try:
|
|
|
|
return implementSingleRead(s.buffers, dst, dstLen, ReadFlags {},
|
|
|
|
readStartAddr, readLen):
|
|
|
|
await s.conn.readOnce(readStartAddr, readLen)
|
|
|
|
except LPStreamEOFError:
|
|
|
|
s.buffers.eofReached = true
|
|
|
|
|
|
|
|
proc libp2pCloseWait(s: LibP2PInputStream) {.async.} =
|
|
|
|
fsTranslateErrors closingErrMsg:
|
|
|
|
await safeClose(s.conn)
|
|
|
|
|
|
|
|
# TODO: Use the Raising type here
|
|
|
|
let libp2pInputVTable = InputStreamVTable(
|
|
|
|
readSync: proc (s: InputStream, dst: pointer, dstLen: Natural): Natural
|
|
|
|
{.nimcall, gcsafe, raises: [IOError, Defect].} =
|
|
|
|
doAssert(false, "synchronous reading is not allowed")
|
|
|
|
,
|
|
|
|
readAsync: proc (s: InputStream, dst: pointer, dstLen: Natural): Future[Natural]
|
|
|
|
{.nimcall, gcsafe, raises: [IOError, Defect].} =
|
|
|
|
fsTranslateErrors "Unexpected exception from merely forwarding a future":
|
|
|
|
return libp2pReadOnce(Libp2pInputStream s, dst, dstLen)
|
|
|
|
,
|
|
|
|
closeSync: proc (s: InputStream)
|
|
|
|
{.nimcall, gcsafe, raises: [IOError, Defect].} =
|
|
|
|
fsTranslateErrors closingErrMsg:
|
|
|
|
s.closeFut = Libp2pInputStream(s).conn.close()
|
|
|
|
,
|
|
|
|
closeAsync: proc (s: InputStream): Future[void]
|
|
|
|
{.nimcall, gcsafe, raises: [IOError, Defect].} =
|
|
|
|
fsTranslateErrors "Unexpected exception from merely forwarding a future":
|
|
|
|
return libp2pCloseWait(Libp2pInputStream s)
|
|
|
|
)
|
|
|
|
|
|
|
|
func libp2pInput*(conn: Connection,
|
|
|
|
pageSize = defaultPageSize): AsyncInputStream =
|
|
|
|
AsyncInputStream LibP2PInputStream(
|
|
|
|
vtable: vtableAddr libp2pInputVTable,
|
|
|
|
buffers: initPageBuffers(pageSize),
|
|
|
|
conn: conn)
|
|
|
|
|
2020-05-12 22:37:07 +00:00
|
|
|
proc readSizePrefix(s: AsyncInputStream,
|
|
|
|
maxSize: uint32): Future[NetRes[uint32]] {.async.} =
|
|
|
|
var parser: VarintParser[uint32, ProtoBuf]
|
2020-05-12 22:35:40 +00:00
|
|
|
while s.readable:
|
|
|
|
case parser.feedByte(s.read)
|
|
|
|
of Done:
|
|
|
|
let res = parser.getResult
|
|
|
|
if res > maxSize:
|
2020-05-12 22:37:07 +00:00
|
|
|
return neterr SizePrefixOverflow
|
2020-05-12 22:35:40 +00:00
|
|
|
else:
|
2020-05-12 22:37:07 +00:00
|
|
|
return ok res
|
2020-05-12 22:35:40 +00:00
|
|
|
of Overflow:
|
2020-05-12 22:37:07 +00:00
|
|
|
return neterr SizePrefixOverflow
|
2020-05-12 22:35:40 +00:00
|
|
|
of Incomplete:
|
|
|
|
continue
|
|
|
|
|
2020-05-12 22:37:07 +00:00
|
|
|
return neterr UnexpectedEOF
|
|
|
|
|
|
|
|
proc readSszValue(s: AsyncInputStream,
|
|
|
|
size: int,
|
|
|
|
MsgType: type): Future[NetRes[MsgType]] {.async.} =
|
|
|
|
if s.readable(size):
|
2020-05-12 22:35:40 +00:00
|
|
|
s.withReadableRange(size, r):
|
|
|
|
return r.readValue(SSZ, MsgType)
|
|
|
|
else:
|
2020-05-12 22:37:07 +00:00
|
|
|
return neterr UnexpectedEOF
|
|
|
|
|
|
|
|
proc readChunkPayload(s: AsyncInputStream,
|
|
|
|
noSnappy: bool,
|
|
|
|
MsgType: type): Future[NetRes[MsgType]] {.async.} =
|
|
|
|
let prefix = await readSizePrefix(s, MAX_CHUNK_SIZE)
|
|
|
|
let size = if prefix.isOk: prefix.value.int
|
|
|
|
else: return err(prefix.error)
|
|
|
|
|
|
|
|
if size > 0:
|
|
|
|
let processingFut = if noSnappy:
|
|
|
|
readSszValue(s, size, MsgType)
|
|
|
|
else:
|
|
|
|
executePipeline(uncompressFramedStream,
|
|
|
|
readSszValue(size, MsgType))
|
|
|
|
|
|
|
|
return await processingFut
|
2020-05-12 22:35:40 +00:00
|
|
|
else:
|
2020-05-12 22:37:07 +00:00
|
|
|
return neterr ZeroSizePrefix
|
|
|
|
|
|
|
|
proc readResponseChunk(s: AsyncInputStream,
|
|
|
|
noSnappy: bool,
|
|
|
|
MsgType: typedesc): Future[NetRes[MsgType]] {.async.} =
|
|
|
|
let responseCodeByte = s.read
|
|
|
|
|
|
|
|
static: assert ResponseCode.low.ord == 0
|
|
|
|
if responseCodeByte > ResponseCode.high.byte:
|
|
|
|
return neterr InvalidResponseCode
|
|
|
|
|
|
|
|
let responseCode = ResponseCode responseCodeByte
|
|
|
|
case responseCode:
|
|
|
|
of InvalidRequest, ServerError:
|
|
|
|
let errorMsgChunk = await readChunkPayload(s, noSnappy, string)
|
|
|
|
let errorMsg = if errorMsgChunk.isOk: errorMsgChunk.value
|
|
|
|
else: return err(errorMsgChunk.error)
|
|
|
|
return err Eth2NetworkingError(kind: ReceivedErrorResponse,
|
|
|
|
responseCode: responseCode,
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
errorMsg: toPrettyString(errorMsg.asSeq()))
|
2020-05-12 22:37:07 +00:00
|
|
|
of Success:
|
|
|
|
discard
|
|
|
|
|
|
|
|
return await readChunkPayload(s, noSnappy, MsgType)
|
2020-05-12 22:35:40 +00:00
|
|
|
|
|
|
|
proc readResponse(s: AsyncInputStream,
|
2020-05-12 22:37:07 +00:00
|
|
|
noSnappy: bool,
|
|
|
|
MsgType: type): Future[NetRes[MsgType]] {.gcsafe, async.} =
|
2020-05-12 22:35:40 +00:00
|
|
|
when MsgType is seq:
|
|
|
|
type E = ElemType(MsgType)
|
|
|
|
var results: MsgType
|
2020-05-12 22:37:07 +00:00
|
|
|
while s.readable:
|
|
|
|
results.add(? await s.readResponseChunk(noSnappy, E))
|
|
|
|
return ok results
|
2020-05-12 22:35:40 +00:00
|
|
|
else:
|
2020-05-12 22:37:07 +00:00
|
|
|
if s.readable:
|
|
|
|
return await s.readResponseChunk(noSnappy, MsgType)
|
|
|
|
else:
|
|
|
|
return neterr UnexpectedEOF
|
2020-05-12 22:35:40 +00:00
|
|
|
|