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-03-05 09:53:56 -08:00
|
|
|
import {NUM_TICKS_PER_SECOND} from './timing';
|
2018-10-26 13:19:47 -07:00
|
|
|
import {Transaction} from './transaction';
|
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';
|
2018-10-17 09:35:24 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Program loader interface
|
|
|
|
*/
|
|
|
|
export class Loader {
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
connection: Connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
programId: PublicKey;
|
|
|
|
|
2018-11-27 08:31:06 -08:00
|
|
|
/**
|
|
|
|
* Amount of program data placed in each load Transaction
|
|
|
|
*/
|
|
|
|
static get chunkSize(): number {
|
|
|
|
return 256;
|
|
|
|
}
|
|
|
|
|
2018-10-17 09:35:24 -07:00
|
|
|
/**
|
|
|
|
* @param connection The connection to use
|
|
|
|
* @param programId Public key that identifies the loader
|
|
|
|
*/
|
|
|
|
constructor(connection: Connection, programId: PublicKey) {
|
|
|
|
Object.assign(this, {connection, programId});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load program data
|
|
|
|
*
|
|
|
|
* @param program Account to load the program info
|
2018-10-23 20:56:54 -07:00
|
|
|
* @param data Program data
|
2018-10-17 09:35:24 -07:00
|
|
|
*/
|
2018-10-23 20:56:54 -07:00
|
|
|
async load(program: Account, data: Array<number>) {
|
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({
|
|
|
|
keys: [program.publicKey],
|
|
|
|
programId: this.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(
|
2018-11-18 08:48:14 -08:00
|
|
|
sendAndConfirmTransaction(this.connection, transaction, program),
|
2018-11-04 11:41:21 -08:00
|
|
|
);
|
2018-10-23 20:56:54 -07:00
|
|
|
|
2019-03-05 09:53:56 -08:00
|
|
|
// Delay ~1 tick between write transactions in an attempt to reduce AccountInUse errors
|
|
|
|
// since all the write transactions modify the same program account
|
|
|
|
await sleep(1000 / NUM_TICKS_PER_SECOND);
|
|
|
|
|
2018-11-01 20:35:19 -07:00
|
|
|
// Run up to 8 Loads in parallel to prevent too many parallel transactions from
|
|
|
|
// getting rejected with AccountInUse.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finalize an account loaded with program data for execution
|
|
|
|
*
|
|
|
|
* @param program `load()`ed Account
|
|
|
|
*/
|
|
|
|
async finalize(program: Account) {
|
2019-03-14 13:27:47 -07:00
|
|
|
const dataLayout = BufferLayout.struct([
|
2018-10-17 09:35:24 -07:00
|
|
|
BufferLayout.u32('instruction'),
|
|
|
|
]);
|
|
|
|
|
2019-03-14 13:27:47 -07:00
|
|
|
const data = Buffer.alloc(dataLayout.span);
|
|
|
|
dataLayout.encode(
|
2018-10-17 09:35:24 -07:00
|
|
|
{
|
|
|
|
instruction: 1, // Finalize instruction
|
|
|
|
},
|
2019-03-14 13:27:47 -07:00
|
|
|
data,
|
2018-10-17 09:35:24 -07:00
|
|
|
);
|
|
|
|
|
2019-02-14 22:04:13 -08:00
|
|
|
const transaction = new Transaction().add({
|
2018-10-17 09:35:24 -07:00
|
|
|
keys: [program.publicKey],
|
|
|
|
programId: this.programId,
|
2019-03-14 13:27:47 -07:00
|
|
|
data,
|
2018-10-17 09:35:24 -07:00
|
|
|
});
|
2018-11-18 08:48:14 -08:00
|
|
|
await sendAndConfirmTransaction(this.connection, transaction, program);
|
2018-10-17 09:35:24 -07:00
|
|
|
}
|
|
|
|
}
|