Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeng-jump committed Dec 18, 2024
1 parent 291ead2 commit 74f73e0
Show file tree
Hide file tree
Showing 21 changed files with 96 additions and 162 deletions.
3 changes: 0 additions & 3 deletions src/flamenco/runtime/Local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ $(call add-objs,fd_pubkey_utils,fd_flamenco)

$(call add-hdrs,fd_rent_lists.h)

$(call add-hdrs,fd_runtime_spad.h)
$(call add-objs,fd_runtime_spad,fd_flamenco)

ifdef FD_HAS_ATOMIC
$(call add-hdrs,fd_runtime.h fd_runtime_init.h fd_runtime_err.h)
$(call add-objs,fd_runtime fd_runtime_init,fd_flamenco)
Expand Down
5 changes: 4 additions & 1 deletion src/flamenco/runtime/context/fd_exec_instr_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ struct __attribute__((aligned(8UL))) fd_exec_instr_ctx {

fd_funk_txn_t * funk_txn;
fd_acc_mgr_t * acc_mgr;
fd_valloc_t valloc; /* Scratch-backed valloc TODO: migrate to transaction spad */
/* This is for instruction-level test harnesses.
Offline and live instruction execution should use the spad in
txn_ctx. */
fd_valloc_t valloc;

/* Most instructions log the base58 program id multiple times, so it's
convenient to compute it once and reuse it. */
Expand Down
1 change: 0 additions & 1 deletion src/flamenco/runtime/context/fd_exec_txn_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ fd_exec_txn_ctx_from_exec_slot_ctx( fd_exec_slot_ctx_t * slot_ctx,
fd_exec_txn_ctx_t * txn_ctx ) {
txn_ctx->slot_ctx = slot_ctx;
txn_ctx->epoch_ctx = slot_ctx->epoch_ctx;
txn_ctx->valloc = slot_ctx->valloc;
txn_ctx->funk_txn = NULL;
txn_ctx->acc_mgr = slot_ctx->acc_mgr;
}
Expand Down
1 change: 0 additions & 1 deletion src/flamenco/runtime/context/fd_exec_txn_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ struct __attribute__((aligned(8UL))) fd_exec_txn_ctx {

fd_funk_txn_t * funk_txn;
fd_acc_mgr_t * acc_mgr;
fd_valloc_t valloc; /* Workspace-backed valloc TODO: migrate to spad */
fd_spad_t * spad; /* Sized out to handle the worst case footprint of single transaction execution. */

ulong paid_fees;
Expand Down
43 changes: 1 addition & 42 deletions src/flamenco/runtime/fd_executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1454,45 +1454,6 @@ fd_execute_txn_prepare_phase3( fd_exec_slot_ctx_t * slot_ctx,
return 0;
}

/* Stuff to be done after multithreading ends */
int
fd_execute_txn_finalize( fd_exec_txn_ctx_t * txn_ctx,
int exec_txn_err ) {
if( exec_txn_err != 0 ) {
for( ulong i = 0; i < txn_ctx->accounts_cnt; i++ ) {
fd_borrowed_account_t * acc_rec = &txn_ctx->borrowed_accounts[i];
void * acc_rec_data = fd_borrowed_account_destroy( acc_rec );
if( acc_rec_data != NULL ) {
fd_valloc_free( txn_ctx->valloc, acc_rec_data );
}
}

// fd_funk_txn_cancel( slot_ctx->acc_mgr->funk, txn_ctx->funk_txn, 0 );
return 0;
}

for( ulong i = 0; i < txn_ctx->accounts_cnt; i++ ) {
if( !fd_txn_account_is_writable_idx( txn_ctx, (int)i ) ) {
continue;
}

fd_borrowed_account_t * acc_rec = &txn_ctx->borrowed_accounts[i];

int ret = fd_acc_mgr_save_non_tpool( txn_ctx->acc_mgr, txn_ctx->funk_txn, acc_rec );
if( ret != FD_ACC_MGR_SUCCESS ) {
FD_LOG_ERR(( "failed to save edits to accounts" ));
return -1;
}

void * borrow_account_data = fd_borrowed_account_destroy( acc_rec );
if( borrow_account_data != NULL ) {
fd_valloc_free( txn_ctx->valloc, borrow_account_data );
}
}

return 0;
}

/* Creates a TxnContext Protobuf message from a provided txn_ctx.
- The transaction is assumed to have just finished phase 1 of preparation
- Caller of this function should have a scratch frame ready
Expand Down Expand Up @@ -1828,10 +1789,8 @@ fd_dump_txn_to_protobuf( fd_exec_txn_ctx_t *txn_ctx, fd_spad_t * spad ) {
fd_base58_encode_64( signature, &out_size, encoded_signature );

if( txn_ctx->capture_ctx->dump_proto_sig_filter ) {
ulong filter_strlen = (ulong) strlen(txn_ctx->capture_ctx->dump_proto_sig_filter);

// Terminate early if the signature does not match
if( memcmp( txn_ctx->capture_ctx->dump_proto_sig_filter, encoded_signature, filter_strlen < out_size ? filter_strlen : out_size ) ) {
if( strcmp( txn_ctx->capture_ctx->dump_proto_sig_filter, encoded_signature ) ) {
return;
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/flamenco/runtime/fd_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ fd_execute_txn_prepare_phase3( fd_exec_slot_ctx_t * slot_ctx,
fd_exec_txn_ctx_t * txn_ctx,
fd_txn_p_t * txn );

int
fd_execute_txn_finalize( fd_exec_txn_ctx_t * txn_ctx,
int exec_txn_err );

/*
Execute the given transaction.
Expand Down
15 changes: 7 additions & 8 deletions src/flamenco/runtime/fd_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,6 @@ fd_runtime_prepare_execute_finalize_txn( fd_exec_slot_ctx_t * slot_ctx,
return -1;
}

txn_ctx->valloc = fd_scratch_virtual();

/* NOTE: This intentionally does not have sigverify */

fd_runtime_pre_execute_check( task_info );
Expand Down Expand Up @@ -1452,7 +1450,7 @@ fd_runtime_finalize_txn( fd_exec_slot_ctx_t * slot_ctx,
fd_bincode_decode_ctx_t decode_vsv =
{ .data = acc_rec->const_data,
.dataend = acc_rec->const_data + acc_rec->const_meta->dlen,
.valloc = fd_runtime_spad_virtual( txn_ctx->spad ) };
.valloc = fd_spad_virtual( txn_ctx->spad ) };

int err = fd_vote_state_versioned_decode( vsv, &decode_vsv );
if( err ) break; /* out of scratch scope */
Expand All @@ -1472,7 +1470,7 @@ fd_runtime_finalize_txn( fd_exec_slot_ctx_t * slot_ctx,
__builtin_unreachable();
}

fd_valloc_t valloc = fd_runtime_spad_virtual( txn_ctx->spad );
fd_valloc_t valloc = fd_spad_virtual( txn_ctx->spad );
fd_vote_record_timestamp_vote_with_slot( slot_ctx, acc_rec->pubkey, ts->timestamp, ts->slot, valloc );
} FD_SPAD_FRAME_END;
fd_funk_end_write( slot_ctx->acc_mgr->funk );
Expand Down Expand Up @@ -1844,7 +1842,7 @@ fd_runtime_finalize_txns_tpool( fd_exec_slot_ctx_t * slot_ctx,
fd_bincode_decode_ctx_t decode_vsv =
{ .data = acc_rec->const_data,
.dataend = acc_rec->const_data + acc_rec->const_meta->dlen,
.valloc = fd_runtime_spad_virtual( txn_ctx->spad ) };
.valloc = fd_spad_virtual( txn_ctx->spad ) };

int err = fd_vote_state_versioned_decode( vsv, &decode_vsv );
if( err ) break; /* out of scratch scope */
Expand All @@ -1864,7 +1862,7 @@ fd_runtime_finalize_txns_tpool( fd_exec_slot_ctx_t * slot_ctx,
__builtin_unreachable();
}

fd_valloc_t valloc = fd_runtime_spad_virtual( txn_ctx->spad );
fd_valloc_t valloc = fd_spad_virtual( txn_ctx->spad );
fd_vote_record_timestamp_vote_with_slot( slot_ctx, acc_rec->pubkey, ts->timestamp, ts->slot, valloc );
} FD_SPAD_FRAME_END;
}
Expand Down Expand Up @@ -2136,7 +2134,6 @@ fd_runtime_execute_txns_in_waves_tpool( fd_exec_slot_ctx_t * slot_ctx,
// Dump txns in waves
if( dump_txn ) {
for( ulong i = 0; i < wave_task_infos_cnt; ++i ) {
// TODO this dumper still relies on scratch, move to spad?
/* Manual push/pop on the spad within the callee. */
fd_dump_txn_to_protobuf( wave_task_infos[i].txn_ctx, spads[0] );
}
Expand Down Expand Up @@ -2977,10 +2974,12 @@ fd_runtime_txn_lamports_per_signature( fd_exec_txn_ctx_t * txn_ctx,
// why is asan not detecting access to uninitialized memory here?!
fd_nonce_state_versions_t state;
int err;
if ((NULL != txn_descriptor) && fd_load_nonce_account(txn_ctx, &state, txn_ctx->valloc, &err)) {
FD_SPAD_FRAME_BEGIN( txn_ctx->spad ) {
if ((NULL != txn_descriptor) && fd_load_nonce_account(txn_ctx, &state, fd_spad_virtual( txn_ctx->spad ), &err)) {
if (state.inner.current.discriminant == fd_nonce_state_enum_initialized)
return state.inner.current.inner.initialized.fee_calculator.lamports_per_signature;
}
} FD_SPAD_FRAME_END;

// lamports_per_signature = (transaction has a DurableNonce, use the lamports_per_signature from that nonce instead of looking up the recent_block_hash and using the lamports_per_signature associated with that hash
// let TransactionExecutionDetails {
Expand Down
3 changes: 1 addition & 2 deletions src/flamenco/runtime/fd_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "../fd_flamenco_base.h"
#include "fd_runtime_err.h"
#include "fd_runtime_init.h"
#include "fd_runtime_spad.h"
#include "fd_rocksdb.h"
#include "fd_acc_mgr.h"
#include "../features/fd_features.h"
Expand Down Expand Up @@ -246,7 +245,7 @@ fd_runtime_spad_private_frame_end( fd_runtime_spad_verify_handle_private_t * _sp
then we invoke verify to check things. */
/* -1UL because spad pop is called after instr stack pop. */
if( FD_UNLIKELY( _spad_handle->txn_ctx->instr_stack_sz>=FD_MAX_INSTRUCTION_STACK_DEPTH-1UL && fd_spad_verify( _spad_handle->txn_ctx->spad ) ) ) {
uchar * txn_signature = (uchar*)_spad_handle->txn_ctx->_txn_raw->raw + _spad_handle->txn_ctx->txn_descriptor->signature_off;
uchar const * txn_signature = (uchar const *)fd_txn_get_signatures( _spad_handle->txn_ctx->txn_descriptor, _spad_handle->txn_ctx->_txn_raw->raw );
FD_BASE58_ENCODE_64_BYTES( txn_signature, sig );
FD_LOG_ERR(( "spad corrupted or overflown on transaction %s", sig ));
}
Expand Down
26 changes: 0 additions & 26 deletions src/flamenco/runtime/fd_runtime_spad.c

This file was deleted.

24 changes: 0 additions & 24 deletions src/flamenco/runtime/fd_runtime_spad.h

This file was deleted.

16 changes: 8 additions & 8 deletions src/flamenco/runtime/program/fd_bpf_loader_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fd_bpf_loader_v3_is_executable( fd_exec_slot_ctx_t * slot_ctx,
fd_bincode_decode_ctx_t ctx = {
.data = (uchar *)meta + meta->hlen,
.dataend = (char *) ctx.data + meta->dlen,
.valloc = fd_runtime_spad_virtual( instr_ctx->txn_ctx->spad ),
.valloc = fd_spad_virtual( instr_ctx->txn_ctx->spad ),
};

fd_bpf_upgradeable_loader_state_t loader_state = {0};
Expand Down Expand Up @@ -187,7 +187,7 @@ read_bpf_upgradeable_loader_state_for_program( fd_exec_txn_ctx_t *
fd_bincode_decode_ctx_t ctx = {
.data = rec->const_data,
.dataend = rec->const_data + rec->const_meta->dlen,
.valloc = fd_runtime_spad_virtual( txn_ctx->spad ),
.valloc = fd_spad_virtual( txn_ctx->spad ),
};

if( FD_UNLIKELY( fd_bpf_upgradeable_loader_state_decode( result, &ctx ) ) ) {
Expand Down Expand Up @@ -370,7 +370,7 @@ fd_bpf_loader_v3_program_get_state( fd_exec_instr_ctx_t const * instr_ct
fd_bincode_decode_ctx_t ctx = {
.data = borrowed_acc->const_data,
.dataend = borrowed_acc->const_data + borrowed_acc->const_meta->dlen,
.valloc = fd_runtime_spad_virtual( instr_ctx->txn_ctx->spad ),
.valloc = fd_spad_virtual( instr_ctx->txn_ctx->spad ),
};

int err = fd_bpf_upgradeable_loader_state_decode( state, &ctx );
Expand Down Expand Up @@ -535,7 +535,7 @@ fd_bpf_execute( fd_exec_instr_ctx_t * instr_ctx, fd_sbpf_validated_program_t * p
return FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE;
}

fd_valloc_t valloc = fd_runtime_spad_virtual( instr_ctx->txn_ctx->spad );
fd_valloc_t valloc = fd_spad_virtual( instr_ctx->txn_ctx->spad );

#ifdef FD_DEBUG_SBPF_TRACES
uchar * signature = (uchar*)vm->instr_ctx->txn_ctx->_txn_raw->raw + vm->instr_ctx->txn_ctx->txn_descriptor->signature_off;
Expand Down Expand Up @@ -658,7 +658,7 @@ fd_bpf_execute( fd_exec_instr_ctx_t * instr_ctx, fd_sbpf_validated_program_t * p
static int
process_loader_upgradeable_instruction( fd_exec_instr_ctx_t * instr_ctx ) {
uchar const * data = instr_ctx->instr->data;
fd_valloc_t valloc = fd_runtime_spad_virtual( instr_ctx->txn_ctx->spad );
fd_valloc_t valloc = fd_spad_virtual( instr_ctx->txn_ctx->spad );

fd_bpf_upgradeable_loader_program_instruction_t instruction = {0};
fd_bincode_decode_ctx_t decode_ctx = {0};
Expand Down Expand Up @@ -962,7 +962,7 @@ process_loader_upgradeable_instruction( fd_exec_instr_ctx_t * instr_ctx ) {

const uchar * buffer_data = buffer->const_data + buffer_data_offset;

err = fd_deploy_program( instr_ctx, buffer_data, buffer_data_len, fd_runtime_spad_virtual( instr_ctx->txn_ctx->spad ) );
err = fd_deploy_program( instr_ctx, buffer_data, buffer_data_len, fd_spad_virtual( instr_ctx->txn_ctx->spad ) );
if( FD_UNLIKELY( err ) ) {
FD_LOG_WARNING(( "Failed to deploy program" )); // custom log
return err;
Expand Down Expand Up @@ -1218,7 +1218,7 @@ process_loader_upgradeable_instruction( fd_exec_instr_ctx_t * instr_ctx ) {
}

const uchar * buffer_data = buffer->const_data + buffer_data_offset;
err = fd_deploy_program( instr_ctx, buffer_data, buffer_data_len, fd_runtime_spad_virtual( instr_ctx->txn_ctx->spad ) );
err = fd_deploy_program( instr_ctx, buffer_data, buffer_data_len, fd_spad_virtual( instr_ctx->txn_ctx->spad ) );
if( FD_UNLIKELY( err ) ) {
FD_LOG_WARNING(( "Failed to deploy program" ));
return err;
Expand Down Expand Up @@ -1750,7 +1750,7 @@ process_loader_upgradeable_instruction( fd_exec_instr_ctx_t * instr_ctx ) {
uchar * programdata_data = programdata_account->data + PROGRAMDATA_METADATA_SIZE;
ulong programdata_size = new_len - PROGRAMDATA_METADATA_SIZE;

err = fd_deploy_program( instr_ctx, programdata_data, programdata_size, fd_runtime_spad_virtual( instr_ctx->txn_ctx->spad ) );
err = fd_deploy_program( instr_ctx, programdata_data, programdata_size, fd_spad_virtual( instr_ctx->txn_ctx->spad ) );
if( FD_UNLIKELY( err ) ) {
FD_LOG_WARNING(( "Failed to deploy program" ));
return err;
Expand Down
1 change: 1 addition & 0 deletions src/flamenco/runtime/program/fd_bpf_loader_serialization.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "fd_bpf_loader_serialization.h"
#include "../fd_account.h"
#include "../fd_runtime.h"

/* As a general note, copy_account_data implies that direct mapping is not being
used/is inactive. This file is responsible for serializing and deserializing
Expand Down
1 change: 0 additions & 1 deletion src/flamenco/runtime/program/fd_bpf_loader_serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "../../fd_flamenco_base.h"
#include "../../vm/fd_vm.h"
#include "../fd_runtime.h"

#define FD_NON_DUP_MARKER (0xFF )

Expand Down
2 changes: 1 addition & 1 deletion src/flamenco/runtime/program/fd_compute_budget_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fd_executor_compute_budget_program_execute_instructions( fd_exec_txn_ctx_t * ctx
fd_bincode_decode_ctx_t decode_ctx = {
.data = data,
.dataend = &data[ instr->data_sz ],
.valloc = fd_runtime_spad_virtual( ctx->spad ),
.valloc = fd_spad_virtual( ctx->spad ),
};

int ret = fd_compute_budget_program_instruction_decode( &instruction, &decode_ctx );
Expand Down
4 changes: 2 additions & 2 deletions src/flamenco/runtime/program/fd_loader_v4_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ fd_loader_v4_program_instruction_deploy( fd_exec_instr_ctx_t * instr_ctx ) {
end of the slot. Since programs cannot be invoked until the next slot anyways, doing this is okay.
https://github.com/anza-xyz/agave/blob/09ef71223b24e30e59eaeaf5eb95e85f222c7de1/programs/loader-v4/src/lib.rs#L262-L269 */
err = fd_deploy_program( instr_ctx, programdata, buffer->const_meta->dlen - LOADER_V4_PROGRAM_DATA_OFFSET, fd_runtime_spad_virtual( instr_ctx->txn_ctx->spad ) );
err = fd_deploy_program( instr_ctx, programdata, buffer->const_meta->dlen - LOADER_V4_PROGRAM_DATA_OFFSET, fd_spad_virtual( instr_ctx->txn_ctx->spad ) );
if( FD_UNLIKELY( err ) ) {
return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
}
Expand Down Expand Up @@ -704,7 +704,7 @@ fd_loader_v4_program_execute( fd_exec_instr_ctx_t * instr_ctx ) {
fd_bincode_decode_ctx_t decode_ctx = {
.data = data,
.dataend = &data[ instr_ctx->instr->data_sz > 1232UL ? 1232UL : instr_ctx->instr->data_sz ],
.valloc = fd_runtime_spad_virtual( instr_ctx->txn_ctx->spad ),
.valloc = fd_spad_virtual( instr_ctx->txn_ctx->spad ),
};

if( FD_UNLIKELY( fd_loader_v4_program_instruction_decode( &instruction, &decode_ctx ) ) ) {
Expand Down
Loading

0 comments on commit 74f73e0

Please sign in to comment.