1212 chronos,
1313 eth/ p2p/ discoveryv5/ protocol,
1414 beacon_chain/ spec/ forks,
15- stew/ byteutils,
1615 ../ eth_history/ history_data_ssz_e2s,
1716 ../ database/ content_db,
1817 ./ wire/ [portal_stream, portal_protocol_config],
3534
3635 PortalNode * = ref object
3736 discovery: protocol.Protocol
38- contentDB: ContentDB
3937 streamManager: StreamManager
4038 historyNetwork* : Opt [HistoryNetwork ]
4139 beaconNetwork* : Opt [BeaconNetwork ]
@@ -58,13 +56,7 @@ proc onOptimisticHeader(
5856 when lcDataFork > LightClientDataFork .None :
5957 info " New LC optimistic header" , optimistic_header = shortLog (forkyHeader)
6058
61- proc getDbDirectory * (network: PortalNetwork ): string =
62- if network == PortalNetwork .mainnet:
63- " db"
64- else :
65- " db_" & network.symbolName ()
66-
67- const dbDir = " portaldb"
59+ const dbDir* = " portaldb"
6860
6961proc new * (
7062 T: type PortalNode ,
@@ -77,17 +69,6 @@ proc new*(
7769 rng = newRng (),
7870): T =
7971 let
80- # Store the database at contentdb prefixed with the first 8 chars of node id.
81- # This is done because the content in the db is dependant on the `NodeId` and
82- # the selected `Radius`.
83- contentDB = ContentDB .new (
84- config.dataDir / dbDir / " contentdb_" &
85- discovery.localNode.id.toBytesBE ().toOpenArray (0 , 8 ).toHex (),
86- storageCapacity = config.storageCapacity,
87- radiusConfig = config.portalConfig.radiusConfig,
88- localId = discovery.localNode.id,
89- )
90-
9172 networkData =
9273 case network
9374 of PortalNetwork .mainnet:
@@ -99,6 +80,16 @@ proc new*(
9980
10081 historyNetwork =
10182 if PortalSubnetwork .history in subnetworks:
83+ # Store the database at contentdb prefixed with the first 8 chars of node id.
84+ # This is done because the content in the db is dependant on the `NodeId` and
85+ # the selected `Radius`.
86+ let contentDB = ContentDB .new (
87+ config.dataDir / dbDir,
88+ storageCapacity = config.storageCapacity,
89+ radiusConfig = config.portalConfig.radiusConfig,
90+ localId = discovery.localNode.id,
91+ subnetwork = PortalSubnetwork .history,
92+ )
10293 Opt .some (
10394 HistoryNetwork .new (
10495 network,
@@ -119,7 +110,7 @@ proc new*(
119110 beaconNetwork =
120111 if PortalSubnetwork .beacon in subnetworks:
121112 let
122- beaconDb = BeaconDb .new (networkData, config.dataDir / dbDir / " beacondb " )
113+ beaconDb = BeaconDb .new (networkData, config.dataDir / dbDir)
123114 beaconNetwork = BeaconNetwork .new (
124115 network,
125116 discovery,
@@ -158,7 +149,6 @@ proc new*(
158149
159150 PortalNode (
160151 discovery: discovery,
161- contentDB: contentDB,
162152 streamManager: streamManager,
163153 historyNetwork: historyNetwork,
164154 beaconNetwork: beaconNetwork,
@@ -172,16 +162,16 @@ proc statusLogLoop(n: PortalNode) {.async: (raises: []).} =
172162 # drop a lot when using the logbase2 scale, namely `/ 2` per 1 logaritmic
173163 # radius drop.
174164 # TODO : Get some float precision calculus?
175- let
176- radius = n.contentDB.dataRadius
177- radiusPercentage = radius div (UInt256 .high () div u256 (100 ))
178- logRadius = logDistance (radius, u256 (0 ))
165+ if n.historyNetwork.isSome ():
166+ let
167+ radius = n.historyNetwork.value.contentDB.dataRadius
168+ radiusPercentage = radius div (UInt256 .high () div u256 (100 ))
169+ logRadius = logDistance (radius, u256 (0 ))
179170
180- info " Portal node status" ,
181- dbSize = $ (n.contentDB.size () div 1_000_000 ) & " mb" ,
182- radiusPercentage = radiusPercentage.toString (10 ) & " %" ,
183- radius = radius.toHex (),
184- logRadius
171+ info " Portal node status" ,
172+ radiusPercentage = radiusPercentage.toString (10 ) & " %" ,
173+ radius = radius.toHex (),
174+ logRadius
185175
186176 await sleepAsync (60 .seconds)
187177 except CancelledError :
@@ -206,17 +196,16 @@ proc stop*(n: PortalNode) {.async: (raises: []).} =
206196
207197 var futures: seq [Future [void ]]
208198
199+ if not n.statusLogLoop.isNil ():
200+ futures.add (n.statusLogLoop.cancelAndWait ())
209201 if n.historyNetwork.isSome ():
210202 futures.add (n.historyNetwork.value.stop ())
211203 if n.beaconNetwork.isSome ():
212204 futures.add (n.beaconNetwork.value.stop ())
213205 if n.beaconLightClient.isSome ():
214206 futures.add (n.beaconLightClient.value.stop ())
215- if not n.statusLogLoop.isNil ():
216- futures.add (n.statusLogLoop.cancelAndWait ())
217207
218208 await noCancel (allFutures (futures))
219209
220210 await n.discovery.closeWait ()
221- n.contentDB.close ()
222211 n.statusLogLoop = nil
0 commit comments