2023-12-01 16:20:52 +00:00
|
|
|
# Fluffy
|
2024-01-09 09:32:29 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-10-09 11:22:03 +00:00
|
|
|
# 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).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-09-03 08:57:19 +00:00
|
|
|
import
|
2024-05-16 08:16:16 +00:00
|
|
|
results,
|
2024-02-28 17:31:45 +00:00
|
|
|
chronos,
|
|
|
|
chronicles,
|
2024-10-30 13:17:16 +00:00
|
|
|
metrics,
|
2024-10-10 13:24:39 +00:00
|
|
|
eth/common/hashes,
|
2022-01-06 08:06:05 +00:00
|
|
|
eth/p2p/discoveryv5/[protocol, enr],
|
2023-12-01 16:20:52 +00:00
|
|
|
../../database/content_db,
|
2024-05-16 08:16:16 +00:00
|
|
|
../history/history_network,
|
2022-01-18 08:01:22 +00:00
|
|
|
../wire/[portal_protocol, portal_stream, portal_protocol_config],
|
2024-05-06 15:13:35 +00:00
|
|
|
./state_content,
|
2024-05-22 01:22:07 +00:00
|
|
|
./state_validation,
|
|
|
|
./state_gossip
|
2021-09-22 15:07:14 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
export results, state_content, hashes
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2022-04-13 05:56:01 +00:00
|
|
|
logScope:
|
|
|
|
topics = "portal_state"
|
|
|
|
|
2024-10-30 13:17:16 +00:00
|
|
|
declareCounter state_network_offers_success,
|
|
|
|
"Portal state network offers successfully validated", labels = ["protocol_id"]
|
|
|
|
declareCounter state_network_offers_failed,
|
|
|
|
"Portal state network offers which failed validation", labels = ["protocol_id"]
|
|
|
|
|
2021-09-22 15:07:14 +00:00
|
|
|
type StateNetwork* = ref object
|
2021-09-03 08:57:19 +00:00
|
|
|
portalProtocol*: PortalProtocol
|
2023-09-04 10:21:01 +00:00
|
|
|
contentQueue*: AsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])]
|
2022-07-11 14:29:16 +00:00
|
|
|
processContentLoop: Future[void]
|
2024-09-19 13:38:49 +00:00
|
|
|
statusLogLoop: Future[void]
|
2024-05-16 08:16:16 +00:00
|
|
|
historyNetwork: Opt[HistoryNetwork]
|
2024-06-11 13:01:35 +00:00
|
|
|
validateStateIsCanonical: bool
|
2024-10-25 07:15:01 +00:00
|
|
|
contentRequestRetries: int
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2024-07-17 15:07:27 +00:00
|
|
|
func toContentIdHandler(contentKey: ContentKeyByteList): results.Opt[ContentId] =
|
2024-01-19 17:18:57 +00:00
|
|
|
ok(toContentId(contentKey))
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2024-05-22 01:22:07 +00:00
|
|
|
proc new*(
|
|
|
|
T: type StateNetwork,
|
2024-06-18 07:32:57 +00:00
|
|
|
portalNetwork: PortalNetwork,
|
2024-05-22 01:22:07 +00:00
|
|
|
baseProtocol: protocol.Protocol,
|
|
|
|
contentDB: ContentDB,
|
|
|
|
streamManager: StreamManager,
|
|
|
|
bootstrapRecords: openArray[Record] = [],
|
|
|
|
portalConfig: PortalProtocolConfig = defaultPortalProtocolConfig,
|
|
|
|
historyNetwork = Opt.none(HistoryNetwork),
|
2024-06-11 13:01:35 +00:00
|
|
|
validateStateIsCanonical = true,
|
2024-10-25 07:15:01 +00:00
|
|
|
contentRequestRetries = 1,
|
2024-05-22 01:22:07 +00:00
|
|
|
): T =
|
2024-10-10 13:24:39 +00:00
|
|
|
let
|
|
|
|
cq = newAsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])](50)
|
|
|
|
s = streamManager.registerNewStream(cq)
|
|
|
|
portalProtocol = PortalProtocol.new(
|
|
|
|
baseProtocol,
|
|
|
|
getProtocolId(portalNetwork, PortalSubnetwork.state),
|
|
|
|
toContentIdHandler,
|
|
|
|
createGetHandler(contentDB),
|
|
|
|
createStoreHandler(contentDB, portalConfig.radiusConfig),
|
2024-11-04 14:02:51 +00:00
|
|
|
createContainsHandler(contentDB),
|
2024-10-10 13:24:39 +00:00
|
|
|
createRadiusHandler(contentDB),
|
|
|
|
s,
|
|
|
|
bootstrapRecords,
|
|
|
|
config = portalConfig,
|
|
|
|
)
|
|
|
|
|
|
|
|
StateNetwork(
|
2024-05-22 01:22:07 +00:00
|
|
|
portalProtocol: portalProtocol,
|
|
|
|
contentQueue: cq,
|
|
|
|
historyNetwork: historyNetwork,
|
2024-06-11 13:01:35 +00:00
|
|
|
validateStateIsCanonical: validateStateIsCanonical,
|
2024-10-25 07:15:01 +00:00
|
|
|
contentRequestRetries: contentRequestRetries,
|
2024-05-22 01:22:07 +00:00
|
|
|
)
|
|
|
|
|
2024-05-24 07:49:51 +00:00
|
|
|
proc getContent(
|
|
|
|
n: StateNetwork,
|
|
|
|
key: AccountTrieNodeKey | ContractTrieNodeKey | ContractCodeKey,
|
|
|
|
V: type ContentRetrievalType,
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[V]] {.async: (raises: [CancelledError]).} =
|
2021-09-24 09:22:07 +00:00
|
|
|
let
|
2024-05-24 07:49:51 +00:00
|
|
|
contentKeyBytes = key.toContentKey().encode()
|
2024-05-22 01:22:07 +00:00
|
|
|
contentId = contentKeyBytes.toContentId()
|
2024-10-16 13:05:39 +00:00
|
|
|
maybeLocalContent = n.portalProtocol.getLocalContent(contentKeyBytes, contentId)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-10-16 13:05:39 +00:00
|
|
|
if maybeLocalContent.isSome():
|
|
|
|
let contentValue = V.decode(maybeLocalContent.get()).valueOr:
|
2024-10-30 13:17:16 +00:00
|
|
|
raiseAssert("Unable to decode state local content value")
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-10-16 13:05:39 +00:00
|
|
|
info "Fetched state local content value"
|
|
|
|
return Opt.some(contentValue)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-10-25 07:15:01 +00:00
|
|
|
for i in 0 ..< (1 + n.contentRequestRetries):
|
|
|
|
let
|
|
|
|
contentLookupResult = (
|
|
|
|
await n.portalProtocol.contentLookup(contentKeyBytes, contentId)
|
|
|
|
).valueOr:
|
|
|
|
warn "Failed fetching state content from the network"
|
|
|
|
return Opt.none(V)
|
|
|
|
contentValueBytes = contentLookupResult.content
|
|
|
|
|
|
|
|
let contentValue = V.decode(contentValueBytes).valueOr:
|
2024-10-30 13:17:16 +00:00
|
|
|
error "Unable to decode state content value from content lookup"
|
2024-10-25 07:15:01 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
validateRetrieval(key, contentValue).isOkOr:
|
2024-10-30 13:17:16 +00:00
|
|
|
error "Validation of retrieved state content failed"
|
2024-10-25 07:15:01 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
info "Fetched valid state content from the network"
|
|
|
|
n.portalProtocol.storeContent(
|
|
|
|
contentKeyBytes, contentId, contentValueBytes, cacheContent = true
|
|
|
|
)
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-10-25 07:15:01 +00:00
|
|
|
return Opt.some(contentValue)
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-10-25 07:15:01 +00:00
|
|
|
# Content was requested `1 + requestRetries` times and all failed on validation
|
|
|
|
Opt.none(V)
|
2024-05-24 07:49:51 +00:00
|
|
|
|
|
|
|
proc getAccountTrieNode*(
|
|
|
|
n: StateNetwork, key: AccountTrieNodeKey
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[AccountTrieNodeRetrieval]] {.
|
|
|
|
async: (raw: true, raises: [CancelledError])
|
|
|
|
.} =
|
2024-05-24 07:49:51 +00:00
|
|
|
n.getContent(key, AccountTrieNodeRetrieval)
|
|
|
|
|
|
|
|
proc getContractTrieNode*(
|
|
|
|
n: StateNetwork, key: ContractTrieNodeKey
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[ContractTrieNodeRetrieval]] {.
|
|
|
|
async: (raw: true, raises: [CancelledError])
|
|
|
|
.} =
|
2024-05-24 07:49:51 +00:00
|
|
|
n.getContent(key, ContractTrieNodeRetrieval)
|
|
|
|
|
|
|
|
proc getContractCode*(
|
|
|
|
n: StateNetwork, key: ContractCodeKey
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Opt[ContractCodeRetrieval]] {.async: (raw: true, raises: [CancelledError]).} =
|
2024-05-24 07:49:51 +00:00
|
|
|
n.getContent(key, ContractCodeRetrieval)
|
|
|
|
|
2024-09-13 06:51:16 +00:00
|
|
|
proc getStateRootByBlockNumOrHash*(
|
2024-10-07 20:39:07 +00:00
|
|
|
n: StateNetwork, blockNumOrHash: uint64 | Hash32
|
2024-10-04 21:21:26 +00:00
|
|
|
): Future[Opt[Hash32]] {.async: (raises: [CancelledError]).} =
|
2024-10-10 13:24:39 +00:00
|
|
|
let hn = n.historyNetwork.valueOr:
|
2024-06-20 15:41:34 +00:00
|
|
|
warn "History network is not available"
|
2024-10-04 21:21:26 +00:00
|
|
|
return Opt.none(Hash32)
|
2024-02-15 15:37:05 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
let header = (await hn.getVerifiedBlockHeader(blockNumOrHash)).valueOr:
|
2024-09-13 06:51:16 +00:00
|
|
|
warn "Failed to get block header from history", blockNumOrHash
|
2024-10-04 21:21:26 +00:00
|
|
|
return Opt.none(Hash32)
|
2024-02-15 15:37:05 +00:00
|
|
|
|
2024-05-16 08:16:16 +00:00
|
|
|
Opt.some(header.stateRoot)
|
2024-02-15 15:37:05 +00:00
|
|
|
|
2024-05-30 09:00:57 +00:00
|
|
|
proc processOffer*(
|
2024-05-22 01:22:07 +00:00
|
|
|
n: StateNetwork,
|
2024-02-15 15:37:05 +00:00
|
|
|
maybeSrcNodeId: Opt[NodeId],
|
2024-07-17 15:07:27 +00:00
|
|
|
contentKeyBytes: ContentKeyByteList,
|
2024-05-24 07:49:51 +00:00
|
|
|
contentValueBytes: seq[byte],
|
|
|
|
contentKey: AccountTrieNodeKey | ContractTrieNodeKey | ContractCodeKey,
|
|
|
|
V: type ContentOfferType,
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Result[void, string]] {.async: (raises: [CancelledError]).} =
|
2024-10-10 13:24:39 +00:00
|
|
|
let
|
|
|
|
contentValue = V.decode(contentValueBytes).valueOr:
|
|
|
|
return err("Unable to decode offered content value")
|
|
|
|
validationRes =
|
|
|
|
if n.validateStateIsCanonical:
|
|
|
|
let stateRoot = (await n.getStateRootByBlockNumOrHash(contentValue.blockHash)).valueOr:
|
|
|
|
return err("Failed to get state root by block hash")
|
|
|
|
validateOffer(Opt.some(stateRoot), contentKey, contentValue)
|
|
|
|
else:
|
|
|
|
# Skip state root validation
|
|
|
|
validateOffer(Opt.none(Hash32), contentKey, contentValue)
|
|
|
|
|
|
|
|
if validationRes.isErr():
|
|
|
|
return err("Offered content failed validation: " & validationRes.error())
|
2022-08-17 07:32:06 +00:00
|
|
|
|
2024-05-22 01:22:07 +00:00
|
|
|
let contentId = n.portalProtocol.toContentId(contentKeyBytes).valueOr:
|
|
|
|
return err("Received offered content with invalid content key")
|
2022-08-17 07:32:06 +00:00
|
|
|
|
2024-05-22 01:22:07 +00:00
|
|
|
n.portalProtocol.storeContent(
|
|
|
|
contentKeyBytes, contentId, contentValue.toRetrievalValue().encode()
|
2024-02-28 17:31:45 +00:00
|
|
|
)
|
2021-09-13 13:56:44 +00:00
|
|
|
|
2024-06-20 15:41:34 +00:00
|
|
|
await gossipOffer(
|
2024-10-10 13:24:39 +00:00
|
|
|
n.portalProtocol, maybeSrcNodeId, contentKeyBytes, contentValueBytes
|
2024-05-24 07:49:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ok()
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2024-06-14 12:21:30 +00:00
|
|
|
proc processContentLoop(n: StateNetwork) {.async: (raises: []).} =
|
2022-07-11 14:29:16 +00:00
|
|
|
try:
|
|
|
|
while true:
|
2024-05-24 07:49:51 +00:00
|
|
|
let (srcNodeId, contentKeys, contentValues) = await n.contentQueue.popFirst()
|
2024-05-30 09:00:57 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
for i, contentBytes in contentValues:
|
2024-02-19 14:35:53 +00:00
|
|
|
let
|
2024-05-22 01:22:07 +00:00
|
|
|
contentKeyBytes = contentKeys[i]
|
2024-05-30 09:00:57 +00:00
|
|
|
contentKey = ContentKey.decode(contentKeyBytes).valueOr:
|
2024-05-22 01:22:07 +00:00
|
|
|
error "Unable to decode offered content key", contentKeyBytes
|
2024-02-19 14:35:53 +00:00
|
|
|
continue
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2024-10-10 13:24:39 +00:00
|
|
|
offerRes =
|
|
|
|
case contentKey.contentType
|
|
|
|
of unused:
|
|
|
|
error "Received content with unused content type"
|
|
|
|
continue
|
|
|
|
of accountTrieNode:
|
|
|
|
await n.processOffer(
|
|
|
|
srcNodeId, contentKeyBytes, contentBytes, contentKey.accountTrieNodeKey,
|
|
|
|
AccountTrieNodeOffer,
|
|
|
|
)
|
|
|
|
of contractTrieNode:
|
|
|
|
await n.processOffer(
|
|
|
|
srcNodeId, contentKeyBytes, contentBytes,
|
|
|
|
contentKey.contractTrieNodeKey, ContractTrieNodeOffer,
|
|
|
|
)
|
|
|
|
of contractCode:
|
|
|
|
await n.processOffer(
|
|
|
|
srcNodeId, contentKeyBytes, contentBytes, contentKey.contractCodeKey,
|
|
|
|
ContractCodeOffer,
|
|
|
|
)
|
2024-10-30 13:17:16 +00:00
|
|
|
|
2024-05-22 01:22:07 +00:00
|
|
|
if offerRes.isOk():
|
2024-10-30 13:17:16 +00:00
|
|
|
state_network_offers_success.inc(labelValues = [$n.portalProtocol.protocolId])
|
|
|
|
debug "Received offered content validated successfully", contentKeyBytes
|
2024-05-22 01:22:07 +00:00
|
|
|
else:
|
2024-10-30 13:17:16 +00:00
|
|
|
state_network_offers_failed.inc(labelValues = [$n.portalProtocol.protocolId])
|
|
|
|
error "Received offered content failed validation",
|
2024-05-22 01:22:07 +00:00
|
|
|
contentKeyBytes, error = offerRes.error()
|
2022-07-11 14:29:16 +00:00
|
|
|
except CancelledError:
|
|
|
|
trace "processContentLoop canceled"
|
|
|
|
|
2024-09-19 13:38:49 +00:00
|
|
|
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"
|
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
proc start*(n: StateNetwork) =
|
2024-06-14 17:03:18 +00:00
|
|
|
info "Starting Portal execution state network",
|
|
|
|
protocolId = n.portalProtocol.protocolId
|
2024-09-19 13:38:49 +00:00
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
n.portalProtocol.start()
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
n.processContentLoop = processContentLoop(n)
|
2024-09-19 13:38:49 +00:00
|
|
|
n.statusLogLoop = statusLogLoop(n)
|
2022-07-11 14:29:16 +00:00
|
|
|
|
2024-09-20 12:54:36 +00:00
|
|
|
proc stop*(n: StateNetwork) {.async: (raises: []).} =
|
2024-09-19 13:38:49 +00:00
|
|
|
info "Stopping Portal execution state network"
|
|
|
|
|
2024-09-20 12:54:36 +00:00
|
|
|
var futures: seq[Future[void]]
|
|
|
|
futures.add(n.portalProtocol.stop())
|
2022-07-11 14:29:16 +00:00
|
|
|
|
2024-09-19 13:38:49 +00:00
|
|
|
if not n.processContentLoop.isNil():
|
2024-09-20 12:54:36 +00:00
|
|
|
futures.add(n.processContentLoop.cancelAndWait())
|
2024-09-19 13:38:49 +00:00
|
|
|
if not n.statusLogLoop.isNil():
|
2024-09-20 12:54:36 +00:00
|
|
|
futures.add(n.statusLogLoop.cancelAndWait())
|
|
|
|
|
|
|
|
await noCancel(allFutures(futures))
|
|
|
|
|
|
|
|
n.processContentLoop = nil
|
|
|
|
n.statusLogLoop = nil
|