Skip to content

Commit

Permalink
Fluffy state network now enabled by default and improve status logs (#…
Browse files Browse the repository at this point in the history
…2640)

* Enable state network by default. Create status log loop for state and beacon networks. Create status log loop for portal node. Implement stop functions.
  • Loading branch information
bhartnett authored Sep 19, 2024
1 parent 2fe8cc4 commit a9ad10c
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 17 deletions.
2 changes: 1 addition & 1 deletion fluffy/conf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type

portalSubnetworks* {.
desc: "Select which networks (Portal sub-protocols) to enable",
defaultValue: {PortalSubnetwork.history},
defaultValue: {PortalSubnetwork.history, PortalSubnetwork.state},
name: "portal-subnetworks"
.}: set[PortalSubnetwork]

Expand Down
7 changes: 5 additions & 2 deletions fluffy/network/beacon/beacon_light_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,13 @@ proc new*(
)

proc start*(lightClient: LightClient) =
notice "Starting beacon light client",
trusted_block_root = lightClient.trustedBlockRoot
info "Starting beacon light client", trusted_block_root = lightClient.trustedBlockRoot
lightClient.manager.start()

proc stop*(lightClient: LightClient) =
info "Stopping beacon light client"
discard lightClient.manager.stop()

proc resetToFinalizedHeader*(
lightClient: LightClient,
header: ForkedLightClientHeader,
Expand Down
17 changes: 17 additions & 0 deletions fluffy/network/beacon/beacon_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type BeaconNetwork* = ref object
forkDigests*: ForkDigests
trustedBlockRoot: Opt[Eth2Digest]
processContentLoop: Future[void]
statusLogLoop: Future[void]

func toContentIdHandler(contentKey: ContentKeyByteList): results.Opt[ContentId] =
ok(toContentId(contentKey))
Expand Down Expand Up @@ -364,13 +365,29 @@ proc processContentLoop(n: BeaconNetwork) {.async: (raises: []).} =
except CancelledError:
trace "processContentLoop canceled"

proc statusLogLoop(n: BeaconNetwork) {.async: (raises: []).} =
try:
while true:
info "Beacon network status",
routingTableNodes = n.portalProtocol.routingTable.len()

await sleepAsync(60.seconds)
except CancelledError:
trace "statusLogLoop canceled"

proc start*(n: BeaconNetwork) =
info "Starting Portal beacon chain network"

n.portalProtocol.start()
n.processContentLoop = processContentLoop(n)

proc stop*(n: BeaconNetwork) =
info "Stopping Portal beacon chain network"

n.portalProtocol.stop()

if not n.processContentLoop.isNil:
n.processContentLoop.cancelSoon()

if not n.statusLogLoop.isNil():
n.statusLogLoop.cancelSoon()
15 changes: 4 additions & 11 deletions fluffy/network/history/history_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -704,17 +704,7 @@ proc processContentLoop(n: HistoryNetwork) {.async: (raises: []).} =
proc statusLogLoop(n: HistoryNetwork) {.async: (raises: []).} =
try:
while true:
# This is the data radius percentage compared to full storage. This will
# drop a lot when using the logbase2 scale, namely `/ 2` per 1 logaritmic
# radius drop.
# TODO: Get some float precision calculus?
let radiusPercentage =
n.portalProtocol.dataRadius() div (UInt256.high() div u256(100))

info "History network status",
radiusPercentage = radiusPercentage.toString(10) & "%",
radius = n.portalProtocol.dataRadius().toHex(),
dbSize = $(n.contentDB.size() div 1000) & "kb",
routingTableNodes = n.portalProtocol.routingTable.len()

await sleepAsync(60.seconds)
Expand All @@ -725,17 +715,20 @@ proc start*(n: HistoryNetwork) =
info "Starting Portal execution history network",
protocolId = n.portalProtocol.protocolId,
accumulatorRoot = hash_tree_root(n.accumulator)

n.portalProtocol.start()

n.processContentLoop = processContentLoop(n)
n.statusLogLoop = statusLogLoop(n)
pruneDeprecatedAccumulatorRecords(n.accumulator, n.contentDB)

proc stop*(n: HistoryNetwork) =
info "Stopping Portal execution history network"

n.portalProtocol.stop()

if not n.processContentLoop.isNil:
n.processContentLoop.cancelSoon()

if not n.processContentLoop.isNil:
if not n.statusLogLoop.isNil:
n.statusLogLoop.cancelSoon()
1 change: 0 additions & 1 deletion fluffy/network/state/state_gossip.nim
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ proc gossipOffer*(
debug "Offered content gossipped successfully with peers", keyBytes, peers

# Currently only used for testing to gossip an entire account trie proof
# This may also be useful for the state network bridge
proc recursiveGossipOffer*(
p: PortalProtocol,
srcNodeId: Opt[NodeId],
Expand Down
20 changes: 19 additions & 1 deletion fluffy/network/state/state_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type StateNetwork* = ref object
contentDB*: ContentDB
contentQueue*: AsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])]
processContentLoop: Future[void]
statusLogLoop: Future[void]
historyNetwork: Opt[HistoryNetwork]
validateStateIsCanonical: bool

Expand Down Expand Up @@ -221,15 +222,32 @@ proc processContentLoop(n: StateNetwork) {.async: (raises: []).} =
except CancelledError:
trace "processContentLoop canceled"

proc statusLogLoop(n: StateNetwork) {.async: (raises: []).} =
try:
while true:
info "State network status",
routingTableNodes = n.portalProtocol.routingTable.len()

await sleepAsync(60.seconds)
except CancelledError:
trace "statusLogLoop canceled"

proc start*(n: StateNetwork) =
info "Starting Portal execution state network",
protocolId = n.portalProtocol.protocolId

n.portalProtocol.start()

n.processContentLoop = processContentLoop(n)
n.statusLogLoop = statusLogLoop(n)

proc stop*(n: StateNetwork) =
info "Stopping Portal execution state network"

n.portalProtocol.stop()

if not n.processContentLoop.isNil:
if not n.processContentLoop.isNil():
n.processContentLoop.cancelSoon()

if not n.statusLogLoop.isNil():
n.statusLogLoop.cancelSoon()
40 changes: 40 additions & 0 deletions fluffy/portal_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import
results,
chronos,
eth/p2p/discoveryv5/protocol,
beacon_chain/spec/forks,
./network_metadata,
Expand Down Expand Up @@ -39,6 +40,7 @@ type
historyNetwork*: Opt[HistoryNetwork]
stateNetwork*: Opt[StateNetwork]
beaconLightClient*: Opt[LightClient]
statusLogLoop: Future[void]

# Beacon light client application callbacks triggered when new finalized header
# or optimistic header is available.
Expand Down Expand Up @@ -179,7 +181,27 @@ proc new*(
beaconLightClient: beaconLightClient,
)

proc statusLogLoop(n: PortalNode) {.async: (raises: []).} =
try:
while true:
# This is the data radius percentage compared to full storage. This will
# drop a lot when using the logbase2 scale, namely `/ 2` per 1 logaritmic
# radius drop.
# TODO: Get some float precision calculus?
let radiusPercentage = n.contentDB.dataRadius div (UInt256.high() div u256(100))

info "Portal node status",
radiusPercentage = radiusPercentage.toString(10) & "%",
radius = n.contentDB.dataRadius.toHex(),
dbSize = $(n.contentDB.size() div 1000) & "kb"

await sleepAsync(60.seconds)
except CancelledError:
trace "statusLogLoop canceled"

proc start*(n: PortalNode) =
debug "Starting Portal node"

if n.beaconNetwork.isSome():
n.beaconNetwork.value.start()
if n.historyNetwork.isSome():
Expand All @@ -189,3 +211,21 @@ proc start*(n: PortalNode) =

if n.beaconLightClient.isSome():
n.beaconLightClient.value.start()

n.statusLogLoop = statusLogLoop(n)

proc stop*(n: PortalNode) =
debug "Stopping Portal node"

if n.beaconNetwork.isSome():
n.beaconNetwork.value.stop()
if n.historyNetwork.isSome():
n.historyNetwork.value.stop()
if n.stateNetwork.isSome():
n.stateNetwork.value.stop()

if n.beaconLightClient.isSome():
n.beaconLightClient.value.stop()

if not n.statusLogLoop.isNil:
n.statusLogLoop.cancelSoon()
4 changes: 3 additions & 1 deletion fluffy/tools/portal_bridge/portal_bridge_state.nim
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ proc runBackfillGossipBlockOffersLoop(
for k, v in offersMap:
try:
let numPeers = await portalClient.portal_stateGossip(k.to0xHex(), v.to0xHex())
if numPeers == 0:
if numPeers > 0:
debug "Offer successfully gossipped to peers: ", numPeers, workerId
elif numPeers == 0:
warn "Offer gossipped to no peers", workerId
retryGossip = true
break
Expand Down

0 comments on commit a9ad10c

Please sign in to comment.