@proto-kit/protocol 0.1.1-develop.340 → 0.1.1-develop.456

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.
Files changed (110) hide show
  1. package/dist/blockmodules/AccountStateModule.d.ts +1 -1
  2. package/dist/blockmodules/AccountStateModule.d.ts.map +1 -1
  3. package/dist/blockmodules/BlockHeightHook.d.ts +7 -0
  4. package/dist/blockmodules/BlockHeightHook.d.ts.map +1 -0
  5. package/dist/blockmodules/BlockHeightHook.js +16 -0
  6. package/dist/blockmodules/NoopBlockHook.d.ts +7 -0
  7. package/dist/blockmodules/NoopBlockHook.d.ts.map +1 -0
  8. package/dist/blockmodules/NoopBlockHook.js +9 -0
  9. package/dist/blockmodules/NoopTransactionHook.d.ts +1 -1
  10. package/dist/blockmodules/NoopTransactionHook.d.ts.map +1 -1
  11. package/dist/blockmodules/NoopTransactionHook.js +1 -2
  12. package/dist/hooks/AccountStateHook.d.ts +38 -0
  13. package/dist/hooks/AccountStateHook.d.ts.map +1 -0
  14. package/dist/hooks/AccountStateHook.js +36 -0
  15. package/dist/hooks/BlockHeightHook.d.ts +7 -0
  16. package/dist/hooks/BlockHeightHook.d.ts.map +1 -0
  17. package/dist/hooks/BlockHeightHook.js +14 -0
  18. package/dist/hooks/NoopBlockHook.d.ts +7 -0
  19. package/dist/hooks/NoopBlockHook.d.ts.map +1 -0
  20. package/dist/hooks/NoopBlockHook.js +9 -0
  21. package/dist/hooks/NoopTransactionHook.d.ts +6 -0
  22. package/dist/hooks/NoopTransactionHook.d.ts.map +1 -0
  23. package/dist/hooks/NoopTransactionHook.js +4 -0
  24. package/dist/hooks/TransactionFeeHook.d.ts +16 -0
  25. package/dist/hooks/TransactionFeeHook.d.ts.map +1 -0
  26. package/dist/hooks/TransactionFeeHook.js +39 -0
  27. package/dist/index.d.ts +4 -4
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +4 -4
  30. package/dist/model/Option.d.ts +29 -22
  31. package/dist/model/Option.d.ts.map +1 -1
  32. package/dist/model/Option.js +51 -45
  33. package/dist/model/StateTransition.d.ts +4 -2
  34. package/dist/model/StateTransition.d.ts.map +1 -1
  35. package/dist/model/StateTransition.js +2 -2
  36. package/dist/model/network/NetworkState.d.ts +1 -40
  37. package/dist/model/network/NetworkState.d.ts.map +1 -1
  38. package/dist/model/network/NetworkState.js +9 -10
  39. package/dist/protocol/Protocol.d.ts +4 -18
  40. package/dist/protocol/Protocol.d.ts.map +1 -1
  41. package/dist/protocol/Protocol.js +20 -9
  42. package/dist/protocol/ProtocolModule.d.ts +2 -2
  43. package/dist/protocol/ProtocolModule.d.ts.map +1 -1
  44. package/dist/protocol/ProtocolModule.js +1 -1
  45. package/dist/protocol/ProvableBlockHook.d.ts +16 -0
  46. package/dist/protocol/ProvableBlockHook.d.ts.map +1 -0
  47. package/dist/protocol/ProvableBlockHook.js +4 -0
  48. package/dist/protocol/ProvableTransactionHook.d.ts +2 -1
  49. package/dist/protocol/ProvableTransactionHook.d.ts.map +1 -1
  50. package/dist/protocol/TransitioningProtocolModule.d.ts +4 -0
  51. package/dist/protocol/TransitioningProtocolModule.d.ts.map +1 -1
  52. package/dist/protocol/TransitioningProtocolModule.js +4 -0
  53. package/dist/prover/block/BlockProvable.d.ts +21 -6
  54. package/dist/prover/block/BlockProvable.d.ts.map +1 -1
  55. package/dist/prover/block/BlockProvable.js +3 -1
  56. package/dist/prover/block/BlockProver.d.ts +9 -4
  57. package/dist/prover/block/BlockProver.d.ts.map +1 -1
  58. package/dist/prover/block/BlockProver.js +66 -16
  59. package/dist/prover/block/BlockTransactionPosition.d.ts +36 -0
  60. package/dist/prover/block/BlockTransactionPosition.d.ts.map +1 -0
  61. package/dist/prover/block/BlockTransactionPosition.js +25 -0
  62. package/dist/prover/statetransition/StateTransitionProver.d.ts +1 -1
  63. package/dist/prover/statetransition/StateTransitionProver.d.ts.map +1 -1
  64. package/dist/prover/statetransition/StateTransitionProver.js +4 -5
  65. package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts +3 -3
  66. package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts.map +1 -1
  67. package/dist/prover/statetransition/StateTransitionWitnessProvider.js +2 -2
  68. package/dist/state/State.d.ts +8 -1
  69. package/dist/state/State.d.ts.map +1 -1
  70. package/dist/state/State.js +11 -4
  71. package/dist/state/StateServiceProvider.d.ts +0 -2
  72. package/dist/state/StateServiceProvider.d.ts.map +1 -1
  73. package/dist/state/StateServiceProvider.js +5 -17
  74. package/package.json +3 -2
  75. package/src/blockmodules/AccountStateModule.ts +2 -2
  76. package/src/blockmodules/BlockHeightHook.ts +21 -0
  77. package/src/blockmodules/NoopBlockHook.ts +16 -0
  78. package/src/blockmodules/NoopTransactionHook.ts +3 -4
  79. package/src/index.ts +4 -4
  80. package/src/model/Option.ts +71 -56
  81. package/src/model/StateTransition.ts +2 -2
  82. package/src/model/network/NetworkState.ts +9 -9
  83. package/src/protocol/Protocol.ts +33 -32
  84. package/src/protocol/ProtocolModule.ts +3 -3
  85. package/src/protocol/ProvableBlockHook.ts +22 -0
  86. package/src/protocol/ProvableTransactionHook.ts +4 -1
  87. package/src/protocol/TransitioningProtocolModule.ts +4 -0
  88. package/src/prover/block/BlockProvable.ts +4 -2
  89. package/src/prover/block/BlockProver.ts +134 -21
  90. package/src/prover/block/BlockTransactionPosition.ts +34 -0
  91. package/src/prover/statetransition/StateTransitionProver.ts +6 -13
  92. package/src/prover/statetransition/StateTransitionWitnessProvider.ts +4 -5
  93. package/src/state/State.ts +11 -4
  94. package/src/state/StateServiceProvider.ts +2 -10
  95. package/test/Option.test.ts +64 -0
  96. package/test/StateTransition.test.ts +1 -1
  97. package/dist/utils/merkletree/InMemoryMerkleTreeStorage.d.ts +0 -11
  98. package/dist/utils/merkletree/InMemoryMerkleTreeStorage.d.ts.map +0 -1
  99. package/dist/utils/merkletree/InMemoryMerkleTreeStorage.js +0 -12
  100. package/dist/utils/merkletree/MerkleTreeStore.d.ts +0 -11
  101. package/dist/utils/merkletree/MerkleTreeStore.d.ts.map +0 -1
  102. package/dist/utils/merkletree/MerkleTreeStore.js +0 -1
  103. package/dist/utils/merkletree/RollupMerkleTree.d.ts +0 -132
  104. package/dist/utils/merkletree/RollupMerkleTree.d.ts.map +0 -1
  105. package/dist/utils/merkletree/RollupMerkleTree.js +0 -251
  106. package/src/utils/merkletree/InMemoryMerkleTreeStorage.ts +0 -17
  107. package/src/utils/merkletree/MerkleTreeStore.ts +0 -15
  108. package/src/utils/merkletree/RollupMerkleTree.ts +0 -275
  109. package/src/utils/merkletree/VirtualMerkleTreeStore.ts +0 -21
  110. package/test/utils/MerkleTree.test.ts +0 -82
@@ -1,17 +0,0 @@
1
- import { MerkleTreeStore } from "./MerkleTreeStore";
2
-
3
- export class InMemoryMerkleTreeStorage implements MerkleTreeStore {
4
- protected readonly nodes: {
5
- [key: number]: {
6
- [key: string]: bigint;
7
- };
8
- } = {};
9
-
10
- public getNode(key: bigint, level: number): bigint | undefined {
11
- return this.nodes[level]?.[key.toString()];
12
- }
13
-
14
- public setNode(key: bigint, level: number, value: bigint): void {
15
- (this.nodes[level] ??= {})[key.toString()] = value;
16
- }
17
- }
@@ -1,15 +0,0 @@
1
- export interface AsyncMerkleTreeStore {
2
- openTransaction: () => void;
3
-
4
- commit: () => void;
5
-
6
- setNodeAsync: (key: bigint, level: number, value: bigint) => Promise<void>;
7
-
8
- getNodeAsync: (key: bigint, level: number) => Promise<bigint | undefined>;
9
- }
10
-
11
- export interface MerkleTreeStore {
12
- setNode: (key: bigint, level: number, value: bigint) => void;
13
-
14
- getNode: (key: bigint, level: number) => bigint | undefined;
15
- }
@@ -1,275 +0,0 @@
1
- /* eslint-disable id-length */
2
- /* eslint-disable line-comment-position */
3
- /* eslint-disable no-inline-comments */
4
- /* eslint-disable @typescript-eslint/no-magic-numbers */
5
- import { Bool, Field, Poseidon, Provable, Struct } from "o1js";
6
- import { range } from "@proto-kit/common";
7
-
8
- import { notInCircuit } from "../utils";
9
-
10
- import { MerkleTreeStore } from "./MerkleTreeStore";
11
-
12
- // external API
13
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
14
- export { RollupMerkleTree, RollupMerkleWitness };
15
-
16
- // internal API
17
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
18
- export { maybeSwap };
19
-
20
- /**
21
- * The {@link BaseMerkleWitness} class defines a circuit-compatible base class
22
- * for [Merkle Witness'](https://computersciencewiki.org/index.php/Merkle_proof).
23
- */
24
- class RollupMerkleWitness extends Struct({
25
- path: Provable.Array(Field, 256 - 1),
26
- isLeft: Provable.Array(Bool, 256 - 1),
27
- }) {
28
- public static height = 256;
29
-
30
- public height(): number {
31
- return RollupMerkleWitness.height;
32
- }
33
-
34
- /**
35
- * Calculates a root depending on the leaf value.
36
- * @param leaf Value of the leaf node that belongs to this Witness.
37
- * @returns The calculated root.
38
- */
39
- public calculateRoot(hash: Field): Field {
40
- const n = this.height();
41
-
42
- for (let index = 1; index < n; ++index) {
43
- const isLeft = this.isLeft[index - 1];
44
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
45
- const [left, right] = maybeSwap(isLeft, hash, this.path[index - 1]);
46
- hash = Poseidon.hash([left, right]);
47
- }
48
-
49
- return hash;
50
- }
51
-
52
- /**
53
- * Calculates the index of the leaf node that belongs to this Witness.
54
- * @returns Index of the leaf.
55
- */
56
- public calculateIndex(): Field {
57
- let powerOfTwo = Field(1);
58
- let index = Field(0);
59
- const n = this.height();
60
-
61
- // eslint-disable-next-line no-underscore-dangle
62
- for (let index_ = 1; index_ < n; ++index_) {
63
- index = Provable.if(
64
- this.isLeft[index_ - 1],
65
- index,
66
- index.add(powerOfTwo)
67
- );
68
- powerOfTwo = powerOfTwo.mul(2);
69
- }
70
-
71
- return index;
72
- }
73
-
74
- public toShortenedEntries() {
75
- return range(0, 5)
76
- .concat(range(251, 255))
77
- .map((index) =>
78
- [this.path[index].toString(), this.isLeft[index].toString()].toString()
79
- );
80
- }
81
- }
82
-
83
- /**
84
- * A [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree) is a binary tree in
85
- * which every leaf is the cryptography hash of a piece of data,
86
- * and every node is the hash of the concatenation of its two child nodes.
87
- *
88
- * A Merkle Tree allows developers to easily and securely verify
89
- * the integrity of large amounts of data.
90
- *
91
- * Take a look at our [documentation](https://docs.minaprotocol.com/en/zkapps)
92
- * on how to use Merkle Trees in combination with zkApps and
93
- * zero knowledge programming!
94
- *
95
- * Levels are indexed from leaves (level 0) to root (level N - 1).
96
- */
97
- class RollupMerkleTree {
98
- public static height = 256;
99
-
100
- public static get leafCount(): bigint {
101
- return 2n ** BigInt(RollupMerkleTree.height - 1);
102
- }
103
-
104
- private readonly zeroes: bigint[];
105
-
106
- public readonly store: MerkleTreeStore;
107
-
108
- public constructor(store: MerkleTreeStore) {
109
- this.store = store;
110
- // eslint-disable-next-line @shopify/prefer-class-properties
111
- this.zeroes = [0n];
112
- for (let index = 1; index < RollupMerkleTree.height; index += 1) {
113
- const previousLevel = Field(this.zeroes[index - 1]);
114
- this.zeroes.push(
115
- Poseidon.hash([previousLevel, previousLevel]).toBigInt()
116
- );
117
- }
118
- }
119
-
120
- /**
121
- * Returns a node which lives at a given index and level.
122
- * @param level Level of the node.
123
- * @param index Index of the node.
124
- * @returns The data of the node.
125
- */
126
- @notInCircuit()
127
- public getNode(level: number, index: bigint): Field {
128
- return Field(this.store.getNode(index, level) ?? this.zeroes[level]);
129
- }
130
-
131
- /**
132
- * Returns the root of the [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree).
133
- * @returns The root of the Merkle Tree.
134
- */
135
- @notInCircuit()
136
- public getRoot(): Field {
137
- return this.getNode(RollupMerkleTree.height - 1, 0n);
138
- }
139
-
140
- // eslint-disable-next-line no-warning-comments
141
- // TODO: this allows to set a node at an index larger than the size. OK?
142
- private setNode(level: number, index: bigint, value: Field) {
143
- this.store.setNode(index, level, value.toBigInt());
144
- }
145
-
146
- /**
147
- * TODO: if this is passed an index bigger than the max, it will set a couple
148
- * of out-of-bounds nodes but not affect the real Merkle root. OK?
149
- */
150
-
151
- /**
152
- * Sets the value of a leaf node at a given index to a given value.
153
- * @param index Position of the leaf node.
154
- * @param leaf New value.
155
- */
156
- @notInCircuit()
157
- public setLeaf(index: bigint, leaf: Field) {
158
- if (index >= this.leafCount) {
159
- index %= this.leafCount;
160
- }
161
- this.setNode(0, index, leaf);
162
- let currentIndex = index;
163
- for (let level = 1; level < RollupMerkleTree.height; level += 1) {
164
- currentIndex /= 2n;
165
-
166
- const left = this.getNode(level - 1, currentIndex * 2n);
167
- const right = this.getNode(level - 1, currentIndex * 2n + 1n);
168
-
169
- this.setNode(level, currentIndex, Poseidon.hash([left, right]));
170
- }
171
- }
172
-
173
- /**
174
- * Returns the witness (also known as
175
- * [Merkle Proof or Merkle Witness](https://computersciencewiki.org/index.php/Merkle_proof))
176
- * for the leaf at the given index.
177
- * @param index Position of the leaf node.
178
- * @returns The witness that belongs to the leaf.
179
- */
180
- public getWitness(index: bigint): RollupMerkleWitness {
181
- if (index >= this.leafCount) {
182
- throw new Error("Index too large for merkle height");
183
- }
184
- const path = [];
185
- const isLefts = [];
186
- for (let level = 0; level < RollupMerkleTree.height - 1; level += 1) {
187
- const isLeft = index % 2n === 0n;
188
- const sibling = this.getNode(level, isLeft ? index + 1n : index - 1n);
189
- isLefts.push(Bool(isLeft));
190
- path.push(sibling);
191
- index /= 2n;
192
- }
193
- return new RollupMerkleWitness({
194
- isLeft: isLefts,
195
- path,
196
- });
197
- }
198
-
199
- // eslint-disable-next-line no-warning-comments, max-len
200
- // TODO: should this take an optional offset? should it fail if the array is too long?
201
- /**
202
- * Fills all leaves of the tree.
203
- * @param leaves Values to fill the leaves with.
204
- */
205
- @notInCircuit()
206
- public fill(leaves: Field[]) {
207
- leaves.forEach((value, index) => {
208
- this.setLeaf(BigInt(index), value);
209
- });
210
- }
211
-
212
- /**
213
- * Returns the amount of leaf nodes.
214
- * @returns Amount of leaf nodes.
215
- */
216
- public get leafCount(): bigint {
217
- return RollupMerkleTree.leafCount;
218
- }
219
- }
220
-
221
- // eslint-disable-next-line @typescript-eslint/no-namespace
222
- export namespace MerkleTreeUtils {
223
- export function normalizeKey(key: Field): Field {
224
- // if(NJORD_MERKLE_TREE_HEIGHT < 256){
225
- // return fieldMod(key, Field(RollupMerkleTree.leafCount).toConstant())
226
- // eslint-disable-next-line max-len
227
- // // return modPower2(key, RollupMerkleTree.height - 1) //TODO Fix modPower2
228
- // }else{
229
- return key;
230
-
231
- // }
232
- }
233
-
234
- export function checkMembership(
235
- witness: RollupMerkleWitness,
236
- root: Field,
237
- key: Field,
238
- value: Field
239
- ): Bool {
240
- const root2 = witness.calculateRoot(value);
241
- const key2 = witness.calculateIndex();
242
- key.assertEquals(key2, "Keys of MerkleWitness does not match");
243
- return root.equals(root2);
244
- }
245
-
246
- export function checkMembershipGetRoots(
247
- witness: RollupMerkleWitness,
248
- root: Field,
249
- key: Field,
250
- value: Field
251
- ): [Bool, Field, Field] {
252
- const root2 = witness.calculateRoot(value);
253
- const key2 = witness.calculateIndex();
254
- key.assertEquals(key2, "Keys of MerkleWitness does not match");
255
- return [root.equals(root2), root, root2];
256
- }
257
-
258
- export function computeRoot(
259
- witness: RollupMerkleWitness,
260
- value: Field
261
- ): Field {
262
- return witness.calculateRoot(value);
263
- }
264
- }
265
-
266
- /**
267
- * More efficient version of `maybeSwapBad` which
268
- * reuses an intermediate variable
269
- */
270
- function maybeSwap(b: Bool, x: Field, y: Field): [Field, Field] {
271
- const m = b.toField().mul(x.sub(y)); // b*(x - y)
272
- const x1 = y.add(m); // y + b*(x - y)
273
- const y2 = x.sub(m); // x - b*(x - y) = x + b*(y - x)
274
- return [x1, y2];
275
- }
@@ -1,21 +0,0 @@
1
- import { MerkleTreeStore } from "./MerkleTreeStore";
2
- import { InMemoryMerkleTreeStorage } from "./InMemoryMerkleTreeStorage";
3
-
4
- /**
5
- * A MemoryMerkleTreeStore that, if falls back to a parent store if it
6
- * has no data
7
- */
8
- export class VirtualMerkleTreeStore extends InMemoryMerkleTreeStorage {
9
- public constructor(private readonly parent: MerkleTreeStore) {
10
- super()
11
- }
12
-
13
- public getNode(key: bigint, level: number): bigint | undefined {
14
- return super.getNode(key, level) ?? this.parent.getNode(key, level);
15
- }
16
-
17
- public setNode(key: bigint, level: number, value: bigint): void {
18
- super.setNode(key, level, value);
19
- }
20
-
21
- }
@@ -1,82 +0,0 @@
1
- import { MockAsyncMerkleTreeStore } from "@proto-kit/module/test/state/MockAsyncMerkleStore";
2
- import { Field, Provable } from "o1js";
3
-
4
- import { CachedMerkleTreeStore, RollupMerkleTree } from "../../src";
5
- import { log } from "@proto-kit/common";
6
-
7
- describe("merkle tree caching", () => {
8
- it("should cache, merge and cache again correctly", async () => {
9
- expect.assertions(1);
10
- log.setLevel(log.levels.DEBUG);
11
-
12
- const asyncService = new MockAsyncMerkleTreeStore();
13
- const cachedStore1 = new CachedMerkleTreeStore(asyncService);
14
- const tree = new RollupMerkleTree(cachedStore1);
15
-
16
- tree.setLeaf(
17
- BigInt(
18
- "8315128407888076827747583618264069725166559532323886730251648126140875106375"
19
- ),
20
- Field(
21
- "7555220006856562833147743033256142154591945963958408607501861037584894828141"
22
- )
23
- );
24
- tree.setLeaf(
25
- BigInt(
26
- "1135724539086759849828193257478967224998007355448882542027535720593543464209"
27
- ),
28
- Field(
29
- "1259702704738371196984972831885384469288156549426257425022876410209069764640"
30
- )
31
- );
32
-
33
- await cachedStore1.mergeIntoParent();
34
-
35
- const cachedStore2 = new CachedMerkleTreeStore(asyncService);
36
-
37
- await cachedStore2.preloadKey(
38
- BigInt(
39
- "135413826711051814106331721573646171856233955198202976565146349582599235605"
40
- )
41
- );
42
- await cachedStore2.preloadKey(
43
- BigInt(
44
- "187096936125835301165703149714221977896269014196675103496127974415786508468"
45
- )
46
- );
47
-
48
- const tree2 = new RollupMerkleTree(cachedStore2);
49
-
50
- tree2.setLeaf(
51
- BigInt(
52
- "135413826711051814106331721573646171856233955198202976565146349582599235605"
53
- ),
54
- Field(
55
- "7555220006856562833147743033256142154591945963958408607501861037584894828141"
56
- )
57
- );
58
- tree2.setLeaf(
59
- BigInt(
60
- "187096936125835301165703149714221977896269014196675103496127974415786508468"
61
- ),
62
- Field(
63
- "12719911732789803963178147640045939094940211306984772742957499065837303164652"
64
- )
65
- );
66
-
67
- const witness = tree2.getWitness(
68
- BigInt(
69
- "135413826711051814106331721573646171856233955198202976565146349582599235605"
70
- )
71
- );
72
- expect(
73
- witness
74
- .calculateRoot(
75
- Field(
76
- "7555220006856562833147743033256142154591945963958408607501861037584894828141"
77
- )
78
- )
79
- .toString()
80
- ).toEqual(tree2.getRoot().toString());
81
- });
82
- });