working makeReceipt

This commit is contained in:
andri lim 2018-12-10 22:54:19 +07:00 committed by zah
parent ad1202f98a
commit 9c0bb70ba7
2 changed files with 11 additions and 14 deletions

View File

@ -39,7 +39,7 @@ template createRangeFromAddress(address: EthAddress): ByteRange =
## it can remain searchable in the code. ## it can remain searchable in the code.
toRange(@address) toRange(@address)
proc getAccount(db: AccountStateDB, address: EthAddress): Account = proc getAccount*(db: AccountStateDB, address: EthAddress): Account =
let recordFound = db.trie.get(createRangeFromAddress address) let recordFound = db.trie.get(createRangeFromAddress address)
if recordFound.len > 0: if recordFound.len > 0:
result = rlp.decode(recordFound, Account) result = rlp.decode(recordFound, Account)

View File

@ -1,7 +1,7 @@
import ../db/[db_chain, state_db], eth_common, chronicles, ../vm_state, ../vm_types, ../transaction, ranges, import ../db/[db_chain, state_db], eth_common, chronicles, ../vm_state, ../vm_types, ../transaction, ranges,
../vm/[computation, interpreter_dispatch, message], ../constants, stint, nimcrypto, ../vm/[computation, interpreter_dispatch, message], ../constants, stint, nimcrypto,
../vm_state_transactions, sugar, ../utils, eth_trie/db, ../tracer, ./executor, json, ../vm_state_transactions, sugar, ../utils, eth_trie/db, ../tracer, ./executor, json,
eth_bloom eth_bloom, strutils
type type
# these types need to eradicated # these types need to eradicated
@ -59,7 +59,7 @@ method getSuccessorHeader*(c: Chain, h: BlockHeader, output: var BlockHeader): b
method getBlockBody*(c: Chain, blockHash: KeccakHash): BlockBodyRef = method getBlockBody*(c: Chain, blockHash: KeccakHash): BlockBodyRef =
result = nil result = nil
method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarray[BlockBody]): bool = method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarray[BlockBody]) =
# Run the VM here # Run the VM here
assert(headers.len == bodies.len) assert(headers.len == bodies.len)
@ -68,18 +68,17 @@ method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarr
let transaction = c.db.db.beginTransaction() let transaction = c.db.db.beginTransaction()
defer: transaction.dispose() defer: transaction.dispose()
trace "Persisting blocks", range = headers[0].blockNumber & " - " & headers[^1].blockNumber debug "Persisting blocks", range = $headers[0].blockNumber & " - " & $headers[^1].blockNumber
for i in 0 ..< headers.len: for i in 0 ..< headers.len:
let head = c.db.getCanonicalHead() let head = c.db.getCanonicalHead()
assert(head.blockNumber == headers[i].blockNumber - 1) assert(head.blockNumber == headers[i].blockNumber - 1)
var stateDb = newAccountStateDB(c.db.db, head.stateRoot, c.db.pruneTrie) var stateDb = newAccountStateDB(c.db.db, head.stateRoot, c.db.pruneTrie)
var gasReward = 0.u256
var receipts = newSeq[Receipt](bodies[i].transactions.len) var receipts = newSeq[Receipt](bodies[i].transactions.len)
assert(bodies[i].transactions.calcTxRoot == headers[i].txRoot) assert(bodies[i].transactions.calcTxRoot == headers[i].txRoot)
if headers[i].txRoot != BLANK_ROOT_HASH: if headers[i].txRoot != BLANK_ROOT_HASH:
# assert(head.blockNumber == headers[i].blockNumber - 1) assert(head.blockNumber == headers[i].blockNumber - 1)
let vmState = newBaseVMState(head, c.db) let vmState = newBaseVMState(head, c.db)
assert(bodies[i].transactions.len != 0) assert(bodies[i].transactions.len != 0)
var cumulativeGasUsed = GasInt(0) var cumulativeGasUsed = GasInt(0)
@ -91,16 +90,18 @@ method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarr
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)
gasReward += txFee
# perhaps this can be moved somewhere else
let gasUsed = (txFee div tx.gasPrice.u256).truncate(GasInt) let gasUsed = (txFee div tx.gasPrice.u256).truncate(GasInt)
cumulativeGasUsed += gasUsed cumulativeGasUsed += gasUsed
# miner fee
stateDb.addBalance(headers[i].coinbase, txFee)
else: else:
assert(false, "Could not get sender") assert(false, "Could not get sender")
receipts[txIndex] = makeReceipt(vmState, stateDb.rootHash, cumulativeGasUsed) receipts[txIndex] = makeReceipt(vmState, stateDb.rootHash, cumulativeGasUsed)
var mainReward = blockReward + gasReward var mainReward = blockReward
#echo "mainReward = ", mainReward , " with blockReward = ", blockReward, " and gasReward = ", gasReward
if headers[i].ommersHash != EMPTY_UNCLE_HASH: if headers[i].ommersHash != EMPTY_UNCLE_HASH:
let h = c.db.persistUncles(bodies[i].uncles) let h = c.db.persistUncles(bodies[i].uncles)
assert(h == headers[i].ommersHash) assert(h == headers[i].ommersHash)
@ -130,8 +131,6 @@ method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarr
let receiptRoot = calcReceiptRoot(receipts) let receiptRoot = calcReceiptRoot(receipts)
if headers[i].receiptRoot != receiptRoot: if headers[i].receiptRoot != receiptRoot:
debug "wrong receipt in block", blockNumber = headers[i].blockNumber, receiptRoot, valid=headers[i].receiptRoot debug "wrong receipt in block", blockNumber = headers[i].blockNumber, receiptRoot, valid=headers[i].receiptRoot
echo "CA:", receipts
return false
assert(headers[i].receiptRoot == receiptRoot) assert(headers[i].receiptRoot == receiptRoot)
discard c.db.persistHeaderToDb(headers[i]) discard c.db.persistHeaderToDb(headers[i])
@ -141,5 +140,3 @@ method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarr
c.db.persistReceipts(receipts) c.db.persistReceipts(receipts)
transaction.commit() transaction.commit()
result = true