Line data Source code
1 : #ifndef HEADER_fd_src_discof_backup_fd_txncache_writer_h 2 : #define HEADER_fd_src_discof_backup_fd_txncache_writer_h 3 : 4 : /* fd_txncache_writer serializes the rooted part of a txncache into the 5 : snapshots/status_cache file of a Solana snapshot (bincode 6 : Vec<SlotDelta>), shaped the way Agave writes it: one slot delta per 7 : recent rooted slot with a block, holding the transactions that 8 : executed in that slot, grouped by the blockhash they referenced. 9 : The txncache bucket iterator handles locking and paging. */ 10 : 11 : #include "../../flamenco/runtime/fd_txncache.h" 12 : #include "../../flamenco/runtime/fd_txncache_shmem.h" 13 : 14 : /* FD_TXNCACHE_WRITER_MAX_SLOT_DELTAS is the number of slot deltas a 15 : status cache holds, mirroring Agave's StatusCache::max_root_entries 16 : (default MAX_ROOT_ENTRIES = MAX_RECENT_BLOCKHASHES = 300; the 17 : constant status_cache::MAX_CACHE_ENTRIES up to v3.x). Snapshot load 18 : walks the SlotHistory sysvar back from the snapshot slot and requires 19 : a slot delta for each of the newest max_root_entries slots that has 20 : a block. No shipped Agave changes it from 300; if it ever becomes 21 : configurable, this and FD_SLOT_DELTA_MAX_ENTRIES must follow. 22 : https://github.com/anza-xyz/agave/blob/v3.1.8/runtime/src/snapshot_bank_utils.rs#L609 */ 23 : 24 0 : #define FD_TXNCACHE_WRITER_MAX_SLOT_DELTAS (300UL) 25 : 26 0 : #define FD_TXNCACHE_WRITER_MAX_BLOCKHASHES (FD_TXNCACHE_MAX_SLOT_DELTAS+1UL) 27 : 28 : #define FD_TXNCACHE_WRITER_MAX_GROUPS (FD_TXNCACHE_WRITER_MAX_SLOT_DELTAS*FD_TXNCACHE_WRITER_MAX_BLOCKHASHES) 29 : 30 : /* FD_TXNCACHE_WRITER_BUF_MIN is the smallest output buffer 31 : fd_txncache_writer_serialize accepts (fits any single record). 32 : Serialization emits whole records only and stops when the next one 33 : does not fit; the caller comes back with more room. */ 34 : 35 0 : #define FD_TXNCACHE_WRITER_BUF_MIN (4096UL) 36 : 37 : struct fd_txncache_writer_blockhash_desc { 38 : ulong blockcache_idx; /* blockcache pool index */ 39 : uint generation; /* blockcache generation at init, detects reuse */ 40 : ushort slot_i; /* execution slot index, or USHORT_MAX for genesis */ 41 : uchar blockhash[ 32UL ]; 42 : ulong txnhash_offset; 43 : }; 44 : 45 : typedef struct fd_txncache_writer_blockhash_desc fd_txncache_writer_blockhash_desc_t; 46 : 47 : /* A group is one non-empty (execution slot, referenced blockhash) pair 48 : and serializes as one wire group in one slot delta. The dense group 49 : key is a position in slots[] paired with a position in 50 : blockhash_descs[]. */ 51 : struct fd_txncache_writer_group { 52 : ushort slot_i; 53 : ushort blockhash_i; 54 : uint entry_cnt; /* transactions in the group */ 55 : uint arena_entry_off; 56 : uint filled_entry_cnt; /* entries copied so far in the current fill */ 57 : }; 58 : 59 : typedef struct fd_txncache_writer_group fd_txncache_writer_group_t; 60 : 61 : typedef uchar fd_txnhash_t[ 20UL ]; 62 : 63 : struct fd_txncache_writer { 64 : uint state; 65 : fd_txncache_t * tc; 66 : ulong snapshot_slot; 67 : 68 : /* SlotDeltas (ascending). */ 69 : ulong slots[ FD_TXNCACHE_WRITER_MAX_SLOT_DELTAS ]; 70 : ulong slot_cnt; 71 : ulong group_cnt_by_slot_i[ FD_TXNCACHE_WRITER_MAX_SLOT_DELTAS ]; 72 : 73 : /* Referenced blockhash descriptors, oldest rooted blockcache first. */ 74 : fd_txncache_writer_blockhash_desc_t blockhash_descs[ FD_TXNCACHE_WRITER_MAX_BLOCKHASHES ]; 75 : ulong blockhash_cnt; 76 : ushort fork_id_to_blockhash_i[ USHORT_MAX+1UL ]; 77 : 78 : /* Groups in wire order (slot ascending, then blockhash descriptor ascending). */ 79 : uint entry_cnt_by_key[ FD_TXNCACHE_WRITER_MAX_GROUPS ]; 80 : uint group_i_by_key[ FD_TXNCACHE_WRITER_MAX_GROUPS ]; 81 : fd_txncache_writer_group_t groups[ FD_TXNCACHE_WRITER_MAX_GROUPS ]; 82 : ulong group_cnt; 83 : ulong entry_cnt; /* Transactions across all groups. */ 84 : 85 : /* Scratchpad for collecting transaction hashes in serialization 86 : order. */ 87 : fd_txnhash_t * arena; 88 : ulong arena_entry_cnt_max; 89 : ulong batch_hi; 90 : 91 : /* Serialization cursor. */ 92 : ulong slot_i; /* Next slot delta. */ 93 : ulong group_i; /* Next group. */ 94 : ulong entry_i; /* Next entry of the current group. */ 95 : }; 96 : 97 : typedef struct fd_txncache_writer fd_txncache_writer_t; 98 : 99 : FD_PROTOTYPES_BEGIN 100 : 101 : /* Returns the arena size the writer recommends for a txncache whose 102 : blocks hold at most max_txn_per_slot transactions. 103 : 104 : The writer stages transaction hashes in an arena the caller provides. 105 : The txncache stores transactions by referenced blockhash while the 106 : wire format wants them by execution slot, so the writer counts every 107 : group in one walk of the txncache, then fills the arena with as many 108 : consecutive groups as fit per further walk. A single group that does 109 : not fit the arena is fatal. Caller should use this function to size 110 : the arena. 111 : 112 : The arena must be aligned to fd_txncache_writer_arena_align(). */ 113 : 114 : FD_FN_CONST ulong 115 : fd_txncache_writer_arena_sz( ulong max_txn_per_slot ); 116 : 117 : FD_FN_CONST ulong 118 : fd_txncache_writer_arena_align( void ); 119 : 120 : /* fd_txncache_writer_init prepares writer to serialize the status 121 : cache of tc as of its newest root. The caller must keep the root 122 : from advancing until the writer is done. snapshot_slot is the newest 123 : root's slot. slot_history points to the slot_history_sz byte bincode 124 : encoding of the bank's SlotHistory sysvar account, which decides 125 : which slots get a slot delta and names each execution slot. arena 126 : is the arena_sz byte staging area. 127 : 128 : Walks the whole txncache once to count the transactions of every 129 : (slot, blockhash) group, so the serialized size is known on return. 130 : 131 : Returns writer. Crashes if more than one rooted blockhash 132 : descriptor lacks a named execution slot. */ 133 : 134 : fd_txncache_writer_t * 135 : fd_txncache_writer_init( fd_txncache_writer_t * writer, 136 : fd_txncache_t * tc, 137 : ulong snapshot_slot, 138 : uchar const * slot_history, 139 : ulong slot_history_sz, 140 : void * arena, 141 : ulong arena_sz ); 142 : 143 : /* fd_txncache_writer_serialized_sz returns the exact size in bytes of 144 : the status cache an initialized writer will produce. */ 145 : 146 : FD_FN_PURE ulong 147 : fd_txncache_writer_serialized_sz( fd_txncache_writer_t const * writer ); 148 : 149 : /* fd_txncache_writer_serialize writes the next chunk of the status 150 : cache to out_buf (buf_sz>=FD_TXNCACHE_WRITER_BUF_MIN). Returns the 151 : chunk size, or zero once the whole status cache was written. May 152 : walk the txncache to fill the arena with the next batch of groups. */ 153 : 154 : ulong 155 : fd_txncache_writer_serialize( fd_txncache_writer_t * writer, 156 : uchar * out_buf, 157 : ulong buf_sz ); 158 : 159 : FD_PROTOTYPES_END 160 : 161 : #endif /* HEADER_fd_src_discof_backup_fd_txncache_writer_h */