@proto-kit/module 0.1.1-develop.161 → 0.1.1-develop.163

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.
@@ -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;AAM1C;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,QAYvD"}
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,QAWvD"}
@@ -15,10 +15,9 @@ export function assert(condition, message) {
15
15
  const executionContext = container.resolve(RuntimeMethodExecutionContext);
16
16
  const previousStatus = executionContext.current().result.status;
17
17
  const status = Provable.if(previousStatus, Bool, condition, previousStatus);
18
- if (!status.toBoolean()) {
18
+ if (!condition.toBoolean()) {
19
19
  log.debug("Assertion failed: ", message);
20
+ executionContext.setStatusMessage(message);
20
21
  }
21
- // const status = previousStatus.and(condition);
22
22
  executionContext.setStatus(status);
23
- executionContext.setStatusMessage(message);
24
23
  }
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.161+923a146",
5
+ "version": "0.1.1-develop.163+1656fd6",
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.161+923a146",
21
- "@proto-kit/protocol": "0.1.1-develop.161+923a146",
20
+ "@proto-kit/common": "0.1.1-develop.163+1656fd6",
21
+ "@proto-kit/protocol": "0.1.1-develop.163+1656fd6",
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": "923a1466aac4dd2e2d2206845eadcc6c0f413849"
35
+ "gitHead": "1656fd6c13ef14f1bea45cd2629a569f1da02236"
36
36
  }
@@ -18,11 +18,10 @@ export function assert(condition: Bool, message?: string) {
18
18
  const previousStatus = executionContext.current().result.status;
19
19
  const status = Provable.if(previousStatus, Bool, condition, previousStatus);
20
20
 
21
- if (!status.toBoolean()) {
21
+ if (!condition.toBoolean()) {
22
22
  log.debug("Assertion failed: ", message);
23
+ executionContext.setStatusMessage(message);
23
24
  }
24
25
 
25
- // const status = previousStatus.and(condition);
26
26
  executionContext.setStatus(status);
27
- executionContext.setStatusMessage(message);
28
27
  }
@@ -0,0 +1,95 @@
1
+ import {
2
+ CachedMerkleTreeStore,
3
+ InMemoryMerkleTreeStorage,
4
+ RollupMerkleTree,
5
+ } from "@proto-kit/protocol";
6
+ import { MockAsyncMerkleTreeStore } from "./MockAsyncMerkleStore";
7
+ import { beforeEach } from "@jest/globals";
8
+ import { Field, Poseidon } from "snarkyjs";
9
+ import { log } from "@proto-kit/common";
10
+
11
+ describe("cachedMerkleTree", () => {
12
+ let store: MockAsyncMerkleTreeStore;
13
+ let syncStore: InMemoryMerkleTreeStorage;
14
+ let tree: RollupMerkleTree;
15
+
16
+ let cached: CachedMerkleTreeStore;
17
+ let cachedTree: RollupMerkleTree;
18
+
19
+ beforeEach(async () => {
20
+ log.setLevel("DEBUG");
21
+
22
+ store = new MockAsyncMerkleTreeStore();
23
+ syncStore = store.store;
24
+ tree = new RollupMerkleTree(syncStore);
25
+
26
+ tree.setLeaf(1n, Field(10));
27
+ tree.setLeaf(3n, Field(30));
28
+ tree.setLeaf(5n, Field(50));
29
+
30
+ cached = new CachedMerkleTreeStore(store);
31
+ await cached.preloadKey(1n);
32
+ await cached.preloadKey(3n);
33
+ await cached.preloadKey(5n);
34
+ cachedTree = new RollupMerkleTree(cached);
35
+ });
36
+
37
+ it("should have the same root", async () => {
38
+ expect(cached.getNode(1n, 0)).toBe(10n);
39
+ expect(cached.getNode(3n, 0)).toBe(30n);
40
+ expect(cached.getNode(5n, 0)).toBe(50n);
41
+ expect(syncStore.getNode(5n, 0)).toBe(50n);
42
+
43
+ expect(cached.getNode(0n, 1)).toBe(
44
+ Poseidon.hash([Field(0), Field(10)]).toBigInt()
45
+ );
46
+
47
+ expect(cached.getNode(0n, 254)).toBe(await store.getNode(0n, 254));
48
+ expect(cached.getNode(0n, 254)).toBe(syncStore.getNode(0n, 254));
49
+
50
+ expect(cachedTree.getRoot().toBigInt()).toBe(tree.getRoot().toBigInt());
51
+ });
52
+
53
+ it("should load the correct root when only loading a subset of keys", async () => {
54
+ await cached.preloadKey(3n);
55
+
56
+ const correctRoot = tree.getRoot();
57
+ expect(cachedTree.getRoot().toBigInt()).toBe(correctRoot.toBigInt());
58
+ });
59
+
60
+ it("should generate correct witnesses when only loading a subset of keys", async () => {
61
+ await cached.preloadKey(5n);
62
+
63
+ const correctRoot = tree.getRoot();
64
+ const witness = tree.getWitness(5n);
65
+ expect(witness.calculateRoot(Field(50)).toBigInt()).toBe(
66
+ correctRoot.toBigInt()
67
+ );
68
+ });
69
+
70
+ it("should generate correct witnesses after merging", async () => {
71
+ expect(cachedTree.getRoot().toBigInt()).toBe(tree.getRoot().toBigInt());
72
+
73
+ await cached.preloadKey(5n);
74
+ cachedTree.setLeaf(5n, Field(100));
75
+
76
+ const correctRoot = cachedTree.getRoot();
77
+
78
+ await cached.mergeIntoParent();
79
+
80
+ const cached2 = new CachedMerkleTreeStore(store);
81
+ await cached2.preloadKey(1n);
82
+ await cached2.preloadKey(3n);
83
+ await cached2.preloadKey(5n);
84
+ expect(cached2.getNode(1n, 0)).toBe(10n);
85
+ expect(cached2.getNode(3n, 0)).toBe(30n);
86
+ expect(cached2.getNode(5n, 0)).toBe(100n);
87
+ const tree2 = new RollupMerkleTree(cached2);
88
+
89
+ const witness = tree2.getWitness(3n);
90
+
91
+ expect(witness.calculateRoot(Field(30)).toBigInt()).toBe(
92
+ correctRoot.toBigInt()
93
+ );
94
+ });
95
+ });
@@ -0,0 +1,28 @@
1
+ import { AsyncMerkleTreeStore, InMemoryMerkleTreeStorage, noop } from "@proto-kit/protocol";
2
+
3
+ export class MockAsyncMerkleTreeStore implements AsyncMerkleTreeStore {
4
+ public readonly store = new InMemoryMerkleTreeStorage();
5
+
6
+ public commit(): void {
7
+ noop();
8
+ }
9
+
10
+ public openTransaction(): void {
11
+ noop();
12
+ }
13
+
14
+ public async getNode(
15
+ key: bigint,
16
+ level: number
17
+ ): Promise<bigint | undefined> {
18
+ return this.store.getNode(key, level);
19
+ }
20
+
21
+ public async setNode(
22
+ key: bigint,
23
+ level: number,
24
+ value: bigint
25
+ ): Promise<void> {
26
+ this.store.setNode(key, level, value);
27
+ }
28
+ }