nimbus-eth1/nimbus/p2p/chain.nim

63 lines
2.2 KiB
Nim
Raw Normal View History

import ../db/[db_chain, state_db], eth_common, chronicles, ../vm_state, ../vm_types, ../transaction, ranges,
2018-12-14 07:32:45 +00:00
../vm/[computation, interpreter_dispatch, message], ../constants, stint, nimcrypto,
../vm_state_transactions, sugar, ../utils, eth_trie/db, ../tracer, ./executor
2018-08-29 08:49:01 +00:00
type
Chain* = ref object of AbstractChainDB
db: BaseChainDB
proc newChain*(db: BaseChainDB): Chain =
result.new
result.db = db
method genesisHash*(c: Chain): KeccakHash =
c.db.getBlockHash(0.toBlockNumber)
method getBlockHeader*(c: Chain, b: HashOrNum, output: var BlockHeader): bool =
case b.isHash
of true:
c.db.getBlockHeader(b.hash, output)
else:
c.db.getBlockHeader(b.number, output)
method getBestBlockHeader*(c: Chain): BlockHeader =
c.db.getCanonicalHead()
method getSuccessorHeader*(c: Chain, h: BlockHeader, output: var BlockHeader): bool =
let n = h.blockNumber + 1
c.db.getBlockHeader(n, output)
method getBlockBody*(c: Chain, blockHash: KeccakHash): BlockBodyRef =
result = nil
method persistBlocks*(c: Chain, headers: openarray[BlockHeader], bodies: openarray[BlockBody]): ValidationResult =
2018-08-29 08:49:01 +00:00
# Run the VM here
if headers.len != bodies.len:
debug "Number of headers not matching number of bodies"
return ValidationResult.Error
2018-08-29 08:49:01 +00:00
let transaction = c.db.db.beginTransaction()
defer: transaction.dispose()
trace "Persisting blocks", fromBlock = headers[0].blockNumber, toBlock = headers[^1].blockNumber
2018-08-29 08:49:01 +00:00
for i in 0 ..< headers.len:
2018-09-10 08:44:07 +00:00
let head = c.db.getCanonicalHead()
2018-12-25 12:10:04 +00:00
let vmState = newBaseVMState(head, c.db)
2018-12-25 05:58:55 +00:00
let validationResult = processBlock(c.db, head, headers[i], bodies[i], vmState)
2018-09-10 08:44:07 +00:00
2018-12-12 04:31:53 +00:00
when not defined(release):
2018-12-25 05:58:55 +00:00
if validationResult == ValidationResult.Error:
2018-12-25 10:31:51 +00:00
dumpDebuggingMetaData(c.db, headers[i], bodies[i], vmState.receipts)
2018-09-10 08:44:07 +00:00
2018-12-25 05:58:55 +00:00
assert(validationResult == ValidationResult.OK)
2018-12-10 12:04:34 +00:00
discard c.db.persistHeaderToDb(headers[i])
if c.db.getCanonicalHead().blockHash != headers[i].blockHash:
debug "Stored block header hash doesn't match declared hash"
return ValidationResult.Error
c.db.persistTransactions(headers[i].blockNumber, bodies[i].transactions)
2018-12-25 05:58:55 +00:00
c.db.persistReceipts(vmState.receipts)
transaction.commit()