@proto-kit/protocol 0.1.1-develop.263 → 0.1.1-develop.298

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 (132) hide show
  1. package/dist/blockmodules/AccountStateModule.d.ts +5 -5
  2. package/dist/blockmodules/AccountStateModule.d.ts.map +1 -1
  3. package/dist/blockmodules/AccountStateModule.js +1 -1
  4. package/dist/blockmodules/NoopTransactionHook.d.ts +1 -1
  5. package/dist/blockmodules/NoopTransactionHook.d.ts.map +1 -1
  6. package/dist/model/MethodPublicOutput.d.ts +27 -27
  7. package/dist/model/MethodPublicOutput.js +1 -1
  8. package/dist/model/Option.d.ts +19 -19
  9. package/dist/model/Option.d.ts.map +1 -1
  10. package/dist/model/Option.js +1 -1
  11. package/dist/model/Path.d.ts +2 -2
  12. package/dist/model/Path.d.ts.map +1 -1
  13. package/dist/model/Path.js +1 -1
  14. package/dist/model/StateTransition.d.ts +12 -12
  15. package/dist/model/StateTransition.d.ts.map +1 -1
  16. package/dist/model/StateTransition.js +1 -1
  17. package/dist/model/StateTransitionProvableBatch.d.ts +13 -13
  18. package/dist/model/StateTransitionProvableBatch.d.ts.map +1 -1
  19. package/dist/model/StateTransitionProvableBatch.js +1 -1
  20. package/dist/model/network/NetworkState.d.ts +7 -7
  21. package/dist/model/network/NetworkState.d.ts.map +1 -1
  22. package/dist/model/network/NetworkState.js +1 -1
  23. package/dist/model/transaction/ProtocolTransaction.d.ts +16 -16
  24. package/dist/model/transaction/ProtocolTransaction.d.ts.map +1 -1
  25. package/dist/model/transaction/ProtocolTransaction.js +1 -1
  26. package/dist/model/transaction/RuntimeTransaction.d.ts +16 -16
  27. package/dist/model/transaction/RuntimeTransaction.d.ts.map +1 -1
  28. package/dist/model/transaction/RuntimeTransaction.js +1 -1
  29. package/dist/protocol/Protocol.d.ts +17 -10
  30. package/dist/protocol/Protocol.d.ts.map +1 -1
  31. package/dist/protocol/Protocol.js +30 -32
  32. package/dist/protocol/ProtocolEnvironment.d.ts +9 -0
  33. package/dist/protocol/ProtocolEnvironment.d.ts.map +1 -0
  34. package/dist/protocol/ProtocolEnvironment.js +1 -0
  35. package/dist/protocol/ProtocolModule.d.ts +5 -5
  36. package/dist/protocol/ProtocolModule.d.ts.map +1 -1
  37. package/dist/protocol/ProtocolModule.js +7 -5
  38. package/dist/protocol/ProvableTransactionHook.d.ts +1 -1
  39. package/dist/protocol/ProvableTransactionHook.d.ts.map +1 -1
  40. package/dist/protocol/TransitioningProtocolModule.d.ts +1 -1
  41. package/dist/protocol/TransitioningProtocolModule.d.ts.map +1 -1
  42. package/dist/prover/block/BlockProvable.d.ts +40 -40
  43. package/dist/prover/block/BlockProvable.d.ts.map +1 -1
  44. package/dist/prover/block/BlockProvable.js +1 -1
  45. package/dist/prover/block/BlockProver.d.ts +4 -4
  46. package/dist/prover/block/BlockProver.d.ts.map +1 -1
  47. package/dist/prover/block/BlockProver.js +1 -1
  48. package/dist/prover/statetransition/StateTransitionProvable.d.ts +55 -55
  49. package/dist/prover/statetransition/StateTransitionProvable.d.ts.map +1 -1
  50. package/dist/prover/statetransition/StateTransitionProvable.js +1 -1
  51. package/dist/prover/statetransition/StateTransitionProver.d.ts +2 -2
  52. package/dist/prover/statetransition/StateTransitionProver.d.ts.map +1 -1
  53. package/dist/prover/statetransition/StateTransitionProver.js +1 -6
  54. package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts +1 -1
  55. package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts.map +1 -1
  56. package/dist/state/State.d.ts +1 -1
  57. package/dist/state/State.d.ts.map +1 -1
  58. package/dist/state/State.js +1 -1
  59. package/dist/state/StateMap.d.ts +1 -1
  60. package/dist/state/StateMap.d.ts.map +1 -1
  61. package/dist/state/StateService.d.ts +1 -1
  62. package/dist/state/StateService.d.ts.map +1 -1
  63. package/dist/state/StateServiceProvider.d.ts +4 -4
  64. package/dist/state/StateServiceProvider.d.ts.map +1 -1
  65. package/dist/state/StateServiceProvider.js +19 -11
  66. package/dist/state/assert/assert.d.ts +1 -1
  67. package/dist/state/assert/assert.d.ts.map +1 -1
  68. package/dist/state/assert/assert.js +3 -1
  69. package/dist/state/context/RuntimeMethodExecutionContext.d.ts +2 -1
  70. package/dist/state/context/RuntimeMethodExecutionContext.d.ts.map +1 -1
  71. package/dist/state/context/RuntimeMethodExecutionContext.js +3 -2
  72. package/dist/state/protocol/ProtocolState.d.ts +1 -1
  73. package/dist/state/protocol/ProtocolState.d.ts.map +1 -1
  74. package/dist/utils/PrefixedProvableHashList.d.ts +1 -1
  75. package/dist/utils/PrefixedProvableHashList.d.ts.map +1 -1
  76. package/dist/utils/PrefixedProvableHashList.js +1 -1
  77. package/dist/utils/ProvableHashList.d.ts +2 -2
  78. package/dist/utils/ProvableHashList.d.ts.map +1 -1
  79. package/dist/utils/ProvableHashList.js +1 -1
  80. package/dist/utils/merkletree/InMemoryMerkleTreeStorage.d.ts +1 -15
  81. package/dist/utils/merkletree/InMemoryMerkleTreeStorage.d.ts.map +1 -1
  82. package/dist/utils/merkletree/InMemoryMerkleTreeStorage.js +0 -71
  83. package/dist/utils/merkletree/MerkleTreeStore.d.ts +2 -2
  84. package/dist/utils/merkletree/MerkleTreeStore.d.ts.map +1 -1
  85. package/dist/utils/merkletree/RollupMerkleTree.d.ts +17 -16
  86. package/dist/utils/merkletree/RollupMerkleTree.d.ts.map +1 -1
  87. package/dist/utils/merkletree/RollupMerkleTree.js +7 -1
  88. package/dist/utils/utils.d.ts +2 -3
  89. package/dist/utils/utils.d.ts.map +1 -1
  90. package/dist/utils/utils.js +2 -3
  91. package/package.json +3 -3
  92. package/src/blockmodules/AccountStateModule.ts +2 -2
  93. package/src/blockmodules/NoopTransactionHook.ts +1 -1
  94. package/src/model/MethodPublicOutput.ts +1 -1
  95. package/src/model/Option.test.ts +1 -1
  96. package/src/model/Option.ts +1 -1
  97. package/src/model/Path.ts +1 -1
  98. package/src/model/StateTransition.ts +1 -1
  99. package/src/model/StateTransitionProvableBatch.ts +1 -1
  100. package/src/model/Transaction.ts +1 -1
  101. package/src/model/network/NetworkState.ts +1 -1
  102. package/src/model/transaction/ProtocolTransaction.ts +1 -1
  103. package/src/model/transaction/RuntimeTransaction.ts +1 -1
  104. package/src/protocol/Protocol.ts +72 -63
  105. package/src/protocol/ProtocolEnvironment.ts +9 -0
  106. package/src/protocol/ProtocolModule.ts +14 -11
  107. package/src/protocol/ProvableTransactionHook.ts +1 -1
  108. package/src/protocol/TransitioningProtocolModule.ts +3 -1
  109. package/src/prover/block/BlockProvable.ts +1 -1
  110. package/src/prover/block/BlockProver.ts +4 -4
  111. package/src/prover/statetransition/StateTransitionProvable.ts +1 -1
  112. package/src/prover/statetransition/StateTransitionProver.ts +2 -8
  113. package/src/prover/statetransition/StateTransitionWitnessProvider.ts +1 -1
  114. package/src/state/State.ts +1 -1
  115. package/src/state/StateMap.ts +1 -1
  116. package/src/state/StateService.ts +2 -2
  117. package/src/state/StateServiceProvider.ts +21 -12
  118. package/src/state/assert/assert.test.ts +1 -1
  119. package/src/state/assert/assert.ts +4 -2
  120. package/src/state/context/RuntimeMethodExecutionContext.ts +6 -2
  121. package/src/state/protocol/ProtocolState.ts +3 -1
  122. package/src/utils/PrefixedProvableHashList.ts +1 -1
  123. package/src/utils/ProvableHashList.ts +1 -1
  124. package/src/utils/merkletree/InMemoryMerkleTreeStorage.ts +1 -97
  125. package/src/utils/merkletree/MerkleTreeStore.ts +2 -2
  126. package/src/utils/merkletree/RollupMerkleTree.ts +10 -1
  127. package/src/utils/utils.ts +2 -4
  128. package/test/BlockProver.test.ts +9 -12
  129. package/test/State.test.ts +1 -1
  130. package/test/StateTransition.test.ts +4 -2
  131. package/test/utils/MerkleTree.test.ts +1 -1
  132. package/test/utils.test.ts +1 -1
@@ -1,6 +1,6 @@
1
- import { Field } from "snarkyjs";
1
+ import { Field } from "o1js";
2
2
 
3
3
  export interface StateService {
4
4
  get: (key: Field) => Field[] | undefined;
5
5
  set: (key: Field, value: Field[] | undefined) => void;
6
- }
6
+ }
@@ -1,37 +1,46 @@
1
1
  import { inject, injectable } from "tsyringe";
2
2
 
3
3
  import { StateService } from "./StateService";
4
+ import { log } from "@proto-kit/common";
4
5
 
5
6
  const errors = {
6
7
  stateServiceNotSet: () =>
7
8
  new Error(
8
- `StateService has not been set yet. Be sure to either call your runtime or
9
- protocol function by creating them with an AppChain or by setting the
10
- stateService manually.`
9
+ "StateService has not been set yet. Be sure to either call your runtime or protocol function by creating them with an AppChain or by setting the stateService manually."
11
10
  ),
12
11
  };
13
12
 
14
13
  @injectable()
15
14
  export class StateServiceProvider {
16
- private readonly defaultStateService?: StateService =
17
- this.currentStateService;
15
+ private readonly stateServiceStack: StateService[] = [];
18
16
 
19
17
  public constructor(
20
- @inject("StateService") private currentStateService?: StateService
21
- ) {}
18
+ @inject("StateService") private readonly baseStateService?: StateService
19
+ ) {
20
+ if (baseStateService !== undefined) {
21
+ this.stateServiceStack.push(baseStateService);
22
+ }
23
+ }
22
24
 
23
25
  public get stateService(): StateService {
24
- if (this.currentStateService === undefined) {
26
+ if (this.stateServiceStack.length === 0) {
25
27
  throw errors.stateServiceNotSet();
26
28
  }
27
- return this.currentStateService;
29
+
30
+ // Assertion here is ok, because we check that the array is not empty above
31
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
32
+ return this.stateServiceStack.at(-1)!;
28
33
  }
29
34
 
30
35
  public setCurrentStateService(service: StateService) {
31
- this.currentStateService = service;
36
+ this.stateServiceStack.push(service);
32
37
  }
33
38
 
34
- public resetToDefault() {
35
- this.currentStateService = this.defaultStateService;
39
+ public popCurrentStateService() {
40
+ if (this.stateServiceStack.length === 1) {
41
+ log.trace("Trying to pop last available (root) stateservice");
42
+ return;
43
+ }
44
+ this.stateServiceStack.pop();
36
45
  }
37
46
  }
@@ -1,6 +1,6 @@
1
1
  import "reflect-metadata";
2
2
 
3
- import { Bool } from "snarkyjs";
3
+ import { Bool } from "o1js";
4
4
  import { container } from "tsyringe";
5
5
 
6
6
  import { assert } from "./assert";
@@ -1,4 +1,4 @@
1
- import { Bool } from "snarkyjs";
1
+ import { Bool } from "o1js";
2
2
  import { container } from "tsyringe";
3
3
  import { log } from "@proto-kit/common";
4
4
 
@@ -19,7 +19,9 @@ export function assert(condition: Bool, message?: string) {
19
19
  const status = condition.and(previousStatus);
20
20
 
21
21
  if (!condition.toBoolean()) {
22
- log.debug("Assertion failed: ", message);
22
+ if (!executionContext.current().isSimulated) {
23
+ log.debug("Assertion failed: ", message);
24
+ }
23
25
  executionContext.setStatusMessage(message);
24
26
  }
25
27
 
@@ -1,6 +1,9 @@
1
- import { Bool } from "snarkyjs";
1
+ import { Bool } from "o1js";
2
2
  import { singleton } from "tsyringe";
3
- import { ProvableMethodExecutionContext, ProvableMethodExecutionResult } from "@proto-kit/common";
3
+ import {
4
+ ProvableMethodExecutionContext,
5
+ ProvableMethodExecutionResult,
6
+ } from "@proto-kit/common";
4
7
  import { StateTransition } from "../../model/StateTransition";
5
8
  import { RuntimeTransaction } from "../../model/transaction/RuntimeTransaction";
6
9
  import { NetworkState } from "../../model/network/NetworkState";
@@ -119,6 +122,7 @@ export class RuntimeMethodExecutionContext extends ProvableMethodExecutionContex
119
122
  isFinished: this.isFinished,
120
123
  result: this.result,
121
124
  input: this.lastInput,
125
+ isSimulated: this.isSimulated,
122
126
  };
123
127
  }
124
128
  }
@@ -21,7 +21,9 @@ const errors = {
21
21
  * underlying values to improve developer experience.
22
22
  */
23
23
  export function protocolState() {
24
- return <TargetTransitioningModule extends TransitioningProtocolModule>(
24
+ return <
25
+ TargetTransitioningModule extends TransitioningProtocolModule<unknown>
26
+ >(
25
27
  target: TargetTransitioningModule,
26
28
  propertyKey: string
27
29
  ) => {
@@ -1,4 +1,4 @@
1
- import { Field, FlexibleProvablePure, Poseidon } from "snarkyjs";
1
+ import { Field, FlexibleProvablePure, Poseidon } from "o1js";
2
2
 
3
3
  import { ProvableHashList } from "./ProvableHashList.js";
4
4
  import { stringToField } from "./utils";
@@ -1,4 +1,4 @@
1
- import { Field, Poseidon, FlexibleProvablePure, Bool, Provable } from "snarkyjs";
1
+ import { Field, Poseidon, FlexibleProvablePure, Bool, Provable } from "o1js";
2
2
 
3
3
  /**
4
4
  * Utilities for creating a hash list from a given value type.
@@ -1,8 +1,4 @@
1
- /* eslint-disable @typescript-eslint/no-magic-numbers */
2
- import { log } from "@proto-kit/common";
3
-
4
- import { RollupMerkleTree } from "./RollupMerkleTree.js";
5
- import { AsyncMerkleTreeStore, MerkleTreeStore } from "./MerkleTreeStore";
1
+ import { MerkleTreeStore } from "./MerkleTreeStore";
6
2
 
7
3
  export class InMemoryMerkleTreeStorage implements MerkleTreeStore {
8
4
  protected readonly nodes: {
@@ -19,95 +15,3 @@ export class InMemoryMerkleTreeStorage implements MerkleTreeStore {
19
15
  (this.nodes[level] ??= {})[key.toString()] = value;
20
16
  }
21
17
  }
22
-
23
- export class CachedMerkleTreeStore extends InMemoryMerkleTreeStorage {
24
- private writeCache: {
25
- [key: number]: {
26
- [key: string]: bigint;
27
- };
28
- } = {};
29
-
30
- public constructor(private readonly parent: AsyncMerkleTreeStore) {
31
- super();
32
- }
33
-
34
- public setNode(key: bigint, level: number, value: bigint) {
35
- super.setNode(key, level, value);
36
- (this.writeCache[level] ??= {})[key.toString()] = value;
37
- }
38
-
39
- public getWrittenNodes(): {
40
- [key: number]: {
41
- [key: string]: bigint;
42
- };
43
- } {
44
- return this.writeCache;
45
- }
46
-
47
- public resetWrittenNodes() {
48
- this.writeCache = {};
49
- }
50
-
51
- // eslint-disable-next-line sonarjs/cognitive-complexity
52
- public async preloadKey(index: bigint): Promise<void> {
53
- // Algo from RollupMerkleTree.getWitness()
54
- const { leafCount, height } = RollupMerkleTree;
55
-
56
- if (index >= leafCount) {
57
- index %= leafCount;
58
- }
59
-
60
- // eslint-disable-next-line no-warning-comments,max-len
61
- // TODO Not practical at the moment. Improve pattern when implementing DB storage
62
- for (let level = 0; level < height; level++) {
63
- const key = index;
64
-
65
- const isLeft = index % 2n === 0n;
66
- const siblingKey = isLeft ? index + 1n : index - 1n;
67
-
68
- // Only preload node if it is not already preloaded.
69
- // We also don't want to overwrite because changes will get lost (tracing)
70
- if (this.getNode(key, level) === undefined) {
71
- // eslint-disable-next-line no-await-in-loop
72
- const value = await this.parent.getNode(key, level);
73
- if (level === 0) {
74
- log.debug(`Preloaded ${key} @ ${level} -> ${value ?? "-"}`);
75
- }
76
- if (value !== undefined) {
77
- this.setNode(key, level, value);
78
- }
79
- }
80
-
81
- if (this.getNode(siblingKey, level) === undefined) {
82
- // eslint-disable-next-line no-await-in-loop
83
- const sibling = await this.parent.getNode(siblingKey, level);
84
- if (sibling !== undefined) {
85
- this.setNode(siblingKey, level, sibling);
86
- }
87
- }
88
- index /= 2n;
89
- }
90
- }
91
-
92
- public async mergeIntoParent(): Promise<void> {
93
- // In case no state got set we can skip this step
94
- if (Object.keys(this.writeCache).length === 0) {
95
- return;
96
- }
97
-
98
- this.parent.openTransaction();
99
- const { height } = RollupMerkleTree;
100
- const nodes = this.getWrittenNodes();
101
-
102
- const promises = Array.from({ length: height }).flatMap((ignored, level) =>
103
- Object.entries(nodes[level]).map(async (entry) => {
104
- await this.parent.setNode(BigInt(entry[0]), level, entry[1]);
105
- })
106
- );
107
-
108
- await Promise.all(promises);
109
-
110
- this.parent.commit();
111
- this.resetWrittenNodes();
112
- }
113
- }
@@ -3,9 +3,9 @@ export interface AsyncMerkleTreeStore {
3
3
 
4
4
  commit: () => void;
5
5
 
6
- setNode: (key: bigint, level: number, value: bigint) => Promise<void>;
6
+ setNodeAsync: (key: bigint, level: number, value: bigint) => Promise<void>;
7
7
 
8
- getNode: (key: bigint, level: number) => Promise<bigint | undefined>;
8
+ getNodeAsync: (key: bigint, level: number) => Promise<bigint | undefined>;
9
9
  }
10
10
 
11
11
  export interface MerkleTreeStore {
@@ -2,7 +2,8 @@
2
2
  /* eslint-disable line-comment-position */
3
3
  /* eslint-disable no-inline-comments */
4
4
  /* eslint-disable @typescript-eslint/no-magic-numbers */
5
- import { Bool, Field, Poseidon, Provable, Struct } from "snarkyjs";
5
+ import { Bool, Field, Poseidon, Provable, Struct } from "o1js";
6
+ import { range } from "@proto-kit/common";
6
7
 
7
8
  import { notInCircuit } from "../utils";
8
9
 
@@ -69,6 +70,14 @@ class RollupMerkleWitness extends Struct({
69
70
 
70
71
  return index;
71
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
+ }
72
81
  }
73
82
 
74
83
  /**
@@ -1,7 +1,7 @@
1
1
  // eslint-disable-next-line max-len
2
- /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/ban-types, @typescript-eslint/no-unsafe-return,@typescript-eslint/no-empty-function */
2
+ /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/ban-types, @typescript-eslint/no-unsafe-return */
3
3
 
4
- import { Field, Poseidon, Provable } from "snarkyjs";
4
+ import { Field, Poseidon, Provable } from "o1js";
5
5
  import floor from "lodash/floor";
6
6
 
7
7
  export type ReturnType<FunctionType extends Function> = FunctionType extends (
@@ -82,5 +82,3 @@ export function singleFieldToString(value: Field | bigint): string {
82
82
  }
83
83
  return value.toString();
84
84
  }
85
-
86
- export function noop(): void {}
@@ -1,12 +1,5 @@
1
1
  import "reflect-metadata";
2
- import {
3
- Bool,
4
- Experimental,
5
- Field,
6
- PrivateKey,
7
- Proof,
8
- UInt64,
9
- } from "snarkyjs";
2
+ import { Bool, Experimental, Field, PrivateKey, Proof, UInt64 } from "o1js";
10
3
 
11
4
  import {
12
5
  BlockProver,
@@ -29,12 +22,16 @@ import { UnsignedTransaction } from "@proto-kit/sequencer";
29
22
  import { AccountStateModule } from "../src/blockmodules/AccountStateModule";
30
23
  import { container } from "tsyringe";
31
24
  import {
32
- BlockModule, DefaultProvableHashList,
25
+ BlockModule,
26
+ DefaultProvableHashList,
33
27
  MethodPublicOutput,
34
28
  NetworkState,
35
- Protocol, ProtocolMethodExecutionContext,
36
- ProtocolTransaction, ProvableStateTransition, RuntimeTransaction,
37
- StateTransitionProver
29
+ Protocol,
30
+ ProtocolMethodExecutionContext,
31
+ ProtocolTransaction,
32
+ ProvableStateTransition,
33
+ RuntimeTransaction,
34
+ StateTransitionProver,
38
35
  } from "../src";
39
36
 
40
37
  type BlockProverProofPair = [
@@ -1,5 +1,5 @@
1
1
  import "reflect-metadata";
2
- import { Bool, Field, Provable, UInt64 } from "snarkyjs";
2
+ import { Bool, Field, Provable, UInt64 } from "o1js";
3
3
  import { container } from "tsyringe";
4
4
 
5
5
  import {
@@ -1,12 +1,14 @@
1
1
  import "reflect-metadata";
2
- import { Bool, Field } from "snarkyjs";
2
+ import { Bool, Field } from "o1js";
3
3
  import { container } from "tsyringe";
4
4
 
5
5
  import {
6
6
  Option,
7
7
  ProvableStateTransition,
8
8
  DefaultProvableHashList,
9
- StateTransitionProvableBatch, CachedMerkleTreeStore, InMemoryMerkleTreeStorage
9
+ StateTransitionProvableBatch,
10
+ CachedMerkleTreeStore,
11
+ InMemoryMerkleTreeStorage,
10
12
  } from "../src/index";
11
13
  import {
12
14
  RollupMerkleTree,
@@ -1,5 +1,5 @@
1
1
  import { MockAsyncMerkleTreeStore } from "@proto-kit/module/test/state/MockAsyncMerkleStore";
2
- import { Field, Provable } from "snarkyjs";
2
+ import { Field, Provable } from "o1js";
3
3
 
4
4
  import { CachedMerkleTreeStore, RollupMerkleTree } from "../../src";
5
5
  import { log } from "@proto-kit/common";
@@ -1,5 +1,5 @@
1
1
  import "reflect-metadata";
2
- import { Field } from "snarkyjs";
2
+ import { Field } from "o1js";
3
3
 
4
4
  import { stringToField } from "../src";
5
5