implement validate block unchanged

This commit is contained in:
andri lim 2019-09-07 17:32:06 +07:00 committed by zah
parent 594c3b8d08
commit 7c65b8a2b2

View File

@ -207,7 +207,7 @@ proc parseTester(fixture: JsonNode, testStatusIMPL: var TestStatus): Tester =
except ValueError: except ValueError:
result.good = false result.good = false
# TODO: we don't have these VM implementation yet, skip it # TODO: implement missing VM
if network in ["Constantinople", "HomesteadToDaoAt5"]: if network in ["Constantinople", "HomesteadToDaoAt5"]:
result.good = false result.good = false
@ -247,11 +247,11 @@ proc assignBlockRewards(minedBlock: PlainBlock, vmState: BaseVMState, fork: Fork
if minedBlock.header.txRoot != txRoot: if minedBlock.header.txRoot != txRoot:
raise newException(ValidationError, "wrong txRoot") raise newException(ValidationError, "wrong txRoot")
proc processBlock(vmState: BaseVMState, preminedBlock: PlainBlock, fork: Fork) = proc processBlock(vmState: BaseVMState, minedBlock: PlainBlock, fork: Fork) =
vmState.receipts = newSeq[Receipt](preminedBlock.transactions.len) vmState.receipts = newSeq[Receipt](minedBlock.transactions.len)
vmState.cumulativeGasUsed = 0 vmState.cumulativeGasUsed = 0
for txIndex, tx in preminedBlock.transactions: for txIndex, tx in minedBlock.transactions:
var sender: EthAddress var sender: EthAddress
if tx.getSender(sender): if tx.getSender(sender):
let gasUsed = processTransaction(tx, sender, vmState, fork) let gasUsed = processTransaction(tx, sender, vmState, fork)
@ -259,21 +259,31 @@ proc processBlock(vmState: BaseVMState, preminedBlock: PlainBlock, fork: Fork) =
raise newException(ValidationError, "could not get sender") raise newException(ValidationError, "could not get sender")
vmState.receipts[txIndex] = makeReceipt(vmState, fork) vmState.receipts[txIndex] = makeReceipt(vmState, fork)
# TODO: change this preminedBlock to minedBlock assignBlockRewards(minedBlock, vmState, fork, vmState.chainDB)
assignBlockRewards(preminedBlock, vmState, fork, vmState.chainDB)
func validateBlockUnchanged(a, b: PlainBlock): bool =
result = rlp.encode(a) == rlp.encode(b)
func validateBlock(blck: PlainBlock): bool =
# TODO: implement block validation
result = true
proc importBlock(chainDB: BaseChainDB, preminedBlock: PlainBlock, fork: Fork, validation = true): PlainBlock = proc importBlock(chainDB: BaseChainDB, preminedBlock: PlainBlock, fork: Fork, validation = true): PlainBlock =
let parentHeader = chainDB.getBlockHeader(preminedBlock.header.parentHash) let parentHeader = chainDB.getBlockHeader(preminedBlock.header.parentHash)
let baseHeaderForImport = generateHeaderFromParentHeader(parentHeader, let baseHeaderForImport = generateHeaderFromParentHeader(parentHeader,
preminedBlock.header.coinbase, fork, some(preminedBlock.header.timestamp), @[]) preminedBlock.header.coinbase, fork, some(preminedBlock.header.timestamp), @[])
deepCopy(result, preminedBlock)
var vmState = newBaseVMState(parentHeader.stateRoot, baseHeaderForImport, chainDB) var vmState = newBaseVMState(parentHeader.stateRoot, baseHeaderForImport, chainDB)
processBlock(vmState, preminedBlock, fork) processBlock(vmState, result, fork)
#if validation: deepCopy(result.header, vmState.blockHeader)
#validate_imported_block_unchanged(importedBlock, preminedBlock)
#self.validate_block(importedBlock) if validation:
if not validateBlockUnchanged(result, preminedBlock):
raise newException(ValidationError, "block changed")
if not validateBlock(result):
raise newException(ValidationError, "invalid block")
discard chainDB.persistHeaderToDb(preminedBlock.header) discard chainDB.persistHeaderToDb(preminedBlock.header)
@ -298,17 +308,15 @@ proc runTester(tester: Tester, chainDB: BaseChainDB, testStatusIMPL: var TestSta
let (preminedBlock, minedBlock, blockRlp) = applyFixtureBlockToChain( let (preminedBlock, minedBlock, blockRlp) = applyFixtureBlockToChain(
testerBlock, chainDB, fork, validation = false) # we manually validate below testerBlock, chainDB, fork, validation = false) # we manually validate below
# assert_mined_block_unchanged(preminedBlock, minedBlock) check validateBlock(preminedBlock) == true
# chain.validate_block(preminedBlock) #else:
# else: # try:
# try: # apply_fixture_block_to_chain(block_fixture, chain)
# apply_fixture_block_to_chain(block_fixture, chain) # except (TypeError, rlp.DecodingError, rlp.DeserializationError, ValidationError) as err:
# except (TypeError, rlp.DecodingError, rlp.DeserializationError, ValidationError) as err: # # failure is expected on this bad block
# # failure is expected on this bad block # pass
# pass # else:
# else: # raise AssertionError("Block should have caused a validation error")
# raise AssertionError("Block should have caused a validation error")
#
proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus, debugMode = false) = proc testFixture(node: JsonNode, testStatusIMPL: var TestStatus, debugMode = false) =
# 1 - mine the genesis block # 1 - mine the genesis block