Line data Source code
1 : #include "fd_sysvar_slot_history.h"
2 : #include "fd_sysvar.h"
3 : #include "../fd_system_ids.h"
4 : #include "../fd_accdb_svm.h"
5 :
6 1692 : #define FD_SLOT_HISTORY_BITS_PER_BLOCK (8UL * sizeof(ulong))
7 :
8 18 : #define FD_SLOT_HISTORY_BLOCKS_LEN (FD_SLOT_HISTORY_MAX_ENTRIES / FD_SLOT_HISTORY_BITS_PER_BLOCK)
9 :
10 : /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/program/src/slot_history.rs#L16 */
11 :
12 : void
13 : fd_sysvar_slot_history_init( fd_bank_t * bank,
14 : fd_accdb_t * accdb,
15 9 : fd_capture_ctx_t * capture_ctx ) {
16 9 : uchar data[ FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ ];
17 9 : uchar * p = data;
18 :
19 : /* has_bits */
20 9 : *p = 1;
21 9 : p++;
22 :
23 : /* bits_bitvec_len */
24 9 : FD_STORE( ulong, p, FD_SLOT_HISTORY_BLOCKS_LEN );
25 9 : p += sizeof(ulong);
26 :
27 : /* content */
28 9 : fd_memset( p, 0, FD_SLOT_HISTORY_BLOCKS_LEN * sizeof(ulong) );
29 9 : p += FD_SLOT_HISTORY_BLOCKS_LEN * sizeof(ulong);
30 :
31 : /* bits_len */
32 9 : FD_STORE( ulong, p, FD_SLOT_HISTORY_MAX_ENTRIES );
33 9 : p += sizeof(ulong);
34 :
35 : /* next_slot */
36 9 : FD_STORE( ulong, p, bank->f.slot + 1UL );
37 9 : p += sizeof(ulong);
38 :
39 9 : FD_STATIC_ASSERT( FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ == 1 + sizeof(ulong) + FD_SLOT_HISTORY_BLOCKS_LEN * sizeof(ulong) + sizeof(ulong) + sizeof(ulong), "bin code size mismatch" );
40 :
41 9 : fd_sysvar_account_update( bank, accdb, capture_ctx, &fd_sysvar_slot_history_id, data, FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ );
42 9 : }
43 :
44 : void
45 : fd_sysvar_slot_history_update( fd_bank_t * bank,
46 : fd_accdb_t * accdb,
47 357 : fd_capture_ctx_t * capture_ctx ) {
48 357 : fd_accdb_svm_update_t update[1];
49 357 : fd_acc_t acc = fd_accdb_svm_open_rw( bank, accdb, update, &fd_sysvar_slot_history_id, 0 );
50 357 : FD_TEST( acc.lamports ); /* Slot history account must exist */
51 357 : FD_TEST( !memcmp( acc.owner, fd_sysvar_owner_id.uc, sizeof(fd_pubkey_t) ) ); /* Slot history account must be owned by sysvar owner */
52 :
53 357 : if( FD_UNLIKELY( acc.data[ 0 ]!=1 ) ) {
54 : /* initialize if !has_bits */
55 0 : if( FD_UNLIKELY( acc.data_len<FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ ) ) FD_LOG_HEXDUMP_ERR(( "invalid slot history sysvar (data_sz too small)", acc.data, acc.data_len ));
56 0 : acc.data[ 0 ] = 1;
57 0 : FD_STORE( ulong, acc.data+1, FD_SLOT_HISTORY_BLOCKS_LEN );
58 0 : fd_memset( acc.data+9, 0, FD_SLOT_HISTORY_BLOCKS_LEN * sizeof(ulong) );
59 0 : FD_STORE( ulong, acc.data+9+FD_SLOT_HISTORY_BLOCKS_LEN*sizeof(ulong), FD_SLOT_HISTORY_MAX_ENTRIES );
60 0 : FD_STORE( ulong, acc.data+9+FD_SLOT_HISTORY_BLOCKS_LEN*sizeof(ulong)+8UL, 0UL );
61 0 : }
62 :
63 357 : ulong bits_bitvec_len = FD_LOAD( ulong, acc.data+1UL );
64 357 : if( FD_UNLIKELY( !bits_bitvec_len ) ) {
65 3 : fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
66 3 : return;
67 3 : }
68 354 : ulong min_sz;
69 354 : if( FD_UNLIKELY( __builtin_umull_overflow( bits_bitvec_len, sizeof(ulong), &min_sz ) ) ) {
70 0 : FD_LOG_ERR(( "invalid slot history sysvar: bits_bitvec_len overflow (%lu)", bits_bitvec_len ));
71 0 : }
72 354 : if( FD_UNLIKELY( __builtin_uaddl_overflow( min_sz, 25UL, &min_sz ) ) ) {
73 0 : FD_LOG_ERR(( "invalid slot history sysvar: min_sz overflow" ));
74 0 : }
75 354 : if( FD_UNLIKELY( acc.data_len < min_sz ) ) {
76 0 : FD_LOG_ERR(( "invalid slot history sysvar: data_sz too small (%lu, required %lu)", acc.data_len, min_sz ));
77 0 : }
78 354 : uchar * bits = acc.data + 9UL;
79 354 : uchar * footer = acc.data + 9UL + bits_bitvec_len * sizeof(ulong);
80 354 : ulong next_slot = FD_LOAD( ulong, footer+8UL );
81 :
82 : /* https://github.com/anza-xyz/solana-sdk/blob/slot-history%40v2.2.1/slot-history/src/lib.rs#L62-L74 */
83 354 : ulong cur_slot = bank->f.slot;
84 354 : if( FD_UNLIKELY( cur_slot>next_slot && cur_slot-next_slot>=FD_SLOT_HISTORY_MAX_ENTRIES ) ) {
85 3 : fd_memset( bits, 0, bits_bitvec_len * sizeof(ulong) );
86 351 : } else {
87 1581 : for( ulong i=next_slot; i<cur_slot; i++ ) {
88 1230 : ulong block_idx = (i / FD_SLOT_HISTORY_BITS_PER_BLOCK) % bits_bitvec_len;
89 1230 : uchar * word = &bits[ block_idx*sizeof(ulong) ];
90 1230 : FD_STORE( ulong, word, FD_LOAD( ulong, word ) & (~(1UL << (i % FD_SLOT_HISTORY_BITS_PER_BLOCK))) );
91 1230 : }
92 351 : }
93 :
94 : /* new slot */
95 354 : ulong block_idx = (cur_slot / FD_SLOT_HISTORY_BITS_PER_BLOCK) % bits_bitvec_len;
96 354 : uchar * word = &bits[ block_idx*sizeof(ulong) ];
97 354 : FD_STORE( ulong, word, FD_LOAD( ulong, word ) | (1UL << (cur_slot % FD_SLOT_HISTORY_BITS_PER_BLOCK)) );
98 :
99 354 : FD_STORE( ulong, footer+8UL, cur_slot+1UL );
100 :
101 354 : fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
102 354 : }
103 :
104 : int
105 : fd_sysvar_slot_history_validate( uchar const * data,
106 8028 : ulong sz ) {
107 8028 : if( FD_UNLIKELY( sz < 17UL ) ) return 0;
108 8019 : uchar has_bits = data[0];
109 8019 : if( FD_UNLIKELY( has_bits>1 ) ) return 0;
110 8016 : if( !has_bits ) return 1;
111 8016 : if( FD_UNLIKELY( sz < 25UL ) ) return 0;
112 8007 : ulong blocks_len = FD_LOAD( ulong, data+1 );
113 8007 : ulong min_sz;
114 8007 : if( FD_UNLIKELY( __builtin_umull_overflow( blocks_len, sizeof(ulong), &min_sz ) ) ) return 0;
115 8004 : if( FD_UNLIKELY( __builtin_uaddl_overflow( min_sz, 25UL, &min_sz ) ) ) return 0;
116 8001 : if( FD_UNLIKELY( sz < min_sz ) ) return 0;
117 7998 : return 1;
118 8001 : }
119 :
120 : fd_slot_history_view_t *
121 : fd_sysvar_slot_history_view( fd_slot_history_view_t * view,
122 : uchar const * data,
123 33 : ulong sz ) {
124 33 : if( FD_UNLIKELY( !fd_sysvar_slot_history_validate( data, sz ) ) ) return NULL;
125 27 : if( FD_UNLIKELY( !data[0] ) ) {
126 0 : view->bits = NULL;
127 0 : view->blocks_len = 0UL;
128 0 : view->bits_len = FD_LOAD( ulong, data+1 );
129 0 : view->next_slot = FD_LOAD( ulong, data+1+8UL );
130 0 : return view;
131 0 : }
132 27 : ulong blocks_len = FD_LOAD( ulong, data+1 );
133 27 : uchar const * footer = data + 9UL + blocks_len * sizeof(ulong);
134 27 : view->bits = data + 9UL;
135 27 : view->blocks_len = blocks_len;
136 27 : view->bits_len = FD_LOAD( ulong, footer );
137 27 : view->next_slot = FD_LOAD( ulong, footer+8UL );
138 27 : return view;
139 27 : }
140 :
141 : int
142 : fd_sysvar_slot_history_find_slot( fd_slot_history_view_t const * view,
143 81 : ulong slot ) {
144 81 : if( FD_UNLIKELY( !view->blocks_len ) ) return FD_SLOT_HISTORY_SLOT_NOT_FOUND;
145 78 : if( slot > view->next_slot - 1UL ) {
146 21 : return FD_SLOT_HISTORY_SLOT_FUTURE;
147 21 : }
148 57 : if( slot < fd_ulong_sat_sub( view->next_slot, FD_SLOT_HISTORY_MAX_ENTRIES ) ) {
149 12 : return FD_SLOT_HISTORY_SLOT_TOO_OLD;
150 12 : }
151 45 : ulong block_idx = (slot / FD_SLOT_HISTORY_BITS_PER_BLOCK) % view->blocks_len;
152 45 : ulong word = FD_LOAD( ulong, view->bits + block_idx*sizeof(ulong) );
153 45 : if( word & (1UL << (slot % FD_SLOT_HISTORY_BITS_PER_BLOCK)) ) {
154 21 : return FD_SLOT_HISTORY_SLOT_FOUND;
155 21 : }
156 24 : return FD_SLOT_HISTORY_SLOT_NOT_FOUND;
157 45 : }
|