@proto-kit/module 0.1.1-develop.154 → 0.1.1-develop.160

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.
@@ -6,9 +6,11 @@ export interface Fieldable {
6
6
  export interface FromFieldClass {
7
7
  new: (...args: any[]) => any;
8
8
  fromFields: (fields: Field[]) => Fieldable;
9
+ name: string;
9
10
  prototype: {
10
- _fields: any[];
11
+ _fields?: any[];
11
12
  };
13
+ sizeInFields?: () => number;
12
14
  }
13
15
  export declare class MethodParameterDecoder {
14
16
  private readonly types;
@@ -1 +1 @@
1
- {"version":3,"file":"MethodParameterDecoder.d.ts","sourceRoot":"","sources":["../../src/method/MethodParameterDecoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD,MAAM,WAAW,SAAS;IACxB,QAAQ,EAAE,MAAM,KAAK,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;IAC7B,UAAU,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,SAAS,CAAC;IAE3C,SAAS,EAAE;QACT,OAAO,EAAE,GAAG,EAAE,CAAC;KAChB,CAAC;CACH;AAOD,qBAAa,sBAAsB;IAWb,OAAO,CAAC,QAAQ,CAAC,KAAK;WAV5B,UAAU,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,MAAM;IAU3E,OAAO;IAEA,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,EAAE;IAc/C,IAAW,SAAS,IAAI,MAAM,CAI7B;CACF"}
1
+ {"version":3,"file":"MethodParameterDecoder.d.ts","sourceRoot":"","sources":["../../src/method/MethodParameterDecoder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD,MAAM,WAAW,SAAS;IACxB,QAAQ,EAAE,MAAM,KAAK,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;IAC7B,UAAU,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,SAAS,CAAC;IAC3C,IAAI,EAAE,MAAM,CAAC;IAEb,SAAS,EAAE;QACT,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;KACjB,CAAC;IACF,YAAY,CAAC,EAAE,MAAM,MAAM,CAAC;CAC7B;AAYD,qBAAa,sBAAsB;IAWb,OAAO,CAAC,QAAQ,CAAC,KAAK;WAV5B,UAAU,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,MAAM;IAU3E,OAAO;IAEA,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,EAAE;IAiB/C,IAAW,SAAS,IAAI,MAAM,CAM7B;CACF"}
@@ -1,5 +1,6 @@
1
1
  const errors = {
2
2
  fieldLengthNotMatching: (expected, actual) => new Error(`Expected ${expected} field elements, got ${actual}`),
3
+ typeNotCompatible: (name) => new Error(`Cannot decode type ${name}, it has to be either a Struct, CircuitValue or built-in snarkyjs type`),
3
4
  };
4
5
  export class MethodParameterDecoder {
5
6
  static fromMethod(target, methodName) {
@@ -15,8 +16,10 @@ export class MethodParameterDecoder {
15
16
  }
16
17
  let stack = fields.slice();
17
18
  return this.types.map((type) => {
18
- // eslint-disable-next-line no-underscore-dangle
19
- const numberFieldsNeeded = type.prototype._fields.length;
19
+ const numberFieldsNeeded = type.prototype._fields?.length ?? type.sizeInFields?.() ?? -1;
20
+ if (numberFieldsNeeded === -1) {
21
+ throw errors.typeNotCompatible(type.name);
22
+ }
20
23
  const structFields = stack.slice(0, numberFieldsNeeded);
21
24
  stack = stack.slice(numberFieldsNeeded);
22
25
  return type.fromFields(structFields);
@@ -24,7 +27,7 @@ export class MethodParameterDecoder {
24
27
  }
25
28
  get fieldSize() {
26
29
  return this.types
27
- .map((type) => type.prototype._fields.length)
30
+ .map((type) => type.prototype._fields?.length ?? type.sizeInFields?.() ?? 0)
28
31
  .reduce((a, b) => a + b);
29
32
  }
30
33
  }
@@ -1 +1 @@
1
- {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/method/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAY,MAAM,UAAU,CAAC;AAK1C;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,QAQvD"}
1
+ {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/method/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAY,MAAM,UAAU,CAAC;AAM1C;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,QAYvD"}
@@ -1,6 +1,7 @@
1
1
  import { Bool, Provable } from "snarkyjs";
2
2
  import { container } from "tsyringe";
3
3
  import { RuntimeMethodExecutionContext } from "./RuntimeMethodExecutionContext";
4
+ import { log } from "@proto-kit/common";
4
5
  /**
5
6
  * Maintains an execution status of the current runtime module method,
6
7
  * while prioritizing one-time failures. The assertion won't change the
@@ -14,6 +15,9 @@ export function assert(condition, message) {
14
15
  const executionContext = container.resolve(RuntimeMethodExecutionContext);
15
16
  const previousStatus = executionContext.current().result.status;
16
17
  const status = Provable.if(previousStatus, Bool, condition, previousStatus);
18
+ if (!status.toBoolean()) {
19
+ log.debug("Assertion failed: ", message);
20
+ }
17
21
  // const status = previousStatus.and(condition);
18
22
  executionContext.setStatus(status);
19
23
  executionContext.setStatusMessage(message);
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.154+8ce23c3",
5
+ "version": "0.1.1-develop.160+5120680",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "build": "tsc -p tsconfig.json",
@@ -17,8 +17,8 @@
17
17
  "access": "public"
18
18
  },
19
19
  "dependencies": {
20
- "@proto-kit/common": "0.1.1-develop.154+8ce23c3",
21
- "@proto-kit/protocol": "0.1.1-develop.154+8ce23c3",
20
+ "@proto-kit/common": "0.1.1-develop.160+5120680",
21
+ "@proto-kit/protocol": "0.1.1-develop.160+5120680",
22
22
  "lodash": "^4.17.21",
23
23
  "loglevel": "^1.8.1",
24
24
  "reflect-metadata": "^0.1.13",
@@ -32,5 +32,5 @@
32
32
  "snarkyjs": "0.11.0",
33
33
  "tsyringe": "^4.7.0"
34
34
  },
35
- "gitHead": "8ce23c33e6e251febc9ff6a77e2afa19d41a6ee9"
35
+ "gitHead": "51206808f0eba77c4ad8c1af476a6dbdd478d2a3"
36
36
  }
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-underscore-dangle */
1
2
  import { Field } from "snarkyjs";
2
3
 
3
4
  import { RuntimeModule } from "../runtime/RuntimeModule";
@@ -9,15 +10,22 @@ export interface Fieldable {
9
10
  export interface FromFieldClass {
10
11
  new: (...args: any[]) => any;
11
12
  fromFields: (fields: Field[]) => Fieldable;
13
+ name: string;
12
14
  // Maybe this is wrong IDK
13
15
  prototype: {
14
- _fields: any[];
16
+ _fields?: any[];
15
17
  };
18
+ sizeInFields?: () => number;
16
19
  }
17
20
 
18
21
  const errors = {
19
22
  fieldLengthNotMatching: (expected: number, actual: number) =>
20
23
  new Error(`Expected ${expected} field elements, got ${actual}`),
24
+
25
+ typeNotCompatible: (name: string) =>
26
+ new Error(
27
+ `Cannot decode type ${name}, it has to be either a Struct, CircuitValue or built-in snarkyjs type`
28
+ ),
21
29
  };
22
30
 
23
31
  export class MethodParameterDecoder {
@@ -39,8 +47,11 @@ export class MethodParameterDecoder {
39
47
  }
40
48
  let stack = fields.slice();
41
49
  return this.types.map((type) => {
42
- // eslint-disable-next-line no-underscore-dangle
43
- const numberFieldsNeeded = type.prototype._fields.length;
50
+ const numberFieldsNeeded =
51
+ type.prototype._fields?.length ?? type.sizeInFields?.() ?? -1;
52
+ if (numberFieldsNeeded === -1) {
53
+ throw errors.typeNotCompatible(type.name);
54
+ }
44
55
  const structFields = stack.slice(0, numberFieldsNeeded);
45
56
  stack = stack.slice(numberFieldsNeeded);
46
57
  return type.fromFields(structFields);
@@ -49,7 +60,9 @@ export class MethodParameterDecoder {
49
60
 
50
61
  public get fieldSize(): number {
51
62
  return this.types
52
- .map((type) => type.prototype._fields.length)
63
+ .map(
64
+ (type) => type.prototype._fields?.length ?? type.sizeInFields?.() ?? 0
65
+ )
53
66
  .reduce((a, b) => a + b);
54
67
  }
55
68
  }
@@ -2,6 +2,7 @@ import { Bool, Provable } from "snarkyjs";
2
2
  import { container } from "tsyringe";
3
3
 
4
4
  import { RuntimeMethodExecutionContext } from "./RuntimeMethodExecutionContext";
5
+ import { log } from "@proto-kit/common";
5
6
 
6
7
  /**
7
8
  * Maintains an execution status of the current runtime module method,
@@ -17,6 +18,10 @@ export function assert(condition: Bool, message?: string) {
17
18
  const previousStatus = executionContext.current().result.status;
18
19
  const status = Provable.if(previousStatus, Bool, condition, previousStatus);
19
20
 
21
+ if (!status.toBoolean()) {
22
+ log.debug("Assertion failed: ", message);
23
+ }
24
+
20
25
  // const status = previousStatus.and(condition);
21
26
  executionContext.setStatus(status);
22
27
  executionContext.setStatusMessage(message);