Skip to content

Commit 875a7f7

Browse files
authored
pvm host-call updates (#215)
* update * pass test vector first
1 parent 026e249 commit 875a7f7

File tree

12 files changed

+233
-289
lines changed

12 files changed

+233
-289
lines changed

Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public struct ProtocolConfig: Sendable, Codable, Equatable {
4949
// L = 14, 400: The maximum age in timeslots of the lookup anchor.
5050
public var maxLookupAnchorAge: Int
5151

52-
// M = 128: The size of a transfer memo in octets.
52+
// WT = 128: The size of a transfer memo in octets.
5353
public var transferMemoSize: Int
5454

5555
// N = 2: The number of ticket entries per validator.

Blockchain/Sources/Blockchain/RuntimeProtocols/AccumulateFunction.swift

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -36,78 +36,53 @@ public struct DeferredTransfers: Codable {
3636
}
3737
}
3838

39-
public struct AccumlateResultContext {
40-
// s: updated current account
41-
public var account: ServiceAccount?
42-
// c
39+
/// U: a characterization (i.e. values capable of representing) of state components
40+
/// which are both needed and mutable by the accumulation process.
41+
public struct AccumulateState {
42+
/// d
43+
public var serviceAccounts: [ServiceIndex: ServiceAccount]
44+
/// i
45+
public var validatorQueue: ConfigFixedSizeArray<
46+
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
47+
>
48+
/// q
4349
public var authorizationQueue: ConfigFixedSizeArray<
4450
ConfigFixedSizeArray<
4551
Data32,
4652
ProtocolConfig.MaxAuthorizationsQueueItems
4753
>,
4854
ProtocolConfig.TotalNumberOfCores
4955
>
50-
// v
51-
public var validatorQueue: ConfigFixedSizeArray<
52-
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
53-
>
54-
// i
55-
public var serviceIndex: ServiceIndex
56-
// t
57-
public var transfers: [DeferredTransfers]
58-
// n
59-
public var newAccounts: [ServiceIndex: ServiceAccount]
60-
// p
56+
/// x
6157
public var privilegedServices: PrivilegedServices
58+
}
6259

63-
public init(
64-
account: ServiceAccount?,
65-
authorizationQueue: ConfigFixedSizeArray<
66-
ConfigFixedSizeArray<
67-
Data32,
68-
ProtocolConfig.MaxAuthorizationsQueueItems
69-
>,
70-
ProtocolConfig.TotalNumberOfCores
71-
>,
72-
validatorQueue: ConfigFixedSizeArray<
73-
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
74-
>,
75-
serviceIndex: ServiceIndex,
76-
transfers: [DeferredTransfers],
77-
newAccounts: [ServiceIndex: ServiceAccount],
78-
privilegedServices: PrivilegedServices
79-
) {
80-
self.account = account
81-
self.authorizationQueue = authorizationQueue
82-
self.validatorQueue = validatorQueue
83-
self.serviceIndex = serviceIndex
84-
self.transfers = transfers
85-
self.newAccounts = newAccounts
86-
self.privilegedServices = privilegedServices
87-
}
60+
/// X
61+
public struct AccumlateResultContext {
62+
/// d
63+
public var serviceAccounts: [ServiceIndex: ServiceAccount]
64+
/// s: the accumulating service account index
65+
public var serviceIndex: ServiceIndex
66+
/// u
67+
public var accumulateState: AccumulateState
68+
/// i
69+
public var nextAccountIndex: ServiceIndex
70+
/// t: deferred transfers
71+
public var transfers: [DeferredTransfers]
8872
}
8973

9074
public protocol AccumulateFunction {
9175
func invoke(
9276
config: ProtocolConfigRef,
77+
// u
78+
state: AccumulateState,
79+
// s
9380
serviceIndex: ServiceIndex,
94-
code: Data,
95-
serviceAccounts: [ServiceIndex: ServiceAccount],
81+
// g
9682
gas: Gas,
83+
// o
9784
arguments: [AccumulateArguments],
98-
// other inputs needed (not directly in GP's Accumulation function signature)
99-
validatorQueue: ConfigFixedSizeArray<
100-
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
101-
>,
102-
authorizationQueue: ConfigFixedSizeArray<
103-
ConfigFixedSizeArray<
104-
Data32,
105-
ProtocolConfig.MaxAuthorizationsQueueItems
106-
>,
107-
ProtocolConfig.TotalNumberOfCores
108-
>,
109-
privilegedServices: PrivilegedServices,
11085
initialIndex: ServiceIndex,
11186
timeslot: TimeslotIndex
112-
) throws -> (ctx: AccumlateResultContext, result: Data32?)
87+
) throws -> (state: AccumulateState, transfers: [DeferredTransfers], result: Data32?, gas: Gas)
11388
}

Blockchain/Sources/Blockchain/RuntimeProtocols/Accumulation.swift

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,17 @@ extension Accumulation {
107107
assertionFailure("unreachable: service not found")
108108
throw AccumulationError.invalidServiceIndex
109109
}
110-
let acc = try serviceAccounts[service].unwrap(orError: AccumulationError.invalidServiceIndex)
111-
guard let code = acc.preimages[acc.codeHash] else {
112-
continue
113-
}
114-
let (ctx, commitment) = try accumlateFunction.invoke(
110+
let (newState, transfers, commitment, _) = try accumlateFunction.invoke(
115111
config: config,
112+
state: AccumulateState(
113+
serviceAccounts: serviceAccounts,
114+
validatorQueue: validatorQueue,
115+
authorizationQueue: authorizationQueue,
116+
privilegedServices: privilegedServices
117+
),
116118
serviceIndex: service,
117-
code: code,
118-
serviceAccounts: serviceAccounts,
119119
gas: gas,
120120
arguments: arguments,
121-
validatorQueue: validatorQueue,
122-
authorizationQueue: authorizationQueue,
123-
privilegedServices: privilegedServices,
124121
initialIndex: Blake2b256.hash(service.encode(), entropyPool.t0.data, block.header.timeslot.encode())
125122
.data.decode(UInt32.self),
126123
timeslot: block.header.timeslot
@@ -129,27 +126,25 @@ extension Accumulation {
129126
commitments.append((service, commitment))
130127
}
131128

132-
for (service, account) in ctx.newAccounts {
129+
for (service, account) in newState.serviceAccounts {
133130
guard newServiceAccounts[service] == nil else {
134131
throw AccumulationError.duplicatedServiceIndex
135132
}
136133
newServiceAccounts[service] = account
137134
}
138135

139-
newServiceAccounts[service] = ctx.account
140-
141136
switch service {
142137
case privilegedServices.empower:
143-
newPrivilegedServices = ctx.privilegedServices
138+
newPrivilegedServices = newState.privilegedServices
144139
case privilegedServices.assign:
145-
newAuthorizationQueue = ctx.authorizationQueue
140+
newAuthorizationQueue = newState.authorizationQueue
146141
case privilegedServices.designate:
147-
newValidatorQueue = ctx.validatorQueue
142+
newValidatorQueue = newState.validatorQueue
148143
default:
149144
break
150145
}
151146

152-
for transfer in ctx.transfers {
147+
for transfer in transfers {
153148
transferReceivers[transfer.sender, default: []].append(transfer)
154149
}
155150
}

Blockchain/Sources/Blockchain/Types/State.swift

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,25 +215,13 @@ extension State: Guaranteeing {
215215
struct DummyFunction: AccumulateFunction, OnTransferFunction {
216216
func invoke(
217217
config _: ProtocolConfigRef,
218+
state _: AccumulateState,
218219
serviceIndex _: ServiceIndex,
219-
code _: Data,
220-
serviceAccounts _: [ServiceIndex: ServiceAccount],
221220
gas _: Gas,
222221
arguments _: [AccumulateArguments],
223-
validatorQueue _: ConfigFixedSizeArray<
224-
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
225-
>,
226-
authorizationQueue _: ConfigFixedSizeArray<
227-
ConfigFixedSizeArray<
228-
Data32,
229-
ProtocolConfig.MaxAuthorizationsQueueItems
230-
>,
231-
ProtocolConfig.TotalNumberOfCores
232-
>,
233-
privilegedServices _: PrivilegedServices,
234222
initialIndex _: ServiceIndex,
235223
timeslot _: TimeslotIndex
236-
) throws -> (ctx: AccumlateResultContext, result: Data32?) {
224+
) throws -> (state: AccumulateState, transfers: [DeferredTransfers], result: Data32?, gas: Gas) {
237225
fatalError("not implemented")
238226
}
239227

0 commit comments

Comments
 (0)