This commit is contained in:
andri lim 2018-12-25 12:58:55 +07:00 committed by zah
parent a6bf970b1b
commit 3b5562d85b
3 changed files with 39 additions and 38 deletions

View File

@ -44,13 +44,13 @@ method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarr
let head = c.db.getCanonicalHead() let head = c.db.getCanonicalHead()
let vmState = if headers[i].txRoot != BLANK_ROOT_HASH: newBaseVMState(head, c.db) let vmState = if headers[i].txRoot != BLANK_ROOT_HASH: newBaseVMState(head, c.db)
else: nil else: nil
let success = processBlock(c.db, head, headers[i], bodies[i], vmState) let validationResult = processBlock(c.db, head, headers[i], bodies[i], vmState)
when not defined(release): when not defined(release):
if not success: if validationResult == ValidationResult.Error:
dumpDebuggingMetaData(c.db, headers[i], bodies[i]) dumpDebuggingMetaData(c.db, headers[i], bodies[i])
assert(success) assert(validationResult == ValidationResult.OK)
discard c.db.persistHeaderToDb(headers[i]) discard c.db.persistHeaderToDb(headers[i])
if c.db.getCanonicalHead().blockHash != headers[i].blockHash: if c.db.getCanonicalHead().blockHash != headers[i].blockHash:
@ -58,6 +58,6 @@ method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarr
return ValidationResult.Error return ValidationResult.Error
c.db.persistTransactions(headers[i].blockNumber, bodies[i].transactions) c.db.persistTransactions(headers[i].blockNumber, bodies[i].transactions)
c.db.persistReceipts(receipts) c.db.persistReceipts(vmState.receipts)
transaction.commit() transaction.commit()

View File

@ -1,8 +1,9 @@
import eth_common, ranges, chronicles, import eth_common, ranges, chronicles, eth_bloom,
../db/[db_chain, state_db], ../db/[db_chain, state_db],
../utils, ../constants, ../transaction, ../utils, ../constants, ../transaction,
../vm_state, ../vm_types, ../vm_state_transactions, ../vm_state, ../vm_types, ../vm_state_transactions,
../vm/[computation, interpreter_dispatch, message] ../vm/[computation, interpreter_dispatch, message],
../vm/interpreter/vm_forks
proc processTransaction*(db: var AccountStateDB, t: Transaction, sender: EthAddress, vmState: BaseVMState): UInt256 = proc processTransaction*(db: var AccountStateDB, t: Transaction, sender: EthAddress, vmState: BaseVMState): UInt256 =
## Process the transaction, write the results to db. ## Process the transaction, write the results to db.
@ -102,26 +103,25 @@ proc makeReceipt(vmState: BaseVMState, stateRoot: Hash256, cumulativeGasUsed: Ga
result.logs = vmState.getAndClearLogEntries() result.logs = vmState.getAndClearLogEntries()
result.bloom = logsBloom(result.logs).value.toByteArrayBE result.bloom = logsBloom(result.logs).value.toByteArrayBE
proc processBlock*(chainDB: BaseChainDB, head, header: BlockHeader, body: BlockBody, vmState: BaseVMState): bool = proc processBlock*(chainDB: BaseChainDB, head, header: BlockHeader, body: BlockBody, vmState: BaseVMState): ValidationResult =
let blockReward = 5.u256 * pow(10.u256, 18) # 5 ETH let blockReward = 5.u256 * pow(10.u256, 18) # 5 ETH
var stateDb = newAccountStateDB(c.db.db, head.stateRoot, c.db.pruneTrie) var stateDb = newAccountStateDB(chainDB.db, head.stateRoot, chainDB.pruneTrie)
var receipts = newSeq[Receipt](bodies[i].transactions.len) vmState.receipts = newSeq[Receipt](body.transactions.len)
if bodies[i].transactions.calcTxRoot != headers[i].txRoot: if body.transactions.calcTxRoot != header.txRoot:
debug "Mismatched txRoot", i debug "Mismatched txRoot", blockNumber=header.blockNumber
return ValidationResult.Error return ValidationResult.Error
if headers[i].txRoot != BLANK_ROOT_HASH: if header.txRoot != BLANK_ROOT_HASH:
let vmState = newBaseVMState(head, c.db) if body.transactions.len == 0:
if bodies[i].transactions.len == 0: debug "No transactions in body", blockNumber=header.blockNumber
debug "No transactions in body", i
return ValidationResult.Error return ValidationResult.Error
else: else:
trace "Has transactions", blockNumber = headers[i].blockNumber, blockHash = headers[i].blockHash trace "Has transactions", blockNumber = header.blockNumber, blockHash = header.blockHash
var cumulativeGasUsed = GasInt(0) var cumulativeGasUsed = GasInt(0)
for txIndex, tx in bodies[i].transactions: for txIndex, tx in body.transactions:
var sender: EthAddress var sender: EthAddress
if tx.getSender(sender): if tx.getSender(sender):
let txFee = processTransaction(stateDb, tx, sender, vmState) let txFee = processTransaction(stateDb, tx, sender, vmState)
@ -133,41 +133,41 @@ proc processBlock*(chainDB: BaseChainDB, head, header: BlockHeader, body: BlockB
cumulativeGasUsed += gasUsed cumulativeGasUsed += gasUsed
# miner fee # miner fee
stateDb.addBalance(headers[i].coinbase, txFee) stateDb.addBalance(header.coinbase, txFee)
else: else:
debug "Could not get sender", i, tx debug "Could not get sender", txIndex, tx
return ValidationResult.Error return ValidationResult.Error
receipts[txIndex] = makeReceipt(vmState, stateDb.rootHash, cumulativeGasUsed) vmState.receipts[txIndex] = makeReceipt(vmState, stateDb.rootHash, cumulativeGasUsed)
var mainReward = blockReward var mainReward = blockReward
if headers[i].ommersHash != EMPTY_UNCLE_HASH: if header.ommersHash != EMPTY_UNCLE_HASH:
let h = c.db.persistUncles(bodies[i].uncles) let h = chainDB.persistUncles(body.uncles)
if h != headers[i].ommersHash: if h != header.ommersHash:
debug "Uncle hash mismatch" debug "Uncle hash mismatch"
return ValidationResult.Error return ValidationResult.Error
for u in 0 ..< bodies[i].uncles.len: for uncle in body.uncles:
var uncleReward = bodies[i].uncles[u].blockNumber + 8.u256 var uncleReward = uncle.blockNumber + 8.u256
uncleReward -= headers[i].blockNumber uncleReward -= header.blockNumber
uncleReward = uncleReward * blockReward uncleReward = uncleReward * blockReward
uncleReward = uncleReward div 8.u256 uncleReward = uncleReward div 8.u256
stateDb.addBalance(bodies[i].uncles[u].coinbase, uncleReward) stateDb.addBalance(uncle.coinbase, uncleReward)
mainReward += blockReward div 32.u256 mainReward += blockReward div 32.u256
# Reward beneficiary # Reward beneficiary
stateDb.addBalance(headers[i].coinbase, mainReward) stateDb.addBalance(header.coinbase, mainReward)
if headers[i].stateRoot != stateDb.rootHash: if header.stateRoot != stateDb.rootHash:
error "Wrong state root in block", blockNumber = headers[i].blockNumber, expected = headers[i].stateRoot, actual = stateDb.rootHash, arrivedFrom = c.db.getCanonicalHead().stateRoot error "Wrong state root in block", blockNumber=header.blockNumber, expected=header.stateRoot, actual=stateDb.rootHash, arrivedFrom=chainDB.getCanonicalHead().stateRoot
# this one is a show stopper until we are confident in our VM's # this one is a show stopper until we are confident in our VM's
# compatibility with the main chain # compatibility with the main chain
raise(newException(Exception, "Wrong state root in block")) raise(newException(Exception, "Wrong state root in block"))
let bloom = createBloom(receipts) let bloom = createBloom(vmState.receipts)
if headers[i].bloom != bloom: if header.bloom != bloom:
debug "wrong bloom in block", blockNumber = headers[i].blockNumber debug "wrong bloom in block", blockNumber=header.blockNumber
assert(headers[i].bloom == bloom) return ValidationResult.Error
let receiptRoot = calcReceiptRoot(receipts) let receiptRoot = calcReceiptRoot(vmState.receipts)
if headers[i].receiptRoot != receiptRoot: if header.receiptRoot != receiptRoot:
debug "wrong receiptRoot in block", blockNumber = headers[i].blockNumber, actual=receiptRoot, expected=headers[i].receiptRoot debug "wrong receiptRoot in block", blockNumber=header.blockNumber, actual=receiptRoot, expected=header.receiptRoot
assert(headers[i].receiptRoot == receiptRoot) return ValidationResult.Error

View File

@ -22,6 +22,7 @@ type
tracingEnabled*: bool tracingEnabled*: bool
tracer* : TransactionTracer tracer* : TransactionTracer
logEntries* : seq[Log] logEntries* : seq[Log]
receipts* : seq[Receipt]
AccessLogs* = ref object AccessLogs* = ref object
reads*: Table[string, string] reads*: Table[string, string]