Skip to content

Commit 4626c64

Browse files
authored
Rpc refactoring (#227)
* rpc wip * wip * server name could be null * rpc refactoring * show rpc methods * fix
1 parent 7fcb90c commit 4626c64

File tree

25 files changed

+434
-190
lines changed

25 files changed

+434
-190
lines changed

Blockchain/Sources/Blockchain/State/State+Genesis.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,4 @@ extension State {
4242

4343
return (StateRef(state), block)
4444
}
45-
// TODO: add file genesis
46-
// public static func fileGenesis(config: ProtocolConfigRef) throws -> State
4745
}

Blockchain/Sources/Blockchain/State/State.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ public struct State: Sendable {
230230
await backend.rootHash
231231
}
232232
}
233+
234+
public func read(key: Data32) async throws -> Data? {
235+
let res = try layer[key].map { try JamEncoder.encode($0) }
236+
if let res {
237+
return res
238+
}
239+
return try await backend.readRaw(key)
240+
}
233241
}
234242

235243
extension State {

Blockchain/Sources/Blockchain/State/StateBackend.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ public final class StateBackend: Sendable {
4747
return ret
4848
}
4949

50-
public func write(_ values: any Sequence<(key: any StateKey, value: (Codable & Sendable)?)>) async throws {
51-
try await trie.update(values.map { try (key: $0.key.encode(), value: $0.value.map { try JamEncoder.encode($0) }) })
50+
public func write(_ values: any Sequence<(key: Data32, value: (Codable & Sendable)?)>) async throws {
51+
try await trie.update(values.map { try (key: $0.key, value: $0.value.map { try JamEncoder.encode($0) }) })
5252
try await trie.save()
5353
}
5454

55+
public func readRaw(_ key: Data32) async throws -> Data? {
56+
try await trie.read(key: key)
57+
}
58+
5559
public func writeRaw(_ values: [(key: Data32, value: Data?)]) async throws {
5660
try await trie.update(values)
5761
try await trie.save()

Blockchain/Sources/Blockchain/State/StateLayer.swift

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,190 +22,190 @@ private enum StateLayerValue: Sendable {
2222
}
2323

2424
// @unchecked because AnyHashable is not Sendable
25-
public struct StateLayer: @unchecked Sendable {
26-
private var changes: [AnyHashable: StateLayerValue] = [:]
25+
public struct StateLayer: Sendable {
26+
private var changes: [Data32: StateLayerValue] = [:]
2727

2828
public init(backend: StateBackend) async throws {
2929
let results = try await backend.batchRead(StateKeys.prefetchKeys)
3030

3131
for (key, value) in results {
32-
changes[AnyHashable(key)] = try .init(value.unwrap())
32+
changes[key.encode()] = try .init(value.unwrap())
3333
}
3434
}
3535

3636
public init(changes: [(key: any StateKey, value: Codable & Sendable)]) {
3737
for (key, value) in changes {
38-
self.changes[AnyHashable(key)] = .value(value)
38+
self.changes[key.encode()] = .value(value)
3939
}
4040
}
4141

4242
// α: The core αuthorizations pool.
4343
public var coreAuthorizationPool: StateKeys.CoreAuthorizationPoolKey.Value {
4444
get {
45-
changes[StateKeys.CoreAuthorizationPoolKey()]!.value()!
45+
changes[StateKeys.CoreAuthorizationPoolKey().encode()]!.value()!
4646
}
4747
set {
48-
changes[StateKeys.CoreAuthorizationPoolKey()] = .init(newValue)
48+
changes[StateKeys.CoreAuthorizationPoolKey().encode()] = .init(newValue)
4949
}
5050
}
5151

5252
// φ: The authorization queue.
5353
public var authorizationQueue: StateKeys.AuthorizationQueueKey.Value {
5454
get {
55-
changes[StateKeys.AuthorizationQueueKey()]!.value()!
55+
changes[StateKeys.AuthorizationQueueKey().encode()]!.value()!
5656
}
5757
set {
58-
changes[StateKeys.AuthorizationQueueKey()] = .init(newValue)
58+
changes[StateKeys.AuthorizationQueueKey().encode()] = .init(newValue)
5959
}
6060
}
6161

6262
// β: Information on the most recent βlocks.
6363
public var recentHistory: StateKeys.RecentHistoryKey.Value {
6464
get {
65-
changes[StateKeys.RecentHistoryKey()]!.value()!
65+
changes[StateKeys.RecentHistoryKey().encode()]!.value()!
6666
}
6767
set {
68-
changes[StateKeys.RecentHistoryKey()] = .init(newValue)
68+
changes[StateKeys.RecentHistoryKey().encode()] = .init(newValue)
6969
}
7070
}
7171

7272
// γ: State concerning Safrole.
7373
public var safroleState: StateKeys.SafroleStateKey.Value {
7474
get {
75-
changes[StateKeys.SafroleStateKey()]!.value()!
75+
changes[StateKeys.SafroleStateKey().encode()]!.value()!
7676
}
7777
set {
78-
changes[StateKeys.SafroleStateKey()] = .init(newValue)
78+
changes[StateKeys.SafroleStateKey().encode()] = .init(newValue)
7979
}
8080
}
8181

8282
// ψ: past judgements
8383
public var judgements: StateKeys.JudgementsKey.Value {
8484
get {
85-
changes[StateKeys.JudgementsKey()]!.value()!
85+
changes[StateKeys.JudgementsKey().encode()]!.value()!
8686
}
8787
set {
88-
changes[StateKeys.JudgementsKey()] = .init(newValue)
88+
changes[StateKeys.JudgementsKey().encode()] = .init(newValue)
8989
}
9090
}
9191

9292
// η: The eηtropy accumulator and epochal raηdomness.
9393
public var entropyPool: StateKeys.EntropyPoolKey.Value {
9494
get {
95-
changes[StateKeys.EntropyPoolKey()]!.value()!
95+
changes[StateKeys.EntropyPoolKey().encode()]!.value()!
9696
}
9797
set {
98-
changes[StateKeys.EntropyPoolKey()] = .init(newValue)
98+
changes[StateKeys.EntropyPoolKey().encode()] = .init(newValue)
9999
}
100100
}
101101

102102
// ι: The validator keys and metadata to be drawn from next.
103103
public var validatorQueue: StateKeys.ValidatorQueueKey.Value {
104104
get {
105-
changes[StateKeys.ValidatorQueueKey()]!.value()!
105+
changes[StateKeys.ValidatorQueueKey().encode()]!.value()!
106106
}
107107
set {
108-
changes[StateKeys.ValidatorQueueKey()] = .init(newValue)
108+
changes[StateKeys.ValidatorQueueKey().encode()] = .init(newValue)
109109
}
110110
}
111111

112112
// κ: The validator κeys and metadata currently active.
113113
public var currentValidators: StateKeys.CurrentValidatorsKey.Value {
114114
get {
115-
changes[StateKeys.CurrentValidatorsKey()]!.value()!
115+
changes[StateKeys.CurrentValidatorsKey().encode()]!.value()!
116116
}
117117
set {
118-
changes[StateKeys.CurrentValidatorsKey()] = .init(newValue)
118+
changes[StateKeys.CurrentValidatorsKey().encode()] = .init(newValue)
119119
}
120120
}
121121

122122
// λ: The validator keys and metadata which were active in the prior epoch.
123123
public var previousValidators: StateKeys.PreviousValidatorsKey.Value {
124124
get {
125-
changes[StateKeys.PreviousValidatorsKey()]!.value()!
125+
changes[StateKeys.PreviousValidatorsKey().encode()]!.value()!
126126
}
127127
set {
128-
changes[StateKeys.PreviousValidatorsKey()] = .init(newValue)
128+
changes[StateKeys.PreviousValidatorsKey().encode()] = .init(newValue)
129129
}
130130
}
131131

132132
// ρ: The ρending reports, per core, which are being made available prior to accumulation.
133133
public var reports: StateKeys.ReportsKey.Value {
134134
get {
135-
changes[StateKeys.ReportsKey()]!.value()!
135+
changes[StateKeys.ReportsKey().encode()]!.value()!
136136
}
137137
set {
138-
changes[StateKeys.ReportsKey()] = .init(newValue)
138+
changes[StateKeys.ReportsKey().encode()] = .init(newValue)
139139
}
140140
}
141141

142142
// τ: The most recent block’s τimeslot.
143143
public var timeslot: StateKeys.TimeslotKey.Value {
144144
get {
145-
changes[StateKeys.TimeslotKey()]!.value()!
145+
changes[StateKeys.TimeslotKey().encode()]!.value()!
146146
}
147147
set {
148-
changes[StateKeys.TimeslotKey()] = .init(newValue)
148+
changes[StateKeys.TimeslotKey().encode()] = .init(newValue)
149149
}
150150
}
151151

152152
// χ: The privileged service indices.
153153
public var privilegedServices: StateKeys.PrivilegedServicesKey.Value {
154154
get {
155-
changes[StateKeys.PrivilegedServicesKey()]!.value()!
155+
changes[StateKeys.PrivilegedServicesKey().encode()]!.value()!
156156
}
157157
set {
158-
changes[StateKeys.PrivilegedServicesKey()] = .init(newValue)
158+
changes[StateKeys.PrivilegedServicesKey().encode()] = .init(newValue)
159159
}
160160
}
161161

162162
// π: The activity statistics for the validators.
163163
public var activityStatistics: StateKeys.ActivityStatisticsKey.Value {
164164
get {
165-
changes[StateKeys.ActivityStatisticsKey()]!.value()!
165+
changes[StateKeys.ActivityStatisticsKey().encode()]!.value()!
166166
}
167167
set {
168-
changes[StateKeys.ActivityStatisticsKey()] = .init(newValue)
168+
changes[StateKeys.ActivityStatisticsKey().encode()] = .init(newValue)
169169
}
170170
}
171171

172172
// ϑ: The accumulation queue.
173173
public var accumulationQueue: StateKeys.AccumulationQueueKey.Value {
174174
get {
175-
changes[StateKeys.AccumulationQueueKey()]!.value()!
175+
changes[StateKeys.AccumulationQueueKey().encode()]!.value()!
176176
}
177177
set {
178-
changes[StateKeys.AccumulationQueueKey()] = .init(newValue)
178+
changes[StateKeys.AccumulationQueueKey().encode()] = .init(newValue)
179179
}
180180
}
181181

182182
// ξ: The accumulation history.
183183
public var accumulationHistory: StateKeys.AccumulationHistoryKey.Value {
184184
get {
185-
changes[StateKeys.AccumulationHistoryKey()]!.value()!
185+
changes[StateKeys.AccumulationHistoryKey().encode()]!.value()!
186186
}
187187
set {
188-
changes[StateKeys.AccumulationHistoryKey()] = .init(newValue)
188+
changes[StateKeys.AccumulationHistoryKey().encode()] = .init(newValue)
189189
}
190190
}
191191

192192
// δ: The (prior) state of the service accounts.
193193
public subscript(serviceAccount index: ServiceIndex) -> StateKeys.ServiceAccountKey.Value? {
194194
get {
195-
changes[StateKeys.ServiceAccountKey(index: index)]?.value()
195+
changes[StateKeys.ServiceAccountKey(index: index).encode()]?.value()
196196
}
197197
set {
198-
changes[StateKeys.ServiceAccountKey(index: index)] = .init(newValue)
198+
changes[StateKeys.ServiceAccountKey(index: index).encode()] = .init(newValue)
199199
}
200200
}
201201

202202
// s
203203
public subscript(serviceAccount index: ServiceIndex, storageKey key: Data32) -> StateKeys.ServiceAccountStorageKey.Value? {
204204
get {
205-
changes[StateKeys.ServiceAccountStorageKey(index: index, key: key)]?.value()
205+
changes[StateKeys.ServiceAccountStorageKey(index: index, key: key).encode()]?.value()
206206
}
207207
set {
208-
changes[StateKeys.ServiceAccountStorageKey(index: index, key: key)] = .init(newValue)
208+
changes[StateKeys.ServiceAccountStorageKey(index: index, key: key).encode()] = .init(newValue)
209209
}
210210
}
211211

@@ -214,10 +214,10 @@ public struct StateLayer: @unchecked Sendable {
214214
serviceAccount index: ServiceIndex, preimageHash hash: Data32
215215
) -> StateKeys.ServiceAccountPreimagesKey.Value? {
216216
get {
217-
changes[StateKeys.ServiceAccountPreimagesKey(index: index, hash: hash)]?.value()
217+
changes[StateKeys.ServiceAccountPreimagesKey(index: index, hash: hash).encode()]?.value()
218218
}
219219
set {
220-
changes[StateKeys.ServiceAccountPreimagesKey(index: index, hash: hash)] = .init(newValue)
220+
changes[StateKeys.ServiceAccountPreimagesKey(index: index, hash: hash).encode()] = .init(newValue)
221221
}
222222
}
223223

@@ -227,36 +227,51 @@ public struct StateLayer: @unchecked Sendable {
227227
) -> StateKeys.ServiceAccountPreimageInfoKey.Value? {
228228
get {
229229
changes[
230-
StateKeys.ServiceAccountPreimageInfoKey(index: index, hash: hash, length: length)
230+
StateKeys.ServiceAccountPreimageInfoKey(
231+
index: index, hash: hash, length: length
232+
).encode()
231233
]?.value()
232234
}
233235
set {
234-
changes[StateKeys.ServiceAccountPreimageInfoKey(index: index, hash: hash, length: length)] = .init(newValue)
236+
changes[
237+
StateKeys.ServiceAccountPreimageInfoKey(
238+
index: index, hash: hash, length: length
239+
).encode()
240+
] = .init(newValue)
235241
}
236242
}
237243
}
238244

239245
extension StateLayer {
240-
public func toKV() -> some Sequence<(key: any StateKey, value: (Codable & Sendable)?)> {
241-
changes.map { (key: $0.key.base as! any StateKey, value: $0.value.value()) }
246+
public func toKV() -> some Sequence<(key: Data32, value: (Codable & Sendable)?)> {
247+
changes.map { (key: $0.key, value: $0.value.value()) }
242248
}
243249
}
244250

245251
extension StateLayer {
246252
public func read<Key: StateKey>(_ key: Key) -> Key.Value? {
247-
changes[key] as? Key.Value
253+
changes[key.encode()] as? Key.Value
248254
}
249255

250256
public mutating func write<Key: StateKey>(_ key: Key, value: Key.Value?) {
251-
changes[key] = .init(value)
257+
changes[key.encode()] = .init(value)
252258
}
253259

254260
public subscript(key: any StateKey) -> (Codable & Sendable)? {
255261
get {
256-
changes[AnyHashable(key)]?.value()
262+
changes[key.encode()]?.value()
263+
}
264+
set {
265+
changes[key.encode()] = .init(newValue)
266+
}
267+
}
268+
269+
public subscript(key: Data32) -> (Codable & Sendable)? {
270+
get {
271+
changes[key]?.value()
257272
}
258273
set {
259-
changes[AnyHashable(key)] = .init(newValue)
274+
changes[key] = .init(newValue)
260275
}
261276
}
262277
}

Boka/Sources/Boka.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ struct Boka: AsyncParsableCommand {
167167
rpc: rpcConfig,
168168
network: networkConfig,
169169
peers: peers,
170-
local: local
170+
local: local,
171+
name: name
171172
)
172173

173174
let node: Node = if validator {

Codec/Sources/Codec/JamEncoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ public class JamEncoder {
1111
encoder = EncodeContext(Data(capacity: capacity))
1212
}
1313

14-
public func encode(_ value: some Encodable) throws {
14+
public func encode(_ value: any Encodable) throws {
1515
try encoder.encode(value)
1616
}
1717

18-
public static func encode(_ value: some Encodable) throws -> Data {
18+
public static func encode(_ value: any Encodable) throws -> Data {
1919
let encoder = if let value = value as? EncodedSize {
2020
JamEncoder(capacity: value.encodedSize)
2121
} else {

Networking/Sources/MsQuicSwift/QuicListener.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private final class ListenerHandle: Sendable {
132132
localAddress: NetAddr(quicAddr: evtInfo.pointee.LocalAddress.pointee),
133133
remoteAddress: NetAddr(quicAddr: evtInfo.pointee.RemoteAddress.pointee),
134134
negotiatedAlpn: Data(bytes: evtInfo.pointee.NegotiatedAlpn, count: Int(evtInfo.pointee.NegotiatedAlpnLength)),
135-
serverName: String(
135+
serverName: evtInfo.pointee.ServerNameLength == 0 ? "" : String(
136136
bytes: Data(bytes: evtInfo.pointee.ServerName, count: Int(evtInfo.pointee.ServerNameLength)),
137137
encoding: .utf8
138138
) ?? ""

Node/Sources/Node/Genesis.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension Genesis {
4747
var kv = [String: Data]()
4848
for (key, value) in state.value.layer.toKV() {
4949
if let value {
50-
kv[key.encode().toHexString()] = try JamEncoder.encode(value)
50+
kv[key.toHexString()] = try JamEncoder.encode(value)
5151
}
5252
}
5353
return try ChainSpec(

0 commit comments

Comments
 (0)