2023-11-01 03:32:09 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2020-2023 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-01-31 01:32:17 +00:00
|
|
|
import stew/bitops2
|
|
|
|
|
|
|
|
{.push raises: [].}
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-21 11:04:18 +00:00
|
|
|
type
|
|
|
|
TrieNodeType* = enum
|
|
|
|
BranchNodeType
|
|
|
|
ExtensionNodeType
|
|
|
|
AccountNodeType
|
|
|
|
HashNodeType
|
|
|
|
|
|
|
|
AccountType* = enum
|
|
|
|
SimpleAccountType
|
|
|
|
ExtendedAccountType
|
2020-05-14 04:09:01 +00:00
|
|
|
|
|
|
|
BytecodeType* = enum
|
|
|
|
CodeTouched
|
2020-05-05 08:21:39 +00:00
|
|
|
CodeUntouched
|
2020-04-22 04:21:39 +00:00
|
|
|
|
2020-04-29 05:46:50 +00:00
|
|
|
WitnessFlag* = enum
|
|
|
|
wfNoFlag
|
|
|
|
wfEIP170 # fork >= Spurious Dragon
|
|
|
|
|
2020-04-29 08:29:25 +00:00
|
|
|
MetadataType* = enum
|
|
|
|
MetadataNothing
|
|
|
|
MetadataSomething
|
|
|
|
|
2020-04-29 05:46:50 +00:00
|
|
|
WitnessFlags* = set[WitnessFlag]
|
|
|
|
|
|
|
|
ContractCodeError* = object of ValueError
|
2020-04-29 08:29:25 +00:00
|
|
|
ParsingError* = object of ValueError
|
2020-05-05 06:50:31 +00:00
|
|
|
|
|
|
|
StorageSlot* = array[32, byte]
|
2020-04-29 05:46:50 +00:00
|
|
|
|
2020-04-28 14:06:37 +00:00
|
|
|
const
|
|
|
|
StorageLeafNodeType* = AccountNodeType
|
2020-04-29 04:12:15 +00:00
|
|
|
BlockWitnessVersion* = 0x01
|
2020-07-08 14:05:20 +00:00
|
|
|
ShortRlpPrefix* = 0.byte
|
2020-04-29 04:12:15 +00:00
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
proc setBranchMaskBit*(x: var uint, i: int) =
|
2020-04-22 04:21:39 +00:00
|
|
|
assert(i >= 0 and i < 17)
|
|
|
|
x = x or (1 shl i).uint
|
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
func branchMaskBitIsSet*(x: uint, i: int): bool =
|
2020-04-22 04:21:39 +00:00
|
|
|
assert(i >= 0 and i < 17)
|
|
|
|
result = ((x shr i.uint) and 1'u) == 1'u
|
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
func constructBranchMask*(b1, b2: byte): uint
|
|
|
|
{.gcsafe, raises: [ParsingError].} =
|
2020-04-22 04:21:39 +00:00
|
|
|
result = uint(b1) shl 8 or uint(b2)
|
2020-04-29 08:29:25 +00:00
|
|
|
if countOnes(result) < 2 or ((result and (not 0x1FFFF'u)) != 0):
|
2020-05-05 06:50:31 +00:00
|
|
|
raise newException(ParsingError, "Invalid branch mask pattern " & $result)
|
2020-05-07 15:03:00 +00:00
|
|
|
|
|
|
|
iterator nonEmpty*(branchMask: uint): int =
|
|
|
|
for i in 0..<16:
|
|
|
|
if not branchMask.branchMaskBitIsSet(i):
|
|
|
|
# we skip an empty elem
|
|
|
|
continue
|
|
|
|
yield i
|