Further fluffy code clean-up from warnings/hints (#1987)

This commit is contained in:
Kim De Mey 2024-01-25 11:04:09 +01:00 committed by GitHub
parent 54644fa3cb
commit 3199857ed5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 55 additions and 133 deletions

View File

@ -1,5 +1,5 @@
# Fluffy
# Copyright (c) 2021-2023 Status Research & Development GmbH
# Copyright (c) 2021-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -260,9 +260,10 @@ proc run(config: PortalConf) {.raises: [CatchableError].} =
lc.start()
proc onSecond(time: Moment) =
let wallSlot = lc.getBeaconTime().slotOrZero()
discard
# TODO:
# Figure out what to do with this one.
# let wallSlot = lc.getBeaconTime().slotOrZero()
# lc.updateGossipStatus(wallSlot + 1)
proc runOnSecondLoop() {.async.} =

View File

@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2022-2023 Status Research & Development GmbH
# fluffy
# Copyright (c) 2022-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -101,7 +101,7 @@ proc initBestUpdatesStore(
delStmt: delStmt
)
func close(store: var BestLightClientUpdateStore) =
func close*(store: var BestLightClientUpdateStore) =
store.getStmt.disposeSafe()
store.getBulkStmt.disposeSafe()
store.putStmt.disposeSafe()

View File

@ -1,4 +1,4 @@
# Nimbus - Portal Network
# fluffy
# Copyright (c) 2022-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
@ -18,25 +18,19 @@ import
type
NetworkInitData* = object
clock*: BeaconClock
metaData*: Eth2NetworkMetadata
metadata*: Eth2NetworkMetadata
forks*: ForkDigests
genesis_validators_root*: Eth2Digest
proc loadNetworkData*(
networkName: string): NetworkInitData {.raises: [CatchableError].} =
proc loadNetworkData*(networkName: string): NetworkInitData =
let
metadata =
try:
loadEth2Network(some("mainnet"))
except CatchableError as exc:
raiseAssert(exc.msg)
metadata = loadEth2Network(some("mainnet"))
genesisState =
try:
template genesisData(): auto = metadata.genesis.bakedBytes
newClone(readSszForkedHashedBeaconState(
metadata.cfg, genesisData.toOpenArray(genesisData.low, genesisData.high)))
except CatchableError as err:
except SerializationError as err:
raiseAssert "Invalid baked-in state: " & err.msg
genesisTime = getStateField(genesisState[], genesis_time)
@ -51,7 +45,7 @@ proc loadNetworkData*(
return NetworkInitData(
clock: beaconClock,
metaData: metaData,
metadata: metadata,
forks: forks[],
genesis_validators_root: genesis_validators_root
)

View File

@ -1,5 +1,5 @@
# beacon hain light client
# Copyright (c) 2022-2023 Status Research & Development GmbH
# fluffy
# Copyright (c) 2022-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -275,41 +275,6 @@ proc query[E](
# requests however, see: https://github.com/status-im/nimbus-eth1/issues/1769
self.workerTask(e, key)
template query[E](
self: LightClientManager,
e: typedesc[E]
): Future[bool] =
self.query(e, Nothing())
type SchedulingMode = enum
Soon,
CurrentPeriod,
NextPeriod
func fetchTime(
self: LightClientManager,
wallTime: BeaconTime,
schedulingMode: SchedulingMode
): BeaconTime =
let
remainingTime =
case schedulingMode:
of Soon:
chronos.seconds(0)
of CurrentPeriod:
let
wallPeriod = wallTime.slotOrZero().sync_committee_period
deadlineSlot = (wallPeriod + 1).start_slot - 1
deadline = deadlineSlot.start_beacon_time()
chronos.nanoseconds((deadline - wallTime).nanoseconds)
of NextPeriod:
chronos.seconds(
(SLOTS_PER_SYNC_COMMITTEE_PERIOD * SECONDS_PER_SLOT).int64)
minDelay = max(remainingTime div 8, chronos.seconds(10))
jitterSeconds = (minDelay * 2).seconds
jitterDelay = chronos.seconds(self.rng[].rand(jitterSeconds).int64)
return wallTime + minDelay + jitterDelay
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.1/specs/altair/light-client/light-client.md#light-client-sync-process
proc loop(self: LightClientManager) {.async.} =
var nextSyncTaskTime = self.getBeaconTime()

View File

@ -1,5 +1,5 @@
# Fluffy
# Copyright (c) 2021-2023 Status Research & Development GmbH
# Copyright (c) 2021-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -291,7 +291,9 @@ func truncateEnrs(
for n in nodes:
let enr = ByteList.init(n.record.raw)
if totalSize + enr.len() + enrOverhead <= maxSize:
let res = enrs.add(enr) # 32 limit will not be reached
let res = enrs.add(enr)
# With max payload of discv5 and the sizes of ENRs this should not occur.
doAssert(res, "32 limit will not be reached")
totalSize = totalSize + enr.len()
else:
break
@ -857,6 +859,8 @@ proc offer(p: PortalProtocol, o: OfferRequest):
# No point in trying to continue writing data
socket.close()
return err("Error writing requested data")
trace "Offered content item send", dataWritten = dataWritten
of Database:
for i, b in m.contentKeys:
if b:
@ -884,6 +888,7 @@ proc offer(p: PortalProtocol, o: OfferRequest):
socket.close()
return err("Error writing requested data")
trace "Offered content item send", dataWritten = dataWritten
await socket.closeWait()
debug "Content successfully offered"
@ -1231,8 +1236,6 @@ proc traceContentLookup*(p: PortalProtocol, target: ByteList, targetId: UInt256)
let distance = p.distance(content.src.id, targetId)
let address = content.src.address.get()
responses["0x" & $content.src.id] = TraceResponse(
durationMs: duration,
respondedWith: respondedWith,

View File

@ -16,6 +16,5 @@
--styleCheck:usages
--styleCheck:hint
--hint[XDeclaredButNotUsed]:off
--hint[ConvFromXtoItselfNotNeeded]:off
--hint[Processing]:off

View File

@ -116,8 +116,7 @@ proc installEthApiHandlers*(
# Currently only HistoryNetwork needed, later we might want a master object
# holding all the networks.
rpcServerWithProxy: var RpcProxy, historyNetwork: HistoryNetwork,
beaconLightClient: Opt[LightClient])
{.raises: [CatchableError].} =
beaconLightClient: Opt[LightClient]) =
# Supported API
rpcServerWithProxy.registerProxyMethod("eth_blockNumber")

View File

@ -1,4 +1,4 @@
# Nimbus
# fluffy
# Copyright (c) 2021-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
@ -40,8 +40,7 @@ TraceResponse.useDefaultSerializationIn JrpcConv
# as the proc becomes generic, where the rpc macro from router.nim can no longer
# be found, which is why we export rpcserver which should export router.
proc installPortalApiHandlers*(
rpcServer: RpcServer|RpcProxy, p: PortalProtocol, network: static string)
{.raises: [CatchableError].} =
rpcServer: RpcServer|RpcProxy, p: PortalProtocol, network: static string) =
rpcServer.rpc("portal_" & network & "NodeInfo") do() -> NodeInfo:
return p.routingTable.getNodeInfo()

View File

@ -1,5 +1,5 @@
# Fluffy
# Copyright (c) 2022-2023 Status Research & Development GmbH
# Copyright (c) 2022-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -18,8 +18,7 @@ export rpcserver
# Non-spec-RPCs that are (currently) useful for testing & debugging
proc installPortalDebugApiHandlers*(
rpcServer: RpcServer|RpcProxy, p: PortalProtocol, network: static string)
{.raises: [CatchableError].} =
rpcServer: RpcServer|RpcProxy, p: PortalProtocol, network: static string) =
rpcServer.rpc("portal_" & network & "_storeContent") do(
dataFile: string) -> bool:

View File

@ -1,5 +1,5 @@
# Nimbus fluffy
# Copyright (c) 2023 Status Research & Development GmbH
# fluffy
# Copyright (c) 2023-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -13,8 +13,7 @@ import
export rpcserver
proc installWeb3ApiHandlers*(rpcServer: RpcServer|RpcProxy)
{.raises: [CatchableError].} =
proc installWeb3ApiHandlers*(rpcServer: RpcServer|RpcProxy) =
rpcServer.rpc("web3_clientVersion") do() -> string:
return clientVersion

View File

@ -106,23 +106,6 @@ template asEthHash(hash: web3types.BlockHash): Hash256 =
template unsafeQuantityToInt64(q: Quantity): int64 =
int64 q
# TODO: Cannot use the `hexToInt` from rpc_utils as it importing that causes a
# strange "Exception can raise an unlisted exception: Exception` compile error.
func hexToInt(
s: string, T: typedesc[SomeInteger]): T {.raises: [ValueError].} =
var i = 0
if s[i] == '0' and (s[i+1] in {'x', 'X'}):
inc(i, 2)
if s.len - i > sizeof(T) * 2:
raise newException(ValueError, "Input hex too big for destination int")
var res: T = 0
while i < s.len:
res = res shl 4 or readHexChar(s[i]).T
inc(i)
res
func asTxType(quantity: Option[Quantity]): Result[TxType, string] =
let value = quantity.get(0.Quantity).uint8
var txType: TxType
@ -553,7 +536,6 @@ proc run(config: BeaconBridgeConf) {.raises: [CatchableError].} =
let
root = hash_tree_root(forkyObject.header)
contentKey = encode(bootstrapContentKey(root))
contentId = beacon_content.toContentId(contentKey)
forkDigest = forkDigestAtEpoch(
forkDigests[], epoch(forkyObject.header.beacon.slot), cfg)
content = encodeBootstrapForked(
@ -586,7 +568,6 @@ proc run(config: BeaconBridgeConf) {.raises: [CatchableError].} =
let
period = forkyObject.attested_header.beacon.slot.sync_committee_period
contentKey = encode(updateContentKey(period.uint64, uint64(1)))
contentId = beacon_content.toContentId(contentKey)
forkDigest = forkDigestAtEpoch(
forkDigests[], epoch(forkyObject.attested_header.beacon.slot), cfg)
content = encodeLightClientUpdatesForked(
@ -621,7 +602,6 @@ proc run(config: BeaconBridgeConf) {.raises: [CatchableError].} =
let
slot = forkyObject.signature_slot
contentKey = encode(optimisticUpdateContentKey(slot.uint64))
contentId = beacon_content.toContentId(contentKey)
forkDigest = forkDigestAtEpoch(
forkDigests[], epoch(forkyObject.attested_header.beacon.slot), cfg)
content = encodeOptimisticUpdateForked(
@ -655,7 +635,6 @@ proc run(config: BeaconBridgeConf) {.raises: [CatchableError].} =
let
finalizedSlot = forkyObject.finalized_header.beacon.slot
contentKey = encode(finalityUpdateContentKey(finalizedSlot.uint64))
contentId = beacon_content.toContentId(contentKey)
forkDigest = forkDigestAtEpoch(
forkDigests[], epoch(forkyObject.attested_header.beacon.slot), cfg)
content = encodeFinalityUpdateForked(

View File

@ -23,7 +23,7 @@ const
restRequestsTimeout = 30.seconds
proc getBeaconData*(): (
RuntimeConfig, ref ForkDigests, BeaconClock) {.raises: [IOError].} =
RuntimeConfig, ref ForkDigests, BeaconClock) =
let
metadata = getMetadataForNetwork("mainnet")
genesisState =
@ -32,7 +32,7 @@ proc getBeaconData*(): (
newClone(readSszForkedHashedBeaconState(
metadata.cfg,
genesisData.toOpenArray(genesisData.low, genesisData.high)))
except CatchableError as err:
except SerializationError as err:
raiseAssert "Invalid baked-in state: " & err.msg
genesis_validators_root =
getStateField(genesisState[], genesis_validators_root)
@ -60,14 +60,12 @@ proc exportLCBootstrapUpdate*(
fatal "Error occured while closing file", error = e.msg
quit 1
var
let
client = RestClientRef.new(restUrl).valueOr:
error "Cannot connect to server", error = error
quit 1
var contentTable: JsonPortalContentTable
var update =
let update =
try:
notice "Downloading LC bootstrap"
awaitWithTimeout(
@ -118,14 +116,12 @@ proc exportLCUpdates*(
fatal "Error occured while closing file", error = e.msg
quit 1
var
let
client = RestClientRef.new(restUrl).valueOr:
error "Cannot connect to server", error = error
quit 1
var contentTable: JsonPortalContentTable
var updates =
let updates =
try:
notice "Downloading LC updates"
awaitWithTimeout(
@ -181,14 +177,12 @@ proc exportLCFinalityUpdate*(
fatal "Error occured while closing file", error = e.msg
quit 1
var
let
client = RestClientRef.new(restUrl).valueOr:
error "Cannot connect to server", error = error
quit 1
var contentTable: JsonPortalContentTable
var update =
let update =
try:
notice "Downloading LC finality update"
awaitWithTimeout(
@ -207,7 +201,6 @@ proc exportLCFinalityUpdate*(
let
finalizedSlot = forkyObject.finalized_header.beacon.slot
contentKey = encode(finalityUpdateContentKey(finalizedSlot.uint64))
contentId = beacon_content.toContentId(contentKey)
forkDigest = forkDigestAtEpoch(
forkDigests[], epoch(forkyObject.attested_header.beacon.slot), cfg)
content = encodeFinalityUpdateForked(
@ -238,14 +231,12 @@ proc exportLCOptimisticUpdate*(
fatal "Error occured while closing file", error = e.msg
quit 1
var
let
client = RestClientRef.new(restUrl).valueOr:
error "Cannot connect to server", error = error
quit 1
var contentTable: JsonPortalContentTable
var update =
let update =
try:
notice "Downloading LC optimistic update"
awaitWithTimeout(
@ -264,7 +255,6 @@ proc exportLCOptimisticUpdate*(
let
slot = forkyObject.signature_slot
contentKey = encode(optimisticUpdateContentKey(slot.uint64))
contentId = beacon_content.toContentId(contentKey)
forkDigest = forkDigestAtEpoch(
forkDigests[], epoch(forkyObject.attested_header.beacon.slot), cfg)
content = encodeOptimisticUpdateForked(

View File

@ -1,5 +1,5 @@
# Fluffy
# Copyright (c) 2023 Status Research & Development GmbH
# Copyright (c) 2023-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -108,7 +108,7 @@ proc cmdBench(conf: DbConf) =
for key in keys:
withTimer(timers[tDbGet]):
let val = db.get(key)
let _ = db.get(key)
for key in keys:
withTimer(timers[tDbContains]):
@ -120,17 +120,17 @@ proc cmdBench(conf: DbConf) =
for i in 0..<conf.samples:
withTimer(timers[tDbSize]):
let size = db.size()
let _ = db.size()
withTimer(timers[tDbUsedSize]):
let size = db.usedSize()
let _ = db.usedSize()
withTimer(timers[tDbContentSize]):
let size = db.contentSize()
let _ = db.contentSize()
withTimer(timers[tDbContentCount]):
let count = db.contentCount()
let _ = db.contentCount()
withTimer(timers[tDbLargestDistance]):
# The selected local ID doesn't matter here as it currently needs to
# iterate over all content for this call.
let distance = db.getLargestDistance(u256(0))
let _ = db.getLargestDistance(u256(0))
printTimers(timers)

View File

@ -95,7 +95,7 @@ proc runBeacon(config: PortalBridgeConf) {.raises: [CatchableError].} =
currentPeriod =
wallSlot div (SLOTS_PER_EPOCH * EPOCHS_PER_SYNC_COMMITTEE_PERIOD)
requestAmount = backfillAmount div updatesPerRequest
leftOver = backFillAmount mod updatesPerRequest
leftOver = backfillAmount mod updatesPerRequest
for i in 0..<requestAmount:
await portalRpcClient.connect(rpcAddress, rpcPort, false)

View File

@ -1,5 +1,5 @@
# Fluffy
# Copyright (c) 2023 Status Research & Development GmbH
# Copyright (c) 2023-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -100,10 +100,9 @@ proc gossipLCUpdates*(
when lcDataFork > LightClientDataFork.None:
let
slot = forkyObject.attested_header.beacon.slot
period = forkyObject.attested_header.beacon.slot.sync_committee_period
period = slot.sync_committee_period
contentKey = encode(updateContentKey(period.uint64, count))
forkDigest = forkDigestAtEpoch(
forkDigests[], epoch(forkyObject.attested_header.beacon.slot), cfg)
forkDigest = forkDigestAtEpoch(forkDigests[], epoch(slot), cfg)
content = encodeLightClientUpdatesForked(
forkDigest,

View File

@ -1,5 +1,5 @@
# Fluffy
# Copyright (c) 2023 Status Research & Development GmbH
# Copyright (c) 2023-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -60,7 +60,7 @@ type
name: "rest-url" .}: string
# Backfill options
backFillAmount* {.
backfillAmount* {.
desc: "Amount of beacon LC updates to backfill gossip into the network"
defaultValue: 64
name: "backfill-amount" .}: uint64

View File

@ -1,5 +1,5 @@
# Fluffy
# Copyright (c) 2021-2023 Status Research & Development GmbH
# Copyright (c) 2021-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
@ -261,10 +261,7 @@ proc run(config: PortalCliConf) =
else:
echo nodes.error
of findContent:
proc random(T: type UInt256, rng: var HmacDrbgContext): T =
rng.generate(T)
# For now just some random bytes
# For now just some bogus bytes
let contentKey = ByteList.init(@[1'u8])
let foundContent = waitFor portal.findContent(config.findContentTarget,

View File

@ -19,6 +19,5 @@
--styleCheck:usages
--styleCheck:hint
--hint[XDeclaredButNotUsed]:off
--hint[ConvFromXtoItselfNotNeeded]:off
--hint[Processing]:off

View File

@ -66,7 +66,7 @@ proc run(config: VerifiedProxyConf) {.raises: [CatchableError].} =
except CatchableError as err:
raiseAssert "Invalid baked-in state: " & err.msg
genesistime = getStateField(genesisState[], genesis_time)
genesisTime = getStateField(genesisState[], genesis_time)
beaconClock = BeaconClock.init(genesisTime).valueOr:
error "Invalid genesis time in state", genesisTime
quit QuitFailure