-
Notifications
You must be signed in to change notification settings - Fork 197
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
flamenco: adding cpi to system program to bpf deploy #1654
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2d82951
adding native program cpi to system program for bpf deploy
ibhatt-jumptrading fb50e7f
adding ledger test
ibhatt-jumptrading 754796b
changing buffer to be stack allocated to prevent leaks
ibhatt-jumptrading 282ceb5
updating size bounds for buffer
ibhatt-jumptrading 143e43a
additional buffer checks
ibhatt-jumptrading 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
This file contains 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 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 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 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,97 @@ | ||
#include "../fd_account.h" | ||
#include "../../vm/fd_vm_syscalls.h" | ||
#include "../../vm/fd_vm_interp.h" | ||
|
||
int | ||
fd_native_cpi_execute_system_program_instruction( fd_exec_instr_ctx_t * ctx, | ||
fd_system_program_instruction_t const * instr, | ||
fd_vm_rust_account_meta_t const * acct_metas, | ||
ulong acct_metas_len, | ||
fd_pubkey_t const * signers, | ||
ulong signers_cnt ) { | ||
fd_instr_info_t instr_info[1]; | ||
fd_instruction_account_t instruction_accounts[256]; | ||
ulong instruction_accounts_cnt; | ||
|
||
for ( ulong i = 0; i < ctx->txn_ctx->accounts_cnt; i++ ) { | ||
if ( memcmp( fd_solana_system_program_id.key, ctx->txn_ctx->accounts[i].key, sizeof(fd_pubkey_t) ) == 0 ) { | ||
instr_info->program_id = (uchar)i; | ||
break; | ||
} | ||
} | ||
|
||
ulong starting_lamports = 0; | ||
uchar acc_idx_seen[256]; | ||
memset( acc_idx_seen, 0, 256 ); | ||
|
||
instr_info->program_id_pubkey = fd_solana_system_program_id; | ||
instr_info->acct_cnt = (ushort)acct_metas_len; | ||
for ( ulong j = 0; j < acct_metas_len; j++ ) { | ||
fd_vm_rust_account_meta_t const * acct_meta = &acct_metas[j]; | ||
|
||
for ( ulong k = 0; k < ctx->txn_ctx->accounts_cnt; k++ ) { | ||
if ( memcmp( acct_meta->pubkey, ctx->txn_ctx->accounts[k].uc, sizeof(fd_pubkey_t) ) == 0 ) { | ||
instr_info->acct_pubkeys[j] = ctx->txn_ctx->accounts[k]; | ||
instr_info->acct_txn_idxs[j] = (uchar)k; | ||
instr_info->acct_flags[j] = 0; | ||
instr_info->borrowed_accounts[j] = &ctx->txn_ctx->borrowed_accounts[k]; | ||
|
||
instr_info->is_duplicate[j] = acc_idx_seen[k]; | ||
if( FD_LIKELY( !acc_idx_seen[k] ) ) { | ||
/* This is the first time seeing this account */ | ||
acc_idx_seen[k] = 1; | ||
if( instr_info->borrowed_accounts[j]->const_meta != NULL ) { | ||
starting_lamports += instr_info->borrowed_accounts[j]->const_meta->info.lamports; | ||
} | ||
} | ||
|
||
if( acct_meta->is_writable ) { | ||
instr_info->acct_flags[j] |= FD_INSTR_ACCT_FLAGS_IS_WRITABLE; | ||
} | ||
// TODO: should check the parent has signer flag set | ||
if( acct_meta->is_signer ) { | ||
instr_info->acct_flags[j] |= FD_INSTR_ACCT_FLAGS_IS_SIGNER; | ||
} else { | ||
for( ulong k = 0; k < signers_cnt; k++ ) { | ||
if( memcmp( &signers[k], &acct_meta->pubkey, sizeof( fd_pubkey_t ) ) == 0 ) { | ||
instr_info->acct_flags[j] |= FD_INSTR_ACCT_FLAGS_IS_SIGNER; | ||
break; | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
|
||
instr_info->starting_lamports = starting_lamports; | ||
} | ||
|
||
fd_bincode_encode_ctx_t ctx2; | ||
uchar buf[4096UL]; | ||
ctx2.data = buf; | ||
ctx2.dataend = (uchar*)ctx2.data + sizeof(fd_system_program_instruction_t); | ||
int err = fd_system_program_instruction_encode( instr, &ctx2 ); | ||
if (err != FD_EXECUTOR_INSTR_SUCCESS) { | ||
FD_LOG_WARNING(("Encode failed")); | ||
return err; | ||
} | ||
|
||
instr_info->data = buf; | ||
instr_info->data_sz = (ushort) sizeof(fd_system_program_instruction_t); | ||
ulong exec_err = fd_vm_prepare_instruction( ctx->instr, instr_info, ctx, instruction_accounts, | ||
&instruction_accounts_cnt, signers, signers_cnt ); | ||
if( exec_err != FD_EXECUTOR_INSTR_SUCCESS ) { | ||
FD_LOG_WARNING(("Preparing instruction failed")); | ||
return (int)exec_err; | ||
} | ||
|
||
return fd_execute_instr( ctx->txn_ctx, instr_info ); | ||
} | ||
|
||
void | ||
fd_native_cpi_create_account_meta( fd_pubkey_t const * key, uchar is_signer, | ||
uchar is_writable, fd_vm_rust_account_meta_t * meta ) { | ||
meta->is_signer = is_signer; | ||
meta->is_writable = is_writable; | ||
fd_memcpy( meta->pubkey, key->key, sizeof(fd_pubkey_t) ); | ||
} |
This file contains 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,24 @@ | ||
#ifndef HEADER_fd_src_flamenco_runtime_program_fd_native_program_cpi_h | ||
#define HEADER_fd_src_flamenco_runtime_program_fd_native_program_cpi_h | ||
|
||
#include "../../fd_flamenco_base.h" | ||
#include "../fd_executor.h" | ||
#include "../fd_runtime.h" | ||
|
||
FD_PROTOTYPES_BEGIN | ||
|
||
int | ||
fd_native_cpi_execute_system_program_instruction( fd_exec_instr_ctx_t * ctx, | ||
fd_system_program_instruction_t const * instr, | ||
fd_vm_rust_account_meta_t const * acct_metas, | ||
ulong acct_metas_len, | ||
fd_pubkey_t const * signers, | ||
ulong signers_cnt ); | ||
|
||
void | ||
fd_native_cpi_create_account_meta( fd_pubkey_t const * key, uchar is_signer, | ||
uchar is_writable, fd_vm_rust_account_meta_t * meta ); | ||
|
||
FD_PROTOTYPES_END | ||
|
||
#endif /* HEADER_fd_src_flamenco_runtime_program_fd_native_program_cpi_h */ |
This file contains 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
Oops, something went wrong.
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.