Skip to content

bank manager [wip] #5144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
81 changes: 52 additions & 29 deletions src/app/ledger/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../../flamenco/runtime/fd_runtime_public.h"
#include "../../flamenco/runtime/fd_rocksdb.h"
#include "../../flamenco/runtime/fd_txncache.h"
#include "../../flamenco/runtime/fd_bank_mgr.h"
#include "../../flamenco/rewards/fd_rewards.h"
#include "../../ballet/base58/fd_base58.h"
#include "../../flamenco/runtime/context/fd_capture_ctx.h"
Expand Down Expand Up @@ -291,7 +292,7 @@ runtime_replay( fd_ledger_args_t * ledger_args ) {

fd_features_restore( ledger_args->slot_ctx, ledger_args->runtime_spad );

fd_runtime_update_leaders( ledger_args->slot_ctx, ledger_args->slot_ctx->slot_bank.slot, ledger_args->runtime_spad );
fd_runtime_update_leaders( ledger_args->slot_ctx, ledger_args->slot_ctx->slot, ledger_args->runtime_spad );

fd_calculate_epoch_accounts_hash_values( ledger_args->slot_ctx );

Expand All @@ -300,8 +301,8 @@ runtime_replay( fd_ledger_args_t * ledger_args ) {
ulong slot_cnt = 0;
fd_blockstore_t * blockstore = ledger_args->slot_ctx->blockstore;

ulong prev_slot = ledger_args->slot_ctx->slot_bank.slot;
ulong start_slot = ledger_args->slot_ctx->slot_bank.slot + 1;
ulong prev_slot = ledger_args->slot_ctx->slot;
ulong start_slot = ledger_args->slot_ctx->slot + 1;

ledger_args->slot_ctx->root_slot = prev_slot;

Expand Down Expand Up @@ -349,8 +350,9 @@ runtime_replay( fd_ledger_args_t * ledger_args ) {

for( ulong slot = start_slot; slot<=ledger_args->end_slot && !aborted; ++slot ) {

ledger_args->slot_ctx->slot_bank.prev_slot = prev_slot;
ledger_args->slot_ctx->slot_bank.slot = slot;
ulong * prev_slot_bm = fd_bank_mgr_prev_slot_modify( ledger_args->slot_ctx->bank_mgr );
*prev_slot_bm = prev_slot;
fd_bank_mgr_prev_slot_save( ledger_args->slot_ctx->bank_mgr );

FD_LOG_DEBUG(( "reading slot %lu", slot ));

Expand Down Expand Up @@ -405,10 +407,18 @@ runtime_replay( fd_ledger_args_t * ledger_args ) {
continue;
}

ledger_args->slot_ctx->slot_bank.tick_height = ledger_args->slot_ctx->slot_bank.max_tick_height;
if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS != fd_runtime_compute_max_tick_height( ledger_args->epoch_ctx->epoch_bank.ticks_per_slot, slot, &ledger_args->slot_ctx->slot_bank.max_tick_height ) ) ) {
FD_LOG_ERR(( "couldn't compute max tick height slot %lu ticks_per_slot %lu", slot, ledger_args->epoch_ctx->epoch_bank.ticks_per_slot ));
ulong * max_tick_height = fd_bank_mgr_max_tick_height_query( ledger_args->slot_ctx->bank_mgr );
ulong * ticks_per_slot = fd_bank_mgr_ticks_per_slot_query( ledger_args->slot_ctx->bank_mgr );
ulong * tick_height = fd_bank_mgr_tick_height_modify( ledger_args->slot_ctx->bank_mgr );
*tick_height = *max_tick_height;
fd_bank_mgr_tick_height_save( ledger_args->slot_ctx->bank_mgr );

max_tick_height = fd_bank_mgr_max_tick_height_modify( ledger_args->slot_ctx->bank_mgr );

if( FD_UNLIKELY( FD_RUNTIME_EXECUTE_SUCCESS != fd_runtime_compute_max_tick_height( *ticks_per_slot, slot, max_tick_height ) ) ) {
FD_LOG_ERR(( "couldn't compute max tick height slot %lu ticks_per_slot %lu", slot, *ticks_per_slot ));
}
fd_bank_mgr_max_tick_height_save( ledger_args->slot_ctx->bank_mgr );

if( ledger_args->slot_ctx->root_slot%ledger_args->snapshot_freq==0UL && !ledger_args->is_snapshotting ) {

Expand Down Expand Up @@ -455,8 +465,9 @@ runtime_replay( fd_ledger_args_t * ledger_args ) {
}

ulong blk_txn_cnt = 0UL;
FD_LOG_NOTICE(( "Used memory in spad before slot=%lu %lu", ledger_args->slot_ctx->slot_bank.slot, ledger_args->runtime_spad->mem_used ));
FD_LOG_NOTICE(( "Used memory in spad before slot=%lu %lu", slot, ledger_args->runtime_spad->mem_used ));
FD_TEST( fd_runtime_block_eval_tpool( ledger_args->slot_ctx,
slot,
blk,
ledger_args->capture_ctx,
ledger_args->tpool,
Expand All @@ -471,11 +482,11 @@ runtime_replay( fd_ledger_args_t * ledger_args ) {
fd_hash_t expected;
int err = fd_blockstore_block_hash_query( blockstore, slot, &expected );
if( FD_UNLIKELY( err ) ) FD_LOG_ERR( ( "slot %lu is missing its hash", slot ) );
else if( FD_UNLIKELY( 0 != memcmp( ledger_args->slot_ctx->slot_bank.poh.hash, expected.hash, 32UL ) ) ) {
else if( FD_UNLIKELY( 0 != memcmp( fd_bank_mgr_poh_query( ledger_args->slot_ctx->bank_mgr )->hash, expected.hash, sizeof(fd_hash_t) ) ) ) {
char expected_hash[ FD_BASE58_ENCODED_32_SZ ];
fd_acct_addr_cstr( expected_hash, expected.hash );
char poh_hash[ FD_BASE58_ENCODED_32_SZ ];
fd_acct_addr_cstr( poh_hash, ledger_args->slot_ctx->slot_bank.poh.hash );
fd_acct_addr_cstr( poh_hash, fd_bank_mgr_poh_query( ledger_args->slot_ctx->bank_mgr )->hash );
FD_LOG_WARNING(( "PoH hash mismatch! slot=%lu expected=%s, got=%s",
slot,
expected_hash,
Expand Down Expand Up @@ -503,17 +514,18 @@ runtime_replay( fd_ledger_args_t * ledger_args ) {
}
}

fd_hash_t * bank_hash_bm = fd_bank_mgr_bank_hash_query( ledger_args->slot_ctx->bank_mgr );
err = fd_blockstore_bank_hash_query( blockstore, slot, &expected );
if( FD_UNLIKELY( err) ) {
FD_LOG_ERR(( "slot %lu is missing its bank hash", slot ));
} else if( FD_UNLIKELY( 0 != memcmp( ledger_args->slot_ctx->slot_bank.banks_hash.hash,
} else if( FD_UNLIKELY( 0 != memcmp( bank_hash_bm,
expected.hash,
32UL ) ) ) {

char expected_hash[ FD_BASE58_ENCODED_32_SZ ];
fd_acct_addr_cstr( expected_hash, expected.hash );
char bank_hash[ FD_BASE58_ENCODED_32_SZ ];
fd_acct_addr_cstr( bank_hash, ledger_args->slot_ctx->slot_bank.banks_hash.hash );
fd_acct_addr_cstr( bank_hash, bank_hash_bm->hash );

FD_LOG_WARNING(( "Bank hash mismatch! slot=%lu expected=%s, got=%s",
slot,
Expand Down Expand Up @@ -718,7 +730,7 @@ fd_ledger_main_setup( fd_ledger_args_t * args ) {

/* Finish other runtime setup steps */
fd_features_restore( args->slot_ctx, args->runtime_spad );
fd_runtime_update_leaders( args->slot_ctx, args->slot_ctx->slot_bank.slot, args->runtime_spad );
fd_runtime_update_leaders( args->slot_ctx, args->slot_ctx->slot, args->runtime_spad );
fd_calculate_epoch_accounts_hash_values( args->slot_ctx );

fd_exec_para_cb_ctx_t exec_para_ctx = {
Expand Down Expand Up @@ -1156,11 +1168,11 @@ ingest( fd_ledger_args_t * args ) {

/* At this point the account state has been ingested into funk. Intake rocksdb */
if( args->start_slot == 0 ) {
args->start_slot = slot_ctx->slot_bank.slot + 1;
args->start_slot = slot_ctx->slot + 1;
}
fd_blockstore_t * blockstore = args->blockstore;
if( blockstore ) {
blockstore->shmem->lps = blockstore->shmem->hcs = blockstore->shmem->wmk = slot_ctx->slot_bank.slot;
blockstore->shmem->lps = blockstore->shmem->hcs = blockstore->shmem->wmk = slot_ctx->slot;
}

if( args->funk_only ) {
Expand All @@ -1169,8 +1181,8 @@ ingest( fd_ledger_args_t * args ) {
FD_LOG_NOTICE(( "using shredcap" ));
fd_shredcap_populate_blockstore( args->shredcap, blockstore, args->start_slot, args->end_slot );
} else if( args->rocksdb_list[ 0UL ] ) {
if( args->end_slot >= slot_ctx->slot_bank.slot + args->slot_history_max ) {
args->end_slot = slot_ctx->slot_bank.slot + args->slot_history_max - 1;
if( args->end_slot >= slot_ctx->slot + args->slot_history_max ) {
args->end_slot = slot_ctx->slot + args->slot_history_max - 1;
}
ingest_rocksdb( args->rocksdb_list[ 0UL ], args->start_slot, args->end_slot,
blockstore, args->copy_txn_status, args->trash_hash, args->valloc );
Expand Down Expand Up @@ -1269,24 +1281,31 @@ replay( fd_ledger_args_t * args ) {
fd_exec_epoch_ctx_bank_mem_clear( args->epoch_ctx );

/* TODO: This is very hacky, needs to be cleaned up */
args->epoch_ctx->epoch_bank.cluster_version[0] = args->cluster_version[0];
args->epoch_ctx->epoch_bank.cluster_version[1] = args->cluster_version[1];
args->epoch_ctx->epoch_bank.cluster_version[2] = args->cluster_version[2];

void * slot_ctx_mem = fd_spad_alloc_check( spad, FD_EXEC_SLOT_CTX_ALIGN, FD_EXEC_SLOT_CTX_FOOTPRINT );
args->slot_ctx = fd_exec_slot_ctx_join( fd_exec_slot_ctx_new( slot_ctx_mem, spad ) );
args->slot_ctx->epoch_ctx = args->epoch_ctx;
args->slot_ctx->funk = funk;
args->slot_ctx->blockstore = args->blockstore;

FD_TEST( args->slot_ctx->bank_mgr_mem );
args->slot_ctx->bank_mgr = fd_bank_mgr_join( fd_bank_mgr_new( args->slot_ctx->bank_mgr_mem ), funk, NULL );

fd_cluster_version_t * cluster_version = fd_bank_mgr_cluster_version_modify( args->slot_ctx->bank_mgr );

cluster_version->major = args->cluster_version[0];
cluster_version->minor = args->cluster_version[1];
cluster_version->patch = args->cluster_version[2];
fd_bank_mgr_cluster_version_save( args->slot_ctx->bank_mgr );

args->epoch_ctx->runtime_public = runtime_public;

fd_features_enable_cleaned_up( &args->epoch_ctx->features, args->epoch_ctx->epoch_bank.cluster_version );
fd_features_enable_cleaned_up( &args->epoch_ctx->features, cluster_version );
fd_features_enable_one_offs( &args->epoch_ctx->features, args->one_off_features, args->one_off_features_cnt, 0UL );

// activate them
fd_memcpy( &args->epoch_ctx->runtime_public->features, &args->epoch_ctx->features, sizeof(fd_features_t) );

void * slot_ctx_mem = fd_spad_alloc_check( spad, FD_EXEC_SLOT_CTX_ALIGN, FD_EXEC_SLOT_CTX_FOOTPRINT );
args->slot_ctx = fd_exec_slot_ctx_join( fd_exec_slot_ctx_new( slot_ctx_mem, spad ) );
args->slot_ctx->epoch_ctx = args->epoch_ctx;
args->slot_ctx->funk = funk;
args->slot_ctx->blockstore = args->blockstore;

void * status_cache_mem = fd_spad_alloc_check( spad,
FD_TXNCACHE_ALIGN,
fd_txncache_footprint( FD_TXNCACHE_DEFAULT_MAX_ROOTED_SLOTS,
Expand Down Expand Up @@ -1347,7 +1366,11 @@ replay( fd_ledger_args_t * args ) {

fd_ledger_main_setup( args );

fd_blockstore_init( args->blockstore, -1, FD_BLOCKSTORE_ARCHIVE_MIN_SIZE, &args->slot_ctx->slot_bank );
fd_blockstore_init( args->blockstore,
-1,
FD_BLOCKSTORE_ARCHIVE_MIN_SIZE,
&args->slot_ctx->slot_bank,
args->slot_ctx->slot );
fd_buf_shred_pool_reset( args->blockstore->shred_pool, 0 );

FD_LOG_WARNING(( "setup done" ));
Expand Down
8 changes: 6 additions & 2 deletions src/app/shared_dev/commands/configure/genesis.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ create_genesis( config_t const * config,

fd_features_t features[1];
fd_features_disable_all( features );
uint version[] = {FD_DEFAULT_AGAVE_CLUSTER_VERSION_MAJOR, FD_DEFAULT_AGAVE_CLUSTER_VERSION_MINOR, FD_DEFAULT_AGAVE_CLUSTER_VERSION_PATCH};
fd_features_enable_cleaned_up(features, version);
fd_cluster_version_t cluster_version = {
.major = FD_DEFAULT_AGAVE_CLUSTER_VERSION_MAJOR,
.minor = FD_DEFAULT_AGAVE_CLUSTER_VERSION_MINOR,
.patch = FD_DEFAULT_AGAVE_CLUSTER_VERSION_PATCH
};
fd_features_enable_cleaned_up( features, &cluster_version );
default_enable_features( features );

options->features = features;
Expand Down
15 changes: 9 additions & 6 deletions src/choreo/epoch/fd_epoch.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "fd_epoch.h"

#include "../../flamenco/runtime/fd_bank_mgr.h"
void *
fd_epoch_new( void * shmem, ulong voter_max ) {
if( FD_UNLIKELY( !shmem ) ) {
Expand Down Expand Up @@ -103,12 +103,15 @@ fd_epoch_delete( void * epoch ) {
}

void
fd_epoch_init( fd_epoch_t * epoch, fd_epoch_bank_t const * epoch_bank ) {
epoch->first_slot = epoch_bank->eah_start_slot;
epoch->last_slot = epoch_bank->eah_stop_slot;
fd_epoch_init( fd_epoch_t * epoch,
ulong eah_start_slot,
ulong eah_stop_slot,
fd_vote_accounts_t const * vote_accounts ) {

epoch->first_slot = eah_start_slot;
epoch->last_slot = eah_stop_slot;

fd_voter_t * epoch_voters = fd_epoch_voters( epoch );
fd_vote_accounts_t const * vote_accounts = &epoch_bank->stakes.vote_accounts;
fd_voter_t * epoch_voters = fd_epoch_voters( epoch );

for( fd_vote_accounts_pair_t_mapnode_t * curr = fd_vote_accounts_pair_t_map_minimum(
vote_accounts->vote_accounts_pool,
Expand Down
5 changes: 4 additions & 1 deletion src/choreo/epoch/fd_epoch.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ fd_epoch_delete( void * epoch );
epoch. */

void
fd_epoch_init( fd_epoch_t * epoch, fd_epoch_bank_t const * epoch_bank );
fd_epoch_init( fd_epoch_t * epoch,
ulong eah_start_slot,
ulong eah_stop_slot,
fd_vote_accounts_t const * vote_accounts );

/* fd_epoch_fini finishes an epoch. Assumes epoch is a valid local join
and epoch has already been initialized. This should only be called
Expand Down
25 changes: 11 additions & 14 deletions src/choreo/forks/fd_forks.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "../../flamenco/runtime/fd_runtime.h"
#include "../../flamenco/runtime/program/fd_program_util.h"
#include "../../flamenco/runtime/program/fd_vote_program.h"

#include "../../flamenco/runtime/fd_bank_mgr.h"
void *
fd_forks_new( void * shmem, ulong max, ulong seed ) {

Expand Down Expand Up @@ -115,7 +115,7 @@ fd_forks_init( fd_forks_t * forks, fd_exec_slot_ctx_t * slot_ctx ) {
}

fd_fork_t * fork = fd_fork_pool_ele_acquire( forks->pool );
fork->slot = slot_ctx->slot_bank.slot;
fork->slot = slot_ctx->slot;
fork->prev = fd_fork_pool_idx_null( forks->pool );
fork->lock = 0;
fork->end_idx = UINT_MAX;
Expand Down Expand Up @@ -167,8 +167,8 @@ fd_forks_query_const( fd_forks_t const * forks, ulong slot ) {
// // fork is advancing
// FD_LOG_DEBUG(( "new block execution - slot: %lu, parent_slot: %lu", curr_slot, parent_slot ));

// fork->slot_ctx->slot_bank.prev_slot = fork->slot_ctx->slot_bank.slot;
// fork->slot_ctx->slot_bank.slot = curr_slot;
// fork->slot_ctx->slot_bank.prev_slot = fork->slot_ctx->slot;
// fork->slot_ctx->slot = curr_slot;

// fork->slot_ctx.status_cache = status_cache;
// fd_funk_txn_xid_t xid;
Expand Down Expand Up @@ -245,6 +245,7 @@ slot_ctx_restore( ulong slot,
continue;
}

slot_ctx_out->slot = slot;
slot_ctx_out->slot_bank = *slot_bank;
FD_TEST( !fd_runtime_sysvar_cache_load( slot_ctx_out, runtime_spad ) );

Expand All @@ -258,21 +259,17 @@ slot_ctx_restore( ulong slot,

// signature_cnt, account_delta_hash, prev_banks_hash are used for the banks
// hash calculation and not needed when restoring parent

FD_LOG_NOTICE( ( "recovered slot_bank for slot=%lu banks_hash=%s poh_hash %s",
slot_ctx_out->slot_bank.slot,
FD_BASE58_ENC_32_ALLOCA( slot_ctx_out->slot_bank.banks_hash.hash ),
FD_BASE58_ENC_32_ALLOCA( slot_ctx_out->slot_bank.poh.hash ) ) );
fd_hash_t * bank_hash = fd_bank_mgr_bank_hash_query( slot_ctx_out->bank_mgr );
FD_LOG_NOTICE(( "recovered slot_bank for slot=%lu banks_hash=%s",
slot_ctx_out->slot,
FD_BASE58_ENC_32_ALLOCA( bank_hash->hash ) ));

/* Prepare bank for next slot */
slot_ctx_out->slot_bank.slot = slot;
slot_ctx_out->slot_bank.collected_execution_fees = 0;
slot_ctx_out->slot_bank.collected_priority_fees = 0;
slot_ctx_out->slot_bank.collected_rent = 0;
slot_ctx_out->slot = slot;

/* FIXME epoch boundary stuff when replaying */
// fd_features_restore( slot_ctx );
// fd_runtime_update_leaders( slot_ctx, slot_ctx->slot_bank.slot );
// fd_runtime_update_leaders( slot_ctx, slot_ctx->slot );
// fd_calculate_epoch_accounts_hash_values( slot_ctx );
}

Expand Down
16 changes: 9 additions & 7 deletions src/discof/batch/fd_batch_tile.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,18 +381,20 @@ produce_eah( fd_snapshot_tile_ctx_t * ctx, fd_stem_context_t * stem, ulong batch
FD_LOG_ERR(( "Slot bank record has wrong magic" ));
}

int err;
fd_slot_bank_t * slot_bank = fd_bincode_decode_spad( slot_bank, ctx->spad, (uchar *)slot_val+sizeof(uint), fd_funk_val_sz( slot_rec )-sizeof(uint), &err );
if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
FD_LOG_ERR(( "Failed to read slot bank record: invalid decode" ));
continue;
}
/* FIXME: The slot bank is getting replaced with the bank manager. */
// int err;
// fd_slot_bank_t * slot_bank = fd_bincode_decode_spad( slot_bank, ctx->spad, (uchar *)slot_val+sizeof(uint), fd_funk_val_sz( slot_rec )-sizeof(uint), &err );
// if( FD_UNLIKELY( err!=FD_BINCODE_SUCCESS ) ) {
// FD_LOG_ERR(( "Failed to read slot bank record: invalid decode" ));
// continue;
// }

/* At this point, calculate the epoch account hash. */

fd_hash_t epoch_account_hash = {0};

fd_accounts_hash( funk, slot_bank, &epoch_account_hash, ctx->spad, &ctx->runtime_public->features, NULL, NULL );
/* FIXME: this has an invalid slot number. */
fd_accounts_hash( funk, 0UL, &epoch_account_hash, ctx->spad, &ctx->runtime_public->features, NULL, NULL );

FD_LOG_NOTICE(( "Done computing epoch account hash (%s)", FD_BASE58_ENC_32_ALLOCA( &epoch_account_hash ) ));

Expand Down
6 changes: 3 additions & 3 deletions src/discof/consensus/test_consensus.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,14 @@ main( void ) {
// if( incremental_snapshot ) {
// ulong i, j;
// FD_TEST( sscanf( incremental_snapshot, "incremental-snapshot-%lu-%lu", &i, &j ) == 2 );
// FD_TEST( i == snapshot_slot_ctx->slot_bank.slot );
// FD_TEST( i == snapshot_slot_ctx->slot );
// FD_TEST( epoch_bank );
// FD_TEST( fd_slot_to_epoch( &epoch_bank->epoch_schedule, i, NULL ) ==
// fd_slot_to_epoch( &epoch_bank->epoch_schedule, j, NULL ) );
// fd_snapshot_load_all( incremental_snapshot, NULL, snapshot_slot_ctx, 1, 1, FD_SNAPSHOT_TYPE_INCREMENTAL );
// }

// ulong snapshot_slot = snapshot_slot_ctx->slot_bank.slot;
// ulong snapshot_slot = snapshot_slot_ctx->slot;
// FD_LOG_NOTICE( ( "snapshot_slot: %lu", snapshot_slot ) );

// snapshot_fork->slot = snapshot_slot;
Expand All @@ -568,7 +568,7 @@ main( void ) {
// FD_TEST( !fd_runtime_sysvar_cache_load( snapshot_slot_ctx ) );

// fd_features_restore( snapshot_slot_ctx );
// fd_runtime_update_leaders( snapshot_slot_ctx, snapshot_slot_ctx->slot_bank.slot );
// fd_runtime_update_leaders( snapshot_slot_ctx, snapshot_slot_ctx->slot );
// fd_calculate_epoch_accounts_hash_values( snapshot_slot_ctx );

// fd_funk_start_write( funk );
Expand Down
Loading
Loading