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.
|
|
|
|
|
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-02-15 15:37:05 +00:00
|
|
|
eth/common/eth_hash,
|
|
|
|
eth/common,
|
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-05-24 07:49:51 +00:00
|
|
|
export results, state_content
|
2024-05-16 08:16:16 +00:00
|
|
|
|
2022-04-13 05:56:01 +00:00
|
|
|
logScope:
|
|
|
|
topics = "portal_state"
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
const stateProtocolId* = [byte 0x50, 0x0A]
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2021-09-22 15:07:14 +00:00
|
|
|
type StateNetwork* = ref object
|
2021-09-03 08:57:19 +00:00
|
|
|
portalProtocol*: PortalProtocol
|
2021-09-28 17:58:41 +00:00
|
|
|
contentDB*: ContentDB
|
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-05-16 08:16:16 +00:00
|
|
|
historyNetwork: Opt[HistoryNetwork]
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2022-11-08 17:31:45 +00:00
|
|
|
func toContentIdHandler(contentKey: ByteList): 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,
|
|
|
|
baseProtocol: protocol.Protocol,
|
|
|
|
contentDB: ContentDB,
|
|
|
|
streamManager: StreamManager,
|
|
|
|
bootstrapRecords: openArray[Record] = [],
|
|
|
|
portalConfig: PortalProtocolConfig = defaultPortalProtocolConfig,
|
|
|
|
historyNetwork = Opt.none(HistoryNetwork),
|
|
|
|
): T =
|
|
|
|
let cq = newAsyncQueue[(Opt[NodeId], ContentKeysList, seq[seq[byte]])](50)
|
|
|
|
|
|
|
|
let s = streamManager.registerNewStream(cq)
|
|
|
|
|
|
|
|
let portalProtocol = PortalProtocol.new(
|
|
|
|
baseProtocol,
|
|
|
|
stateProtocolId,
|
|
|
|
toContentIdHandler,
|
|
|
|
createGetHandler(contentDB),
|
|
|
|
s,
|
|
|
|
bootstrapRecords,
|
|
|
|
config = portalConfig,
|
|
|
|
)
|
|
|
|
|
|
|
|
portalProtocol.dbPut =
|
|
|
|
createStoreHandler(contentDB, portalConfig.radiusConfig, portalProtocol)
|
|
|
|
|
|
|
|
return StateNetwork(
|
|
|
|
portalProtocol: portalProtocol,
|
|
|
|
contentDB: contentDB,
|
|
|
|
contentQueue: cq,
|
|
|
|
historyNetwork: historyNetwork,
|
|
|
|
)
|
|
|
|
|
2024-05-24 07:49:51 +00:00
|
|
|
proc getContent(
|
|
|
|
n: StateNetwork,
|
|
|
|
key: AccountTrieNodeKey | ContractTrieNodeKey | ContractCodeKey,
|
|
|
|
V: type ContentRetrievalType,
|
|
|
|
): Future[Opt[V]] {.async.} =
|
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()
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-05-24 07:49:51 +00:00
|
|
|
if n.portalProtocol.inRange(contentId):
|
2021-10-09 11:22:03 +00:00
|
|
|
let contentFromDB = n.contentDB.get(contentId)
|
|
|
|
if contentFromDB.isSome():
|
2024-05-24 07:49:51 +00:00
|
|
|
let contentValue = V.decode(contentFromDB.get()).valueOr:
|
|
|
|
error "Unable to decode account trie node content value from database"
|
|
|
|
return Opt.none(V)
|
|
|
|
|
|
|
|
info "Fetched account trie node from database"
|
|
|
|
return Opt.some(contentValue)
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-05-22 01:22:07 +00:00
|
|
|
let
|
|
|
|
contentLookupResult = (
|
|
|
|
await n.portalProtocol.contentLookup(contentKeyBytes, contentId)
|
|
|
|
).valueOr:
|
2024-05-24 07:49:51 +00:00
|
|
|
return Opt.none(V)
|
2024-05-22 01:22:07 +00:00
|
|
|
contentValueBytes = contentLookupResult.content
|
2021-10-09 11:22:03 +00:00
|
|
|
|
2024-05-24 07:49:51 +00:00
|
|
|
let contentValue = V.decode(contentValueBytes).valueOr:
|
|
|
|
error "Unable to decode account trie node content value from content lookup"
|
|
|
|
return Opt.none(V)
|
|
|
|
|
|
|
|
validateRetrieval(key, contentValue).isOkOr:
|
|
|
|
error "Validation of retrieved content failed"
|
|
|
|
return Opt.none(V)
|
|
|
|
|
|
|
|
n.portalProtocol.storeContent(contentKeyBytes, contentId, contentValueBytes)
|
|
|
|
|
|
|
|
return Opt.some(contentValue)
|
|
|
|
|
|
|
|
proc getAccountTrieNode*(
|
|
|
|
n: StateNetwork, key: AccountTrieNodeKey
|
|
|
|
): Future[Opt[AccountTrieNodeRetrieval]] {.inline.} =
|
|
|
|
n.getContent(key, AccountTrieNodeRetrieval)
|
|
|
|
|
|
|
|
proc getContractTrieNode*(
|
|
|
|
n: StateNetwork, key: ContractTrieNodeKey
|
|
|
|
): Future[Opt[ContractTrieNodeRetrieval]] {.inline.} =
|
|
|
|
n.getContent(key, ContractTrieNodeRetrieval)
|
|
|
|
|
|
|
|
proc getContractCode*(
|
|
|
|
n: StateNetwork, key: ContractCodeKey
|
|
|
|
): Future[Opt[ContractCodeRetrieval]] {.inline.} =
|
|
|
|
n.getContent(key, ContractCodeRetrieval)
|
|
|
|
|
2024-05-16 08:16:16 +00:00
|
|
|
proc getStateRootByBlockHash(
|
|
|
|
n: StateNetwork, hash: BlockHash
|
|
|
|
): Future[Opt[KeccakHash]] {.async.} =
|
|
|
|
if n.historyNetwork.isNone():
|
|
|
|
warn "History network is not available. Unable to get state root by block hash"
|
|
|
|
return Opt.none(KeccakHash)
|
2024-02-15 15:37:05 +00:00
|
|
|
|
2024-05-16 08:16:16 +00:00
|
|
|
let header = (await n.historyNetwork.get().getVerifiedBlockHeader(hash)).valueOr:
|
|
|
|
warn "Failed to get block header by hash", hash
|
|
|
|
return Opt.none(KeccakHash)
|
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-05-22 01:22:07 +00:00
|
|
|
contentKeyBytes: ByteList,
|
2024-05-24 07:49:51 +00:00
|
|
|
contentValueBytes: seq[byte],
|
|
|
|
contentKey: AccountTrieNodeKey | ContractTrieNodeKey | ContractCodeKey,
|
|
|
|
V: type ContentOfferType,
|
2024-05-22 01:22:07 +00:00
|
|
|
): Future[Result[void, string]] {.async.} =
|
2024-05-24 07:49:51 +00:00
|
|
|
let contentValue = V.decode(contentValueBytes).valueOr:
|
|
|
|
return err("Unable to decode offered content value")
|
2024-02-15 15:37:05 +00:00
|
|
|
|
2024-05-22 01:22:07 +00:00
|
|
|
let stateRoot = (await n.getStateRootByBlockHash(contentValue.blockHash)).valueOr:
|
|
|
|
return err("Failed to get state root by block hash")
|
2024-02-15 15:37:05 +00:00
|
|
|
|
2024-05-22 01:22:07 +00:00
|
|
|
let res = validateOffer(stateRoot, contentKey, contentValue)
|
|
|
|
if res.isErr():
|
2024-05-28 00:45:30 +00:00
|
|
|
return err("Offered content failed validation: " & res.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
|
|
|
)
|
2024-05-28 00:45:30 +00:00
|
|
|
info "Offered content validated successfully", contentKeyBytes
|
2021-09-13 13:56:44 +00:00
|
|
|
|
2024-05-24 07:49:51 +00:00
|
|
|
asyncSpawn gossipOffer(
|
|
|
|
n.portalProtocol, maybeSrcNodeId, contentKeyBytes, contentValueBytes, contentKey,
|
|
|
|
contentValue,
|
|
|
|
)
|
|
|
|
|
|
|
|
ok()
|
2021-09-03 08:57:19 +00:00
|
|
|
|
2022-07-11 14:29:16 +00:00
|
|
|
proc processContentLoop(n: StateNetwork) {.async.} =
|
|
|
|
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-05-22 01:22:07 +00:00
|
|
|
for i, contentValueBytes 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-05-22 01:22:07 +00:00
|
|
|
let offerRes =
|
|
|
|
case contentKey.contentType
|
|
|
|
of unused:
|
|
|
|
error "Received content with unused content type"
|
2024-05-16 08:16:16 +00:00
|
|
|
continue
|
2024-05-22 01:22:07 +00:00
|
|
|
of accountTrieNode:
|
2024-05-24 07:49:51 +00:00
|
|
|
await n.processOffer(
|
|
|
|
srcNodeId, contentKeyBytes, contentValueBytes,
|
|
|
|
contentKey.accountTrieNodeKey, AccountTrieNodeOffer,
|
2024-05-22 01:22:07 +00:00
|
|
|
)
|
|
|
|
of contractTrieNode:
|
2024-05-24 07:49:51 +00:00
|
|
|
await n.processOffer(
|
|
|
|
srcNodeId, contentKeyBytes, contentValueBytes,
|
|
|
|
contentKey.contractTrieNodeKey, ContractTrieNodeOffer,
|
2024-05-22 01:22:07 +00:00
|
|
|
)
|
|
|
|
of contractCode:
|
2024-05-24 07:49:51 +00:00
|
|
|
await n.processOffer(
|
|
|
|
srcNodeId, contentKeyBytes, contentValueBytes, contentKey.contractCodeKey,
|
|
|
|
ContractCodeOffer,
|
2024-05-22 01:22:07 +00:00
|
|
|
)
|
|
|
|
if offerRes.isOk():
|
|
|
|
info "Offered content processed successfully", contentKeyBytes
|
|
|
|
else:
|
|
|
|
error "Offered content processing failed",
|
|
|
|
contentKeyBytes, error = offerRes.error()
|
2022-07-11 14:29:16 +00:00
|
|
|
except CancelledError:
|
|
|
|
trace "processContentLoop canceled"
|
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
proc start*(n: StateNetwork) =
|
2024-05-30 09:00:57 +00:00
|
|
|
info "Starting Portal State Network", protocolId = n.portalProtocol.protocolId
|
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)
|
|
|
|
|
2021-10-09 11:22:03 +00:00
|
|
|
proc stop*(n: StateNetwork) =
|
|
|
|
n.portalProtocol.stop()
|
2022-07-11 14:29:16 +00:00
|
|
|
|
|
|
|
if not n.processContentLoop.isNil:
|
2023-10-17 12:19:50 +00:00
|
|
|
n.processContentLoop.cancelSoon()
|