2021-07-14 15:13:27 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
2023-02-14 20:27:17 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
import
|
|
|
|
../../vm_state,
|
2022-01-18 16:19:32 +00:00
|
|
|
../../vm_types,
|
2022-12-05 11:25:44 +00:00
|
|
|
../clique/clique_verify,
|
2021-07-14 15:13:27 +00:00
|
|
|
../clique,
|
|
|
|
../executor,
|
|
|
|
../validate,
|
|
|
|
./chain_desc,
|
|
|
|
chronicles,
|
|
|
|
stint
|
|
|
|
|
|
|
|
when not defined(release):
|
2021-07-30 14:06:51 +00:00
|
|
|
import
|
|
|
|
../../tracer,
|
2022-12-06 17:35:56 +00:00
|
|
|
../../utils/utils
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-07-04 12:31:41 +00:00
|
|
|
type
|
|
|
|
PersistBlockFlag = enum
|
|
|
|
NoPersistHeader
|
|
|
|
NoSaveTxs
|
|
|
|
NoSaveReceipts
|
2023-05-23 05:25:42 +00:00
|
|
|
NoSaveWithdrawals
|
2022-07-04 12:31:41 +00:00
|
|
|
|
|
|
|
PersistBlockFlags = set[PersistBlockFlag]
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc persistBlocksImpl(c: ChainRef; headers: openArray[BlockHeader];
|
2022-07-04 12:31:41 +00:00
|
|
|
bodies: openArray[BlockBody],
|
|
|
|
flags: PersistBlockFlags = {}): ValidationResult
|
2021-07-21 13:31:52 +00:00
|
|
|
# wildcard exception, wrapped below in public section
|
2023-02-14 20:27:17 +00:00
|
|
|
{.inline, raises: [CatchableError].} =
|
2022-12-02 04:35:41 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
let transaction = c.db.db.beginTransaction()
|
|
|
|
defer: transaction.dispose()
|
|
|
|
|
2021-07-21 13:31:52 +00:00
|
|
|
var cliqueState = c.clique.cliqueSave
|
|
|
|
defer: c.clique.cliqueRestore(cliqueState)
|
|
|
|
|
2022-12-05 08:46:37 +00:00
|
|
|
c.com.hardForkTransition(headers[0])
|
2022-12-02 04:35:41 +00:00
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
# Note that `0 < headers.len`, assured when called from `persistBlocks()`
|
2022-07-21 12:14:41 +00:00
|
|
|
let vmState = BaseVMState()
|
2022-12-02 04:35:41 +00:00
|
|
|
if not vmState.init(headers[0], c.com):
|
2022-07-21 12:14:41 +00:00
|
|
|
debug "Cannot initialise VmState",
|
|
|
|
fromBlock = headers[0].blockNumber,
|
|
|
|
toBlock = headers[^1].blockNumber
|
|
|
|
return ValidationResult.Error
|
|
|
|
|
|
|
|
trace "Persisting blocks",
|
|
|
|
fromBlock = headers[0].blockNumber,
|
|
|
|
toBlock = headers[^1].blockNumber
|
2022-01-18 16:19:32 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
for i in 0 ..< headers.len:
|
2022-01-18 16:19:32 +00:00
|
|
|
let
|
|
|
|
(header, body) = (headers[i], bodies[i])
|
|
|
|
|
2022-12-05 08:46:37 +00:00
|
|
|
c.com.hardForkTransition(header)
|
2022-12-02 04:35:41 +00:00
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
if not vmState.reinit(header):
|
|
|
|
debug "Cannot update VmState",
|
|
|
|
blockNumber = header.blockNumber,
|
|
|
|
item = i
|
|
|
|
return ValidationResult.Error
|
|
|
|
|
|
|
|
let
|
2022-09-16 04:23:25 +00:00
|
|
|
validationResult = if c.validateBlock:
|
|
|
|
vmState.processBlock(c.clique, header, body)
|
|
|
|
else:
|
|
|
|
ValidationResult.OK
|
2021-07-14 15:13:27 +00:00
|
|
|
when not defined(release):
|
|
|
|
if validationResult == ValidationResult.Error and
|
|
|
|
body.transactions.calcTxRoot == header.txRoot:
|
2022-12-06 17:35:56 +00:00
|
|
|
dumpDebuggingMetaData(c.com, header, body, vmState)
|
2021-07-14 15:13:27 +00:00
|
|
|
warn "Validation error. Debugging metadata dumped."
|
|
|
|
|
|
|
|
if validationResult != ValidationResult.OK:
|
|
|
|
return validationResult
|
2022-03-11 08:13:59 +00:00
|
|
|
|
2022-09-16 04:23:25 +00:00
|
|
|
if c.validateBlock and c.extraValidation and
|
|
|
|
c.verifyFrom <= header.blockNumber:
|
2022-12-02 04:35:41 +00:00
|
|
|
|
|
|
|
if c.com.consensus == ConsensusType.POA:
|
2021-07-30 14:06:51 +00:00
|
|
|
var parent = if 0 < i: @[headers[i-1]] else: @[]
|
2022-12-05 11:25:44 +00:00
|
|
|
let rc = c.clique.cliqueVerify(c.com, header,parent)
|
2022-04-08 04:54:11 +00:00
|
|
|
if rc.isOk:
|
2021-07-30 14:06:51 +00:00
|
|
|
# mark it off so it would not auto-restore previous state
|
|
|
|
c.clique.cliqueDispose(cliqueState)
|
|
|
|
else:
|
|
|
|
debug "PoA header verification failed",
|
|
|
|
blockNumber = header.blockNumber,
|
|
|
|
msg = $rc.error
|
|
|
|
return ValidationResult.Error
|
2021-07-21 13:31:52 +00:00
|
|
|
else:
|
2022-12-02 04:35:41 +00:00
|
|
|
let res = c.com.validateHeaderAndKinship(
|
2021-07-30 14:06:51 +00:00
|
|
|
header,
|
|
|
|
body,
|
|
|
|
checkSealOK = false, # TODO: how to checkseal from here
|
2021-10-05 23:31:35 +00:00
|
|
|
pow = c.pow)
|
2021-07-30 14:06:51 +00:00
|
|
|
if res.isErr:
|
|
|
|
debug "block validation error",
|
|
|
|
msg = res.error
|
|
|
|
return ValidationResult.Error
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2022-07-04 12:31:41 +00:00
|
|
|
if NoPersistHeader notin flags:
|
2023-04-14 22:28:57 +00:00
|
|
|
discard c.db.persistHeaderToDb(
|
|
|
|
header, c.com.consensus == ConsensusType.POS, c.com.startOfHistory)
|
2022-03-11 08:13:59 +00:00
|
|
|
|
2022-07-04 12:31:41 +00:00
|
|
|
if NoSaveTxs notin flags:
|
|
|
|
discard c.db.persistTransactions(header.blockNumber, body.transactions)
|
|
|
|
|
|
|
|
if NoSaveReceipts notin flags:
|
|
|
|
discard c.db.persistReceipts(vmState.receipts)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2023-05-23 05:25:42 +00:00
|
|
|
if NoSaveWithdrawals notin flags and body.withdrawals.isSome:
|
|
|
|
discard c.db.persistWithdrawals(body.withdrawals.get)
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# update currentBlock *after* we persist it
|
|
|
|
# so the rpc return consistent result
|
|
|
|
# between eth_blockNumber and eth_syncing
|
2022-12-02 04:35:41 +00:00
|
|
|
c.com.syncCurrent = header.blockNumber
|
2022-03-11 08:13:59 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
transaction.commit()
|
|
|
|
|
2022-03-11 08:13:59 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public `ChainDB` methods
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc insertBlockWithoutSetHead*(c: ChainRef, header: BlockHeader,
|
2022-03-11 08:13:59 +00:00
|
|
|
body: BlockBody): ValidationResult
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2023-02-14 20:27:17 +00:00
|
|
|
result = c.persistBlocksImpl(
|
|
|
|
[header], [body], {NoPersistHeader, NoSaveReceipts})
|
|
|
|
if result == ValidationResult.OK:
|
2023-04-14 22:28:57 +00:00
|
|
|
c.db.persistHeaderToDbWithoutSetHead(header, c.com.startOfHistory)
|
2022-07-04 12:31:41 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc setCanonical*(c: ChainRef, header: BlockHeader): ValidationResult
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2022-07-04 12:31:41 +00:00
|
|
|
|
|
|
|
if header.parentHash == Hash256():
|
|
|
|
discard c.db.setHead(header.blockHash)
|
|
|
|
return ValidationResult.OK
|
|
|
|
|
|
|
|
var body: BlockBody
|
|
|
|
if not c.db.getBlockBody(header, body):
|
|
|
|
debug "Failed to get BlockBody",
|
|
|
|
hash = header.blockHash
|
|
|
|
return ValidationResult.Error
|
|
|
|
|
2023-02-14 20:27:17 +00:00
|
|
|
result = c.persistBlocksImpl([header], [body], {NoPersistHeader, NoSaveTxs})
|
|
|
|
if result == ValidationResult.OK:
|
|
|
|
discard c.db.setHead(header.blockHash)
|
2022-07-04 12:31:41 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc setCanonical*(c: ChainRef, blockHash: Hash256): ValidationResult
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2022-07-04 12:31:41 +00:00
|
|
|
var header: BlockHeader
|
|
|
|
if not c.db.getBlockHeader(blockHash, header):
|
|
|
|
debug "Failed to get BlockHeader",
|
|
|
|
hash = blockHash
|
|
|
|
return ValidationResult.Error
|
|
|
|
|
|
|
|
setCanonical(c, header)
|
2022-03-11 08:13:59 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
proc persistBlocks*(c: ChainRef; headers: openArray[BlockHeader];
|
2022-04-08 04:54:11 +00:00
|
|
|
bodies: openArray[BlockBody]): ValidationResult
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
# Run the VM here
|
|
|
|
if headers.len != bodies.len:
|
|
|
|
debug "Number of headers not matching number of bodies"
|
|
|
|
return ValidationResult.Error
|
|
|
|
|
|
|
|
if headers.len == 0:
|
|
|
|
debug "Nothing to do"
|
|
|
|
return ValidationResult.OK
|
|
|
|
|
2023-02-14 20:27:17 +00:00
|
|
|
c.persistBlocksImpl(headers,bodies)
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|