2024-10-02 04:23:13 +00:00
|
|
|
# Nimbus - Portal Network
|
|
|
|
# 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).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
chronos,
|
|
|
|
testutils/unittests,
|
|
|
|
json_rpc/rpcserver,
|
|
|
|
json_rpc/clients/httpclient,
|
|
|
|
stint,
|
|
|
|
eth/p2p/discoveryv5/enr,
|
2024-10-04 21:21:26 +00:00
|
|
|
eth/common/keys,
|
|
|
|
eth/common/[headers_rlp, blocks_rlp, receipts_rlp],
|
2024-10-02 04:23:13 +00:00
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol,
|
|
|
|
../../network/wire/[portal_protocol, portal_stream, portal_protocol_config],
|
|
|
|
../../network/history/
|
2024-11-06 07:49:55 +00:00
|
|
|
[history_network, history_content, history_type_conversions, history_validation],
|
2024-10-02 04:23:13 +00:00
|
|
|
../../database/content_db,
|
2024-10-07 14:33:02 +00:00
|
|
|
../../rpc/[portal_rpc_client, rpc_portal_history_api],
|
2024-10-02 04:23:13 +00:00
|
|
|
../test_helpers
|
|
|
|
|
2024-10-04 21:21:26 +00:00
|
|
|
from eth/common/eth_types_rlp import rlpHash
|
|
|
|
|
2024-10-02 04:23:13 +00:00
|
|
|
type HistoryNode = ref object
|
|
|
|
discoveryProtocol*: discv5_protocol.Protocol
|
|
|
|
historyNetwork*: HistoryNetwork
|
|
|
|
|
|
|
|
proc newHistoryNode(rng: ref HmacDrbgContext, port: int): HistoryNode =
|
|
|
|
let
|
|
|
|
node = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(port))
|
|
|
|
db = ContentDB.new(
|
|
|
|
"", uint32.high, RadiusConfig(kind: Dynamic), node.localNode.id, inMemory = true
|
|
|
|
)
|
|
|
|
streamManager = StreamManager.new(node)
|
|
|
|
historyNetwork = HistoryNetwork.new(
|
|
|
|
PortalNetwork.none, node, db, streamManager, FinishedHistoricalHashesAccumulator()
|
|
|
|
)
|
|
|
|
|
|
|
|
return HistoryNode(discoveryProtocol: node, historyNetwork: historyNetwork)
|
|
|
|
|
|
|
|
proc portalProtocol(hn: HistoryNode): PortalProtocol =
|
|
|
|
hn.historyNetwork.portalProtocol
|
|
|
|
|
|
|
|
proc localNode(hn: HistoryNode): Node =
|
|
|
|
hn.discoveryProtocol.localNode
|
|
|
|
|
|
|
|
proc start(hn: HistoryNode) =
|
|
|
|
hn.historyNetwork.start()
|
|
|
|
|
|
|
|
proc stop(hn: HistoryNode) {.async.} =
|
|
|
|
discard hn.historyNetwork.stop()
|
|
|
|
await hn.discoveryProtocol.closeWait()
|
|
|
|
|
|
|
|
proc containsId(hn: HistoryNode, contentId: ContentId): bool =
|
2024-11-04 14:02:51 +00:00
|
|
|
return hn.historyNetwork.contentDB.contains(contentId)
|
2024-10-02 04:23:13 +00:00
|
|
|
|
2024-10-07 20:39:07 +00:00
|
|
|
proc store*(hn: HistoryNode, blockHash: Hash32, blockHeader: Header) =
|
2024-10-02 04:23:13 +00:00
|
|
|
let
|
|
|
|
headerRlp = rlp.encode(blockHeader)
|
|
|
|
blockHeaderWithProof = BlockHeaderWithProof(
|
|
|
|
header: ByteList[2048].init(headerRlp), proof: BlockHeaderProof.init()
|
|
|
|
)
|
|
|
|
contentKeyBytes = blockHeaderContentKey(blockHash).encode()
|
|
|
|
contentId = history_content.toContentId(contentKeyBytes)
|
|
|
|
|
|
|
|
hn.portalProtocol().storeContent(
|
|
|
|
contentKeyBytes, contentId, SSZ.encode(blockHeaderWithProof)
|
|
|
|
)
|
|
|
|
|
2024-10-07 20:39:07 +00:00
|
|
|
proc store*(hn: HistoryNode, blockHash: Hash32, blockBody: BlockBody) =
|
2024-10-02 04:23:13 +00:00
|
|
|
let
|
|
|
|
contentKeyBytes = blockBodyContentKey(blockHash).encode()
|
|
|
|
contentId = history_content.toContentId(contentKeyBytes)
|
|
|
|
|
|
|
|
hn.portalProtocol().storeContent(contentKeyBytes, contentId, blockBody.encode())
|
|
|
|
|
2024-10-07 20:39:07 +00:00
|
|
|
proc store*(hn: HistoryNode, blockHash: Hash32, receipts: seq[Receipt]) =
|
2024-10-02 04:23:13 +00:00
|
|
|
let
|
|
|
|
contentKeyBytes = receiptsContentKey(blockHash).encode()
|
|
|
|
contentId = history_content.toContentId(contentKeyBytes)
|
|
|
|
|
|
|
|
hn.portalProtocol().storeContent(contentKeyBytes, contentId, receipts.encode())
|
|
|
|
|
|
|
|
type TestCase = ref object
|
2024-10-14 08:46:35 +00:00
|
|
|
historyNode1: HistoryNode
|
|
|
|
historyNode2: HistoryNode
|
2024-10-02 04:23:13 +00:00
|
|
|
server: RpcHttpServer
|
|
|
|
client: PortalRpcClient
|
|
|
|
|
|
|
|
proc setupTest(rng: ref HmacDrbgContext): Future[TestCase] {.async.} =
|
|
|
|
let
|
|
|
|
localSrvAddress = "127.0.0.1"
|
|
|
|
localSrvPort = 0 # let the OS choose a port
|
|
|
|
ta = initTAddress(localSrvAddress, localSrvPort)
|
|
|
|
client = newRpcHttpClient()
|
2024-10-14 08:46:35 +00:00
|
|
|
historyNode1 = newHistoryNode(rng, 20333)
|
|
|
|
historyNode2 = newHistoryNode(rng, 20334)
|
|
|
|
|
|
|
|
historyNode1.start()
|
|
|
|
historyNode2.start()
|
|
|
|
|
|
|
|
check:
|
|
|
|
historyNode1.portalProtocol().addNode(historyNode2.localNode()) == Added
|
|
|
|
historyNode2.portalProtocol().addNode(historyNode1.localNode()) == Added
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
let rpcHttpServer = RpcHttpServer.new()
|
2024-10-23 08:26:56 +00:00
|
|
|
rpcHttpServer.addHttpServer(ta, maxRequestBodySize = 16 * 1024 * 1024)
|
2024-10-07 14:33:02 +00:00
|
|
|
rpcHttpServer.installPortalHistoryApiHandlers(
|
2024-10-14 08:46:35 +00:00
|
|
|
historyNode1.historyNetwork.portalProtocol
|
2024-10-02 04:23:13 +00:00
|
|
|
)
|
|
|
|
rpcHttpServer.start()
|
|
|
|
|
|
|
|
await client.connect(localSrvAddress, rpcHttpServer.localAddress[0].port, false)
|
|
|
|
|
|
|
|
return TestCase(
|
2024-10-14 08:46:35 +00:00
|
|
|
historyNode1: historyNode1,
|
|
|
|
historyNode2: historyNode2,
|
2024-10-02 04:23:13 +00:00
|
|
|
server: rpcHttpServer,
|
|
|
|
client: PortalRpcClient.init(client),
|
|
|
|
)
|
|
|
|
|
|
|
|
proc stop(testCase: TestCase) {.async.} =
|
|
|
|
await testCase.server.stop()
|
|
|
|
await testCase.server.closeWait()
|
2024-10-14 08:46:35 +00:00
|
|
|
await testCase.historyNode1.stop()
|
|
|
|
await testCase.historyNode2.stop()
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
procSuite "Portal RPC Client":
|
|
|
|
let rng = newRng()
|
|
|
|
|
|
|
|
asyncTest "Test historyGetBlockHeader with validation":
|
|
|
|
let
|
|
|
|
tc = await setupTest(rng)
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHeader = Header(number: 100)
|
|
|
|
blockHash = blockHeader.rlpHash()
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
# Test content not found
|
|
|
|
block:
|
|
|
|
let blockHeaderRes =
|
|
|
|
await tc.client.historyGetBlockHeader(blockHash, validateContent = true)
|
|
|
|
check:
|
|
|
|
blockHeaderRes.isErr()
|
|
|
|
blockHeaderRes.error() == ContentNotFound
|
|
|
|
|
|
|
|
# Test content found
|
|
|
|
block:
|
2024-10-14 08:46:35 +00:00
|
|
|
tc.historyNode2.store(blockHash, blockHeader)
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
let blockHeaderRes =
|
|
|
|
await tc.client.historyGetBlockHeader(blockHash, validateContent = true)
|
|
|
|
check:
|
|
|
|
blockHeaderRes.isOk()
|
|
|
|
blockHeaderRes.value() == blockHeader
|
|
|
|
|
|
|
|
# Test content validation failed
|
|
|
|
block:
|
2024-10-14 08:46:35 +00:00
|
|
|
tc.historyNode1.store(blockHash, Header()) # bad header
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
let blockHeaderRes =
|
|
|
|
await tc.client.historyGetBlockHeader(blockHash, validateContent = true)
|
|
|
|
check:
|
|
|
|
blockHeaderRes.isErr()
|
|
|
|
blockHeaderRes.error() == ContentValidationFailed
|
|
|
|
|
|
|
|
waitFor tc.stop()
|
|
|
|
|
|
|
|
asyncTest "Test historyGetBlockHeader without validation":
|
|
|
|
let
|
|
|
|
tc = await setupTest(rng)
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHeader = Header(number: 200)
|
|
|
|
blockHash = blockHeader.rlpHash()
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
# Test content not found
|
|
|
|
block:
|
|
|
|
let blockHeaderRes =
|
|
|
|
await tc.client.historyGetBlockHeader(blockHash, validateContent = false)
|
|
|
|
check:
|
|
|
|
blockHeaderRes.isErr()
|
|
|
|
blockHeaderRes.error() == ContentNotFound
|
|
|
|
|
|
|
|
# Test content found
|
|
|
|
block:
|
2024-10-14 08:46:35 +00:00
|
|
|
tc.historyNode1.store(blockHash, blockHeader)
|
|
|
|
|
2024-10-02 04:23:13 +00:00
|
|
|
let blockHeaderRes =
|
|
|
|
await tc.client.historyGetBlockHeader(blockHash, validateContent = false)
|
|
|
|
check:
|
|
|
|
blockHeaderRes.isOk()
|
|
|
|
blockHeaderRes.value() == blockHeader
|
|
|
|
|
|
|
|
waitFor tc.stop()
|
|
|
|
|
|
|
|
asyncTest "Test historyGetBlockBody with validation":
|
|
|
|
let
|
|
|
|
tc = await setupTest(rng)
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHeader = Header(number: 300)
|
2024-10-02 04:23:13 +00:00
|
|
|
blockBody = BlockBody()
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHash = blockHeader.rlpHash()
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
# Test content not found
|
|
|
|
block:
|
|
|
|
let blockBodyRes =
|
|
|
|
await tc.client.historyGetBlockBody(blockHash, validateContent = true)
|
|
|
|
check:
|
|
|
|
blockBodyRes.isErr()
|
|
|
|
blockBodyRes.error() == ContentNotFound
|
|
|
|
|
|
|
|
# Test content validation failed
|
|
|
|
block:
|
2024-10-14 08:46:35 +00:00
|
|
|
tc.historyNode1.store(blockHash, blockHeader)
|
|
|
|
tc.historyNode1.store(blockHash, blockBody)
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
let blockBodyRes =
|
|
|
|
await tc.client.historyGetBlockBody(blockHash, validateContent = true)
|
|
|
|
check:
|
|
|
|
blockBodyRes.isErr()
|
|
|
|
blockBodyRes.error() == ContentValidationFailed
|
|
|
|
|
|
|
|
waitFor tc.stop()
|
|
|
|
|
|
|
|
asyncTest "Test historyGetBlockBody without validation":
|
|
|
|
let
|
|
|
|
tc = await setupTest(rng)
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHeader = Header(number: 300)
|
2024-10-02 04:23:13 +00:00
|
|
|
blockBody = BlockBody()
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHash = blockHeader.rlpHash()
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
# Test content not found
|
|
|
|
block:
|
|
|
|
let blockBodyRes =
|
|
|
|
await tc.client.historyGetBlockBody(blockHash, validateContent = false)
|
|
|
|
check:
|
|
|
|
blockBodyRes.isErr()
|
|
|
|
blockBodyRes.error() == ContentNotFound
|
|
|
|
|
|
|
|
# Test content found
|
|
|
|
block:
|
2024-10-14 08:46:35 +00:00
|
|
|
tc.historyNode1.store(blockHash, blockHeader)
|
|
|
|
tc.historyNode1.store(blockHash, blockBody)
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
let blockBodyRes =
|
|
|
|
await tc.client.historyGetBlockBody(blockHash, validateContent = false)
|
|
|
|
check:
|
|
|
|
blockBodyRes.isOk()
|
|
|
|
blockBodyRes.value() == blockBody
|
|
|
|
|
|
|
|
waitFor tc.stop()
|
|
|
|
|
|
|
|
asyncTest "Test historyGetReceipts with validation":
|
|
|
|
let
|
|
|
|
tc = await setupTest(rng)
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHeader = Header(number: 300)
|
2024-10-02 04:23:13 +00:00
|
|
|
receipts = @[Receipt()]
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHash = blockHeader.rlpHash()
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
# Test content not found
|
|
|
|
block:
|
|
|
|
let receiptsRes =
|
|
|
|
await tc.client.historyGetReceipts(blockHash, validateContent = true)
|
|
|
|
check:
|
|
|
|
receiptsRes.isErr()
|
|
|
|
receiptsRes.error() == ContentNotFound
|
|
|
|
|
|
|
|
# Test content validation failed
|
|
|
|
block:
|
2024-10-14 08:46:35 +00:00
|
|
|
tc.historyNode1.store(blockHash, blockHeader)
|
|
|
|
tc.historyNode1.store(blockHash, receipts)
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
let receiptsRes =
|
|
|
|
await tc.client.historyGetReceipts(blockHash, validateContent = true)
|
|
|
|
check:
|
|
|
|
receiptsRes.isErr()
|
|
|
|
receiptsRes.error() == ContentValidationFailed
|
|
|
|
|
|
|
|
waitFor tc.stop()
|
|
|
|
|
|
|
|
asyncTest "Test historyGetReceipts without validation":
|
|
|
|
let
|
|
|
|
tc = await setupTest(rng)
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHeader = Header(number: 300)
|
2024-10-02 04:23:13 +00:00
|
|
|
receipts = @[Receipt()]
|
2024-10-04 21:21:26 +00:00
|
|
|
blockHash = blockHeader.rlpHash()
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
# Test content not found
|
|
|
|
block:
|
|
|
|
let receiptsRes =
|
|
|
|
await tc.client.historyGetReceipts(blockHash, validateContent = false)
|
|
|
|
check:
|
|
|
|
receiptsRes.isErr()
|
|
|
|
receiptsRes.error() == ContentNotFound
|
|
|
|
|
|
|
|
# Test content found
|
|
|
|
block:
|
2024-10-14 08:46:35 +00:00
|
|
|
tc.historyNode1.store(blockHash, blockHeader)
|
|
|
|
tc.historyNode1.store(blockHash, receipts)
|
2024-10-02 04:23:13 +00:00
|
|
|
|
|
|
|
let receiptsRes =
|
|
|
|
await tc.client.historyGetReceipts(blockHash, validateContent = false)
|
|
|
|
check:
|
|
|
|
receiptsRes.isOk()
|
|
|
|
receiptsRes.value() == receipts
|
|
|
|
|
|
|
|
waitFor tc.stop()
|