2021-07-06 13:14:45 +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.
|
|
|
|
|
|
|
|
import
|
2023-08-16 13:05:43 +00:00
|
|
|
../../utils/utils,
|
2022-12-02 04:35:41 +00:00
|
|
|
../../common/common,
|
2021-07-06 13:14:45 +00:00
|
|
|
../../constants,
|
2022-12-02 04:35:41 +00:00
|
|
|
../../db/accounts_cache,
|
2021-07-06 13:14:45 +00:00
|
|
|
../../transaction,
|
|
|
|
../../vm_state,
|
|
|
|
../../vm_types,
|
|
|
|
../clique,
|
|
|
|
../dao,
|
|
|
|
./calculate_reward,
|
|
|
|
./executor_helpers,
|
|
|
|
./process_transaction,
|
|
|
|
chronicles,
|
2022-01-10 09:04:06 +00:00
|
|
|
stew/results
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises: [].}
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2023-04-12 12:39:11 +00:00
|
|
|
# Factored this out of procBlkPreamble so that it can be used directly for
|
|
|
|
# stateless execution of specific transactions.
|
|
|
|
proc processTransactions*(vmState: BaseVMState;
|
|
|
|
header: BlockHeader;
|
|
|
|
transactions: seq[Transaction]): Result[void, string]
|
2023-05-10 16:04:35 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2023-04-12 12:39:11 +00:00
|
|
|
vmState.receipts = newSeq[Receipt](transactions.len)
|
|
|
|
vmState.cumulativeGasUsed = 0
|
2023-08-30 16:29:48 +00:00
|
|
|
|
|
|
|
if header.parentBeaconBlockRoot.isSome:
|
|
|
|
vmState.processBeaconBlockRoot(header.parentBeaconBlockRoot.get).isOkOr:
|
|
|
|
return err(error)
|
|
|
|
|
2023-04-12 12:39:11 +00:00
|
|
|
for txIndex, tx in transactions:
|
|
|
|
var sender: EthAddress
|
|
|
|
if not tx.getSender(sender):
|
2023-08-21 02:10:18 +00:00
|
|
|
return err("Could not get sender for tx with index " & $(txIndex))
|
2023-04-12 12:39:11 +00:00
|
|
|
let rc = vmState.processTransaction(tx, sender, header)
|
|
|
|
if rc.isErr:
|
2023-08-21 02:10:18 +00:00
|
|
|
return err("Error processing tx with index " & $(txIndex) & ":" & rc.error)
|
2023-04-12 12:39:11 +00:00
|
|
|
vmState.receipts[txIndex] = vmState.makeReceipt(tx.txType)
|
|
|
|
ok()
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc procBlkPreamble(vmState: BaseVMState;
|
|
|
|
header: BlockHeader; body: BlockBody): bool
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2022-01-10 09:04:06 +00:00
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
if vmState.com.daoForkSupport and
|
|
|
|
vmState.com.daoForkBlock.get == header.blockNumber:
|
2021-07-06 13:14:45 +00:00
|
|
|
vmState.mutateStateDB:
|
|
|
|
db.applyDAOHardFork()
|
|
|
|
|
|
|
|
if body.transactions.calcTxRoot != header.txRoot:
|
|
|
|
debug "Mismatched txRoot",
|
|
|
|
blockNumber = header.blockNumber
|
|
|
|
return false
|
|
|
|
|
2023-08-30 16:29:48 +00:00
|
|
|
if vmState.determineFork >= FkCancun:
|
|
|
|
if header.parentBeaconBlockRoot.isNone:
|
|
|
|
raise ValidationError.newException("Post-Cancun block header must have parentBeaconBlockRoot")
|
|
|
|
else:
|
|
|
|
if header.parentBeaconBlockRoot.isSome:
|
|
|
|
raise ValidationError.newException("Pre-Cancun block header must not have parentBeaconBlockRoot")
|
|
|
|
|
2022-09-03 18:15:35 +00:00
|
|
|
if header.txRoot != EMPTY_ROOT_HASH:
|
2021-07-06 13:14:45 +00:00
|
|
|
if body.transactions.len == 0:
|
|
|
|
debug "No transactions in body",
|
|
|
|
blockNumber = header.blockNumber
|
|
|
|
return false
|
|
|
|
else:
|
2023-04-12 12:39:11 +00:00
|
|
|
let r = processTransactions(vmState, header, body.transactions)
|
|
|
|
if r.isErr:
|
|
|
|
error("error in processing transactions", err=r.error)
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2023-03-17 18:16:24 +00:00
|
|
|
if vmState.determineFork >= FkShanghai:
|
|
|
|
if header.withdrawalsRoot.isNone:
|
|
|
|
raise ValidationError.newException("Post-Shanghai block header must have withdrawalsRoot")
|
2023-06-25 13:30:34 +00:00
|
|
|
if body.withdrawals.isNone:
|
2023-03-17 18:16:24 +00:00
|
|
|
raise ValidationError.newException("Post-Shanghai block body must have withdrawals")
|
2023-03-09 23:40:55 +00:00
|
|
|
|
2023-06-25 13:30:34 +00:00
|
|
|
for withdrawal in body.withdrawals.get:
|
|
|
|
vmState.stateDB.addBalance(withdrawal.address, withdrawal.amount.gwei)
|
2023-03-17 18:16:24 +00:00
|
|
|
else:
|
|
|
|
if header.withdrawalsRoot.isSome:
|
|
|
|
raise ValidationError.newException("Pre-Shanghai block header must not have withdrawalsRoot")
|
2023-06-25 13:30:34 +00:00
|
|
|
if body.withdrawals.isSome:
|
2023-03-17 18:16:24 +00:00
|
|
|
raise ValidationError.newException("Pre-Shanghai block body must not have withdrawals")
|
2023-03-09 23:40:55 +00:00
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
if vmState.cumulativeGasUsed != header.gasUsed:
|
|
|
|
debug "gasUsed neq cumulativeGasUsed",
|
|
|
|
gasUsed = header.gasUsed,
|
|
|
|
cumulativeGasUsed = vmState.cumulativeGasUsed
|
|
|
|
return false
|
|
|
|
|
|
|
|
if header.ommersHash != EMPTY_UNCLE_HASH:
|
2022-12-02 04:35:41 +00:00
|
|
|
let h = vmState.com.db.persistUncles(body.uncles)
|
2021-07-06 13:14:45 +00:00
|
|
|
if h != header.ommersHash:
|
|
|
|
debug "Uncle hash mismatch"
|
|
|
|
return false
|
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
true
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc procBlkEpilogue(vmState: BaseVMState;
|
|
|
|
header: BlockHeader; body: BlockBody): bool
|
2023-09-18 20:20:28 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2021-07-06 13:14:45 +00:00
|
|
|
# Reward beneficiary
|
|
|
|
vmState.mutateStateDB:
|
|
|
|
if vmState.generateWitness:
|
|
|
|
db.collectWitnessData()
|
2023-03-20 11:51:09 +00:00
|
|
|
let clearEmptyAccount = vmState.determineFork >= FkSpurious
|
|
|
|
db.persist(clearEmptyAccount, ClearCache in vmState.flags)
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2021-10-28 09:42:39 +00:00
|
|
|
let stateDb = vmState.stateDB
|
2021-07-06 13:14:45 +00:00
|
|
|
if header.stateRoot != stateDb.rootHash:
|
|
|
|
debug "wrong state root in block",
|
|
|
|
blockNumber = header.blockNumber,
|
|
|
|
expected = header.stateRoot,
|
|
|
|
actual = stateDb.rootHash,
|
2022-12-02 04:35:41 +00:00
|
|
|
arrivedFrom = vmState.com.db.getCanonicalHead().stateRoot
|
2021-07-06 13:14:45 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
let bloom = createBloom(vmState.receipts)
|
|
|
|
if header.bloom != bloom:
|
|
|
|
debug "wrong bloom in block",
|
|
|
|
blockNumber = header.blockNumber
|
|
|
|
return false
|
|
|
|
|
|
|
|
let receiptRoot = calcReceiptRoot(vmState.receipts)
|
|
|
|
if header.receiptRoot != receiptRoot:
|
|
|
|
debug "wrong receiptRoot in block",
|
|
|
|
blockNumber = header.blockNumber,
|
|
|
|
actual = receiptRoot,
|
|
|
|
expected = header.receiptRoot
|
|
|
|
return false
|
|
|
|
|
2022-01-18 13:05:00 +00:00
|
|
|
true
|
2021-07-06 13:14:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc processBlockNotPoA*(
|
|
|
|
vmState: BaseVMState; ## Parent environment of header/body block
|
|
|
|
header: BlockHeader; ## Header/body block to add to the blockchain
|
|
|
|
body: BlockBody): ValidationResult
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2021-07-30 14:06:51 +00:00
|
|
|
## Processes `(header,body)` pair for a non-PoA network, only. This function
|
|
|
|
## will fail when applied to a PoA network like `Goerli`.
|
2022-12-02 04:35:41 +00:00
|
|
|
if vmState.com.consensus == ConsensusType.POA:
|
2021-07-06 13:14:45 +00:00
|
|
|
# PoA consensus engine unsupported, see the other version of
|
|
|
|
# processBlock() below
|
|
|
|
debug "Unsupported PoA request"
|
|
|
|
return ValidationResult.Error
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
var dbTx = vmState.com.db.beginTransaction()
|
2021-07-06 13:14:45 +00:00
|
|
|
defer: dbTx.dispose()
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
if not vmState.procBlkPreamble(header, body):
|
2021-07-06 13:14:45 +00:00
|
|
|
return ValidationResult.Error
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
# EIP-3675: no reward for miner in POA/POS
|
|
|
|
if vmState.com.consensus == ConsensusType.POW:
|
2022-02-08 09:27:04 +00:00
|
|
|
vmState.calculateReward(header, body)
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
if not vmState.procBlkEpilogue(header, body):
|
2021-07-06 13:14:45 +00:00
|
|
|
return ValidationResult.Error
|
|
|
|
|
|
|
|
# `applyDeletes = false`
|
|
|
|
# If the trie pruning activated, each of the block will have its own state
|
|
|
|
# trie keep intact, rather than destroyed by trie pruning. But the current
|
|
|
|
# block will still get a pruned trie. If trie pruning deactivated,
|
|
|
|
# `applyDeletes` have no effects.
|
|
|
|
dbTx.commit(applyDeletes = false)
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
ValidationResult.OK
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
proc processBlock*(
|
|
|
|
vmState: BaseVMState; ## Parent environment of header/body block
|
|
|
|
poa: Clique; ## PoA descriptor (if needed, at all)
|
|
|
|
header: BlockHeader; ## Header/body block to add to the blockchain
|
|
|
|
body: BlockBody): ValidationResult
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Generalised function to processes `(header,body)` pair for any network,
|
2021-07-30 14:06:51 +00:00
|
|
|
## regardless of PoA or not. Currently there is no mining support so this
|
|
|
|
## function is mostly the same as `processBlockNotPoA()`.
|
|
|
|
##
|
|
|
|
## Rather than calculating the PoA state change here, it is done with the
|
|
|
|
## verification in the `chain/persist_blocks.persistBlocks()` method. So
|
|
|
|
## the `poa` descriptor is currently unused and only provided for later
|
|
|
|
## implementations (but can be savely removed, as well.)
|
2022-01-10 09:04:06 +00:00
|
|
|
## variant of `processBlock()` where the `header` argument is explicitely set.
|
|
|
|
##
|
2021-07-30 14:06:51 +00:00
|
|
|
# # Process PoA state transition first so there is no need to re-wind on
|
|
|
|
# # an error.
|
|
|
|
# if vmState.chainDB.config.poaEngine and
|
|
|
|
# not poa.updatePoaState(header, body):
|
|
|
|
# debug "PoA update failed"
|
|
|
|
# return ValidationResult.Error
|
2021-07-14 15:13:27 +00:00
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
var dbTx = vmState.com.db.beginTransaction()
|
2021-07-06 13:14:45 +00:00
|
|
|
defer: dbTx.dispose()
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
if not vmState.procBlkPreamble(header, body):
|
2021-07-06 13:14:45 +00:00
|
|
|
return ValidationResult.Error
|
|
|
|
|
2022-12-02 04:35:41 +00:00
|
|
|
# EIP-3675: no reward for miner in POA/POS
|
|
|
|
if vmState.com.consensus == ConsensusType.POW:
|
2021-07-06 13:14:45 +00:00
|
|
|
vmState.calculateReward(header, body)
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
if not vmState.procBlkEpilogue(header, body):
|
2021-07-06 13:14:45 +00:00
|
|
|
return ValidationResult.Error
|
|
|
|
|
|
|
|
dbTx.commit(applyDeletes = false)
|
|
|
|
|
2022-01-10 09:04:06 +00:00
|
|
|
ValidationResult.OK
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|