Add BpfLoader (#34)

feat: Add BpfLoader
This commit is contained in:
jackcmay
2018-10-23 20:56:54 -07:00
committed by Michael Vines
parent b8d586c67e
commit 02787df7b9
14 changed files with 3552 additions and 3189 deletions

55
web3.js/src/bpf-loader.js Normal file
View File

@@ -0,0 +1,55 @@
// @flow
import fs from 'mz/fs';
import elfy from 'elfy';
import {Account, PublicKey, Loader, SystemProgram} from '.';
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
import type {Connection} from '.';
/**
* Factory class for transactions to interact with a program loader
*/
export class BpfLoader {
/**
* Public key that identifies the NativeLoader
*/
static get programId(): PublicKey {
return new PublicKey('0x0606060606060606060606060606060606060606060606060606060606060606');
}
/**
* Loads a BPF program
*
* @param connection The connection to use
* @param owner User account to load the program with
* @param programName Name of the BPF program
*/
static async load(
connection: Connection,
owner: Account,
programName: string,
): Promise<PublicKey> {
const programAccount = new Account();
const data = await fs.readFile(programName);
const elf = elfy.parse(data);
const section = elf.body.sections.find(section => section.name === '.text.entrypoint');
// Allocate memory for the program account
const transaction = SystemProgram.createAccount(
owner.publicKey,
programAccount.publicKey,
1,
section.data.length,
BpfLoader.programId,
);
await sendAndConfirmTransaction(connection, owner, transaction);
const loader = new Loader(connection, BpfLoader.programId);
await loader.load(programAccount, section.data);
await loader.finalize(programAccount);
return programAccount.publicKey;
}
}

View File

@@ -1,5 +1,6 @@
// @flow
export {Account} from './account';
export {BpfLoader} from './bpf-loader';
export {BudgetProgram} from './budget-program';
export {Connection} from './connection';
export {Loader} from './loader';

View File

@@ -32,10 +32,9 @@ export class Loader {
* Load program data
*
* @param program Account to load the program info
* @param offset Account userdata offset to write `bytes` into
* @param bytes Program data
* @param data Program data
*/
async load(program: Account, offset: number, bytes: Array<number>) {
async load(program: Account, data: Array<number>) {
const userdataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'),
BufferLayout.u32('offset'),
@@ -43,27 +42,35 @@ export class Loader {
BufferLayout.u32('bytesLengthPadding'),
BufferLayout.seq(
BufferLayout.u8('byte'),
BufferLayout.offset(BufferLayout.u32(), -8),
'bytes'
),
BufferLayout.offset(BufferLayout.u32(), -8), 'bytes'),
]);
let userdata = Buffer.alloc(bytes.length + 16);
userdataLayout.encode(
{
instruction: 0, // Load instruction
offset,
bytes,
},
userdata,
);
const chunkSize = 256;
let userdata = Buffer.alloc(chunkSize + 16);
let offset = 0;
let array = data;
while (array.length > 0) {
const bytes = array.slice(0, chunkSize);
const transaction = new Transaction().add({
keys: [program.publicKey],
programId: this.programId,
userdata,
});
await sendAndConfirmTransaction(this.connection, program, transaction);
userdataLayout.encode(
{
instruction: 0, // Load instruction
offset,
bytes,
},
userdata,
);
const transaction = new Transaction().add({
keys: [program.publicKey],
programId: this.programId,
userdata,
});
await sendAndConfirmTransaction(this.connection, program, transaction);
offset += chunkSize;
array = array.slice(chunkSize);
}
}
/**

View File

@@ -42,7 +42,7 @@ export class NativeLoader {
await sendAndConfirmTransaction(connection, owner, transaction);
const loader = new Loader(connection, NativeLoader.programId);
await loader.load(programAccount, 0, bytes);
await loader.load(programAccount, bytes);
await loader.finalize(programAccount);
return programAccount.publicKey;