Skip to content

Commit

Permalink
Change EthTime from std.Time to distinct uint64 (#1820)
Browse files Browse the repository at this point in the history
* Change EthTime from std.Time to distinct uint64

* Bump nimbus-eth2
  • Loading branch information
jangko authored Oct 18, 2023
1 parent ca61a70 commit 04c7ed8
Show file tree
Hide file tree
Showing 60 changed files with 170 additions and 166 deletions.
6 changes: 2 additions & 4 deletions fluffy/network/history/history_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import
../wire/[portal_protocol, portal_stream, portal_protocol_config],
"."/[history_content, accumulator]

from std/times import toUnix

logScope:
topics = "portal_hist"

Expand Down Expand Up @@ -278,7 +276,7 @@ proc validateBlockBodyBytes*(
## header.
## TODO: improve this decoding in combination with the block body validation
## calls.
let timestamp = Moment.init(header.timestamp.toUnix(), Second)
let timestamp = Moment.init(header.timestamp.int64, Second)
# TODO: The additional header checks are not needed as header is implicitly
# verified by means of the accumulator? Except that we don't use this yet
# post merge, so the checks are still useful, for now.
Expand Down Expand Up @@ -352,7 +350,7 @@ proc get(db: ContentDB, T: type BlockBody, contentId: ContentId,
return Opt.none(T)

let
timestamp = Moment.init(header.timestamp.toUnix(), Second)
timestamp = Moment.init(header.timestamp.int64, Second)
body =
if isShanghai(chainConfig, timestamp):
BlockBody.fromPortalBlockBodyOrRaise(
Expand Down
2 changes: 1 addition & 1 deletion fluffy/rpc/rpc_eth_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func init*(
totalDifficulty: encodeQuantity(UInt256.low()),
gasLimit: encodeQuantity(header.gasLimit.uint64),
gasUsed: encodeQuantity(header.gasUsed.uint64),
timestamp: encodeQuantity(header.timestamp.toUnix.uint64)
timestamp: encodeQuantity(header.timestamp.uint64)
)

let size = sizeof(BlockHeader) - sizeof(Blob) + header.extraData.len
Expand Down
4 changes: 2 additions & 2 deletions fluffy/tools/beacon_lc_bridge/beacon_lc_bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ proc asPortalBlockData*(
blockNumber: payload.blockNumber.distinctBase.u256,
gasLimit: payload.gasLimit.unsafeQuantityToInt64,
gasUsed: payload.gasUsed.unsafeQuantityToInt64,
timestamp: fromUnix payload.timestamp.unsafeQuantityToInt64,
timestamp: payload.timestamp.EthTime,
extraData: bytes payload.extraData,
mixDigest: payload.prevRandao.asEthHash,
nonce: default(BlockNonce),
Expand Down Expand Up @@ -286,7 +286,7 @@ proc asPortalBlockData*(
blockNumber: payload.blockNumber.distinctBase.u256,
gasLimit: payload.gasLimit.unsafeQuantityToInt64,
gasUsed: payload.gasUsed.unsafeQuantityToInt64,
timestamp: fromUnix payload.timestamp.unsafeQuantityToInt64,
timestamp: payload.timestamp.EthTime,
extraData: bytes payload.extraData,
mixDigest: payload.prevRandao.asEthHash,
nonce: default(BlockNonce),
Expand Down
16 changes: 8 additions & 8 deletions hive_integration/nodocker/engine/clmock.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import
std/[times, tables],
std/[tables],
chronicles,
nimcrypto/sysrand,
stew/[byteutils, endians2],
Expand Down Expand Up @@ -165,16 +165,16 @@ proc isBlockPoS*(cl: CLMocker, bn: common.BlockNumber): bool =
return true

# Return the per-block timestamp value increment
func getTimestampIncrement(cl: CLMocker): int =
cl.blockTimestampIncrement.get(1)
func getTimestampIncrement(cl: CLMocker): EthTime =
EthTime cl.blockTimestampIncrement.get(1)

# Returns the timestamp value to be included in the next payload attributes
func getNextBlockTimestamp(cl: CLMocker): int64 =
func getNextBlockTimestamp(cl: CLMocker): EthTime =
if cl.firstPoSBlockNumber.isNone and cl.transitionPayloadTimestamp.isSome:
# We are producing the transition payload and there's a value specified
# for this specific payload
return cl.transitionPayloadTimestamp.get
return cl.latestHeader.timestamp.toUnix + cl.getTimestampIncrement().int64
return EthTime cl.transitionPayloadTimestamp.get
return cl.latestHeader.timestamp + cl.getTimestampIncrement()

func setNextWithdrawals(cl: CLMocker, nextWithdrawals: Option[seq[WithdrawalV1]]) =
cl.nextWithdrawals = nextWithdrawals
Expand All @@ -185,11 +185,11 @@ func timestampToBeaconRoot(timestamp: Quantity): FixedBytes[32] =
FixedBytes[32](h.data)

func isShanghai(cl: CLMocker, timestamp: Quantity): bool =
let ts = fromUnix(timestamp.int64)
let ts = EthTime(timestamp.uint64)
cl.com.isShanghaiOrLater(ts)

func isCancun(cl: CLMocker, timestamp: Quantity): bool =
let ts = fromUnix(timestamp.int64)
let ts = EthTime(timestamp.uint64)
cl.com.isCancunOrLater(ts)

# Picks the next payload producer from the set of clients registered
Expand Down
2 changes: 1 addition & 1 deletion hive_integration/nodocker/engine/engine_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ proc toBlockHeader(bc: eth_api.BlockObject): common.BlockHeader =
mixDigest : bc.mixHash,
gasLimit : hexToInt(string bc.gasLimit, GasInt),
gasUsed : hexToInt(string bc.gasUsed, GasInt),
timestamp : initTime(hexToInt(string bc.timestamp, int64), 0),
timestamp : EthTime hexToInt(string bc.timestamp, uint64),
fee : maybeU256(bc.baseFeePerGas),
withdrawalsRoot: bc.withdrawalsRoot,
blobGasUsed : maybeU64(bc.blobGasUsed),
Expand Down
10 changes: 6 additions & 4 deletions hive_integration/nodocker/engine/exchange_cap_tests.nim
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import
std/[options, times],
std/[options],
eth/common/eth_types,
./test_env,
./types,
chronicles,
../../tools/common/helpers,
../../nimbus/common/hardforks

import ../../tools/common/helpers except LogLevel

type
ECSpec* = ref object of BaseSpec
exec*: proc(env: TestEnv): bool
Expand Down Expand Up @@ -50,11 +52,11 @@ proc ecCancun(env: TestEnv): bool =

proc getCCShanghai(timestamp: int): ChainConfig =
result = getChainConfig("Shanghai")
result.shanghaiTime = some(fromUnix(timestamp))
result.shanghaiTime = some(EthTime(timestamp))

proc getCCCancun(timestamp: int): ChainConfig =
result = getChainConfig("Cancun")
result.cancunTime = some(fromUnix(timestamp))
result.cancunTime = some(EthTime(timestamp))

proc specExecute(ws: BaseSpec): bool =
let ws = ECSpec(ws)
Expand Down
4 changes: 2 additions & 2 deletions hive_integration/nodocker/engine/helper.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import
std/[typetraits, times],
std/[typetraits],
nimcrypto/sysrand,
eth/[common, rlp, keys],
json_rpc/[rpcclient],
Expand Down Expand Up @@ -283,7 +283,7 @@ proc generateInvalidPayload*(basePayload: ExecutableData,
of InvalidGasUsed:
customPayload.gasUsed = some(basePayload.gasUsed - 1)
of InvalidTimestamp:
customPayload.timestamp = some(basePayload.timestamp - 1.seconds)
customPayload.timestamp = some(basePayload.timestamp - 1)
of InvalidPrevRandao:
# This option potentially requires a transaction that uses the PREVRANDAO opcode.
# Otherwise the payload will still be valid.
Expand Down
7 changes: 4 additions & 3 deletions hive_integration/nodocker/engine/withdrawals/wd_base_spec.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import
std/[times, options],
std/[options],
stint,
chronicles,
chronos,
Expand All @@ -11,13 +11,14 @@ import
../test_env,
../engine_client,
../types,
../../../tools/common/helpers,
../../../nimbus/common/common,
../../../nimbus/utils/utils,
../../../nimbus/common/chain_config,
../../../nimbus/beacon/execution_types,
../../../nimbus/beacon/web3_eth_conv

import ../../../tools/common/helpers except LogLevel

type
WDBaseSpec* = ref object of BaseSpec
timeIncrements*: int # Timestamp increments per block throughout the test
Expand Down Expand Up @@ -63,7 +64,7 @@ func getWithdrawalsForkTime(ws: WDBaseSpec): int =
# Generates the fork config, including withdrawals fork timestamp.
func getForkConfig*(ws: WDBaseSpec): ChainConfig =
result = getChainConfig("Shanghai")
result.shanghaiTime = some(ws.getWithdrawalsForkTime().fromUnix)
result.shanghaiTime = some(ws.getWithdrawalsForkTime().EthTime)

# Get the start account for all withdrawals.
func getWithdrawalsStartAccount*(ws: WDBaseSpec): UInt256 =
Expand Down
3 changes: 1 addition & 2 deletions hive_integration/nodocker/pyspec/pyspec_sim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ proc runTest(node: JsonNode, network: string): TestStatus =

const
skipName = [
"beacon_root_contract_timestamps.json",
"beacon_root_equal_to_timestamp.json",
"nothing skipped",
]

caseFolderCancun = "tests/fixtures/eth_tests/EIPTests/Pyspecs/cancun"
Expand Down
1 change: 0 additions & 1 deletion nimbus/beacon/api_handler/api_newpayload.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# those terms.

import
std/[times],
eth/common,
stew/results,
../web3_eth_conv,
Expand Down
8 changes: 4 additions & 4 deletions nimbus/beacon/web3_eth_conv.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# those terms.

import
std/[options, times, typetraits],
std/[options, typetraits],
web3/ethtypes,
web3/engine_api_types,
eth/common/eth_types_rlp,
Expand Down Expand Up @@ -86,7 +86,7 @@ func u256*(x: Web3Quantity): UInt256 =
u256(x.uint64)

func ethTime*(x: Web3Quantity): common.EthTime =
fromUnix(x.unsafeQuantityToInt64)
common.EthTime(x.unsafeQuantityToInt64)

func ethHash*(x: Web3PrevRandao): common.Hash256 =
common.Hash256(data: distinctBase x)
Expand Down Expand Up @@ -163,10 +163,10 @@ func w3Qty*(x: common.GasInt): Web3Quantity =
Web3Quantity x.uint64

func w3Qty*(x: common.EthTime): Web3Quantity =
Web3Quantity x.toUnix
Web3Quantity x.uint64

func w3Qty*(x: common.EthTime, y: int): Web3Quantity =
Web3Quantity(x.toUnix + y.int64)
Web3Quantity(x + y.EthTime)

func w3Qty*(x: Web3Quantity, y: int): Web3Quantity =
Web3Quantity(x.uint64 + y.uint64)
Expand Down
28 changes: 18 additions & 10 deletions nimbus/common/chain_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ const
# ------------------------------------------------------------------------------
# Private helper functions
# ------------------------------------------------------------------------------
proc writeValue(writer: var JsonWriter, value: Option[EthTime])
{.gcsafe, raises: [IOError].} =
mixin writeValue

if value.isSome:
writer.writeValue value.get.uint64
else:
writer.writeValue JsonString("null")

proc read(rlp: var Rlp, x: var AddressBalance, _: type EthAddress): EthAddress
{.gcsafe, raises: [RlpError].} =
Expand Down Expand Up @@ -172,13 +180,13 @@ proc readValue(reader: var JsonReader, value: var BlockNonce)
proc readValue(reader: var JsonReader, value: var EthTime)
{.gcsafe, raises: [SerializationError, IOError].} =
try:
value = fromHex[int64](reader.readValue(string)).fromUnix
value = fromHex[int64](reader.readValue(string)).EthTime
except ValueError as ex:
reader.raiseUnexpectedValue(ex.msg)

# but shanghaiTime and cancunTime in config is in int literal
proc readValue(reader: var JsonReader, value: var Option[EthTime])
{.gcsafe, raises: [SerializationError, IOError].} =
{.gcsafe, raises: [IOError].} =
let tok = reader.lexer.lazyTok
if tok == tkNull:
reset value
Expand All @@ -187,8 +195,8 @@ proc readValue(reader: var JsonReader, value: var Option[EthTime])
# both readValue(GasInt/AccountNonce) will be called if
# we use readValue(int64/uint64)
let tok {.used.} = reader.lexer.tok # resove lazy token
let val = reader.lexer.absIntVal.int64
value = some val.fromUnix
let val = EthTime reader.lexer.absIntVal
value = some val
reader.lexer.next()

proc readValue(reader: var JsonReader, value: var seq[byte])
Expand Down Expand Up @@ -401,7 +409,7 @@ proc chainConfigForNetwork*(id: NetworkId): ChainConfig =
arrowGlacierBlock: some(13_773_000.toBlockNumber), # 2021-12-09 19:55:23 UTC
grayGlacierBlock: some(15_050_000.toBlockNumber), # 2022-06-30 10:54:04 UTC
terminalTotalDifficulty: some(mainNetTTD),
shanghaiTime: some(1_681_338_455.fromUnix)
shanghaiTime: some(1_681_338_455.EthTime)
)
of RopstenNet:
ChainConfig(
Expand Down Expand Up @@ -462,7 +470,7 @@ proc chainConfigForNetwork*(id: NetworkId): ChainConfig =
berlinBlock: some(4_460_644.toBlockNumber), # 2021-03-18 05:29:51 UTC
londonBlock: some(5_062_605.toBlockNumber), # 2021-07-01 03:19:39 UTC
terminalTotalDifficulty: some(10790000.u256),
shanghaiTime: some(1_678_832_736.fromUnix)
shanghaiTime: some(1_678_832_736.EthTime)
)
of SepoliaNet:
const sepoliaTTD = parse("17000000000000000",UInt256)
Expand All @@ -483,7 +491,7 @@ proc chainConfigForNetwork*(id: NetworkId): ChainConfig =
berlinBlock: some(0.toBlockNumber),
londonBlock: some(0.toBlockNumber),
terminalTotalDifficulty: some(sepoliaTTD),
shanghaiTime: some(1_677_557_088.fromUnix)
shanghaiTime: some(1_677_557_088.EthTime)
)
else:
ChainConfig()
Expand All @@ -510,7 +518,7 @@ proc genesisBlockForNetwork*(id: NetworkId): Genesis
of RinkebyNet:
Genesis(
nonce: 0.toBlockNonce,
timestamp: initTime(0x58ee40ba, 0),
timestamp: EthTime(0x58ee40ba),
extraData: hexToSeqByte("0x52657370656374206d7920617574686f7269746168207e452e436172746d616e42eb768f2244c8811c63729a21a3569731535f067ffc57839b00206d1ad20c69a1981b489f772031b279182d99e65703f0076e4812653aab85fca0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
gasLimit: 4700000,
difficulty: 1.u256,
Expand All @@ -519,7 +527,7 @@ proc genesisBlockForNetwork*(id: NetworkId): Genesis
of GoerliNet:
Genesis(
nonce: 0.toBlockNonce,
timestamp: initTime(0x5c51a607, 0),
timestamp: EthTime(0x5c51a607),
extraData: hexToSeqByte("0x22466c6578692069732061207468696e6722202d204166726900000000000000e0a2bd4258d2768837baa26a28fe71dc079f84c70000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
gasLimit: 0xa00000,
difficulty: 1.u256,
Expand All @@ -528,7 +536,7 @@ proc genesisBlockForNetwork*(id: NetworkId): Genesis
of SepoliaNet:
Genesis(
nonce: 0.toBlockNonce,
timestamp: initTime(0x6159af19, 0),
timestamp: EthTime(0x6159af19),
extraData: hexToSeqByte("0x5365706f6c69612c20417468656e732c204174746963612c2047726565636521"),
gasLimit: 0x1c9c380,
difficulty: 0x20000.u256,
Expand Down
10 changes: 5 additions & 5 deletions nimbus/common/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{.push raises: [].}

import
std/[options, times],
std/[options],
chronicles,
eth/trie/trie_defs,
../core/[pow, clique, casper],
Expand Down Expand Up @@ -101,7 +101,7 @@ proc hardForkTransition*(
com: CommonRef, forkDeterminer: ForkDeterminationInfo)
{.gcsafe, raises: [].}

func cliquePeriod*(com: CommonRef): int
func cliquePeriod*(com: CommonRef): EthTime

func cliqueEpoch*(com: CommonRef): int

Expand Down Expand Up @@ -158,7 +158,7 @@ proc init(com : CommonRef,
# set it before creating genesis block
# TD need to be some(0.u256) because it can be the genesis
# already at the MergeFork
const TimeZero = fromUnix(0)
const TimeZero = EthTime(0)

# com.forkIds and com.blockZeroHash is set
# by setForkId
Expand Down Expand Up @@ -432,9 +432,9 @@ func ttd*(com: CommonRef): Option[DifficultyInt] =
# if you messing with clique period and
# and epoch, it likely will fail clique verification
# at epoch * blocknumber
func cliquePeriod*(com: CommonRef): int =
func cliquePeriod*(com: CommonRef): EthTime =
if com.config.clique.period.isSome:
return com.config.clique.period.get()
return EthTime com.config.clique.period.get()

func cliqueEpoch*(com: CommonRef): int =
if com.config.clique.epoch.isSome:
Expand Down
6 changes: 3 additions & 3 deletions nimbus/common/hardforks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# those terms.

import
std/[options, times],
std/[options],
eth/common,
stew/endians2,
json_serialization,
Expand Down Expand Up @@ -114,7 +114,7 @@ func adjustForNextBlock*(t: EthTime): EthTime =
#
# If this makes no sense, what should the callers
# do instead?
fromUnix(t.toUnix + 12)
t + 12

func adjustForNextBlock*(f: ForkDeterminationInfo): ForkDeterminationInfo =
ForkDeterminationInfo(
Expand Down Expand Up @@ -343,7 +343,7 @@ func toNextFork(n: Option[BlockNumber]): uint64 =
# EIP-6122: ForkID now works with timestamps too.
func toNextFork(t: Option[EthTime]): uint64 =
if t.isSome:
t.get.toUnix.uint64
t.get.uint64
else:
0'u64

Expand Down
Loading

0 comments on commit 04c7ed8

Please sign in to comment.