fix: Remove fs dep, bpf_loader now takes ELF as bytes
This commit is contained in:
parent
224d929d4d
commit
f871b0410a
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,5 @@
|
||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
import fs from 'mz/fs';
|
|
||||||
import elfy from 'elfy';
|
import elfy from 'elfy';
|
||||||
|
|
||||||
import {Account, PublicKey, Loader, SystemProgram} from '.';
|
import {Account, PublicKey, Loader, SystemProgram} from '.';
|
||||||
|
@ -12,31 +11,29 @@ import type {Connection} from '.';
|
||||||
*/
|
*/
|
||||||
export class BpfLoader {
|
export class BpfLoader {
|
||||||
/**
|
/**
|
||||||
* Public key that identifies the NativeLoader
|
* Public key that identifies the BpfLoader
|
||||||
*/
|
*/
|
||||||
static get programId(): PublicKey {
|
static get programId(): PublicKey {
|
||||||
return new PublicKey('0x0606060606060606060606060606060606060606060606060606060606060606');
|
return new PublicKey('0x0606060606060606060606060606060606060606060606060606060606060606');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a BPF program
|
* Load a BPF program
|
||||||
*
|
*
|
||||||
* @param connection The connection to use
|
* @param connection The connection to use
|
||||||
* @param owner User account to load the program with
|
* @param owner User account to load the program into
|
||||||
* @param programName Name of the BPF program
|
* @param elfBytes the entire ELF containing the BPF program in its .text.entrypoint section
|
||||||
*/
|
*/
|
||||||
static async load(
|
static async load(
|
||||||
connection: Connection,
|
connection: Connection,
|
||||||
owner: Account,
|
owner: Account,
|
||||||
programName: string,
|
elfBytes: Array<number>,
|
||||||
): Promise<PublicKey> {
|
): Promise<PublicKey> {
|
||||||
const programAccount = new Account();
|
const programAccount = new Account();
|
||||||
|
|
||||||
const data = await fs.readFile(programName);
|
const elf = elfy.parse(elfBytes);
|
||||||
const elf = elfy.parse(data);
|
|
||||||
const section = elf.body.sections.find(section => section.name === '.text.entrypoint');
|
const section = elf.body.sections.find(section => section.name === '.text.entrypoint');
|
||||||
|
|
||||||
// Allocate memory for the program account
|
|
||||||
const transaction = SystemProgram.createAccount(
|
const transaction = SystemProgram.createAccount(
|
||||||
owner.publicKey,
|
owner.publicKey,
|
||||||
programAccount.publicKey,
|
programAccount.publicKey,
|
||||||
|
|
|
@ -51,7 +51,7 @@ export class Loader {
|
||||||
const transactions = [];
|
const transactions = [];
|
||||||
while (array.length > 0) {
|
while (array.length > 0) {
|
||||||
const bytes = array.slice(0, chunkSize);
|
const bytes = array.slice(0, chunkSize);
|
||||||
let userdata = Buffer.alloc(chunkSize + 16);
|
const userdata = Buffer.alloc(chunkSize + 16);
|
||||||
userdataLayout.encode(
|
userdataLayout.encode(
|
||||||
{
|
{
|
||||||
instruction: 0, // Load instruction
|
instruction: 0, // Load instruction
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
|
import fs from 'mz/fs';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Connection,
|
Connection,
|
||||||
BpfLoader,
|
BpfLoader,
|
||||||
|
@ -23,7 +25,8 @@ test('load BPF program', async () => {
|
||||||
|
|
||||||
const connection = new Connection(url);
|
const connection = new Connection(url);
|
||||||
const from = await newAccountWithTokens(connection);
|
const from = await newAccountWithTokens(connection);
|
||||||
const programId = await BpfLoader.load(connection, from, 'test/bin/noop_c.o');
|
const data = await fs.readFile('test/bin/noop_c.o');
|
||||||
|
const programId = await BpfLoader.load(connection, from, data);
|
||||||
const transaction = new Transaction().add({
|
const transaction = new Transaction().add({
|
||||||
keys: [from.publicKey],
|
keys: [from.publicKey],
|
||||||
programId,
|
programId,
|
||||||
|
|
Loading…
Reference in New Issue