Skip to content

Commit

Permalink
bump stint to v2.0: new array backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jun 26, 2023
1 parent ff1a45e commit e412c93
Show file tree
Hide file tree
Showing 50 changed files with 84 additions and 70 deletions.
12 changes: 6 additions & 6 deletions fluffy/content_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,19 @@ proc contentSize*(db: ContentDB): int64 =

proc get*(db: ContentDB, key: ContentId): Opt[seq[byte]] =
# TODO: Here it is unfortunate that ContentId is a uint256 instead of Digest256.
db.get(key.toByteArrayBE())
db.get(key.toBytesBE())

proc put*(db: ContentDB, key: ContentId, value: openArray[byte]) =
db.put(key.toByteArrayBE(), value)
db.put(key.toBytesBE(), value)

proc contains*(db: ContentDB, key: ContentId): bool =
db.contains(key.toByteArrayBE())
db.contains(key.toBytesBE())

proc del*(db: ContentDB, key: ContentId) =
db.del(key.toByteArrayBE())
db.del(key.toBytesBE())

proc getSszDecoded*(db: ContentDB, key: ContentId, T: type auto): Opt[T] =
db.getSszDecoded(key.toByteArrayBE(), T)
db.getSszDecoded(key.toBytesBE(), T)

proc deleteContentFraction(
db: ContentDB,
Expand All @@ -264,7 +264,7 @@ proc deleteContentFraction(

var ri: RowInfo
var bytesDeleted: int64 = 0
let targetBytes = target.toByteArrayBE()
let targetBytes = target.toBytesBE()
for e in db.getAllOrderedByDistanceStmt.exec(targetBytes, ri):
if bytesDeleted + ri.payloadLength < bytesToDelete:
db.del(ri.contentId)
Expand Down
2 changes: 1 addition & 1 deletion fluffy/fluffy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ proc run(config: PortalConf) {.raises: [CatchableError].} =
# the selected `Radius`.
let
db = ContentDB.new(config.dataDir / "db" / "contentdb_" &
d.localNode.id.toByteArrayBE().toOpenArray(0, 8).toHex(), maxSize = config.storageSize)
d.localNode.id.toBytesBE().toOpenArray(0, 8).toHex(), maxSize = config.storageSize)

portalConfig = PortalProtocolConfig.init(
config.tableIpLimit,
Expand Down
4 changes: 2 additions & 2 deletions fluffy/network/beacon_light_client/beacon_light_client_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ proc put(db: LightClientDb, key, value: openArray[byte]) =

proc get*(db: LightClientDb, key: ContentId): results.Opt[seq[byte]] =
# TODO: Here it is unfortunate that ContentId is a uint256 instead of Digest256.
db.get(key.toByteArrayBE())
db.get(key.toBytesBE())

proc put*(db: LightClientDb, key: ContentId, value: openArray[byte]) =
db.put(key.toByteArrayBE(), value)
db.put(key.toBytesBE(), value)

proc createGetHandler*(db: LightClientDb): DbGetHandler =
return (
Expand Down
2 changes: 1 addition & 1 deletion fluffy/network/state/state_content.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{.push raises: [].}

import
nimcrypto/[hash, sha2, keccak], stew/[objects, results], stint,
nimcrypto/[hash, sha2, keccak], stew/[objects, results, endians2], stint,
ssz_serialization,
../../common/common_types

Expand Down
2 changes: 1 addition & 1 deletion fluffy/network/wire/messages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{.push raises: [].}

import
stint, stew/[results, objects],
stint, stew/[results, objects, endians2],
ssz_serialization,
../../common/common_types

Expand Down
2 changes: 1 addition & 1 deletion fluffy/network/wire/portal_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import
std/[sequtils, sets, algorithm],
stew/[results, byteutils, leb128], chronicles, chronos, nimcrypto/hash,
stew/[results, byteutils, leb128, endians2], chronicles, chronos, nimcrypto/hash,
bearssl, ssz_serialization, metrics, faststreams,
eth/rlp, eth/p2p/discoveryv5/[protocol, node, enr, routing_table, random2,
nodes_verification, lru],
Expand Down
2 changes: 1 addition & 1 deletion fluffy/network/wire/portal_stream.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import
std/sequtils,
chronos, stew/[byteutils, leb128], chronicles,
chronos, stew/[byteutils, leb128, endians2], chronicles,
eth/utp/utp_discv5_protocol,
# even though utp_discv5_protocol exports this, import is still needed,
# perhaps protocol.Protocol type of usage?
Expand Down
6 changes: 3 additions & 3 deletions fluffy/seed_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ proc put*(db: SeedDb, contentId: array[32, byte], contentKey: seq[byte], content
db.putStmt.exec((contentId, contentKey, content)).expectDb()

proc put*(db: SeedDb, contentId: UInt256, contentKey: seq[byte], content: seq[byte]): void =
db.put(contentId.toByteArrayBE(), contentKey, content)
db.put(contentId.toBytesBE(), contentKey, content)

proc get*(db: SeedDb, contentId: array[32, byte]): Option[ContentData] =
var res = none[ContentData]()
discard db.getStmt.exec(contentId, proc (v: ContentData) = res = some(v)).expectDb()
return res

proc get*(db: SeedDb, contentId: UInt256): Option[ContentData] =
db.get(contentId.toByteArrayBE())
db.get(contentId.toBytesBE())

proc getContentInRange*(
db: SeedDb,
Expand All @@ -147,7 +147,7 @@ proc getContentInRange*(

var res: seq[ContentDataDist] = @[]
var cd: ContentDataDist
for e in db.getInRangeStmt.exec((nodeId.toByteArrayBE(), nodeRadius.toByteArrayBE(), max, offset), cd):
for e in db.getInRangeStmt.exec((nodeId.toBytesBE(), nodeRadius.toBytesBE(), max, offset), cd):
res.add(cd)
return res

Expand Down
2 changes: 1 addition & 1 deletion fluffy/tools/utp_testing/utp_rpc_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import
std/hashes,
json_rpc/jsonmarshal,
stew/byteutils,
stew/[byteutils, endians2],
eth/p2p/discoveryv5/node,
eth/utp/[utp_discv5_protocol, utp_router]

Expand Down
2 changes: 1 addition & 1 deletion fluffy/tools/utp_testing/utp_test_app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import
std/[hashes, tables],
chronos, chronicles, confutils,
confutils/std/net as confNet,
stew/byteutils,
stew/[byteutils, endians2],
stew/shims/net,
json_rpc/servers/httpserver,
eth/p2p/discoveryv5/protocol,
Expand Down
2 changes: 1 addition & 1 deletion nimbus/common/chain_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const

proc read(rlp: var Rlp, x: var AddressBalance, _: type EthAddress): EthAddress
{.gcsafe, raises: [RlpError].} =
let val = rlp.read(UInt256).toByteArrayBE()
let val = rlp.read(UInt256).toBytesBE()
result[0 .. ^1] = val.toOpenArray(12, val.high)

proc read(rlp: var Rlp, x: var AddressBalance, _: type GenesisAccount): GenesisAccount
Expand Down
2 changes: 1 addition & 1 deletion nimbus/common/genesis.nim
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ proc toGenesisHeader*(
elif fork >= London:
result.baseFee = EIP1559_INITIAL_BASE_FEE.u256

if g.gasLimit.isZero:
if g.gasLimit == 0:
result.gasLimit = GENESIS_GAS_LIMIT

if g.difficulty.isZero and fork <= London:
Expand Down
3 changes: 2 additions & 1 deletion nimbus/common/hardforks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import
std/[options, times],
eth/common,
stew/endians2,
json_serialization,
../utils/utils,
./evmforks
Expand Down Expand Up @@ -258,7 +259,7 @@ proc populateFromForkTransitionTable*(conf: ChainConfig, t: ForkTransitionTable)

conf.mergeForkBlock = t.mergeForkTransitionThreshold.blockNumber
conf.terminalTotalDifficulty = t.mergeForkTransitionThreshold.ttd

conf.shanghaiTime = t.timeThresholds[HardFork.Shanghai]
conf.cancunTime = t.timeThresholds[HardFork.Cancun]

Expand Down
4 changes: 2 additions & 2 deletions nimbus/core/executor/executor_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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
result = bloom.value.toBytesBE

proc makeReceipt*(vmState: BaseVMState; txType: TxType): Receipt =

Expand All @@ -61,7 +61,7 @@ proc makeReceipt*(vmState: BaseVMState; txType: TxType): Receipt =
rec.receiptType = txType
rec.cumulativeGasUsed = vmState.cumulativeGasUsed
rec.logs = vmState.getAndClearLogEntries()
rec.bloom = logsBloom(rec.logs).value.toByteArrayBE
rec.bloom = logsBloom(rec.logs).value.toBytesBE
rec

# ------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions nimbus/core/pow.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import
../utils/utils,
./pow/[pow_cache, pow_dataset],
eth/[common, keys, p2p, rlp],
stew/endians2,
ethash,
stint

Expand Down
2 changes: 1 addition & 1 deletion nimbus/db/accounts_cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ template createTrieKeyFromSlot(slot: UInt256): auto =
# XXX: This is too expensive. Similar to `createRangeFromAddress`
# Converts a number to hex big-endian representation including
# prefix and leading zeros:
slot.toByteArrayBE
slot.toBytesBE
# Original py-evm code:
# pad32(int_to_big_endian(slot))
# morally equivalent to toByteRange_Unnecessary but with different types
Expand Down
2 changes: 1 addition & 1 deletion nimbus/db/aristo/aristo_desc/aristo_types_identifiers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func cmp*(x, y: HashID): int = cmp(x.UInt256, y.UInt256)
func to*(hid: HashID; T: type Hash256): T =
result.data = hid.UInt256.toBytesBE

func to*(hid: HashID; T: type HashKey): T =
proc to*(hid: HashID; T: type HashKey): T =
hid.UInt256.toBytesBE.T

func to*(key: HashKey; T: type HashID): T =
Expand Down
2 changes: 1 addition & 1 deletion nimbus/db/aristo/aristo_init/aristo_rocksdb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ proc putKeyFn(db: RdbBackendRef): PutKeyFn =
hdl.cache[KeyPfx][vid] = key.to(Blob)
else:
hdl.cache[KeyPfx][vid] = EmptyBlob


proc putIdgFn(db: RdbBackendRef): PutIdgFn =
result =
Expand Down
1 change: 1 addition & 0 deletions nimbus/db/aristo/aristo_init/aristo_rocksdb/rdb_desc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import
eth/common,
rocksdb,
stint,
stew/endians2,
../../aristo_desc,
../aristo_init_common.nim

Expand Down
1 change: 1 addition & 0 deletions nimbus/db/aristo/aristo_init/aristo_rocksdb/rdb_walk.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import
std/sequtils,
eth/common,
stew/endians2,
rocksdb,
../../aristo_desc,
../aristo_init_common,
Expand Down
2 changes: 1 addition & 1 deletion nimbus/db/aristo/aristo_transcode.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import
std/[bitops, sequtils],
eth/[common, trie/nibbles],
stew/results,
stew/[results, endians2],
"."/[aristo_constants, aristo_desc]

# ------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion nimbus/db/distinct_tries.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ template createTrieKeyFromSlot*(slot: UInt256): auto =
# XXX: This is too expensive. Similar to `createRangeFromAddress`
# Converts a number to hex big-endian representation including
# prefix and leading zeros:
slot.toByteArrayBE
slot.toBytesBE
# Original py-evm code:
# pad32(int_to_big_endian(slot))
# morally equivalent to toByteRange_Unnecessary but with different types
Expand Down
4 changes: 2 additions & 2 deletions nimbus/db/state_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ proc subBalance*(db: AccountStateDB, address: EthAddress, delta: UInt256) =
template createTrieKeyFromSlot(slot: UInt256): auto =
# Converts a number to hex big-endian representation including
# prefix and leading zeros:
slot.toByteArrayBE
slot.toBytesBE
# Original py-evm code:
# pad32(int_to_big_endian(slot))
# morally equivalent to toByteRange_Unnecessary but with different types
Expand Down Expand Up @@ -157,7 +157,7 @@ proc setStorage*(db: AccountStateDB,
var
triedb = trieDB(db)
# slotHash can be obtained from accountTrie.put?
slotHash = keccakHash(slot.toByteArrayBE)
slotHash = keccakHash(slot.toBytesBE)
triedb.put(slotHashToSlotKey(slotHash.data).toOpenArray, rlp.encode(slot))

account.storageRoot = accountTrie.rootHash
Expand Down
4 changes: 2 additions & 2 deletions nimbus/evm/computation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ when defined(evmc_enabled):
evmc/evmc,
evmc_helpers,
evmc_api,
stew/ranges/ptr_arith
stew/ptrops

export
evmc,
evmc_helpers,
evmc_api,
ptr_arith
ptrops

const
evmc_enabled* = defined(evmc_enabled)
Expand Down
2 changes: 1 addition & 1 deletion nimbus/evm/evmc_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func toEvmc*(n: UInt256): evmc_uint256be {.inline.} =
when evmc_native:
cast[evmc_uint256be](n)
else:
cast[evmc_uint256be](n.toByteArrayBE)
cast[evmc_uint256be](n.toBytesBE)

func fromEvmc*(T: type, n: evmc_bytes32): T {.inline.} =
when T is Hash256 | ContractSalt:
Expand Down
19 changes: 13 additions & 6 deletions nimbus/evm/interpreter/op_handlers/oph_arithmetic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import
./oph_defs,
eth/common

func slt(x, y: UInt256): bool =
type SignedWord = signedWordType(UInt256)
let x_neg = cast[SignedWord](x.mostSignificantWord) < 0
let y_neg = cast[SignedWord](y.mostSignificantWord) < 0
if x_neg xor y_neg: x_neg else: x < y

{.push raises: [CatchableError].} # basically the annotation type of a `Vm2OpFn`

# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -185,16 +191,17 @@ const
## 0x12, Signed less-than comparison
let (lhs, rhs) = k.cpt.stack.popInt(2)
k.cpt.stack.push:
(cast[Int256](lhs) < cast[Int256](rhs)).uint.u256
slt(lhs, rhs).uint.u256

sgtOp: Vm2OpFn = proc(k: var Vm2Ctx) =
## 0x14, Signed greater-than comparison
## 0x13, Signed greater-than comparison
let (lhs, rhs) = k.cpt.stack.popInt(2)
k.cpt.stack.push:
(cast[Int256](lhs) > cast[Int256](rhs)).uint.u256
# Arguments are swapped and SLT is used.
slt(rhs, lhs).uint.u256

eqOp: Vm2OpFn = proc(k: var Vm2Ctx) =
## 0x14, Signed greater-than comparison
## 0x14, Equality comparison
let (lhs, rhs) = k.cpt.stack.popInt(2)
k.cpt.stack.push:
(lhs == rhs).uint.u256
Expand Down Expand Up @@ -231,7 +238,7 @@ const

byteOp: Vm2OpFn = proc(k: var Vm2Ctx) =
## 0x20, Retrieve single byte from word.
let (position, value) = k.cpt.stack.popInt(2)
let (position, value) = k.cpt.stack.popInt(2)
k.cpt.stack.push:
if position >= 32.u256:
zero(UInt256)
Expand Down Expand Up @@ -273,7 +280,7 @@ const
k.cpt.stack.push:
cast[UInt256]((-1).i256)
else:
k.cpt.stack. push:
k.cpt.stack. push:
0
else:
# int version of `shr` then force the result
Expand Down
4 changes: 2 additions & 2 deletions nimbus/evm/interpreter/op_handlers/oph_memory.nim
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ const
reason = "MSTORE: GasVeryLow + memory expansion")

k.cpt.memory.extend(memPos, 32)
k.cpt.memory.write(memPos, value.toByteArrayBE)
k.cpt.memory.write(memPos, value.toBytesBE)


mstore8Op: Vm2OpFn = proc (k: var Vm2Ctx) =
Expand All @@ -160,7 +160,7 @@ const
reason = "MSTORE8: GasVeryLow + memory expansion")

k.cpt.memory.extend(memPos, 1)
k.cpt.memory.write(memPos, [value.toByteArrayBE[31]])
k.cpt.memory.write(memPos, [value.toBytesBE[31]])

# -------

Expand Down
5 changes: 2 additions & 3 deletions nimbus/evm/interpreter/utils/utils_numeric.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ proc rangeToPadded*[T: StUint](x: openArray[byte], first, last, size: int): T =
var temp: array[N, byte]
temp[0..hi-lo] = x.toOpenArray(lo, hi)
result = T.fromBytesBE(
temp.toOpenArray(0, size-1),
allowPadding = true
temp.toOpenArray(0, size-1)
)

proc rangeToPadded*(x: openArray[byte], first, size: int): seq[byte] =
Expand All @@ -90,7 +89,7 @@ proc rangeToPadded*(x: openArray[byte], first, size: int): seq[byte] =

# calculates the memory size required for a step
func calcMemSize*(offset, length: int): int {.inline.} =
if length.isZero: return 0
if length == 0: return 0
result = offset + length

func safeInt*(x: UInt256): int {.inline.} =
Expand Down
Loading

0 comments on commit e412c93

Please sign in to comment.