Skip to content

Commit

Permalink
a basic telemetry rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
qiweiii committed Oct 22, 2024
1 parent 52fde8a commit 3ba96ba
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 22 deletions.
4 changes: 3 additions & 1 deletion Node/Sources/Node/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public class Node {
blockchain: blockchain
)

let nodeDataSource = NodeDataSource(blockchain: blockchain, chainDataProvider: dataProvider, networkManager: network)

rpcServer = try config.rpc.map {
try Server(config: $0, source: blockchain)
try Server(config: $0, source: nodeDataSource)
}
}

Expand Down
36 changes: 36 additions & 0 deletions Node/Sources/Node/NodeDataSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Blockchain
import RPC
import Utils

public final class NodeDataSource: DataSource {
public let blockchain: Blockchain
public let chainDataProvider: BlockchainDataProvider
public let networkManager: NetworkManager

public init(blockchain: Blockchain, chainDataProvider: BlockchainDataProvider, networkManager: NetworkManager) {
self.blockchain = blockchain
self.chainDataProvider = chainDataProvider
self.networkManager = networkManager
}

public func importBlock(_ block: BlockRef) async throws {
try await blockchain.importBlock(block)
}

public func getBestBlock() async throws -> BlockRef {
try await chainDataProvider.getBlock(hash: chainDataProvider.bestHead)
}

public func getBlock(hash: Data32) async throws -> BlockRef? {
try await chainDataProvider.getBlock(hash: hash)
}

public func getState(hash: Data32) async throws -> StateRef? {
try await chainDataProvider.getState(hash: hash)
}

public func getPeersCount() async throws -> Int {
// TODO: implememnt
0
}
}
4 changes: 2 additions & 2 deletions Node/Sources/Node/ValidatorNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class ValidatorNode: Node {
dataProvider: dataProvider
)

let genesisState = try await blockchain.getState(hash: blockchain.dataProvider.genesisBlockHash)
let genesisState = try await dataProvider.getState(hash: blockchain.dataProvider.genesisBlockHash)

await validator.on(genesis: genesisState!)
await validator.on(genesis: genesisState)
}
}
16 changes: 0 additions & 16 deletions RPC/Sources/RPC/DataSource/Blockchain+DataSource.swift

This file was deleted.

3 changes: 2 additions & 1 deletion RPC/Sources/RPC/DataSource/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Utils
public protocol DataSource: Sendable {
func getBestBlock() async throws -> BlockRef
func getBlock(hash: Data32) async throws -> BlockRef?

func importBlock(_: BlockRef) async throws
func getState(hash: Data32) async throws -> StateRef?
func getPeersCount() async throws -> Int
}
10 changes: 8 additions & 2 deletions RPC/Sources/RPC/Handlers/ChainHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ struct ChainHandler {
throw JSONError(code: -32602, message: "Invalid block hash")
}
let block = try await source.getBlock(hash: data32)
return block.map { ["hash": $0.hash.description, "parentHash": $0.header.parentHash.description] }
return block.map { [
"hash": $0.hash.description,
"parentHash": $0.header.parentHash.description,
] }
} else {
let block = try await source.getBestBlock()
return ["hash": block.hash.description, "parentHash": block.header.parentHash.description]
return [
"hash": block.hash.description,
"parentHash": block.header.parentHash.description,
]
}
}
}
5 changes: 5 additions & 0 deletions RPC/Sources/RPC/Handlers/SystemHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ struct SystemHandler {

return [
"system_health": handler.health,
"system_name": handler.name,
]
}

func health(request _: JSONRequest) async throws -> any Encodable {
true
}

func name(request _: JSONRequest) async throws -> any Encodable {
"Boka"
}
}
26 changes: 26 additions & 0 deletions RPC/Sources/RPC/Handlers/TelemetryHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Blockchain
import Foundation
import Utils

struct TelemetryHandler {
let source: DataSource

static func getHandlers(source: DataSource) -> [String: JSONRPCHandler] {
let handler = TelemetryHandler(source: source)

return [
"telemetry_getUpdate": handler.getUpdate,
]
}

func getUpdate(request _: JSONRequest) async throws -> any Encodable {
let block = try await source.getBestBlock()
let peerCount = try await source.getPeersCount()
return [
"name": "Boka",
"chainHead": block.header.timeslot.description,
"blockHash": block.hash.description,
"peerCount": peerCount.description,
]
}
}
1 change: 1 addition & 0 deletions RPC/Sources/RPC/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Server {

var handlers: [String: JSONRPCHandler] = SystemHandler.getHandlers()
handlers.merge(ChainHandler.getHandlers(source: source)) { _, new in new }
handlers.merge(TelemetryHandler.getHandlers(source: source)) { _, new in new }

// Register routes
let rpcController = JSONRPCController(handlers: handlers)
Expand Down

0 comments on commit 3ba96ba

Please sign in to comment.