@proto-kit/module 0.1.1-develop.191 → 0.1.1-develop.2024
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/factories/MethodIdFactory.d.ts +6 -6
- package/dist/factories/MethodIdFactory.d.ts.map +1 -1
- package/dist/factories/MethodIdFactory.js +10 -34
- package/dist/factories/MethodIdFactory.js.map +1 -0
- package/dist/index.d.ts +4 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -7
- package/dist/index.js.map +1 -0
- package/dist/messages/OutgoingMessage.d.ts +96 -0
- package/dist/messages/OutgoingMessage.d.ts.map +1 -0
- package/dist/messages/OutgoingMessage.js +68 -0
- package/dist/messages/OutgoingMessage.js.map +1 -0
- package/dist/messages/OutgoingMessages.d.ts +231 -0
- package/dist/messages/OutgoingMessages.d.ts.map +1 -0
- package/dist/messages/OutgoingMessages.js +66 -0
- package/dist/messages/OutgoingMessages.js.map +1 -0
- package/dist/method/MethodParameterEncoder.d.ts +26 -0
- package/dist/method/MethodParameterEncoder.d.ts.map +1 -0
- package/dist/method/MethodParameterEncoder.js +169 -0
- package/dist/method/MethodParameterEncoder.js.map +1 -0
- package/dist/method/runtimeMethod.d.ts +21 -5
- package/dist/method/runtimeMethod.d.ts.map +1 -1
- package/dist/method/runtimeMethod.js +69 -25
- package/dist/method/runtimeMethod.js.map +1 -0
- package/dist/module/decorator.js +1 -0
- package/dist/module/decorator.js.map +1 -0
- package/dist/runtime/MethodIdResolver.d.ts +10 -8
- package/dist/runtime/MethodIdResolver.d.ts.map +1 -1
- package/dist/runtime/MethodIdResolver.js +74 -33
- package/dist/runtime/MethodIdResolver.js.map +1 -0
- package/dist/runtime/Runtime.d.ts +20 -26
- package/dist/runtime/Runtime.d.ts.map +1 -1
- package/dist/runtime/Runtime.js +117 -62
- package/dist/runtime/Runtime.js.map +1 -0
- package/dist/runtime/RuntimeEnvironment.d.ts +10 -0
- package/dist/runtime/RuntimeEnvironment.d.ts.map +1 -0
- package/dist/runtime/RuntimeEnvironment.js +2 -0
- package/dist/runtime/RuntimeEnvironment.js.map +1 -0
- package/dist/runtime/RuntimeModule.d.ts +18 -15
- package/dist/runtime/RuntimeModule.d.ts.map +1 -1
- package/dist/runtime/RuntimeModule.js +33 -8
- package/dist/runtime/RuntimeModule.js.map +1 -0
- package/dist/state/InMemoryStateService.d.ts +11 -10
- package/dist/state/InMemoryStateService.d.ts.map +1 -1
- package/dist/state/InMemoryStateService.js +11 -8
- package/dist/state/InMemoryStateService.js.map +1 -0
- package/dist/state/decorator.d.ts.map +1 -1
- package/dist/state/decorator.js +2 -4
- package/dist/state/decorator.js.map +1 -0
- package/dist/testing/TestingRuntime.d.ts +8 -0
- package/dist/testing/TestingRuntime.d.ts.map +1 -0
- package/dist/testing/TestingRuntime.js +31 -0
- package/dist/testing/TestingRuntime.js.map +1 -0
- package/jest.config.cjs +12 -1
- package/package.json +10 -11
- package/src/factories/MethodIdFactory.ts +10 -17
- package/src/index.ts +4 -7
- package/src/messages/OutgoingMessages.ts +122 -0
- package/src/method/MethodParameterEncoder.ts +262 -0
- package/src/method/runtimeMethod.ts +130 -33
- package/src/runtime/MethodIdResolver.ts +83 -46
- package/src/runtime/Runtime.ts +193 -100
- package/src/runtime/RuntimeEnvironment.ts +16 -0
- package/src/runtime/RuntimeModule.ts +62 -30
- package/src/state/InMemoryStateService.ts +14 -18
- package/test/Runtime.test.ts +68 -42
- package/test/TestingRuntime.ts +43 -0
- package/test/messages/message.test.ts +42 -0
- package/test/method/MethodParameterEncoder.test.ts +124 -0
- package/test/method/runtimeMethod-fail.test.ts +53 -0
- package/{src/method/decorator.test.ts → test/method/runtimeMethod.test.ts} +3 -3
- package/test/modules/Admin.ts +4 -4
- package/test/modules/Balances.test.ts +92 -78
- package/test/modules/Balances.ts +15 -17
- package/test/modules/{methodId.test.ts → MethodIdResolver.test.ts} +24 -36
- package/test/modules/State.test.ts +81 -0
- package/test/runtimeMethod.test.ts +192 -20
- package/test/tsconfig.json +7 -0
- package/tsconfig.json +2 -2
- package/LICENSE.md +0 -201
- package/dist/method/MethodParameterDecoder.d.ts +0 -22
- package/dist/method/MethodParameterDecoder.d.ts.map +0 -1
- package/dist/method/MethodParameterDecoder.js +0 -33
- package/dist/method/RuntimeMethodExecutionContext.d.ts +0 -57
- package/dist/method/RuntimeMethodExecutionContext.d.ts.map +0 -1
- package/dist/method/RuntimeMethodExecutionContext.js +0 -92
- package/dist/method/assert.d.ts +0 -12
- package/dist/method/assert.d.ts.map +0 -1
- package/dist/method/assert.js +0 -23
- package/dist/state/State.d.ts +0 -65
- package/dist/state/State.d.ts.map +0 -1
- package/dist/state/State.js +0 -114
- package/dist/state/StateMap.d.ts +0 -37
- package/dist/state/StateMap.d.ts.map +0 -1
- package/dist/state/StateMap.js +0 -56
- package/dist/state/StateServiceProvider.d.ts +0 -10
- package/dist/state/StateServiceProvider.d.ts.map +0 -1
- package/dist/state/StateServiceProvider.js +0 -34
- package/src/method/MethodParameterDecoder.ts +0 -68
- package/src/method/RuntimeMethodExecutionContext.ts +0 -111
- package/src/method/assert.test.ts +0 -49
- package/src/method/assert.ts +0 -27
- package/src/state/State.ts +0 -154
- package/src/state/StateMap.ts +0 -69
- package/src/state/StateServiceProvider.ts +0 -24
- package/src/state/decorator.ts +0 -65
- package/test/state/MerkleTree.test.ts +0 -95
- package/test/state/MockAsyncMerkleStore.ts +0 -28
- package/test/transaction.test.ts +0 -82
- package/tsconfig.test.json +0 -9
package/src/state/State.ts
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
/* eslint-disable new-cap */
|
|
2
|
-
|
|
3
|
-
import { Mixin } from "ts-mixer";
|
|
4
|
-
import { Bool, Field, Provable, type FlexibleProvablePure } from "snarkyjs";
|
|
5
|
-
import { container } from "tsyringe";
|
|
6
|
-
import { Option, StateTransition, type Path } from "@proto-kit/protocol";
|
|
7
|
-
|
|
8
|
-
import { PartialRuntime } from "../runtime/RuntimeModule.js";
|
|
9
|
-
import { RuntimeMethodExecutionContext } from "../method/RuntimeMethodExecutionContext.js";
|
|
10
|
-
|
|
11
|
-
export class WithPath {
|
|
12
|
-
public path?: Field;
|
|
13
|
-
|
|
14
|
-
public hasPathOrFail(): asserts this is { path: Path } {
|
|
15
|
-
if (!this.path) {
|
|
16
|
-
throw new Error(
|
|
17
|
-
"Could not find 'path', did you forget to add '@state' to your state property?"
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class WithRuntime {
|
|
24
|
-
public runtime?: PartialRuntime;
|
|
25
|
-
|
|
26
|
-
public hasRuntimeOrFail(): asserts this is {
|
|
27
|
-
runtime: PartialRuntime;
|
|
28
|
-
} {
|
|
29
|
-
if (!this.runtime) {
|
|
30
|
-
throw new Error(
|
|
31
|
-
"Could not find 'runtime', did you forget to add '@state' to your state property?"
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Utilities for runtime module state, such as get/set
|
|
39
|
-
*/
|
|
40
|
-
export class State<Value> extends Mixin(WithPath, WithRuntime) {
|
|
41
|
-
/**
|
|
42
|
-
* Creates a new state wrapper for the provided value type.
|
|
43
|
-
*
|
|
44
|
-
* @param valueType - Type of value to be stored (e.g. UInt64, Struct, ...)
|
|
45
|
-
* @returns New state for the given value type.
|
|
46
|
-
*/
|
|
47
|
-
public static from<Value>(valueType: FlexibleProvablePure<Value>) {
|
|
48
|
-
return new State<Value>(valueType);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Computes a dummy value for the given value type.
|
|
53
|
-
*
|
|
54
|
-
* @param valueType - Value type to generate the dummy value for
|
|
55
|
-
* @returns Dummy value for the given value type
|
|
56
|
-
*/
|
|
57
|
-
public static dummyValue<Value>(
|
|
58
|
-
valueType: FlexibleProvablePure<Value>
|
|
59
|
-
): Value {
|
|
60
|
-
const length = valueType.sizeInFields();
|
|
61
|
-
const fields = Array.from({ length }, () => Field(0));
|
|
62
|
-
|
|
63
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
64
|
-
return valueType.fromFields(fields) as Value;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
public constructor(public valueType: FlexibleProvablePure<Value>) {
|
|
68
|
-
super();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Provides an in-circuit witness for the current state representation,
|
|
73
|
-
* and constructs an Option out of it.
|
|
74
|
-
*
|
|
75
|
-
* @returns Optional value of the current state
|
|
76
|
-
*/
|
|
77
|
-
private witnessState() {
|
|
78
|
-
// get the value from storage, or return a dummy value instead
|
|
79
|
-
const value = Provable.witness(this.valueType, () => {
|
|
80
|
-
this.hasRuntimeOrFail();
|
|
81
|
-
this.hasPathOrFail();
|
|
82
|
-
|
|
83
|
-
const fields = this.runtime.stateService.get(this.path);
|
|
84
|
-
if (fields) {
|
|
85
|
-
// eslint-disable-next-line max-len
|
|
86
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
87
|
-
return this.valueType.fromFields(fields) as Value;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return State.dummyValue(this.valueType);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// check if the value exists in the storage or not
|
|
94
|
-
const isSome = Provable.witness(Bool, () => {
|
|
95
|
-
this.hasRuntimeOrFail();
|
|
96
|
-
this.hasPathOrFail();
|
|
97
|
-
|
|
98
|
-
const fields = this.runtime.stateService.get(this.path);
|
|
99
|
-
|
|
100
|
-
return Bool(fields !== undefined);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
return Option.from(isSome, value, this.valueType);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Retrieves the current state and creates a state transition
|
|
108
|
-
* anchoring the use of the current state value in the circuit.
|
|
109
|
-
*
|
|
110
|
-
* @returns Option representation of the current state.
|
|
111
|
-
*/
|
|
112
|
-
public get(): Option<Value> {
|
|
113
|
-
const option = this.witnessState();
|
|
114
|
-
|
|
115
|
-
this.hasPathOrFail();
|
|
116
|
-
|
|
117
|
-
const stateTransition = StateTransition.from(this.path, option);
|
|
118
|
-
|
|
119
|
-
container
|
|
120
|
-
.resolve(RuntimeMethodExecutionContext)
|
|
121
|
-
.addStateTransition(stateTransition);
|
|
122
|
-
|
|
123
|
-
return option;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Sets a new state value by creating a state transition from
|
|
128
|
-
* the current value to the newly set value.
|
|
129
|
-
*
|
|
130
|
-
* The newly set value isn't available via state.get(), since the
|
|
131
|
-
* state transitions are not applied within the same circuit.
|
|
132
|
-
* You can however store and access your new value in
|
|
133
|
-
* a separate circuit variable.
|
|
134
|
-
*
|
|
135
|
-
* @param value - Value to be set as the current state
|
|
136
|
-
*/
|
|
137
|
-
public set(value: Value) {
|
|
138
|
-
// link the transition to the current state
|
|
139
|
-
const fromOption = this.witnessState();
|
|
140
|
-
const toOption = Option.from(Bool(true), value, this.valueType);
|
|
141
|
-
|
|
142
|
-
this.hasPathOrFail();
|
|
143
|
-
|
|
144
|
-
const stateTransition = StateTransition.fromTo(
|
|
145
|
-
this.path,
|
|
146
|
-
fromOption,
|
|
147
|
-
toOption
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
container
|
|
151
|
-
.resolve(RuntimeMethodExecutionContext)
|
|
152
|
-
.addStateTransition(stateTransition);
|
|
153
|
-
}
|
|
154
|
-
}
|
package/src/state/StateMap.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import type { Field, FlexibleProvablePure } from "snarkyjs";
|
|
2
|
-
import { Path, type Option } from "@proto-kit/protocol";
|
|
3
|
-
import { Mixin } from "ts-mixer";
|
|
4
|
-
|
|
5
|
-
import { State, WithRuntime, WithPath } from "./State.js";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Map-like wrapper for state
|
|
9
|
-
*/
|
|
10
|
-
// eslint-disable-next-line new-cap
|
|
11
|
-
export class StateMap<KeyType, ValueType> extends Mixin(WithPath, WithRuntime) {
|
|
12
|
-
/**
|
|
13
|
-
* Create a new state map with the given key and value types
|
|
14
|
-
*
|
|
15
|
-
* @param keyType - Type to be used as a key
|
|
16
|
-
* @param valueType - Type to be stored as a value
|
|
17
|
-
* @returns State map with provided key and value types.
|
|
18
|
-
*/
|
|
19
|
-
public static from<KeyType, ValueType>(
|
|
20
|
-
keyType: FlexibleProvablePure<KeyType>,
|
|
21
|
-
valueType: FlexibleProvablePure<ValueType>
|
|
22
|
-
) {
|
|
23
|
-
return new StateMap<KeyType, ValueType>(keyType, valueType);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public constructor(
|
|
27
|
-
public keyType: FlexibleProvablePure<KeyType>,
|
|
28
|
-
public valueType: FlexibleProvablePure<ValueType>
|
|
29
|
-
) {
|
|
30
|
-
super();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public getPath(key: KeyType): Field {
|
|
34
|
-
this.hasPathOrFail();
|
|
35
|
-
return Path.fromKey(this.path, this.keyType, key);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Obtains a value for the provided key in the current state map.
|
|
40
|
-
*
|
|
41
|
-
* @param key - Key to obtain the state for
|
|
42
|
-
* @returns Value for the provided key.
|
|
43
|
-
*/
|
|
44
|
-
public get(key: KeyType): Option<ValueType> {
|
|
45
|
-
const state = State.from(this.valueType);
|
|
46
|
-
this.hasPathOrFail();
|
|
47
|
-
this.hasRuntimeOrFail();
|
|
48
|
-
|
|
49
|
-
state.path = this.getPath(key);
|
|
50
|
-
state.runtime = this.runtime;
|
|
51
|
-
return state.get();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Sets a value for the given key in the current state map.
|
|
56
|
-
*
|
|
57
|
-
* @param key - Key to store the value under
|
|
58
|
-
* @param value - Value to be stored under the given key
|
|
59
|
-
*/
|
|
60
|
-
public set(key: KeyType, value: ValueType) {
|
|
61
|
-
const state = State.from(this.valueType);
|
|
62
|
-
this.hasPathOrFail();
|
|
63
|
-
this.hasRuntimeOrFail();
|
|
64
|
-
|
|
65
|
-
state.path = Path.fromKey(this.path, this.keyType, key);
|
|
66
|
-
state.runtime = this.runtime;
|
|
67
|
-
state.set(value);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { inject, injectable } from "tsyringe";
|
|
2
|
-
|
|
3
|
-
import { StateService } from "./InMemoryStateService";
|
|
4
|
-
|
|
5
|
-
@injectable()
|
|
6
|
-
export class StateServiceProvider {
|
|
7
|
-
private readonly defaultStateService: StateService = this.currentStateService;
|
|
8
|
-
|
|
9
|
-
public constructor(
|
|
10
|
-
@inject("StateService") private currentStateService: StateService
|
|
11
|
-
) {}
|
|
12
|
-
|
|
13
|
-
public get stateService(): StateService {
|
|
14
|
-
return this.currentStateService;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public setCurrentStateService(service: StateService) {
|
|
18
|
-
this.currentStateService = service;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public resetToDefault() {
|
|
22
|
-
this.currentStateService = this.defaultStateService;
|
|
23
|
-
}
|
|
24
|
-
}
|
package/src/state/decorator.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { Path } from "@proto-kit/protocol";
|
|
2
|
-
|
|
3
|
-
import type { RuntimeModule } from "../runtime/RuntimeModule.js";
|
|
4
|
-
|
|
5
|
-
import type { State } from "./State.js";
|
|
6
|
-
|
|
7
|
-
const errors = {
|
|
8
|
-
missingName: (className: string) =>
|
|
9
|
-
new Error(
|
|
10
|
-
`Unable to provide a unique identifier for state, ${className} is missing a name.
|
|
11
|
-
Did you forget to extend your runtime module with 'extends RuntimeModule'?`
|
|
12
|
-
),
|
|
13
|
-
|
|
14
|
-
missingRuntime: (className: string) =>
|
|
15
|
-
new Error(
|
|
16
|
-
`Unable to provide 'runtime' for state, ${className} is missing a name.
|
|
17
|
-
Did you forget to extend your runtime module with 'extends RuntimeModule'?`
|
|
18
|
-
),
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Decorates a runtime module property as state, passing down some
|
|
23
|
-
* underlying values to improve developer experience.
|
|
24
|
-
*/
|
|
25
|
-
export function state() {
|
|
26
|
-
return <TargetRuntimeModule extends RuntimeModule<unknown>>(
|
|
27
|
-
target: TargetRuntimeModule,
|
|
28
|
-
propertyKey: string
|
|
29
|
-
) => {
|
|
30
|
-
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
31
|
-
let value: State<unknown> | undefined;
|
|
32
|
-
|
|
33
|
-
Object.defineProperty(target, propertyKey, {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
|
|
36
|
-
get: function get() {
|
|
37
|
-
// eslint-disable-next-line max-len
|
|
38
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
39
|
-
const self = this as TargetRuntimeModule;
|
|
40
|
-
|
|
41
|
-
if (self.name === undefined) {
|
|
42
|
-
throw errors.missingName(self.constructor.name);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (!self.runtime) {
|
|
46
|
-
throw errors.missingRuntime(self.constructor.name);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const path = Path.fromProperty(self.name, propertyKey);
|
|
50
|
-
if (value) {
|
|
51
|
-
value.path = path;
|
|
52
|
-
// eslint-disable-next-line no-warning-comments
|
|
53
|
-
// TODO: why is this complaining about `any`?
|
|
54
|
-
|
|
55
|
-
value.runtime = self.runtime;
|
|
56
|
-
}
|
|
57
|
-
return value;
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
set: (newValue: State<unknown>) => {
|
|
61
|
-
value = newValue;
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
};
|
|
65
|
-
}
|
|
@@ -1,95 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,28 +0,0 @@
|
|
|
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
|
-
}
|
package/test/transaction.test.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import "reflect-metadata";
|
|
2
|
-
import { PrivateKey, Proof, PublicKey, UInt64 } from "snarkyjs";
|
|
3
|
-
import { container } from "tsyringe";
|
|
4
|
-
import {
|
|
5
|
-
InMemoryStateService,
|
|
6
|
-
MethodPublicOutput,
|
|
7
|
-
Runtime,
|
|
8
|
-
runtimeMethod,
|
|
9
|
-
RuntimeMethodExecutionContext,
|
|
10
|
-
} from "../src";
|
|
11
|
-
import { runtimeModule } from "../src/module/decorator";
|
|
12
|
-
import { RuntimeModule } from "../src/runtime/RuntimeModule";
|
|
13
|
-
import { state } from "../src/state/decorator";
|
|
14
|
-
import { StateMap } from "../src/state/StateMap";
|
|
15
|
-
|
|
16
|
-
interface BalancesConfig {}
|
|
17
|
-
@runtimeModule()
|
|
18
|
-
class Balances extends RuntimeModule<BalancesConfig> {
|
|
19
|
-
@state() public balances = StateMap.from<PublicKey, UInt64>(
|
|
20
|
-
PublicKey,
|
|
21
|
-
UInt64
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
@runtimeMethod()
|
|
25
|
-
public setBalance(to: PublicKey, amount: UInt64) {
|
|
26
|
-
this.balances.set(to, amount);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
@runtimeMethod()
|
|
30
|
-
public transfer(from: PublicKey, to: PublicKey, amount: UInt64) {
|
|
31
|
-
const fromBalance = this.balances.get(from);
|
|
32
|
-
const toBalance = this.balances.get(to);
|
|
33
|
-
|
|
34
|
-
this.balances.set(from, fromBalance.value.sub(amount));
|
|
35
|
-
this.balances.set(to, toBalance.value.add(amount));
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
describe("transaction", () => {
|
|
40
|
-
it("should update the state", async () => {
|
|
41
|
-
expect.assertions(0);
|
|
42
|
-
|
|
43
|
-
const runtime = Runtime.from({
|
|
44
|
-
modules: {
|
|
45
|
-
Balances,
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
config: {
|
|
49
|
-
Balances: {},
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
state: new InMemoryStateService(),
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
const alice = PrivateKey.random().toPublicKey();
|
|
56
|
-
const bob = PrivateKey.random().toPublicKey();
|
|
57
|
-
|
|
58
|
-
const balances = runtime.resolve("Balances");
|
|
59
|
-
|
|
60
|
-
runtime.transaction(() => {
|
|
61
|
-
balances.setBalance(alice, UInt64.from(1000));
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
runtime.transaction(() => {
|
|
65
|
-
balances.transfer(alice, bob, UInt64.from(100));
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
const executionContext = container.resolve<RuntimeMethodExecutionContext>(
|
|
69
|
-
RuntimeMethodExecutionContext
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
const balanceAlice = balances.balances.get(alice);
|
|
73
|
-
const balanceBob = balances.balances.get(bob);
|
|
74
|
-
|
|
75
|
-
console.log("balances", {
|
|
76
|
-
// 900
|
|
77
|
-
alice: balanceAlice.value.toString(),
|
|
78
|
-
// 100
|
|
79
|
-
bob: balanceBob.value.toString(),
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
});
|