Skip to content

Commit

Permalink
pvm host-call updates (#215)
Browse files Browse the repository at this point in the history
* update

* pass test vector first
  • Loading branch information
qiweiii authored Nov 3, 2024
1 parent 026e249 commit 875a7f7
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 289 deletions.
2 changes: 1 addition & 1 deletion Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public struct ProtocolConfig: Sendable, Codable, Equatable {
// L = 14, 400: The maximum age in timeslots of the lookup anchor.
public var maxLookupAnchorAge: Int

// M = 128: The size of a transfer memo in octets.
// WT = 128: The size of a transfer memo in octets.
public var transferMemoSize: Int

// N = 2: The number of ticket entries per validator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,78 +36,53 @@ public struct DeferredTransfers: Codable {
}
}

public struct AccumlateResultContext {
// s: updated current account
public var account: ServiceAccount?
// c
/// U: a characterization (i.e. values capable of representing) of state components
/// which are both needed and mutable by the accumulation process.
public struct AccumulateState {
/// d
public var serviceAccounts: [ServiceIndex: ServiceAccount]
/// i
public var validatorQueue: ConfigFixedSizeArray<
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
>
/// q
public var authorizationQueue: ConfigFixedSizeArray<
ConfigFixedSizeArray<
Data32,
ProtocolConfig.MaxAuthorizationsQueueItems
>,
ProtocolConfig.TotalNumberOfCores
>
// v
public var validatorQueue: ConfigFixedSizeArray<
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
>
// i
public var serviceIndex: ServiceIndex
// t
public var transfers: [DeferredTransfers]
// n
public var newAccounts: [ServiceIndex: ServiceAccount]
// p
/// x
public var privilegedServices: PrivilegedServices
}

public init(
account: ServiceAccount?,
authorizationQueue: ConfigFixedSizeArray<
ConfigFixedSizeArray<
Data32,
ProtocolConfig.MaxAuthorizationsQueueItems
>,
ProtocolConfig.TotalNumberOfCores
>,
validatorQueue: ConfigFixedSizeArray<
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
>,
serviceIndex: ServiceIndex,
transfers: [DeferredTransfers],
newAccounts: [ServiceIndex: ServiceAccount],
privilegedServices: PrivilegedServices
) {
self.account = account
self.authorizationQueue = authorizationQueue
self.validatorQueue = validatorQueue
self.serviceIndex = serviceIndex
self.transfers = transfers
self.newAccounts = newAccounts
self.privilegedServices = privilegedServices
}
/// X
public struct AccumlateResultContext {
/// d
public var serviceAccounts: [ServiceIndex: ServiceAccount]
/// s: the accumulating service account index
public var serviceIndex: ServiceIndex
/// u
public var accumulateState: AccumulateState
/// i
public var nextAccountIndex: ServiceIndex
/// t: deferred transfers
public var transfers: [DeferredTransfers]
}

public protocol AccumulateFunction {
func invoke(
config: ProtocolConfigRef,
// u
state: AccumulateState,
// s
serviceIndex: ServiceIndex,
code: Data,
serviceAccounts: [ServiceIndex: ServiceAccount],
// g
gas: Gas,
// o
arguments: [AccumulateArguments],
// other inputs needed (not directly in GP's Accumulation function signature)
validatorQueue: ConfigFixedSizeArray<
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
>,
authorizationQueue: ConfigFixedSizeArray<
ConfigFixedSizeArray<
Data32,
ProtocolConfig.MaxAuthorizationsQueueItems
>,
ProtocolConfig.TotalNumberOfCores
>,
privilegedServices: PrivilegedServices,
initialIndex: ServiceIndex,
timeslot: TimeslotIndex
) throws -> (ctx: AccumlateResultContext, result: Data32?)
) throws -> (state: AccumulateState, transfers: [DeferredTransfers], result: Data32?, gas: Gas)
}
29 changes: 12 additions & 17 deletions Blockchain/Sources/Blockchain/RuntimeProtocols/Accumulation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,17 @@ extension Accumulation {
assertionFailure("unreachable: service not found")
throw AccumulationError.invalidServiceIndex
}
let acc = try serviceAccounts[service].unwrap(orError: AccumulationError.invalidServiceIndex)
guard let code = acc.preimages[acc.codeHash] else {
continue
}
let (ctx, commitment) = try accumlateFunction.invoke(
let (newState, transfers, commitment, _) = try accumlateFunction.invoke(
config: config,
state: AccumulateState(
serviceAccounts: serviceAccounts,
validatorQueue: validatorQueue,
authorizationQueue: authorizationQueue,
privilegedServices: privilegedServices
),
serviceIndex: service,
code: code,
serviceAccounts: serviceAccounts,
gas: gas,
arguments: arguments,
validatorQueue: validatorQueue,
authorizationQueue: authorizationQueue,
privilegedServices: privilegedServices,
initialIndex: Blake2b256.hash(service.encode(), entropyPool.t0.data, block.header.timeslot.encode())
.data.decode(UInt32.self),
timeslot: block.header.timeslot
Expand All @@ -129,27 +126,25 @@ extension Accumulation {
commitments.append((service, commitment))
}

for (service, account) in ctx.newAccounts {
for (service, account) in newState.serviceAccounts {
guard newServiceAccounts[service] == nil else {
throw AccumulationError.duplicatedServiceIndex
}
newServiceAccounts[service] = account
}

newServiceAccounts[service] = ctx.account

switch service {
case privilegedServices.empower:
newPrivilegedServices = ctx.privilegedServices
newPrivilegedServices = newState.privilegedServices
case privilegedServices.assign:
newAuthorizationQueue = ctx.authorizationQueue
newAuthorizationQueue = newState.authorizationQueue
case privilegedServices.designate:
newValidatorQueue = ctx.validatorQueue
newValidatorQueue = newState.validatorQueue
default:
break
}

for transfer in ctx.transfers {
for transfer in transfers {
transferReceivers[transfer.sender, default: []].append(transfer)
}
}
Expand Down
16 changes: 2 additions & 14 deletions Blockchain/Sources/Blockchain/Types/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,25 +215,13 @@ extension State: Guaranteeing {
struct DummyFunction: AccumulateFunction, OnTransferFunction {
func invoke(
config _: ProtocolConfigRef,
state _: AccumulateState,
serviceIndex _: ServiceIndex,
code _: Data,
serviceAccounts _: [ServiceIndex: ServiceAccount],
gas _: Gas,
arguments _: [AccumulateArguments],
validatorQueue _: ConfigFixedSizeArray<
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
>,
authorizationQueue _: ConfigFixedSizeArray<
ConfigFixedSizeArray<
Data32,
ProtocolConfig.MaxAuthorizationsQueueItems
>,
ProtocolConfig.TotalNumberOfCores
>,
privilegedServices _: PrivilegedServices,
initialIndex _: ServiceIndex,
timeslot _: TimeslotIndex
) throws -> (ctx: AccumlateResultContext, result: Data32?) {
) throws -> (state: AccumulateState, transfers: [DeferredTransfers], result: Data32?, gas: Gas) {
fatalError("not implemented")
}

Expand Down
Loading

0 comments on commit 875a7f7

Please sign in to comment.