Skip to content

Commit 81ee8ce

Browse files
advaita-sahajangko
andauthored
eth_config endpoint addition (#217)
* eth_config basic setup * Add custom serialization and add hash/forkid fields * remove importutils import * Don't update to pkg/results yet * import njs/pkg/results * update to Uint256 * update activation time to Quantity * allow nil return * adjust to new eip changes * use JsonNumber * add timestamp * shift to generalized primtive Number * incorporate suggestions --------- Co-authored-by: jangko <jangko128@gmail.com>
1 parent 304afd1 commit 81ee8ce

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

tests/test_json_marshalling.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ proc rand[T: Quantity](_: type T): T =
4040
discard randomBytes(res)
4141
T(distinctBase(T).fromBytesBE(res))
4242

43+
proc rand[T: Number](_: type T): T =
44+
var res: array[sizeof(T), byte]
45+
discard randomBytes(res)
46+
T(distinctBase(T).fromBytesBE(res))
47+
4348
proc rand[T: ChainId](_: type T): T =
4449
var res: array[8, byte]
4550
discard randomBytes(res)
@@ -222,6 +227,8 @@ suite "JSON-RPC Quantity":
222227
checkRandomObject(PayloadAttributes)
223228
checkRandomObject(GetPayloadResponse)
224229

230+
checkRandomObject(EthConfigObject)
231+
225232
test "check blockId":
226233
let a = RtBlockIdentifier(kind: bidNumber, number: 77.Quantity)
227234
let x = JrpcConv.encode(a)

web3/conversions.nim

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ BlockHeader.useDefaultSerializationIn JrpcConv
6262
BlockObject.useDefaultSerializationIn JrpcConv
6363
TransactionObject.useDefaultSerializationIn JrpcConv
6464
ReceiptObject.useDefaultSerializationIn JrpcConv
65+
BlobScheduleObject.useDefaultSerializationIn JrpcConv
66+
ConfigObject.useDefaultSerializationIn JrpcConv
67+
EthConfigObject.useDefaultSerializationIn JrpcConv
6568

6669
#------------------------------------------------------------------------------
6770
# engine_api_types
@@ -252,6 +255,16 @@ proc readValue*(r: var JsonReader[JrpcConv], val: var Hash32)
252255
wrapValueError:
253256
val = fromHex(Hash32, r.parseString())
254257

258+
proc writeValue*(w: var JsonWriter[JrpcConv], v: Number)
259+
{.gcsafe, raises: [IOError].} =
260+
w.streamElement(s):
261+
s.writeText distinctBase(v)
262+
263+
proc readValue*(r: var JsonReader[JrpcConv], val: var Number)
264+
{.gcsafe, raises: [IOError, JsonReaderError].} =
265+
wrapValueError:
266+
val = r.parseInt(uint64).Number
267+
255268
proc readValue*[F: CommonJsonFlavors](r: var JsonReader[F], val: var TypedTransaction)
256269
{.gcsafe, raises: [IOError, JsonReaderError].} =
257270
wrapValueError:
@@ -313,6 +326,16 @@ proc readValue*[F: CommonJsonFlavors](r: var JsonReader[F], val: var UInt256)
313326
wrapValueError:
314327
val = hexStr.parse(StUint[256], 16)
315328

329+
proc readValue*[F: CommonJsonFlavors](r: var JsonReader[F], val: var seq[PrecompilePair])
330+
{.gcsafe, raises: [IOError, SerializationError].} =
331+
for k,v in readObject(r, string, Address):
332+
val.add PrecompilePair(name: k, address: v)
333+
334+
proc readValue*[F: CommonJsonFlavors](r: var JsonReader[F], val: var seq[SystemContractPair])
335+
{.gcsafe, raises: [IOError, SerializationError].} =
336+
for k,v in readObject(r, string, Address):
337+
val.add SystemContractPair(name: k, address: v)
338+
316339
#------------------------------------------------------------------------------
317340
# Exclusive to JrpcConv
318341
#------------------------------------------------------------------------------
@@ -435,6 +458,20 @@ proc writeValue*(w: var JsonWriter[JrpcConv], v: Opt[seq[ReceiptObject]])
435458
else:
436459
w.writeValue JsonString("null")
437460

461+
proc writeValue*(w: var JsonWriter[JrpcConv], v: seq[PrecompilePair])
462+
{.gcsafe, raises: [IOError].} =
463+
w.beginObject()
464+
for x in v:
465+
w.writeMember(x.name, x.address)
466+
w.endObject()
467+
468+
proc writeValue*(w: var JsonWriter[JrpcConv], v: seq[SystemContractPair])
469+
{.gcsafe, raises: [IOError].} =
470+
w.beginObject()
471+
for x in v:
472+
w.writeMember(x.name, x.address)
473+
w.endObject()
474+
438475
proc writeValue*(w: var JsonWriter[JrpcConv], v: TransactionArgs)
439476
{.gcsafe, raises: [IOError].} =
440477
mixin writeValue

web3/eth_api.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ createRpcSigsFromNim(RpcClient):
7070
proc eth_getFilterLogs(filterId: string): JsonNode
7171
proc eth_getLogs(filterOptions: FilterOptions): seq[LogObject]
7272
proc eth_chainId(): UInt256
73+
proc eth_config(): EthConfigObject
7374

7475
proc eth_getWork(): seq[UInt256]
7576
proc eth_submitWork(nonce: uint64, powHash: Hash32, mixDigest: Hash32): bool

web3/eth_api_types.nim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,32 @@ type
262262
blobGasUsedRatio*: seq[float64]
263263
reward*: Opt[seq[FeeHistoryReward]]
264264

265+
BlobScheduleObject* = object
266+
baseFeeUpdateFraction*: Number
267+
max*: Number
268+
target*: Number
269+
270+
PrecompilePair* = object
271+
address*: Address
272+
name*: string
273+
274+
SystemContractPair* = object
275+
name*: string
276+
address*: Address
277+
278+
ConfigObject* = object
279+
activationTime*: Number
280+
blobSchedule*: BlobScheduleObject
281+
chainId*: UInt256
282+
forkId*: Bytes4
283+
precompiles*: seq[PrecompilePair]
284+
systemContracts*: seq[SystemContractPair]
285+
286+
EthConfigObject* = ref object
287+
current*: ConfigObject
288+
next*: Opt[ConfigObject]
289+
last*: Opt[ConfigObject]
290+
265291
func blockId*(n: uint64): RtBlockIdentifier =
266292
RtBlockIdentifier(kind: bidNumber, number: Quantity n)
267293

web3/primitives.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ type
4343
# Quantity is use in lieu of an ordinary `uint64` to avoid the default
4444
# format that comes with json_serialization
4545

46+
Number* = distinct uint64
47+
# Number is use in lieu of an ordinary `uint64` to avoid the default
48+
# format that comes with json_serialization
49+
4650
Blob* = FixedBytes[FIELD_ELEMENTS_PER_BLOB * 32]
4751

4852
template `==`*[minLen, maxLen](a, b: DynamicBytes[minLen, maxLen]): bool =

0 commit comments

Comments
 (0)