@proto-kit/protocol 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.
@@ -82,10 +82,10 @@ let BlockProver = class BlockProver extends ProtocolModule {
82
82
  stateRoot: publicInput.stateRoot,
83
83
  networkStateHash: publicInput.networkStateHash,
84
84
  };
85
- this.applyTransaction(state, stateProof, appProof, executionData);
85
+ const stateTo = this.applyTransaction(state, stateProof, appProof, executionData);
86
86
  return new BlockProverPublicOutput({
87
- stateRoot: state.stateRoot,
88
- transactionsHash: state.transactionsHash,
87
+ stateRoot: stateTo.stateRoot,
88
+ transactionsHash: stateTo.transactionsHash,
89
89
  });
90
90
  }
91
91
  merge(publicInput, proof1, proof2) {
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAKA,OAAO,EAAW,KAAK,EAAmB,MAAM,UAAU,CAAC;AAE3D,MAAM,MAAM,UAAU,CAAC,YAAY,SAAS,QAAQ,IAAI,YAAY,SAAS,CAC3E,GAAG,IAAI,EAAE,GAAG,EAAE,KACX,MAAM,MAAM,GACb,MAAM,GACN,GAAG,CAAC;AAER,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;AAEvD,MAAM,MAAM,UAAU,CAAC,KAAK,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,KAAK,CAAC;AAE9D,MAAM,MAAM,QAAQ,CAAC,KAAK,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,GAAG,IAAI,CAAC,KAC/D,GAAG,IAAI,EAAE,GAAG,KACT,YAAY,CAAC,KAAK,CAAC,CAAC,GAAG;KACzB,GAAG,IAAI,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC;CACjC,GAAG;IAAE,SAAS,EAAE,YAAY,CAAC,KAAK,CA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B,IAAI,IAAI,IAAI,CAAG"}
@@ -1,6 +1,5 @@
1
1
  // eslint-disable-next-line max-len
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/ban-types, @typescript-eslint/no-unsafe-return,@typescript-eslint/no-empty-function */
3
- import { TextEncoder, TextDecoder } from "node:util";
4
3
  import { Circuit, Field, Poseidon } from "snarkyjs";
5
4
  export function notInCircuit() {
6
5
  return function ReplacedFunction(target, propertyKey, descriptor) {
@@ -19,6 +18,7 @@ export function notInCircuit() {
19
18
  }
20
19
  export function stringToField(value, throwOnOverflow = false) {
21
20
  const fieldSize = Field.sizeInBytes();
21
+ // eslint-disable-next-line putout/putout
22
22
  const encoder = new TextEncoder();
23
23
  const stringBytes = Array.from(encoder.encode(value));
24
24
  const padding = Array.from({
@@ -57,6 +57,7 @@ export function fieldToString(value) {
57
57
  .reverse()
58
58
  .findIndex((element) => element !== 0);
59
59
  bytes = bytes.slice(0, zeroesStart);
60
+ // eslint-disable-next-line putout/putout
60
61
  const decoder = new TextDecoder();
61
62
  return decoder.decode(new Uint8Array(bytes));
62
63
  }
@@ -1 +1 @@
1
- {"version":3,"file":"InMemoryMerkleTreeStorage.d.ts","sourceRoot":"","sources":["../../../src/utils/merkletree/InMemoryMerkleTreeStorage.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAE1E,qBAAa,yBAA0B,YAAW,eAAe;IAC/D,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE;QACxB,CAAC,GAAG,EAAE,MAAM,GAAG;YACb,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;SACvB,CAAC;KACH,CAAM;IAEA,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAIvD,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAA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AAM,EAAE,KAAK,EAAE,MAAM;IAKjD,eAAe,IAAI;QACxB,CAAC,GAAG,EAAE,MAAM,GAAG;YACb,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;SACvB,CAAC;KACH;IAIM,iBAAiB;IAIX,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAiCxC,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;CAqB9C"}
@@ -32,7 +32,6 @@ export class CachedMerkleTreeStore extends InMemoryMerkleTreeStorage {
32
32
  this.writeCache = {};
33
33
  }
34
34
  async preloadKey(index) {
35
- var _a;
36
35
  log.debug(`Preloading MT ${index}`);
37
36
  // Algo from RollupMerkleTree.getWitness()
38
37
  const { leafCount, height } = RollupMerkleTree;
@@ -43,13 +42,19 @@ export class CachedMerkleTreeStore extends InMemoryMerkleTreeStorage {
43
42
  // TODO Not practical at the moment. Improve pattern when implementing DB storage
44
43
  for (let level = 0; level < height; level++) {
45
44
  const key = index;
45
+ const isLeft = index % 2n === 0n;
46
+ const siblingKey = isLeft ? index + 1n : index - 1n;
46
47
  // eslint-disable-next-line no-await-in-loop
47
48
  const value = await this.parent.getNode(key, level);
49
+ const sibling = await this.parent.getNode(siblingKey, level);
48
50
  if (level === 0) {
49
- log.debug(`Preloaded ${key} -> ${value ?? "-"}`);
51
+ log.debug(`Preloaded ${key} @ ${level} -> ${value ?? "-"}`);
50
52
  }
51
53
  if (value !== undefined) {
52
- ((_a = this.nodes)[level] ?? (_a[level] = {}))[key.toString()] = value;
54
+ this.setNode(key, level, value);
55
+ }
56
+ if (sibling !== undefined) {
57
+ this.setNode(siblingKey, level, sibling);
53
58
  }
54
59
  index /= 2n;
55
60
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "license": "MIT",
4
4
  "private": false,
5
5
  "type": "module",
6
- "version": "0.1.1-develop.161+923a146",
6
+ "version": "0.1.1-develop.163+1656fd6",
7
7
  "scripts": {
8
8
  "build": "tsc -p tsconfig.json",
9
9
  "dev": "tsc -p tsconfig.json --watch",
@@ -17,7 +17,7 @@
17
17
  "access": "public"
18
18
  },
19
19
  "dependencies": {
20
- "@proto-kit/common": "0.1.1-develop.161+923a146",
20
+ "@proto-kit/common": "0.1.1-develop.163+1656fd6",
21
21
  "lodash": "^4.17.21",
22
22
  "loglevel": "^1.8.1",
23
23
  "reflect-metadata": "^0.1.13",
@@ -31,5 +31,5 @@
31
31
  "@jest/globals": "^29.5.0",
32
32
  "@types/lodash": "^4.14.194"
33
33
  },
34
- "gitHead": "923a1466aac4dd2e2d2206845eadcc6c0f413849"
34
+ "gitHead": "1656fd6c13ef14f1bea45cd2629a569f1da02236"
35
35
  }
@@ -182,11 +182,11 @@ export class BlockProver
182
182
  networkStateHash: publicInput.networkStateHash,
183
183
  };
184
184
 
185
- this.applyTransaction(state, stateProof, appProof, executionData);
185
+ const stateTo = this.applyTransaction(state, stateProof, appProof, executionData);
186
186
 
187
187
  return new BlockProverPublicOutput({
188
- stateRoot: state.stateRoot,
189
- transactionsHash: state.transactionsHash,
188
+ stateRoot: stateTo.stateRoot,
189
+ transactionsHash: stateTo.transactionsHash,
190
190
  });
191
191
  }
192
192
 
@@ -63,13 +63,20 @@ export class CachedMerkleTreeStore extends InMemoryMerkleTreeStorage {
63
63
  for (let level = 0; level < height; level++) {
64
64
  const key = index;
65
65
 
66
+ const isLeft = index % 2n === 0n;
67
+ const siblingKey = isLeft ? index + 1n : index - 1n;
68
+
66
69
  // eslint-disable-next-line no-await-in-loop
67
70
  const value = await this.parent.getNode(key, level);
71
+ const sibling = await this.parent.getNode(siblingKey, level);
68
72
  if (level === 0) {
69
- log.debug(`Preloaded ${key} -> ${value ?? "-"}`);
73
+ log.debug(`Preloaded ${key} @ ${level} -> ${value ?? "-"}`);
70
74
  }
71
75
  if (value !== undefined) {
72
- (this.nodes[level] ??= {})[key.toString()] = value;
76
+ this.setNode(key, level, value);
77
+ }
78
+ if(sibling !== undefined) {
79
+ this.setNode(siblingKey, level, sibling);
73
80
  }
74
81
  index /= 2n;
75
82
  }
@@ -1,8 +1,6 @@
1
1
  // eslint-disable-next-line max-len
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/ban-types, @typescript-eslint/no-unsafe-return,@typescript-eslint/no-empty-function */
3
3
 
4
- import { TextEncoder, TextDecoder } from "node:util";
5
-
6
4
  import { Circuit, Field, Poseidon, Proof } from "snarkyjs";
7
5
 
8
6
  export type ReturnType<FunctionType extends Function> = FunctionType extends (
@@ -46,6 +44,7 @@ export function notInCircuit(): MethodDecorator {
46
44
  export function stringToField(value: string, throwOnOverflow = false) {
47
45
  const fieldSize = Field.sizeInBytes();
48
46
 
47
+ // eslint-disable-next-line putout/putout
49
48
  const encoder = new TextEncoder();
50
49
 
51
50
  const stringBytes = Array.from(encoder.encode(value));
@@ -95,6 +94,7 @@ export function fieldToString(value: Field | bigint): string {
95
94
 
96
95
  bytes = bytes.slice(0, zeroesStart);
97
96
 
97
+ // eslint-disable-next-line putout/putout
98
98
  const decoder = new TextDecoder();
99
99
 
100
100
  return decoder.decode(new Uint8Array(bytes));
@@ -6,7 +6,7 @@ import {
6
6
  Option,
7
7
  ProvableStateTransition,
8
8
  DefaultProvableHashList,
9
- StateTransitionProvableBatch, CachedMerkleTreeStore
9
+ StateTransitionProvableBatch, CachedMerkleTreeStore, InMemoryMerkleTreeStorage
10
10
  } from "../src/index";
11
11
  import {
12
12
  RollupMerkleTree,
@@ -108,7 +108,7 @@ describe("stateTransition", () => {
108
108
  ])("should pass without throwing", async (transitions) => {
109
109
  expect.assertions(2);
110
110
 
111
- const tree = new RollupMerkleTree(new MemoryMerkleTreeStorage());
111
+ const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
112
112
 
113
113
  // Is ignored because overwritten by first transition
114
114
  tree.setLeaf(1n, Option.fromValue(Field(1), Field).treeValue);
@@ -168,7 +168,7 @@ describe("stateTransition", () => {
168
168
  async (transitions, index) => {
169
169
  expect.assertions(1);
170
170
 
171
- const tree = new RollupMerkleTree(new MemoryMerkleTreeStorage());
171
+ const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
172
172
 
173
173
  // Is ignored because overwritten by first transition
174
174
  tree.setLeaf(1n, Option.fromValue(Field(1), Field).treeValue);