Skip to content

Commit 3ba96ba

Browse files
committed
a basic telemetry rpc
1 parent 52fde8a commit 3ba96ba

File tree

9 files changed

+83
-22
lines changed

9 files changed

+83
-22
lines changed

Node/Sources/Node/Node.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ public class Node {
5353
blockchain: blockchain
5454
)
5555

56+
let nodeDataSource = NodeDataSource(blockchain: blockchain, chainDataProvider: dataProvider, networkManager: network)
57+
5658
rpcServer = try config.rpc.map {
57-
try Server(config: $0, source: blockchain)
59+
try Server(config: $0, source: nodeDataSource)
5860
}
5961
}
6062

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Blockchain
2+
import RPC
3+
import Utils
4+
5+
public final class NodeDataSource: DataSource {
6+
public let blockchain: Blockchain
7+
public let chainDataProvider: BlockchainDataProvider
8+
public let networkManager: NetworkManager
9+
10+
public init(blockchain: Blockchain, chainDataProvider: BlockchainDataProvider, networkManager: NetworkManager) {
11+
self.blockchain = blockchain
12+
self.chainDataProvider = chainDataProvider
13+
self.networkManager = networkManager
14+
}
15+
16+
public func importBlock(_ block: BlockRef) async throws {
17+
try await blockchain.importBlock(block)
18+
}
19+
20+
public func getBestBlock() async throws -> BlockRef {
21+
try await chainDataProvider.getBlock(hash: chainDataProvider.bestHead)
22+
}
23+
24+
public func getBlock(hash: Data32) async throws -> BlockRef? {
25+
try await chainDataProvider.getBlock(hash: hash)
26+
}
27+
28+
public func getState(hash: Data32) async throws -> StateRef? {
29+
try await chainDataProvider.getState(hash: hash)
30+
}
31+
32+
public func getPeersCount() async throws -> Int {
33+
// TODO: implememnt
34+
0
35+
}
36+
}

Node/Sources/Node/ValidatorNode.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public class ValidatorNode: Node {
2020
dataProvider: dataProvider
2121
)
2222

23-
let genesisState = try await blockchain.getState(hash: blockchain.dataProvider.genesisBlockHash)
23+
let genesisState = try await dataProvider.getState(hash: blockchain.dataProvider.genesisBlockHash)
2424

25-
await validator.on(genesis: genesisState!)
25+
await validator.on(genesis: genesisState)
2626
}
2727
}

RPC/Sources/RPC/DataSource/Blockchain+DataSource.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.

RPC/Sources/RPC/DataSource/DataSource.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Utils
44
public protocol DataSource: Sendable {
55
func getBestBlock() async throws -> BlockRef
66
func getBlock(hash: Data32) async throws -> BlockRef?
7-
87
func importBlock(_: BlockRef) async throws
8+
func getState(hash: Data32) async throws -> StateRef?
9+
func getPeersCount() async throws -> Int
910
}

RPC/Sources/RPC/Handlers/ChainHandler.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ struct ChainHandler {
2020
throw JSONError(code: -32602, message: "Invalid block hash")
2121
}
2222
let block = try await source.getBlock(hash: data32)
23-
return block.map { ["hash": $0.hash.description, "parentHash": $0.header.parentHash.description] }
23+
return block.map { [
24+
"hash": $0.hash.description,
25+
"parentHash": $0.header.parentHash.description,
26+
] }
2427
} else {
2528
let block = try await source.getBestBlock()
26-
return ["hash": block.hash.description, "parentHash": block.header.parentHash.description]
29+
return [
30+
"hash": block.hash.description,
31+
"parentHash": block.header.parentHash.description,
32+
]
2733
}
2834
}
2935
}

RPC/Sources/RPC/Handlers/SystemHandler.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ struct SystemHandler {
44

55
return [
66
"system_health": handler.health,
7+
"system_name": handler.name,
78
]
89
}
910

1011
func health(request _: JSONRequest) async throws -> any Encodable {
1112
true
1213
}
14+
15+
func name(request _: JSONRequest) async throws -> any Encodable {
16+
"Boka"
17+
}
1318
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Blockchain
2+
import Foundation
3+
import Utils
4+
5+
struct TelemetryHandler {
6+
let source: DataSource
7+
8+
static func getHandlers(source: DataSource) -> [String: JSONRPCHandler] {
9+
let handler = TelemetryHandler(source: source)
10+
11+
return [
12+
"telemetry_getUpdate": handler.getUpdate,
13+
]
14+
}
15+
16+
func getUpdate(request _: JSONRequest) async throws -> any Encodable {
17+
let block = try await source.getBestBlock()
18+
let peerCount = try await source.getPeersCount()
19+
return [
20+
"name": "Boka",
21+
"chainHead": block.header.timeslot.description,
22+
"blockHash": block.hash.description,
23+
"peerCount": peerCount.description,
24+
]
25+
}
26+
}

RPC/Sources/RPC/Server.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class Server {
2929

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

3334
// Register routes
3435
let rpcController = JSONRPCController(handlers: handlers)

0 commit comments

Comments
 (0)