2018-10-17 09:35:24 -07:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as BufferLayout from 'buffer-layout';
|
|
|
|
|
2018-10-26 13:19:47 -07:00
|
|
|
import {Account} from './account';
|
|
|
|
import {PublicKey} from './publickey';
|
2019-05-28 15:47:14 -07:00
|
|
|
import {Transaction, PACKET_DATA_SIZE} from './transaction';
|
2019-12-23 10:46:49 -08:00
|
|
|
import {SYSVAR_RENT_PUBKEY} from './sysvar';
|
2018-10-17 09:35:24 -07:00
|
|
|
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
|
2019-03-05 09:53:56 -08:00
|
|
|
import {sleep} from './util/sleep';
|
2018-10-26 13:19:47 -07:00
|
|
|
import type {Connection} from './connection';
|
2019-05-08 09:33:04 -07:00
|
|
|
import {SystemProgram} from './system-program';
|
2018-10-17 09:35:24 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Program loader interface
|
|
|
|
*/
|
|
|
|
export class Loader {
|
2018-11-27 08:31:06 -08:00
|
|
|
/**
|
|
|
|
* Amount of program data placed in each load Transaction
|
|
|
|
*/
|
|
|
|
static get chunkSize(): number {
|
2019-05-28 15:47:14 -07:00
|
|
|
// Keep program chunks under PACKET_DATA_SIZE, leaving enough room for the
|
|
|
|
// rest of the Transaction fields
|
|
|
|
//
|
|
|
|
// TODO: replace 300 with a proper constant for the size of the other
|
|
|
|
// Transaction fields
|
|
|
|
return PACKET_DATA_SIZE - 300;
|
2018-11-27 08:31:06 -08:00
|
|
|
}
|
|
|
|
|
2019-10-22 15:16:12 -07:00
|
|
|
/**
|
|
|
|
* Minimum number of signatures required to load a program not including
|
|
|
|
* retries
|
|
|
|
*
|
|
|
|
* Can be used to calculate transaction fees
|
|
|
|
*/
|
|
|
|
static getMinNumSignatures(dataLength: number): number {
|
|
|
|
return Math.ceil(dataLength / Loader.chunkSize);
|
|
|
|
}
|
|
|
|
|
2018-10-17 09:35:24 -07:00
|
|
|
/**
|
2019-05-08 09:33:04 -07:00
|
|
|
* Loads a generic program
|
|
|
|
*
|
2018-10-17 09:35:24 -07:00
|
|
|
* @param connection The connection to use
|
2019-05-08 09:33:04 -07:00
|
|
|
* @param payer System account that pays to load the program
|
|
|
|
* @param program Account to load the program into
|
2018-10-17 09:35:24 -07:00
|
|
|
* @param programId Public key that identifies the loader
|
2019-05-08 09:33:04 -07:00
|
|
|
* @param data Program octets
|
2020-09-23 07:54:27 -07:00
|
|
|
* @return true if program was loaded successfully, false if program was already loaded
|
2018-10-17 09:35:24 -07:00
|
|
|
*/
|
2019-05-08 09:33:04 -07:00
|
|
|
static async load(
|
|
|
|
connection: Connection,
|
|
|
|
payer: Account,
|
|
|
|
program: Account,
|
|
|
|
programId: PublicKey,
|
2020-02-12 16:25:22 -08:00
|
|
|
data: Buffer | Uint8Array | Array<number>,
|
2020-09-23 07:54:27 -07:00
|
|
|
): Promise<boolean> {
|
2019-05-08 09:33:04 -07:00
|
|
|
{
|
2019-10-19 11:36:06 -07:00
|
|
|
const balanceNeeded = await connection.getMinimumBalanceForRentExemption(
|
|
|
|
data.length,
|
|
|
|
);
|
2020-09-23 07:54:27 -07:00
|
|
|
|
|
|
|
// Fetch program account info to check if it has already been created
|
|
|
|
const programInfo = await connection.getAccountInfo(
|
|
|
|
program.publicKey,
|
|
|
|
'single',
|
2020-05-20 02:13:21 -07:00
|
|
|
);
|
2020-09-23 07:54:27 -07:00
|
|
|
|
|
|
|
let transaction: Transaction | null = null;
|
|
|
|
if (programInfo !== null) {
|
|
|
|
if (programInfo.executable) {
|
|
|
|
console.error('Program load failed, account is already executable');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (programInfo.data.length !== data.length) {
|
|
|
|
transaction = transaction || new Transaction();
|
|
|
|
transaction.add(
|
|
|
|
SystemProgram.allocate({
|
|
|
|
accountPubkey: program.publicKey,
|
|
|
|
space: data.length,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!programInfo.owner.equals(programId)) {
|
|
|
|
transaction = transaction || new Transaction();
|
|
|
|
transaction.add(
|
|
|
|
SystemProgram.assign({
|
|
|
|
accountPubkey: program.publicKey,
|
|
|
|
programId,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (programInfo.lamports < balanceNeeded) {
|
|
|
|
transaction = transaction || new Transaction();
|
|
|
|
transaction.add(
|
|
|
|
SystemProgram.transfer({
|
|
|
|
fromPubkey: payer.publicKey,
|
|
|
|
toPubkey: program.publicKey,
|
|
|
|
lamports: balanceNeeded - programInfo.lamports,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
transaction = new Transaction().add(
|
|
|
|
SystemProgram.createAccount({
|
|
|
|
fromPubkey: payer.publicKey,
|
|
|
|
newAccountPubkey: program.publicKey,
|
|
|
|
lamports: balanceNeeded > 0 ? balanceNeeded : 1,
|
|
|
|
space: data.length,
|
|
|
|
programId,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the account is already created correctly, skip this step
|
|
|
|
// and proceed directly to loading instructions
|
|
|
|
if (transaction !== null) {
|
|
|
|
await sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
transaction,
|
|
|
|
[payer, program],
|
|
|
|
{
|
|
|
|
commitment: 'single',
|
|
|
|
skipPreflight: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2019-05-08 09:33:04 -07:00
|
|
|
}
|
2018-10-17 09:35:24 -07:00
|
|
|
|
2019-03-14 13:27:47 -07:00
|
|
|
const dataLayout = BufferLayout.struct([
|
2018-10-17 09:35:24 -07:00
|
|
|
BufferLayout.u32('instruction'),
|
|
|
|
BufferLayout.u32('offset'),
|
|
|
|
BufferLayout.u32('bytesLength'),
|
|
|
|
BufferLayout.u32('bytesLengthPadding'),
|
|
|
|
BufferLayout.seq(
|
|
|
|
BufferLayout.u8('byte'),
|
2018-11-04 11:41:21 -08:00
|
|
|
BufferLayout.offset(BufferLayout.u32(), -8),
|
|
|
|
'bytes',
|
|
|
|
),
|
2018-10-17 09:35:24 -07:00
|
|
|
]);
|
|
|
|
|
2018-11-27 08:31:06 -08:00
|
|
|
const chunkSize = Loader.chunkSize;
|
2018-10-23 20:56:54 -07:00
|
|
|
let offset = 0;
|
|
|
|
let array = data;
|
2018-11-01 20:35:19 -07:00
|
|
|
let transactions = [];
|
2018-10-23 20:56:54 -07:00
|
|
|
while (array.length > 0) {
|
|
|
|
const bytes = array.slice(0, chunkSize);
|
2019-03-14 13:27:47 -07:00
|
|
|
const data = Buffer.alloc(chunkSize + 16);
|
|
|
|
dataLayout.encode(
|
2018-10-23 20:56:54 -07:00
|
|
|
{
|
|
|
|
instruction: 0, // Load instruction
|
|
|
|
offset,
|
|
|
|
bytes,
|
|
|
|
},
|
2019-03-14 13:27:47 -07:00
|
|
|
data,
|
2018-10-23 20:56:54 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
const transaction = new Transaction().add({
|
2019-11-06 09:42:01 -08:00
|
|
|
keys: [{pubkey: program.publicKey, isSigner: true, isWritable: true}],
|
2019-05-08 09:33:04 -07:00
|
|
|
programId,
|
2019-03-14 13:27:47 -07:00
|
|
|
data,
|
2018-10-23 20:56:54 -07:00
|
|
|
});
|
2018-11-04 11:41:21 -08:00
|
|
|
transactions.push(
|
2020-06-03 04:55:42 -07:00
|
|
|
sendAndConfirmTransaction(connection, transaction, [payer, program], {
|
2020-09-07 22:12:47 -07:00
|
|
|
commitment: 'single',
|
2020-06-03 04:55:42 -07:00
|
|
|
skipPreflight: true,
|
|
|
|
}),
|
2018-11-04 11:41:21 -08:00
|
|
|
);
|
2018-10-23 20:56:54 -07:00
|
|
|
|
2020-09-07 22:12:47 -07:00
|
|
|
// Delay between sends in an attempt to reduce rate limit errors
|
|
|
|
const REQUESTS_PER_SECOND = 4;
|
|
|
|
await sleep(1000 / REQUESTS_PER_SECOND);
|
2019-03-05 09:53:56 -08:00
|
|
|
|
2018-11-01 20:35:19 -07:00
|
|
|
// Run up to 8 Loads in parallel to prevent too many parallel transactions from
|
2020-09-07 22:12:47 -07:00
|
|
|
// getting retried due to AccountInUse errors.
|
2018-11-01 20:35:19 -07:00
|
|
|
//
|
|
|
|
// TODO: 8 was selected empirically and should probably be revisited
|
|
|
|
if (transactions.length === 8) {
|
|
|
|
await Promise.all(transactions);
|
|
|
|
transactions = [];
|
|
|
|
}
|
|
|
|
|
2018-10-23 20:56:54 -07:00
|
|
|
offset += chunkSize;
|
|
|
|
array = array.slice(chunkSize);
|
|
|
|
}
|
2018-10-24 09:21:49 -07:00
|
|
|
await Promise.all(transactions);
|
2018-10-17 09:35:24 -07:00
|
|
|
|
2019-05-08 09:33:04 -07:00
|
|
|
// Finalize the account loaded with program data for execution
|
|
|
|
{
|
|
|
|
const dataLayout = BufferLayout.struct([BufferLayout.u32('instruction')]);
|
2018-10-17 09:35:24 -07:00
|
|
|
|
2019-05-08 09:33:04 -07:00
|
|
|
const data = Buffer.alloc(dataLayout.span);
|
|
|
|
dataLayout.encode(
|
|
|
|
{
|
|
|
|
instruction: 1, // Finalize instruction
|
|
|
|
},
|
|
|
|
data,
|
|
|
|
);
|
2018-10-17 09:35:24 -07:00
|
|
|
|
2019-05-08 09:33:04 -07:00
|
|
|
const transaction = new Transaction().add({
|
2019-10-03 14:21:24 -07:00
|
|
|
keys: [
|
2019-11-06 09:42:01 -08:00
|
|
|
{pubkey: program.publicKey, isSigner: true, isWritable: true},
|
|
|
|
{pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false},
|
2019-10-03 14:21:24 -07:00
|
|
|
],
|
2019-05-08 09:33:04 -07:00
|
|
|
programId,
|
|
|
|
data,
|
|
|
|
});
|
2020-05-20 02:13:21 -07:00
|
|
|
await sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
transaction,
|
|
|
|
[payer, program],
|
2020-06-03 04:55:42 -07:00
|
|
|
{
|
2020-09-07 22:12:47 -07:00
|
|
|
commitment: 'single',
|
2020-06-03 04:55:42 -07:00
|
|
|
skipPreflight: true,
|
|
|
|
},
|
2020-05-20 02:13:21 -07:00
|
|
|
);
|
2019-05-08 09:33:04 -07:00
|
|
|
}
|
2020-09-23 07:54:27 -07:00
|
|
|
|
|
|
|
// success
|
|
|
|
return true;
|
2018-10-17 09:35:24 -07:00
|
|
|
}
|
|
|
|
}
|