Line data Source code
1 : #include <stdio.h>
2 : #include <stdlib.h>
3 : #include <stddef.h>
4 : #include <alloca.h>
5 : #include <unistd.h>
6 : #include <sys/types.h>
7 : #include <sys/stat.h>
8 : #include <fcntl.h>
9 : #include <errno.h>
10 : #include <strings.h>
11 : #include "../../choreo/fd_choreo.h"
12 : #include "../../disco/fd_disco.h"
13 : #include "../../util/fd_util.h"
14 : #include "../../flamenco/fd_flamenco.h"
15 : #include "../../flamenco/nanopb/pb_decode.h"
16 : #include "../../flamenco/runtime/fd_hashes.h"
17 : #include "../../funk/fd_funk_filemap.h"
18 : #include "../../flamenco/types/fd_types.h"
19 : #include "../../flamenco/runtime/fd_runtime.h"
20 : #include "../../flamenco/runtime/fd_account.h"
21 : #include "../../flamenco/runtime/fd_rocksdb.h"
22 : #include "../../flamenco/runtime/fd_txncache.h"
23 : #include "../../ballet/base58/fd_base58.h"
24 : #include "../../flamenco/types/fd_solana_block.pb.h"
25 : #include "../../flamenco/runtime/context/fd_capture_ctx.h"
26 : #include "../../flamenco/runtime/fd_blockstore.h"
27 : #include "../../flamenco/runtime/program/fd_builtin_programs.h"
28 : #include "../../flamenco/shredcap/fd_shredcap.h"
29 : #include "../../flamenco/runtime/program/fd_bpf_program_util.h"
30 : #include "../../flamenco/snapshot/fd_snapshot.h"
31 : #include "../../flamenco/snapshot/fd_snapshot_create.h"
32 :
33 : extern void fd_write_builtin_bogus_account( fd_exec_slot_ctx_t * slot_ctx, uchar const pubkey[ static 32 ], char const * data, ulong sz );
34 :
35 : struct fd_ledger_args {
36 : fd_wksp_t * wksp; /* wksp for blockstore */
37 : fd_wksp_t * funk_wksp; /* wksp for funk */
38 : fd_wksp_t * status_cache_wksp; /* wksp for status cache. */
39 : fd_blockstore_t * blockstore; /* blockstore for replay */
40 : fd_funk_t * funk; /* handle to funk */
41 : fd_alloc_t * alloc; /* handle to alloc */
42 : char const * cmd; /* user passed command to fd_ledger */
43 : ulong start_slot; /* start slot for offline replay */
44 : ulong end_slot; /* end slot for offline replay */
45 : uint hashseed; /* hashseed */
46 : char const * checkpt; /* wksp checkpoint */
47 : char const * checkpt_funk; /* wksp checkpoint for a funk wksp */
48 : char const * checkpt_status_cache; /* status cache checkpoint */
49 : char const * restore; /* wksp restore */
50 : char const * restore_funk; /* wksp restore for a funk wksp */
51 : char const * allocator; /* allocator used during replay (libc/wksp) */
52 : ulong shred_max; /* maximum number of shreds*/
53 : ulong slot_history_max; /* number of slots stored by blockstore*/
54 : ulong txns_max; /* txns_max*/
55 : ulong index_max; /* size of funk index (same as rec max) */
56 : char const * funk_file; /* path to funk backing store */
57 : ulong funk_page_cnt;
58 : fd_funk_close_file_args_t funk_close_args;
59 : char const * snapshot; /* path to agave snapshot */
60 : char const * incremental; /* path to agave incremental snapshot */
61 : char const * genesis; /* path to agave genesis */
62 : char const * mini_db_dir; /* path to minifed rocksdb that's to be created */
63 : int copy_txn_status; /* determine if txns should be copied to the blockstore during minify/replay */
64 : int funk_only; /* determine if only funk should be ingested */
65 : char const * shredcap; /* path to replay using shredcap instead of rocksdb */
66 : int abort_on_mismatch; /* determine if execution should abort on mismatch*/
67 : ulong pages_pruned; /* ledger pruning: how many pages should the pruned wksp have */
68 : ulong index_max_pruned; /* ledger pruning: how large should the pruned funk index be */
69 : fd_funk_t * pruned_funk; /* ledger pruning: funk used by the pruned wksp */
70 : char const * capture_fpath; /* solcap: path for solcap file to be created */
71 : int capture_txns; /* solcap: determine if transaction results should be captured for solcap*/
72 : char const * checkpt_path; /* path to dump funk wksp checkpoints during execution*/
73 : ulong checkpt_freq; /* how often funk wksp checkpoints will be dumped (defaults to never) */
74 : int checkpt_mismatch; /* determine if a funk wksp checkpoint should be dumped on a mismatch*/
75 :
76 : int dump_insn_to_pb; /* instruction dumping: should insns be dumped */
77 : int dump_txn_to_pb; /* txn dumping: should txns be dumped */
78 : ulong dump_proto_start_slot; /* instruction / txn dumping: what slot to start dumping*/
79 : char const * dump_proto_sig_filter; /* instruction / txn dumping: specify txn sig to dump at */
80 : char const * dump_proto_output_dir; /* instruction / txn dumping: output directory for protobuf messages */
81 :
82 : int verify_funk; /* verify funk before execution starts */
83 : uint verify_acc_hash; /* verify account hash from the snapshot */
84 : uint check_acc_hash; /* check account hash by reconstructing with data */
85 : ulong trash_hash; /* trash hash to be used for negative cases*/
86 : ulong vote_acct_max; /* max number of vote accounts */
87 : char const * rocksdb_list[32]; /* max number of rocksdb dirs that can be passed in */
88 : ulong rocksdb_list_slot[32]; /* start slot for each rocksdb dir that's passed in assuming there are mulitple */
89 : ulong rocksdb_list_cnt; /* number of rocksdb dirs passed in */
90 : uint cluster_version[3]; /* What version of solana is the genesis block? */
91 : char const * one_off_features[32]; /* List of one off feature pubkeys to enable for execution agnostic of cluster version */
92 : uint one_off_features_cnt; /* Number of one off features */
93 : ulong snapshot_freq; /* How often a snapshot should be produced */
94 : ulong incremental_freq; /* How often an incremental snapshot should be produced */
95 : char const * snapshot_dir; /* Directory to create a snapshot in */
96 : ulong snapshot_tcnt; /* Number of threads to use for snapshot creation */
97 : double allowed_mem_delta; /* Percent of memory in the blockstore wksp that can be
98 : used and not freed between the start of end of execution.
99 : If the difference in usage exceeds this value, error out. */
100 :
101 : /* These values are setup and maintained before replay */
102 : fd_capture_ctx_t * capture_ctx; /* capture_ctx is used in runtime_replay for various debugging tasks */
103 : fd_acc_mgr_t acc_mgr[ 1UL ]; /* funk wrapper*/
104 : fd_exec_slot_ctx_t * slot_ctx; /* slot_ctx */
105 : fd_exec_epoch_ctx_t * epoch_ctx; /* epoch_ctx */
106 : fd_tpool_t * tpool; /* thread pool for execution */
107 : uchar tpool_mem[FD_TPOOL_FOOTPRINT( FD_TILE_MAX )] __attribute__( ( aligned( FD_TPOOL_ALIGN ) ) );
108 :
109 : fd_spad_t * spads[ 128UL ]; /* scratchpad allocators that are eventually assigned to each txn_ctx */
110 : ulong spad_cnt; /* number of scratchpads, bounded by number of threads */
111 : fd_tpool_t * snapshot_tpool; /* thread pool for snapshot creation */
112 : uchar tpool_mem_snapshot[FD_TPOOL_FOOTPRINT( FD_TILE_MAX )] __attribute__( ( aligned( FD_TPOOL_ALIGN ) ) );
113 : fd_tpool_t * snapshot_bg_tpool; /* thread pool for snapshot creation */
114 : uchar tpool_mem_snapshot_bg[FD_TPOOL_FOOTPRINT( FD_TILE_MAX )] __attribute__( ( aligned( FD_TPOOL_ALIGN ) ) );
115 : ulong last_snapshot_slot; /* last snapshot slot */
116 : fd_hash_t last_snapshot_hash; /* last snapshot hash */
117 : ulong last_snapshot_cap; /* last snapshot account capitalization */
118 : int is_snapshotting; /* determine if a snapshot is being created */
119 : int snapshot_mismatch; /* determine if a snapshot should be created on a mismatch */
120 :
121 : char const * lthash;
122 : };
123 : typedef struct fd_ledger_args fd_ledger_args_t;
124 :
125 : /* Snapshot *******************************************************************/
126 :
127 : static void
128 : fd_create_snapshot_task( void FD_PARAM_UNUSED *tpool,
129 : ulong t0 FD_PARAM_UNUSED, ulong t1 FD_PARAM_UNUSED,
130 : void *args FD_PARAM_UNUSED,
131 : void *reduce FD_PARAM_UNUSED, ulong stride FD_PARAM_UNUSED,
132 : ulong l0 FD_PARAM_UNUSED, ulong l1 FD_PARAM_UNUSED,
133 : ulong m0 FD_PARAM_UNUSED, ulong m1 FD_PARAM_UNUSED,
134 0 : ulong n0 FD_PARAM_UNUSED, ulong n1 FD_PARAM_UNUSED ) {
135 :
136 0 : fd_snapshot_ctx_t * snapshot_ctx = (fd_snapshot_ctx_t *)t0;
137 0 : fd_ledger_args_t * ledger_args = (fd_ledger_args_t *)t1;
138 :
139 0 : char tmp_dir_buf[ FD_SNAPSHOT_DIR_MAX ];
140 0 : int err = snprintf( tmp_dir_buf, FD_SNAPSHOT_DIR_MAX, "%s/%s",
141 0 : snapshot_ctx->out_dir,
142 0 : snapshot_ctx->is_incremental ? FD_SNAPSHOT_TMP_INCR_ARCHIVE : FD_SNAPSHOT_TMP_ARCHIVE );
143 0 : if( FD_UNLIKELY( err<0 ) ) {
144 0 : FD_LOG_WARNING(( "Failed to format directory string" ));
145 0 : return;
146 0 : }
147 :
148 0 : char zstd_dir_buf[ FD_SNAPSHOT_DIR_MAX ];
149 0 : err = snprintf( zstd_dir_buf, FD_SNAPSHOT_DIR_MAX, "%s/%s",
150 0 : snapshot_ctx->out_dir,
151 0 : snapshot_ctx->is_incremental ? FD_SNAPSHOT_TMP_INCR_ARCHIVE_ZSTD : FD_SNAPSHOT_TMP_FULL_ARCHIVE_ZSTD );
152 0 : if( FD_UNLIKELY( err<0 ) ) {
153 0 : FD_LOG_WARNING(( "Failed to format directory string" ));
154 0 : return;
155 0 : }
156 :
157 : /* Create and open the relevant files for snapshots. */
158 :
159 0 : snapshot_ctx->tmp_fd = open( tmp_dir_buf, O_CREAT | O_RDWR | O_TRUNC, 0644 );
160 0 : if( FD_UNLIKELY( snapshot_ctx->tmp_fd==-1 ) ) {
161 0 : FD_LOG_WARNING(( "Failed to open and create tarball for file=%s (%i-%s)", tmp_dir_buf, errno, fd_io_strerror( errno ) ));
162 0 : return;
163 0 : }
164 :
165 0 : snapshot_ctx->snapshot_fd = open( zstd_dir_buf, O_RDWR | O_CREAT | O_TRUNC, 0644 );
166 0 : if( FD_UNLIKELY( snapshot_ctx->snapshot_fd==-1 ) ) {
167 0 : FD_LOG_WARNING(( "Failed to open the snapshot file (%i-%s)", errno, fd_io_strerror( errno ) ));
168 0 : return;
169 0 : }
170 :
171 0 : FD_LOG_WARNING(( "Starting snapshot creation at slot=%lu", snapshot_ctx->slot ));
172 :
173 0 : fd_snapshot_create_new_snapshot( snapshot_ctx,
174 0 : &ledger_args->last_snapshot_hash,
175 0 : &ledger_args->last_snapshot_cap );
176 :
177 0 : FD_LOG_NOTICE(( "Successfully produced a snapshot at directory=%s", ledger_args->snapshot_dir ));
178 :
179 0 : ledger_args->slot_ctx->epoch_ctx->constipate_root = 0;
180 0 : ledger_args->is_snapshotting = 0;
181 :
182 0 : err = close( snapshot_ctx->tmp_fd );
183 0 : if( FD_UNLIKELY( err ) ) {
184 0 : FD_LOG_ERR(( "failed to close tmp_fd" ));
185 0 : }
186 :
187 0 : err = close( snapshot_ctx->snapshot_fd );
188 0 : if( FD_UNLIKELY( err ) ) {
189 0 : FD_LOG_ERR(( "failed to close snapshot_fd" ));
190 0 : }
191 :
192 0 : }
193 :
194 : /* Runtime Replay *************************************************************/
195 : static int
196 0 : init_tpool( fd_ledger_args_t * ledger_args ) {
197 :
198 0 : ulong snapshot_tcnt = ledger_args->snapshot_tcnt;
199 :
200 0 : ulong tcnt = fd_tile_cnt() - snapshot_tcnt;
201 0 : uchar * tpool_scr_mem = NULL;
202 0 : fd_tpool_t * tpool = NULL;
203 :
204 0 : ulong start_idx = 1UL;
205 0 : if( tcnt>=1UL ) {
206 0 : tpool = fd_tpool_init( ledger_args->tpool_mem, tcnt );
207 0 : if( tpool == NULL ) {
208 0 : FD_LOG_ERR(( "failed to create thread pool" ));
209 0 : }
210 0 : ulong scratch_sz = fd_scratch_smem_footprint( 256UL<<20UL );
211 0 : tpool_scr_mem = fd_valloc_malloc( ledger_args->slot_ctx->valloc, FD_SCRATCH_SMEM_ALIGN, scratch_sz*(tcnt) );
212 0 : if( tpool_scr_mem == NULL ) {
213 0 : FD_LOG_ERR( ( "failed to allocate thread pool scratch space" ) );
214 0 : }
215 0 : for( ulong i=1UL; i<tcnt; ++i ) {
216 0 : if( fd_tpool_worker_push( tpool, start_idx++, tpool_scr_mem + scratch_sz*(i-1UL), scratch_sz ) == NULL ) {
217 0 : FD_LOG_ERR(( "failed to launch worker" ));
218 0 : }
219 0 : else {
220 0 : FD_LOG_NOTICE(( "launched worker %lu", start_idx - 1UL ));
221 0 : }
222 0 : }
223 0 : }
224 :
225 0 : ledger_args->tpool = tpool;
226 :
227 : /* Setup a background thread for the snapshot service as well as a tpool
228 : used for snapshot hashing. */
229 :
230 0 : if( !snapshot_tcnt ) {
231 0 : return 0;
232 0 : }
233 :
234 0 : else if( snapshot_tcnt==1UL ) {
235 0 : FD_LOG_ERR(( "This is an invalid value for the number of threads to use for snapshot creation" ));
236 0 : }
237 :
238 0 : fd_tpool_t * snapshot_bg_tpool = fd_tpool_init( ledger_args->tpool_mem_snapshot_bg, snapshot_tcnt );
239 0 : ulong scratch_sz = fd_scratch_smem_footprint( 256UL<<20UL );
240 0 : tpool_scr_mem = fd_valloc_malloc( ledger_args->slot_ctx->valloc, FD_SCRATCH_SMEM_ALIGN, scratch_sz );
241 0 : if( FD_UNLIKELY( !fd_tpool_worker_push( snapshot_bg_tpool, start_idx++, tpool_scr_mem, scratch_sz ) ) ) {
242 0 : FD_LOG_ERR(( "failed to launch worker" ));
243 0 : } else {
244 0 : FD_LOG_NOTICE(( "launched snapshot bg worker %lu", start_idx - 1UL ));
245 0 : }
246 :
247 0 : ledger_args->snapshot_bg_tpool = snapshot_bg_tpool;
248 :
249 :
250 0 : if( snapshot_tcnt==2UL ) {
251 0 : return 0;
252 0 : }
253 :
254 : /* If a snapshot is being created, setup its own tpool. */
255 :
256 0 : fd_tpool_t * snapshot_tpool = fd_tpool_init( ledger_args->tpool_mem_snapshot, snapshot_tcnt - 1UL );
257 0 : scratch_sz = fd_scratch_smem_footprint( 256UL<<20UL );
258 0 : tpool_scr_mem = fd_valloc_malloc( ledger_args->slot_ctx->valloc, FD_SCRATCH_SMEM_ALIGN, scratch_sz );
259 0 : for( ulong i=1UL; i<snapshot_tcnt - 1UL; ++i ) {
260 0 : if( FD_UNLIKELY( !fd_tpool_worker_push( snapshot_tpool, start_idx++, tpool_scr_mem + scratch_sz*(i-1UL), scratch_sz ) ) ) {
261 0 : FD_LOG_ERR(( "failed to launch worker" ));
262 0 : } else {
263 0 : FD_LOG_NOTICE(( "launched snapshot hash %lu", start_idx - 1UL ));
264 0 : }
265 0 : }
266 :
267 0 : ledger_args->snapshot_tpool = snapshot_tpool;
268 :
269 0 : return 0;
270 0 : }
271 :
272 : int
273 0 : runtime_replay( fd_ledger_args_t * ledger_args ) {
274 0 : fd_features_restore( ledger_args->slot_ctx );
275 :
276 0 : fd_runtime_update_leaders( ledger_args->slot_ctx, ledger_args->slot_ctx->slot_bank.slot );
277 :
278 0 : fd_calculate_epoch_accounts_hash_values( ledger_args->slot_ctx );
279 :
280 0 : long replay_time = -fd_log_wallclock();
281 0 : ulong txn_cnt = 0;
282 0 : ulong slot_cnt = 0;
283 0 : fd_blockstore_t * blockstore = ledger_args->slot_ctx->blockstore;
284 :
285 0 : ulong prev_slot = ledger_args->slot_ctx->slot_bank.slot;
286 0 : ulong start_slot = ledger_args->slot_ctx->slot_bank.slot + 1;
287 :
288 0 : ledger_args->slot_ctx->root_slot = prev_slot;
289 :
290 : /* On demand rocksdb ingest */
291 0 : fd_rocksdb_t rocks_db = {0};
292 0 : fd_rocksdb_root_iter_t iter = {0};
293 0 : fd_slot_meta_t slot_meta = {0};
294 0 : ulong curr_rocksdb_idx = 0UL;
295 :
296 0 : char * err = fd_rocksdb_init( &rocks_db, ledger_args->rocksdb_list[ 0UL ] );
297 0 : if( FD_UNLIKELY( err!=NULL ) ) {
298 0 : FD_LOG_ERR(( "fd_rocksdb_init at path=%s returned error=%s", ledger_args->rocksdb_list[ 0UL ], err ));
299 0 : }
300 0 : fd_rocksdb_root_iter_new( &iter );
301 :
302 0 : int block_found = -1;
303 0 : while ( block_found!=0 && start_slot<=ledger_args->end_slot ) {
304 0 : block_found = fd_rocksdb_root_iter_seek( &iter, &rocks_db, start_slot, &slot_meta, ledger_args->slot_ctx->valloc );
305 0 : if ( block_found!=0 ) {
306 0 : start_slot++;
307 0 : }
308 0 : }
309 :
310 0 : if( FD_UNLIKELY( block_found!=0 ) ) {
311 0 : FD_LOG_ERR(( "unable to seek to any slot" ));
312 0 : }
313 :
314 0 : if( ledger_args->capture_ctx && ledger_args->capture_ctx->pruned_funk != NULL ) {
315 : /* If prune enabled: setup rent partitions */
316 0 : fd_funk_start_write( ledger_args->capture_ctx->pruned_funk );
317 0 : fd_funk_t * funk = ledger_args->slot_ctx->acc_mgr->funk;
318 0 : fd_wksp_t * wksp = fd_funk_wksp( funk );
319 0 : fd_funk_partvec_t * partvec = fd_funk_get_partvec( funk, wksp );
320 0 : fd_funk_t * pruned_funk = ledger_args->capture_ctx->pruned_funk;
321 0 : fd_funk_set_num_partitions( pruned_funk, partvec->num_part );
322 0 : fd_funk_end_write( ledger_args->capture_ctx->pruned_funk );
323 0 : }
324 :
325 : /* Setup trash_hash */
326 0 : uchar trash_hash_buf[32];
327 0 : memset( trash_hash_buf, 0xFE, sizeof(trash_hash_buf) );
328 :
329 0 : ledger_args->is_snapshotting = 0;
330 :
331 : /* Calculate and store wksp free size before execution. */
332 0 : fd_wksp_usage_t init_usage = {0};
333 0 : fd_wksp_usage( fd_blockstore_wksp( ledger_args->blockstore ), NULL, 0UL, &init_usage );
334 :
335 0 : ulong block_slot = start_slot;
336 0 : for( ulong slot = start_slot; slot <= ledger_args->end_slot; ++slot ) {
337 0 : ledger_args->slot_ctx->slot_bank.prev_slot = prev_slot;
338 0 : ledger_args->slot_ctx->slot_bank.slot = slot;
339 :
340 0 : FD_LOG_DEBUG(( "reading slot %lu", slot ));
341 :
342 0 : if( ledger_args->capture_ctx && ledger_args->capture_ctx->pruned_funk != NULL ) {
343 0 : fd_funk_start_write( ledger_args->capture_ctx->pruned_funk );
344 0 : fd_runtime_collect_rent_accounts_prune( slot, ledger_args->slot_ctx, ledger_args->capture_ctx );
345 0 : fd_funk_end_write( ledger_args->capture_ctx->pruned_funk );
346 0 : }
347 :
348 : /* If we have reached a new block, load one in from rocksdb to the blockstore */
349 0 : fd_blockstore_start_read( blockstore );
350 0 : fd_block_t * block = fd_blockstore_block_query( blockstore, slot );
351 0 : fd_blockstore_end_read( blockstore );
352 0 : if( block == NULL && slot_meta.slot == slot ) {
353 0 : int err = fd_rocksdb_import_block_blockstore( &rocks_db, &slot_meta, blockstore,
354 0 : ledger_args->copy_txn_status, slot == (ledger_args->trash_hash) ? trash_hash_buf : NULL );
355 0 : if( FD_UNLIKELY( err ) ) {
356 0 : FD_LOG_ERR(( "Failed to import block %lu", start_slot ));
357 0 : }
358 :
359 0 : fd_blockstore_start_write( blockstore );
360 :
361 : /* Remove the previous block from the blockstore */
362 0 : if ( FD_LIKELY( block_slot < slot ) ) {
363 : /* Mark the block as successfully processed */
364 0 : fd_block_map_t * block_map_entry = fd_block_map_query( fd_blockstore_block_map( blockstore ), &block_slot, NULL );
365 0 : block_map_entry->flags = fd_uchar_clear_bit( block_map_entry->flags, FD_BLOCK_FLAG_REPLAYING );
366 0 : block_map_entry->flags = fd_uchar_set_bit( block_map_entry->flags, FD_BLOCK_FLAG_PROCESSED );
367 :
368 : /* Remove the old block from the blockstore */
369 0 : fd_blockstore_slot_remove( blockstore, block_slot );
370 0 : }
371 :
372 : /* Mark the new block as replaying */
373 0 : fd_block_map_t * block_map_entry = fd_block_map_query( fd_blockstore_block_map( blockstore ), &slot, NULL );
374 0 : block_map_entry->flags = fd_uchar_set_bit( block_map_entry->flags, FD_BLOCK_FLAG_REPLAYING );
375 :
376 0 : fd_blockstore_end_write( blockstore );
377 :
378 0 : block_slot = slot;
379 0 : }
380 :
381 0 : fd_blockstore_start_read( blockstore );
382 0 : fd_block_t * blk = fd_blockstore_block_query( blockstore, slot );
383 0 : fd_blockstore_end_read( blockstore );
384 0 : if( blk == NULL ) {
385 0 : FD_LOG_WARNING(( "failed to read slot %lu", slot ));
386 : /* TODO: This is currently a hack because ticks are not correctly
387 : computed or handled in the runtime. It is neceesary to update ticks
388 : for skipped slots for snapshot creation. */
389 0 : ledger_args->slot_ctx->slot_bank.tick_height += 64UL;
390 0 : ledger_args->slot_ctx->slot_bank.max_tick_height += 64UL;
391 :
392 0 : continue;
393 0 : }
394 0 : ledger_args->slot_ctx->block = blk;
395 :
396 0 : if( ledger_args->slot_ctx->root_slot%ledger_args->snapshot_freq==0UL && !ledger_args->is_snapshotting ) {
397 :
398 0 : ledger_args->is_snapshotting = 1;
399 :
400 0 : ledger_args->last_snapshot_slot = ledger_args->slot_ctx->root_slot;
401 :
402 0 : fd_snapshot_ctx_t snapshot_ctx = {
403 0 : .slot = ledger_args->slot_ctx->root_slot,
404 0 : .out_dir = ledger_args->snapshot_dir,
405 0 : .is_incremental = 0,
406 0 : .valloc = ledger_args->slot_ctx->valloc,
407 0 : .funk = ledger_args->slot_ctx->acc_mgr->funk,
408 0 : .status_cache = ledger_args->slot_ctx->status_cache,
409 0 : .tpool = ledger_args->snapshot_tpool
410 0 : };
411 :
412 0 : fd_tpool_exec( ledger_args->snapshot_bg_tpool, 1UL, fd_create_snapshot_task, NULL,
413 0 : (ulong)&snapshot_ctx, (ulong)ledger_args, 0UL, NULL,
414 0 : 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL );
415 :
416 0 : } else if( ledger_args->slot_ctx->root_slot%ledger_args->incremental_freq==0UL && !ledger_args->is_snapshotting && ledger_args->last_snapshot_slot ) {
417 :
418 0 : ledger_args->is_snapshotting = 1;
419 :
420 0 : fd_snapshot_ctx_t snapshot_ctx = {
421 0 : .slot = ledger_args->slot_ctx->root_slot,
422 0 : .out_dir = ledger_args->snapshot_dir,
423 0 : .is_incremental = 1,
424 0 : .valloc = ledger_args->slot_ctx->valloc,
425 0 : .funk = ledger_args->slot_ctx->acc_mgr->funk,
426 0 : .status_cache = ledger_args->slot_ctx->status_cache,
427 0 : .last_snap_slot = ledger_args->last_snapshot_slot,
428 0 : .tpool = ledger_args->snapshot_tpool,
429 0 : .last_snap_acc_hash = &ledger_args->last_snapshot_hash,
430 0 : .last_snap_capitalization = ledger_args->last_snapshot_cap
431 0 : };
432 :
433 0 : fd_tpool_exec( ledger_args->snapshot_bg_tpool, 1UL, fd_create_snapshot_task, NULL,
434 0 : (ulong)&snapshot_ctx, (ulong)ledger_args, 0UL, NULL,
435 0 : 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL );
436 0 : }
437 :
438 0 : ulong blk_txn_cnt = 0;
439 0 : FD_TEST( fd_runtime_block_eval_tpool( ledger_args->slot_ctx,
440 0 : ledger_args->capture_ctx,
441 0 : ledger_args->tpool,
442 0 : 1,
443 0 : &blk_txn_cnt,
444 0 : ledger_args->spads,
445 0 : ledger_args->spad_cnt ) == FD_RUNTIME_EXECUTE_SUCCESS );
446 0 : txn_cnt += blk_txn_cnt;
447 0 : slot_cnt++;
448 :
449 0 : fd_blockstore_start_read( blockstore );
450 0 : fd_hash_t const * expected = fd_blockstore_block_hash_query( blockstore, slot );
451 0 : if( FD_UNLIKELY( !expected ) ) FD_LOG_ERR( ( "slot %lu is missing its hash", slot ) );
452 0 : else if( FD_UNLIKELY( 0 != memcmp( ledger_args->slot_ctx->slot_bank.poh.hash, expected->hash, 32UL ) ) ) {
453 0 : char expected_hash[ FD_BASE58_ENCODED_32_SZ ];
454 0 : fd_acct_addr_cstr( expected_hash, expected->hash );
455 0 : char poh_hash[ FD_BASE58_ENCODED_32_SZ ];
456 0 : fd_acct_addr_cstr( poh_hash, ledger_args->slot_ctx->slot_bank.poh.hash );
457 0 : FD_LOG_WARNING(( "PoH hash mismatch! slot=%lu expected=%s, got=%s",
458 0 : slot,
459 0 : expected_hash,
460 0 : poh_hash ));
461 :
462 0 : if( ledger_args->checkpt_mismatch ) {
463 0 : fd_runtime_checkpt( ledger_args->capture_ctx, ledger_args->slot_ctx, ULONG_MAX );
464 0 : }
465 0 : if( ledger_args->snapshot_mismatch ) {
466 0 : fd_snapshot_ctx_t snapshot_ctx = {
467 0 : .slot = ledger_args->slot_ctx->root_slot,
468 0 : .out_dir = ledger_args->snapshot_dir,
469 0 : .is_incremental = 0,
470 0 : .valloc = ledger_args->slot_ctx->valloc,
471 0 : .funk = ledger_args->slot_ctx->acc_mgr->funk,
472 0 : .status_cache = ledger_args->slot_ctx->status_cache,
473 0 : .tpool = ledger_args->snapshot_tpool
474 0 : };
475 0 : fd_create_snapshot_task( NULL, (ulong)&snapshot_ctx, (ulong)ledger_args, NULL, NULL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL );
476 0 : }
477 0 : if( ledger_args->abort_on_mismatch ) {
478 0 : fd_blockstore_end_read( blockstore );
479 0 : return 1;
480 0 : }
481 0 : }
482 :
483 0 : expected = fd_blockstore_bank_hash_query( blockstore, slot );
484 0 : if( FD_UNLIKELY( !expected ) ) {
485 0 : FD_LOG_ERR(( "slot %lu is missing its bank hash", slot ));
486 0 : } else if( FD_UNLIKELY( 0 != memcmp( ledger_args->slot_ctx->slot_bank.banks_hash.hash,
487 0 : expected->hash,
488 0 : 32UL ) ) ) {
489 :
490 0 : char expected_hash[ FD_BASE58_ENCODED_32_SZ ];
491 0 : fd_acct_addr_cstr( expected_hash, expected->hash );
492 0 : char bank_hash[ FD_BASE58_ENCODED_32_SZ ];
493 0 : fd_acct_addr_cstr( bank_hash, ledger_args->slot_ctx->slot_bank.banks_hash.hash );
494 :
495 0 : FD_LOG_WARNING(( "Bank hash mismatch! slot=%lu expected=%s, got=%s",
496 0 : slot,
497 0 : expected_hash,
498 0 : bank_hash ));
499 :
500 0 : if( ledger_args->checkpt_mismatch ) {
501 0 : fd_runtime_checkpt( ledger_args->capture_ctx, ledger_args->slot_ctx, ULONG_MAX );
502 0 : }
503 0 : if( ledger_args->snapshot_mismatch ) {
504 0 : fd_snapshot_ctx_t snapshot_ctx = {
505 0 : .slot = ledger_args->slot_ctx->root_slot,
506 0 : .out_dir = ledger_args->snapshot_dir,
507 0 : .is_incremental = 0,
508 0 : .valloc = ledger_args->slot_ctx->valloc,
509 0 : .funk = ledger_args->slot_ctx->acc_mgr->funk,
510 0 : .status_cache = ledger_args->slot_ctx->status_cache,
511 0 : .tpool = ledger_args->snapshot_tpool
512 0 : };
513 0 : fd_create_snapshot_task( NULL, (ulong)&snapshot_ctx, (ulong)ledger_args, NULL, NULL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL );
514 0 : }
515 0 : if( ledger_args->abort_on_mismatch ) {
516 0 : fd_blockstore_end_read( blockstore );
517 0 : return 1;
518 0 : }
519 0 : }
520 0 : fd_blockstore_end_read( blockstore );
521 :
522 0 : prev_slot = slot;
523 :
524 0 : if( slot<ledger_args->end_slot ) {
525 : /* TODO: This currently doesn't support switching over on slots that occur
526 : on a fork */
527 : /* If need to go to next rocksdb, switch over */
528 0 : if( FD_UNLIKELY( ledger_args->rocksdb_list_cnt>1UL &&
529 0 : slot+1UL==ledger_args->rocksdb_list_slot[curr_rocksdb_idx] ) ) {
530 0 : curr_rocksdb_idx++;
531 0 : FD_LOG_WARNING(( "Switching to next rocksdb=%s", ledger_args->rocksdb_list[curr_rocksdb_idx] ));
532 0 : fd_rocksdb_root_iter_destroy( &iter );
533 0 : fd_rocksdb_destroy( &rocks_db );
534 :
535 0 : fd_memset( &rocks_db, 0, sizeof(fd_rocksdb_t) );
536 0 : fd_memset( &iter, 0, sizeof(fd_rocksdb_root_iter_t) );
537 0 : fd_memset( &slot_meta, 0, sizeof(fd_slot_meta_t) );
538 :
539 0 : char * err = fd_rocksdb_init( &rocks_db, ledger_args->rocksdb_list[curr_rocksdb_idx] );
540 0 : if( FD_UNLIKELY( err!=NULL ) ) {
541 0 : FD_LOG_ERR(( "fd_rocksdb_init at path=%s returned error=%s", ledger_args->rocksdb_list[curr_rocksdb_idx], err ));
542 0 : }
543 0 : fd_rocksdb_root_iter_new( &iter );
544 0 : int ret = fd_rocksdb_root_iter_seek( &iter, &rocks_db, slot+1UL, &slot_meta, ledger_args->slot_ctx->valloc );
545 0 : if( ret<0 ) {
546 0 : FD_LOG_ERR(( "Failed to seek to slot %lu", slot+1UL ));
547 0 : }
548 0 : } else {
549 : /* Otherwise look for next slot in current rocksdb */
550 0 : int ret = fd_rocksdb_root_iter_next( &iter, &slot_meta, ledger_args->slot_ctx->valloc );
551 0 : if( ret<0 ) {
552 0 : ret = fd_rocksdb_get_meta( &rocks_db, slot+1UL, &slot_meta, ledger_args->slot_ctx->valloc );
553 0 : if( ret<0 ) {
554 0 : FD_LOG_ERR(( "Failed to get meta for slot %lu", slot+1UL ));
555 0 : }
556 0 : }
557 0 : }
558 0 : }
559 0 : }
560 :
561 : /* Throw an error if the blockstore wksp has a usage which exceeds the allowed
562 : threshold. This likely indicates that a memory leak was introduced. */
563 :
564 0 : fd_wksp_usage_t final_usage = {0};
565 0 : fd_wksp_usage( fd_blockstore_wksp( ledger_args->blockstore ), NULL, 0UL, &final_usage );
566 :
567 0 : ulong mem_delta = fd_ulong_sat_sub( init_usage.free_sz, final_usage.free_sz );
568 0 : double pcnt_mem_delta = (double)mem_delta / (double)init_usage.free_sz;
569 0 : if( pcnt_mem_delta > ledger_args->allowed_mem_delta ) {
570 0 : FD_LOG_ERR(( "Memory usage delta (%4f%%) exceeded allowed limit (%4f%%)", 100UL * pcnt_mem_delta, 100UL * ledger_args->allowed_mem_delta ));
571 0 : } else {
572 0 : FD_LOG_NOTICE(( "Memory usage delta (%4f%%) within allowed limit (%4f%%)", 100UL * pcnt_mem_delta, 100UL * ledger_args->allowed_mem_delta ));
573 0 : }
574 :
575 0 : if( ledger_args->tpool ) {
576 0 : fd_tpool_fini( ledger_args->tpool );
577 0 : }
578 :
579 0 : fd_rocksdb_root_iter_destroy( &iter );
580 0 : fd_rocksdb_destroy( &rocks_db );
581 :
582 0 : replay_time += fd_log_wallclock();
583 0 : double replay_time_s = (double)replay_time * 1e-9;
584 0 : double tps = (double)txn_cnt / replay_time_s;
585 0 : double sec_per_slot = replay_time_s / (double)slot_cnt;
586 0 : FD_LOG_NOTICE((
587 0 : "replay completed - slots: %lu, elapsed: %6.6f s, txns: %lu, tps: %6.6f, sec/slot: %6.6f",
588 0 : slot_cnt,
589 0 : replay_time_s,
590 0 : txn_cnt,
591 0 : tps,
592 0 : sec_per_slot ));
593 :
594 0 : if ( slot_cnt == 0 ) {
595 0 : FD_LOG_ERR(( "No slots replayed" ));
596 0 : }
597 :
598 0 : return 0;
599 0 : }
600 :
601 : /***************************** Helpers ****************************************/
602 0 : fd_valloc_t allocator_setup( fd_wksp_t * wksp, char const * allocator ) {
603 0 : if( strcmp( allocator, "libc" ) == 0 ) {
604 0 : return fd_libc_alloc_virtual();
605 0 : }
606 :
607 0 : if( strcmp( allocator, "wksp" ) != 0 ) {
608 0 : FD_LOG_ERR( ( "unknown allocator specified" ) );
609 0 : }
610 :
611 0 : FD_TEST( wksp );
612 :
613 0 : void * alloc_shmem =
614 0 : fd_wksp_alloc_laddr( wksp, fd_alloc_align(), fd_alloc_footprint(), 3UL );
615 0 : if( FD_UNLIKELY( !alloc_shmem ) ) { FD_LOG_ERR( ( "fd_alloc too large for workspace" ) ); }
616 0 : void * alloc_shalloc = fd_alloc_new( alloc_shmem, 3UL );
617 0 : if( FD_UNLIKELY( !alloc_shalloc ) ) { FD_LOG_ERR( ( "fd_alloc_new failed" ) ); }
618 0 : fd_alloc_t * alloc = fd_alloc_join( alloc_shalloc, 3UL );
619 0 : if( FD_UNLIKELY( !alloc ) ) { FD_LOG_ERR( ( "fd_alloc_join failed" ) ); }
620 0 : return fd_alloc_virtual( alloc );
621 0 : }
622 :
623 : void
624 0 : fd_ledger_main_setup( fd_ledger_args_t * args ) {
625 0 : fd_flamenco_boot( NULL, NULL );
626 0 : fd_funk_t * funk = args->funk;
627 :
628 : /* Setup valloc */
629 0 : fd_valloc_t valloc = args->slot_ctx->valloc;
630 :
631 : /* Setup capture context */
632 0 : int has_solcap = args->capture_fpath && args->capture_fpath[0] != '\0';
633 0 : int has_checkpt = args->checkpt_path && args->checkpt_path[0] != '\0';
634 0 : int has_checkpt_funk = args->checkpt_funk && args->checkpt_funk[0] != '\0';
635 0 : int has_prune = args->pruned_funk != NULL;
636 0 : int has_dump_to_protobuf = args->dump_insn_to_pb || args->dump_txn_to_pb;
637 :
638 0 : if( has_solcap || has_checkpt || has_checkpt_funk || has_prune || has_dump_to_protobuf ) {
639 0 : FILE * capture_file = NULL;
640 :
641 0 : void * capture_ctx_mem = fd_valloc_malloc( valloc, FD_CAPTURE_CTX_ALIGN, FD_CAPTURE_CTX_FOOTPRINT );
642 0 : FD_TEST( capture_ctx_mem );
643 0 : fd_memset( capture_ctx_mem, 0, sizeof( fd_capture_ctx_t ) );
644 0 : args->capture_ctx = fd_capture_ctx_new( capture_ctx_mem );
645 :
646 0 : args->capture_ctx->checkpt_freq = ULONG_MAX;
647 :
648 0 : if( has_solcap ) {
649 0 : capture_file = fopen( args->capture_fpath, "w+" );
650 0 : if( FD_UNLIKELY( !capture_file ) ) {
651 0 : FD_LOG_ERR(( "fopen(%s) failed (%d-%s)", args->capture_fpath, errno, strerror( errno ) ));
652 0 : }
653 0 : fd_solcap_writer_init( args->capture_ctx->capture, capture_file );
654 0 : args->capture_ctx->capture_txns = args->capture_txns;
655 0 : } else {
656 0 : args->capture_ctx->capture = NULL;
657 0 : }
658 :
659 0 : if( has_checkpt || has_checkpt_funk ) {
660 0 : args->capture_ctx->checkpt_path = ( has_checkpt ? args->checkpt_path : args->checkpt_funk );
661 0 : args->capture_ctx->checkpt_freq = args->checkpt_freq;
662 0 : }
663 0 : if( has_prune ) {
664 0 : args->capture_ctx->pruned_funk = args->pruned_funk;
665 0 : }
666 0 : if( has_dump_to_protobuf ) {
667 0 : args->capture_ctx->dump_insn_to_pb = args->dump_insn_to_pb;
668 0 : args->capture_ctx->dump_txn_to_pb = args->dump_txn_to_pb;
669 0 : args->capture_ctx->dump_proto_sig_filter = args->dump_proto_sig_filter;
670 0 : args->capture_ctx->dump_proto_output_dir = args->dump_proto_output_dir;
671 0 : args->capture_ctx->dump_proto_start_slot = args->dump_proto_start_slot;
672 0 : }
673 0 : }
674 :
675 0 : fd_runtime_recover_banks( args->slot_ctx, 0, args->genesis==NULL );
676 :
677 0 : args->slot_ctx->snapshot_freq = args->snapshot_freq;
678 0 : args->slot_ctx->incremental_freq = args->incremental_freq;
679 0 : args->slot_ctx->last_snapshot_slot = 0UL;
680 0 : args->last_snapshot_slot = 0UL;
681 :
682 : /* Finish other runtime setup steps */
683 0 : fd_features_restore( args->slot_ctx );
684 0 : fd_runtime_update_leaders( args->slot_ctx, args->slot_ctx->slot_bank.slot );
685 0 : fd_calculate_epoch_accounts_hash_values( args->slot_ctx );
686 :
687 0 : fd_funk_start_write( funk );
688 0 : fd_bpf_scan_and_create_bpf_program_cache_entry_tpool( args->slot_ctx, args->slot_ctx->funk_txn, args->tpool );
689 0 : fd_funk_end_write( funk );
690 :
691 : /* Allocate memory for the account scratch space. In live execution, each of
692 : the spad allocations should be tied to its respective execution thread.
693 : In the future, the spad should be allocated from its tiles' workspace.
694 : It is important that the spads are only allocated on startup for
695 : performance reasons to avoid dynamic allocation in the critical path. */
696 :
697 0 : args->spad_cnt = fd_tpool_worker_cnt( args->tpool );
698 0 : for( ulong i=0UL; i<args->spad_cnt; i++ ) {
699 0 : ulong total_mem_sz = FD_RUNTIME_BORROWED_ACCOUNT_FOOTPRINT; /* TODO: is this right? */
700 0 : uchar * mem = fd_wksp_alloc_laddr( args->wksp, FD_SPAD_ALIGN, total_mem_sz, 999UL );
701 0 : fd_spad_t * spad = fd_spad_join( fd_spad_new( mem, total_mem_sz ) );
702 0 : if( FD_UNLIKELY( !spad ) ) {
703 0 : FD_LOG_ERR(( "failed to allocate spad" ));
704 0 : }
705 0 : args->spads[ i ] = spad;
706 0 : }
707 :
708 0 : }
709 :
710 : void
711 0 : fd_ledger_main_teardown( fd_ledger_args_t * args ) {
712 : /* Flush solcap file and cleanup */
713 0 : if( args->capture_ctx && args->capture_ctx->capture ) {
714 0 : fd_solcap_writer_flush( args->capture_ctx->capture );
715 0 : fd_solcap_writer_delete( args->capture_ctx->capture );
716 0 : }
717 0 : fd_exec_epoch_ctx_delete( fd_exec_epoch_ctx_leave( args->epoch_ctx ) );
718 0 : fd_exec_slot_ctx_delete( fd_exec_slot_ctx_leave( args->slot_ctx ) );
719 0 : }
720 :
721 : void
722 : ingest_rocksdb( fd_alloc_t * alloc,
723 : char const * file,
724 : ulong start_slot,
725 : ulong end_slot,
726 : fd_blockstore_t * blockstore,
727 : int txn_status,
728 0 : ulong trash_hash ) {
729 :
730 0 : fd_valloc_t valloc = fd_alloc_virtual( alloc );
731 0 : fd_rocksdb_t rocks_db;
732 0 : char * err = fd_rocksdb_init( &rocks_db, file );
733 0 : if( FD_UNLIKELY( err!=NULL ) ) {
734 0 : FD_LOG_ERR(( "fd_rocksdb_init returned %s", err ));
735 0 : }
736 :
737 0 : ulong last_slot = fd_rocksdb_last_slot( &rocks_db, &err );
738 0 : if( FD_UNLIKELY( err!=NULL ) ) {
739 0 : FD_LOG_ERR(( "fd_rocksdb_last_slot returned %s", err ));
740 0 : }
741 :
742 0 : if( last_slot < start_slot ) {
743 0 : FD_LOG_ERR(( "rocksdb blocks are older than snapshot. first=%lu last=%lu wanted=%lu",
744 0 : fd_rocksdb_first_slot(&rocks_db, &err), last_slot, start_slot ));
745 0 : }
746 :
747 0 : FD_LOG_NOTICE(( "ingesting rocksdb from start=%lu to end=%lu", start_slot, end_slot ));
748 :
749 0 : fd_rocksdb_root_iter_t iter = {0};
750 0 : fd_rocksdb_root_iter_new( &iter );
751 :
752 0 : fd_slot_meta_t slot_meta = {0};
753 0 : fd_memset( &slot_meta, 0, sizeof(slot_meta) );
754 :
755 0 : int ret = fd_rocksdb_root_iter_seek( &iter, &rocks_db, start_slot, &slot_meta, valloc );
756 0 : if( ret < 0 ) {
757 0 : FD_LOG_ERR(( "fd_rocksdb_root_iter_seek returned %d", ret ));
758 0 : }
759 :
760 0 : uchar trash_hash_buf[32];
761 0 : memset( trash_hash_buf, 0xFE, sizeof(trash_hash_buf) );
762 :
763 0 : ulong blk_cnt = 0;
764 0 : do {
765 0 : ulong slot = slot_meta.slot;
766 0 : if( slot > end_slot ) {
767 0 : break;
768 0 : }
769 :
770 : /* Read and deshred block from RocksDB */
771 0 : if( blk_cnt % 100 == 0 ) {
772 0 : FD_LOG_WARNING(( "imported %lu blocks", blk_cnt ));
773 0 : }
774 :
775 0 : int err = fd_rocksdb_import_block_blockstore( &rocks_db, &slot_meta, blockstore, txn_status,
776 0 : (slot == trash_hash) ? trash_hash_buf : NULL );
777 0 : if( FD_UNLIKELY( err ) ) {
778 0 : FD_LOG_ERR(( "fd_rocksdb_get_block failed" ));
779 0 : }
780 :
781 0 : ++blk_cnt;
782 :
783 0 : fd_bincode_destroy_ctx_t ctx = { .valloc = valloc };
784 0 : fd_slot_meta_destroy( &slot_meta, &ctx );
785 :
786 0 : ret = fd_rocksdb_root_iter_next( &iter, &slot_meta, valloc );
787 0 : if( ret < 0 ) {
788 : // FD_LOG_WARNING(("Failed for slot %lu", slot + 1));
789 0 : ret = fd_rocksdb_get_meta( &rocks_db, slot + 1, &slot_meta, valloc );
790 0 : if( ret < 0 ) {
791 0 : break;
792 0 : }
793 0 : }
794 : // FD_LOG_ERR(("fd_rocksdb_root_iter_seek returned %d", ret));
795 0 : } while (1);
796 :
797 0 : fd_rocksdb_root_iter_destroy( &iter );
798 0 : fd_rocksdb_destroy( &rocks_db );
799 :
800 0 : FD_LOG_NOTICE(( "ingested %lu blocks", blk_cnt ));
801 0 : }
802 :
803 : void
804 0 : parse_one_off_features( fd_ledger_args_t * args, char const * one_off_features ) {
805 0 : if( !one_off_features ) {
806 0 : FD_LOG_NOTICE(( "No one-off features passed in" ));
807 0 : return;
808 0 : }
809 :
810 0 : char * one_off_features_str = strdup( one_off_features );
811 0 : char * token = NULL;
812 0 : token = strtok( one_off_features_str, "," );
813 0 : while( token ) {
814 0 : args->one_off_features[ args->one_off_features_cnt++ ] = token;
815 0 : token = strtok( NULL, "," );
816 0 : }
817 :
818 0 : FD_LOG_NOTICE(( "Found %u one off features to include", args->one_off_features_cnt ));
819 :
820 : /* TODO: Fix the leak here and in parse_rocksdb_list */
821 0 : }
822 :
823 : void
824 : parse_rocksdb_list( fd_ledger_args_t * args,
825 : char const * rocksdb_list,
826 0 : char const * rocksdb_start_slots ) {
827 : /* First parse the paths to the different rocksdb */
828 0 : if( FD_UNLIKELY( !rocksdb_list ) ) {
829 0 : FD_LOG_NOTICE(( "No rocksdb list passed in" ));
830 0 : return;
831 0 : }
832 :
833 0 : char * rocksdb_str = strdup( rocksdb_list );
834 0 : char * token = NULL;
835 0 : token = strtok( rocksdb_str, "," );
836 0 : while( token ) {
837 0 : args->rocksdb_list[ args->rocksdb_list_cnt++ ] = token;
838 0 : token = strtok( NULL, "," );
839 0 : }
840 :
841 : /* Now repeat for the start slots assuming there are multiple */
842 0 : if( rocksdb_start_slots == NULL && args->rocksdb_list_cnt > 1 ) {
843 0 : FD_LOG_ERR(( "Multiple rocksdb dirs passed in but no start slots" ));
844 0 : }
845 0 : ulong index = 0UL;
846 0 : if( rocksdb_start_slots ) {
847 0 : char * rocksdb_start_slot_str = strdup( rocksdb_start_slots );
848 0 : token = NULL;
849 0 : token = strtok( rocksdb_start_slot_str, "," );
850 0 : while( token ) {
851 0 : args->rocksdb_list_slot[ index++ ] = strtoul( token, NULL, 10 );
852 0 : token = strtok( NULL, "," );
853 0 : }
854 0 : }
855 :
856 0 : if( index != args->rocksdb_list_cnt - 1UL ) {
857 0 : FD_LOG_ERR(( "Number of rocksdb dirs passed in doesn't match number of start slots" ));
858 0 : }
859 :
860 :
861 : /* TODO: There is technically a leak here since we don't free the duplicated
862 : string but it's not a big deal. */
863 0 : }
864 :
865 : void
866 0 : init_scratch( fd_wksp_t * wksp ) {
867 0 : #define FD_SCRATCH_TAG (421UL)
868 0 : ulong smax = 1UL << 33UL; /* 8 GiB */
869 0 : ulong sdepth = 2048UL; /* 2048 scratch frames */
870 0 : void * smem = fd_wksp_alloc_laddr( wksp, fd_scratch_smem_align(), fd_scratch_smem_footprint( smax ), FD_SCRATCH_TAG );
871 0 : void * fmem = fd_wksp_alloc_laddr( wksp, fd_scratch_fmem_align(), fd_scratch_fmem_footprint( sdepth ), FD_SCRATCH_TAG );
872 0 : #undef FD_SCRATCH_TAG
873 0 : FD_TEST( (!!smem) & (!!fmem) );
874 0 : fd_scratch_attach( smem, fmem, smax, sdepth );
875 0 : }
876 :
877 : void
878 0 : cleanup_scratch( void ) {
879 0 : void * fmem = NULL;
880 0 : void * smem = fd_scratch_detach( &fmem );
881 0 : fd_wksp_free_laddr( smem );
882 0 : fd_wksp_free_laddr( fmem );
883 0 : }
884 :
885 : void
886 0 : init_funk( fd_ledger_args_t * args ) {
887 0 : fd_funk_t * funk;
888 0 : if( args->restore_funk ) {
889 0 : funk = fd_funk_recover_checkpoint( args->funk_file, 1, args->restore_funk, &args->funk_close_args );
890 0 : } else {
891 0 : funk = fd_funk_open_file( args->funk_file, 1, args->hashseed, args->txns_max, args->index_max, args->funk_page_cnt*(1UL<<30), FD_FUNK_OVERWRITE, &args->funk_close_args );
892 0 : }
893 0 : args->funk = funk;
894 0 : args->funk_wksp = fd_funk_wksp( funk );
895 0 : FD_LOG_NOTICE(( "funky at global address 0x%016lx with %lu records", fd_wksp_gaddr_fast( args->funk_wksp, funk ),
896 0 : fd_funk_rec_cnt( fd_funk_rec_map( funk, args->funk_wksp ) ) ));
897 0 : }
898 :
899 : void
900 0 : cleanup_funk( fd_ledger_args_t * args ) {
901 0 : fd_funk_close_file( &args->funk_close_args );
902 0 : }
903 :
904 : void
905 0 : init_blockstore( fd_ledger_args_t * args ) {
906 0 : fd_wksp_tag_query_info_t info;
907 0 : ulong blockstore_tag = FD_BLOCKSTORE_MAGIC;
908 0 : void * shmem;
909 0 : if( fd_wksp_tag_query( args->wksp, &blockstore_tag, 1, &info, 1 ) > 0 ) {
910 0 : shmem = fd_wksp_laddr_fast( args->wksp, info.gaddr_lo );
911 0 : args->blockstore = fd_blockstore_join( shmem );
912 0 : if( args->blockstore == NULL ) {
913 0 : FD_LOG_ERR(( "failed to join a blockstore" ));
914 0 : }
915 0 : FD_LOG_NOTICE(( "joined blockstore" ));
916 0 : } else {
917 0 : ulong txn_max = 1 << 22UL;
918 0 : shmem = fd_wksp_alloc_laddr( args->wksp, fd_blockstore_align(), fd_blockstore_footprint( args->shred_max, args->slot_history_max, 16, txn_max ), blockstore_tag );
919 0 : if( shmem == NULL ) {
920 0 : FD_LOG_ERR(( "failed to allocate a blockstore" ));
921 0 : }
922 0 : args->blockstore = fd_blockstore_join( fd_blockstore_new( shmem, 1, args->hashseed, args->shred_max,
923 0 : args->slot_history_max, 16, txn_max ) );
924 0 : if( args->blockstore == NULL ) {
925 0 : fd_wksp_free_laddr( shmem );
926 0 : FD_LOG_ERR(( "failed to allocate a blockstore" ));
927 0 : }
928 0 : FD_LOG_NOTICE(( "allocating a new blockstore" ));
929 0 : }
930 0 : }
931 :
932 : void
933 0 : checkpt( fd_ledger_args_t * args ) {
934 0 : if( !args->checkpt && !args->checkpt_funk && !args->checkpt_status_cache ) {
935 0 : FD_LOG_WARNING(( "No checkpt argument specified" ));
936 0 : }
937 :
938 0 : if( args->checkpt_funk ) {
939 0 : if( args->funk_wksp == NULL ) {
940 0 : FD_LOG_ERR(( "funk_wksp is NULL" ));
941 0 : }
942 0 : FD_LOG_NOTICE(( "writing funk checkpt %s", args->checkpt_funk ));
943 0 : unlink( args->checkpt_funk );
944 : #ifdef FD_FUNK_WKSP_PROTECT
945 : fd_wksp_mprotect( args->funk_wksp, 0 );
946 : #endif
947 0 : int err = fd_wksp_checkpt( args->funk_wksp, args->checkpt_funk, 0666, 0, NULL );
948 : #ifdef FD_FUNK_WKSP_PROTECT
949 : fd_wksp_mprotect( args->funk_wksp, 1 );
950 : #endif
951 0 : if( err ) {
952 0 : FD_LOG_ERR(( "funk checkpt failed: error %d", err ));
953 0 : }
954 0 : }
955 0 : if( args->checkpt ) {
956 0 : FD_LOG_NOTICE(( "writing %s", args->checkpt ));
957 0 : unlink( args->checkpt );
958 0 : int err = fd_wksp_checkpt( args->wksp, args->checkpt, 0666, 0, NULL );
959 0 : if( err ) {
960 0 : FD_LOG_ERR(( "checkpt failed: error %d", err ));
961 0 : }
962 0 : }
963 0 : if( args->checkpt_status_cache ) {
964 0 : FD_LOG_NOTICE(( "writing %s", args->checkpt_status_cache ));
965 0 : unlink( args->checkpt_status_cache );
966 0 : int err = fd_wksp_checkpt( args->status_cache_wksp, args->checkpt_status_cache, 0666, 0, NULL );
967 0 : if( err ) {
968 0 : FD_LOG_ERR(( "status cache checkpt failed: error %d", err ));
969 0 : }
970 0 : }
971 0 : }
972 :
973 : void
974 0 : wksp_restore( fd_ledger_args_t * args ) {
975 0 : if( args->restore != NULL ) {
976 0 : FD_LOG_NOTICE(( "restoring wksp %s", args->restore ));
977 0 : fd_wksp_restore( args->wksp, args->restore, args->hashseed );
978 0 : }
979 0 : }
980 :
981 : /********************* Main Command Functions and Setup ***********************/
982 : void
983 0 : minify( fd_ledger_args_t * args ) {
984 : /* Example commmand:
985 : fd_ledger --cmd minify --rocksdb <LARGE_ROCKSDB> --minified-rocksdb <MINI_ROCKSDB>
986 : --start-slot <START_SLOT> --end-slot <END_SLOT> --copy-txn-status 1
987 : */
988 0 : if( args->rocksdb_list[ 0UL ] == NULL ) {
989 0 : FD_LOG_ERR(( "rocksdb path is NULL" ));
990 0 : }
991 0 : if( args->mini_db_dir == NULL ) {
992 0 : FD_LOG_ERR(( "minified rocksdb path is NULL" ));
993 0 : }
994 :
995 :
996 0 : fd_rocksdb_t big_rocksdb;
997 0 : char * err = fd_rocksdb_init( &big_rocksdb, args->rocksdb_list[ 0UL ] );
998 0 : if( FD_UNLIKELY( err!=NULL ) ) {
999 0 : FD_LOG_ERR(( "fd_rocksdb_init at path=%s returned error=%s", args->rocksdb_list[ 0UL ], err ));
1000 0 : }
1001 :
1002 : /* If the directory for the minified rocksdb already exists, error out */
1003 0 : struct stat statbuf;
1004 0 : if( stat( args->mini_db_dir, &statbuf ) == 0 ) {
1005 0 : FD_LOG_ERR(( "path for mini_db_dir=%s already exists", args->mini_db_dir ));
1006 0 : }
1007 :
1008 : /* Create a new smaller rocksdb */
1009 0 : fd_rocksdb_t mini_rocksdb;
1010 0 : fd_rocksdb_new( &mini_rocksdb, args->mini_db_dir );
1011 :
1012 : /* Correctly bound off start and end slot */
1013 0 : ulong first_slot = fd_rocksdb_first_slot( &big_rocksdb, &err );
1014 0 : ulong last_slot = fd_rocksdb_last_slot( &big_rocksdb, &err );
1015 0 : if( args->start_slot < first_slot ) { args->start_slot = first_slot; }
1016 0 : if( args->end_slot > last_slot ) { args->end_slot = last_slot; }
1017 :
1018 0 : FD_LOG_NOTICE(( "copying over rocks db for range [%lu, %lu]", args->start_slot, args->end_slot ));
1019 :
1020 : /* Copy over all slot indexed columns */
1021 0 : for( ulong cf_idx = 1; cf_idx < FD_ROCKSDB_CF_CNT; ++cf_idx ) {
1022 0 : fd_rocksdb_copy_over_slot_indexed_range( &big_rocksdb, &mini_rocksdb, cf_idx,
1023 0 : args->start_slot, args->end_slot );
1024 0 : }
1025 0 : FD_LOG_NOTICE(("copied over all slot indexed columns"));
1026 :
1027 : /* Copy over transactions. This is more complicated because first, a temporary
1028 : blockstore will be populated. This will be used to look up transactions
1029 : which can be quickly queried */
1030 0 : if( args->copy_txn_status ) {
1031 0 : init_blockstore( args );
1032 : /* Ingest block range into blockstore */
1033 0 : ingest_rocksdb( args->alloc, args->rocksdb_list[ 0UL ], args->start_slot,
1034 0 : args->end_slot, args->blockstore, 0, ULONG_MAX );
1035 :
1036 0 : fd_rocksdb_copy_over_txn_status_range( &big_rocksdb, &mini_rocksdb, args->blockstore,
1037 0 : args->start_slot, args->end_slot );
1038 0 : FD_LOG_NOTICE(( "copied over all transaction statuses" ));
1039 0 : } else {
1040 0 : FD_LOG_NOTICE(( "skipping copying of transaction statuses" ));
1041 0 : }
1042 :
1043 : /* TODO: Currently, the address signatures column family isn't copied as it
1044 : is indexed on the pubkey. */
1045 :
1046 0 : fd_rocksdb_destroy( &big_rocksdb );
1047 0 : fd_rocksdb_destroy( &mini_rocksdb );
1048 0 : }
1049 :
1050 : void
1051 0 : ingest( fd_ledger_args_t * args ) {
1052 : /* Setup funk, blockstore, epoch_ctx, and slot_ctx */
1053 0 : wksp_restore( args );
1054 0 : init_funk( args );
1055 0 : if( !args->funk_only ) {
1056 0 : init_blockstore( args );
1057 0 : }
1058 :
1059 0 : fd_funk_t * funk = args->funk;
1060 :
1061 0 : fd_alloc_t * alloc = fd_alloc_join( fd_wksp_laddr_fast( fd_funk_wksp( funk ), funk->alloc_gaddr ), 0UL );
1062 0 : if( FD_UNLIKELY( !alloc ) ) FD_LOG_ERR(( "fd_alloc_join(gaddr=%#lx) failed", funk->alloc_gaddr ));
1063 :
1064 0 : fd_valloc_t valloc = allocator_setup( args->wksp, args->allocator );
1065 0 : uchar * epoch_ctx_mem = fd_valloc_malloc( valloc, fd_exec_epoch_ctx_align(), fd_exec_epoch_ctx_footprint( args->vote_acct_max ) );
1066 0 : fd_memset( epoch_ctx_mem, 0, fd_exec_epoch_ctx_footprint( args->vote_acct_max ) );
1067 0 : fd_exec_epoch_ctx_t * epoch_ctx = fd_exec_epoch_ctx_join( fd_exec_epoch_ctx_new( epoch_ctx_mem, args->vote_acct_max ) );
1068 :
1069 0 : uchar slot_ctx_mem[FD_EXEC_SLOT_CTX_FOOTPRINT] __attribute__((aligned(FD_EXEC_SLOT_CTX_ALIGN)));
1070 0 : fd_exec_slot_ctx_t * slot_ctx = fd_exec_slot_ctx_join( fd_exec_slot_ctx_new( slot_ctx_mem, valloc ) );
1071 0 : slot_ctx->epoch_ctx = epoch_ctx;
1072 0 : args->slot_ctx = slot_ctx;
1073 :
1074 0 : fd_acc_mgr_t mgr[1];
1075 0 : slot_ctx->acc_mgr = fd_acc_mgr_new( mgr, funk );
1076 0 : slot_ctx->blockstore = args->blockstore;
1077 :
1078 0 : if( args->status_cache_wksp ) {
1079 0 : void * status_cache_mem = fd_wksp_alloc_laddr( args->status_cache_wksp,
1080 0 : fd_txncache_align(),
1081 0 : fd_txncache_footprint( FD_TXNCACHE_DEFAULT_MAX_ROOTED_SLOTS,
1082 0 : FD_TXNCACHE_DEFAULT_MAX_LIVE_SLOTS,
1083 0 : MAX_CACHE_TXNS_PER_SLOT,
1084 0 : FD_TXNCACHE_DEFAULT_MAX_CONSTIPATED_SLOTS ),
1085 0 : FD_TXNCACHE_MAGIC );
1086 0 : FD_TEST( status_cache_mem );
1087 0 : slot_ctx->status_cache = fd_txncache_join( fd_txncache_new( status_cache_mem,
1088 0 : FD_TXNCACHE_DEFAULT_MAX_ROOTED_SLOTS,
1089 0 : FD_TXNCACHE_DEFAULT_MAX_LIVE_SLOTS,
1090 0 : MAX_CACHE_TXNS_PER_SLOT,
1091 0 : FD_TXNCACHE_DEFAULT_MAX_CONSTIPATED_SLOTS ) );
1092 0 : FD_TEST( slot_ctx->status_cache );
1093 0 : }
1094 :
1095 0 : init_tpool( args );
1096 :
1097 : /* Load in snapshot(s) */
1098 0 : if( args->snapshot ) {
1099 0 : fd_snapshot_load( args->snapshot, slot_ctx, args->tpool, args->verify_acc_hash, args->check_acc_hash , FD_SNAPSHOT_TYPE_FULL );
1100 0 : FD_LOG_NOTICE(( "imported %lu records from snapshot", fd_funk_rec_cnt( fd_funk_rec_map( funk, fd_funk_wksp( funk ) ) ) ));
1101 0 : }
1102 0 : if( args->incremental ) {
1103 0 : fd_snapshot_load( args->incremental, slot_ctx, args->tpool, args->verify_acc_hash, args->check_acc_hash, FD_SNAPSHOT_TYPE_INCREMENTAL );
1104 0 : FD_LOG_NOTICE(( "imported %lu records from incremental snapshot", fd_funk_rec_cnt( fd_funk_rec_map( funk, fd_funk_wksp( funk ) ) ) ));
1105 0 : }
1106 :
1107 0 : if( args->genesis ) {
1108 0 : fd_runtime_read_genesis( slot_ctx, args->genesis, args->snapshot != NULL, NULL, args->tpool );
1109 0 : }
1110 :
1111 0 : if( !args->snapshot && (args->restore_funk != NULL || args->restore != NULL) ) {
1112 0 : fd_runtime_recover_banks( slot_ctx, 0, 1 );
1113 0 : }
1114 :
1115 : /* At this point the account state has been ingested into funk. Intake rocksdb */
1116 0 : if( args->start_slot == 0 ) {
1117 0 : args->start_slot = slot_ctx->slot_bank.slot + 1;
1118 0 : }
1119 0 : fd_blockstore_t * blockstore = args->blockstore;
1120 0 : if( blockstore ) {
1121 0 : blockstore->lps = blockstore->hcs = blockstore->smr = slot_ctx->slot_bank.slot;
1122 0 : }
1123 :
1124 0 : if( args->funk_only ) {
1125 0 : FD_LOG_NOTICE(( "using funk only, skipping blockstore ingest" ));
1126 0 : } else if( args->shredcap ) {
1127 0 : FD_LOG_NOTICE(( "using shredcap" ));
1128 0 : fd_shredcap_populate_blockstore( args->shredcap, blockstore, args->start_slot, args->end_slot );
1129 0 : } else if( args->rocksdb_list[ 0UL ] ) {
1130 0 : if( args->end_slot >= slot_ctx->slot_bank.slot + args->slot_history_max ) {
1131 0 : args->end_slot = slot_ctx->slot_bank.slot + args->slot_history_max - 1;
1132 0 : }
1133 0 : ingest_rocksdb( args->alloc, args->rocksdb_list[ 0UL ], args->start_slot, args->end_slot,
1134 0 : blockstore, args->copy_txn_status, args->trash_hash );
1135 0 : }
1136 :
1137 : /* Verification */
1138 0 : for( fd_feature_id_t const * id = fd_feature_iter_init();
1139 0 : !fd_feature_iter_done( id );
1140 0 : id = fd_feature_iter_next( id ) ) {
1141 0 : ulong activated_at = fd_features_get( &slot_ctx->epoch_ctx->features, id );
1142 0 : if( activated_at ) {
1143 0 : FD_LOG_DEBUG(( "feature %s activated at slot %lu", FD_BASE58_ENC_32_ALLOCA( id->id.key ), activated_at ));
1144 0 : }
1145 0 : }
1146 :
1147 0 : if( args->verify_funk ) {
1148 0 : FD_LOG_NOTICE(( "verifying funky" ));
1149 0 : if( fd_funk_verify( funk ) ) {
1150 0 : FD_LOG_ERR(( "verification failed" ));
1151 0 : }
1152 0 : }
1153 :
1154 0 : checkpt( args );
1155 :
1156 0 : cleanup_funk( args );
1157 :
1158 0 : cleanup_scratch();
1159 0 : }
1160 :
1161 : int
1162 0 : replay( fd_ledger_args_t * args ) {
1163 : /* Allows for ingest and direct replay. This can be done with a full checkpoint
1164 : that contains a blockstore and funk, a checkpoint that just has funk, or directly
1165 : using a rocksdb and snapshot.
1166 :
1167 : On demand block ingest is enabled by default and can be disabled with
1168 : '--on-demand-block-ingest 0'. The number of blocks retained in a blockstore during
1169 : on demand block ingest can be set with '--on-demand-block-history <N slots>'
1170 :
1171 : In order to replay from a checkpoint, use '--checkpoint <path to checkpoint>'.
1172 :
1173 : To use a checkpoint, but to consume blocks on demand use '--funkonly true'.
1174 : This option MUST be used if the checkpoint was generated during a replay with
1175 : on demand block ingest.
1176 :
1177 : For blocks to contain transaction status information use '--txnstatus true'
1178 :
1179 : Example command loading in from on demand checkpoint and replaying with on demand block ingest.
1180 : It creates a checkpoint every 1000 slots.
1181 : fd_ledger --funk-restore <CHECKPOINT_TO_LOAD_IN> --cmd replay --page-cnt 20
1182 : --abort-on-mismatch 1 --tile-cpus 5-21 --allocator wksp
1183 : --rocksdb dump/rocksdb --checkpt-path dump/checkpoint_new
1184 : --checkpt-freq 1000 --funk-only 1 --on-demand-block-ingest 1 --funk-page-cnt 350
1185 :
1186 : Example command directly loading in a rocksdb and snapshot and replaying.
1187 : fd_ledger --reset 1 --cmd replay --rocksdb dump/mainnet-257068890/rocksdb --index-max 5000000
1188 : --end-slot 257068895 --txn-max 100 --page-cnt 16 --verify-acc-hash 1
1189 : --snapshot dump/mainnet-257068890/snapshot-257068890-uRVtagPzKhYorycp4CRtKdWrYPij6iBxCYYXmqRvdSp.tar.zst
1190 : --slot-history 5000 --allocator wksp --tile-cpus 5-21 --funk-page-cnt 16
1191 : */
1192 :
1193 0 : wksp_restore( args ); /* Restores checkpointed workspace(s) */
1194 :
1195 0 : init_funk( args ); /* Joins or creates funk based on if one exists in the workspace */
1196 0 : init_blockstore( args ); /* Does the same for the blockstore */
1197 :
1198 0 : fd_funk_t * funk = args->funk;
1199 :
1200 : /* Setup slot_ctx */
1201 0 : fd_valloc_t valloc = allocator_setup( args->wksp, args->allocator );
1202 :
1203 0 : void * epoch_ctx_mem = fd_wksp_alloc_laddr( args->wksp, fd_exec_epoch_ctx_align(),
1204 0 : fd_exec_epoch_ctx_footprint( args->vote_acct_max ), FD_EXEC_EPOCH_CTX_MAGIC );
1205 0 : fd_memset( epoch_ctx_mem, 0, fd_exec_epoch_ctx_footprint( args->vote_acct_max ) );
1206 0 : void * slot_ctx_mem = fd_wksp_alloc_laddr( args->wksp, FD_EXEC_SLOT_CTX_ALIGN, FD_EXEC_SLOT_CTX_FOOTPRINT, FD_EXEC_SLOT_CTX_MAGIC );
1207 0 : args->epoch_ctx = fd_exec_epoch_ctx_join( fd_exec_epoch_ctx_new( epoch_ctx_mem, args->vote_acct_max ) );
1208 0 : fd_exec_epoch_ctx_bank_mem_clear( args->epoch_ctx );
1209 :
1210 0 : args->epoch_ctx->epoch_bank.cluster_version[0] = args->cluster_version[0];
1211 0 : args->epoch_ctx->epoch_bank.cluster_version[1] = args->cluster_version[1];
1212 0 : args->epoch_ctx->epoch_bank.cluster_version[2] = args->cluster_version[2];
1213 :
1214 0 : fd_features_enable_cleaned_up( &args->epoch_ctx->features, args->epoch_ctx->epoch_bank.cluster_version );
1215 0 : fd_features_enable_one_offs( &args->epoch_ctx->features, args->one_off_features, args->one_off_features_cnt, 0UL );
1216 :
1217 0 : args->slot_ctx = fd_exec_slot_ctx_join( fd_exec_slot_ctx_new( slot_ctx_mem, valloc ) );
1218 0 : args->slot_ctx->epoch_ctx = args->epoch_ctx;
1219 0 : args->slot_ctx->valloc = valloc;
1220 0 : args->slot_ctx->acc_mgr = fd_acc_mgr_new( args->acc_mgr, funk );
1221 0 : args->slot_ctx->blockstore = args->blockstore;
1222 0 : void * status_cache_mem = fd_wksp_alloc_laddr( args->wksp,
1223 0 : FD_TXNCACHE_ALIGN,
1224 0 : fd_txncache_footprint( FD_TXNCACHE_DEFAULT_MAX_ROOTED_SLOTS,
1225 0 : FD_TXNCACHE_DEFAULT_MAX_LIVE_SLOTS,
1226 0 : MAX_CACHE_TXNS_PER_SLOT,
1227 0 : FD_TXNCACHE_DEFAULT_MAX_ROOTED_SLOTS),
1228 0 : FD_TXNCACHE_MAGIC );
1229 0 : args->slot_ctx->status_cache = fd_txncache_join( fd_txncache_new( status_cache_mem,
1230 0 : FD_TXNCACHE_DEFAULT_MAX_ROOTED_SLOTS,
1231 0 : FD_TXNCACHE_DEFAULT_MAX_LIVE_SLOTS,
1232 0 : MAX_CACHE_TXNS_PER_SLOT,
1233 0 : FD_TXNCACHE_DEFAULT_MAX_CONSTIPATED_SLOTS ) );
1234 0 : FD_TEST( args->slot_ctx->status_cache );
1235 :
1236 0 : init_tpool( args );
1237 :
1238 : /* Check number of records in funk. If rec_cnt == 0, then it can be assumed
1239 : that you need to load in snapshot(s). */
1240 :
1241 0 : ulong rec_cnt = fd_funk_rec_cnt( fd_funk_rec_map( funk, fd_funk_wksp( funk ) ) );
1242 0 : if( !rec_cnt ) {
1243 : /* Load in snapshot(s) */
1244 0 : if( args->snapshot ) {
1245 0 : fd_snapshot_load( args->snapshot, args->slot_ctx, args->tpool, args->verify_acc_hash, args->check_acc_hash, FD_SNAPSHOT_TYPE_FULL );
1246 0 : FD_LOG_NOTICE(( "imported %lu records from snapshot", fd_funk_rec_cnt( fd_funk_rec_map( funk, fd_funk_wksp( funk ) ) ) ));
1247 0 : }
1248 0 : if( args->incremental ) {
1249 0 : fd_snapshot_load( args->incremental, args->slot_ctx, args->tpool, args->verify_acc_hash, args->check_acc_hash, FD_SNAPSHOT_TYPE_INCREMENTAL );
1250 0 : FD_LOG_NOTICE(( "imported %lu records from snapshot", fd_funk_rec_cnt( fd_funk_rec_map( funk, fd_funk_wksp( funk ) ) ) ));
1251 0 : }
1252 0 : if( args->genesis ) {
1253 0 : fd_runtime_read_genesis( args->slot_ctx, args->genesis, args->snapshot != NULL, NULL, args->tpool );
1254 0 : }
1255 0 : } else {
1256 0 : FD_LOG_NOTICE(( "found funk with %lu records", rec_cnt ));
1257 0 : }
1258 :
1259 0 : fd_ledger_main_setup( args );
1260 :
1261 0 : fd_blockstore_init( args->blockstore, -1, FD_BLOCKSTORE_ARCHIVE_MIN_SIZE, &args->slot_ctx->slot_bank );
1262 :
1263 0 : FD_LOG_WARNING(( "setup done" ));
1264 :
1265 0 : int ret = runtime_replay( args );
1266 :
1267 0 : fd_ledger_main_teardown( args );
1268 :
1269 0 : cleanup_funk( args );
1270 :
1271 0 : return ret;
1272 0 : }
1273 :
1274 : void
1275 0 : prune( fd_ledger_args_t * args ) {
1276 0 : if( args->restore || args->restore_funk ) {
1277 0 : FD_LOG_NOTICE(("restoring workspace"));
1278 0 : fd_wksp_restore( args->funk_wksp == NULL ? args->wksp : args->funk_wksp, args->restore_funk == NULL ? args->restore : args->restore_funk, args->hashseed );
1279 0 : }
1280 :
1281 : /* Setup data structures required for the unpruned workspace & replay ********/
1282 0 : init_funk( args );
1283 0 : init_blockstore( args );
1284 :
1285 0 : fd_funk_t * funk = args->funk;
1286 :
1287 0 : fd_valloc_t valloc = allocator_setup( args->wksp, args->allocator );
1288 :
1289 0 : void * epoch_ctx_mem = fd_wksp_alloc_laddr( args->wksp, fd_exec_epoch_ctx_align(),
1290 0 : fd_exec_epoch_ctx_footprint( args->vote_acct_max ), FD_EXEC_EPOCH_CTX_MAGIC );
1291 0 : fd_memset( epoch_ctx_mem, 0, fd_exec_epoch_ctx_footprint( args->vote_acct_max ) );
1292 0 : void * slot_ctx_mem = fd_wksp_alloc_laddr( args->wksp, FD_EXEC_SLOT_CTX_ALIGN, FD_EXEC_SLOT_CTX_FOOTPRINT, FD_EXEC_SLOT_CTX_MAGIC );
1293 0 : args->epoch_ctx = fd_exec_epoch_ctx_join( fd_exec_epoch_ctx_new( epoch_ctx_mem, args->vote_acct_max ) );
1294 0 : args->slot_ctx = fd_exec_slot_ctx_join( fd_exec_slot_ctx_new( slot_ctx_mem, valloc ) );
1295 0 : args->slot_ctx->epoch_ctx = args->epoch_ctx;
1296 0 : args->slot_ctx->valloc = valloc;
1297 0 : args->slot_ctx->acc_mgr = fd_acc_mgr_new( args->acc_mgr, funk );
1298 0 : args->slot_ctx->blockstore = args->blockstore;
1299 :
1300 0 : ulong rec_cnt = fd_funk_rec_cnt( fd_funk_rec_map( funk, fd_funk_wksp( funk ) ) );
1301 0 : if( !rec_cnt ) {
1302 : /* Load in snapshot(s) */
1303 0 : if( args->snapshot ) {
1304 0 : fd_snapshot_load( args->snapshot, args->slot_ctx, args->tpool, args->verify_acc_hash, args->check_acc_hash, FD_SNAPSHOT_TYPE_FULL );
1305 0 : FD_LOG_NOTICE(( "imported %lu records from snapshot", fd_funk_rec_cnt( fd_funk_rec_map( funk, fd_funk_wksp( funk ) ) ) ));
1306 0 : }
1307 0 : if( args->incremental ) {
1308 0 : fd_snapshot_load( args->incremental, args->slot_ctx, args->tpool, args->verify_acc_hash, args->check_acc_hash, FD_SNAPSHOT_TYPE_INCREMENTAL );
1309 0 : FD_LOG_NOTICE(( "imported %lu records from snapshot", fd_funk_rec_cnt( fd_funk_rec_map( funk, fd_funk_wksp( funk ) ) ) ));
1310 0 : }
1311 0 : }
1312 :
1313 : /* Repeat for the pruned worksapce ******************************************/
1314 : /* Create wksp */
1315 0 : fd_wksp_t * pruned_wksp = fd_wksp_new_anonymous( FD_SHMEM_GIGANTIC_PAGE_SZ, args->pages_pruned, 0, "prunedwksp", 0UL );
1316 0 : if( pruned_wksp == NULL ) {
1317 0 : FD_LOG_ERR(( "failed to create and attach to a pruned_wksp" ));
1318 0 : }
1319 : /* Create blockstore */
1320 0 : fd_blockstore_t * pruned_blockstore;
1321 0 : void * shmem = fd_wksp_alloc_laddr( pruned_wksp, fd_blockstore_align(), fd_blockstore_footprint( args->shred_max, args->slot_history_max, 16, 1UL << 22UL ), FD_BLOCKSTORE_MAGIC );
1322 0 : if( shmem == NULL ) {
1323 0 : FD_LOG_ERR(( "failed to allocate a blockstore" ));
1324 0 : }
1325 0 : pruned_blockstore = fd_blockstore_join( fd_blockstore_new( shmem, 1, args->hashseed, args->shred_max,
1326 0 : args->slot_history_max, 16, 1UL << 22UL ) );
1327 0 : if( pruned_blockstore == NULL ) {
1328 0 : fd_wksp_free_laddr( shmem );
1329 0 : FD_LOG_ERR(( "failed to allocate a blockstore" ));
1330 0 : }
1331 0 : FD_LOG_NOTICE(( "pruned blockstore at global address 0x%016lx", fd_wksp_gaddr_fast( pruned_wksp, shmem ) ));
1332 :
1333 : /* Create funk */
1334 0 : fd_funk_t * pruned_funk = NULL;
1335 0 : shmem = fd_wksp_alloc_laddr( pruned_wksp, fd_funk_align(), fd_funk_footprint(), 1 );
1336 0 : if( shmem == NULL ) {
1337 0 : FD_LOG_ERR(( "failed to allocate a funky" ));
1338 0 : }
1339 0 : pruned_funk = fd_funk_join( fd_funk_new( shmem, 1, args->hashseed,
1340 0 : args->txns_max, args->index_max_pruned ) );
1341 0 : if( pruned_funk == NULL ) {
1342 0 : fd_wksp_free_laddr( shmem );
1343 0 : FD_LOG_ERR(( "failed to allocate a funky" ));
1344 0 : }
1345 0 : FD_LOG_NOTICE(( "pruned funky at global address 0x%016lx", fd_wksp_gaddr_fast( pruned_wksp, shmem ) ));
1346 :
1347 : /* Junk xid for pruning transaction */
1348 0 : fd_funk_txn_xid_t prune_xid = {0};
1349 0 : fd_memset( &prune_xid, 0x42, sizeof(fd_funk_txn_xid_t) );
1350 0 : fd_funk_start_write( pruned_funk );
1351 0 : fd_funk_txn_t * prune_txn = fd_funk_txn_prepare( pruned_funk, NULL, &prune_xid, 1 );
1352 0 : fd_funk_end_write( pruned_funk );
1353 0 : FD_TEST(( !!prune_txn ));
1354 :
1355 : /* Setup slot/epoch contexts */
1356 0 : fd_valloc_t pruned_valloc = allocator_setup( pruned_wksp, args->allocator );
1357 :
1358 0 : void * epoch_ctx_mem_pruned = fd_wksp_alloc_laddr( pruned_wksp, fd_exec_epoch_ctx_align(),
1359 0 : fd_exec_epoch_ctx_footprint( args->vote_acct_max ), FD_EXEC_EPOCH_CTX_MAGIC );
1360 0 : fd_memset( epoch_ctx_mem_pruned, 0, fd_exec_epoch_ctx_footprint( args->vote_acct_max ) );
1361 0 : void * slot_ctx_mem_pruned = fd_wksp_alloc_laddr( pruned_wksp, FD_EXEC_SLOT_CTX_ALIGN, FD_EXEC_SLOT_CTX_FOOTPRINT, FD_EXEC_SLOT_CTX_MAGIC );
1362 0 : fd_exec_epoch_ctx_t * epoch_ctx_pruned = fd_exec_epoch_ctx_join( fd_exec_epoch_ctx_new( epoch_ctx_mem_pruned, args->vote_acct_max ) );
1363 0 : fd_exec_slot_ctx_t * slot_ctx_pruned = fd_exec_slot_ctx_join( fd_exec_slot_ctx_new( slot_ctx_mem_pruned, pruned_valloc ) );
1364 0 : slot_ctx_pruned->epoch_ctx = epoch_ctx_pruned;
1365 0 : slot_ctx_pruned->valloc = pruned_valloc;
1366 0 : fd_acc_mgr_t acc_mgr_pruned[1];
1367 0 : slot_ctx_pruned->acc_mgr = fd_acc_mgr_new( acc_mgr_pruned, pruned_funk );
1368 0 : slot_ctx_pruned->blockstore = pruned_blockstore;
1369 :
1370 0 : args->pruned_funk = pruned_funk;
1371 :
1372 : /* Replay through the desired slot range ************************************/
1373 :
1374 0 : init_tpool( args );
1375 0 : fd_ledger_main_setup( args );
1376 0 : runtime_replay( args );
1377 :
1378 0 : FD_LOG_NOTICE(("There are currently %lu records in the pruned funk", fd_funk_rec_cnt( fd_funk_rec_map( pruned_funk, pruned_wksp ) ) ));
1379 :
1380 : /* Reset the unpruned wksp, reload snapshot *********************************/
1381 : /* Reset the wksp */
1382 0 : fd_funk_delete( fd_funk_leave( args->funk ) );
1383 0 : ulong funk_tag = FD_FUNK_MAGIC;
1384 0 : fd_wksp_tag_free( args->wksp, &funk_tag, 1 );
1385 0 : fd_wksp_reset( args->wksp, args->hashseed );
1386 0 : fd_wksp_reset( args->funk_wksp, args->hashseed );
1387 :
1388 : /* Setup funk again */
1389 0 : if( args->restore || args->restore_funk ) {
1390 0 : FD_LOG_NOTICE(("restoring workspace"));
1391 0 : fd_wksp_restore( args->funk_wksp == NULL ? args->wksp : args->funk_wksp, args->restore_funk == NULL ? args->restore : args->restore_funk, args->hashseed );
1392 0 : }
1393 0 : init_funk( args );
1394 0 : init_blockstore( args );
1395 0 : init_scratch( args->wksp );
1396 :
1397 : /* Setup contexts */
1398 0 : valloc = allocator_setup( args->wksp, args->allocator );
1399 :
1400 0 : epoch_ctx_mem = fd_wksp_alloc_laddr( args->wksp, fd_exec_epoch_ctx_align(),
1401 0 : fd_exec_epoch_ctx_footprint( args->vote_acct_max ), FD_EXEC_EPOCH_CTX_MAGIC );
1402 0 : fd_memset( epoch_ctx_mem, 0, fd_exec_epoch_ctx_footprint( args->vote_acct_max ) );
1403 0 : slot_ctx_mem = fd_wksp_alloc_laddr( args->wksp, FD_EXEC_SLOT_CTX_ALIGN, FD_EXEC_SLOT_CTX_FOOTPRINT, FD_EXEC_SLOT_CTX_MAGIC );
1404 0 : args->epoch_ctx = fd_exec_epoch_ctx_join( fd_exec_epoch_ctx_new( epoch_ctx_mem, args->vote_acct_max ) );
1405 0 : args->slot_ctx = fd_exec_slot_ctx_join( fd_exec_slot_ctx_new( slot_ctx_mem, valloc ) );
1406 0 : args->slot_ctx->epoch_ctx = args->epoch_ctx;
1407 0 : args->slot_ctx->valloc = valloc;
1408 0 : fd_acc_mgr_t mgr[1];
1409 0 : args->slot_ctx->acc_mgr = fd_acc_mgr_new( mgr, args->funk );
1410 :
1411 : /* Load in snapshot(s) */
1412 0 : if( args->snapshot ) {
1413 0 : fd_snapshot_load( args->snapshot, args->slot_ctx, args->tpool, 0, 0, FD_SNAPSHOT_TYPE_FULL );
1414 0 : FD_LOG_NOTICE(( "reload: imported %lu records from snapshot", fd_funk_rec_cnt( fd_funk_rec_map( funk, fd_funk_wksp( funk ) ) ) ));
1415 0 : }
1416 0 : if( args->incremental ) {
1417 0 : fd_snapshot_load( args->incremental, args->slot_ctx, args->tpool, 0, 0, FD_SNAPSHOT_TYPE_INCREMENTAL );
1418 0 : FD_LOG_NOTICE(( "reload: imported %lu records from snapshot", fd_funk_rec_cnt( fd_funk_rec_map( funk, fd_funk_wksp( funk ) ) ) ));
1419 0 : }
1420 :
1421 : /* Copy over funk record state **********************************************/
1422 : /* After replaying, update all touched accounts to contain the data that is
1423 : present before execution begins. Look up the corresponding account in the
1424 : unpruned funk and copy over the contents */
1425 0 : fd_funk_t * unpruned_funk = args->funk;
1426 0 : fd_funk_start_write( pruned_funk );
1427 0 : fd_funk_start_write( unpruned_funk );
1428 0 : fd_funk_rec_t * rec_map = fd_funk_rec_map( pruned_funk, fd_funk_wksp( pruned_funk ) );
1429 0 : ulong txn_rec_cnt = 0UL;
1430 0 : for( const fd_funk_rec_t * rec = fd_funk_txn_rec_head( prune_txn, rec_map );
1431 0 : rec; rec = fd_funk_txn_next_rec( pruned_funk, rec ) ) {
1432 :
1433 0 : const fd_funk_rec_t * original_rec = fd_funk_rec_query_global( unpruned_funk, NULL, rec->pair.key, NULL );
1434 0 : if( original_rec != NULL ) {
1435 0 : txn_rec_cnt++;
1436 0 : fd_funk_rec_t * mod_rec = fd_funk_rec_modify( pruned_funk, rec );
1437 0 : mod_rec = fd_funk_val_copy( mod_rec, fd_funk_val_const( original_rec, fd_funk_wksp( unpruned_funk ) ),
1438 0 : fd_funk_val_sz( original_rec ), fd_funk_val_sz( original_rec ),
1439 0 : fd_funk_alloc( pruned_funk, pruned_wksp ), pruned_wksp, NULL );
1440 0 : FD_TEST(( memcmp( fd_funk_val( original_rec, fd_funk_wksp( unpruned_funk ) ), fd_funk_val_const( rec, pruned_wksp ),
1441 0 : fd_funk_val_sz( original_rec ) ) == 0 ));
1442 0 : } else {
1443 0 : fd_funk_rec_t * mod_rec = fd_funk_rec_modify( pruned_funk, rec );
1444 0 : int res = fd_funk_rec_remove( pruned_funk, mod_rec, rec->pair.xid->ul[0] );
1445 0 : FD_TEST(( res == 0 ));
1446 0 : }
1447 0 : }
1448 0 : FD_LOG_NOTICE(( "Copied over %lu records from transactions", txn_rec_cnt ));
1449 :
1450 : /* Repeat above steps with all features */
1451 0 : ulong features_cnt = 0UL;
1452 0 : for( fd_feature_id_t const * id = fd_feature_iter_init();
1453 0 : !fd_feature_iter_done( id ); id = fd_feature_iter_next( id ) ) {
1454 0 : features_cnt++;
1455 :
1456 0 : fd_pubkey_t const * pubkey = (fd_pubkey_t *) id->id.key;
1457 0 : fd_funk_rec_key_t feature_id = fd_acc_funk_key( pubkey );
1458 0 : fd_funk_rec_t const * feature_rec = fd_funk_rec_query_global( unpruned_funk, NULL, &feature_id, NULL );
1459 0 : if( !feature_rec ) {
1460 0 : continue;
1461 0 : }
1462 0 : fd_funk_rec_t * new_feature_rec = fd_funk_rec_write_prepare( pruned_funk, prune_txn, &feature_id,
1463 0 : 0, 1, NULL, NULL );
1464 0 : FD_TEST(( !!new_feature_rec ));
1465 0 : new_feature_rec = fd_funk_val_copy( new_feature_rec, fd_funk_val_const( feature_rec, fd_funk_wksp( unpruned_funk ) ),
1466 0 : fd_funk_val_sz( feature_rec ), fd_funk_val_sz( feature_rec ),
1467 0 : fd_funk_alloc( pruned_funk, fd_funk_wksp( pruned_funk ) ), pruned_wksp, NULL );
1468 0 : FD_TEST(( !!new_feature_rec ));
1469 0 : }
1470 0 : FD_LOG_NOTICE(( "Copied over %lu features", features_cnt ));
1471 :
1472 : /* Do the same with the epoch/slot bank keys and sysvars */
1473 0 : fd_runtime_recover_banks( args->slot_ctx, 0, 1 );
1474 :
1475 0 : fd_funk_rec_key_t id_epoch_bank = fd_runtime_epoch_bank_key();
1476 0 : fd_funk_rec_key_t id_slot_bank = fd_runtime_slot_bank_key();
1477 0 : fd_funk_rec_key_t recent_block_hashes = fd_acc_funk_key( &fd_sysvar_recent_block_hashes_id );
1478 0 : fd_funk_rec_key_t clock = fd_acc_funk_key( &fd_sysvar_clock_id );
1479 0 : fd_funk_rec_key_t slot_history = fd_acc_funk_key( &fd_sysvar_slot_history_id );
1480 0 : fd_funk_rec_key_t slot_hashes = fd_acc_funk_key( &fd_sysvar_slot_hashes_id );
1481 0 : fd_funk_rec_key_t epoch_schedule = fd_acc_funk_key( &fd_sysvar_epoch_schedule_id );
1482 0 : fd_funk_rec_key_t epoch_rewards = fd_acc_funk_key( &fd_sysvar_epoch_rewards_id );
1483 0 : fd_funk_rec_key_t sysvar_fees = fd_acc_funk_key( &fd_sysvar_fees_id );
1484 0 : fd_funk_rec_key_t rent = fd_acc_funk_key( &fd_sysvar_rent_id );
1485 0 : fd_funk_rec_key_t stake_history = fd_acc_funk_key( &fd_sysvar_stake_history_id );
1486 0 : fd_funk_rec_key_t owner = fd_acc_funk_key( &fd_sysvar_owner_id );
1487 0 : fd_funk_rec_key_t last_restart_slot = fd_acc_funk_key( &fd_sysvar_last_restart_slot_id );
1488 0 : fd_funk_rec_key_t instructions = fd_acc_funk_key( &fd_sysvar_instructions_id );
1489 0 : fd_funk_rec_key_t incinerator = fd_acc_funk_key( &fd_sysvar_incinerator_id );
1490 :
1491 0 : fd_funk_rec_key_t records[15] = { id_epoch_bank, id_slot_bank, recent_block_hashes, clock, slot_history,
1492 0 : slot_hashes, epoch_schedule, epoch_rewards, sysvar_fees, rent,
1493 0 : stake_history, owner, last_restart_slot, instructions, incinerator };
1494 0 : for( uint i = 0; i < sizeof( records ) / sizeof( fd_funk_rec_key_t ); ++i ) {
1495 0 : fd_funk_rec_t const * original_rec = fd_funk_rec_query_global( unpruned_funk, NULL, &records[i], NULL );
1496 0 : if( !original_rec ) {
1497 : /* Some sysvars aren't touched during execution. Not a problem. */
1498 0 : char record[ FD_BASE58_ENCODED_32_SZ ];
1499 0 : fd_acct_addr_cstr( record, (uchar*) &records[i] );
1500 0 : FD_LOG_DEBUG(( "Record is not in account pubkey=%s at index=%u", record, i ));
1501 0 : continue;
1502 0 : }
1503 0 : fd_funk_rec_t * new_rec = fd_funk_rec_write_prepare( pruned_funk, prune_txn, &records[i], 0, 1, NULL, NULL );
1504 0 : FD_TEST(( !!new_rec ));
1505 0 : new_rec = fd_funk_val_copy( new_rec, fd_funk_val_const( original_rec, fd_funk_wksp( unpruned_funk) ),
1506 0 : fd_funk_val_sz( original_rec ), fd_funk_val_sz( original_rec ),
1507 0 : fd_funk_alloc( pruned_funk, pruned_wksp ), pruned_wksp, NULL );
1508 0 : FD_TEST( memcmp( fd_funk_val( original_rec, fd_funk_wksp( unpruned_funk ) ), fd_funk_val_const( new_rec, pruned_wksp ),
1509 0 : fd_funk_val_sz( original_rec ) ) == 0 );
1510 0 : FD_TEST(( !!new_rec ));
1511 0 : }
1512 0 : FD_LOG_NOTICE(( "Copied over all sysvars and bank keys" ));
1513 :
1514 : /* Publish transaction with pruned records to the root of funk */
1515 0 : if( fd_funk_txn_publish( pruned_funk, prune_txn, 1 )==0 ) {
1516 0 : FD_LOG_ERR(( "failed to publish transaction into pruned funk" ));
1517 0 : }
1518 :
1519 : /* Verify that the pruned records are in the funk */
1520 0 : FD_LOG_NOTICE(( "Pruned funk record count is %lu", fd_funk_rec_global_cnt( pruned_funk, pruned_wksp ) ));
1521 :
1522 0 : fd_funk_leave( unpruned_funk );
1523 :
1524 0 : if( fd_funk_verify( pruned_funk ) ) {
1525 0 : FD_LOG_ERR(( "pruned funk verification failed" ));
1526 0 : }
1527 :
1528 0 : slot_ctx_pruned->funk_txn = NULL;
1529 0 : fd_funk_end_write( pruned_funk );
1530 0 : fd_funk_end_write( unpruned_funk );
1531 0 : args->funk = pruned_funk;
1532 0 : args->wksp = pruned_wksp;
1533 0 : checkpt( args );
1534 :
1535 0 : cleanup_funk( args );
1536 :
1537 0 : cleanup_scratch();
1538 0 : }
1539 :
1540 : /* Parse user arguments and setup shared data structures used across commands */
1541 : int
1542 0 : initial_setup( int argc, char ** argv, fd_ledger_args_t * args ) {
1543 0 : if( FD_UNLIKELY( argc==1 ) ) {
1544 0 : return 1;
1545 0 : }
1546 :
1547 0 : fd_boot( &argc, &argv );
1548 0 : fd_flamenco_boot( &argc, &argv );
1549 :
1550 0 : char const * wksp_name = fd_env_strip_cmdline_cstr ( &argc, &argv, "--wksp-name", NULL, NULL );
1551 0 : ulong funk_page_cnt = fd_env_strip_cmdline_ulong ( &argc, &argv, "--funk-page-cnt", NULL, 5 );
1552 0 : ulong page_cnt = fd_env_strip_cmdline_ulong ( &argc, &argv, "--page-cnt", NULL, 5 );
1553 0 : int reset = fd_env_strip_cmdline_int ( &argc, &argv, "--reset", NULL, 0 );
1554 0 : char const * cmd = fd_env_strip_cmdline_cstr ( &argc, &argv, "--cmd", NULL, NULL );
1555 0 : ulong index_max = fd_env_strip_cmdline_ulong ( &argc, &argv, "--index-max", NULL, 450000000 );
1556 0 : ulong txns_max = fd_env_strip_cmdline_ulong ( &argc, &argv, "--txn-max", NULL, 1000 );
1557 0 : char const * funk_file = fd_env_strip_cmdline_cstr ( &argc, &argv, "--funk-file", NULL, NULL );
1558 0 : int verify_funk = fd_env_strip_cmdline_int ( &argc, &argv, "--verify-funky", NULL, 0 );
1559 0 : char const * snapshot = fd_env_strip_cmdline_cstr ( &argc, &argv, "--snapshot", NULL, NULL );
1560 0 : char const * incremental = fd_env_strip_cmdline_cstr ( &argc, &argv, "--incremental", NULL, NULL );
1561 0 : char const * genesis = fd_env_strip_cmdline_cstr ( &argc, &argv, "--genesis", NULL, NULL );
1562 0 : int copy_txn_status = fd_env_strip_cmdline_int ( &argc, &argv, "--copy-txn-status", NULL, 0 );
1563 0 : ulong slot_history_max = fd_env_strip_cmdline_ulong ( &argc, &argv, "--slot-history", NULL, FD_BLOCK_MAX );
1564 0 : ulong shred_max = fd_env_strip_cmdline_ulong ( &argc, &argv, "--shred-max", NULL, 1UL << 17 );
1565 0 : ulong start_slot = fd_env_strip_cmdline_ulong ( &argc, &argv, "--start-slot", NULL, 0UL );
1566 0 : ulong end_slot = fd_env_strip_cmdline_ulong ( &argc, &argv, "--end-slot", NULL, ULONG_MAX );
1567 0 : uint verify_acc_hash = fd_env_strip_cmdline_uint ( &argc, &argv, "--verify-acc-hash", NULL, 1 );
1568 0 : uint check_acc_hash = fd_env_strip_cmdline_uint ( &argc, &argv, "--check-acc-hash", NULL, 1 );
1569 0 : char const * restore = fd_env_strip_cmdline_cstr ( &argc, &argv, "--restore", NULL, NULL );
1570 0 : char const * restore_funk = fd_env_strip_cmdline_cstr ( &argc, &argv, "--funk-restore", NULL, NULL );
1571 0 : char const * shredcap = fd_env_strip_cmdline_cstr ( &argc, &argv, "--shred-cap", NULL, NULL );
1572 0 : ulong trash_hash = fd_env_strip_cmdline_ulong ( &argc, &argv, "--trash-hash", NULL, ULONG_MAX );
1573 0 : char const * mini_db_dir = fd_env_strip_cmdline_cstr ( &argc, &argv, "--minified-rocksdb", NULL, NULL );
1574 0 : ulong index_max_pruned = fd_env_strip_cmdline_ulong ( &argc, &argv, "--pruned-index-max", NULL, 450000000 );
1575 0 : ulong pages_pruned = fd_env_strip_cmdline_ulong ( &argc, &argv, "--pruned-page-cnt", NULL, ULONG_MAX );
1576 0 : int funk_only = fd_env_strip_cmdline_int ( &argc, &argv, "--funk-only", NULL, 0 );
1577 0 : char const * checkpt = fd_env_strip_cmdline_cstr ( &argc, &argv, "--checkpt", NULL, NULL );
1578 0 : char const * checkpt_funk = fd_env_strip_cmdline_cstr ( &argc, &argv, "--checkpt-funk", NULL, NULL );
1579 0 : char const * capture_fpath = fd_env_strip_cmdline_cstr ( &argc, &argv, "--capture-solcap", NULL, NULL );
1580 0 : int capture_txns = fd_env_strip_cmdline_int ( &argc, &argv, "--capture-txns", NULL, 1 );
1581 0 : char const * checkpt_path = fd_env_strip_cmdline_cstr ( &argc, &argv, "--checkpt-path", NULL, NULL );
1582 0 : ulong checkpt_freq = fd_env_strip_cmdline_ulong ( &argc, &argv, "--checkpt-freq", NULL, ULONG_MAX );
1583 0 : int checkpt_mismatch = fd_env_strip_cmdline_int ( &argc, &argv, "--checkpt-mismatch", NULL, 0 );
1584 0 : char const * allocator = fd_env_strip_cmdline_cstr ( &argc, &argv, "--allocator", NULL, "wksp" );
1585 0 : int abort_on_mismatch = fd_env_strip_cmdline_int ( &argc, &argv, "--abort-on-mismatch", NULL, 1 );
1586 0 : int dump_insn_to_pb = fd_env_strip_cmdline_int ( &argc, &argv, "--dump-insn-to-pb", NULL, 0 );
1587 0 : int dump_txn_to_pb = fd_env_strip_cmdline_int ( &argc, &argv, "--dump-txn-to-pb", NULL, 0 );
1588 0 : ulong dump_proto_start_slot = fd_env_strip_cmdline_ulong ( &argc, &argv, "--dump-proto-start-slot", NULL, 0 );
1589 0 : char const * dump_proto_sig_filter = fd_env_strip_cmdline_cstr ( &argc, &argv, "--dump-proto-sig-filter", NULL, NULL );
1590 0 : char const * dump_proto_output_dir = fd_env_strip_cmdline_cstr ( &argc, &argv, "--dump-proto-output-dir", NULL, NULL );
1591 0 : ulong vote_acct_max = fd_env_strip_cmdline_ulong ( &argc, &argv, "--vote_acct_max", NULL, 2000000UL );
1592 0 : char const * rocksdb_list = fd_env_strip_cmdline_cstr ( &argc, &argv, "--rocksdb", NULL, NULL );
1593 0 : char const * rocksdb_list_starts = fd_env_strip_cmdline_cstr ( &argc, &argv, "--rocksdb-starts", NULL, NULL );
1594 0 : char const * cluster_version = fd_env_strip_cmdline_cstr ( &argc, &argv, "--cluster-version", NULL, "2.0.0" );
1595 0 : char const * checkpt_status_cache = fd_env_strip_cmdline_cstr ( &argc, &argv, "--checkpt-status-cache", NULL, NULL );
1596 0 : char const * one_off_features = fd_env_strip_cmdline_cstr ( &argc, &argv, "--one-off-features", NULL, NULL );
1597 0 : char const * lthash = fd_env_strip_cmdline_cstr ( &argc, &argv, "--lthash", NULL, "false" );
1598 0 : ulong snapshot_freq = fd_env_strip_cmdline_ulong ( &argc, &argv, "--snapshot-freq", NULL, ULONG_MAX );
1599 0 : ulong incremental_freq = fd_env_strip_cmdline_ulong ( &argc, &argv, "--incremental-freq", NULL, ULONG_MAX );
1600 0 : char const * snapshot_dir = fd_env_strip_cmdline_cstr ( &argc, &argv, "--snapshot-dir", NULL, NULL );
1601 0 : ulong snapshot_tcnt = fd_env_strip_cmdline_ulong ( &argc, &argv, "--snapshot-tcnt", NULL, 2UL );
1602 0 : double allowed_mem_delta = fd_env_strip_cmdline_double( &argc, &argv, "--allowed-mem-delta", NULL, 0.1 );
1603 0 : int snapshot_mismatch = fd_env_strip_cmdline_int ( &argc, &argv, "--snapshot-mismatch", NULL, 0 );
1604 :
1605 0 : if( FD_UNLIKELY( !verify_acc_hash ) ) {
1606 : /* We've got full snapshots that contain all 0s for the account
1607 : hash in account meta. Running hash verify allows us to
1608 : populate the hash in account meta with real values. */
1609 0 : FD_LOG_ERR(( "verify-acc-hash needs to be 1" ));
1610 0 : }
1611 :
1612 : // TODO: Add argument validation. Make sure that we aren't including any arguments that aren't parsed for
1613 :
1614 0 : char hostname[64];
1615 0 : gethostname( hostname, sizeof(hostname) );
1616 0 : ulong hashseed = fd_hash( 0, hostname, strnlen( hostname, sizeof(hostname) ) );
1617 0 : args->hashseed = (uint)hashseed;
1618 :
1619 : /* Setup workspace */
1620 0 : fd_wksp_t * wksp;
1621 0 : if( wksp_name == NULL ) {
1622 0 : FD_LOG_NOTICE(( "--wksp not specified, using an anonymous local workspace" ));
1623 0 : wksp = fd_wksp_new_anonymous( FD_SHMEM_GIGANTIC_PAGE_SZ, page_cnt, 0, "wksp", 0UL );
1624 0 : } else {
1625 0 : fd_shmem_info_t shmem_info[1];
1626 0 : if( FD_UNLIKELY( fd_shmem_info( wksp_name, 0UL, shmem_info ) ) )
1627 0 : FD_LOG_ERR(( "unable to query region \"%s\"\n\tprobably does not exist or bad permissions", wksp_name ));
1628 0 : wksp = fd_wksp_attach( wksp_name );
1629 0 : }
1630 :
1631 0 : if( wksp == NULL ) {
1632 0 : FD_LOG_ERR(( "failed to attach to workspace %s", wksp_name ));
1633 0 : }
1634 0 : if( reset ) {
1635 0 : fd_wksp_reset( wksp, args->hashseed );
1636 0 : }
1637 0 : args->wksp = wksp;
1638 :
1639 0 : init_scratch( wksp );
1640 :
1641 0 : if( checkpt_status_cache && checkpt_status_cache[0] != '\0' ) {
1642 0 : FD_LOG_NOTICE(( "Creating status cache wksp" ));
1643 0 : fd_wksp_t * status_cache_wksp = fd_wksp_new_anonymous( FD_SHMEM_GIGANTIC_PAGE_SZ, 23UL, 0, "status_cache_wksp", 0UL );
1644 0 : fd_wksp_reset( status_cache_wksp, args->hashseed );
1645 0 : args->status_cache_wksp = status_cache_wksp;
1646 0 : } else {
1647 0 : args->status_cache_wksp = NULL;
1648 0 : }
1649 :
1650 : /* Setup alloc and valloc */
1651 0 : #define FD_ALLOC_TAG (422UL)
1652 0 : void * alloc_shmem = fd_wksp_alloc_laddr( wksp, fd_alloc_align(), fd_alloc_footprint(), FD_ALLOC_TAG );
1653 0 : if( FD_UNLIKELY( !alloc_shmem ) ) { FD_LOG_ERR( ( "fd_alloc too large for workspace" ) ); }
1654 0 : void * alloc_shalloc = fd_alloc_new( alloc_shmem, FD_ALLOC_TAG );
1655 0 : if( FD_UNLIKELY( !alloc_shalloc ) ) { FD_LOG_ERR( ( "fd_alloc_new failed" ) ); }
1656 0 : fd_alloc_t * alloc = fd_alloc_join( alloc_shalloc, FD_ALLOC_TAG );
1657 0 : args->alloc = alloc;
1658 0 : #undef FD_ALLOC_TAG
1659 :
1660 : /* Copy over arguments */
1661 0 : args->cmd = cmd;
1662 0 : args->start_slot = start_slot;
1663 0 : args->end_slot = end_slot;
1664 0 : args->checkpt = checkpt;
1665 0 : args->checkpt_funk = checkpt_funk;
1666 0 : args->shred_max = shred_max;
1667 0 : args->slot_history_max = slot_history_max;
1668 0 : args->txns_max = txns_max;
1669 0 : args->index_max = index_max;
1670 0 : args->funk_page_cnt = funk_page_cnt;
1671 0 : args->funk_file = funk_file;
1672 0 : args->restore = restore;
1673 0 : args->restore_funk = restore_funk;
1674 0 : args->mini_db_dir = mini_db_dir;
1675 0 : args->funk_only = funk_only;
1676 0 : args->copy_txn_status = copy_txn_status;
1677 0 : args->snapshot = snapshot;
1678 0 : args->incremental = incremental;
1679 0 : args->genesis = genesis;
1680 0 : args->shredcap = shredcap;
1681 0 : args->verify_funk = verify_funk;
1682 0 : args->check_acc_hash = check_acc_hash;
1683 0 : args->verify_acc_hash = verify_acc_hash;
1684 0 : args->trash_hash = trash_hash;
1685 0 : args->index_max_pruned = index_max_pruned;
1686 0 : args->pages_pruned = pages_pruned;
1687 0 : args->capture_fpath = capture_fpath;
1688 0 : args->capture_txns = capture_txns;
1689 0 : args->checkpt_path = checkpt_path;
1690 0 : args->checkpt_freq = checkpt_freq;
1691 0 : args->checkpt_mismatch = checkpt_mismatch;
1692 0 : args->allocator = allocator;
1693 0 : args->abort_on_mismatch = abort_on_mismatch;
1694 0 : args->dump_insn_to_pb = dump_insn_to_pb;
1695 0 : args->dump_txn_to_pb = dump_txn_to_pb;
1696 0 : args->dump_proto_start_slot = dump_proto_start_slot;
1697 0 : args->dump_proto_sig_filter = dump_proto_sig_filter;
1698 0 : args->dump_proto_output_dir = dump_proto_output_dir;
1699 0 : args->vote_acct_max = vote_acct_max;
1700 0 : args->rocksdb_list_cnt = 0UL;
1701 0 : args->checkpt_status_cache = checkpt_status_cache;
1702 0 : args->one_off_features_cnt = 0UL;
1703 0 : args->snapshot_freq = snapshot_freq;
1704 0 : args->incremental_freq = incremental_freq;
1705 0 : args->snapshot_dir = snapshot_dir;
1706 0 : args->snapshot_tcnt = snapshot_tcnt;
1707 0 : args->allowed_mem_delta = allowed_mem_delta;
1708 0 : args->lthash = lthash;
1709 0 : args->snapshot_mismatch = snapshot_mismatch;
1710 0 : parse_one_off_features( args, one_off_features );
1711 0 : parse_rocksdb_list( args, rocksdb_list, rocksdb_list_starts );
1712 :
1713 0 : if( FD_UNLIKELY( sscanf( cluster_version, "%u.%u.%u", &args->cluster_version[0], &args->cluster_version[1], &args->cluster_version[2] )!=3 ) ) {
1714 0 : FD_LOG_ERR(( "failed to decode cluster version" ));;
1715 0 : }
1716 :
1717 0 : if( args->rocksdb_list_cnt==1UL ) {
1718 0 : FD_LOG_NOTICE(( "rocksdb=%s", args->rocksdb_list[0] ));
1719 0 : } else {
1720 0 : for( ulong i=0UL; i<args->rocksdb_list_cnt; ++i ) {
1721 0 : FD_LOG_NOTICE(( "rocksdb_list[ %lu ]=%s slot=%lu", i, args->rocksdb_list[i], args->rocksdb_list_slot[i-1] ));
1722 0 : }
1723 0 : }
1724 :
1725 0 : return 0;
1726 0 : }
1727 :
1728 : int main( int argc, char ** argv ) {
1729 : fd_ledger_args_t args = {0};
1730 : initial_setup( argc, argv, &args );
1731 :
1732 : if( args.cmd == NULL ) {
1733 : FD_LOG_ERR(( "no command specified" ));
1734 : } else if( strcmp( args.cmd, "replay" ) == 0 ) {
1735 : return replay( &args );
1736 : } else if( strcmp( args.cmd, "ingest" ) == 0 ) {
1737 : ingest( &args );
1738 : } else if( strcmp( args.cmd, "minify" ) == 0 ) {
1739 : minify( &args );
1740 : } else if( strcmp( args.cmd, "prune" ) == 0 ) {
1741 : prune( &args );
1742 : } else {
1743 : FD_LOG_ERR(( "unknown command=%s", args.cmd ));
1744 : }
1745 : return 0;
1746 : }
|