-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathloader.js
More file actions
399 lines (367 loc) · 11.8 KB
/
loader.js
File metadata and controls
399 lines (367 loc) · 11.8 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
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
const { Buffer } = require("buffer");
const BufferLayout = require("@solana/buffer-layout");
const {
PublicKey,
Transaction,
SYSVAR_RENT_PUBKEY,
SYSVAR_CLOCK_PUBKEY,
sendAndConfirmTransaction,
SystemProgram,
Account,
TransactionInstruction,
} = require("@solana/web3.js");
const UPGRADEABLE_BPF_LOADER_PROGRAM_ID = new PublicKey(
"BPFLoaderUpgradeab1e11111111111111111111111",
);
// Keep program chunks under PACKET_DATA_SIZE, leaving enough room for the
// rest of the Transaction fields
const CHUNK_SIZE = 900;
function encodeInstruction(data) {
const dataLayout = BufferLayout.union(BufferLayout.u32("tag"), null, "tag");
dataLayout.addVariant(0, BufferLayout.struct([]), "InitializeBuffer");
const write = BufferLayout.struct([
BufferLayout.u32("offset"),
BufferLayout.nu64("length"),
BufferLayout.seq(
BufferLayout.u8("byte"),
BufferLayout.offset(BufferLayout.u32(), -8),
"bytes",
),
]);
dataLayout.addVariant(1, write, "Write");
const deployWithMaxLen = BufferLayout.struct([
BufferLayout.nu64("max_data_len"),
]);
dataLayout.addVariant(2, deployWithMaxLen, "DeployWithMaxDataLen");
dataLayout.addVariant(3, BufferLayout.struct([]), "Upgrade");
dataLayout.addVariant(4, BufferLayout.struct([]), "SetAuthority");
dataLayout.addVariant(5, BufferLayout.struct([]), "Close");
const extendProgram = BufferLayout.struct([
BufferLayout.u32("additional_bytes"),
]);
dataLayout.addVariant(6, extendProgram, "ExtendProgram");
const extendProgramChecked = BufferLayout.struct([
BufferLayout.u32("additional_bytes"),
]);
dataLayout.addVariant(9, extendProgramChecked, "ExtendProgramChecked");
// UpgradeableLoaderInstruction tag + offset + chunk length + chunk data
const instructionBuffer = Buffer.alloc(4 + 4 + 8 + Loader.chunkSize);
const encodedSize = dataLayout.encode(data, instructionBuffer);
return instructionBuffer.slice(0, encodedSize);
}
/**
* Program loader interface
*/
class Loader {
/**
* Amount of program data placed in each load Transaction
*/
static chunkSize = CHUNK_SIZE;
/**
* Minimum number of signatures required to load a program not including
* retries
*
* Can be used to calculate transaction fees
*/
static getMinNumSignatures(dataLength) {
return (
// Add one for Finalize transaction
2 * // Every transaction requires two signatures (payer + program)
(Math.ceil(dataLength / Loader.chunkSize) +
1 + // Add one for Create transaction
1)
);
}
static async deploy(connection, payer, program, authority, data) {
const buffer = new Account();
await initBuffer(connection, payer, authority, data, buffer);
await produceWriteTransactions(
authority,
data,
buffer,
async (transaction, offset) => {
await sendAndConfirmTransaction(
connection,
transaction,
[payer, authority],
{
commitment: "confirmed",
},
);
console.log("write progress:", offset, "/", data.length);
},
);
console.log("buffer write complete");
await deployBuffer(connection, payer, program, authority, data, buffer);
return true;
}
static async deployAsync(connection, payer, program, authority, data) {
const buffer = new Account();
await initBuffer(connection, payer, authority, data, buffer);
const signatures_promises = [];
await produceWriteTransactions(
authority,
data,
buffer,
async (transaction, _offset) => {
signatures_promises.push(
connection.sendTransaction(transaction, [payer, authority], {
preflightCommitment: "processed",
}),
);
await Promise.resolve();
},
);
const signatures = await Promise.all(signatures_promises);
console.log("transactions were sent");
const confirmations = [];
for (const signature of signatures) {
confirmations.push(connection.confirmTransaction(signature, "confirmed"));
}
await Promise.all(confirmations);
console.log("transactions were confirmed;buffer write complete");
await deployBuffer(connection, payer, program, authority, data, buffer);
return true;
}
static async upgradeInstruction(program, buffer, spillAddress, authority) {
const [programDataKey, _nonce] = PublicKey.findProgramAddressSync(
[program.toBuffer()],
UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
);
return new TransactionInstruction({
keys: [
{ pubkey: programDataKey, isSigner: false, isWritable: true },
{ pubkey: program, isSigner: false, isWritable: true },
{ pubkey: buffer, isSigner: false, isWritable: true },
{ pubkey: spillAddress, isSigner: false, isWritable: true },
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: authority, isSigner: true, isWritable: false },
],
programId: UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
data: encodeInstruction({ Upgrade: {} }),
});
}
static async setUpgradeAuthorityInstruction(
program,
currentAuthority,
newAuthority,
) {
const [programDataKey, _nonce] = PublicKey.findProgramAddressSync(
[program.toBuffer()],
UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
);
return new TransactionInstruction({
keys: [
{ pubkey: programDataKey, isSigner: false, isWritable: true },
{ pubkey: currentAuthority, isSigner: true, isWritable: false },
{ pubkey: newAuthority, isSigner: false, isWritable: false },
],
programId: UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
data: encodeInstruction({ SetAuthority: {} }),
});
}
static async extendProgramInstruction(program, payer, additionalBytes) {
const [programDataKey, _nonce] = PublicKey.findProgramAddressSync(
[program.toBuffer()],
UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
);
return new TransactionInstruction({
keys: [
{ pubkey: programDataKey, isSigner: false, isWritable: true },
{ pubkey: program, isSigner: false, isWritable: true },
{
pubkey: SystemProgram.programId,
isSigner: false,
isWritable: false,
},
{ pubkey: payer, isSigner: true, isWritable: true },
],
programId: UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
data: encodeInstruction({
ExtendProgram: { additional_bytes: additionalBytes },
}),
});
}
static async extendProgramCheckedInstruction(
program,
authority,
payer,
additionalBytes,
) {
const [programDataKey, _nonce] = PublicKey.findProgramAddressSync(
[program.toBuffer()],
UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
);
return new TransactionInstruction({
keys: [
{ pubkey: programDataKey, isSigner: false, isWritable: true },
{ pubkey: program, isSigner: false, isWritable: true },
{ pubkey: authority, isSigner: true, isWritable: false },
{
pubkey: SystemProgram.programId,
isSigner: false,
isWritable: false,
},
{ pubkey: payer, isSigner: true, isWritable: true },
],
programId: UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
data: encodeInstruction({
ExtendProgramChecked: { additional_bytes: additionalBytes },
}),
});
}
static async getDeployBufferTransaction(
connection,
payer,
data,
program,
bufferAccount,
authority,
) {
const programSpace = 36; // UpgradeableLoaderState::program_len()
const programBalanceNeeded =
await connection.getMinimumBalanceForRentExemption(programSpace);
const [programDataKey, _nonce] = PublicKey.findProgramAddressSync(
[program.toBuffer()],
UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
);
const deployTransaction = new Transaction()
.add(
SystemProgram.createAccount({
fromPubkey: payer,
newAccountPubkey: program,
lamports: programBalanceNeeded,
space: programSpace,
programId: UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
}),
)
.add(
new TransactionInstruction({
keys: [
{ pubkey: payer, isSigner: true, isWritable: true },
{ pubkey: programDataKey, isSigner: false, isWritable: true },
{ pubkey: program, isSigner: false, isWritable: true },
{
pubkey: bufferAccount,
isSigner: false,
isWritable: true,
},
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
{
pubkey: SystemProgram.programId,
isSigner: false,
isWritable: false,
},
{ pubkey: authority, isSigner: true, isWritable: false },
],
programId: UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
data: encodeInstruction({
DeployWithMaxDataLen: { max_data_len: data.length * 3 },
}),
}),
);
return deployTransaction;
}
}
async function initBuffer(connection, payer, authority, data, bufferAccount) {
console.log("buffer account", bufferAccount.publicKey.toBase58());
console.log("authority account", authority.publicKey.toBase58());
// UpgradeableLoaderState::buffer_len(program_len) = 37 + program_len
const bufferSpace = 37 + data.length;
const balanceNeeded =
await connection.getMinimumBalanceForRentExemption(bufferSpace);
const initTransaction = new Transaction()
.add(
SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: bufferAccount.publicKey,
lamports: balanceNeeded,
space: bufferSpace,
programId: UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
}),
)
.add(
new TransactionInstruction({
keys: [
{
pubkey: bufferAccount.publicKey,
isSigner: false,
isWritable: true,
},
{ pubkey: authority.publicKey, isSigner: false, isWritable: false },
],
programId: UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
data: encodeInstruction({ InitializeBuffer: {} }),
}),
);
await sendAndConfirmTransaction(
connection,
initTransaction,
[payer, bufferAccount],
{
commitment: "confirmed",
},
);
console.log("program buffer initialized");
}
// transactionProcessor takes produced transactions and perform operations on them.
// For example async deploy takes and stores then in an array,
// while sync deploy awaits for confirmations on them serially.
async function produceWriteTransactions(
authority,
data,
bufferAccount,
transactionProcessor,
) {
const chunkSize = Loader.chunkSize;
let offset = 0;
let array = data;
while (array.length > 0) {
const bytes = array.slice(0, Loader.chunkSize);
const transaction = new Transaction().add({
keys: [
{ pubkey: bufferAccount.publicKey, isSigner: false, isWritable: true },
{ pubkey: authority.publicKey, isSigner: true, isWritable: false },
],
programId: UPGRADEABLE_BPF_LOADER_PROGRAM_ID,
data: encodeInstruction({
Write: {
offset,
bytes,
},
}),
});
await transactionProcessor(transaction, offset);
offset += chunkSize;
array = array.slice(chunkSize);
}
console.log("buffer write complete");
}
async function deployBuffer(
connection,
payer,
program,
authority,
data,
bufferAccount,
) {
const deployTransaction = await Loader.getDeployBufferTransaction(
connection,
payer.publicKey,
data,
program.publicKey,
bufferAccount.publicKey,
authority.publicKey,
);
await sendAndConfirmTransaction(
connection,
deployTransaction,
[payer, program, authority],
{
commitment: "confirmed",
},
);
}
module.exports = Loader;