-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolana_timelock.ts
493 lines (438 loc) · 16.5 KB
/
solana_timelock.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import * as anchor from "@coral-xyz/anchor";
const assert = require("assert");
describe("solana_timelock", () => {
const provider = anchor.getProvider();
anchor.setProvider(provider);
const program = anchor.workspace.SolanaTimelock;
let timelock: anchor.web3.Keypair;
let timelockSignerPubkey: anchor.web3.PublicKey;
let timelockAuthority: anchor.web3.Keypair;
it("Creates the timelock program", async () => {
timelock = anchor.web3.Keypair.generate();
let nonce: number;
[timelockSignerPubkey, nonce] =
anchor.web3.PublicKey.findProgramAddressSync(
[timelock.publicKey.toBuffer()],
program.programId
);
const timelockSize = 200; // Big enough.
const delayInSlots = new anchor.BN(1);
timelockAuthority = anchor.web3.Keypair.generate();
await program.methods
.createTimelock(timelockAuthority.publicKey, delayInSlots)
.accounts({
timelock: timelock.publicKey,
timelockSigner: timelockSignerPubkey,
})
.preInstructions([
await program.account.timelock.createInstruction(
timelock,
timelockSize
),
])
.signers([timelock])
.rpc();
let timelockAccount = await program.account.timelock.fetch(
timelock.publicKey
);
assert.strictEqual(timelockAccount.signerBump, nonce);
assert.ok(timelockAccount.delayInSlots.eq(delayInSlots));
});
let transactionBatchAuthority: anchor.web3.Keypair;
let transactionBatch: anchor.web3.Keypair;
it("Creates a transaction batch", async () => {
transactionBatchAuthority = anchor.web3.Keypair.generate();
transactionBatch = anchor.web3.Keypair.generate();
const transactionBatchSize = 30000; // 3kb ought to be enough
await program.methods
.createTransactionBatch()
.accounts({
transactionBatchAuthority: transactionBatchAuthority.publicKey,
timelock: timelock.publicKey,
transactionBatch: transactionBatch.publicKey,
})
.preInstructions([
await program.account.transactionBatch.createInstruction(
transactionBatch,
transactionBatchSize
),
])
.signers([transactionBatchAuthority, transactionBatch])
.rpc();
const transactionBatchAccount =
await program.account.transactionBatch.fetch(transactionBatch.publicKey);
// Assert the transaction batch has been created with the correct status and authority
assert.ok(
"created" in transactionBatchAccount.status,
"The batch status should still be 'Created' after creating transaction batch."
);
assert.ok(transactionBatchAccount.timelock.equals(timelock.publicKey));
assert.ok(
transactionBatchAccount.transactionBatchAuthority.equals(
transactionBatchAuthority.publicKey
)
);
});
let recipient: anchor.web3.Keypair;
it("Adds three transactions to the transaction batch", async () => {
// First set up a transfer sol instruction
recipient = anchor.web3.Keypair.generate();
await provider.connection.requestAirdrop(timelockSignerPubkey, 200_000_000);
let transferInstruction = anchor.web3.SystemProgram.transfer({
fromPubkey: timelockSignerPubkey,
toPubkey: recipient.publicKey,
lamports: 100_000_000,
});
await program.methods
.addTransaction(
transferInstruction.programId,
transferInstruction.keys.map((key) => ({
pubkey: key.pubkey,
isSigner: key.isSigner,
isWritable: key.isWritable,
})),
transferInstruction.data
)
.accounts({
transactionBatch: transactionBatch.publicKey,
transactionBatchAuthority: transactionBatchAuthority.publicKey,
})
.signers([transactionBatchAuthority])
.rpc();
// Next set the timelock delay
const newDelayInSlots = new anchor.BN(2);
let setDelayInSlotsInstruction = program.instruction.setDelayInSlots(
newDelayInSlots,
{
accounts: {
timelock: timelock.publicKey,
timelockSigner: timelockSignerPubkey,
},
}
);
await program.methods
.addTransaction(
setDelayInSlotsInstruction.programId,
setDelayInSlotsInstruction.keys.map((key) => ({
pubkey: key.pubkey,
isSigner: key.isSigner,
isWritable: key.isWritable,
})),
setDelayInSlotsInstruction.data
)
.accounts({
transactionBatch: transactionBatch.publicKey,
transactionBatchAuthority: transactionBatchAuthority.publicKey,
})
.signers([transactionBatchAuthority])
.rpc();
// Next change the authority
let setAuthorityInstruction = program.instruction.setAuthority(
recipient.publicKey,
{
accounts: {
timelock: timelock.publicKey,
timelockSigner: timelockSignerPubkey,
},
}
);
await program.methods
.addTransaction(
setAuthorityInstruction.programId,
setAuthorityInstruction.keys.map((key) => ({
pubkey: key.pubkey,
isSigner: key.isSigner,
isWritable: key.isWritable,
})),
setAuthorityInstruction.data
)
.accounts({
transactionBatch: transactionBatch.publicKey,
transactionBatchAuthority: transactionBatchAuthority.publicKey,
})
.signers([transactionBatchAuthority])
.rpc();
// Finally, assert that the transaction batch contains the three transactions
const transactionBatchAccount =
await program.account.transactionBatch.fetch(transactionBatch.publicKey);
assert.strictEqual(
transactionBatchAccount.transactions.length,
3,
"There should be three transactions in the batch."
);
assert.ok(
"created" in transactionBatchAccount.status,
"The batch status should still be 'Created' after adding transactions."
);
});
it("Seals the transaction batch", async () => {
await program.methods
.sealTransactionBatch()
.accounts({
transactionBatch: transactionBatch.publicKey,
transactionBatchAuthority: transactionBatchAuthority.publicKey,
})
.signers([transactionBatchAuthority])
.rpc();
const sealedTransactionBatch = await program.account.transactionBatch.fetch(
transactionBatch.publicKey
);
// Assert the transaction batch is now sealed
assert.ok(
"sealed" in sealedTransactionBatch.status,
"The batch status should be 'Sealed' after sealing."
);
});
it("Enqueues the transaction batch", async () => {
// Enqueue the transaction batch
await program.methods
.enqueueTransactionBatch()
.accounts({
transactionBatch: transactionBatch.publicKey,
authority: timelockAuthority.publicKey,
timelock: timelock.publicKey,
})
.signers([timelockAuthority])
.rpc();
// Fetch the updated transaction batch account to verify its status and enqueued slot
const enqueuedTransactionBatch =
await program.account.transactionBatch.fetch(transactionBatch.publicKey);
// Assert the transaction batch is now in TimelockStarted status
assert.ok(
"enqueued" in enqueuedTransactionBatch.status,
"The batch status should be 'Enqueued' after enqueueing."
);
// Assert the enqueued slot is set
assert.ok(
enqueuedTransactionBatch.enqueuedSlot > 0,
"The enqueued slot should be set and greater than 0."
);
});
it("Dummy transaction to move to the next slot", async () => {
const dummyTx = new anchor.web3.Transaction();
dummyTx.add(
anchor.web3.SystemProgram.transfer({
fromPubkey: provider.publicKey,
toPubkey: provider.publicKey,
lamports: 10,
})
);
await provider.sendAndConfirm(dummyTx);
await provider.sendAndConfirm(dummyTx);
// Fetch the current slot from the blockchain
const currentSlot = await provider.connection.getSlot();
// Fetch the transaction batch to get the enqueued slot and delay
const transactionBatchAccount =
await program.account.transactionBatch.fetch(transactionBatch.publicKey);
const enqueuedSlot = transactionBatchAccount.enqueuedSlot.toNumber();
const timelockAccount = await program.account.timelock.fetch(
timelock.publicKey
);
const delayInSlots = timelockAccount.delayInSlots.toNumber();
const expectedExecutionSlot = enqueuedSlot + delayInSlots;
// Check if the current slot is greater than the expected execution slot
assert.ok(
currentSlot > expectedExecutionSlot,
`The current slot (${currentSlot}) should be greater than the expected execution slot (${expectedExecutionSlot}).`
);
});
it("Execute the transfer sol transaction in the batch", async () => {
// First execution call - This will execute the first transaction in the batch
await program.methods
.executeTransactionBatch()
.accounts({
timelock: timelock.publicKey,
timelockSigner: timelockSignerPubkey,
transactionBatch: transactionBatch.publicKey,
})
.remainingAccounts([
// Add remaining accounts needed for the first transaction here (transfer SOL)
// This includes the from account (which should be the timelock signer), and the to account
{ pubkey: timelockSignerPubkey, isWritable: true, isSigner: false },
{ pubkey: recipient.publicKey, isWritable: true, isSigner: false },
{
pubkey: anchor.web3.SystemProgram.programId,
isWritable: false,
isSigner: false,
},
])
.rpc();
// Verification step
const transactionBatchAccount =
await program.account.transactionBatch.fetch(transactionBatch.publicKey);
// Check if the first transaction did execute
assert.strictEqual(
transactionBatchAccount.transactions[0].didExecute,
true,
"The transfer SOL transaction should have been executed."
);
// Verify recipient's balance
const recipientBalance = await provider.connection.getBalance(
recipient.publicKey
);
assert.strictEqual(
recipientBalance,
100_000_000, // This should match the lamports sent in the transaction
"The recipient's balance should be increased by the amount of lamports sent."
);
});
it("Executes the set delay in slots transaction in the batch", async () => {
// Execute the second transaction in the batch (set delay in slots)
await program.methods
.executeTransactionBatch()
.accounts({
timelock: timelock.publicKey,
timelockSigner: timelockSignerPubkey,
transactionBatch: transactionBatch.publicKey,
})
.remainingAccounts([
{ pubkey: timelockSignerPubkey, isWritable: false, isSigner: false },
{ pubkey: timelock.publicKey, isWritable: true, isSigner: false },
{ pubkey: program.programId, isWritable: false, isSigner: false },
])
.rpc();
// Verification step
const transactionBatchAccount =
await program.account.transactionBatch.fetch(transactionBatch.publicKey);
// Check if the second transaction did execute
assert.strictEqual(
transactionBatchAccount.transactions[1].didExecute,
true,
"The set delay in slots transaction should have been executed."
);
// Fetch the updated Timelock account to verify the delay has been modified
const updatedTimelockAccount = await program.account.timelock.fetch(
timelock.publicKey
);
// The new delay in slots set by the transaction
const newDelayInSlots = new anchor.BN(2); // Ensure this matches the value set in the transaction
// Verify the timelock delay was updated correctly
assert.ok(
updatedTimelockAccount.delayInSlots.eq(newDelayInSlots),
`The delay in slots should be updated to ${newDelayInSlots.toString()}`
);
});
it("Executes the change authority transaction and verifies the update", async () => {
// Execute the transaction batch to change the authority
await program.methods
.executeTransactionBatch()
.accounts({
timelock: timelock.publicKey,
timelockSigner: timelockSignerPubkey,
transactionBatch: transactionBatch.publicKey,
})
// Assuming the remaining accounts are correctly specified as required for the transaction execution
.remainingAccounts([
// Include necessary remaining accounts specific to this transaction
{ pubkey: timelockSignerPubkey, isWritable: false, isSigner: false },
{ pubkey: timelock.publicKey, isWritable: true, isSigner: false },
{ pubkey: program.programId, isWritable: false, isSigner: false },
])
.rpc();
// Fetch the updated TransactionBatch and Timelock account to verify changes
const updatedTransactionBatch =
await program.account.transactionBatch.fetch(transactionBatch.publicKey);
const updatedTimelockAccount = await program.account.timelock.fetch(
timelock.publicKey
);
// Check if the third transaction did execute
assert.strictEqual(
updatedTransactionBatch.transactions[2].didExecute,
true,
"The change authority transaction should have been executed."
);
// Verify the transaction batch status is 'Executed'
assert.ok(
"executed" in updatedTransactionBatch.status,
"The batch status should be 'Executed' after all transactions are processed."
);
// Verify the timelock authority was updated correctly
assert.ok(
updatedTimelockAccount.authority.equals(recipient.publicKey),
"The recipient should now be the authority of the timelock."
);
});
it("Creates, seals, enqueues, and then cancels a transaction batch", async () => {
// Step 1: Create Transaction Batch
transactionBatchAuthority = anchor.web3.Keypair.generate();
transactionBatch = anchor.web3.Keypair.generate();
const transactionBatchSize = 30000; // Adequate size for the transaction batch
await program.methods
.createTransactionBatch()
.accounts({
transactionBatchAuthority: transactionBatchAuthority.publicKey,
timelock: timelock.publicKey,
transactionBatch: transactionBatch.publicKey,
})
.preInstructions([
await program.account.transactionBatch.createInstruction(
transactionBatch,
transactionBatchSize
),
])
.signers([transactionBatchAuthority, transactionBatch])
.rpc();
// Verify the transaction batch has been created
let transactionBatchAccount = await program.account.transactionBatch.fetch(
transactionBatch.publicKey
);
assert.ok(
"created" in transactionBatchAccount.status,
"The transaction batch should be in 'Created' status after creation."
);
// Step 2: Seal the Transaction Batch
await program.methods
.sealTransactionBatch()
.accounts({
transactionBatch: transactionBatch.publicKey,
transactionBatchAuthority: transactionBatchAuthority.publicKey,
})
.signers([transactionBatchAuthority])
.rpc();
// Verify the transaction batch has been sealed
transactionBatchAccount = await program.account.transactionBatch.fetch(
transactionBatch.publicKey
);
assert.ok(
"sealed" in transactionBatchAccount.status,
"The transaction batch should be in 'Sealed' status after sealing."
);
// Step 3: Enqueue the Transaction Batch
// Recipient is the new authority of the timelock
await program.methods
.enqueueTransactionBatch()
.accounts({
transactionBatch: transactionBatch.publicKey,
authority: recipient.publicKey,
timelock: timelock.publicKey,
})
.signers([recipient])
.rpc();
// Verify the transaction batch has been enqueued
transactionBatchAccount = await program.account.transactionBatch.fetch(
transactionBatch.publicKey
);
assert.ok(
"enqueued" in transactionBatchAccount.status,
"The transaction batch should be in 'Enqueued' status after enqueueing."
);
// Step 4: Cancel the Transaction Batch
await program.methods
.cancelTransactionBatch()
.accounts({
transactionBatch: transactionBatch.publicKey,
authority: recipient.publicKey,
timelock: timelock.publicKey,
})
.signers([recipient])
.rpc();
// Verify the transaction batch has been cancelled
transactionBatchAccount = await program.account.transactionBatch.fetch(
transactionBatch.publicKey
);
assert.ok(
"cancelled" in transactionBatchAccount.status,
"The transaction batch should be in 'Cancelled' status after cancellation."
);
});
});