Skip to content

Commit

Permalink
update chainhandler test
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Jan 22, 2025
1 parent bab6513 commit fcde29c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
4 changes: 4 additions & 0 deletions Node/Sources/Node/NodeDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions RPC/Sources/RPC/DataSource/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public protocol ChainDataSource: Sendable {
func getBlockHash(byTimeslot timeslot: TimeslotIndex) async throws -> Set<Data32>
func getBestBlockHash() async throws -> Set<Data32>
func getHeader(hash: Data32) async throws -> HeaderRef?
func getFinalizedHead() async throws -> Data32?
}

public protocol TelemetryDataSource: Sendable {
Expand Down
3 changes: 1 addition & 2 deletions RPC/Sources/RPC/Handlers/ChainHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ public enum ChainHandlers {
}

public func handle(request _: Request) async throws -> Response? {
// TODO: implement
nil
try await source.getFinalizedHead()
}
}

Expand Down
2 changes: 1 addition & 1 deletion RPC/Sources/RPC/JSONRPC/JSONRPCController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 17 additions & 7 deletions RPC/Tests/RPCTests/ChainHandlesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
}
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}

Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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 == [])
Expand All @@ -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")
Expand Down

0 comments on commit fcde29c

Please sign in to comment.