@proto-kit/protocol 0.1.1-develop.263 → 0.1.1-develop.267
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.
- package/dist/state/StateServiceProvider.d.ts +4 -4
- package/dist/state/StateServiceProvider.d.ts.map +1 -1
- package/dist/state/StateServiceProvider.js +19 -11
- package/dist/state/assert/assert.d.ts.map +1 -1
- package/dist/state/assert/assert.js +3 -1
- package/dist/state/context/RuntimeMethodExecutionContext.d.ts +1 -0
- package/dist/state/context/RuntimeMethodExecutionContext.d.ts.map +1 -1
- package/dist/state/context/RuntimeMethodExecutionContext.js +1 -0
- package/dist/utils/merkletree/InMemoryMerkleTreeStorage.d.ts +1 -15
- package/dist/utils/merkletree/InMemoryMerkleTreeStorage.d.ts.map +1 -1
- package/dist/utils/merkletree/InMemoryMerkleTreeStorage.js +0 -71
- package/dist/utils/merkletree/MerkleTreeStore.d.ts +2 -2
- package/dist/utils/merkletree/MerkleTreeStore.d.ts.map +1 -1
- package/dist/utils/merkletree/RollupMerkleTree.d.ts +1 -0
- package/dist/utils/merkletree/RollupMerkleTree.d.ts.map +1 -1
- package/dist/utils/merkletree/RollupMerkleTree.js +6 -0
- package/package.json +2 -2
- package/src/state/StateServiceProvider.ts +21 -12
- package/src/state/assert/assert.ts +3 -1
- package/src/state/context/RuntimeMethodExecutionContext.ts +1 -0
- package/src/utils/merkletree/InMemoryMerkleTreeStorage.ts +1 -97
- package/src/utils/merkletree/MerkleTreeStore.ts +2 -2
- package/src/utils/merkletree/RollupMerkleTree.ts +9 -0
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { StateService } from "./StateService";
|
|
2
2
|
export declare class StateServiceProvider {
|
|
3
|
-
private
|
|
4
|
-
private readonly
|
|
5
|
-
constructor(
|
|
3
|
+
private readonly baseStateService?;
|
|
4
|
+
private readonly stateServiceStack;
|
|
5
|
+
constructor(baseStateService?: StateService | undefined);
|
|
6
6
|
get stateService(): StateService;
|
|
7
7
|
setCurrentStateService(service: StateService): void;
|
|
8
|
-
|
|
8
|
+
popCurrentStateService(): void;
|
|
9
9
|
}
|
|
10
10
|
//# sourceMappingURL=StateServiceProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StateServiceProvider.d.ts","sourceRoot":"","sources":["../../src/state/StateServiceProvider.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"StateServiceProvider.d.ts","sourceRoot":"","sources":["../../src/state/StateServiceProvider.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAU9C,qBACa,oBAAoB;IAIL,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAC;IAH5D,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAsB;gBAGb,gBAAgB,CAAC,0BAAc;IAO1E,IAAW,YAAY,IAAI,YAAY,CAQtC;IAEM,sBAAsB,CAAC,OAAO,EAAE,YAAY;IAI5C,sBAAsB;CAO9B"}
|
|
@@ -11,27 +11,35 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
13
|
import { inject, injectable } from "tsyringe";
|
|
14
|
+
import { log } from "@proto-kit/common";
|
|
14
15
|
const errors = {
|
|
15
|
-
stateServiceNotSet: () => new Error(
|
|
16
|
-
protocol function by creating them with an AppChain or by setting the
|
|
17
|
-
stateService manually.`),
|
|
16
|
+
stateServiceNotSet: () => new Error("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."),
|
|
18
17
|
};
|
|
19
18
|
let StateServiceProvider = class StateServiceProvider {
|
|
20
|
-
constructor(
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
19
|
+
constructor(baseStateService) {
|
|
20
|
+
this.baseStateService = baseStateService;
|
|
21
|
+
this.stateServiceStack = [];
|
|
22
|
+
if (baseStateService !== undefined) {
|
|
23
|
+
this.stateServiceStack.push(baseStateService);
|
|
24
|
+
}
|
|
23
25
|
}
|
|
24
26
|
get stateService() {
|
|
25
|
-
if (this.
|
|
27
|
+
if (this.stateServiceStack.length === 0) {
|
|
26
28
|
throw errors.stateServiceNotSet();
|
|
27
29
|
}
|
|
28
|
-
|
|
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);
|
|
29
33
|
}
|
|
30
34
|
setCurrentStateService(service) {
|
|
31
|
-
this.
|
|
35
|
+
this.stateServiceStack.push(service);
|
|
32
36
|
}
|
|
33
|
-
|
|
34
|
-
this.
|
|
37
|
+
popCurrentStateService() {
|
|
38
|
+
if (this.stateServiceStack.length === 1) {
|
|
39
|
+
log.trace("Trying to pop last available (root) stateservice");
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
this.stateServiceStack.pop();
|
|
35
43
|
}
|
|
36
44
|
};
|
|
37
45
|
StateServiceProvider = __decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../../src/state/assert/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAMhC;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../../src/state/assert/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAMhC;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,QAavD"}
|
|
@@ -15,7 +15,9 @@ export function assert(condition, message) {
|
|
|
15
15
|
const previousStatus = executionContext.current().result.status;
|
|
16
16
|
const status = condition.and(previousStatus);
|
|
17
17
|
if (!condition.toBoolean()) {
|
|
18
|
-
|
|
18
|
+
if (!executionContext.current().isSimulated) {
|
|
19
|
+
log.debug("Assertion failed: ", message);
|
|
20
|
+
}
|
|
19
21
|
executionContext.setStatusMessage(message);
|
|
20
22
|
}
|
|
21
23
|
executionContext.setStatus(status);
|
|
@@ -55,6 +55,7 @@ export declare class RuntimeMethodExecutionContext extends ProvableMethodExecuti
|
|
|
55
55
|
isFinished: boolean;
|
|
56
56
|
result: RuntimeProvableMethodExecutionResult;
|
|
57
57
|
input: RuntimeMethodExecutionData | undefined;
|
|
58
|
+
isSimulated: boolean;
|
|
58
59
|
};
|
|
59
60
|
}
|
|
60
61
|
//# sourceMappingURL=RuntimeMethodExecutionContext.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RuntimeMethodExecutionContext.d.ts","sourceRoot":"","sources":["../../../src/state/context/RuntimeMethodExecutionContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAEhC,OAAO,EAAE,8BAA8B,EAAE,6BAA6B,EAAE,MAAM,mBAAmB,CAAC;AAClG,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4CAA4C,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAShE,qBAAa,oCAAqC,SAAQ,6BAA6B;IAE9E,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,CAAM;IAE9C,MAAM,EAAE,IAAI,CAAc;IAE1B,aAAa,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,0BAA0B;IACzC,WAAW,EAAE,kBAAkB,CAAC;IAChC,YAAY,EAAE,YAAY,CAAC;CAC5B;AAED;;;;GAIG;AACH,qBACa,6BAA8B,SAAQ,8BAA8B;IACxE,OAAO,EAAE,MAAM,EAAE,CAAM;IAEvB,KAAK,EAAE,0BAA0B,GAAG,SAAS,CAAC;IAGrD,OAAO,CAAC,SAAS,CAAyC;IAE1C,MAAM,uCAA8C;IAEpE,OAAO,CAAC,WAAW,CAAkB;IAErC,OAAO,CAAC,iBAAiB;IAQzB;;;OAGG;IACI,kBAAkB,CAAC,KAAK,EAAE,eAAe,EAAE,eAAe,CAAC,KAAK,CAAC;IAKxE;;OAEG;IACI,gBAAgB,CAAC,OAAO,CAAC,EAAE,MAAM;IAQxC;;OAEG;IACI,SAAS,CAAC,MAAM,EAAE,IAAI;IAQ7B;;OAEG;IACI,KAAK,CAAC,KAAK,EAAE,0BAA0B;IAIvC,YAAY,CAAC,SAAS,EAAE,OAAO;IAItC;;OAEG;IACI,KAAK;IAIL,WAAW;IASlB;;;OAGG;IACI,OAAO
|
|
1
|
+
{"version":3,"file":"RuntimeMethodExecutionContext.d.ts","sourceRoot":"","sources":["../../../src/state/context/RuntimeMethodExecutionContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAEhC,OAAO,EAAE,8BAA8B,EAAE,6BAA6B,EAAE,MAAM,mBAAmB,CAAC;AAClG,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4CAA4C,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAShE,qBAAa,oCAAqC,SAAQ,6BAA6B;IAE9E,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,CAAM;IAE9C,MAAM,EAAE,IAAI,CAAc;IAE1B,aAAa,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,0BAA0B;IACzC,WAAW,EAAE,kBAAkB,CAAC;IAChC,YAAY,EAAE,YAAY,CAAC;CAC5B;AAED;;;;GAIG;AACH,qBACa,6BAA8B,SAAQ,8BAA8B;IACxE,OAAO,EAAE,MAAM,EAAE,CAAM;IAEvB,KAAK,EAAE,0BAA0B,GAAG,SAAS,CAAC;IAGrD,OAAO,CAAC,SAAS,CAAyC;IAE1C,MAAM,uCAA8C;IAEpE,OAAO,CAAC,WAAW,CAAkB;IAErC,OAAO,CAAC,iBAAiB;IAQzB;;;OAGG;IACI,kBAAkB,CAAC,KAAK,EAAE,eAAe,EAAE,eAAe,CAAC,KAAK,CAAC;IAKxE;;OAEG;IACI,gBAAgB,CAAC,OAAO,CAAC,EAAE,MAAM;IAQxC;;OAEG;IACI,SAAS,CAAC,MAAM,EAAE,IAAI;IAQ7B;;OAEG;IACI,KAAK,CAAC,KAAK,EAAE,0BAA0B;IAIvC,YAAY,CAAC,SAAS,EAAE,OAAO;IAItC;;OAEG;IACI,KAAK;IAIL,WAAW;IASlB;;;OAGG;IACI,OAAO;;;;;;CAQf"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MerkleTreeStore } from "./MerkleTreeStore";
|
|
2
2
|
export declare class InMemoryMerkleTreeStorage implements MerkleTreeStore {
|
|
3
3
|
protected readonly nodes: {
|
|
4
4
|
[key: number]: {
|
|
@@ -8,18 +8,4 @@ export declare class InMemoryMerkleTreeStorage implements MerkleTreeStore {
|
|
|
8
8
|
getNode(key: bigint, level: number): bigint | undefined;
|
|
9
9
|
setNode(key: bigint, level: number, value: bigint): void;
|
|
10
10
|
}
|
|
11
|
-
export declare class CachedMerkleTreeStore extends InMemoryMerkleTreeStorage {
|
|
12
|
-
private readonly parent;
|
|
13
|
-
private writeCache;
|
|
14
|
-
constructor(parent: AsyncMerkleTreeStore);
|
|
15
|
-
setNode(key: bigint, level: number, value: bigint): void;
|
|
16
|
-
getWrittenNodes(): {
|
|
17
|
-
[key: number]: {
|
|
18
|
-
[key: string]: bigint;
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
resetWrittenNodes(): void;
|
|
22
|
-
preloadKey(index: bigint): Promise<void>;
|
|
23
|
-
mergeIntoParent(): Promise<void>;
|
|
24
|
-
}
|
|
25
11
|
//# sourceMappingURL=InMemoryMerkleTreeStorage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InMemoryMerkleTreeStorage.d.ts","sourceRoot":"","sources":["../../../src/utils/merkletree/InMemoryMerkleTreeStorage.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"InMemoryMerkleTreeStorage.d.ts","sourceRoot":"","sources":["../../../src/utils/merkletree/InMemoryMerkleTreeStorage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,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,GAAG,IAAI;CAGhE"}
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-magic-numbers */
|
|
2
|
-
import { log } from "@proto-kit/common";
|
|
3
|
-
import { RollupMerkleTree } from "./RollupMerkleTree.js";
|
|
4
1
|
export class InMemoryMerkleTreeStorage {
|
|
5
2
|
constructor() {
|
|
6
3
|
this.nodes = {};
|
|
@@ -13,71 +10,3 @@ export class InMemoryMerkleTreeStorage {
|
|
|
13
10
|
((_a = this.nodes)[level] ?? (_a[level] = {}))[key.toString()] = value;
|
|
14
11
|
}
|
|
15
12
|
}
|
|
16
|
-
export class CachedMerkleTreeStore extends InMemoryMerkleTreeStorage {
|
|
17
|
-
constructor(parent) {
|
|
18
|
-
super();
|
|
19
|
-
this.parent = parent;
|
|
20
|
-
this.writeCache = {};
|
|
21
|
-
}
|
|
22
|
-
setNode(key, level, value) {
|
|
23
|
-
var _a;
|
|
24
|
-
super.setNode(key, level, value);
|
|
25
|
-
((_a = this.writeCache)[level] ?? (_a[level] = {}))[key.toString()] = value;
|
|
26
|
-
}
|
|
27
|
-
getWrittenNodes() {
|
|
28
|
-
return this.writeCache;
|
|
29
|
-
}
|
|
30
|
-
resetWrittenNodes() {
|
|
31
|
-
this.writeCache = {};
|
|
32
|
-
}
|
|
33
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
34
|
-
async preloadKey(index) {
|
|
35
|
-
// Algo from RollupMerkleTree.getWitness()
|
|
36
|
-
const { leafCount, height } = RollupMerkleTree;
|
|
37
|
-
if (index >= leafCount) {
|
|
38
|
-
index %= leafCount;
|
|
39
|
-
}
|
|
40
|
-
// eslint-disable-next-line no-warning-comments,max-len
|
|
41
|
-
// TODO Not practical at the moment. Improve pattern when implementing DB storage
|
|
42
|
-
for (let level = 0; level < height; level++) {
|
|
43
|
-
const key = index;
|
|
44
|
-
const isLeft = index % 2n === 0n;
|
|
45
|
-
const siblingKey = isLeft ? index + 1n : index - 1n;
|
|
46
|
-
// Only preload node if it is not already preloaded.
|
|
47
|
-
// We also don't want to overwrite because changes will get lost (tracing)
|
|
48
|
-
if (this.getNode(key, level) === undefined) {
|
|
49
|
-
// eslint-disable-next-line no-await-in-loop
|
|
50
|
-
const value = await this.parent.getNode(key, level);
|
|
51
|
-
if (level === 0) {
|
|
52
|
-
log.debug(`Preloaded ${key} @ ${level} -> ${value ?? "-"}`);
|
|
53
|
-
}
|
|
54
|
-
if (value !== undefined) {
|
|
55
|
-
this.setNode(key, level, value);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (this.getNode(siblingKey, level) === undefined) {
|
|
59
|
-
// eslint-disable-next-line no-await-in-loop
|
|
60
|
-
const sibling = await this.parent.getNode(siblingKey, level);
|
|
61
|
-
if (sibling !== undefined) {
|
|
62
|
-
this.setNode(siblingKey, level, sibling);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
index /= 2n;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
async mergeIntoParent() {
|
|
69
|
-
// In case no state got set we can skip this step
|
|
70
|
-
if (Object.keys(this.writeCache).length === 0) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
this.parent.openTransaction();
|
|
74
|
-
const { height } = RollupMerkleTree;
|
|
75
|
-
const nodes = this.getWrittenNodes();
|
|
76
|
-
const promises = Array.from({ length: height }).flatMap((ignored, level) => Object.entries(nodes[level]).map(async (entry) => {
|
|
77
|
-
await this.parent.setNode(BigInt(entry[0]), level, entry[1]);
|
|
78
|
-
}));
|
|
79
|
-
await Promise.all(promises);
|
|
80
|
-
this.parent.commit();
|
|
81
|
-
this.resetWrittenNodes();
|
|
82
|
-
}
|
|
83
|
-
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export interface AsyncMerkleTreeStore {
|
|
2
2
|
openTransaction: () => void;
|
|
3
3
|
commit: () => void;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
setNodeAsync: (key: bigint, level: number, value: bigint) => Promise<void>;
|
|
5
|
+
getNodeAsync: (key: bigint, level: number) => Promise<bigint | undefined>;
|
|
6
6
|
}
|
|
7
7
|
export interface MerkleTreeStore {
|
|
8
8
|
setNode: (key: bigint, level: number, value: bigint) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MerkleTreeStore.d.ts","sourceRoot":"","sources":["../../../src/utils/merkletree/MerkleTreeStore.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,oBAAoB;IACnC,eAAe,EAAE,MAAM,IAAI,CAAC;IAE5B,MAAM,EAAE,MAAM,IAAI,CAAC;IAEnB,
|
|
1
|
+
{"version":3,"file":"MerkleTreeStore.d.ts","sourceRoot":"","sources":["../../../src/utils/merkletree/MerkleTreeStore.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,oBAAoB;IACnC,eAAe,EAAE,MAAM,IAAI,CAAC;IAE5B,MAAM,EAAE,MAAM,IAAI,CAAC;IAEnB,YAAY,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAE3E,YAAY,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;CAC3E;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAE7D,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;CAC7D"}
|
|
@@ -54,6 +54,7 @@ declare class RollupMerkleWitness extends RollupMerkleWitness_base {
|
|
|
54
54
|
* @returns Index of the leaf.
|
|
55
55
|
*/
|
|
56
56
|
calculateIndex(): Field;
|
|
57
|
+
toShortenedEntries(): string[];
|
|
57
58
|
}
|
|
58
59
|
/**
|
|
59
60
|
* A [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree) is a binary tree in
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RollupMerkleTree.d.ts","sourceRoot":"","sources":["../../../src/utils/merkletree/RollupMerkleTree.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,IAAI,EAAE,KAAK,EAA8B,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"RollupMerkleTree.d.ts","sourceRoot":"","sources":["../../../src/utils/merkletree/RollupMerkleTree.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,IAAI,EAAE,KAAK,EAA8B,MAAM,UAAU,CAAC;AAKnE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAIpD,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,CAAC;AAIjD,OAAO,EAAE,SAAS,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAErB;;;GAGG;AACH,cAAM,mBAAoB,SAAQ,wBAGhC;IACA,OAAc,MAAM,SAAO;IAEpB,MAAM,IAAI,MAAM;IAIvB;;;;OAIG;IACI,aAAa,CAAC,IAAI,EAAE,KAAK,GAAG,KAAK;IAaxC;;;OAGG;IACI,cAAc,IAAI,KAAK;IAkBvB,kBAAkB;CAO1B;AAED;;;;;;;;;;;;;GAaG;AACH,cAAM,gBAAgB;IACpB,OAAc,MAAM,SAAO;IAE3B,WAAkB,SAAS,IAAI,MAAM,CAEpC;IAED,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAW;IAElC,SAAgB,KAAK,EAAE,eAAe,CAAC;gBAEpB,KAAK,EAAE,eAAe;IAYzC;;;;;OAKG;IAEI,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,KAAK;IAInD;;;OAGG;IAEI,OAAO,IAAI,KAAK;IAMvB,OAAO,CAAC,OAAO;IAIf;;;OAGG;IAEH;;;;OAIG;IAEI,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK;IAgBzC;;;;;;OAMG;IACI,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,mBAAmB;IAqBrD;;;OAGG;IAEI,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE;IAM3B;;;OAGG;IACH,IAAW,SAAS,IAAI,MAAM,CAE7B;CACF;AAGD,yBAAiB,eAAe,CAAC;IAC/B,SAAgB,YAAY,CAAC,GAAG,EAAE,KAAK,GAAG,KAAK,CAS9C;IAED,SAAgB,eAAe,CAC7B,OAAO,EAAE,mBAAmB,EAC5B,IAAI,EAAE,KAAK,EACX,GAAG,EAAE,KAAK,EACV,KAAK,EAAE,KAAK,GACX,IAAI,CAKN;IAED,SAAgB,uBAAuB,CACrC,OAAO,EAAE,mBAAmB,EAC5B,IAAI,EAAE,KAAK,EACX,GAAG,EAAE,KAAK,EACV,KAAK,EAAE,KAAK,GACX,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,CAKtB;IAED,SAAgB,WAAW,CACzB,OAAO,EAAE,mBAAmB,EAC5B,KAAK,EAAE,KAAK,GACX,KAAK,CAEP;CACF;AAED;;;GAGG;AACH,iBAAS,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAK9D"}
|
|
@@ -12,6 +12,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
12
12
|
/* eslint-disable no-inline-comments */
|
|
13
13
|
/* eslint-disable @typescript-eslint/no-magic-numbers */
|
|
14
14
|
import { Bool, Field, Poseidon, Provable, Struct } from "snarkyjs";
|
|
15
|
+
import { range } from "@proto-kit/common";
|
|
15
16
|
import { notInCircuit } from "../utils";
|
|
16
17
|
// external API
|
|
17
18
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
@@ -60,6 +61,11 @@ class RollupMerkleWitness extends Struct({
|
|
|
60
61
|
}
|
|
61
62
|
return index;
|
|
62
63
|
}
|
|
64
|
+
toShortenedEntries() {
|
|
65
|
+
return range(0, 5)
|
|
66
|
+
.concat(range(251, 255))
|
|
67
|
+
.map((index) => [this.path[index].toString(), this.isLeft[index].toString()].toString());
|
|
68
|
+
}
|
|
63
69
|
}
|
|
64
70
|
RollupMerkleWitness.height = 256;
|
|
65
71
|
/**
|
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.
|
|
6
|
+
"version": "0.1.1-develop.267+b252853",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc -p tsconfig.json",
|
|
9
9
|
"dev": "tsc -p tsconfig.json --watch",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"@jest/globals": "^29.5.0",
|
|
32
32
|
"@types/lodash": "^4.14.194"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "b2528538c73747d000cc3ea99ee26ee415d8248d"
|
|
35
35
|
}
|
|
@@ -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
|
-
|
|
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
|
|
17
|
-
this.currentStateService;
|
|
15
|
+
private readonly stateServiceStack: StateService[] = [];
|
|
18
16
|
|
|
19
17
|
public constructor(
|
|
20
|
-
@inject("StateService") private
|
|
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.
|
|
26
|
+
if (this.stateServiceStack.length === 0) {
|
|
25
27
|
throw errors.stateServiceNotSet();
|
|
26
28
|
}
|
|
27
|
-
|
|
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.
|
|
36
|
+
this.stateServiceStack.push(service);
|
|
32
37
|
}
|
|
33
38
|
|
|
34
|
-
public
|
|
35
|
-
this.
|
|
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
|
}
|
|
@@ -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
|
-
|
|
22
|
+
if (!executionContext.current().isSimulated) {
|
|
23
|
+
log.debug("Assertion failed: ", message);
|
|
24
|
+
}
|
|
23
25
|
executionContext.setStatusMessage(message);
|
|
24
26
|
}
|
|
25
27
|
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
6
|
+
setNodeAsync: (key: bigint, level: number, value: bigint) => Promise<void>;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
getNodeAsync: (key: bigint, level: number) => Promise<bigint | undefined>;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export interface MerkleTreeStore {
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
/* eslint-disable no-inline-comments */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-magic-numbers */
|
|
5
5
|
import { Bool, Field, Poseidon, Provable, Struct } from "snarkyjs";
|
|
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
|
/**
|