From fcde29c68fdc837f05826476786aafaf6e4e199c Mon Sep 17 00:00:00 2001 From: MacOMNI <414294494@qq.com> Date: Wed, 22 Jan 2025 15:21:14 +0800 Subject: [PATCH] update chainhandler test --- .../BlockchainDataProvider.swift | 4 +++ Node/Sources/Node/NodeDataSource.swift | 4 +++ RPC/Sources/RPC/DataSource/DataSource.swift | 1 + RPC/Sources/RPC/Handlers/ChainHandlers.swift | 3 +- .../RPC/JSONRPC/JSONRPCController.swift | 2 +- RPC/Tests/RPCTests/ChainHandlesTests.swift | 24 ++++++++++----- ...rTests.swift => SystemHandlersTests.swift} | 29 ++++++++++++------- 7 files changed, 47 insertions(+), 20 deletions(-) rename RPC/Tests/RPCTests/{JSONRPCControllerTests.swift => SystemHandlersTests.swift} (87%) diff --git a/Blockchain/Sources/Blockchain/BlockchainDataProvider/BlockchainDataProvider.swift b/Blockchain/Sources/Blockchain/BlockchainDataProvider/BlockchainDataProvider.swift index 3da0693b..f9fabba2 100644 --- a/Blockchain/Sources/Blockchain/BlockchainDataProvider/BlockchainDataProvider.swift +++ b/Blockchain/Sources/Blockchain/BlockchainDataProvider/BlockchainDataProvider.swift @@ -98,6 +98,10 @@ extension BlockchainDataProvider { try await dataProvider.getBlockHash(byNumber: number) } + public func getFinalizedHead() async throws -> Data32? { + try await dataProvider.getFinalizedHead() + } + // add forks of finalized head is not allowed public func add(block: BlockRef) async throws { logger.debug("adding block: \(block.hash)") diff --git a/Node/Sources/Node/NodeDataSource.swift b/Node/Sources/Node/NodeDataSource.swift index 030e6938..be571202 100644 --- a/Node/Sources/Node/NodeDataSource.swift +++ b/Node/Sources/Node/NodeDataSource.swift @@ -50,6 +50,10 @@ extension NodeDataSource: ChainDataSource { public func getHeader(hash: Data32) async throws -> HeaderRef? { try await chainDataProvider.getHeader(hash: hash) } + + public func getFinalizedHead() async throws -> Data32? { + try await chainDataProvider.getFinalizedHead() + } } extension NodeDataSource: TelemetryDataSource { diff --git a/RPC/Sources/RPC/DataSource/DataSource.swift b/RPC/Sources/RPC/DataSource/DataSource.swift index 679d1a5b..e45af750 100644 --- a/RPC/Sources/RPC/DataSource/DataSource.swift +++ b/RPC/Sources/RPC/DataSource/DataSource.swift @@ -11,6 +11,7 @@ public protocol ChainDataSource: Sendable { func getBlockHash(byTimeslot timeslot: TimeslotIndex) async throws -> Set func getBestBlockHash() async throws -> Set func getHeader(hash: Data32) async throws -> HeaderRef? + func getFinalizedHead() async throws -> Data32? } public protocol TelemetryDataSource: Sendable { diff --git a/RPC/Sources/RPC/Handlers/ChainHandlers.swift b/RPC/Sources/RPC/Handlers/ChainHandlers.swift index 1899f41b..b5af6ced 100644 --- a/RPC/Sources/RPC/Handlers/ChainHandlers.swift +++ b/RPC/Sources/RPC/Handlers/ChainHandlers.swift @@ -92,8 +92,7 @@ public enum ChainHandlers { } public func handle(request _: Request) async throws -> Response? { - // TODO: implement - nil + try await source.getFinalizedHead() } } diff --git a/RPC/Sources/RPC/JSONRPC/JSONRPCController.swift b/RPC/Sources/RPC/JSONRPC/JSONRPCController.swift index 5031a1ac..05cecfc3 100644 --- a/RPC/Sources/RPC/JSONRPC/JSONRPCController.swift +++ b/RPC/Sources/RPC/JSONRPC/JSONRPCController.swift @@ -63,7 +63,7 @@ final class JSONRPCController: RouteCollection, Sendable { let responseData = try encoder.encode(jsonResponse) try await ws.send(raw: responseData, opcode: .text) } catch { - logger.info("Failed to decode JSON request: \(error)") + logger.error("Failed to decode JSON request: \(error)") let rpcError = JSONError(code: -32600, message: "Invalid Request") let rpcResponse = JSONResponse(id: nil, error: rpcError) diff --git a/RPC/Tests/RPCTests/ChainHandlesTests.swift b/RPC/Tests/RPCTests/ChainHandlesTests.swift index dda5b1ce..39909a0d 100644 --- a/RPC/Tests/RPCTests/ChainHandlesTests.swift +++ b/RPC/Tests/RPCTests/ChainHandlesTests.swift @@ -21,6 +21,10 @@ extension DummyNodeDataSource: ChainDataSource { return try await chainDataProvider.getBlockHash(byTimeslot: timeslot) } + public func getFinalizedHead() async throws -> Data32? { + try await chainDataProvider.getFinalizedHead() + } + public func getBestBlock() async throws -> BlockRef { try await chainDataProvider.getBlock(hash: chainDataProvider.bestHead.hash) } @@ -65,32 +69,36 @@ final class ChainRPCControllerTests { try buffer.writeJSONEncodable(req) try await app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res async in #expect(res.status == .ok) + let resp = try! res.content.decode(JSONResponse.self, using: JSONDecoder()) + #expect(resp.result!.value != nil) } try await app.asyncShutdown() } @Test func getBlockHash() async throws { try await setUp() - let hashHex = await dataProvider.bestHead.hash.toHexString() - let params = JSON.array([.string(hashHex)]) - let req = JSONRequest(jsonrpc: "2.0", method: "chain_getBlockHash", params: params, id: 1) + let timeslot = await dataProvider.bestHead.timeslot + let params = JSON.array([JSON(integerLiteral: Int32(timeslot))]) + let req = JSONRequest(jsonrpc: "2.0", method: "chain_getBlockHash", params: params, id: 2) var buffer = ByteBuffer() try buffer.writeJSONEncodable(req) try await app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res async in #expect(res.status == .ok) + let resp = try! res.content.decode(JSONResponse.self, using: JSONDecoder()) + #expect(resp.result!.value != nil) } try await app.asyncShutdown() } @Test func getFinalizedHead() async throws { try await setUp() - let hashHex = await dataProvider.bestHead.hash.toHexString() - let params = JSON.array([.string(hashHex)]) - let req = JSONRequest(jsonrpc: "2.0", method: "chain_getFinalziedHead", params: params, id: 1) + let req = JSONRequest(jsonrpc: "2.0", method: "chain_getFinalizedHead", params: nil, id: 3) var buffer = ByteBuffer() try buffer.writeJSONEncodable(req) try await app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res async in #expect(res.status == .ok) + let resp = try! res.content.decode(JSONResponse.self, using: JSONDecoder()) + #expect(resp.result!.value != nil) } try await app.asyncShutdown() } @@ -99,11 +107,13 @@ final class ChainRPCControllerTests { try await setUp() let hashHex = await dataProvider.bestHead.hash.toHexString() let params = JSON.array([.string(hashHex)]) - let req = JSONRequest(jsonrpc: "2.0", method: "chain_getHeader", params: params, id: 1) + let req = JSONRequest(jsonrpc: "2.0", method: "chain_getHeader", params: params, id: 4) var buffer = ByteBuffer() try buffer.writeJSONEncodable(req) try await app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res async in #expect(res.status == .ok) + let resp = try! res.content.decode(JSONResponse.self, using: JSONDecoder()) + #expect(resp.result!.value != nil) } try await app.asyncShutdown() } diff --git a/RPC/Tests/RPCTests/JSONRPCControllerTests.swift b/RPC/Tests/RPCTests/SystemHandlersTests.swift similarity index 87% rename from RPC/Tests/RPCTests/JSONRPCControllerTests.swift rename to RPC/Tests/RPCTests/SystemHandlersTests.swift index ca2ed82d..b993bec1 100644 --- a/RPC/Tests/RPCTests/JSONRPCControllerTests.swift +++ b/RPC/Tests/RPCTests/SystemHandlersTests.swift @@ -1,20 +1,23 @@ import Blockchain -@testable import RPC import Testing import TracingUtils -@testable import Utils import Vapor import XCTVapor +@testable import RPC +@testable import Utils + struct DummySource: SystemDataSource {} -final class JSONRPCControllerTests { +final class SystemHandlersTests { var app: Application init() throws { app = Application(.testing) - let rpcController = JSONRPCController(handlers: SystemHandlers.getHandlers(source: DummySource())) + let rpcController = JSONRPCController( + handlers: SystemHandlers.getHandlers(source: DummySource()) + ) try app.register(collection: rpcController) } @@ -26,7 +29,8 @@ final class JSONRPCControllerTests { let req = JSONRequest(jsonrpc: "2.0", method: "system_health", params: nil, id: 1) var buffer = ByteBuffer() try buffer.writeJSONEncodable(req) - try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res in + try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { + res in #expect(res.status == .ok) let resp = try res.content.decode(JSONResponse.self, using: JSONDecoder()) #expect((resp.result!.value as! Utils.JSON).bool == true) @@ -37,7 +41,8 @@ final class JSONRPCControllerTests { let req = JSONRequest(jsonrpc: "2.0", method: "system_implementation", params: nil, id: 2) var buffer = ByteBuffer() try buffer.writeJSONEncodable(req) - try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res in + try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { + res in #expect(res.status == .ok) let resp = try res.content.decode(JSONResponse.self, using: JSONDecoder()) #expect((resp.result!.value as! Utils.JSON).string == "Boka") @@ -48,7 +53,8 @@ final class JSONRPCControllerTests { let req = JSONRequest(jsonrpc: "2.0", method: "system_version", params: nil, id: 3) var buffer = ByteBuffer() try buffer.writeJSONEncodable(req) - try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res in + try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { + res in #expect(res.status == .ok) let resp = try res.content.decode(JSONResponse.self, using: JSONDecoder()) #expect((resp.result!.value as! Utils.JSON).string == "0.0.1") @@ -59,7 +65,8 @@ final class JSONRPCControllerTests { let req = JSONRequest(jsonrpc: "2.0", method: "system_properties", params: nil, id: 4) var buffer = ByteBuffer() try buffer.writeJSONEncodable(req) - try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res in + try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { + res in #expect(res.status == .ok) let resp = try res.content.decode(JSONResponse.self, using: JSONDecoder()) #expect(resp.result?.value != nil) @@ -70,7 +77,8 @@ final class JSONRPCControllerTests { let req = JSONRequest(jsonrpc: "2.0", method: "system_nodeRoles", params: nil, id: 5) var buffer = ByteBuffer() try buffer.writeJSONEncodable(req) - try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res in + try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { + res in #expect(res.status == .ok) let resp = try res.content.decode(JSONResponse.self, using: JSONDecoder()) #expect((resp.result!.value as! Utils.JSON).array == []) @@ -81,7 +89,8 @@ final class JSONRPCControllerTests { let req = JSONRequest(jsonrpc: "2.0", method: "system_chain", params: nil, id: 6) var buffer = ByteBuffer() try buffer.writeJSONEncodable(req) - try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res in + try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { + res in #expect(res.status == .ok) let resp = try res.content.decode(JSONResponse.self, using: JSONDecoder()) #expect((resp.result!.value as! Utils.JSON).string == "dev")