Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify generic constraint of rpc and chronicles encoders #120

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions web3/conversions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -163,72 +163,74 @@ proc writeHexValue(w: var JsonWriter, v: openArray[byte])
# Well, both rpc and chronicles share the same encoding of these types
#------------------------------------------------------------------------------

proc writeValue*[F: JrpcConv or DefaultFlavor](w: var JsonWriter[F], v: DynamicBytes)
type CommonJsonFlavors = JrpcConv | DefaultFlavor

proc writeValue*[F: CommonJsonFlavors](w: var JsonWriter[F], v: DynamicBytes)
{.gcsafe, raises: [IOError].} =
writeHexValue w, distinctBase(v)

proc writeValue*[F: JrpcConv or DefaultFlavor, N](w: var JsonWriter[F], v: FixedBytes[N])
proc writeValue*[F: CommonJsonFlavors, N](w: var JsonWriter[F], v: FixedBytes[N])
{.gcsafe, raises: [IOError].} =
writeHexValue w, distinctBase(v)

proc writeValue*[F: JrpcConv or DefaultFlavor](w: var JsonWriter[F], v: Address)
proc writeValue*[F: CommonJsonFlavors](w: var JsonWriter[F], v: Address)
{.gcsafe, raises: [IOError].} =
writeHexValue w, distinctBase(v)

proc writeValue*[F: JrpcConv or DefaultFlavor](w: var JsonWriter[F], v: TypedTransaction)
proc writeValue*[F: CommonJsonFlavors](w: var JsonWriter[F], v: TypedTransaction)
{.gcsafe, raises: [IOError].} =
writeHexValue w, distinctBase(v)

proc writeValue*[F: JrpcConv or DefaultFlavor](w: var JsonWriter[F], v: RlpEncodedBytes)
proc writeValue*[F: CommonJsonFlavors](w: var JsonWriter[F], v: RlpEncodedBytes)
{.gcsafe, raises: [IOError].} =
writeHexValue w, distinctBase(v)

proc writeValue*[F: JrpcConv or DefaultFlavor](w: var JsonWriter[F], v: Quantity)
proc writeValue*[F: CommonJsonFlavors](w: var JsonWriter[F], v: Quantity)
{.gcsafe, raises: [IOError].} =
w.stream.write "\"0x"
w.stream.toHex(distinctBase v)
w.stream.write "\""

proc readValue*[F: JrpcConv or DefaultFlavor](r: var JsonReader[F], val: var DynamicBytes)
proc readValue*[F: CommonJsonFlavors](r: var JsonReader[F], val: var DynamicBytes)
{.gcsafe, raises: [IOError, JsonReaderError].} =
wrapValueError:
val = fromHex(DynamicBytes, r.parseString())

proc readValue*[F: JrpcConv or DefaultFlavor, N](r: var JsonReader[F], val: var FixedBytes[N])
proc readValue*[F: CommonJsonFlavors, N](r: var JsonReader[F], val: var FixedBytes[N])
{.gcsafe, raises: [IOError, JsonReaderError].} =
wrapValueError:
val = fromHex(FixedBytes[N], r.parseString())

proc readValue*[F: JrpcConv or DefaultFlavor](r: var JsonReader[F], val: var Address)
proc readValue*[F: CommonJsonFlavors](r: var JsonReader[F], val: var Address)
{.gcsafe, raises: [IOError, JsonReaderError].} =
wrapValueError:
val = fromHex(Address, r.parseString())

proc readValue*[F: JrpcConv or DefaultFlavor](r: var JsonReader[F], val: var TypedTransaction)
proc readValue*[F: CommonJsonFlavors](r: var JsonReader[F], val: var TypedTransaction)
{.gcsafe, raises: [IOError, JsonReaderError].} =
wrapValueError:
let hexStr = r.parseString()
if hexStr != "0x":
# skip empty hex
val = TypedTransaction hexToSeqByte(hexStr)

proc readValue*[F: JrpcConv or DefaultFlavor](r: var JsonReader[F], val: var RlpEncodedBytes)
proc readValue*[F: CommonJsonFlavors](r: var JsonReader[F], val: var RlpEncodedBytes)
{.gcsafe, raises: [IOError, JsonReaderError].} =
wrapValueError:
let hexStr = r.parseString()
if hexStr != "0x":
# skip empty hex
val = RlpEncodedBytes hexToSeqByte(hexStr)

proc readValue*[F: JrpcConv or DefaultFlavor](r: var JsonReader[F], val: var Quantity)
proc readValue*[F: CommonJsonFlavors](r: var JsonReader[F], val: var Quantity)
{.gcsafe, raises: [IOError, JsonReaderError].} =
let hexStr = r.parseString()
if hexStr.invalidQuantityPrefix:
r.raiseUnexpectedValue("Quantity value has invalid leading 0")
wrapValueError:
val = Quantity parseHexInt(hexStr)

proc readValue*[F: JrpcConv or DefaultFlavor](r: var JsonReader[F], val: var PayloadExecutionStatus)
proc readValue*[F: CommonJsonFlavors](r: var JsonReader[F], val: var PayloadExecutionStatus)
{.gcsafe, raises: [IOError, JsonReaderError].} =
const enumStrings = static: getEnumStringTable(PayloadExecutionStatus)

Expand All @@ -241,7 +243,7 @@ proc readValue*[F: JrpcConv or DefaultFlavor](r: var JsonReader[F], val: var Pay
except KeyError:
r.raiseUnexpectedValue("Failed to parse PayloadExecutionStatus")

proc writeValue*[F: JrpcConv or DefaultFlavor](w: var JsonWriter[F], v: PayloadExecutionStatus)
proc writeValue*[F: CommonJsonFlavors](w: var JsonWriter[F], v: PayloadExecutionStatus)
{.gcsafe, raises: [IOError].} =
w.writeValue($v)

Expand Down