@proto-kit/module 0.1.1-develop.190 → 0.1.1-develop.211
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/dist/method/MethodParameterDecoder.d.ts.map +1 -1
- package/dist/method/MethodParameterDecoder.js +1 -1
- package/dist/method/RuntimeMethodExecutionContext.d.ts +2 -2
- package/dist/method/RuntimeMethodExecutionContext.d.ts.map +1 -1
- package/dist/module/src/factories/MethodIdFactory.d.ts +9 -0
- package/dist/module/src/factories/MethodIdFactory.d.ts.map +1 -0
- package/dist/module/src/factories/MethodIdFactory.js +36 -0
- package/dist/module/src/index.d.ts +11 -0
- package/dist/module/src/index.d.ts.map +1 -0
- package/dist/module/src/index.js +10 -0
- package/dist/module/src/method/MethodParameterDecoder.d.ts +22 -0
- package/dist/module/src/method/MethodParameterDecoder.d.ts.map +1 -0
- package/dist/module/src/method/MethodParameterDecoder.js +33 -0
- package/dist/module/src/method/runtimeMethod.d.ts +19 -0
- package/dist/module/src/method/runtimeMethod.d.ts.map +1 -0
- package/dist/module/src/method/runtimeMethod.js +123 -0
- package/dist/module/src/module/decorator.d.ts +8 -0
- package/dist/module/src/module/decorator.d.ts.map +1 -0
- package/dist/module/src/module/decorator.js +15 -0
- package/dist/module/src/runtime/MethodIdResolver.d.ts +18 -0
- package/dist/module/src/runtime/MethodIdResolver.d.ts.map +1 -0
- package/dist/module/src/runtime/MethodIdResolver.js +50 -0
- package/dist/module/src/runtime/Runtime.d.ts +72 -0
- package/dist/module/src/runtime/Runtime.d.ts.map +1 -0
- package/dist/module/src/runtime/Runtime.js +184 -0
- package/dist/module/src/runtime/RuntimeModule.d.ts +35 -0
- package/dist/module/src/runtime/RuntimeModule.d.ts.map +1 -0
- package/dist/module/src/runtime/RuntimeModule.js +56 -0
- package/dist/module/src/state/InMemoryStateService.d.ts +11 -0
- package/dist/module/src/state/InMemoryStateService.d.ts.map +1 -0
- package/dist/module/src/state/InMemoryStateService.js +21 -0
- package/dist/module/src/state/decorator.d.ts +7 -0
- package/dist/module/src/state/decorator.d.ts.map +1 -0
- package/dist/module/src/state/decorator.js +42 -0
- package/dist/protocol/src/model/Option.d.ts +98 -0
- package/dist/protocol/src/model/Option.d.ts.map +1 -0
- package/dist/protocol/src/model/Option.js +98 -0
- package/dist/protocol/src/model/StateTransition.d.ts +96 -0
- package/dist/protocol/src/model/StateTransition.d.ts.map +1 -0
- package/dist/protocol/src/model/StateTransition.js +65 -0
- package/dist/protocol/src/model/network/NetworkState.d.ts +64 -0
- package/dist/protocol/src/model/network/NetworkState.d.ts.map +1 -0
- package/dist/protocol/src/model/network/NetworkState.js +12 -0
- package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts +70 -0
- package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts.map +1 -0
- package/dist/protocol/src/model/transaction/ProtocolTransaction.js +18 -0
- package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts +63 -0
- package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts.map +1 -0
- package/dist/protocol/src/model/transaction/RuntimeTransaction.js +29 -0
- package/dist/protocol/src/state/assert/assert.d.ts +12 -0
- package/dist/protocol/src/state/assert/assert.d.ts.map +1 -0
- package/dist/protocol/src/state/assert/assert.js +23 -0
- package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts +60 -0
- package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts.map +1 -0
- package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.js +105 -0
- package/dist/state/StateMap.d.ts +3 -3
- package/dist/state/StateMap.d.ts.map +1 -1
- package/package.json +3 -4
- package/src/index.ts +2 -5
- package/src/method/MethodParameterDecoder.ts +3 -1
- package/src/method/runtimeMethod.ts +1 -2
- package/src/runtime/Runtime.ts +12 -10
- package/src/runtime/RuntimeModule.ts +5 -4
- package/src/state/InMemoryStateService.ts +1 -5
- package/src/state/decorator.ts +2 -4
- package/test/Runtime.test.ts +2 -2
- package/test/modules/Admin.ts +1 -1
- package/test/modules/Balances.test.ts +13 -8
- package/test/modules/Balances.ts +7 -3
- package/test/modules/State.test.ts +88 -0
- package/test/modules/methodId.test.ts +0 -1
- package/test/transaction.test.ts +1 -1
- package/src/method/RuntimeMethodExecutionContext.ts +0 -111
- package/src/method/assert.test.ts +0 -49
- package/src/method/assert.ts +0 -27
- package/src/state/State.ts +0 -154
- package/src/state/StateMap.ts +0 -69
- package/src/state/StateServiceProvider.ts +0 -24
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Field, Struct } from "snarkyjs";
|
|
2
|
+
import { Option, ProvableOption } from "./Option.js";
|
|
3
|
+
/**
|
|
4
|
+
* Provable representation of a State Transition, used to
|
|
5
|
+
* normalize state transitions of various value types for
|
|
6
|
+
* the state transition circuit.
|
|
7
|
+
*/
|
|
8
|
+
export class ProvableStateTransition extends Struct({
|
|
9
|
+
path: Field,
|
|
10
|
+
// must be applied even if `None`
|
|
11
|
+
from: ProvableOption,
|
|
12
|
+
// must be ignored if `None`
|
|
13
|
+
to: ProvableOption,
|
|
14
|
+
}) {
|
|
15
|
+
static dummy() {
|
|
16
|
+
return new ProvableStateTransition({
|
|
17
|
+
path: Field(0),
|
|
18
|
+
from: Option.none().toProvable(),
|
|
19
|
+
to: Option.none().toProvable(),
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Generic state transition that constraints the current method circuit
|
|
25
|
+
* to external state, by providing a state anchor.
|
|
26
|
+
*/
|
|
27
|
+
export class StateTransition {
|
|
28
|
+
static from(path, fromValue) {
|
|
29
|
+
return new StateTransition(path, fromValue, Option.none());
|
|
30
|
+
}
|
|
31
|
+
static fromTo(path, fromValue, toValue) {
|
|
32
|
+
return new StateTransition(path, fromValue, toValue);
|
|
33
|
+
}
|
|
34
|
+
constructor(path, fromValue, toValue) {
|
|
35
|
+
this.path = path;
|
|
36
|
+
this.fromValue = fromValue;
|
|
37
|
+
this.toValue = toValue;
|
|
38
|
+
}
|
|
39
|
+
get from() {
|
|
40
|
+
const from = this.fromValue.clone();
|
|
41
|
+
from.forceSome();
|
|
42
|
+
return from;
|
|
43
|
+
}
|
|
44
|
+
get to() {
|
|
45
|
+
return this.toValue.clone();
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Converts a StateTransition to a ProvableStateTransition,
|
|
49
|
+
* while enforcing the 'from' property to be 'Some' in all cases.
|
|
50
|
+
*/
|
|
51
|
+
toProvable() {
|
|
52
|
+
return new ProvableStateTransition({
|
|
53
|
+
path: this.path,
|
|
54
|
+
from: this.from.toProvable(),
|
|
55
|
+
to: this.to.toProvable(),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
toJSON() {
|
|
59
|
+
return {
|
|
60
|
+
path: this.path.toString(),
|
|
61
|
+
from: this.from.toJSON(),
|
|
62
|
+
to: this.to.toJSON(),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Field, UInt64 } from "snarkyjs";
|
|
2
|
+
declare const CurrentBlock_base: (new (value: {
|
|
3
|
+
height: UInt64;
|
|
4
|
+
}) => {
|
|
5
|
+
height: UInt64;
|
|
6
|
+
}) & {
|
|
7
|
+
_isStruct: true;
|
|
8
|
+
} & import("snarkyjs/dist/node/snarky").ProvablePure<{
|
|
9
|
+
height: UInt64;
|
|
10
|
+
}> & {
|
|
11
|
+
toInput: (x: {
|
|
12
|
+
height: UInt64;
|
|
13
|
+
}) => {
|
|
14
|
+
fields?: import("snarkyjs/dist/node/lib/field").Field[] | undefined;
|
|
15
|
+
packed?: [import("snarkyjs/dist/node/lib/field").Field, number][] | undefined;
|
|
16
|
+
};
|
|
17
|
+
toJSON: (x: {
|
|
18
|
+
height: UInt64;
|
|
19
|
+
}) => {
|
|
20
|
+
height: string;
|
|
21
|
+
};
|
|
22
|
+
fromJSON: (x: {
|
|
23
|
+
height: string;
|
|
24
|
+
}) => {
|
|
25
|
+
height: UInt64;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export declare class CurrentBlock extends CurrentBlock_base {
|
|
29
|
+
}
|
|
30
|
+
declare const NetworkState_base: (new (value: {
|
|
31
|
+
block: CurrentBlock;
|
|
32
|
+
}) => {
|
|
33
|
+
block: CurrentBlock;
|
|
34
|
+
}) & {
|
|
35
|
+
_isStruct: true;
|
|
36
|
+
} & import("snarkyjs/dist/node/snarky").ProvablePure<{
|
|
37
|
+
block: CurrentBlock;
|
|
38
|
+
}> & {
|
|
39
|
+
toInput: (x: {
|
|
40
|
+
block: CurrentBlock;
|
|
41
|
+
}) => {
|
|
42
|
+
fields?: import("snarkyjs/dist/node/lib/field").Field[] | undefined;
|
|
43
|
+
packed?: [import("snarkyjs/dist/node/lib/field").Field, number][] | undefined;
|
|
44
|
+
};
|
|
45
|
+
toJSON: (x: {
|
|
46
|
+
block: CurrentBlock;
|
|
47
|
+
}) => {
|
|
48
|
+
block: {
|
|
49
|
+
height: string;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
fromJSON: (x: {
|
|
53
|
+
block: {
|
|
54
|
+
height: string;
|
|
55
|
+
};
|
|
56
|
+
}) => {
|
|
57
|
+
block: CurrentBlock;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
export declare class NetworkState extends NetworkState_base {
|
|
61
|
+
hash(): Field;
|
|
62
|
+
}
|
|
63
|
+
export {};
|
|
64
|
+
//# sourceMappingURL=NetworkState.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NetworkState.d.ts","sourceRoot":"","sources":["../../../../../../protocol/src/model/network/NetworkState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAoB,MAAM,EAAE,MAAM,UAAU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3D,qBAAa,YAAa,SAAQ,iBAEhC;CAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEL,qBAAa,YAAa,SAAQ,iBAEhC;IACO,IAAI,IAAI,KAAK;CAGrB"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Poseidon, Struct, UInt64 } from "snarkyjs";
|
|
2
|
+
export class CurrentBlock extends Struct({
|
|
3
|
+
height: UInt64,
|
|
4
|
+
}) {
|
|
5
|
+
}
|
|
6
|
+
export class NetworkState extends Struct({
|
|
7
|
+
block: CurrentBlock,
|
|
8
|
+
}) {
|
|
9
|
+
hash() {
|
|
10
|
+
return Poseidon.hash(CurrentBlock.toFields(this.block));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Bool, Field, PublicKey, Signature, UInt64 } from "snarkyjs";
|
|
2
|
+
declare const ProtocolTransaction_base: (new (value: {
|
|
3
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
4
|
+
nonce: UInt64;
|
|
5
|
+
sender: PublicKey;
|
|
6
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
7
|
+
signature: Signature;
|
|
8
|
+
}) => {
|
|
9
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
10
|
+
nonce: UInt64;
|
|
11
|
+
sender: PublicKey;
|
|
12
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
13
|
+
signature: Signature;
|
|
14
|
+
}) & {
|
|
15
|
+
_isStruct: true;
|
|
16
|
+
} & import("snarkyjs/dist/node/snarky").ProvablePure<{
|
|
17
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
18
|
+
nonce: UInt64;
|
|
19
|
+
sender: PublicKey;
|
|
20
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
21
|
+
signature: Signature;
|
|
22
|
+
}> & {
|
|
23
|
+
toInput: (x: {
|
|
24
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
25
|
+
nonce: UInt64;
|
|
26
|
+
sender: PublicKey;
|
|
27
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
28
|
+
signature: Signature;
|
|
29
|
+
}) => {
|
|
30
|
+
fields?: import("snarkyjs/dist/node/lib/field").Field[] | undefined;
|
|
31
|
+
packed?: [import("snarkyjs/dist/node/lib/field").Field, number][] | undefined;
|
|
32
|
+
};
|
|
33
|
+
toJSON: (x: {
|
|
34
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
35
|
+
nonce: UInt64;
|
|
36
|
+
sender: PublicKey;
|
|
37
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
38
|
+
signature: Signature;
|
|
39
|
+
}) => {
|
|
40
|
+
methodId: string;
|
|
41
|
+
nonce: string;
|
|
42
|
+
sender: string;
|
|
43
|
+
argsHash: string;
|
|
44
|
+
signature: any;
|
|
45
|
+
};
|
|
46
|
+
fromJSON: (x: {
|
|
47
|
+
methodId: string;
|
|
48
|
+
nonce: string;
|
|
49
|
+
sender: string;
|
|
50
|
+
argsHash: string;
|
|
51
|
+
signature: any;
|
|
52
|
+
}) => {
|
|
53
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
54
|
+
nonce: UInt64;
|
|
55
|
+
sender: PublicKey;
|
|
56
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
57
|
+
signature: Signature;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
export declare class ProtocolTransaction extends ProtocolTransaction_base {
|
|
61
|
+
static getSignatureData(args: {
|
|
62
|
+
methodId: Field;
|
|
63
|
+
nonce: UInt64;
|
|
64
|
+
argsHash: Field;
|
|
65
|
+
}): Field[];
|
|
66
|
+
getSignatureData(): Field[];
|
|
67
|
+
validateSignature(): Bool;
|
|
68
|
+
}
|
|
69
|
+
export {};
|
|
70
|
+
//# sourceMappingURL=ProtocolTransaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProtocolTransaction.d.ts","sourceRoot":"","sources":["../../../../../../protocol/src/model/transaction/ProtocolTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAU,MAAM,EAAE,MAAM,UAAU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE7E,qBAAa,mBAAoB,SAAQ,wBAMvC;WACc,gBAAgB,CAAC,IAAI,EAAE;QACnC,QAAQ,EAAE,KAAK,CAAC;QAChB,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,KAAK,CAAC;KACjB,GAAG,KAAK,EAAE;IAIJ,gBAAgB,IAAI,KAAK,EAAE;IAI3B,iBAAiB,IAAI,IAAI;CAGjC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Field, PublicKey, Signature, Struct, UInt64 } from "snarkyjs";
|
|
2
|
+
export class ProtocolTransaction extends Struct({
|
|
3
|
+
methodId: Field,
|
|
4
|
+
nonce: UInt64,
|
|
5
|
+
sender: PublicKey,
|
|
6
|
+
argsHash: Field,
|
|
7
|
+
signature: Signature,
|
|
8
|
+
}) {
|
|
9
|
+
static getSignatureData(args) {
|
|
10
|
+
return [args.methodId, ...args.nonce.toFields(), args.argsHash];
|
|
11
|
+
}
|
|
12
|
+
getSignatureData() {
|
|
13
|
+
return ProtocolTransaction.getSignatureData(this);
|
|
14
|
+
}
|
|
15
|
+
validateSignature() {
|
|
16
|
+
return this.signature.verify(this.sender, this.getSignatureData());
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Field, PublicKey, UInt64 } from "snarkyjs";
|
|
2
|
+
import { ProtocolTransaction } from "./ProtocolTransaction";
|
|
3
|
+
declare const RuntimeTransaction_base: (new (value: {
|
|
4
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
5
|
+
nonce: UInt64;
|
|
6
|
+
sender: PublicKey;
|
|
7
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
8
|
+
}) => {
|
|
9
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
10
|
+
nonce: UInt64;
|
|
11
|
+
sender: PublicKey;
|
|
12
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
13
|
+
}) & {
|
|
14
|
+
_isStruct: true;
|
|
15
|
+
} & import("snarkyjs/dist/node/snarky").ProvablePure<{
|
|
16
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
17
|
+
nonce: UInt64;
|
|
18
|
+
sender: PublicKey;
|
|
19
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
20
|
+
}> & {
|
|
21
|
+
toInput: (x: {
|
|
22
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
23
|
+
nonce: UInt64;
|
|
24
|
+
sender: PublicKey;
|
|
25
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
26
|
+
}) => {
|
|
27
|
+
fields?: import("snarkyjs/dist/node/lib/field").Field[] | undefined;
|
|
28
|
+
packed?: [import("snarkyjs/dist/node/lib/field").Field, number][] | undefined;
|
|
29
|
+
};
|
|
30
|
+
toJSON: (x: {
|
|
31
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
32
|
+
nonce: UInt64;
|
|
33
|
+
sender: PublicKey;
|
|
34
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
35
|
+
}) => {
|
|
36
|
+
methodId: string;
|
|
37
|
+
nonce: string;
|
|
38
|
+
sender: string;
|
|
39
|
+
argsHash: string;
|
|
40
|
+
};
|
|
41
|
+
fromJSON: (x: {
|
|
42
|
+
methodId: string;
|
|
43
|
+
nonce: string;
|
|
44
|
+
sender: string;
|
|
45
|
+
argsHash: string;
|
|
46
|
+
}) => {
|
|
47
|
+
methodId: import("snarkyjs/dist/node/lib/field").Field;
|
|
48
|
+
nonce: UInt64;
|
|
49
|
+
sender: PublicKey;
|
|
50
|
+
argsHash: import("snarkyjs/dist/node/lib/field").Field;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* This struct is used to expose transaction information to the runtime method
|
|
55
|
+
* execution. This class has not all data included in transactions on purpose.
|
|
56
|
+
* For example, we don't want to expose the signature or args as fields.
|
|
57
|
+
*/
|
|
58
|
+
export declare class RuntimeTransaction extends RuntimeTransaction_base {
|
|
59
|
+
static fromProtocolTransaction({ methodId, nonce, sender, argsHash, }: ProtocolTransaction): RuntimeTransaction;
|
|
60
|
+
hash(): Field;
|
|
61
|
+
}
|
|
62
|
+
export {};
|
|
63
|
+
//# sourceMappingURL=RuntimeTransaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RuntimeTransaction.d.ts","sourceRoot":"","sources":["../../../../../../protocol/src/model/transaction/RuntimeTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAY,SAAS,EAAU,MAAM,EAAE,MAAM,UAAU,CAAC;AAEtE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE5D;;;;GAIG;AACH,qBAAa,kBAAmB,SAAQ,uBAKtC;WACc,uBAAuB,CAAC,EACpC,QAAQ,EACR,KAAK,EACL,MAAM,EACN,QAAQ,GACT,EAAE,mBAAmB,GAAG,kBAAkB;IASpC,IAAI,IAAI,KAAK;CAQrB"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Field, Poseidon, PublicKey, Struct, UInt64 } from "snarkyjs";
|
|
2
|
+
/**
|
|
3
|
+
* This struct is used to expose transaction information to the runtime method
|
|
4
|
+
* execution. This class has not all data included in transactions on purpose.
|
|
5
|
+
* For example, we don't want to expose the signature or args as fields.
|
|
6
|
+
*/
|
|
7
|
+
export class RuntimeTransaction extends Struct({
|
|
8
|
+
methodId: Field,
|
|
9
|
+
nonce: UInt64,
|
|
10
|
+
sender: PublicKey,
|
|
11
|
+
argsHash: Field,
|
|
12
|
+
}) {
|
|
13
|
+
static fromProtocolTransaction({ methodId, nonce, sender, argsHash, }) {
|
|
14
|
+
return new RuntimeTransaction({
|
|
15
|
+
methodId,
|
|
16
|
+
nonce,
|
|
17
|
+
sender,
|
|
18
|
+
argsHash,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
hash() {
|
|
22
|
+
return Poseidon.hash([
|
|
23
|
+
this.methodId,
|
|
24
|
+
...this.sender.toFields(),
|
|
25
|
+
...this.nonce.toFields(),
|
|
26
|
+
this.argsHash,
|
|
27
|
+
]);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Bool } from "snarkyjs";
|
|
2
|
+
/**
|
|
3
|
+
* Maintains an execution status of the current runtime module method,
|
|
4
|
+
* while prioritizing one-time failures. The assertion won't change the
|
|
5
|
+
* execution status if it has previously failed at least once within the
|
|
6
|
+
* same method execution context.
|
|
7
|
+
*
|
|
8
|
+
* @param condition - Result of the assertion made about the execution status
|
|
9
|
+
* @param message - Optional message describing the prior status
|
|
10
|
+
*/
|
|
11
|
+
export declare function assert(condition: Bool, message?: string): void;
|
|
12
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../../../../../protocol/src/state/assert/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAY,MAAM,UAAU,CAAC;AAO1C;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,QAWvD"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Bool, Provable } from "snarkyjs";
|
|
2
|
+
import { container } from "tsyringe";
|
|
3
|
+
import { log } from "@proto-kit/common";
|
|
4
|
+
import { RuntimeMethodExecutionContext } from "../context/RuntimeMethodExecutionContext";
|
|
5
|
+
/**
|
|
6
|
+
* Maintains an execution status of the current runtime module method,
|
|
7
|
+
* while prioritizing one-time failures. The assertion won't change the
|
|
8
|
+
* execution status if it has previously failed at least once within the
|
|
9
|
+
* same method execution context.
|
|
10
|
+
*
|
|
11
|
+
* @param condition - Result of the assertion made about the execution status
|
|
12
|
+
* @param message - Optional message describing the prior status
|
|
13
|
+
*/
|
|
14
|
+
export function assert(condition, message) {
|
|
15
|
+
const executionContext = container.resolve(RuntimeMethodExecutionContext);
|
|
16
|
+
const previousStatus = executionContext.current().result.status;
|
|
17
|
+
const status = Provable.if(previousStatus, Bool, condition, previousStatus);
|
|
18
|
+
if (!condition.toBoolean()) {
|
|
19
|
+
log.debug("Assertion failed: ", message);
|
|
20
|
+
executionContext.setStatusMessage(message);
|
|
21
|
+
}
|
|
22
|
+
executionContext.setStatus(status);
|
|
23
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Bool } from "snarkyjs";
|
|
2
|
+
import { ProvableMethodExecutionContext, ProvableMethodExecutionResult } from "@proto-kit/common";
|
|
3
|
+
import { StateTransition } from "../../model/StateTransition";
|
|
4
|
+
import { RuntimeTransaction } from "../../model/transaction/RuntimeTransaction";
|
|
5
|
+
import { NetworkState } from "../../model/network/NetworkState";
|
|
6
|
+
export declare class RuntimeProvableMethodExecutionResult extends ProvableMethodExecutionResult {
|
|
7
|
+
stateTransitions: StateTransition<any>[];
|
|
8
|
+
status: Bool;
|
|
9
|
+
statusMessage?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface RuntimeMethodExecutionData {
|
|
12
|
+
transaction: RuntimeTransaction;
|
|
13
|
+
networkState: NetworkState;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Execution context used to wrap runtime module methods,
|
|
17
|
+
* allowing them to post relevant information (such as execution status)
|
|
18
|
+
* into the context without any unnecessary 'prop drilling'.
|
|
19
|
+
*/
|
|
20
|
+
export declare class RuntimeMethodExecutionContext extends ProvableMethodExecutionContext {
|
|
21
|
+
methods: string[];
|
|
22
|
+
input: RuntimeMethodExecutionData | undefined;
|
|
23
|
+
private lastInput;
|
|
24
|
+
result: RuntimeProvableMethodExecutionResult;
|
|
25
|
+
private isSimulated;
|
|
26
|
+
private assertSetupCalled;
|
|
27
|
+
/**
|
|
28
|
+
* Adds an in-method generated state transition to the current context
|
|
29
|
+
* @param stateTransition - State transition to add to the context
|
|
30
|
+
*/
|
|
31
|
+
addStateTransition<Value>(stateTransition: StateTransition<Value>): void;
|
|
32
|
+
/**
|
|
33
|
+
* @param message - Status message to acompany the current status
|
|
34
|
+
*/
|
|
35
|
+
setStatusMessage(message?: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* @param status - Execution status of the current method
|
|
38
|
+
*/
|
|
39
|
+
setStatus(status: Bool): void;
|
|
40
|
+
/**
|
|
41
|
+
* @param input Input witness data required for a runtime execution
|
|
42
|
+
*/
|
|
43
|
+
setup(input: RuntimeMethodExecutionData): void;
|
|
44
|
+
setSimulated(simulated: boolean): void;
|
|
45
|
+
/**
|
|
46
|
+
* Manually clears/resets the execution context
|
|
47
|
+
*/
|
|
48
|
+
clear(): void;
|
|
49
|
+
afterMethod(): void;
|
|
50
|
+
/**
|
|
51
|
+
* Had to override current() otherwise it would not infer
|
|
52
|
+
* the type of result correctly (parent type would be reused)
|
|
53
|
+
*/
|
|
54
|
+
current(): {
|
|
55
|
+
isFinished: boolean;
|
|
56
|
+
result: RuntimeProvableMethodExecutionResult;
|
|
57
|
+
input: RuntimeMethodExecutionData | undefined;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=RuntimeMethodExecutionContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RuntimeMethodExecutionContext.d.ts","sourceRoot":"","sources":["../../../../../../protocol/src/state/context/RuntimeMethodExecutionContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAEhC,OAAO,EAAE,8BAA8B,EAAE,6BAA6B,EAAE,MAAM,mBAAmB,CAAC;AAClG,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4CAA4C,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAShE,qBAAa,oCAAqC,SAAQ,6BAA6B;IAE9E,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,CAAM;IAE9C,MAAM,EAAE,IAAI,CAAc;IAE1B,aAAa,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,0BAA0B;IACzC,WAAW,EAAE,kBAAkB,CAAC;IAChC,YAAY,EAAE,YAAY,CAAC;CAC5B;AAED;;;;GAIG;AACH,qBACa,6BAA8B,SAAQ,8BAA8B;IACxE,OAAO,EAAE,MAAM,EAAE,CAAM;IAEvB,KAAK,EAAE,0BAA0B,GAAG,SAAS,CAAC;IAGrD,OAAO,CAAC,SAAS,CAAyC;IAE1C,MAAM,uCAA8C;IAEpE,OAAO,CAAC,WAAW,CAAkB;IAErC,OAAO,CAAC,iBAAiB;IAQzB;;;OAGG;IACI,kBAAkB,CAAC,KAAK,EAAE,eAAe,EAAE,eAAe,CAAC,KAAK,CAAC;IAKxE;;OAEG;IACI,gBAAgB,CAAC,OAAO,CAAC,EAAE,MAAM;IAQxC;;OAEG;IACI,SAAS,CAAC,MAAM,EAAE,IAAI;IAQ7B;;OAEG;IACI,KAAK,CAAC,KAAK,EAAE,0BAA0B;IAIvC,YAAY,CAAC,SAAS,EAAE,OAAO;IAItC;;OAEG;IACI,KAAK;IAIL,WAAW;IASlB;;;OAGG;IACI,OAAO;;;;;CAOf"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { Bool } from "snarkyjs";
|
|
8
|
+
import { singleton } from "tsyringe";
|
|
9
|
+
import { ProvableMethodExecutionContext, ProvableMethodExecutionResult } from "@proto-kit/common";
|
|
10
|
+
const errors = {
|
|
11
|
+
setupNotCalled: () => new Error("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"),
|
|
12
|
+
};
|
|
13
|
+
export class RuntimeProvableMethodExecutionResult extends ProvableMethodExecutionResult {
|
|
14
|
+
constructor() {
|
|
15
|
+
super(...arguments);
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
+
this.stateTransitions = [];
|
|
18
|
+
this.status = Bool(true);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Execution context used to wrap runtime module methods,
|
|
23
|
+
* allowing them to post relevant information (such as execution status)
|
|
24
|
+
* into the context without any unnecessary 'prop drilling'.
|
|
25
|
+
*/
|
|
26
|
+
let RuntimeMethodExecutionContext = class RuntimeMethodExecutionContext extends ProvableMethodExecutionContext {
|
|
27
|
+
constructor() {
|
|
28
|
+
super(...arguments);
|
|
29
|
+
this.methods = [];
|
|
30
|
+
this.result = new RuntimeProvableMethodExecutionResult();
|
|
31
|
+
this.isSimulated = false;
|
|
32
|
+
}
|
|
33
|
+
assertSetupCalled() {
|
|
34
|
+
if (this.input === undefined) {
|
|
35
|
+
throw errors.setupNotCalled();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Adds an in-method generated state transition to the current context
|
|
40
|
+
* @param stateTransition - State transition to add to the context
|
|
41
|
+
*/
|
|
42
|
+
addStateTransition(stateTransition) {
|
|
43
|
+
this.assertSetupCalled();
|
|
44
|
+
this.result.stateTransitions.push(stateTransition);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @param message - Status message to acompany the current status
|
|
48
|
+
*/
|
|
49
|
+
setStatusMessage(message) {
|
|
50
|
+
var _a;
|
|
51
|
+
this.assertSetupCalled();
|
|
52
|
+
if (this.isSimulated) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
(_a = this.result).statusMessage ?? (_a.statusMessage = message);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @param status - Execution status of the current method
|
|
59
|
+
*/
|
|
60
|
+
setStatus(status) {
|
|
61
|
+
this.assertSetupCalled();
|
|
62
|
+
if (this.isSimulated) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
this.result.status = status;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* @param input Input witness data required for a runtime execution
|
|
69
|
+
*/
|
|
70
|
+
setup(input) {
|
|
71
|
+
this.input = input;
|
|
72
|
+
}
|
|
73
|
+
setSimulated(simulated) {
|
|
74
|
+
this.isSimulated = simulated;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Manually clears/resets the execution context
|
|
78
|
+
*/
|
|
79
|
+
clear() {
|
|
80
|
+
this.result = new RuntimeProvableMethodExecutionResult();
|
|
81
|
+
}
|
|
82
|
+
afterMethod() {
|
|
83
|
+
super.afterMethod();
|
|
84
|
+
if (this.isFinished) {
|
|
85
|
+
this.lastInput = this.input;
|
|
86
|
+
this.input = undefined;
|
|
87
|
+
this.isSimulated = false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Had to override current() otherwise it would not infer
|
|
92
|
+
* the type of result correctly (parent type would be reused)
|
|
93
|
+
*/
|
|
94
|
+
current() {
|
|
95
|
+
return {
|
|
96
|
+
isFinished: this.isFinished,
|
|
97
|
+
result: this.result,
|
|
98
|
+
input: this.lastInput,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
RuntimeMethodExecutionContext = __decorate([
|
|
103
|
+
singleton()
|
|
104
|
+
], RuntimeMethodExecutionContext);
|
|
105
|
+
export { RuntimeMethodExecutionContext };
|
package/dist/state/StateMap.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { Field, FlexibleProvablePure } from "snarkyjs";
|
|
2
|
-
import { type Option } from "@proto-kit/protocol";
|
|
2
|
+
import { type Option, ToFieldable } from "@proto-kit/protocol";
|
|
3
3
|
import { WithRuntime, WithPath } from "./State.js";
|
|
4
4
|
declare const StateMap_base: import("ts-mixer/dist/types/types.js").Class<any[], WithPath & WithRuntime, typeof WithPath & typeof WithRuntime, false>;
|
|
5
5
|
/**
|
|
6
6
|
* Map-like wrapper for state
|
|
7
7
|
*/
|
|
8
|
-
export declare class StateMap<KeyType, ValueType> extends StateMap_base {
|
|
8
|
+
export declare class StateMap<KeyType, ValueType extends ToFieldable> extends StateMap_base {
|
|
9
9
|
keyType: FlexibleProvablePure<KeyType>;
|
|
10
10
|
valueType: FlexibleProvablePure<ValueType>;
|
|
11
11
|
/**
|
|
@@ -15,7 +15,7 @@ export declare class StateMap<KeyType, ValueType> extends StateMap_base {
|
|
|
15
15
|
* @param valueType - Type to be stored as a value
|
|
16
16
|
* @returns State map with provided key and value types.
|
|
17
17
|
*/
|
|
18
|
-
static from<KeyType, ValueType>(keyType: FlexibleProvablePure<KeyType>, valueType: FlexibleProvablePure<ValueType>): StateMap<KeyType, ValueType>;
|
|
18
|
+
static from<KeyType, ValueType extends ToFieldable>(keyType: FlexibleProvablePure<KeyType>, valueType: FlexibleProvablePure<ValueType>): StateMap<KeyType, ValueType>;
|
|
19
19
|
constructor(keyType: FlexibleProvablePure<KeyType>, valueType: FlexibleProvablePure<ValueType>);
|
|
20
20
|
getPath(key: KeyType): Field;
|
|
21
21
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StateMap.d.ts","sourceRoot":"","sources":["../../src/state/StateMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAC5D,OAAO,EAAQ,KAAK,MAAM,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"StateMap.d.ts","sourceRoot":"","sources":["../../src/state/StateMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAC5D,OAAO,EAAQ,KAAK,MAAM,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGrE,OAAO,EAAS,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;;AAE1D;;GAEG;AAEH,qBAAa,QAAQ,CAAC,OAAO,EAAE,SAAS,SAAS,WAAW,CAAE,SAAQ,aAA4B;IAgBvF,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC;IACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;IAhBnD;;;;;;OAMG;WACW,IAAI,CAAC,OAAO,EAAE,SAAS,SAAS,WAAW,EACvD,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,EACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;gBAMnC,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,EACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;IAK5C,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,KAAK;IAKnC;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC;IAU3C;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS;CAS1C"}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@proto-kit/module",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"private": false,
|
|
5
|
-
"version": "0.1.1-develop.
|
|
5
|
+
"version": "0.1.1-develop.211+662735c",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc -p tsconfig.json",
|
|
@@ -21,8 +21,7 @@
|
|
|
21
21
|
"@proto-kit/protocol": "*",
|
|
22
22
|
"lodash": "^4.17.21",
|
|
23
23
|
"loglevel": "^1.8.1",
|
|
24
|
-
"reflect-metadata": "^0.1.13"
|
|
25
|
-
"ts-mixer": "^6.0.3"
|
|
24
|
+
"reflect-metadata": "^0.1.13"
|
|
26
25
|
},
|
|
27
26
|
"devDependencies": {
|
|
28
27
|
"@jest/globals": "^29.5.0",
|
|
@@ -32,5 +31,5 @@
|
|
|
32
31
|
"snarkyjs": "0.12.0",
|
|
33
32
|
"tsyringe": "^4.7.0"
|
|
34
33
|
},
|
|
35
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "662735cd072170aa01a9f6040e89293a3e3fc8a9"
|
|
36
35
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
export * from "
|
|
2
|
-
export * from "./method/assert";
|
|
1
|
+
export * from "../../protocol/src/state/assert/assert";
|
|
3
2
|
export * from "./method/runtimeMethod";
|
|
4
3
|
export * from "./module/decorator";
|
|
5
4
|
export * from "./runtime/RuntimeModule";
|
|
6
5
|
export * from "./runtime/Runtime";
|
|
7
6
|
export * from "./state/InMemoryStateService";
|
|
8
|
-
export * from "
|
|
9
|
-
export * from "./state/StateMap";
|
|
10
|
-
export * from "./state/StateServiceProvider";
|
|
7
|
+
export * from "@proto-kit/protocol/src/state/StateServiceProvider";
|
|
11
8
|
export * from "./state/decorator";
|
|
12
9
|
export * from "./method/MethodParameterDecoder";
|
|
13
10
|
export * from "./runtime/MethodIdResolver";
|
|
@@ -45,7 +45,9 @@ export class MethodParameterDecoder {
|
|
|
45
45
|
if (fields.length < this.fieldSize) {
|
|
46
46
|
throw errors.fieldLengthNotMatching(this.fieldSize, fields.length);
|
|
47
47
|
}
|
|
48
|
+
|
|
48
49
|
let stack = fields.slice();
|
|
50
|
+
|
|
49
51
|
return this.types.map((type) => {
|
|
50
52
|
const numberFieldsNeeded =
|
|
51
53
|
type.prototype._fields?.length ?? type.sizeInFields?.() ?? -1;
|
|
@@ -63,6 +65,6 @@ export class MethodParameterDecoder {
|
|
|
63
65
|
.map(
|
|
64
66
|
(type) => type.prototype._fields?.length ?? type.sizeInFields?.() ?? 0
|
|
65
67
|
)
|
|
66
|
-
.reduce((a, b) => a + b);
|
|
68
|
+
.reduce((a, b) => a + b, 0);
|
|
67
69
|
}
|
|
68
70
|
}
|
|
@@ -5,13 +5,12 @@ import {
|
|
|
5
5
|
DefaultProvableHashList,
|
|
6
6
|
ProvableStateTransition,
|
|
7
7
|
MethodPublicOutput,
|
|
8
|
+
RuntimeMethodExecutionContext
|
|
8
9
|
} from "@proto-kit/protocol";
|
|
9
10
|
import { DecoratedMethod, toProver, ZkProgrammable } from "@proto-kit/common";
|
|
10
11
|
|
|
11
12
|
import type { RuntimeModule } from "../runtime/RuntimeModule.js";
|
|
12
13
|
|
|
13
|
-
import { RuntimeMethodExecutionContext } from "./RuntimeMethodExecutionContext.js";
|
|
14
|
-
|
|
15
14
|
const errors = {
|
|
16
15
|
runtimeNotProvided: (name: string) =>
|
|
17
16
|
new Error(`Runtime was not provided for module: ${name}`),
|