-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProvableMethodExecutionContext.ts
More file actions
127 lines (106 loc) · 3.41 KB
/
ProvableMethodExecutionContext.ts
File metadata and controls
127 lines (106 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import type { Proof } from "o1js";
import { singleton } from "tsyringe";
import uniqueId from "lodash/uniqueId";
import { log } from "../log";
import type { ArgumentTypes } from "./provableMethod";
const errors = {
moduleOrMethodNameNotSet: () => new Error("Module or method name not set"),
proverNotSet: (moduleName: string, methodName: string) =>
new Error(
`Prover not set for '${moduleName}.${methodName}', did you forget to decorate your method?`
),
};
export class ProvableMethodExecutionResult {
public moduleName?: string;
public methodName?: string;
public args?: ArgumentTypes;
public prover?: () => Promise<Proof<unknown, unknown>>;
public async prove<
ProofType extends Proof<unknown, unknown>,
>(): Promise<ProofType> {
if (!this.prover) {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!this.moduleName || !this.methodName) {
throw errors.moduleOrMethodNameNotSet();
}
throw errors.proverNotSet(this.moduleName, this.methodName);
}
log.debug("Proving work started");
// turn the prover result into the desired proof type
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const proof = (await this.prover()) as ProofType;
log.debug("Proving work ended");
return proof;
}
}
/**
* Execution context used to wrap runtime module methods,
* allowing them to post relevant information (such as execution status)
* into the context without any unnecessary 'prop drilling'.
*/
@singleton()
export class ProvableMethodExecutionContext {
public id = uniqueId();
public methods: string[] = [];
public result: ProvableMethodExecutionResult =
new ProvableMethodExecutionResult();
// TODO See if we should make this class generic, bc I think we can persist the type
/**
* Adds a method prover to the current execution context,
* which can be collected and ran asynchronously at a later point in time.
*
* @param prove - Prover function to be ran later,
* when the method execution needs to be proven
*/
public setProver(prover: () => Promise<Proof<unknown, unknown>>) {
this.result.prover = prover;
}
/**
* Adds a method to the method execution stack, reseting the execution context
* in a case a new top-level (non nested) method call is made.
*
* @param methodName - Name of the method being captured in the context
*/
public beforeMethod(
moduleName: string,
methodName: string,
args: ArgumentTypes
) {
if (this.isFinished) {
this.clear();
this.result.moduleName = moduleName;
this.result.methodName = methodName;
this.result.args = args;
}
this.methods.push(methodName);
}
/**
* Removes the latest method from the execution context stack,
* keeping track of the amount of 'unfinished' methods. Allowing
* for the context to distinguish between top-level and nested method calls.
*/
public afterMethod() {
this.methods.pop();
}
public get isTopLevel() {
return this.methods.length === 1;
}
public get isFinished() {
return this.methods.length === 0;
}
/**
* @returns - Current execution context state
*/
public current() {
return {
isFinished: this.isFinished,
result: this.result,
};
}
/**
* Manually clears/resets the execution context
*/
public clear() {
this.result = new ProvableMethodExecutionResult();
}
}