Skip to content

Commit

Permalink
flamenco: add stakes module
Browse files Browse the repository at this point in the history
  • Loading branch information
Firedancer Team authored and ripatel-fd committed Apr 21, 2024
1 parent 2356f17 commit 7a34721
Show file tree
Hide file tree
Showing 11 changed files with 1,033 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/flamenco/runtime/program/fd_stake_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -2888,3 +2888,21 @@ fd_stake_program_execute( fd_exec_instr_ctx_t ctx ) {
done:
return rc;
}

/* Public API *********************************************************/

int
fd_stake_get_state( fd_borrowed_account_t const * self,
fd_valloc_t const * valloc,
fd_stake_state_v2_t * out ) {
return get_state( self, *valloc, out );
}

fd_stake_history_entry_t
fd_stake_activating_and_deactivating( fd_delegation_t const * self,
ulong target_epoch,
fd_stake_history_t const * stake_history,
ulong * new_rate_activation_epoch ) {
return stake_activating_and_deactivating(
self, target_epoch, stake_history, new_rate_activation_epoch );
}
11 changes: 11 additions & 0 deletions src/flamenco/runtime/program/fd_stake_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ FD_PROTOTYPES_BEGIN
int
fd_stake_program_execute( fd_exec_instr_ctx_t ctx );

int
fd_stake_get_state( fd_borrowed_account_t const * self,
fd_valloc_t const * valloc,
fd_stake_state_v2_t * out );

fd_stake_history_entry_t
fd_stake_activating_and_deactivating( fd_delegation_t const * self,
ulong target_epoch,
fd_stake_history_t const * stake_history,
ulong * new_rate_activation_epoch );

FD_PROTOTYPES_END

#endif /* HEADER_fd_src_flamenco_runtime_program_fd_stake_program_h */
6 changes: 6 additions & 0 deletions src/flamenco/runtime/sysvar/Local.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
ifdef FD_HAS_INT128
$(call add-hdrs,fd_sysvar.h)
$(call add-objs,fd_sysvar,fd_flamenco)

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

Expand All @@ -20,4 +23,7 @@ $(call run-unit-test,test_sysvar_rent)

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

$(call add-hdrs,fd_sysvar_stake_history.h)
$(call add-objs,fd_sysvar_stake_history,fd_flamenco)
endif
42 changes: 42 additions & 0 deletions src/flamenco/runtime/sysvar/fd_sysvar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "fd_sysvar.h"
#include "../context/fd_exec_epoch_ctx.h"
#include "../context/fd_exec_slot_ctx.h"
#include "fd_sysvar_rent.h"

int
fd_sysvar_set( fd_exec_slot_ctx_t * slot_ctx,
uchar const * owner,
fd_pubkey_t const * pubkey,
uchar * data,
ulong sz,
ulong slot,
ulong lamports ) {

fd_acc_mgr_t * acc_mgr = slot_ctx->acc_mgr;
fd_funk_txn_t * funk_txn = slot_ctx->funk_txn;

FD_BORROWED_ACCOUNT_DECL(rec);

int err = fd_acc_mgr_modify( acc_mgr, funk_txn, pubkey, 1, sz, rec );
if( FD_UNLIKELY( err != FD_ACC_MGR_SUCCESS ) )
return FD_ACC_MGR_ERR_READ_FAILED;

fd_memcpy(rec->data, data, sz);
// What is the correct behavior here? Where is this code in the
// solana code base? Do I only adjust the lamports if the data
// increases but not decreases? I am inventing money here...
fd_acc_lamports_t lamports_before = rec->meta->info.lamports;
rec->meta->info.lamports = (lamports == 0UL) ? fd_rent_exempt_minimum_balance2( &slot_ctx->epoch_ctx->epoch_bank.rent, sz ) : lamports;
slot_ctx->slot_bank.capitalization = fd_ulong_sat_sub(
fd_ulong_sat_add(
slot_ctx->slot_bank.capitalization,
rec->meta->info.lamports),
lamports_before);
// FD_LOG_DEBUG(("fd_sysvar_set: capitalization={%lu} increased by lamports: %lu for pubkey %32J", slot_ctx->slot_bank.capitalization, (rec->meta->info.lamports - lamports_before), pubkey));


rec->meta->dlen = sz;
fd_memcpy(rec->meta->info.owner, owner, 32);
rec->meta->slot = slot;
return 0;
}
15 changes: 15 additions & 0 deletions src/flamenco/runtime/sysvar/fd_sysvar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef HEADER_fd_src_flamenco_runtime_fd_sysvar_h
#define HEADER_fd_src_flamenco_runtime_fd_sysvar_h

#include "../../fd_flamenco_base.h"

int
fd_sysvar_set( fd_exec_slot_ctx_t * state,
uchar const * owner,
fd_pubkey_t const * pubkey,
uchar * data,
ulong sz,
ulong slot,
ulong lamports );

#endif /* HEADER_fd_src_flamenco_runtime_fd_sysvar_h */
78 changes: 78 additions & 0 deletions src/flamenco/runtime/sysvar/fd_sysvar_stake_history.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "fd_sysvar_stake_history.h"
#include "../context/fd_exec_slot_ctx.h"
#include "../../types/fd_types.h"
#include "fd_sysvar.h"
#include "../fd_system_ids.h"

void
write_stake_history( fd_exec_slot_ctx_t * slot_ctx,
fd_stake_history_t * stake_history ) {
/* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/program/src/sysvar/stake_history.rs#L12 */
uchar enc[16392] = {0};

fd_bincode_encode_ctx_t encode =
{ .data = enc,
.dataend = enc + sizeof(enc) };
if( FD_UNLIKELY( fd_stake_history_encode( stake_history, &encode )!=FD_BINCODE_SUCCESS ) )
FD_LOG_ERR(("fd_stake_history_encode failed"));

fd_sysvar_set( slot_ctx, fd_sysvar_owner_id.key, &fd_sysvar_stake_history_id, enc, sizeof(enc), slot_ctx->slot_bank.slot, 0UL );
}

fd_stake_history_t *
fd_sysvar_stake_history_read( fd_stake_history_t * result,
fd_exec_slot_ctx_t * slot_ctx,
fd_valloc_t * valloc ) {

FD_BORROWED_ACCOUNT_DECL(stake_rec);
int err = fd_acc_mgr_view( slot_ctx->acc_mgr, slot_ctx->funk_txn, &fd_sysvar_stake_history_id, stake_rec);
if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS ) )
return NULL;

fd_bincode_decode_ctx_t ctx = {
.data = stake_rec->const_data,
.dataend = (char *) stake_rec->const_data + stake_rec->const_meta->dlen,
.valloc = *valloc
};

if( FD_UNLIKELY( fd_stake_history_decode( result, &ctx )!=FD_BINCODE_SUCCESS ) )
return NULL;
return result;
}

void
fd_sysvar_stake_history_init( fd_exec_slot_ctx_t * slot_ctx ) {
fd_stake_history_t stake_history = {
.pool = fd_stake_history_pool_alloc( slot_ctx->valloc ),
.treap = fd_stake_history_treap_alloc( slot_ctx->valloc )
};
write_stake_history( slot_ctx, &stake_history );
}

void
fd_sysvar_stake_history_update( fd_exec_slot_ctx_t * slot_ctx,
fd_stake_history_entry_t * entry ) {
// Need to make this maybe zero copies of map...
fd_stake_history_t stake_history;
fd_sysvar_stake_history_read( &stake_history, slot_ctx, &slot_ctx->valloc );

if (fd_stake_history_treap_ele_cnt( stake_history.treap ) == fd_stake_history_treap_ele_max( stake_history.treap )) {
fd_stake_history_treap_fwd_iter_t iter = fd_stake_history_treap_fwd_iter_init( stake_history.treap, stake_history.pool );
fd_stake_history_entry_t * ele = fd_stake_history_treap_fwd_iter_ele( iter, stake_history.pool );
stake_history.treap = fd_stake_history_treap_ele_remove( stake_history.treap, ele, stake_history.pool );
fd_stake_history_pool_ele_release( stake_history.pool, ele );
}

ulong idx = fd_stake_history_pool_idx_acquire( stake_history.pool );

stake_history.pool[ idx ].epoch = entry->epoch;
stake_history.pool[ idx ].activating = entry->activating;
stake_history.pool[ idx ].effective = entry->effective;
stake_history.pool[ idx ].deactivating = entry->deactivating;
stake_history.treap = fd_stake_history_treap_idx_insert( stake_history.treap, idx, stake_history.pool );


write_stake_history( slot_ctx, &stake_history);
fd_bincode_destroy_ctx_t destroy = { .valloc = slot_ctx->valloc };
fd_stake_history_destroy( &stake_history, &destroy );
}
22 changes: 22 additions & 0 deletions src/flamenco/runtime/sysvar/fd_sysvar_stake_history.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef HEADER_fd_src_flamenco_runtime_fd_sysvar_stake_history_h
#define HEADER_fd_src_flamenco_runtime_fd_sysvar_stake_history_h

#include "../../fd_flamenco_base.h"
#include "../fd_executor.h"

FD_PROTOTYPES_BEGIN

/* The stake history sysvar contains the history of cluster-wide activations and de-activations per-epoch. Updated at the start of each epoch. */

/* Initialize the stake history sysvar account. */
void
fd_sysvar_stake_history_init( fd_exec_slot_ctx_t * slot_ctx );

/* Update the stake history sysvar account - called during epoch boundary*/
void
fd_sysvar_stake_history_update( fd_exec_slot_ctx_t * slot_ctx,
fd_stake_history_entry_t * entry );

FD_PROTOTYPES_END

#endif /* HEADER_fd_src_flamenco_runtime_fd_sysvar_stake_history_h */
6 changes: 6 additions & 0 deletions src/flamenco/stakes/Local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ifdef FD_HAS_INT128
$(call add-hdrs,fd_stakes.h)
$(call add-objs,fd_stakes,fd_flamenco)
# TODO this should not depend on fd_funk
$(call make-bin,fd_stakes_from_snapshot,fd_stakes_from_snapshot,fd_flamenco fd_funk fd_ballet fd_util)
endif
Loading

0 comments on commit 7a34721

Please sign in to comment.