premix: fixes premix parser dan graphql_downloader

premix parser:
- fix incorrect tx type comparison
- fix chainId parser to support both graphql and json returned data

graphql_downloadse:
- allow downloader to skip parsing transactions
This commit is contained in:
jangko 2021-09-26 10:38:41 +07:00
parent 8e26acc4eb
commit 8552dda7bb
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 23 additions and 11 deletions

View File

@ -53,6 +53,7 @@ query getBlock($blockNumber: Long!) {
address
storageKeys
}
index
}
}
}
@ -68,7 +69,7 @@ proc fromJson(_: type ChainId, n: JsonNode, name: string): ChainId =
fromJson(n, name, chainId)
ChainId(chainId)
proc requestBlock*(blockNumber: BlockNumber): Block =
proc requestBlock*(blockNumber: BlockNumber, parseTx = true): Block =
let address = initTAddress("127.0.0.1:8545")
let clientRes = GraphqlHttpClientRef.new(address)
if clientRes.isErr:
@ -92,12 +93,13 @@ proc requestBlock*(blockNumber: BlockNumber): Block =
let chainId = ChainId.fromJson(n["data"], "chainID")
result.header = parseBlockHeader(nh)
let txs = nh["transactions"]
for txn in txs:
var tx = parseTransaction(txn)
tx.chainId = chainId
validateTxSenderAndHash(txn, tx)
result.body.transactions.add tx
if parseTx:
let txs = nh["transactions"]
for txn in txs:
var tx = parseTransaction(txn)
tx.chainId = chainId
validateTxSenderAndHash(txn, tx)
result.body.transactions.add tx
let uncles = nh["ommers"]
for un in uncles:

View File

@ -84,6 +84,13 @@ proc fromJson*[T](n: JsonNode, name: string, x: var Option[T]) =
n.fromJson(name, val)
x = some(val)
proc fromJson*(n: JsonNode, name: string, x: var TxType) =
let node = n[name]
if node.kind == JInt:
x = TxType(node.getInt)
else:
x = hexToInt(node.getStr(), int).TxType
proc parseBlockHeader*(n: JsonNode): BlockHeader =
n.fromJson "parentHash", result.parentHash
n.fromJson "sha3Uncles", result.ommersHash
@ -130,14 +137,17 @@ proc parseTransaction*(n: JsonNode): Transaction =
n.fromJson "s", tx.S
if n["type"].kind != JNull:
tx.txType = TxType(n["type"].getInt)
n.fromJson "type", tx.txType
if tx.txType == TxEip1559:
if tx.txType >= TxEip1559:
n.fromJson "maxPriorityFeePerGas", tx.maxPriorityFee
n.fromJson "maxFeePerGas", tx.maxFee
if tx.txType == TxEip2930:
# chainId is set from top level query
if tx.txType >= TxEip2930:
if n.hasKey("chainId"):
let id = hexToInt(n["chainId"].getStr(), int)
tx.chainId = ChainId(id)
let accessList = n["accessList"]
if accessList.len > 0:
for acn in accessList: