From 8f77acd876ba3c13b6628b66c4266c0454a357f7 Mon Sep 17 00:00:00 2001 From: Emanuele Cesena Date: Tue, 7 Jan 2025 23:46:01 +0000 Subject: [PATCH] vm: add syscall sol_get_epoch_rewards_sysvar --- .../vm_interp-fixtures.list | 1 + .../runtime/sysvar/fd_sysvar_epoch_rewards.c | 2 +- .../runtime/sysvar/fd_sysvar_epoch_rewards.h | 2 +- src/flamenco/vm/syscall/fd_vm_syscall.c | 9 +++++- src/flamenco/vm/syscall/fd_vm_syscall.h | 1 + .../vm/syscall/fd_vm_syscall_runtime.c | 29 +++++++++++++++++++ 6 files changed, 41 insertions(+), 3 deletions(-) diff --git a/contrib/test/test-vectors-fixtures/vm-interp-fixtures/vm_interp-fixtures.list b/contrib/test/test-vectors-fixtures/vm-interp-fixtures/vm_interp-fixtures.list index 7560baabd2..5153dfa5ed 100644 --- a/contrib/test/test-vectors-fixtures/vm-interp-fixtures/vm_interp-fixtures.list +++ b/contrib/test/test-vectors-fixtures/vm-interp-fixtures/vm_interp-fixtures.list @@ -5,3 +5,4 @@ dump/test-vectors/vm_interp/fixtures/latest/35cf22f9eec59e8e27368e14e657e5659eb4 dump/test-vectors/vm_interp/fixtures/latest/crash-e83a21538a4cb23f371b138b9b16573cf6c9427e.fix dump/test-vectors/vm_interp/fixtures/latest/74286304a3bc106079f15b2d03f40418f8657a7a_3948568.fix dump/test-vectors/vm_interp/fixtures/latest/d6633e73f5c6a36107118ad018d34a18d7e42dea_3948711.fix +dump/test-vectors/vm_interp/fixtures/latest/4d58264dac713a0ec42c255f11d3b0fb74d32181_2321898.fix diff --git a/src/flamenco/runtime/sysvar/fd_sysvar_epoch_rewards.c b/src/flamenco/runtime/sysvar/fd_sysvar_epoch_rewards.c index f94c1c8cd9..f7128c890f 100644 --- a/src/flamenco/runtime/sysvar/fd_sysvar_epoch_rewards.c +++ b/src/flamenco/runtime/sysvar/fd_sysvar_epoch_rewards.c @@ -24,7 +24,7 @@ write_epoch_rewards( fd_exec_slot_ctx_t * slot_ctx, fd_sysvar_epoch_rewards_t * fd_sysvar_epoch_rewards_t * fd_sysvar_epoch_rewards_read( fd_sysvar_epoch_rewards_t * result, - fd_exec_slot_ctx_t * slot_ctx + fd_exec_slot_ctx_t const * slot_ctx ) { fd_sysvar_epoch_rewards_t const * ret = fd_sysvar_cache_epoch_rewards( slot_ctx->sysvar_cache ); if( FD_UNLIKELY( NULL != ret ) ) { diff --git a/src/flamenco/runtime/sysvar/fd_sysvar_epoch_rewards.h b/src/flamenco/runtime/sysvar/fd_sysvar_epoch_rewards.h index cbe00b72fa..5136150f9c 100644 --- a/src/flamenco/runtime/sysvar/fd_sysvar_epoch_rewards.h +++ b/src/flamenco/runtime/sysvar/fd_sysvar_epoch_rewards.h @@ -10,7 +10,7 @@ FD_PROTOTYPES_BEGIN fd_sysvar_epoch_rewards_t * fd_sysvar_epoch_rewards_read( fd_sysvar_epoch_rewards_t * result, - fd_exec_slot_ctx_t * slot_ctx + fd_exec_slot_ctx_t const * slot_ctx ); /* Update EpochRewards sysvar with distributed rewards diff --git a/src/flamenco/vm/syscall/fd_vm_syscall.c b/src/flamenco/vm/syscall/fd_vm_syscall.c index e8b4381c47..46ce97a9b0 100644 --- a/src/flamenco/vm/syscall/fd_vm_syscall.c +++ b/src/flamenco/vm/syscall/fd_vm_syscall.c @@ -29,6 +29,7 @@ fd_vm_syscall_register_slot( fd_sbpf_syscalls_t * syscalls, int enable_last_restart_slot_syscall = 0; int enable_get_sysvar_syscall = 0; int enable_get_epoch_stake_syscall = 0; + int enable_epoch_rewards_syscall = 0; int disable_fees_sysvar = 0; @@ -42,6 +43,9 @@ fd_vm_syscall_register_slot( fd_sbpf_syscalls_t * syscalls, enable_last_restart_slot_syscall = FD_FEATURE_ACTIVE( slot_ctx, last_restart_slot_sysvar ); enable_get_sysvar_syscall = FD_FEATURE_ACTIVE( slot_ctx, get_sysvar_syscall_enabled ); enable_get_epoch_stake_syscall = FD_FEATURE_ACTIVE( slot_ctx, enable_get_epoch_stake_syscall ); + // https://github.com/anza-xyz/agave/blob/v2.1.7/programs/bpf_loader/src/syscalls/mod.rs#L275-L277 + enable_epoch_rewards_syscall = FD_FEATURE_ACTIVE( slot_ctx, enable_partitioned_epoch_reward ) || + FD_FEATURE_ACTIVE( slot_ctx, partitioned_epoch_rewards_superfeature ); disable_fees_sysvar = FD_FEATURE_ACTIVE( slot_ctx, disable_fees_sysvar ); @@ -55,6 +59,7 @@ fd_vm_syscall_register_slot( fd_sbpf_syscalls_t * syscalls, enable_last_restart_slot_syscall = 1; enable_get_sysvar_syscall = 1; enable_get_epoch_stake_syscall = 1; + enable_epoch_rewards_syscall = 1; } @@ -143,7 +148,9 @@ fd_vm_syscall_register_slot( fd_sbpf_syscalls_t * syscalls, REGISTER( "sol_alt_bn128_group_op", fd_vm_syscall_sol_alt_bn128_group_op ); //REGISTER( "sol_big_mod_exp", fd_vm_syscall_sol_big_mod_exp ); -//REGISTER( "sol_get_epoch_rewards_sysvar", fd_vm_syscall_sol_get_epoch_rewards_sysvar ); + + if( enable_epoch_rewards_syscall ) + REGISTER( "sol_get_epoch_rewards_sysvar", fd_vm_syscall_sol_get_epoch_rewards_sysvar ); if( enable_poseidon_syscall ) REGISTER( "sol_poseidon", fd_vm_syscall_sol_poseidon ); diff --git a/src/flamenco/vm/syscall/fd_vm_syscall.h b/src/flamenco/vm/syscall/fd_vm_syscall.h index f8c97989ff..7499446160 100644 --- a/src/flamenco/vm/syscall/fd_vm_syscall.h +++ b/src/flamenco/vm/syscall/fd_vm_syscall.h @@ -468,6 +468,7 @@ FD_VM_SYSCALL_DECL( sol_get_epoch_schedule_sysvar ); FD_VM_SYSCALL_DECL( sol_get_fees_sysvar ); FD_VM_SYSCALL_DECL( sol_get_rent_sysvar ); FD_VM_SYSCALL_DECL( sol_get_last_restart_slot_sysvar ); +FD_VM_SYSCALL_DECL( sol_get_epoch_rewards_sysvar ); /* syscall(13c1b505) "sol_get_sysvar" diff --git a/src/flamenco/vm/syscall/fd_vm_syscall_runtime.c b/src/flamenco/vm/syscall/fd_vm_syscall_runtime.c index 81ea608e78..fa47b6ed8f 100644 --- a/src/flamenco/vm/syscall/fd_vm_syscall_runtime.c +++ b/src/flamenco/vm/syscall/fd_vm_syscall_runtime.c @@ -2,6 +2,7 @@ #include "../../runtime/program/fd_vote_program.h" #include "../../runtime/sysvar/fd_sysvar.h" #include "../../runtime/sysvar/fd_sysvar_clock.h" +#include "../../runtime/sysvar/fd_sysvar_epoch_rewards.h" #include "../../runtime/sysvar/fd_sysvar_epoch_schedule.h" #include "../../runtime/sysvar/fd_sysvar_fees.h" #include "../../runtime/sysvar/fd_sysvar_rent.h" @@ -585,3 +586,31 @@ fd_vm_syscall_sol_get_processed_sibling_instruction( *ret = 0UL; return FD_VM_SUCCESS; } + +// https://github.com/anza-xyz/agave/blob/master/programs/bpf_loader/src/syscalls/sysvar.rs#L75 +int +fd_vm_syscall_sol_get_epoch_rewards_sysvar( /**/ void * _vm, + /**/ ulong out_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; + + fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx; + if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME; + + FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, FD_SYSVAR_EPOCH_REWARDS_FOOTPRINT ) ); + + void * out = FD_VM_MEM_HADDR_ST( vm, out_vaddr, FD_SYSVAR_EPOCH_REWARDS_ALIGN, FD_SYSVAR_EPOCH_REWARDS_FOOTPRINT ); + + fd_sysvar_epoch_rewards_t var[1]; + fd_sysvar_epoch_rewards_new ( var ); + fd_sysvar_epoch_rewards_read( var, instr_ctx->slot_ctx ); + + memcpy( out, var, FD_SYSVAR_EPOCH_REWARDS_FOOTPRINT ); + + *_ret = 0UL; + return FD_VM_SUCCESS; +}