fix `{.raises.}` annotation to `writeValue` (#631)

`writeValue` doesn't raise `SerializationError`, so the `{.push.}`
is not optimal. Move `{.raises.}` to each `proc`, same as other modules.
This commit is contained in:
Etan Kissling 2023-08-19 19:03:10 +02:00 committed by GitHub
parent 074edff1b4
commit 894ec07d9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 13 deletions

View File

@ -4,6 +4,8 @@
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). # * 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. # at your option. This file may not be copied, modified, or distributed except according to those terms.
{.push raises: [].}
import import
std/[times, net], std/[times, net],
json_serialization, nimcrypto/[hash, utils], json_serialization, nimcrypto/[hash, utils],
@ -12,38 +14,44 @@ import
export export
json_serialization json_serialization
{.push raises: [SerializationError, IOError].} proc writeValue*(w: var JsonWriter, a: MDigest) {.raises: [IOError].} =
proc writeValue*(w: var JsonWriter, a: MDigest) =
w.writeValue a.data.toHex(true) w.writeValue a.data.toHex(true)
proc readValue*(r: var JsonReader, a: var MDigest) {.inline.} = proc readValue*(
r: var JsonReader, a: var MDigest
) {.inline, raises: [IOError, SerializationError].} =
try: try:
a = fromHex(type(a), r.readValue(string)) a = fromHex(type(a), r.readValue(string))
except ValueError: except ValueError:
raiseUnexpectedValue(r, "Hex string expected") raiseUnexpectedValue(r, "Hex string expected")
proc writeValue*(w: var JsonWriter, value: StUint) {.inline.} = proc writeValue*(
w: var JsonWriter, value: StUint) {.inline, raises: [IOError].} =
w.writeValue $value w.writeValue $value
proc readValue*(r: var JsonReader, value: var StUint) {.inline.} = proc readValue*(
r: var JsonReader, value: var StUint
) {.inline, raises: [IOError, SerializationError].} =
value = parse(r.readValue(string), type(value)) value = parse(r.readValue(string), type(value))
proc writeValue*(w: var JsonWriter, value: StInt) = proc writeValue*(w: var JsonWriter, value: StInt) {.raises: [IOError].} =
# The Ethereum Yellow Paper defines the RLP serialization only # The Ethereum Yellow Paper defines the RLP serialization only
# for unsigned integers: # for unsigned integers:
{.error: "RLP serialization of signed integers is not allowed".} {.error: "RLP serialization of signed integers is not allowed".}
discard discard
proc writeValue*(w: var JsonWriter, t: Time) {.inline.} = proc writeValue*(w: var JsonWriter, t: Time) {.inline, raises: [IOError].} =
w.writeValue t.toUnix() w.writeValue t.toUnix()
proc readValue*(r: var JsonReader, t: var Time) {.inline.} = proc readValue*(
r: var JsonReader, t: var Time
) {.inline, raises: [IOError, SerializationError].} =
t = fromUnix r.readValue(int) t = fromUnix r.readValue(int)
# TODO: remove this once case object are fully supported # TODO: remove this once case object are fully supported
# by the serialization library # by the serialization library
proc writeValue*(w: var JsonWriter, value: HashOrNum) = proc writeValue*(
w: var JsonWriter, value: HashOrNum) {.raises: [IOError].} =
w.beginRecord(HashOrNum) w.beginRecord(HashOrNum)
w.writeField("isHash", value.isHash) w.writeField("isHash", value.isHash)
if value.isHash: if value.isHash:
@ -52,12 +60,14 @@ proc writeValue*(w: var JsonWriter, value: HashOrNum) =
w.writeField("number", value.number) w.writeField("number", value.number)
w.endRecord() w.endRecord()
proc writeValue*(w: var JsonWriter, value: BlockHashOrNumber) = proc writeValue*(
w: var JsonWriter, value: BlockHashOrNumber) {.raises: [IOError].} =
w.writeValue $value w.writeValue $value
proc readValue*(r: var JsonReader, value: var BlockHashOrNumber) = proc readValue*(
r: var JsonReader, value: var BlockHashOrNumber
) {.raises: [IOError, SerializationError].} =
try: try:
value = init(BlockHashOrNumber, r.readValue(string)) value = init(BlockHashOrNumber, r.readValue(string))
except ValueError: except ValueError:
r.raiseUnexpectedValue("A hex-encoded block hash or a decimal block number expected") r.raiseUnexpectedValue("A hex-encoded block hash or a decimal block number expected")