Skip to content

Commit

Permalink
clean up json serialization (#735)
Browse files Browse the repository at this point in the history
serialize fewer non-eth types and more eth types - this module is
fraught with issues however since there's no one good "canonical"
encoding to choose - this goes back to the json ser framework lacking
good isolation between projects.
  • Loading branch information
arnetheduck authored Sep 30, 2024
1 parent 6bd6bae commit 00f5fb1
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions eth/common/eth_types_json_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@

{.push raises: [].}

import std/[times, net], json_serialization, nimcrypto/[hash, utils], ./eth_types
import std/typetraits, json_serialization, ./eth_types

export json_serialization
export json_serialization, eth_types

proc writeValue*(w: var JsonWriter, a: MDigest) {.raises: [IOError].} =
w.writeValue a.data.toHex(true)
# This module contains "convenience formatting" for logging `eth_types` - this
# formatting does not conform to any particular Ethereum-based standard - in
# particular, it does not conform to the JSON-RPC conventions which instead
# can be found in `nim-web3`.

proc writeValue*(w: var JsonWriter, a: Address) {.raises: [IOError].} =
w.writeValue $a

proc readValue*(
r: var JsonReader, a: var MDigest
r: var JsonReader, a: var Address
) {.inline, raises: [IOError, SerializationError].} =
try:
a = fromHex(type(a), r.readValue(string))
except ValueError:
raiseUnexpectedValue(r, "Hex string expected")

proc writeValue*(w: var JsonWriter, a: Hash32) {.raises: [IOError].} =
w.writeValue a.data.to0xHex()
w.writeValue $a

proc readValue*(
r: var JsonReader, a: var Hash32
Expand All @@ -33,7 +38,7 @@ proc readValue*(
raiseUnexpectedValue(r, "Hex string expected")

proc writeValue*(w: var JsonWriter, a: FixedBytes) {.raises: [IOError].} =
w.writeValue a.data.to0xHex()
w.writeValue $a

proc readValue*[N](
r: var JsonReader, a: var FixedBytes[N]
Expand All @@ -51,19 +56,13 @@ proc readValue*(
) {.inline, raises: [IOError, SerializationError].} =
value = parse(r.readValue(string), type(value))

proc writeValue*(w: var JsonWriter, value: StInt) {.raises: [IOError].} =
# The Ethereum Yellow Paper defines the RLP serialization only
# for unsigned integers:
{.error: "RLP serialization of signed integers is not allowed".}
discard

proc writeValue*(w: var JsonWriter, t: Time) {.inline, raises: [IOError].} =
w.writeValue t.toUnix()
proc writeValue*(w: var JsonWriter, t: EthTime) {.inline, raises: [IOError].} =
w.writeValue distinctBase(t)

proc readValue*(
r: var JsonReader, t: var Time
r: var JsonReader, t: var EthTime
) {.inline, raises: [IOError, SerializationError].} =
t = fromUnix r.readValue(int)
t = EthTime r.readValue(uint64)

proc writeValue*(w: var JsonWriter, value: BlockHashOrNumber) {.raises: [IOError].} =
w.writeValue $value
Expand Down

0 comments on commit 00f5fb1

Please sign in to comment.