-
Notifications
You must be signed in to change notification settings - Fork 6
/
counter.test.ts
128 lines (101 loc) · 3.82 KB
/
counter.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
/* eslint-disable max-statements */
/* eslint-disable jest/consistent-test-it */
/* eslint-disable no-console */
/* eslint-disable jest/require-top-level-describe */
import { AccountUpdate, UInt64 } from 'snarkyjs';
import Counter from './counter.js';
import describeContract, { withTimer } from './describeContract.js';
Error.stackTraceLimit = 10_000_000;
// eslint-disable-next-line jest/require-hook
describeContract<Counter>('counter', Counter, (context) => {
async function localDeploy() {
const {
deployerAccount,
deployerKey,
zkAppPrivateKey,
zkApp,
contractApi,
} = context();
const tx = await withTimer(
'transaction',
async () =>
await contractApi.transaction(zkApp, deployerAccount, () => {
AccountUpdate.fundNewAccount(deployerAccount);
zkApp.deploy();
})
);
await withTimer(
'prove',
async () => await contractApi.prove(zkApp, async () => await tx.prove())
);
// this tx needs .sign(), because `deploy()` adds an account update
// that requires signature authorization
await tx.sign([deployerKey, zkAppPrivateKey]).send();
contractApi.restoreLatest(zkApp);
return tx;
}
it('correctly updates the count state on the `Counter` smart contract', async () => {
expect.assertions(2);
const { senderAccount, senderKey, zkApp, contractApi } = context();
const tx0 = await localDeploy();
console.log('Counter.deploy() successful, initial offchain state:', {
offchainStateRootHash: zkApp.offchainStateRootHash.get().toString(),
data: zkApp.virtualStorage.data[zkApp.address.toBase58()],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
tx: tx0.toPretty(),
});
console.log('Counter.update(), updating the offchain state...');
// update transaction
const tx1 = await withTimer(
'transaction',
async () =>
await contractApi.transaction(zkApp, senderAccount, () => {
zkApp.update();
})
);
console.log('Counter.update(), proving tx1');
await withTimer(
'prove',
async () => await contractApi.prove(zkApp, async () => await tx1.prove())
);
await tx1.sign([senderKey]).send();
contractApi.restoreLatest(zkApp);
const updatedCountOne = zkApp.count.get();
expect(updatedCountOne.toString()).toStrictEqual(UInt64.from(1).toString());
console.log('Counter.update() successful, new offchain state:', {
count: updatedCountOne.toString(),
offchainStateRootHash: zkApp.offchainStateRootHash.get().toString(),
data: zkApp.virtualStorage.data[zkApp.address.toBase58()],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
tx: tx1.toPretty(),
});
console.log(
'Counter.update() the second time, updating the offchain state...'
);
// update transaction
const tx2 = await withTimer(
'transaction',
async () =>
await contractApi.transaction(zkApp, senderAccount, () => {
console.log('running update');
zkApp.update();
})
);
console.log('Counter.update(), proving tx2');
await withTimer(
'prove',
async () => await contractApi.prove(zkApp, async () => await tx2.prove())
);
await tx2.sign([senderKey]).send();
contractApi.restoreLatest(zkApp);
const updatedCountTwo = zkApp.count.get();
expect(updatedCountTwo.toString()).toStrictEqual(UInt64.from(2).toString());
console.log('Counter.update() 2 successful, new offchain state:', {
count: updatedCountTwo.toString(),
offchainStateRootHash: zkApp.offchainStateRootHash.get().toString(),
data: zkApp.virtualStorage.data[zkApp.address.toBase58()],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
tx: tx2.toPretty(),
});
});
});