Skip to content

Commit

Permalink
connect rpc to blockchain (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc authored Aug 9, 2024
1 parent 0010257 commit d330873
Show file tree
Hide file tree
Showing 29 changed files with 716 additions and 267 deletions.
13 changes: 12 additions & 1 deletion Blockchain/Sources/Blockchain/Blockchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Utils
/// Holds the state of the blockchain.
/// Includes the canonical chain as well as pending forks.
/// Assume all blocks and states are valid and have been validated.
public class Blockchain {
public final class Blockchain: Sendable {
public let config: ProtocolConfigRef

private let dataProvider: BlockchainDataProvider
Expand All @@ -30,4 +30,15 @@ public class Blockchain {
// TODO: purge forks
try await dataProvider.setFinalizedHead(hash: hash)
}

public func getBestBlock() async throws -> BlockRef {
guard let hash = try await dataProvider.getHeads().first else {
try throwUnreachable("no head")
}
return try await dataProvider.getBlock(hash: hash)
}

public func getBlock(hash: Data32) async throws -> BlockRef? {
try await dataProvider.getBlock(hash: hash)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public enum BlockchainDataProviderError: Error {
case unknownHash
}

public protocol BlockchainDataProvider {
public protocol BlockchainDataProvider: Sendable {
func hasHeader(hash: Data32) async throws -> Bool
func isHead(hash: Data32) async throws -> Bool

Expand Down
83 changes: 82 additions & 1 deletion Boka/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
{
"originHash" : "b09a7b54cb34c5b40b9c30546e215da07c64f3935c5d0c0b0daca60d07584231",
"originHash" : "8311dd4d27e8fd2abbee30df498eef1b06b76ecc7d9e7ff836f014d67d6698c7",
"pins" : [
{
"identity" : "async-http-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/async-http-client.git",
"state" : {
"revision" : "0ae99db85b2b9d1e79b362bd31fd1ffe492f7c47",
"version" : "1.21.2"
}
},
{
"identity" : "async-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/async-kit.git",
"state" : {
"revision" : "e048c8ee94967e8d8a1c2ec0e1156d6f7fa34d31",
"version" : "1.20.0"
}
},
{
"identity" : "blake2.swift",
"kind" : "remoteSourceControl",
Expand All @@ -10,6 +28,15 @@
"version" : "0.2.0"
}
},
{
"identity" : "console-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/console-kit.git",
"state" : {
"revision" : "9f7932f22ab6f64aafadc14491e694179b7d0f6f",
"version" : "4.14.3"
}
},
{
"identity" : "grpc-swift",
"kind" : "remoteSourceControl",
Expand All @@ -19,6 +46,24 @@
"version" : "1.23.0"
}
},
{
"identity" : "multipart-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/multipart-kit.git",
"state" : {
"revision" : "a31236f24bfd2ea2f520a74575881f6731d7ae68",
"version" : "4.7.0"
}
},
{
"identity" : "routing-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/routing-kit.git",
"state" : {
"revision" : "8c9a227476555c55837e569be71944e02a056b72",
"version" : "4.9.1"
}
},
{
"identity" : "scalecodec.swift",
"kind" : "remoteSourceControl",
Expand All @@ -28,6 +73,15 @@
"revision" : "dac3e7161de34c60c82794d031de0231b5a5746e"
}
},
{
"identity" : "swift-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-algorithms.git",
"state" : {
"revision" : "f6919dfc309e7f1b56224378b11e28bab5bccc42",
"version" : "1.2.0"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -154,6 +208,15 @@
"version" : "1.21.0"
}
},
{
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-numerics.git",
"state" : {
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
},
{
"identity" : "swift-otel",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -207,6 +270,24 @@
"revision" : "4d2cf7c64443cdf4df833d0bedd767bf9dbc49d9",
"version" : "0.1.3"
}
},
{
"identity" : "vapor",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/vapor.git",
"state" : {
"revision" : "a823735db57b46100b0c61cdfc5a08525b1e7cad",
"version" : "4.102.1"
}
},
{
"identity" : "websocket-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/websocket-kit.git",
"state" : {
"revision" : "4232d34efa49f633ba61afde365d3896fc7f8740",
"version" : "2.15.0"
}
}
],
"version" : 3
Expand Down
2 changes: 2 additions & 0 deletions Boka/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.4.0"),
.package(url: "https://github.com/slashmo/swift-otel.git", from: "0.9.0"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.6.0"),
.package(url: "https://github.com/vapor/console-kit.git", from: "4.14.3"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand All @@ -27,6 +28,7 @@ let package = Package(
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
.product(name: "OTel", package: "swift-otel"),
.product(name: "OTLPGRPC", package: "swift-otel"),
.product(name: "ConsoleKit", package: "console-kit"),
]
),
.testTarget(
Expand Down
20 changes: 16 additions & 4 deletions Boka/Sources/Boka.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ import TracingUtils
struct Boka: AsyncParsableCommand {
mutating func run() async throws {
let services = try await Tracing.bootstrap("Boka")
let node = try await Node(genesis: .dev, config: .dev)
let logger = Logger(label: "boka")

logger.info("Starting Boka...")

let config = Node.Config(rpc: RPCConfig(listenAddress: "127.0.0.1", port: 9955), protocol: .dev)
var node: Node! = try await Node(genesis: .dev, config: config)
node.sayHello()

let config = ServiceGroupConfiguration(services: services, logger: Logger(label: "boka"))
let serviceGroup = ServiceGroup(configuration: config)
for service in services {
Task {
try await service.run()
}
}

try await node.wait()

node = nil

try await serviceGroup.run()
logger.info("Exiting...")
}
}
12 changes: 7 additions & 5 deletions Boka/Sources/Tracing.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ConsoleKit
import OTel
import OTLPGRPC
import ServiceLifecycle
Expand All @@ -6,11 +7,12 @@ import TracingUtils
public enum Tracing {
public static func bootstrap(_ serviceName: String) async throws -> [Service] {
// Bootstrap the logging backend with the OTel metadata provider which includes span IDs in logging messages.
LoggingSystem.bootstrap { label in
var handler = StreamLogHandler.standardError(label: label, metadataProvider: .otel)
handler.logLevel = .trace
return handler
}
LoggingSystem.bootstrap(
fragment: timestampDefaultLoggerFragment(),
console: Terminal(),
level: .trace,
metadataProvider: .otel
)

// Configure OTel resource detection to automatically apply helpful attributes to events.
let environment = OTelEnvironment.detected()
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ lint: githooks
.PHONY: format
format: githooks
swiftformat .

.PHONY: run
run: githooks
swift run --package-path Boka
98 changes: 67 additions & 31 deletions Node/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
{
"originHash" : "9029b5282681161e046bf609299ad38f9148736fe1e0db6b71446a8b1ce5274b",
"originHash" : "9e44dd111684519469ea1d4307ee132a892ac71a51c358d02a5b1ff1d5323474",
"pins" : [
{
"identity" : "async-http-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/async-http-client.git",
"state" : {
"revision" : "0ae99db85b2b9d1e79b362bd31fd1ffe492f7c47",
"version" : "1.21.2"
}
},
{
"identity" : "async-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/async-kit.git",
"state" : {
"revision" : "e048c8ee94967e8d8a1c2ec0e1156d6f7fa34d31",
"version" : "1.20.0"
}
},
{
"identity" : "blake2.swift",
"kind" : "remoteSourceControl",
Expand All @@ -11,12 +29,30 @@
}
},
{
"identity" : "grpc-swift",
"identity" : "console-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/grpc/grpc-swift.git",
"location" : "https://github.com/vapor/console-kit.git",
"state" : {
"revision" : "6a90b7e77e29f9bda6c2b3a4165a40d6c02cfda1",
"version" : "1.23.0"
"revision" : "9f7932f22ab6f64aafadc14491e694179b7d0f6f",
"version" : "4.14.3"
}
},
{
"identity" : "multipart-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/multipart-kit.git",
"state" : {
"revision" : "a31236f24bfd2ea2f520a74575881f6731d7ae68",
"version" : "4.7.0"
}
},
{
"identity" : "routing-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/routing-kit.git",
"state" : {
"revision" : "8c9a227476555c55837e569be71944e02a056b72",
"version" : "4.9.1"
}
},
{
Expand All @@ -29,12 +65,12 @@
}
},
{
"identity" : "swift-async-algorithms",
"identity" : "swift-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-async-algorithms.git",
"location" : "https://github.com/apple/swift-algorithms.git",
"state" : {
"revision" : "6ae9a051f76b81cc668305ceed5b0e0a7fd93d20",
"version" : "1.0.1"
"revision" : "f6919dfc309e7f1b56224378b11e28bab5bccc42",
"version" : "1.2.0"
}
},
{
Expand Down Expand Up @@ -146,21 +182,12 @@
}
},
{
"identity" : "swift-otel",
"kind" : "remoteSourceControl",
"location" : "https://github.com/slashmo/swift-otel.git",
"state" : {
"revision" : "8c271c7fed34a39f29c728598b3358fbdddf8ff4",
"version" : "0.9.0"
}
},
{
"identity" : "swift-protobuf",
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-protobuf.git",
"location" : "https://github.com/apple/swift-numerics.git",
"state" : {
"revision" : "e17d61f26df0f0e06f58f6977ba05a097a720106",
"version" : "1.27.1"
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
},
{
Expand All @@ -172,15 +199,6 @@
"version" : "1.1.0"
}
},
{
"identity" : "swift-service-lifecycle",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/swift-service-lifecycle.git",
"state" : {
"revision" : "24c800fb494fbee6e42bc156dc94232dc08971af",
"version" : "2.6.1"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
Expand All @@ -198,6 +216,24 @@
"revision" : "4d2cf7c64443cdf4df833d0bedd767bf9dbc49d9",
"version" : "0.1.3"
}
},
{
"identity" : "vapor",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/vapor.git",
"state" : {
"revision" : "a823735db57b46100b0c61cdfc5a08525b1e7cad",
"version" : "4.102.1"
}
},
{
"identity" : "websocket-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/websocket-kit.git",
"state" : {
"revision" : "4232d34efa49f633ba61afde365d3896fc7f8740",
"version" : "2.15.0"
}
}
],
"version" : 3
Expand Down
2 changes: 2 additions & 0 deletions Node/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let package = Package(
.package(path: "../Utils"),
.package(path: "../Blockchain"),
.package(path: "../TracingUtils"),
.package(path: "../RPC"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand All @@ -28,6 +29,7 @@ let package = Package(
"Utils",
"Blockchain",
"TracingUtils",
"RPC",
]
),
.testTarget(
Expand Down
Loading

0 comments on commit d330873

Please sign in to comment.