Skip to content

Commit

Permalink
spl: Add shared memory api
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante committed Feb 10, 2021
1 parent ce5ca7d commit d92cb15
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ incremented for features.

* cli: Embed workspace programs into local validator genesis when testing.
* cli: Stream program logs to `.anchor/program-logs` directory when testing.
* spl: Add shared memory api.

## [0.2.0] - 2021-02-08

Expand Down
1 change: 1 addition & 0 deletions spl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ description = "CPI clients for SPL programs"
[dependencies]
anchor-lang = { path = "../lang", version = "0.2.0", features = ["derive"] }
spl-token = { version = "3.0.1", features = ["no-entrypoint"] }
solana-program = "=1.5.0"
1 change: 1 addition & 0 deletions spl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod shmem;
pub mod token;
49 changes: 49 additions & 0 deletions spl/src/shmem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! CPI API for interacting with the SPL shared memory
//! [program](https://github.com/solana-labs/solana-program-library/tree/master/shared-memory).
use anchor_lang::{Accounts, CpiContext};
use solana_program::account_info::AccountInfo;
use solana_program::declare_id;
use solana_program::entrypoint::ProgramResult;
use solana_program::instruction::{AccountMeta, Instruction};
use solana_program::program;

// TODO: update this once the final shared memory program gets released.
// shmem4EWT2sPdVGvTZCzXXRAURL9G5vpPxNwSeKhHUL.
declare_id!("DynWy94wrWp5RimU49creYMQ5py3Up8BBNS4VA73VCpi");

/// `ret` writes the given `data` field to the shared memory account
/// acting as a return value that can be used across CPI.
/// The caleee should use this to write data into the shared memory account.
/// The caler should use the account directly to pull out and interpret the
/// bytes. Shared memory serialization is not specified and is up to the
/// caller and callee.
pub fn ret<'a, 'b, 'c, 'info>(
ctx: CpiContext<'a, 'b, 'c, 'info, Ret<'info>>,
data: Vec<u8>,
) -> ProgramResult {
let instruction = Instruction {
program_id: *ctx.program.key,
accounts: vec![AccountMeta::new(*ctx.accounts.buffer.key, false)],
data,
};
let mut accounts = vec![ctx.accounts.buffer];
accounts.push(ctx.program.clone());
program::invoke(&instruction, &accounts)
}

#[derive(Accounts)]
pub struct Ret<'info> {
#[account(mut)]
pub buffer: AccountInfo<'info>,
}

// A set of accounts that can be used with shared memory.
#[derive(Accounts)]
pub struct Shmem<'info> {
// Shared memory account to write the return value into.
#[account(mut, "shmem.owner == shmem_program.key")]
pub shmem: AccountInfo<'info>,
#[account("shmem_program.key == &ID")]
pub shmem_program: AccountInfo<'info>,
}

0 comments on commit d92cb15

Please sign in to comment.