forked from ensdomains/evmgateway
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathproofs.test.ts
executable file
·237 lines (213 loc) · 7.88 KB
/
proofs.test.ts
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { EthProver } from '../../src/eth/EthProver.js';
import { GatewayRequest } from '../../src/vm.js';
import { toPaddedHex } from '../../src/utils.js';
import { ZeroHash, ZeroAddress } from 'ethers/constants';
import { Foundry } from '@adraffy/blocksmith';
import { afterAll, test, expect } from 'bun:test';
import { describe } from '../bun-describe-fix.js';
describe('proofs', async () => {
const foundry = await Foundry.launch({ infoLog: false });
afterAll(foundry.shutdown);
const contract = await foundry.deploy(`
contract C {
uint256 value1 = 1;
uint256 value2 = 2;
}
`);
let fetchedCalls: number;
let fetchedSlots: number;
resetStats();
function resetStats() {
fetchedCalls = 0;
fetchedSlots = 0;
}
foundry.provider.on('debug', (e) => {
if (e.action === 'sendRpcPayload' && e.payload.method === 'eth_getProof') {
fetchedCalls++;
fetchedSlots += e.payload.params[1].length;
}
});
test(`getStorage: slow / dne`, async () => {
const prover = await EthProver.latest(foundry.provider);
const value = await prover.getStorage(ZeroAddress, 0n, false);
expect(value).toEqual(ZeroHash);
});
test(`getStorage: fast / dne`, async () => {
const prover = await EthProver.latest(foundry.provider);
const value = await prover.getStorage(ZeroAddress, 0n, true);
expect(value).toEqual(ZeroHash);
});
test(`getStorage: slow / eoa`, async () => {
const prover = await EthProver.latest(foundry.provider);
const { address } = foundry.wallets.admin;
const value = await prover.getStorage(address, 0n, false);
expect(value).toEqual(ZeroHash);
});
test(`getStorage: fast / eoa`, async () => {
const prover = await EthProver.latest(foundry.provider);
const { address } = foundry.wallets.admin;
const value = await prover.getStorage(address, 0n, true);
expect(value).toEqual(ZeroHash);
});
test(`getStorage: slow / contract`, async () => {
const prover = await EthProver.latest(foundry.provider);
const value = await prover.getStorage(contract.target, 0n, false);
expect(value).toEqual(toPaddedHex(1));
});
test(`getStorage: fast / contract`, async () => {
const prover = await EthProver.latest(foundry.provider);
const value = await prover.getStorage(contract.target, 0n, true);
expect(value).toEqual(toPaddedHex(1));
});
test('reconstruction: empty', async () => {
const prover = await EthProver.latest(foundry.provider);
const p0 = await prover.fetchProofs(contract.target);
const p1 = await prover.getProofs(contract.target);
expect(p0).toEqual(p1);
});
test('reconstruction: 1 slot', async () => {
const prover = await EthProver.latest(foundry.provider);
const slots = [0n];
const p0 = await prover.fetchProofs(contract.target, slots);
const p1 = await prover.getProofs(contract.target, slots);
expect(p0).toEqual(p1);
});
test('reconstruction: 3 slot scrambled', async () => {
const prover = await EthProver.latest(foundry.provider);
const slots = [2n, 0n, 1n];
const p0 = await prover.fetchProofs(contract.target, slots);
const p1 = await prover.getProofs(contract.target, slots);
expect(p0).toEqual(p1);
});
test('reconstruction: batched', async () => {
const prover = await EthProver.latest(foundry.provider);
const slots = [0n, 1n];
const p0 = await prover.fetchProofs(contract.target, slots);
prover.proofBatchSize = 1;
const p1 = await prover.fetchProofs(contract.target, slots);
expect(p0).toEqual(p1);
});
test('reconstruction: batched cached', async () => {
const prover = await EthProver.latest(foundry.provider);
const slots = [0n, 1n];
const p0 = await prover.getProofs(contract.target, slots);
resetStats();
prover.proofBatchSize = 1;
const p1 = await prover.getProofs(contract.target, slots);
expect(fetchedCalls).toEqual(0);
expect(p0).toEqual(p1);
});
test('fetchProofs() batch = 1', async () => {
const prover = await EthProver.latest(foundry.provider);
resetStats();
prover.proofBatchSize = 1;
await prover.fetchProofs(contract.target, [0n, 1n]);
expect(fetchedCalls).toEqual(2);
});
test('fetchProofs() batch > 1', async () => {
const prover = await EthProver.latest(foundry.provider);
resetStats();
await prover.fetchProofs(contract.target, [0n, 1n]);
expect(fetchedCalls).toEqual(1);
});
test('getProof() 01:10', async () => {
const prover = await EthProver.latest(foundry.provider);
resetStats();
const [p0, p1] = await Promise.all([
prover.getProofs(contract.target, [0n, 1n]),
prover.getProofs(contract.target, [1n, 0n]),
]);
expect(fetchedCalls).toEqual(1);
expect(fetchedSlots).toEqual(2);
expect(p0.storageProof[0]).toEqual(p1.storageProof[1]);
expect(p0.storageProof[1]).toEqual(p1.storageProof[0]);
});
test('getProof() 01:12:02', async () => {
const prover = await EthProver.latest(foundry.provider);
resetStats();
const [p0, p1, p2] = await Promise.all([
prover.getProofs(contract.target, [0n, 1n]),
prover.getProofs(contract.target, [1n, 2n]),
prover.getProofs(contract.target, [2n, 0n]),
]);
expect(fetchedCalls).toEqual(2);
expect(fetchedSlots).toEqual(3);
expect(p0.storageProof[0]).toEqual(p2.storageProof[1]); // 0
expect(p0.storageProof[1]).toEqual(p1.storageProof[0]); // 1
expect(p1.storageProof[1]).toEqual(p2.storageProof[0]); // 2
});
test('getProof() 012345:012:345', async () => {
const prover = await EthProver.latest(foundry.provider);
resetStats();
const [p0, p1, p2] = await Promise.all([
prover.getProofs(contract.target, [0n, 1n, 2n, 3n, 4n, 5n]),
prover.getProofs(contract.target, [0n, 1n, 2n]),
prover.getProofs(contract.target, [3n, 4n, 5n]),
]);
expect(fetchedCalls).toEqual(1);
expect(fetchedSlots).toEqual(6);
expect(p0.storageProof).toEqual(p1.storageProof.concat(p2.storageProof));
});
async function requireV1(req: GatewayRequest, required: boolean) {
const prover = await EthProver.latest(foundry.provider);
const state = await prover.evalRequest(req);
if (required) {
expect(prover.proveV1(state.needs)).resolves.toBeDefined();
} else {
expect(prover.proveV1(state.needs)).rejects.toThrow(/must be storage/);
}
}
test('single target() is V1 compat', async () => {
await requireV1(
new GatewayRequest().setTarget(contract.target).read(),
true
);
});
test('multi target() is not V1 compat', async () => {
await requireV1(
new GatewayRequest().push(1).target().push(2).target(),
false
);
});
test('readHashedBytes() is not V1 compat', async () => {
await requireV1(
new GatewayRequest().setTarget(contract.target).push(0).readHashedBytes(),
false
);
});
async function requireProofs(req: GatewayRequest, required: boolean) {
const prover = await EthProver.latest(foundry.provider);
const state = await prover.evalRequest(req);
const proofSeq = await prover.prove(state.needs);
expect(proofSeq.proofs.every((x) => x !== '0x')).toBe(required);
}
/*
test('read() requires proof', async () => {
await requireProofs(
new GatewayRequest().setTarget(contract.target).read(),
true
);
});
test('isContract() requires proof', async () => {
await requireProofs(
new GatewayRequest().setTarget(contract.target).isContract(),
true
);
});
test('target() w/o access requires no proof', async () => {
await requireProofs(
new GatewayRequest().push(1).target().push(2).target(),
false
);
});
test('getTarget() requires no proof', async () => {
await requireProofs(
new GatewayRequest().setTarget(contract.target).getTarget(),
false
);
});
*/
test('setTarget() requires proof', async () => {
await requireProofs(new GatewayRequest().setTarget(contract.target), true);
});
});