Skip to content

Commit

Permalink
wip implementation of get_epoch_stake syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
topointon-jump committed Nov 11, 2024
1 parent 688cb04 commit e51481d
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/flamenco/features/fd_features_generated.c
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,12 @@ fd_feature_id_t const ids[] = {
.name = "partitioned_epoch_rewards_superfeature",
.cleaned_up = {UINT_MAX, UINT_MAX, UINT_MAX} },

{ .index = offsetof(fd_features_t, enable_get_epoch_stake_syscall)>>3,
.id = {"\x64\x88\xa2\xd0\x5a\xcc\xbb\xae\xa0\x4e\xa8\xaf\xeb\x15\xfb\x71\xa7\x5b\x27\x71\x96\x6c\x2f\x05\x0d\xfe\xf3\x44\xbb\x07\x3b\x21"},
/* 7mScTYkJXsbdrcwTQRs7oeCSXoJm4WjzBsRyf8bCU3Np */
.name = "enable_get_epoch_stake_syscall",
.cleaned_up = {UINT_MAX, UINT_MAX, UINT_MAX} },

{ .index = ULONG_MAX }
};

Expand Down Expand Up @@ -1443,6 +1449,7 @@ fd_feature_id_query( ulong prefix ) {
case 0x0b9047b5bb9ef961: return &ids[ 200 ];
case 0xa5a66405d0ab6309: return &ids[ 201 ];
case 0x81fcbfa0d0f6b105: return &ids[ 202 ];
case 0xaebbcc5ad0a28864: return &ids[ 203 ];
default: break;
}

Expand Down Expand Up @@ -1654,5 +1661,6 @@ FD_STATIC_ASSERT( offsetof( fd_features_t, zk_elgamal_proof_program_enabled
FD_STATIC_ASSERT( offsetof( fd_features_t, move_stake_and_move_lamports_ixs )>>3==200UL, layout );
FD_STATIC_ASSERT( offsetof( fd_features_t, deprecate_legacy_vote_ixs )>>3==201UL, layout );
FD_STATIC_ASSERT( offsetof( fd_features_t, partitioned_epoch_rewards_superfeature )>>3==202UL, layout );
FD_STATIC_ASSERT( offsetof( fd_features_t, enable_get_epoch_stake_syscall )>>3==203UL, layout );

FD_STATIC_ASSERT( sizeof( fd_features_t )>>3==FD_FEATURE_ID_CNT, layout );
3 changes: 2 additions & 1 deletion src/flamenco/features/fd_features_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/* FEATURE_ID_CNT is the number of features in ids */

#define FD_FEATURE_ID_CNT (203UL)
#define FD_FEATURE_ID_CNT (204UL)

union fd_features {

Expand Down Expand Up @@ -216,6 +216,7 @@ union fd_features {
/* 0x0b9047b5bb9ef961 */ ulong move_stake_and_move_lamports_ixs;
/* 0xa5a66405d0ab6309 */ ulong deprecate_legacy_vote_ixs;
/* 0x81fcbfa0d0f6b105 */ ulong partitioned_epoch_rewards_superfeature;
/* 0xaebbcc5ad0a28864 */ ulong enable_get_epoch_stake_syscall;
};

};
3 changes: 2 additions & 1 deletion src/flamenco/features/feature_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,6 @@
{"name":"zk_elgamal_proof_program_enabled","pubkey":"zkhiy5oLowR7HY4zogXjCjeMXyruLqBwSWH21qcFtnv"},
{"name":"move_stake_and_move_lamports_ixs","pubkey":"7bTK6Jis8Xpfrs8ZoUfiMDPazTcdPcTWheZFJTA5Z6X4"},
{"name":"deprecate_legacy_vote_ixs","pubkey":"depVvnQ2UysGrhwdiwU42tCadZL8GcBb1i2GYhMopQv"},
{"name":"partitioned_epoch_rewards_superfeature","pubkey":"PERzQrt5gBD1XEe2c9XdFWqwgHY3mr7cYWbm5V772V8"}
{"name":"partitioned_epoch_rewards_superfeature","pubkey":"PERzQrt5gBD1XEe2c9XdFWqwgHY3mr7cYWbm5V772V8"},
{"name":"enable_get_epoch_stake_syscall","pubkey":"7mScTYkJXsbdrcwTQRs7oeCSXoJm4WjzBsRyf8bCU3Np"}
]
4 changes: 3 additions & 1 deletion src/flamenco/runtime/fd_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ fd_runtime_init_bank_from_genesis( fd_exec_slot_ctx_t * slot_ctx,
.stake_delegations_pool = sacc_pool,
.stake_delegations_root = sacc_root,
.epoch = 0,
.unused = 0,
.total_stake = 0,
.vote_accounts = (fd_vote_accounts_t){
.vote_accounts_pool = vacc_pool,
.vote_accounts_root = vacc_root},
Expand Down Expand Up @@ -3931,6 +3931,7 @@ void fd_update_stake_delegations(fd_exec_slot_ctx_t * slot_ctx ) {
FD_SCRATCH_SCOPE_BEGIN {
fd_epoch_bank_t * epoch_bank = fd_exec_epoch_ctx_epoch_bank( slot_ctx->epoch_ctx );
fd_stakes_t * stakes = &epoch_bank->stakes;
stakes->total_stake = 0UL;

// TODO: is this size correct if the same stake account is in both the slot and epoch cache? Is this possible?
ulong stake_delegations_size = fd_delegation_pair_t_map_size(
Expand Down Expand Up @@ -4006,6 +4007,7 @@ FD_SCRATCH_SCOPE_BEGIN {
} else {
epoch_cache_node->elem.stake = n->elem.stake;
}
stakes->total_stake = fd_ulong_sat_add( stakes->total_stake, epoch_cache_node->elem.stake );
}

fd_bincode_destroy_ctx_t destroy_slot = {.valloc = slot_ctx->valloc};
Expand Down
2 changes: 1 addition & 1 deletion src/flamenco/runtime/program/fd_vote_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,7 @@ do_process_vote_state_update( fd_vote_state_t * vote_state,
}

// ??
static ulong
ulong
query_pubkey_stake( fd_pubkey_t const * pubkey, fd_vote_accounts_t const * vote_accounts ) {
fd_vote_accounts_pair_t_mapnode_t key = { 0 };
key.elem.key = *pubkey;
Expand Down
6 changes: 6 additions & 0 deletions src/flamenco/runtime/program/fd_vote_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ void
fd_vote_store_account( fd_exec_slot_ctx_t * slot_ctx,
fd_borrowed_account_t * vote_account );

/* Queries the delegated stake amount for the given vote account pubkey,
given the vote accounts map. */
ulong
query_pubkey_stake( fd_pubkey_t const * pubkey,
fd_vote_accounts_t const * vote_accounts );

FD_PROTOTYPES_END

#endif /* HEADER_fd_src_flamenco_runtime_program_fd_vote_program_h */
20 changes: 10 additions & 10 deletions src/flamenco/types/fd_types.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/flamenco/types/fd_types.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/flamenco/types/fd_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@
"fields": [
{ "name": "vote_accounts", "type": "vote_accounts" },
{ "name": "stake_delegations", "type": "map", "element": "delegation_pair", "key": "account" },
{ "name": "unused", "type": "ulong" },
{ "name": "total_stake", "type": "ulong" },
{ "name": "epoch", "type": "ulong" },
{ "name": "stake_history", "type": "stake_history" }
],
Expand Down
6 changes: 6 additions & 0 deletions src/flamenco/vm/syscall/fd_vm_syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fd_vm_syscall_register_slot( fd_sbpf_syscalls_t * syscalls,
int enable_alt_bn128_syscall = 0;
int enable_alt_bn128_compression_syscall = 0;
int enable_last_restart_slot_syscall = 0;
int enable_get_epoch_stake_syscall = 0;

int disable_fees_sysvar = 0;

Expand All @@ -38,6 +39,7 @@ fd_vm_syscall_register_slot( fd_sbpf_syscalls_t * syscalls,
enable_alt_bn128_syscall = FD_FEATURE_ACTIVE( slot_ctx, enable_alt_bn128_syscall );
enable_alt_bn128_compression_syscall = FD_FEATURE_ACTIVE( slot_ctx, enable_alt_bn128_compression_syscall );
enable_last_restart_slot_syscall = FD_FEATURE_ACTIVE( slot_ctx, last_restart_slot_sysvar );
enable_get_epoch_stake_syscall = FD_FEATURE_ACTIVE( slot_ctx, enable_get_epoch_stake_syscall );

disable_fees_sysvar = !FD_FEATURE_ACTIVE( slot_ctx, disable_fees_sysvar );

Expand Down Expand Up @@ -139,6 +141,10 @@ fd_vm_syscall_register_slot( fd_sbpf_syscalls_t * syscalls,
if( enable_alt_bn128_compression_syscall )
REGISTER( "sol_alt_bn128_compression", fd_vm_syscall_sol_alt_bn128_compression );

if ( enable_get_epoch_stake_syscall ) {
REGISTER( "sol_get_epoch_stake", fd_vm_syscall_sol_get_epoch_stake );
}

# undef REGISTER

return FD_VM_SUCCESS;
Expand Down
4 changes: 4 additions & 0 deletions src/flamenco/vm/syscall/fd_vm_syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,10 @@ FD_VM_SYSCALL_DECL( sol_curve_group_op );

FD_VM_SYSCALL_DECL( sol_curve_multiscalar_mul );

/* syscall sol_get_epoch_stake */

FD_VM_SYSCALL_DECL( sol_get_epoch_stake );

int
fd_vm_derive_pda( fd_vm_t * vm,
fd_pubkey_t const * program_id,
Expand Down
67 changes: 67 additions & 0 deletions src/flamenco/vm/syscall/fd_vm_syscall_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../../runtime/sysvar/fd_sysvar_rent.h"
#include "../../runtime/sysvar/fd_sysvar_last_restart_slot.h"
#include "../../runtime/context/fd_exec_txn_ctx.h"
#include "../../runtime/program/fd_vote_program.h"
#include "../../runtime/context/fd_exec_instr_ctx.h"

int
Expand Down Expand Up @@ -464,3 +465,69 @@ fd_vm_syscall_sol_get_processed_sibling_instruction(
*ret = 0UL;
return FD_VM_SUCCESS;
}

/* fd_vm_syscall_sol_get_epoch_stake */
int
fd_vm_syscall_sol_get_epoch_stake(
void * _vm,
ulong var_vaddr,
FD_PARAM_UNUSED ulong r2,
FD_PARAM_UNUSED ulong r3,
FD_PARAM_UNUSED ulong r4,
FD_PARAM_UNUSED ulong r5,
ulong * ret
) {
fd_vm_t * vm = (fd_vm_t *)_vm;

/* https://github.com/firedancer-io/agave/blob/a68325a4264f4907067bd2f48526944347c606d4/programs/bpf_loader/src/syscalls/mod.rs#L2047 */
if ( var_vaddr == 0UL ) {
/* Consume base compute cost
https://github.com/firedancer-io/agave/blob/a68325a4264f4907067bd2f48526944347c606d4/programs/bpf_loader/src/syscalls/mod.rs#L2056 */
FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );

fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_ERR_SIGCALL;

/*
Return the total stake in this epoch
https://github.com/firedancer-io/agave/blob/a68325a4264f4907067bd2f48526944347c606d4/programs/bpf_loader/src/syscalls/mod.rs#L2048 */
ulong epoch_total_stake = instr_ctx->epoch_ctx->epoch_bank.stakes.total_stake;

/*
Get the epoch stakes used to calculate the leader schedule for the current epoch.
https://github.com/firedancer-io/agave/blob/a68325a4264f4907067bd2f48526944347c606d4/runtime/src/bank.rs#L6269 */
/* FIXME: get total epoch stake */

*ret = epoch_total_stake;
return FD_VM_SUCCESS;
}

/* Charge compute units: syscall_base + floor(PUBKEY_BYTES/cpi_bytes_per_unit) + mem_op_base
https://github.com/firedancer-io/agave/blob/a68325a4264f4907067bd2f48526944347c606d4/programs/bpf_loader/src/syscalls/mod.rs#L2073 */
FD_VM_CU_UPDATE( vm, fd_ulong_sat_add(
fd_ulong_sat_add(
FD_VM_SYSCALL_BASE_COST,
FD_PUBKEY_FOOTPRINT / FD_VM_CPI_BYTES_PER_UNIT ),
FD_VM_MEM_OP_BASE_COST ) );

fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_ERR_SIGCALL;

/* Look up the amount of stake this vote account has, and return this.
https://github.com/firedancer-io/agave/blob/a68325a4264f4907067bd2f48526944347c606d4/programs/bpf_loader/src/syscalls/mod.rs#L2097 */
fd_pubkey_t const * vote_pubkey = FD_VM_MEM_HADDR_LD(
vm,
var_vaddr,
FD_VM_ALIGN_RUST_PUBKEY,
FD_PUBKEY_FOOTPRINT );

ulong vote_account_stake = query_pubkey_stake(
vote_pubkey, &instr_ctx->epoch_ctx->epoch_bank.stakes.vote_accounts );

*ret = vote_account_stake;
return FD_VM_SUCCESS;
}

0 comments on commit e51481d

Please sign in to comment.