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
|
2021-07-14 15:13:27 +00:00
|
|
|
std/[strformat],
|
2021-09-11 14:58:01 +00:00
|
|
|
../../chain_config,
|
2021-07-06 13:14:45 +00:00
|
|
|
../../db/accounts_cache,
|
|
|
|
../../forks,
|
|
|
|
../../vm_state,
|
|
|
|
../../vm_types,
|
|
|
|
eth/[common, bloom]
|
|
|
|
|
|
|
|
type
|
2021-07-14 15:13:27 +00:00
|
|
|
ExecutorError* = object of CatchableError
|
|
|
|
## Catch and relay exception error
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# TODO: these types need to be removed
|
|
|
|
# once eth/bloom and eth/common sync'ed
|
|
|
|
Bloom = common.BloomFilter
|
|
|
|
LogsBloom = bloom.BloomFilter
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
# TODO: move these three receipt procs below somewhere else more appropriate
|
|
|
|
func logsBloom(logs: openArray[Log]): LogsBloom =
|
|
|
|
for log in logs:
|
|
|
|
result.incl log.address
|
|
|
|
for topic in log.topics:
|
|
|
|
result.incl topic
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template safeExecutor*(info: string; code: untyped) =
|
|
|
|
try:
|
|
|
|
code
|
|
|
|
except CatchableError as e:
|
|
|
|
raise (ref CatchableError)(msg: e.msg)
|
|
|
|
except Defect as e:
|
|
|
|
raise (ref Defect)(msg: e.msg)
|
|
|
|
except:
|
|
|
|
let e = getCurrentException()
|
|
|
|
raise newException(ExecutorError, info & "(): " & $e.name & " -- " & e.msg)
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
func createBloom*(receipts: openArray[Receipt]): Bloom =
|
|
|
|
var bloom: LogsBloom
|
|
|
|
for rec in receipts:
|
|
|
|
bloom.value = bloom.value or logsBloom(rec.logs).value
|
|
|
|
result = bloom.value.toByteArrayBE
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc getForkUnsafe*(vmState: BaseVMState): Fork
|
2022-01-10 09:04:06 +00:00
|
|
|
{.gcsafe, raises: [Exception].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
## Shortcut for configured fork, deliberately not naming it toFork(). This
|
|
|
|
## function may throw an `Exception` and must be wrapped.
|
2021-07-06 13:14:45 +00:00
|
|
|
vmState.chainDB.config.toFork(vmState.blockNumber)
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc makeReceipt*(vmState: BaseVMState; txType: TxType): Receipt
|
2022-01-10 09:04:06 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
proc getFork(vmState: BaseVMState): Fork
|
2022-01-10 09:04:06 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
2021-07-14 15:13:27 +00:00
|
|
|
safeExecutor("getFork"):
|
|
|
|
result = vmState.getForkUnsafe
|
|
|
|
|
2021-07-06 13:14:45 +00:00
|
|
|
var rec: Receipt
|
|
|
|
if vmState.getFork < FkByzantium:
|
|
|
|
rec.isHash = true
|
2021-10-28 09:42:39 +00:00
|
|
|
rec.hash = vmState.stateDB.rootHash
|
2021-07-06 13:14:45 +00:00
|
|
|
else:
|
|
|
|
rec.isHash = false
|
|
|
|
rec.status = vmState.status
|
|
|
|
|
|
|
|
rec.receiptType = txType
|
|
|
|
rec.cumulativeGasUsed = vmState.cumulativeGasUsed
|
|
|
|
rec.logs = vmState.getAndClearLogEntries()
|
|
|
|
rec.bloom = logsBloom(rec.logs).value.toByteArrayBE
|
|
|
|
rec
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-06 13:14:45 +00:00
|
|
|
# End
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|