From 72cebff516b82c76f0c3da208de36d005d952a74 Mon Sep 17 00:00:00 2001 From: andri lim Date: Wed, 13 Mar 2019 09:07:05 +0700 Subject: [PATCH] add 'DownloadAndValidate' flag to premix downloader --- premix/downloader.nim | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/premix/downloader.nim b/premix/downloader.nim index ce5050bdb..157abc562 100644 --- a/premix/downloader.nim +++ b/premix/downloader.nim @@ -16,6 +16,7 @@ type DownloadFlags* = enum DownloadReceipts DownloadTxTrace + DownloadAndValidate proc request*(methodName: string, params: JsonNode): JsonNode = var client = newRpcHttpClient() @@ -87,14 +88,6 @@ proc requestBlock*(blockNumber: BlockNumber, flags: set[DownloadFlags] = {}): Bl if DownloadTxTrace in flags: result.traces = requestTxTraces(header) - let - txRoot = calcTxRoot(result.body.transactions).prefixHex - txRootOK = result.header.txRoot.prefixHex - ommersHash = rlpHash(result.body.uncles).prefixHex - ommersHashOK = result.header.ommersHash.prefixHex - headerHash = rlpHash(result.header).prefixHex - headerHashOK = header["hash"].getStr().toLowerAscii - if DownloadReceipts in flags: result.receipts = requestReceipts(header) let @@ -104,14 +97,23 @@ proc requestBlock*(blockNumber: BlockNumber, flags: set[DownloadFlags] = {}): Bl debug "wrong receipt root", receiptRoot, receiptRootOK, blockNumber raise newException(ValueError, "Error when validating receipt root") - if txRoot != txRootOK: - debug "wrong tx root", txRoot, txRootOK, blockNumber - raise newException(ValueError, "Error when validating tx root") + if DownloadAndValidate in flags: + let + txRoot = calcTxRoot(result.body.transactions).prefixHex + txRootOK = result.header.txRoot.prefixHex + ommersHash = rlpHash(result.body.uncles).prefixHex + ommersHashOK = result.header.ommersHash.prefixHex + headerHash = rlpHash(result.header).prefixHex + headerHashOK = header["hash"].getStr().toLowerAscii - if ommersHash != ommersHashOK: - debug "wrong ommers hash", ommersHash, ommersHashOK, blockNumber - raise newException(ValueError, "Error when validating ommers hash") + if txRoot != txRootOK: + debug "wrong tx root", txRoot, txRootOK, blockNumber + raise newException(ValueError, "Error when validating tx root") - if headerHash != headerHashOK: - debug "wrong header hash", headerHash, headerHashOK, blockNumber - raise newException(ValueError, "Error when validating block header hash") + if ommersHash != ommersHashOK: + debug "wrong ommers hash", ommersHash, ommersHashOK, blockNumber + raise newException(ValueError, "Error when validating ommers hash") + + if headerHash != headerHashOK: + debug "wrong header hash", headerHash, headerHashOK, blockNumber + raise newException(ValueError, "Error when validating block header hash")