Skip to content

[WIP] flamenco, runtime: implement replace_spl_token_with_p_token#8921

Open
topointon-jump wants to merge 1 commit intomainfrom
firedancer-replace-spl-token-with-p-token
Open

[WIP] flamenco, runtime: implement replace_spl_token_with_p_token#8921
topointon-jump wants to merge 1 commit intomainfrom
firedancer-replace-spl-token-with-p-token

Conversation

@topointon-jump
Copy link
Contributor

Copilot AI review requested due to automatic review settings March 16, 2026 19:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements the replace_spl_token_with_p_token feature transition in the Flamenco runtime by upgrading the SPL Token (loader v2) program to loader v3 using a new Core BPF migration helper, and wiring in the required buffer address + feature ID plumbing.

Changes:

  • Adds fd_upgrade_loader_v2_program_with_loader_v3_program() and supporting target checks/ELF validation in Core BPF migration code.
  • Introduces the ptoken program buffer address constant and ID bytes in system IDs.
  • Registers the replace_spl_token_with_p_token feature and triggers the migration at feature activation.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/flamenco/runtime/program/fd_builtin_programs.h Declares the new loader v2 → loader v3 upgrade helper.
src/flamenco/runtime/fd_system_ids_pp.h Adds PTOKEN_PROG_BUFFER_ID macro bytes.
src/flamenco/runtime/fd_system_ids.h Exposes fd_solana_ptoken_program_buffer_address.
src/flamenco/runtime/fd_system_ids.c Defines fd_solana_ptoken_program_buffer_address.
src/flamenco/runtime/fd_runtime.c Triggers the migration when replace_spl_token_with_p_token activates.
src/flamenco/runtime/fd_core_bpf_migration.c Implements the migration logic and ELF validation.
src/flamenco/features/feature_map.json Adds the new feature name/pubkey mapping.
src/flamenco/features/fd_features_generated.h Updates generated feature count/set ID and adds the feature field.
src/flamenco/features/fd_features_generated.c Adds the feature ID entry and perfect-hash mapping entries.

You can also share your feedback on Copilot code review. Take the survey.

@topointon-jump topointon-jump force-pushed the firedancer-replace-spl-token-with-p-token branch from 2bf49a9 to 8d381b3 Compare March 16, 2026 19:56
Copilot AI review requested due to automatic review settings March 16, 2026 19:58
@topointon-jump topointon-jump force-pushed the firedancer-replace-spl-token-with-p-token branch from 8d381b3 to e11aa03 Compare March 16, 2026 19:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements the replace_spl_token_with_p_token feature activation path in the Flamenco runtime by adding a Loader-v2 → Loader-v3 program upgrade routine and wiring it into epoch-boundary feature activation, along with the required new IDs/constants.

Changes:

  • Add fd_upgrade_loader_v2_program_with_loader_v3_program() and supporting target checks in core BPF migration code.
  • Introduce the ptoken program buffer system ID and expose it via fd_system_ids.{h,c} for use at runtime.
  • Add the replace_spl_token_with_p_token feature to the feature map and generated feature tables, and invoke the upgrade when the feature activates.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/flamenco/runtime/program/fd_builtin_programs.h Exposes new upgrade helper prototype to runtime.
src/flamenco/runtime/fd_system_ids_pp.h Adds PTOKEN program buffer ID bytes.
src/flamenco/runtime/fd_system_ids.h Declares new ptoken program buffer address constant.
src/flamenco/runtime/fd_system_ids.c Defines new ptoken program buffer address constant.
src/flamenco/runtime/fd_runtime.c Triggers Loader v2→v3 upgrade on replace_spl_token_with_p_token activation.
src/flamenco/runtime/fd_core_bpf_migration.c Implements target checks and upgrade flow; adds ELF validation step.
src/flamenco/features/feature_map.json Adds new feature pubkey mapping.
src/flamenco/features/fd_features_generated.h Updates feature count/set ID and adds the new feature bitfield entry.
src/flamenco/features/fd_features_generated.c Adds new feature ID entry and perfect-hash lookup/static asserts.

You can also share your feedback on Copilot code review. Take the survey.

@github-actions
Copy link

Performance Measurements ⏳

Suite Baseline New Change
backtest mainnet-368528500-perf per slot 0.060467 s 0.060563 s 0.159%
backtest mainnet-368528500-perf snapshot load 3.225 s 2.784 s -13.674%
backtest mainnet-368528500-perf total elapsed 60.467239 s 60.562513 s 0.158%
firedancer mem usage with mainnet.toml 985.41 GiB 985.41 GiB 0.000%

@topointon-jump topointon-jump force-pushed the firedancer-replace-spl-token-with-p-token branch from e11aa03 to 8f5477f Compare March 16, 2026 21:48
@github-actions
Copy link

Performance Measurements ⏳

Suite Baseline New Change
backtest mainnet-368528500-perf per slot 0.083109 s 0.082831 s -0.335%
backtest mainnet-368528500-perf snapshot load 3.867 s 3.416 s -11.663%
backtest mainnet-368528500-perf total elapsed 83.109279 s 82.831428 s -0.334%
firedancer mem usage with mainnet.toml 985.41 GiB 985.41 GiB 0.000%

There is no direct equivalent in Agave to this function, because
we are not updating the program cache here. However, we do the same
checks that our program cache does upon deployment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth adding a comment why you dont need to explicitly update the progcache

Comment on lines +88 to +89
uchar rodata_mem [ FD_RUNTIME_ACC_SZ_MAX ] __attribute__((aligned(FD_SBPF_PROG_RODATA_ALIGN)));
uchar scratch_mem [ FD_RUNTIME_ACC_SZ_MAX ] __attribute__((aligned(FD_ACCOUNT_REC_ALIGN)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix these bounds to account for the header size of the structs + max acc size

/* Staging memory for ELF validation during BPF program
migrations. */
struct {
uchar calldests_mem[ FD_SBPF_PROGRAM_FOOTPRINT ] __attribute__((aligned(alignof(fd_sbpf_program_t))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double check the worst case mem

return;

fd_rent_t const * rent = fd_bank_rent_query( bank );
ulong const slot = fd_bank_slot_get ( bank );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ulong const slot = fd_bank_slot_get ( bank );
ulong slot = fd_bank_slot_get ( bank );

ulong scratch_sz = sizeof( runtime_stack->bpf_migration.progcache_validate.scratch_mem );

fd_features_t const * features = fd_bank_features_query( bank );
ulong const load_slot = fd_bank_slot_get( bank );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop const here

Comment on lines +371 to +379
/* Setup calldests */
int has_calldests = !fd_sbpf_enable_stricter_elf_headers_enabled( elf_info->sbpf_version );
void * calldests_mem = NULL;
if( has_calldests && elf_info->text_cnt>0UL ) {
FD_SCRATCH_ALLOC_INIT( cd, calldests_base );
calldests_mem = FD_SCRATCH_ALLOC_APPEND( cd, fd_sbpf_calldests_align(), fd_sbpf_calldests_footprint( elf_info->text_cnt ) );
void * cd_end = (void *)FD_SCRATCH_ALLOC_FINI( cd, alignof(uchar) );
if( FD_UNLIKELY( ((ulong)cd_end<(ulong)calldests_base) | ((ulong)cd_end>(ulong)calldests_base + calldests_sz) ) ) return 1;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible to use fd_sbpf_program_new?

FD_SCRATCH_ALLOC_INIT( sc, scratch_base );
void * scratch = FD_SCRATCH_ALLOC_APPEND( sc, alignof(uchar), elf_sz );
void * sc_end = (void *)FD_SCRATCH_ALLOC_FINI( sc, alignof(uchar) );
if( FD_UNLIKELY( ((ulong)sc_end<(ulong)scratch_base) | ((ulong)sc_end>(ulong)scratch_base + scratch_sz) ) ) return 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if( FD_UNLIKELY( ((ulong)sc_end<(ulong)scratch_base) | ((ulong)sc_end>(ulong)scratch_base + scratch_sz) ) ) return 1;
if( FD_UNLIKELY( ((ulong)sc_end<(ulong)scratch_base) || ((ulong)sc_end>(ulong)scratch_base + scratch_sz) ) ) return 1;

if( FD_UNLIKELY( fd_vm_syscall_register_slot( syscalls, load_slot, features, /* is_deploy */ 1 )!=FD_VM_SUCCESS ) ) return 1;

/* fd_sbpf_program_load checks */
if( FD_UNLIKELY( fd_sbpf_program_load( prog, elf, elf_sz, syscalls, &loader_config, scratch, elf_sz ) ) ) return 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scratch_sz for last argument

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants