-
Notifications
You must be signed in to change notification settings - Fork 20
interface: Add cross-program invocation helpers #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
febo
wants to merge
5
commits into
main
Choose a base branch
from
febo/interface-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| use { | ||
| core::{mem::MaybeUninit, slice::from_raw_parts}, | ||
| solana_account_view::AccountView, | ||
| solana_address::Address, | ||
| solana_instruction_view::{ | ||
| cpi::{invoke_signed_unchecked, CpiAccount, Signer, MAX_STATIC_CPI_ACCOUNTS}, | ||
| InstructionAccount, InstructionView, | ||
| }, | ||
| solana_program_error::{ProgramError, ProgramResult}, | ||
| }; | ||
|
|
||
| /// Writes a message to the transaction log, validating that | ||
| /// provided accounts are signers. | ||
| /// | ||
| /// ### Accounts: | ||
| /// 0. `..+N` `[SIGNER]` N signing accounts | ||
| pub struct Memo<'memo, 'signers, S: AsRef<AccountView>> { | ||
| /// The message to log. | ||
| pub message: &'memo str, | ||
|
|
||
| /// Signing accounts. | ||
| pub signers: &'signers [S], | ||
|
|
||
| /// The Memo program to invoke. | ||
| pub program_id: &'static Address, | ||
| } | ||
|
|
||
| impl<S: AsRef<AccountView>> Memo<'_, '_, S> { | ||
| /// Invokes the Memo program with the provided message and signing accounts. | ||
| #[inline(always)] | ||
| pub fn invoke(&self) -> ProgramResult { | ||
| self.invoke_signed(&[]) | ||
| } | ||
|
|
||
| /// Invokes the Memo program with the provided message and signing accounts. | ||
| /// | ||
| /// Seeds for signing accounts can be provided via `signers` parameter. | ||
| #[inline(always)] | ||
| pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult { | ||
| let mut instruction_accounts = | ||
| [const { MaybeUninit::<InstructionAccount>::uninit() }; MAX_STATIC_CPI_ACCOUNTS]; | ||
|
|
||
| let expected_account = self.signers.len(); | ||
|
|
||
| if expected_account > MAX_STATIC_CPI_ACCOUNTS { | ||
| return Err(ProgramError::InvalidArgument); | ||
| } | ||
|
|
||
| let mut accounts = [const { MaybeUninit::<CpiAccount>::uninit() }; MAX_STATIC_CPI_ACCOUNTS]; | ||
|
|
||
| for i in 0..expected_account { | ||
| // SAFETY: `expected_account` is less than MAX_STATIC_CPI_ACCOUNTS. | ||
| unsafe { | ||
| let signer = self.signers.get_unchecked(i); | ||
|
|
||
| instruction_accounts.get_unchecked_mut(i).write( | ||
| InstructionAccount::readonly_signer(signer.as_ref().address()), | ||
| ); | ||
|
|
||
| CpiAccount::init_from_account_view(signer.as_ref(), accounts.get_unchecked_mut(i)); | ||
| } | ||
| } | ||
|
|
||
| // SAFETY: both `instruction_accounts` and `accounts` are initialized. | ||
| unsafe { | ||
| invoke_signed_unchecked( | ||
| &InstructionView { | ||
| program_id: self.program_id, | ||
| accounts: from_raw_parts(instruction_accounts.as_ptr() as _, expected_account), | ||
| data: self.message.as_bytes(), | ||
| }, | ||
| from_raw_parts(accounts.as_ptr() as _, expected_account), | ||
| signers, | ||
| ); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,28 @@ | ||
| #![no_std] | ||
| #![deny(missing_docs)] | ||
|
|
||
| //! An interface for programs that accept a string of encoded characters and | ||
| //! verifies that it parses, while verifying and logging signers. | ||
|
|
||
| /// Instruction type | ||
| /// CPI interface for invoking the Memo program. | ||
| #[cfg(feature = "cpi")] | ||
| pub mod cpi; | ||
|
|
||
| /// Instruction definition for the Memo program. | ||
| #[cfg(feature = "instruction")] | ||
| pub mod instruction; | ||
|
|
||
| /// Legacy symbols from Memo version 1 | ||
| pub mod v1 { | ||
| solana_pubkey::declare_id!("Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo"); | ||
| solana_address::declare_id!("Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo"); | ||
| } | ||
|
|
||
| /// Symbols from Memo version 3 | ||
| pub mod v3 { | ||
| solana_pubkey::declare_id!("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"); | ||
| solana_address::declare_id!("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"); | ||
| } | ||
|
|
||
| /// Symbols from Memo version 4 | ||
| pub mod v4 { | ||
| solana_pubkey::declare_id!("Memo4c2pN8afCj432Lb7RMVKi9PbQnnW7ewFFaV3oAH"); | ||
| solana_address::declare_id!("Memo4c2pN8afCj432Lb7RMVKi9PbQnnW7ewFFaV3oAH"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instructionas a feature might encourage the wrong behavior, since the cpi helpers also create instructions (in a way).I can get behind
instructionif it's preferred by others, but we could go withallocto be technically correct, since that's whatsolana-instructionrequires, or we can go withoffchainto make it clearer where it should be used.Of the options, I think I prefer
allocthe most.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the name is definitely not ideal, but I found it tricky to come up with a feature name for this. Maybe
allocwould be best, although it does not directly reflect what is being enabled;offchainis tricky since anyone still usingAccountInfowill need to use this feature to get an instruction for CPI.