add 'DownloadAndValidate' flag to premix downloader

This commit is contained in:
andri lim 2019-03-13 09:07:05 +07:00 committed by zah
parent 62082fb5f1
commit 72cebff516
1 changed files with 19 additions and 17 deletions

View File

@ -16,6 +16,7 @@ type
DownloadFlags* = enum DownloadFlags* = enum
DownloadReceipts DownloadReceipts
DownloadTxTrace DownloadTxTrace
DownloadAndValidate
proc request*(methodName: string, params: JsonNode): JsonNode = proc request*(methodName: string, params: JsonNode): JsonNode =
var client = newRpcHttpClient() var client = newRpcHttpClient()
@ -87,14 +88,6 @@ proc requestBlock*(blockNumber: BlockNumber, flags: set[DownloadFlags] = {}): Bl
if DownloadTxTrace in flags: if DownloadTxTrace in flags:
result.traces = requestTxTraces(header) 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: if DownloadReceipts in flags:
result.receipts = requestReceipts(header) result.receipts = requestReceipts(header)
let let
@ -104,14 +97,23 @@ proc requestBlock*(blockNumber: BlockNumber, flags: set[DownloadFlags] = {}): Bl
debug "wrong receipt root", receiptRoot, receiptRootOK, blockNumber debug "wrong receipt root", receiptRoot, receiptRootOK, blockNumber
raise newException(ValueError, "Error when validating receipt root") raise newException(ValueError, "Error when validating receipt root")
if txRoot != txRootOK: if DownloadAndValidate in flags:
debug "wrong tx root", txRoot, txRootOK, blockNumber let
raise newException(ValueError, "Error when validating tx root") 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: if txRoot != txRootOK:
debug "wrong ommers hash", ommersHash, ommersHashOK, blockNumber debug "wrong tx root", txRoot, txRootOK, blockNumber
raise newException(ValueError, "Error when validating ommers hash") raise newException(ValueError, "Error when validating tx root")
if headerHash != headerHashOK: if ommersHash != ommersHashOK:
debug "wrong header hash", headerHash, headerHashOK, blockNumber debug "wrong ommers hash", ommersHash, ommersHashOK, blockNumber
raise newException(ValueError, "Error when validating block header hash") 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")