Line data Source code
1 : #include "../../../../disco/tiles.h"
2 :
3 : #include "../../../../ballet/pack/fd_pack.h"
4 : #include "../../../../ballet/blake3/fd_blake3.h"
5 : #include "../../../../ballet/bmtree/fd_bmtree.h"
6 : #include "../../../../disco/metrics/fd_metrics.h"
7 : #include "../../../../disco/topo/fd_pod_format.h"
8 : #include "../../../../disco/bank/fd_bank_abi.h"
9 : #include "../../../../disco/metrics/generated/fd_metrics_bank.h"
10 :
11 0 : #define FD_BANK_TRANSACTION_LANDED 1
12 : #define FD_BANK_TRANSACTION_EXECUTED 2
13 :
14 : typedef struct {
15 : ulong kind_id;
16 :
17 : fd_blake3_t * blake3;
18 : void * bmtree;
19 :
20 : uchar * txn_abi_mem;
21 : uchar * txn_sidecar_mem;
22 :
23 : void const * _bank;
24 : ulong _microblock_idx;
25 :
26 : ulong * busy_fseq;
27 :
28 : fd_wksp_t * pack_in_mem;
29 : ulong pack_in_chunk0;
30 : ulong pack_in_wmark;
31 :
32 : fd_wksp_t * out_mem;
33 : ulong out_chunk0;
34 : ulong out_wmark;
35 : ulong out_chunk;
36 :
37 : struct {
38 : ulong slot_acquire[ 3 ];
39 :
40 : ulong txn_load_address_lookup_tables[ 6 ];
41 : ulong transaction_result[ 39 ];
42 : ulong processing_failed;
43 : ulong fee_only;
44 : ulong exec_failed;
45 : ulong success;
46 : } metrics;
47 : } fd_bank_ctx_t;
48 :
49 : FD_FN_CONST static inline ulong
50 12 : scratch_align( void ) {
51 12 : return 128UL;
52 12 : }
53 :
54 : FD_FN_PURE static inline ulong
55 12 : scratch_footprint( fd_topo_tile_t const * tile ) {
56 12 : (void)tile;
57 12 : ulong l = FD_LAYOUT_INIT;
58 12 : l = FD_LAYOUT_APPEND( l, alignof( fd_bank_ctx_t ), sizeof( fd_bank_ctx_t ) );
59 12 : l = FD_LAYOUT_APPEND( l, FD_BLAKE3_ALIGN, FD_BLAKE3_FOOTPRINT );
60 12 : l = FD_LAYOUT_APPEND( l, FD_BMTREE_COMMIT_ALIGN, FD_BMTREE_COMMIT_FOOTPRINT(0) );
61 12 : l = FD_LAYOUT_APPEND( l, FD_BANK_ABI_TXN_ALIGN, MAX_TXN_PER_MICROBLOCK*FD_BANK_ABI_TXN_FOOTPRINT );
62 12 : l = FD_LAYOUT_APPEND( l, FD_BANK_ABI_TXN_ALIGN, FD_BANK_ABI_TXN_FOOTPRINT_SIDECAR_MAX );
63 12 : return FD_LAYOUT_FINI( l, scratch_align() );
64 12 : }
65 :
66 : static inline void
67 0 : metrics_write( fd_bank_ctx_t * ctx ) {
68 0 : FD_MCNT_ENUM_COPY( BANK, SLOT_ACQUIRE, ctx->metrics.slot_acquire );
69 :
70 0 : FD_MCNT_ENUM_COPY( BANK, TRANSACTION_LOAD_ADDRESS_TABLES, ctx->metrics.txn_load_address_lookup_tables );
71 0 : FD_MCNT_ENUM_COPY( BANK, TRANSACTION_RESULT, ctx->metrics.transaction_result );
72 :
73 0 : FD_MCNT_SET( BANK, PROCESSING_FAILED, ctx->metrics.processing_failed );
74 0 : FD_MCNT_SET( BANK, FEE_ONLY_TRANSACTIONS, ctx->metrics.fee_only );
75 0 : FD_MCNT_SET( BANK, EXECUTED_FAILED_TRANSACTIONS, ctx->metrics.exec_failed );
76 0 : FD_MCNT_SET( BANK, SUCCESSFUL_TRANSACTIONS, ctx->metrics.success );
77 0 : }
78 :
79 : static int
80 : before_frag( fd_bank_ctx_t * ctx,
81 : ulong in_idx,
82 : ulong seq,
83 0 : ulong sig ) {
84 0 : (void)in_idx;
85 0 : (void)seq;
86 :
87 : /* Pack also outputs "leader slot done" which we can ignore. */
88 0 : if( FD_UNLIKELY( fd_disco_poh_sig_pkt_type( sig )!=POH_PKT_TYPE_MICROBLOCK ) ) return 1;
89 :
90 0 : ulong target_bank_idx = fd_disco_poh_sig_bank_tile( sig );
91 0 : if( FD_UNLIKELY( target_bank_idx!=ctx->kind_id ) ) return 1;
92 :
93 0 : return 0;
94 0 : }
95 :
96 : extern void * fd_ext_bank_pre_balance_info( void const * bank, void * txns, ulong txn_cnt );
97 : extern void * fd_ext_bank_load_and_execute_txns( void const * bank, void * txns, ulong txn_cnt, int * out_processing_results, int * out_transaction_err, uint * out_consumed_cus );
98 : extern void fd_ext_bank_commit_txns( void const * bank, void const * txns, ulong txn_cnt , void * load_and_execute_output, void * pre_balance_info );
99 : extern void fd_ext_bank_release_thunks( void * load_and_execute_output );
100 : extern void fd_ext_bank_release_pre_balance_info( void * pre_balance_info );
101 : extern int fd_ext_bank_verify_precompiles( void const * bank, void const * txn );
102 :
103 : static inline void
104 : during_frag( fd_bank_ctx_t * ctx,
105 : ulong in_idx,
106 : ulong seq,
107 : ulong sig,
108 : ulong chunk,
109 0 : ulong sz ) {
110 0 : (void)in_idx;
111 0 : (void)seq;
112 0 : (void)sig;
113 :
114 0 : uchar * src = (uchar *)fd_chunk_to_laddr( ctx->pack_in_mem, chunk );
115 0 : uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
116 :
117 0 : if( FD_UNLIKELY( chunk<ctx->pack_in_chunk0 || chunk>ctx->pack_in_wmark || sz>USHORT_MAX ) )
118 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->pack_in_chunk0, ctx->pack_in_wmark ));
119 :
120 0 : fd_memcpy( dst, src, sz-sizeof(fd_microblock_bank_trailer_t) );
121 0 : fd_microblock_bank_trailer_t * trailer = (fd_microblock_bank_trailer_t *)( src+sz-sizeof(fd_microblock_bank_trailer_t) );
122 0 : ctx->_bank = trailer->bank;
123 0 : ctx->_microblock_idx = trailer->microblock_idx;
124 0 : }
125 :
126 : static void
127 : hash_transactions( void * mem,
128 : fd_txn_p_t * txns,
129 : ulong txn_cnt,
130 0 : uchar * mixin ) {
131 0 : fd_bmtree_commit_t * bmtree = fd_bmtree_commit_init( mem, 32UL, 1UL, 0UL );
132 0 : for( ulong i=0; i<txn_cnt; i++ ) {
133 0 : fd_txn_p_t * _txn = txns + i;
134 0 : if( FD_UNLIKELY( !(_txn->flags & FD_TXN_P_FLAGS_EXECUTE_SUCCESS) ) ) continue;
135 :
136 0 : fd_txn_t * txn = TXN(_txn);
137 0 : for( ulong j=0; j<txn->signature_cnt; j++ ) {
138 0 : fd_bmtree_node_t node[1];
139 0 : fd_bmtree_hash_leaf( node, _txn->payload+txn->signature_off+64UL*j, 64UL, 1UL );
140 0 : fd_bmtree_commit_append( bmtree, node, 1UL );
141 0 : }
142 0 : }
143 0 : uchar * root = fd_bmtree_commit_fini( bmtree );
144 0 : fd_memcpy( mixin, root, 32UL );
145 0 : }
146 :
147 : static inline void
148 : after_frag( fd_bank_ctx_t * ctx,
149 : ulong in_idx,
150 : ulong seq,
151 : ulong sig,
152 : ulong sz,
153 : ulong tsorig,
154 0 : fd_stem_context_t * stem ) {
155 0 : (void)in_idx;
156 0 : (void)tsorig;
157 :
158 0 : uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->out_mem, ctx->out_chunk );
159 :
160 0 : ulong slot = fd_disco_poh_sig_slot( sig );
161 0 : ulong txn_cnt = (sz-sizeof(fd_microblock_bank_trailer_t))/sizeof(fd_txn_p_t);
162 :
163 0 : ulong sanitized_txn_cnt = 0UL;
164 0 : ulong sidecar_footprint_bytes = 0UL;
165 0 : for( ulong i=0UL; i<txn_cnt; i++ ) {
166 0 : fd_txn_p_t * txn = (fd_txn_p_t *)( dst + (i*sizeof(fd_txn_p_t)) );
167 :
168 0 : void * abi_txn = ctx->txn_abi_mem + (sanitized_txn_cnt*FD_BANK_ABI_TXN_FOOTPRINT);
169 0 : void * abi_txn_sidecar = ctx->txn_sidecar_mem + sidecar_footprint_bytes;
170 0 : txn->flags &= ~FD_TXN_P_FLAGS_SANITIZE_SUCCESS;
171 :
172 0 : int result = fd_bank_abi_txn_init( abi_txn, abi_txn_sidecar, ctx->_bank, slot, ctx->blake3, txn->payload, txn->payload_sz, TXN(txn), !!(txn->flags & FD_TXN_P_FLAGS_IS_SIMPLE_VOTE) );
173 0 : ctx->metrics.txn_load_address_lookup_tables[ result ]++;
174 0 : if( FD_UNLIKELY( result!=FD_BANK_ABI_TXN_INIT_SUCCESS ) ) continue;
175 :
176 0 : int precompile_result = fd_ext_bank_verify_precompiles( ctx->_bank, abi_txn );
177 0 : if( FD_UNLIKELY( precompile_result ) ) {
178 0 : FD_MCNT_INC( BANK, PRECOMPILE_VERIFY_FAILURE, 1 );
179 0 : continue;
180 0 : }
181 :
182 0 : txn->flags |= FD_TXN_P_FLAGS_SANITIZE_SUCCESS;
183 :
184 0 : fd_txn_t * txn1 = TXN(txn);
185 0 : sidecar_footprint_bytes += FD_BANK_ABI_TXN_FOOTPRINT_SIDECAR( txn1->acct_addr_cnt, txn1->addr_table_adtl_cnt, txn1->instr_cnt, txn1->addr_table_lookup_cnt );
186 0 : sanitized_txn_cnt++;
187 0 : }
188 :
189 : /* Just because a transaction was executed doesn't mean it succeeded,
190 : but all executed transactions get committed. */
191 0 : int processing_results[ MAX_TXN_PER_MICROBLOCK ] = { 0 };
192 0 : int transaction_err [ MAX_TXN_PER_MICROBLOCK ] = { 0 };
193 0 : uint consumed_cus [ MAX_TXN_PER_MICROBLOCK ] = { 0U };
194 :
195 0 : void * pre_balance_info = fd_ext_bank_pre_balance_info( ctx->_bank, ctx->txn_abi_mem, sanitized_txn_cnt );
196 :
197 0 : void * load_and_execute_output = fd_ext_bank_load_and_execute_txns( ctx->_bank,
198 0 : ctx->txn_abi_mem,
199 0 : sanitized_txn_cnt,
200 0 : processing_results,
201 0 : transaction_err,
202 0 : consumed_cus );
203 :
204 0 : ulong sanitized_idx = 0UL;
205 0 : for( ulong i=0; i<txn_cnt; i++ ) {
206 0 : fd_txn_p_t * txn = (fd_txn_p_t *)( dst + (i*sizeof(fd_txn_p_t)) );
207 :
208 0 : uint requested_cus = txn->pack_cu.requested_execution_cus;
209 0 : uint non_execution_cus = txn->pack_cu.non_execution_cus;
210 : /* Assume failure, set below if success. If it doesn't land in the
211 : block, rebate the non-execution CUs too. */
212 0 : txn->bank_cu.rebated_cus = requested_cus + non_execution_cus;
213 0 : txn->flags &= ~FD_TXN_P_FLAGS_EXECUTE_SUCCESS;
214 0 : if( FD_UNLIKELY( !(txn->flags & FD_TXN_P_FLAGS_SANITIZE_SUCCESS) ) ) continue;
215 :
216 0 : sanitized_idx++;
217 :
218 : /* Stash the result in the flags value so that pack can inspect it.
219 : */
220 0 : txn->flags = (txn->flags & 0x00FFFFFFU) | ((uint)transaction_err[ sanitized_idx-1UL ]<<24);
221 :
222 0 : ctx->metrics.transaction_result[ transaction_err [ sanitized_idx-1UL ] ]++;
223 :
224 0 : ctx->metrics.processing_failed += (ulong)(processing_results[ sanitized_idx-1UL ]==0 );
225 0 : ctx->metrics.fee_only += (ulong)(processing_results[ sanitized_idx-1UL ]==FD_BANK_TRANSACTION_LANDED);
226 :
227 0 : if( FD_UNLIKELY( !(processing_results[ sanitized_idx-1UL ] & FD_BANK_TRANSACTION_LANDED) ) ) continue;
228 :
229 : /* TXN_P_FLAGS_EXECUTE_SUCCESS means that it should be included in
230 : the block. It's a bit of a misnomer now that there are fee-only
231 : transactions. */
232 0 : txn->flags |= FD_TXN_P_FLAGS_EXECUTE_SUCCESS;
233 :
234 0 : if( FD_UNLIKELY( !(processing_results[ sanitized_idx-1UL ] & FD_BANK_TRANSACTION_EXECUTED) ) ) continue;
235 :
236 0 : if( transaction_err[ sanitized_idx-1UL ] ) ctx->metrics.exec_failed++;
237 0 : else ctx->metrics.success++;
238 :
239 0 : uint executed_cus = consumed_cus[ sanitized_idx-1UL ];
240 0 : txn->bank_cu.actual_consumed_cus = non_execution_cus + executed_cus;
241 0 : if( FD_UNLIKELY( executed_cus>requested_cus ) ) {
242 : /* There's basically a bug in the Agave codebase right now
243 : regarding the cost model for some transactions. Some built-in
244 : instructions like creating an address lookup table consume more
245 : CUs than the cost model allocates for them, which is only
246 : allowed because the runtime computes requested CUs differently
247 : from the cost model. Rather than implement a broken system,
248 : we'll just permit the risk of slightly overpacking blocks by
249 : ignoring these transactions when it comes to rebating. */
250 0 : FD_LOG_INFO(( "Transaction executed %u CUs but only requested %u CUs", executed_cus, requested_cus ));
251 0 : FD_MCNT_INC( BANK, COST_MODEL_UNDERCOUNT, 1UL );
252 0 : txn->bank_cu.rebated_cus = 0U;
253 0 : continue;
254 0 : }
255 0 : txn->bank_cu.rebated_cus = requested_cus - executed_cus;
256 0 : }
257 :
258 : /* Commit must succeed so no failure path. This function takes
259 : ownership of the load_and_execute_output and pre_balance_info heap
260 : allocations and will free them before it returns. They should not
261 : be reused. Once commit is called, the transactions MUST be mixed
262 : into the PoH otherwise we will fork and diverge, so the link from
263 : here til PoH mixin must be completely reliable with nothing dropped. */
264 0 : fd_ext_bank_commit_txns( ctx->_bank, ctx->txn_abi_mem, sanitized_txn_cnt, load_and_execute_output, pre_balance_info );
265 0 : pre_balance_info = NULL;
266 0 : load_and_execute_output = NULL;
267 :
268 : /* Indicate to pack tile we are done processing the transactions so
269 : it can pack new microblocks using these accounts. This has to be
270 : done after commiting the transactions to poh otherwise there is a
271 : race. */
272 0 : fd_fseq_update( ctx->busy_fseq, seq );
273 :
274 : /* Now produce the merkle hash of the transactions for inclusion
275 : (mixin) to the PoH hash. This is done on the bank tile because
276 : it shards / scales horizontally here, while PoH does not. */
277 0 : fd_microblock_trailer_t * trailer = (fd_microblock_trailer_t *)( dst + txn_cnt*sizeof(fd_txn_p_t) );
278 0 : hash_transactions( ctx->bmtree, (fd_txn_p_t*)dst, txn_cnt, trailer->hash );
279 :
280 : /* MAX_MICROBLOCK_SZ - (MAX_TXN_PER_MICROBLOCK*sizeof(fd_txn_p_t)) == 64
281 : so there's always 64 extra bytes at the end to stash the hash. */
282 0 : FD_STATIC_ASSERT( MAX_MICROBLOCK_SZ-(MAX_TXN_PER_MICROBLOCK*sizeof(fd_txn_p_t))>=sizeof(fd_microblock_trailer_t), poh_shred_mtu );
283 0 : FD_STATIC_ASSERT( MAX_MICROBLOCK_SZ-(MAX_TXN_PER_MICROBLOCK*sizeof(fd_txn_p_t))>=sizeof(fd_microblock_bank_trailer_t), poh_shred_mtu );
284 :
285 : /* We have a race window with the GUI, where if the slot is ending it
286 : will snap these metrics to draw the waterfall, but see them outdated
287 : because housekeeping hasn't run. For now just update them here, but
288 : PoH should eventually flush the pipeline before ending the slot. */
289 0 : metrics_write( ctx );
290 :
291 0 : ulong bank_sig = fd_disco_bank_sig( slot, ctx->_microblock_idx );
292 :
293 : /* We always need to publish, even if there are no successfully executed
294 : transactions so the PoH tile can keep an accurate count of microblocks
295 : it has seen. */
296 0 : ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_tickcount() );
297 0 : ulong new_sz = txn_cnt*sizeof(fd_txn_p_t) + sizeof(fd_microblock_trailer_t);
298 0 : fd_stem_publish( stem, 0UL, bank_sig, ctx->out_chunk, new_sz, 0UL, 0UL, tspub );
299 0 : ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, new_sz, ctx->out_chunk0, ctx->out_wmark );
300 0 : }
301 :
302 : static void
303 : unprivileged_init( fd_topo_t * topo,
304 0 : fd_topo_tile_t * tile ) {
305 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
306 :
307 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
308 0 : fd_bank_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_bank_ctx_t ), sizeof( fd_bank_ctx_t ) );
309 0 : void * blake3 = FD_SCRATCH_ALLOC_APPEND( l, FD_BLAKE3_ALIGN, FD_BLAKE3_FOOTPRINT );
310 0 : void * bmtree = FD_SCRATCH_ALLOC_APPEND( l, FD_BMTREE_COMMIT_ALIGN, FD_BMTREE_COMMIT_FOOTPRINT(0) );
311 0 : ctx->txn_abi_mem = FD_SCRATCH_ALLOC_APPEND( l, FD_BANK_ABI_TXN_ALIGN, MAX_TXN_PER_MICROBLOCK*FD_BANK_ABI_TXN_FOOTPRINT );
312 0 : ctx->txn_sidecar_mem = FD_SCRATCH_ALLOC_APPEND( l, FD_BANK_ABI_TXN_ALIGN, FD_BANK_ABI_TXN_FOOTPRINT_SIDECAR_MAX );
313 :
314 0 : #define NONNULL( x ) (__extension__({ \
315 0 : __typeof__((x)) __x = (x); \
316 0 : if( FD_UNLIKELY( !__x ) ) FD_LOG_ERR(( #x " was unexpectedly NULL" )); \
317 0 : __x; }))
318 :
319 0 : ctx->kind_id = tile->kind_id;
320 0 : ctx->blake3 = NONNULL( fd_blake3_join( fd_blake3_new( blake3 ) ) );
321 0 : ctx->bmtree = NONNULL( bmtree );
322 :
323 0 : ulong busy_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "bank_busy.%lu", tile->kind_id );
324 0 : FD_TEST( busy_obj_id!=ULONG_MAX );
325 0 : ctx->busy_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, busy_obj_id ) );
326 0 : if( FD_UNLIKELY( !ctx->busy_fseq ) ) FD_LOG_ERR(( "banking tile %lu has no busy flag", tile->kind_id ));
327 :
328 0 : memset( &ctx->metrics, 0, sizeof( ctx->metrics ) );
329 :
330 0 : ctx->pack_in_mem = topo->workspaces[ topo->objs[ topo->links[ tile->in_link_id[ 0UL ] ].dcache_obj_id ].wksp_id ].wksp;
331 0 : ctx->pack_in_chunk0 = fd_dcache_compact_chunk0( ctx->pack_in_mem, topo->links[ tile->in_link_id[ 0UL ] ].dcache );
332 0 : ctx->pack_in_wmark = fd_dcache_compact_wmark ( ctx->pack_in_mem, topo->links[ tile->in_link_id[ 0UL ] ].dcache, topo->links[ tile->in_link_id[ 0UL ] ].mtu );
333 :
334 0 : ctx->out_mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
335 0 : ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
336 0 : ctx->out_wmark = fd_dcache_compact_wmark ( ctx->out_mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
337 0 : ctx->out_chunk = ctx->out_chunk0;
338 0 : }
339 :
340 0 : #define STEM_BURST (1UL)
341 :
342 : /* See explanation in fd_pack */
343 0 : #define STEM_LAZY (128L*3000L)
344 :
345 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_bank_ctx_t
346 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_bank_ctx_t)
347 :
348 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
349 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
350 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
351 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
352 :
353 : #include "../../../../disco/stem/fd_stem.c"
354 :
355 : fd_topo_run_tile_t fd_tile_bank = {
356 : .name = "bank",
357 : .scratch_align = scratch_align,
358 : .scratch_footprint = scratch_footprint,
359 : .unprivileged_init = unprivileged_init,
360 : .run = stem_run,
361 : };
|