-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathStateTransition.test.ts
More file actions
176 lines (165 loc) · 5.41 KB
/
StateTransition.test.ts
File metadata and controls
176 lines (165 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* eslint etc/no-commented-out-code:
*/
import "reflect-metadata";
import { InMemoryMerkleTreeStorage } from "@proto-kit/common";
import { Bool, Field } from "o1js";
import { Option, ProvableStateTransition } from "../src/index";
import { RollupMerkleTree } from "../../common/src/trees/RollupMerkleTree.js";
// TODO Not worth fixing rn because we will revamp the STProver very soon
describe.skip("stateTransition", () => {
async function checkTransitions(
tree: RollupMerkleTree,
transitions: ProvableStateTransition[]
) {
// const batch = StateTransitionProvableBatch.fromTransitions(transitions, []);
//
// const temporaryTree = new RollupMerkleTree(
// new CachedMerkleTreeStore(tree.store)
// );
// const startRoot = temporaryTree.getRoot();
//
// const hashList = new DefaultProvableHashList(ProvableStateTransition);
//
// batch.batch.forEach((item) => {
// if (item.to.isSome.toBoolean()) {
// temporaryTree.setLeaf(item.path.toBigInt(), item.to.value);
// }
// hashList.push(item);
// });
//
// const endRoot = temporaryTree.getRoot();
//
// class DummySTWP implements StateTransitionWitnessProvider {
// private i = 0;
//
// public constructor(private readonly witnessTree: RollupMerkleTree) {}
//
// public getWitness(key: Field): RollupMerkleTreeWitness {
// const witness = this.witnessTree.getWitness(key.toBigInt());
// const set = batch.batch[this.i];
// if (set.to.isSome.toBoolean()) {
// this.witnessTree.setLeaf(key.toBigInt(), set.to.value);
// }
// this.i += 1;
// return witness;
// }
// }
//
// const childContainer = container.createChildContainer();
// childContainer.registerInstance(
// "StateTransitionWitnessProvider",
// new DummySTWP(tree)
// );
// const prover = childContainer.resolve(StateTransitionProver);
//
// const state = prover.applyTransitions(startRoot, Field(0), batch);
//
// expect(state.stateRoot).toStrictEqual(endRoot);
// expect(state.stateTransitionList.commitment).toStrictEqual(
// hashList.commitment
// );
//
// await childContainer.dispose();
}
it.each([
[
[
new ProvableStateTransition({
from: Option.fromValue(Field(1), Field).toProvable(),
to: Option.fromValue(Field(14), Field).toProvable(),
path: Field(1),
}),
new ProvableStateTransition({
from: Option.fromValue(Field(14), Field).toProvable(),
to: Option.fromValue(Field(4), Field).toProvable(),
path: Field(1),
}),
],
],
[
[
new ProvableStateTransition({
from: Option.none().toProvable(),
to: Option.from(Bool(true), Field(4), Field).toProvable(),
path: Field(1),
}),
new ProvableStateTransition({
from: Option.from(Bool(true), Field(5), Field).toProvable(),
to: Option.from(Bool(true), Field(2), Field).toProvable(),
path: Field(2),
}),
new ProvableStateTransition({
from: Option.from(Bool(true), Field(2), Field).toProvable(),
to: Option.none().toProvable(),
path: Field(2),
}),
],
],
])("should pass without throwing", async (transitions) => {
// expect.assertions(2);
const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
// Is ignored because overwritten by first transition
tree.setLeaf(1n, Option.fromValue(Field(1), Field).treeValue);
tree.setLeaf(2n, Option.fromValue(Field(5), Field).treeValue);
await checkTransitions(tree, transitions);
});
it.each([
[
[
new ProvableStateTransition({
// fail
from: Option.from(Bool(true), Field(2), Field).toProvable(),
to: Option.none().toProvable(),
path: Field(1),
}),
],
0,
],
[
[
new ProvableStateTransition({
// success
from: Option.from(Bool(true), Field(1), Field).toProvable(),
to: Option.from(Bool(true), Field(14), Field).toProvable(),
path: Field(1),
}),
new ProvableStateTransition({
// fail
from: Option.from(Bool(true), Field(6), Field).toProvable(),
to: Option.none().toProvable(),
path: Field(2),
}),
],
1,
],
[
[
new ProvableStateTransition({
// success
from: Option.from(Bool(true), Field(1), Field).toProvable(),
to: Option.from(Bool(true), Field(14), Field).toProvable(),
path: Field(1),
}),
new ProvableStateTransition({
// fail
from: Option.from(Bool(true), Field(15), Field).toProvable(),
to: Option.none().toProvable(),
path: Field(1),
}),
],
1,
],
])(
"should throw because of failing precondition",
async (transitions, index) => {
expect.assertions(1);
const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
// Is ignored because overwritten by first transition
tree.setLeaf(1n, Option.fromValue(Field(1), Field).treeValue);
tree.setLeaf(2n, Option.fromValue(Field(5), Field).treeValue);
await expect(checkTransitions(tree, transitions)).rejects.toThrow(
`MerkleWitness not valid for StateTransition (${index})`
);
}
);
});