Skip to content

Commit

Permalink
Merge branch 'master' into dev_issues
Browse files Browse the repository at this point in the history
* master:
  update
  Define RPC methods (#238)
  • Loading branch information
MacOMNI committed Dec 3, 2024
2 parents a7025df + 54282e5 commit c5b1b10
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ contents.xcworkspacedata
launch.json
settings.json
c_cpp_properties.json

Tools/openrpc.json
11 changes: 7 additions & 4 deletions RPC/Sources/RPC/Handlers/AllHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ public enum AllHandlers {
public static let handlers: [any RPCHandler.Type] =
ChainHandlers.handlers +
SystemHandlers.handlers +
StateHandlers.handlers +
TelemetryHandlers.handlers +
RPCHandlers.handlers

public static func getHandlers(source: ChainDataSource & SystemDataSource & TelemetryDataSource) -> [any RPCHandler] {
ChainHandlers.getHandlers(source: source) +
SystemHandlers.getHandlers(source: source) +
TelemetryHandlers.getHandlers(source: source) +
RPCHandlers.getHandlers(source: handlers)
var handlers = ChainHandlers.getHandlers(source: source)
handlers.append(contentsOf: SystemHandlers.getHandlers(source: source))
handlers.append(contentsOf: StateHandlers.getHandlers(source: source))
handlers.append(contentsOf: TelemetryHandlers.getHandlers(source: source))
handlers.append(contentsOf: RPCHandlers.getHandlers(source: Self.handlers))
return handlers
}
}
73 changes: 70 additions & 3 deletions RPC/Sources/RPC/Handlers/ChainHandlers.swift
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import Blockchain
import Codec
import Foundation
import Utils

public enum ChainHandlers {
public static let handlers: [any RPCHandler.Type] = [
GetBlock.self,
GetBlockHash.self,
GetFinalziedHead.self,
GetHeader.self,
]

public static func getHandlers(source: ChainDataSource) -> [any RPCHandler] {
[
GetBlock(source: source),
GetBlockHash(source: source),
GetFinalziedHead(source: source),
GetHeader(source: source),
]
}

public struct GetBlock: RPCHandler {
public typealias Request = Request1<Data32?>
public typealias Response = BlockRef?
public typealias DataSource = ChainDataSource
public typealias Response = Data?

public static var method: String { "chain_getBlock" }
public static var requestNames: [String] { ["hash"] }
public static var summary: String? { "Get block by hash. If hash is not provided, returns the best block." }

private let source: ChainDataSource
Expand All @@ -28,11 +35,71 @@ public enum ChainHandlers {
}

public func handle(request: Request) async throws -> Response? {
if let hash = request.value {
let block = if let hash = request.value {
try await source.getBlock(hash: hash)
} else {
try await source.getBestBlock()
}
return try block.map { try JamEncoder.encode($0) }
}
}

public struct GetBlockHash: RPCHandler {
public typealias Request = Request1<TimeslotIndex?>
public typealias Response = Data?

public static var method: String { "chain_getBlockHash" }
public static var requestNames: [String] { ["timeslot"] }
public static var summary: String? { "Get the block hash by timeslot. If timeslot is not provided, returns the best block hash." }

private let source: ChainDataSource

init(source: ChainDataSource) {
self.source = source
}

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

public struct GetFinalziedHead: RPCHandler {
public typealias Request = VoidRequest
public typealias Response = Data32?

public static var method: String { "chain_getFinalziedHead" }
public static var summary: String? { "Get hash of the last finalized block in the canon chain." }

private let source: ChainDataSource

init(source: ChainDataSource) {
self.source = source
}

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

public struct GetHeader: RPCHandler {
public typealias Request = Request1<Data32?>
public typealias Response = Data?

public static var method: String { "chain_getHeader" }
public static var requestNames: [String] { ["hash"] }
public static var summary: String? { "Get block header by hash. If hash is not provided, returns the best block header." }

private let source: ChainDataSource

init(source: ChainDataSource) {
self.source = source
}

public func handle(request _: Request) async throws -> Response? {
// TODO: implement
nil
}
}
}
57 changes: 57 additions & 0 deletions RPC/Sources/RPC/Handlers/StateHandlers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Blockchain
import Foundation
import Utils

public enum StateHandlers {
public static let handlers: [any RPCHandler.Type] = [
GetKeys.self,
GetStorage.self,
]

public static func getHandlers(source: ChainDataSource) -> [any RPCHandler] {
[
GetKeys(source: source),
GetStorage(source: source),
]
}

public struct GetKeys: RPCHandler {
public typealias Request = Request4<Data32, UInt32, Data32?, Data32?>
public typealias Response = [String]

public static var method: String { "state_getKeys" }
public static var requestNames: [String] { ["prefix", "count", "startKey", "blockHash"] }
public static var summary: String? { "Returns the keys of the state." }

private let source: ChainDataSource

init(source: ChainDataSource) {
self.source = source
}

public func handle(request _: Request) async throws -> Response? {
// TODO: implement
[]
}
}

public struct GetStorage: RPCHandler {
public typealias Request = Request2<Data32, Data32?>
public typealias Response = [String]

public static var method: String { "state_getStorage" }
public static var requestNames: [String] { ["key", "blockHash"] }
public static var summary: String? { "Returns the storage entry for a key for blockHash or best head." }

private let source: ChainDataSource

init(source: ChainDataSource) {
self.source = source
}

public func handle(request _: Request) async throws -> Response? {
// TODO: implement
[]
}
}
}
82 changes: 77 additions & 5 deletions RPC/Sources/RPC/Handlers/SystemHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ import Utils
public enum SystemHandlers {
public static let handlers: [any RPCHandler.Type] = [
Health.self,
Implementation.self,
Version.self,
Properties.self,
NodeRoles.self,
Chain.self,
]

public static func getHandlers(source _: SystemDataSource) -> [any RPCHandler] {
public static func getHandlers(source: SystemDataSource) -> [any RPCHandler] {
[
Health(),
Version(),
Implementation(),
Version(source: source),
Properties(source: source),
NodeRoles(source: source),
Chain(source: source),
]
}

public struct Health: RPCHandler {
struct Health: RPCHandler {
public typealias Request = VoidRequest
public typealias Response = Bool

Expand All @@ -25,7 +33,7 @@ public enum SystemHandlers {
}
}

public struct Implementation: RPCHandler {
struct Implementation: RPCHandler {
public typealias Request = VoidRequest
public typealias Response = String

Expand All @@ -37,15 +45,79 @@ public enum SystemHandlers {
}
}

public struct Version: RPCHandler {
struct Version: RPCHandler {
public typealias Request = VoidRequest
public typealias Response = String

public static var method: String { "system_version" }
public static var summary: String? { "Returns the version of the node." }

private let source: SystemDataSource

init(source: SystemDataSource) {
self.source = source
}

public func handle(request _: Request) async throws -> Response? {
// TODO: read it from somewhere
"0.0.1"
}
}

struct Properties: RPCHandler {
public typealias Request = VoidRequest
public typealias Response = JSON

public static var method: String { "system_properties" }
public static var summary: String? { "Get a custom set of properties as a JSON object, defined in the chain spec." }

private let source: SystemDataSource

init(source: SystemDataSource) {
self.source = source
}

public func handle(request _: Request) async throws -> Response? {
// TODO: implement
JSON.array([])
}
}

struct NodeRoles: RPCHandler {
public typealias Request = VoidRequest
public typealias Response = [String]

public static var method: String { "system_nodeRoles" }
public static var summary: String? { "Returns the roles the node is running as." }

private let source: SystemDataSource

init(source: SystemDataSource) {
self.source = source
}

public func handle(request _: Request) async throws -> Response? {
// TODO: implement
[]
}
}

struct Chain: RPCHandler {
public typealias Request = VoidRequest
public typealias Response = String

public static var method: String { "system_chain" }
public static var summary: String? { "Returns the chain name, defined in the chain spec." }

private let source: SystemDataSource

init(source: SystemDataSource) {
self.source = source
}

public func handle(request _: Request) async throws -> Response? {
// TODO: implement
"dev"
}
}
}
Loading

0 comments on commit c5b1b10

Please sign in to comment.