@proto-kit/protocol 0.1.1-develop.1086
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +201 -0
- package/README.md +45 -0
- package/dist/Constants.d.ts +4 -0
- package/dist/Constants.d.ts.map +1 -0
- package/dist/Constants.js +3 -0
- package/dist/hooks/AccountStateHook.d.ts +52 -0
- package/dist/hooks/AccountStateHook.d.ts.map +1 -0
- package/dist/hooks/AccountStateHook.js +47 -0
- package/dist/hooks/BlockHeightHook.d.ts +7 -0
- package/dist/hooks/BlockHeightHook.d.ts.map +1 -0
- package/dist/hooks/BlockHeightHook.js +15 -0
- package/dist/hooks/LastStateRootBlockHook.d.ts +8 -0
- package/dist/hooks/LastStateRootBlockHook.d.ts.map +1 -0
- package/dist/hooks/LastStateRootBlockHook.js +15 -0
- package/dist/hooks/NoopBlockHook.d.ts +9 -0
- package/dist/hooks/NoopBlockHook.d.ts.map +1 -0
- package/dist/hooks/NoopBlockHook.js +9 -0
- package/dist/hooks/NoopSettlementHook.d.ts +6 -0
- package/dist/hooks/NoopSettlementHook.d.ts.map +1 -0
- package/dist/hooks/NoopSettlementHook.js +17 -0
- package/dist/hooks/NoopTransactionHook.d.ts +6 -0
- package/dist/hooks/NoopTransactionHook.d.ts.map +1 -0
- package/dist/hooks/NoopTransactionHook.js +7 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +53 -0
- package/dist/model/MethodPublicOutput.d.ts +113 -0
- package/dist/model/MethodPublicOutput.d.ts.map +1 -0
- package/dist/model/MethodPublicOutput.js +14 -0
- package/dist/model/Option.d.ts +122 -0
- package/dist/model/Option.d.ts.map +1 -0
- package/dist/model/Option.js +110 -0
- package/dist/model/Path.d.ts +31 -0
- package/dist/model/Path.d.ts.map +1 -0
- package/dist/model/Path.js +42 -0
- package/dist/model/RuntimeLike.d.ts +11 -0
- package/dist/model/RuntimeLike.d.ts.map +1 -0
- package/dist/model/RuntimeLike.js +1 -0
- package/dist/model/StateTransition.d.ts +135 -0
- package/dist/model/StateTransition.d.ts.map +1 -0
- package/dist/model/StateTransition.js +68 -0
- package/dist/model/StateTransitionProvableBatch.d.ts +172 -0
- package/dist/model/StateTransitionProvableBatch.d.ts.map +1 -0
- package/dist/model/StateTransitionProvableBatch.js +85 -0
- package/dist/model/network/NetworkState.d.ts +160 -0
- package/dist/model/network/NetworkState.d.ts.map +1 -0
- package/dist/model/network/NetworkState.js +31 -0
- package/dist/model/transaction/RuntimeTransaction.d.ts +122 -0
- package/dist/model/transaction/RuntimeTransaction.d.ts.map +1 -0
- package/dist/model/transaction/RuntimeTransaction.js +86 -0
- package/dist/model/transaction/SignedTransaction.d.ts +107 -0
- package/dist/model/transaction/SignedTransaction.d.ts.map +1 -0
- package/dist/model/transaction/SignedTransaction.js +33 -0
- package/dist/model/transaction/ValueOption.d.ts +173 -0
- package/dist/model/transaction/ValueOption.d.ts.map +1 -0
- package/dist/model/transaction/ValueOption.js +24 -0
- package/dist/protocol/Protocol.d.ts +43 -0
- package/dist/protocol/Protocol.d.ts.map +1 -0
- package/dist/protocol/Protocol.js +95 -0
- package/dist/protocol/ProtocolEnvironment.d.ts +9 -0
- package/dist/protocol/ProtocolEnvironment.d.ts.map +1 -0
- package/dist/protocol/ProtocolEnvironment.js +1 -0
- package/dist/protocol/ProtocolModule.d.ts +9 -0
- package/dist/protocol/ProtocolModule.d.ts.map +1 -0
- package/dist/protocol/ProtocolModule.js +12 -0
- package/dist/protocol/ProvableBlockHook.d.ts +8 -0
- package/dist/protocol/ProvableBlockHook.d.ts.map +1 -0
- package/dist/protocol/ProvableBlockHook.js +4 -0
- package/dist/protocol/ProvableTransactionHook.d.ts +7 -0
- package/dist/protocol/ProvableTransactionHook.d.ts.map +1 -0
- package/dist/protocol/ProvableTransactionHook.js +3 -0
- package/dist/protocol/TransitioningProtocolModule.d.ts +10 -0
- package/dist/protocol/TransitioningProtocolModule.d.ts.map +1 -0
- package/dist/protocol/TransitioningProtocolModule.js +8 -0
- package/dist/prover/block/BlockProvable.d.ts +395 -0
- package/dist/prover/block/BlockProvable.d.ts.map +1 -0
- package/dist/prover/block/BlockProvable.js +45 -0
- package/dist/prover/block/BlockProver.d.ts +92 -0
- package/dist/prover/block/BlockProver.d.ts.map +1 -0
- package/dist/prover/block/BlockProver.js +472 -0
- package/dist/prover/block/accummulators/BlockHashMerkleTree.d.ts +64 -0
- package/dist/prover/block/accummulators/BlockHashMerkleTree.d.ts.map +1 -0
- package/dist/prover/block/accummulators/BlockHashMerkleTree.js +16 -0
- package/dist/prover/block/accummulators/RuntimeVerificationKeyTree.d.ts +139 -0
- package/dist/prover/block/accummulators/RuntimeVerificationKeyTree.d.ts.map +1 -0
- package/dist/prover/block/accummulators/RuntimeVerificationKeyTree.js +20 -0
- package/dist/prover/block/services/RuntimeVerificationKeyRootService.d.ts +7 -0
- package/dist/prover/block/services/RuntimeVerificationKeyRootService.d.ts.map +1 -0
- package/dist/prover/block/services/RuntimeVerificationKeyRootService.js +22 -0
- package/dist/prover/statetransition/StateTransitionProvable.d.ts +174 -0
- package/dist/prover/statetransition/StateTransitionProvable.d.ts.map +1 -0
- package/dist/prover/statetransition/StateTransitionProvable.js +15 -0
- package/dist/prover/statetransition/StateTransitionProver.d.ts +51 -0
- package/dist/prover/statetransition/StateTransitionProver.d.ts.map +1 -0
- package/dist/prover/statetransition/StateTransitionProver.js +188 -0
- package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts +16 -0
- package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts.map +1 -0
- package/dist/prover/statetransition/StateTransitionWitnessProvider.js +16 -0
- package/dist/prover/statetransition/StateTransitionWitnessProviderReference.d.ts +7 -0
- package/dist/prover/statetransition/StateTransitionWitnessProviderReference.d.ts.map +1 -0
- package/dist/prover/statetransition/StateTransitionWitnessProviderReference.js +19 -0
- package/dist/settlement/ContractModule.d.ts +15 -0
- package/dist/settlement/ContractModule.d.ts.map +1 -0
- package/dist/settlement/ContractModule.js +11 -0
- package/dist/settlement/SettlementContractModule.d.ts +40 -0
- package/dist/settlement/SettlementContractModule.d.ts.map +1 -0
- package/dist/settlement/SettlementContractModule.js +71 -0
- package/dist/settlement/contracts/DispatchContractProtocolModule.d.ts +12 -0
- package/dist/settlement/contracts/DispatchContractProtocolModule.d.ts.map +1 -0
- package/dist/settlement/contracts/DispatchContractProtocolModule.js +35 -0
- package/dist/settlement/contracts/DispatchSmartContract.d.ts +22 -0
- package/dist/settlement/contracts/DispatchSmartContract.d.ts.map +1 -0
- package/dist/settlement/contracts/DispatchSmartContract.js +110 -0
- package/dist/settlement/contracts/SettlementContractProtocolModule.d.ts +19 -0
- package/dist/settlement/contracts/SettlementContractProtocolModule.d.ts.map +1 -0
- package/dist/settlement/contracts/SettlementContractProtocolModule.js +44 -0
- package/dist/settlement/contracts/SettlementSmartContract.d.ts +42 -0
- package/dist/settlement/contracts/SettlementSmartContract.d.ts.map +1 -0
- package/dist/settlement/contracts/SettlementSmartContract.js +241 -0
- package/dist/settlement/messages/Deposit.d.ts +64 -0
- package/dist/settlement/messages/Deposit.d.ts.map +1 -0
- package/dist/settlement/messages/Deposit.js +6 -0
- package/dist/settlement/messages/OutgoingMessageArgument.d.ts +189 -0
- package/dist/settlement/messages/OutgoingMessageArgument.d.ts.map +1 -0
- package/dist/settlement/messages/OutgoingMessageArgument.js +32 -0
- package/dist/settlement/messages/Withdrawal.d.ts +65 -0
- package/dist/settlement/messages/Withdrawal.d.ts.map +1 -0
- package/dist/settlement/messages/Withdrawal.js +13 -0
- package/dist/settlement/modularity/ProvableSettlementHook.d.ts +24 -0
- package/dist/settlement/modularity/ProvableSettlementHook.d.ts.map +1 -0
- package/dist/settlement/modularity/ProvableSettlementHook.js +3 -0
- package/dist/settlement/modules/NetworkStateSettlementModule.d.ts +11 -0
- package/dist/settlement/modules/NetworkStateSettlementModule.d.ts.map +1 -0
- package/dist/settlement/modules/NetworkStateSettlementModule.js +14 -0
- package/dist/state/State.d.ts +68 -0
- package/dist/state/State.d.ts.map +1 -0
- package/dist/state/State.js +129 -0
- package/dist/state/StateMap.d.ts +37 -0
- package/dist/state/StateMap.d.ts.map +1 -0
- package/dist/state/StateMap.js +55 -0
- package/dist/state/StateService.d.ts +6 -0
- package/dist/state/StateService.d.ts.map +1 -0
- package/dist/state/StateService.js +1 -0
- package/dist/state/StateServiceProvider.d.ts +8 -0
- package/dist/state/StateServiceProvider.d.ts.map +1 -0
- package/dist/state/StateServiceProvider.js +36 -0
- package/dist/state/assert/assert.d.ts +12 -0
- package/dist/state/assert/assert.d.ts.map +1 -0
- package/dist/state/assert/assert.js +30 -0
- package/dist/state/context/ProtocolMethodExecutionContext.d.ts +22 -0
- package/dist/state/context/ProtocolMethodExecutionContext.d.ts.map +1 -0
- package/dist/state/context/ProtocolMethodExecutionContext.js +28 -0
- package/dist/state/context/RuntimeMethodExecutionContext.d.ts +193 -0
- package/dist/state/context/RuntimeMethodExecutionContext.d.ts.map +1 -0
- package/dist/state/context/RuntimeMethodExecutionContext.js +135 -0
- package/dist/state/context/TransitionMethodExecutionContext.d.ts +23 -0
- package/dist/state/context/TransitionMethodExecutionContext.d.ts.map +1 -0
- package/dist/state/context/TransitionMethodExecutionContext.js +5 -0
- package/dist/state/protocol/ProtocolState.d.ts +7 -0
- package/dist/state/protocol/ProtocolState.d.ts.map +1 -0
- package/dist/state/protocol/ProtocolState.js +39 -0
- package/dist/utils/MinaPrefixedProvableHashList.d.ts +24 -0
- package/dist/utils/MinaPrefixedProvableHashList.d.ts.map +1 -0
- package/dist/utils/MinaPrefixedProvableHashList.js +52 -0
- package/dist/utils/PrefixedProvableHashList.d.ts +8 -0
- package/dist/utils/PrefixedProvableHashList.d.ts.map +1 -0
- package/dist/utils/PrefixedProvableHashList.js +12 -0
- package/dist/utils/ProvableHashList.d.ts +27 -0
- package/dist/utils/ProvableHashList.d.ts.map +1 -0
- package/dist/utils/ProvableHashList.js +43 -0
- package/dist/utils/ProvableReductionHashList.d.ts +14 -0
- package/dist/utils/ProvableReductionHashList.d.ts.map +1 -0
- package/dist/utils/ProvableReductionHashList.js +50 -0
- package/dist/utils/StateTransitionReductionList.d.ts +11 -0
- package/dist/utils/StateTransitionReductionList.d.ts.map +1 -0
- package/dist/utils/StateTransitionReductionList.js +60 -0
- package/dist/utils/utils.d.ts +11 -0
- package/dist/utils/utils.d.ts.map +1 -0
- package/dist/utils/utils.js +44 -0
- package/jest.config.cjs +1 -0
- package/package.json +36 -0
- package/src/Constants.ts +3 -0
- package/src/hooks/AccountStateHook.ts +48 -0
- package/src/hooks/BlockHeightHook.ts +17 -0
- package/src/hooks/LastStateRootBlockHook.ts +26 -0
- package/src/hooks/NoopBlockHook.ts +21 -0
- package/src/hooks/NoopSettlementHook.ts +20 -0
- package/src/hooks/NoopTransactionHook.ts +10 -0
- package/src/index.ts +53 -0
- package/src/model/MethodPublicOutput.ts +14 -0
- package/src/model/Option.ts +172 -0
- package/src/model/Path.ts +50 -0
- package/src/model/RuntimeLike.ts +12 -0
- package/src/model/StateTransition.ts +88 -0
- package/src/model/StateTransitionProvableBatch.ts +125 -0
- package/src/model/Transaction.ts +31 -0
- package/src/model/network/NetworkState.ts +33 -0
- package/src/model/transaction/RuntimeTransaction.ts +105 -0
- package/src/model/transaction/SignedTransaction.ts +47 -0
- package/src/model/transaction/ValueOption.ts +28 -0
- package/src/protocol/Protocol.ts +197 -0
- package/src/protocol/ProtocolEnvironment.ts +10 -0
- package/src/protocol/ProtocolModule.ts +27 -0
- package/src/protocol/ProvableBlockHook.ts +19 -0
- package/src/protocol/ProvableTransactionHook.ts +13 -0
- package/src/protocol/TransitioningProtocolModule.ts +12 -0
- package/src/prover/block/BlockProvable.ts +99 -0
- package/src/prover/block/BlockProver.ts +954 -0
- package/src/prover/block/accummulators/BlockHashMerkleTree.ts +16 -0
- package/src/prover/block/accummulators/RuntimeVerificationKeyTree.ts +24 -0
- package/src/prover/block/services/RuntimeVerificationKeyRootService.ts +20 -0
- package/src/prover/statetransition/StateTransitionProvable.ts +44 -0
- package/src/prover/statetransition/StateTransitionProver.ts +371 -0
- package/src/prover/statetransition/StateTransitionWitnessProvider.ts +23 -0
- package/src/prover/statetransition/StateTransitionWitnessProviderReference.ts +17 -0
- package/src/settlement/ContractModule.ts +24 -0
- package/src/settlement/SettlementContractModule.ts +132 -0
- package/src/settlement/contracts/DispatchContractProtocolModule.ts +39 -0
- package/src/settlement/contracts/DispatchSmartContract.ts +136 -0
- package/src/settlement/contracts/SettlementContractProtocolModule.ts +67 -0
- package/src/settlement/contracts/SettlementSmartContract.ts +344 -0
- package/src/settlement/messages/Deposit.ts +6 -0
- package/src/settlement/messages/OutgoingMessageArgument.ts +42 -0
- package/src/settlement/messages/Withdrawal.ts +14 -0
- package/src/settlement/modularity/ProvableSettlementHook.ts +33 -0
- package/src/settlement/modules/NetworkStateSettlementModule.ts +42 -0
- package/src/state/State.ts +175 -0
- package/src/state/StateMap.ts +73 -0
- package/src/state/StateService.ts +6 -0
- package/src/state/StateServiceProvider.ts +37 -0
- package/src/state/assert/assert.ts +35 -0
- package/src/state/context/RuntimeMethodExecutionContext.ts +174 -0
- package/src/state/context/TransitionMethodExecutionContext.ts +26 -0
- package/src/state/protocol/ProtocolState.ts +61 -0
- package/src/utils/MinaPrefixedProvableHashList.ts +73 -0
- package/src/utils/PrefixedProvableHashList.ts +21 -0
- package/src/utils/ProvableHashList.ts +50 -0
- package/src/utils/ProvableReductionHashList.ts +68 -0
- package/src/utils/StateTransitionReductionList.ts +89 -0
- package/src/utils/utils.ts +75 -0
- package/test/BlockProver.test.ts +211 -0
- package/test/Protocol.test.ts +37 -0
- package/test/State.test.ts +45 -0
- package/test/StateTransition.test.ts +174 -0
- package/test/TestingProtocol.ts +47 -0
- package/test/model/Option.test.ts +72 -0
- package/test/state/assert/assert.test.ts +56 -0
- package/test/tsconfig.json +7 -0
- package/test/utils/ProvableReductionHashList.test.ts +117 -0
- package/test/utils.test.ts +27 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { UInt64 } from "o1js";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ProvableSettlementHook,
|
|
5
|
+
SettlementHookInputs,
|
|
6
|
+
} from "../modularity/ProvableSettlementHook";
|
|
7
|
+
import { SettlementSmartContract } from "../contracts/SettlementSmartContract";
|
|
8
|
+
|
|
9
|
+
type NetworkStateSettlementModuleConfig = {
|
|
10
|
+
blocksPerL1Block: UInt64;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
14
|
+
export class NetworkStateSettlementModule extends ProvableSettlementHook<NetworkStateSettlementModuleConfig> {
|
|
15
|
+
public async beforeSettlement(
|
|
16
|
+
smartContract: SettlementSmartContract,
|
|
17
|
+
{
|
|
18
|
+
blockProof,
|
|
19
|
+
fromNetworkState,
|
|
20
|
+
toNetworkState,
|
|
21
|
+
contractState,
|
|
22
|
+
currentL1BlockHeight,
|
|
23
|
+
}: SettlementHookInputs
|
|
24
|
+
) {
|
|
25
|
+
const { lastSettlementL1BlockHeight } = contractState;
|
|
26
|
+
|
|
27
|
+
const blocksPerL1Block = this.config.blocksPerL1Block.toConstant();
|
|
28
|
+
|
|
29
|
+
const numL1Blocks = currentL1BlockHeight.sub(lastSettlementL1BlockHeight);
|
|
30
|
+
const expectedHeightDiff = numL1Blocks.toUInt64().mul(blocksPerL1Block);
|
|
31
|
+
|
|
32
|
+
const actualHeightDiff = toNetworkState.block.height.sub(
|
|
33
|
+
fromNetworkState.block.height
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const acceptableDerivation = numL1Blocks.mul(1).div(10);
|
|
37
|
+
|
|
38
|
+
// TODO Check within bounds efficiently
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/* eslint-enable @typescript-eslint/no-unused-vars */
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { Mixin } from "ts-mixer";
|
|
2
|
+
import { Bool, Field, Provable, type FlexibleProvablePure, Struct } from "o1js";
|
|
3
|
+
import { container } from "tsyringe";
|
|
4
|
+
import { dummyValue } from "@proto-kit/common";
|
|
5
|
+
|
|
6
|
+
import { Path } from "../model/Path";
|
|
7
|
+
import { Option } from "../model/Option";
|
|
8
|
+
import { StateTransition } from "../model/StateTransition";
|
|
9
|
+
|
|
10
|
+
import { StateServiceProvider } from "./StateServiceProvider";
|
|
11
|
+
import { RuntimeMethodExecutionContext } from "./context/RuntimeMethodExecutionContext";
|
|
12
|
+
|
|
13
|
+
export class WithPath {
|
|
14
|
+
public path?: Field;
|
|
15
|
+
|
|
16
|
+
public hasPathOrFail(): asserts this is { path: Path } {
|
|
17
|
+
if (!this.path) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
"Could not find 'path', did you forget to add '@state' to your state property?"
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class WithStateServiceProvider {
|
|
26
|
+
public stateServiceProvider?: StateServiceProvider;
|
|
27
|
+
|
|
28
|
+
public hasStateServiceOrFail(): asserts this is {
|
|
29
|
+
stateServiceProvider: StateServiceProvider;
|
|
30
|
+
} {
|
|
31
|
+
if (!this.stateServiceProvider) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
"Could not find 'stateServiceProvider', did you forget to add '@state' to your state property?"
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Utilities for runtime module state, such as get/set
|
|
41
|
+
*/
|
|
42
|
+
export class State<Value> extends Mixin(WithPath, WithStateServiceProvider) {
|
|
43
|
+
/**
|
|
44
|
+
* Creates a new state wrapper for the provided value type.
|
|
45
|
+
*
|
|
46
|
+
* @param valueType - Type of value to be stored (e.g. UInt64, Struct, ...)
|
|
47
|
+
* @returns New state for the given value type.
|
|
48
|
+
*/
|
|
49
|
+
public static from<Value>(valueType: FlexibleProvablePure<Value>) {
|
|
50
|
+
return new State<Value>(valueType);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public constructor(public valueType: FlexibleProvablePure<Value>) {
|
|
54
|
+
super();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private stateType = class StateType extends Struct({
|
|
58
|
+
value: this.valueType,
|
|
59
|
+
isSome: Bool,
|
|
60
|
+
}) {};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Returns the state that is currently the current state tree
|
|
64
|
+
* value: The value-fields, or if not state was found, dummy values
|
|
65
|
+
* isSome: Whether the values where found in the state or not
|
|
66
|
+
* (Basically, whether the value-fields are dummy values or actual values
|
|
67
|
+
* @private
|
|
68
|
+
*/
|
|
69
|
+
private async getState(): Promise<{ value: Value; isSome: Bool }> {
|
|
70
|
+
this.hasStateServiceOrFail();
|
|
71
|
+
this.hasPathOrFail();
|
|
72
|
+
|
|
73
|
+
const { path, stateServiceProvider, valueType } = this;
|
|
74
|
+
|
|
75
|
+
const { stateTransitions } = container
|
|
76
|
+
.resolve(RuntimeMethodExecutionContext)
|
|
77
|
+
.current().result;
|
|
78
|
+
|
|
79
|
+
// TODO Use Stateservice for this
|
|
80
|
+
// First try to find a match inside already created stateTransitions
|
|
81
|
+
let previousMutatingTransitions: StateTransition<any>[] = [];
|
|
82
|
+
previousMutatingTransitions = stateTransitions.filter((transition) =>
|
|
83
|
+
transition.path.equals(path).and(transition.to.isSome).toBoolean()
|
|
84
|
+
);
|
|
85
|
+
const pmtLength = previousMutatingTransitions.length;
|
|
86
|
+
|
|
87
|
+
let value =
|
|
88
|
+
pmtLength > 0
|
|
89
|
+
? // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
90
|
+
(previousMutatingTransitions[pmtLength - 1].to.value as Value)
|
|
91
|
+
: undefined;
|
|
92
|
+
|
|
93
|
+
if (value !== undefined) {
|
|
94
|
+
return { value, isSome: Bool(true) };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// If the value is still undefined, look it up in the stateService
|
|
98
|
+
const fields = await stateServiceProvider.stateService.get(path);
|
|
99
|
+
if (fields) {
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
101
|
+
value = valueType.fromFields(fields) as Value;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (value !== undefined) {
|
|
105
|
+
return { value, isSome: Bool(true) };
|
|
106
|
+
}
|
|
107
|
+
return { value: dummyValue(valueType), isSome: Bool(false) };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Provides an in-circuit witness for the current state representation,
|
|
112
|
+
* and constructs an Option out of it.
|
|
113
|
+
*
|
|
114
|
+
* @returns Optional value of the current state
|
|
115
|
+
*/
|
|
116
|
+
private async witnessFromState() {
|
|
117
|
+
// get the value from storage, or return a dummy value instead
|
|
118
|
+
// also check if the value exists in the storage or not
|
|
119
|
+
const { value, isSome } = await Provable.witnessAsync(
|
|
120
|
+
this.stateType,
|
|
121
|
+
async () => await this.getState()
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
return Option.from(isSome, value, this.valueType);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Retrieves the current state and creates a state transition
|
|
129
|
+
* anchoring the use of the current state value in the circuit.
|
|
130
|
+
*
|
|
131
|
+
* @returns Option representation of the current state.
|
|
132
|
+
*/
|
|
133
|
+
public async get(): Promise<Option<Value>> {
|
|
134
|
+
const option = await this.witnessFromState();
|
|
135
|
+
|
|
136
|
+
this.hasPathOrFail();
|
|
137
|
+
|
|
138
|
+
const stateTransition = StateTransition.from(this.path, option);
|
|
139
|
+
|
|
140
|
+
container
|
|
141
|
+
.resolve(RuntimeMethodExecutionContext)
|
|
142
|
+
.addStateTransition(stateTransition);
|
|
143
|
+
|
|
144
|
+
return option;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Sets a new state value by creating a state transition from
|
|
149
|
+
* the current value to the newly set value.
|
|
150
|
+
*
|
|
151
|
+
* The newly set value isn't available via state.get(), since the
|
|
152
|
+
* state transitions are not applied within the same circuit.
|
|
153
|
+
* You can however store and access your new value in
|
|
154
|
+
* a separate circuit variable.
|
|
155
|
+
*
|
|
156
|
+
* @param value - Value to be set as the current state
|
|
157
|
+
*/
|
|
158
|
+
public async set(value: Value) {
|
|
159
|
+
// link the transition to the current state
|
|
160
|
+
const fromOption = await this.witnessFromState();
|
|
161
|
+
const toOption = Option.fromValue(value, this.valueType);
|
|
162
|
+
|
|
163
|
+
this.hasPathOrFail();
|
|
164
|
+
|
|
165
|
+
const stateTransition = StateTransition.fromTo(
|
|
166
|
+
this.path,
|
|
167
|
+
fromOption,
|
|
168
|
+
toOption
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
container
|
|
172
|
+
.resolve(RuntimeMethodExecutionContext)
|
|
173
|
+
.addStateTransition(stateTransition);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { Field, FlexibleProvablePure } from "o1js";
|
|
2
|
+
import { Mixin } from "ts-mixer";
|
|
3
|
+
|
|
4
|
+
import { Path } from "../model/Path";
|
|
5
|
+
import { Option } from "../model/Option";
|
|
6
|
+
|
|
7
|
+
import { State, WithStateServiceProvider, WithPath } from "./State";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Map-like wrapper for state
|
|
11
|
+
*/
|
|
12
|
+
export class StateMap<KeyType, ValueType> extends Mixin(
|
|
13
|
+
WithPath,
|
|
14
|
+
WithStateServiceProvider
|
|
15
|
+
) {
|
|
16
|
+
/**
|
|
17
|
+
* Create a new state map with the given key and value types
|
|
18
|
+
*
|
|
19
|
+
* @param keyType - Type to be used as a key
|
|
20
|
+
* @param valueType - Type to be stored as a value
|
|
21
|
+
* @returns State map with provided key and value types.
|
|
22
|
+
*/
|
|
23
|
+
public static from<KeyType, ValueType>(
|
|
24
|
+
keyType: FlexibleProvablePure<KeyType>,
|
|
25
|
+
valueType: FlexibleProvablePure<ValueType>
|
|
26
|
+
): StateMap<KeyType, ValueType> {
|
|
27
|
+
return new StateMap<KeyType, ValueType>(keyType, valueType);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public constructor(
|
|
31
|
+
public keyType: FlexibleProvablePure<KeyType>,
|
|
32
|
+
public valueType: FlexibleProvablePure<ValueType>
|
|
33
|
+
) {
|
|
34
|
+
super();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public getPath(key: KeyType): Field {
|
|
38
|
+
this.hasPathOrFail();
|
|
39
|
+
return Path.fromKey(this.path, this.keyType, key);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Obtains a value for the provided key in the current state map.
|
|
44
|
+
*
|
|
45
|
+
* @param key - Key to obtain the state for
|
|
46
|
+
* @returns Value for the provided key.
|
|
47
|
+
*/
|
|
48
|
+
public async get(key: KeyType): Promise<Option<ValueType>> {
|
|
49
|
+
const state = State.from(this.valueType);
|
|
50
|
+
this.hasPathOrFail();
|
|
51
|
+
this.hasStateServiceOrFail();
|
|
52
|
+
|
|
53
|
+
state.path = this.getPath(key);
|
|
54
|
+
state.stateServiceProvider = this.stateServiceProvider;
|
|
55
|
+
return await state.get();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Sets a value for the given key in the current state map.
|
|
60
|
+
*
|
|
61
|
+
* @param key - Key to store the value under
|
|
62
|
+
* @param value - Value to be stored under the given key
|
|
63
|
+
*/
|
|
64
|
+
public async set(key: KeyType, value: ValueType): Promise<void> {
|
|
65
|
+
const state = State.from(this.valueType);
|
|
66
|
+
this.hasPathOrFail();
|
|
67
|
+
this.hasStateServiceOrFail();
|
|
68
|
+
|
|
69
|
+
state.path = Path.fromKey(this.path, this.keyType, key);
|
|
70
|
+
state.stateServiceProvider = this.stateServiceProvider;
|
|
71
|
+
return await state.set(value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { injectable } from "tsyringe";
|
|
2
|
+
import { log } from "@proto-kit/common";
|
|
3
|
+
|
|
4
|
+
import { SimpleAsyncStateService } from "./StateService";
|
|
5
|
+
|
|
6
|
+
const errors = {
|
|
7
|
+
stateServiceNotSet: () =>
|
|
8
|
+
new Error(
|
|
9
|
+
"StateService has not been set yet. Be sure to either call your runtime or protocol function by creating them with an AppChain or by setting the stateService manually."
|
|
10
|
+
),
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
@injectable()
|
|
14
|
+
export class StateServiceProvider {
|
|
15
|
+
private readonly stateServiceStack: SimpleAsyncStateService[] = [];
|
|
16
|
+
|
|
17
|
+
public get stateService(): SimpleAsyncStateService {
|
|
18
|
+
if (this.stateServiceStack.length === 0) {
|
|
19
|
+
throw errors.stateServiceNotSet();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Assertion here is ok, because we check that the array is not empty above
|
|
23
|
+
return this.stateServiceStack.at(-1)!;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public setCurrentStateService(service: SimpleAsyncStateService) {
|
|
27
|
+
this.stateServiceStack.push(service);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public popCurrentStateService() {
|
|
31
|
+
if (this.stateServiceStack.length === 0) {
|
|
32
|
+
log.trace("Trying to pop from empty state-service stack");
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
this.stateServiceStack.pop();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Bool, Provable } from "o1js";
|
|
2
|
+
import { container } from "tsyringe";
|
|
3
|
+
import { log } from "@proto-kit/common";
|
|
4
|
+
|
|
5
|
+
import { RuntimeMethodExecutionContext } from "../context/RuntimeMethodExecutionContext";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Maintains an execution status of the current runtime module method,
|
|
9
|
+
* while prioritizing one-time failures. The assertion won't change the
|
|
10
|
+
* execution status if it has previously failed at least once within the
|
|
11
|
+
* same method execution context.
|
|
12
|
+
*
|
|
13
|
+
* @param condition - Result of the assertion made about the execution status
|
|
14
|
+
* @param message - Optional message describing the prior status
|
|
15
|
+
*/
|
|
16
|
+
export function assert(condition: Bool, message?: string | (() => string)) {
|
|
17
|
+
const executionContext = container.resolve(RuntimeMethodExecutionContext);
|
|
18
|
+
const previousStatus = executionContext.current().result.status;
|
|
19
|
+
const status = condition.and(previousStatus);
|
|
20
|
+
|
|
21
|
+
Provable.asProver(() => {
|
|
22
|
+
if (!condition.toBoolean()) {
|
|
23
|
+
if (!executionContext.current().isSimulated) {
|
|
24
|
+
log.debug("Assertion failed: ", message);
|
|
25
|
+
}
|
|
26
|
+
const messageString =
|
|
27
|
+
message !== undefined && typeof message === "function"
|
|
28
|
+
? message()
|
|
29
|
+
: message;
|
|
30
|
+
executionContext.setStatusMessage(messageString, new Error().stack);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
executionContext.setStatus(status);
|
|
35
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { Bool, FlexibleProvablePure, Provable, Struct } from "o1js";
|
|
2
|
+
import { singleton } from "tsyringe";
|
|
3
|
+
import {
|
|
4
|
+
ProvableMethodExecutionContext,
|
|
5
|
+
ProvableMethodExecutionResult,
|
|
6
|
+
} from "@proto-kit/common";
|
|
7
|
+
|
|
8
|
+
import { StateTransition } from "../../model/StateTransition";
|
|
9
|
+
import { RuntimeTransaction } from "../../model/transaction/RuntimeTransaction";
|
|
10
|
+
import { NetworkState } from "../../model/network/NetworkState";
|
|
11
|
+
|
|
12
|
+
const errors = {
|
|
13
|
+
setupNotCalled: () =>
|
|
14
|
+
new Error(
|
|
15
|
+
"Setup has not been called prior to executing a runtime method. Be sure to do that so that the Runtime is setup property for execution"
|
|
16
|
+
),
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export class RuntimeProvableMethodExecutionResult extends ProvableMethodExecutionResult {
|
|
20
|
+
public stateTransitions: StateTransition<any>[] = [];
|
|
21
|
+
|
|
22
|
+
public status: Bool = Bool(true);
|
|
23
|
+
|
|
24
|
+
public statusMessage?: string;
|
|
25
|
+
|
|
26
|
+
public stackTrace?: string;
|
|
27
|
+
|
|
28
|
+
public events: {
|
|
29
|
+
eventType: FlexibleProvablePure<any>;
|
|
30
|
+
event: any;
|
|
31
|
+
eventName: string;
|
|
32
|
+
condition: Bool;
|
|
33
|
+
}[] = [];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface RuntimeMethodExecutionData {
|
|
37
|
+
transaction: RuntimeTransaction;
|
|
38
|
+
networkState: NetworkState;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class RuntimeMethodExecutionDataStruct
|
|
42
|
+
extends Struct({
|
|
43
|
+
transaction: RuntimeTransaction,
|
|
44
|
+
networkState: NetworkState,
|
|
45
|
+
})
|
|
46
|
+
implements RuntimeMethodExecutionData {}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Execution context used to wrap runtime module methods,
|
|
50
|
+
* allowing them to post relevant information (such as execution status)
|
|
51
|
+
* into the context without any unnecessary 'prop drilling'.
|
|
52
|
+
*/
|
|
53
|
+
@singleton()
|
|
54
|
+
export class RuntimeMethodExecutionContext extends ProvableMethodExecutionContext {
|
|
55
|
+
public methods: string[] = [];
|
|
56
|
+
|
|
57
|
+
public input: RuntimeMethodExecutionData | undefined;
|
|
58
|
+
|
|
59
|
+
// The input corresponding to the current result
|
|
60
|
+
private lastInput: RuntimeMethodExecutionData | undefined;
|
|
61
|
+
|
|
62
|
+
public override result = new RuntimeProvableMethodExecutionResult();
|
|
63
|
+
|
|
64
|
+
private isSimulated: boolean = false;
|
|
65
|
+
|
|
66
|
+
private assertSetupCalled(): asserts this is {
|
|
67
|
+
input: RuntimeMethodExecutionData;
|
|
68
|
+
} {
|
|
69
|
+
if (this.input === undefined) {
|
|
70
|
+
throw errors.setupNotCalled();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Adds an in-method generated state transition to the current context
|
|
76
|
+
* @param stateTransition - State transition to add to the context
|
|
77
|
+
*/
|
|
78
|
+
public addStateTransition<Value>(stateTransition: StateTransition<Value>) {
|
|
79
|
+
this.assertSetupCalled();
|
|
80
|
+
this.result.stateTransitions.push(stateTransition);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public addEvent(
|
|
84
|
+
eventType: FlexibleProvablePure<any>,
|
|
85
|
+
event: any,
|
|
86
|
+
eventName: string,
|
|
87
|
+
condition: Bool = Bool(true)
|
|
88
|
+
) {
|
|
89
|
+
this.assertSetupCalled();
|
|
90
|
+
this.result.events.push({
|
|
91
|
+
eventType,
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
93
|
+
event,
|
|
94
|
+
eventName,
|
|
95
|
+
condition,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @param message - Status message to acompany the current status
|
|
101
|
+
*/
|
|
102
|
+
public setStatusMessage(message?: string, stackTrace?: string) {
|
|
103
|
+
this.assertSetupCalled();
|
|
104
|
+
if (this.isSimulated) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
this.result.statusMessage ??= message;
|
|
108
|
+
this.result.stackTrace ??= stackTrace;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @param status - Execution status of the current method
|
|
113
|
+
*/
|
|
114
|
+
public setStatus(status: Bool) {
|
|
115
|
+
this.assertSetupCalled();
|
|
116
|
+
if (this.isSimulated) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
this.result.status = status;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @param input Input witness data required for a runtime execution
|
|
124
|
+
*/
|
|
125
|
+
public setup(input: RuntimeMethodExecutionData) {
|
|
126
|
+
this.input = input;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public witnessInput(): RuntimeMethodExecutionDataStruct {
|
|
130
|
+
this.assertSetupCalled();
|
|
131
|
+
return Provable.witness(RuntimeMethodExecutionDataStruct, () => {
|
|
132
|
+
// TODO Is that right? Or this.current().input
|
|
133
|
+
const { transaction, networkState } = this.input!;
|
|
134
|
+
return new RuntimeMethodExecutionDataStruct({
|
|
135
|
+
networkState,
|
|
136
|
+
transaction,
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public setSimulated(simulated: boolean) {
|
|
142
|
+
this.isSimulated = simulated;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Manually clears/resets the execution context
|
|
147
|
+
*/
|
|
148
|
+
public clear() {
|
|
149
|
+
this.result = new RuntimeProvableMethodExecutionResult();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public afterMethod() {
|
|
153
|
+
super.afterMethod();
|
|
154
|
+
if (this.isFinished) {
|
|
155
|
+
this.lastInput = this.input;
|
|
156
|
+
// TODO: find out why input isnt set in TransactionFeeHook during assert
|
|
157
|
+
// this.input = undefined;
|
|
158
|
+
this.isSimulated = false;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Had to override current() otherwise it would not infer
|
|
164
|
+
* the type of result correctly (parent type would be reused)
|
|
165
|
+
*/
|
|
166
|
+
public current() {
|
|
167
|
+
return {
|
|
168
|
+
isFinished: this.isFinished,
|
|
169
|
+
result: this.result,
|
|
170
|
+
input: this.lastInput,
|
|
171
|
+
isSimulated: this.isSimulated,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { StateTransition } from "../../model/StateTransition";
|
|
2
|
+
|
|
3
|
+
export class TransitionMethodExecutionResult {
|
|
4
|
+
public stateTransitions: StateTransition<any>[] = [];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface TransitionMethodExecutionContext {
|
|
8
|
+
/**
|
|
9
|
+
* Adds an in-method generated state transition to the current context
|
|
10
|
+
* @param stateTransition - State transition to add to the context
|
|
11
|
+
*/
|
|
12
|
+
addStateTransition: <Value>(stateTransition: StateTransition<Value>) => void;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Manually clears/resets the execution context
|
|
16
|
+
*/
|
|
17
|
+
clear: () => void;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Had to override current() otherwise it would not infer
|
|
21
|
+
* the type of result correctly (parent type would be reused)
|
|
22
|
+
*/
|
|
23
|
+
current: () => {
|
|
24
|
+
result: TransitionMethodExecutionResult;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { State } from "../State";
|
|
2
|
+
import { Path } from "../../model/Path";
|
|
3
|
+
import { TransitioningProtocolModule } from "../../protocol/TransitioningProtocolModule";
|
|
4
|
+
|
|
5
|
+
const errors = {
|
|
6
|
+
missingName: (className: string) =>
|
|
7
|
+
new Error(
|
|
8
|
+
`Unable to provide a unique identifier for state, ${className} is missing a name.
|
|
9
|
+
Did you forget to extend your block module with 'extends ...Hook'?`
|
|
10
|
+
),
|
|
11
|
+
|
|
12
|
+
missingProtocol: (className: string) =>
|
|
13
|
+
new Error(
|
|
14
|
+
`Unable to provide 'procotol' for state, ${className} is missing a name.
|
|
15
|
+
Did you forget to extend your block module with 'extends ...Hook'?`
|
|
16
|
+
),
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Decorates a runtime module property as state, passing down some
|
|
21
|
+
* underlying values to improve developer experience.
|
|
22
|
+
*/
|
|
23
|
+
export function protocolState() {
|
|
24
|
+
return <
|
|
25
|
+
TargetTransitioningModule extends TransitioningProtocolModule<unknown>,
|
|
26
|
+
>(
|
|
27
|
+
target: TargetTransitioningModule,
|
|
28
|
+
propertyKey: string
|
|
29
|
+
) => {
|
|
30
|
+
let value: State<unknown> | undefined;
|
|
31
|
+
|
|
32
|
+
Object.defineProperty(target, propertyKey, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
|
|
35
|
+
get: function get() {
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
37
|
+
const self = this as TargetTransitioningModule;
|
|
38
|
+
|
|
39
|
+
if (self.name === undefined) {
|
|
40
|
+
throw errors.missingName(self.constructor.name);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!self.protocol) {
|
|
44
|
+
throw errors.missingProtocol(self.constructor.name);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// TODO Add Prefix?
|
|
48
|
+
const path = Path.fromProperty(self.name, propertyKey);
|
|
49
|
+
if (value) {
|
|
50
|
+
value.path = path;
|
|
51
|
+
value.stateServiceProvider = self.protocol.stateServiceProvider;
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
set: (newValue: State<unknown>) => {
|
|
57
|
+
value = newValue;
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
}
|