Line data Source code
1 : #include "fd_hashes.h"
2 : #include "fd_bank.h"
3 : #include "../../ballet/sha256/fd_sha256.h"
4 : #include "../capture/fd_capture_ctx.h"
5 :
6 : static fd_blake3_t *
7 : account_lthash_init( fd_blake3_t * b3,
8 : uchar const pubkey[ static FD_HASH_FOOTPRINT ],
9 : uchar const owner[ static FD_HASH_FOOTPRINT ],
10 : ulong lamports,
11 : int executable,
12 : uchar const * data,
13 7769 : ulong data_len ) {
14 7769 : uchar executable_flag = !!executable;
15 :
16 7769 : fd_blake3_init( b3 );
17 7769 : fd_blake3_append( b3, &lamports, sizeof( ulong ) );
18 7769 : fd_blake3_append( b3, data, data_len );
19 7769 : fd_blake3_append( b3, &executable_flag, sizeof( uchar ) );
20 7769 : fd_blake3_append( b3, owner, FD_HASH_FOOTPRINT );
21 7769 : fd_blake3_append( b3, pubkey, FD_HASH_FOOTPRINT );
22 7769 : return b3;
23 7769 : }
24 :
25 : void
26 : fd_hashes_account_lthash_simple( uchar const pubkey[ static FD_HASH_FOOTPRINT ],
27 : uchar const owner[ static FD_HASH_FOOTPRINT ],
28 : ulong lamports,
29 : int executable,
30 : uchar const * data,
31 : ulong data_len,
32 3717 : fd_lthash_value_t * lthash_out ) {
33 3717 : if( FD_UNLIKELY( !lamports ) ) {
34 211 : fd_lthash_zero( lthash_out );
35 211 : return;
36 211 : }
37 :
38 3506 : fd_blake3_t b3[1];
39 3506 : fd_blake3_fini_2048( account_lthash_init( b3, pubkey, owner, lamports, executable, data, data_len ), lthash_out->bytes );
40 3506 : }
41 :
42 : void
43 : fd_hashes_account_lthash_xn( ulong n,
44 : uchar const * const * pubkey,
45 : uchar const * const * owner,
46 : ulong const * lamports,
47 : int const * executable,
48 : uchar const * const * data,
49 : ulong const * data_len,
50 2143 : fd_lthash_value_t ** lthash_out ) {
51 2143 : fd_blake3_t b3 [ FD_BLAKE3_XN_MAX ];
52 2143 : fd_blake3_t * sha [ FD_BLAKE3_XN_MAX ];
53 2143 : void * hash[ FD_BLAKE3_XN_MAX ];
54 2143 : ulong m = 0UL;
55 :
56 6430 : for( ulong i=0UL; i<n; i++ ) {
57 4287 : if( FD_UNLIKELY( !lamports[ i ] ) ) {
58 26 : fd_lthash_zero( lthash_out[ i ] );
59 26 : continue;
60 26 : }
61 :
62 4261 : sha [ m ] = account_lthash_init( b3+m, pubkey[ i ], owner[ i ], lamports[ i ], executable[ i ], data[ i ], data_len[ i ] );
63 4261 : hash[ m ] = lthash_out[ i ]->bytes;
64 4261 : m++;
65 4261 : }
66 :
67 2147 : if( FD_LIKELY( m ) ) fd_blake3_fini_2048_xn( m, sha, hash );
68 2143 : }
69 :
70 : void
71 : fd_hashes_hash_bank( fd_lthash_value_t const * lthash,
72 : fd_hash_t const * prev_bank_hash,
73 : fd_hash_t const * last_blockhash,
74 : ulong signature_count,
75 611 : fd_hash_t * hash_out ) {
76 :
77 : /* The bank hash for a slot is a sha256 of two sub-hashes:
78 : sha256(
79 : sha256( previous bank hash, signature count, last PoH blockhash ),
80 : lthash of the accounts modified in this slot
81 : )
82 : */
83 611 : fd_sha256_t sha;
84 611 : fd_sha256_init( &sha );
85 611 : fd_sha256_append( &sha, prev_bank_hash, sizeof( fd_hash_t ) );
86 611 : fd_sha256_append( &sha, (uchar const *) &signature_count, sizeof( ulong ) );
87 611 : fd_sha256_append( &sha, (uchar const *) last_blockhash, sizeof( fd_hash_t ) );
88 611 : fd_sha256_fini( &sha, hash_out->hash );
89 :
90 611 : fd_sha256_init( &sha );
91 611 : fd_sha256_append( &sha, (uchar const *) hash_out->hash, sizeof(fd_hash_t) );
92 611 : fd_sha256_append( &sha, (uchar const *) lthash->bytes, sizeof(fd_lthash_value_t) );
93 611 : fd_sha256_fini( &sha, hash_out->hash );
94 611 : }
95 :
96 : static void
97 : update_bank_lthash( fd_bank_t * bank,
98 : fd_lthash_value_t const * lthash_prev,
99 2173 : fd_lthash_value_t const * lthash_post ) {
100 2173 : fd_lthash_value_t delta[1];
101 2173 : fd_memcpy( delta, lthash_post, sizeof(fd_lthash_value_t) );
102 2173 : fd_lthash_sub( delta, lthash_prev );
103 :
104 2173 : fd_lthash_value_t * bank_lthash = fd_bank_lthash_locking_modify( bank );
105 2173 : fd_lthash_add( bank_lthash, delta );
106 2173 : fd_bank_lthash_end_locking_modify( bank );
107 2173 : }
108 :
109 : void
110 : fd_hashes_update_simple( fd_lthash_value_t * lthash_post, /* out */
111 : fd_lthash_value_t const * lthash_prev, /* in */
112 : uchar const pubkey[ static FD_HASH_FOOTPRINT ],
113 : uchar const owner[ static FD_HASH_FOOTPRINT ],
114 : ulong lamports,
115 : int executable,
116 : uchar const * data,
117 : ulong data_len,
118 : fd_bank_t * bank,
119 26 : fd_capture_ctx_t * capture_ctx ) {
120 26 : if( bank->lthash_mode==FD_BANK_LTHASH_MODE_INBAND ) {
121 26 : fd_hashes_account_lthash_simple( pubkey, owner, lamports, executable, data, data_len, lthash_post );
122 26 : update_bank_lthash( bank, lthash_prev, lthash_post );
123 26 : } else if( bank->lthash_mode==FD_BANK_LTHASH_MODE_OOB_RECORD ) {
124 0 : fd_bank_lthash_record( bank, pubkey );
125 0 : }
126 :
127 26 : fd_hashes_capture_account( pubkey, owner, lamports, executable, data, data_len, bank, capture_ctx );
128 26 : }
129 :
130 : void
131 : fd_hashes_update_pair( uchar const pubkey[ static FD_HASH_FOOTPRINT ],
132 : uchar const prev_owner[ static FD_HASH_FOOTPRINT ],
133 : ulong prev_lamports,
134 : int prev_executable,
135 : uchar const * prev_data,
136 : ulong prev_data_len,
137 : uchar const owner[ static FD_HASH_FOOTPRINT ],
138 : ulong lamports,
139 : int executable,
140 : uchar const * data,
141 : ulong data_len,
142 : fd_bank_t * bank,
143 2148 : fd_capture_ctx_t * capture_ctx ) {
144 2148 : if( bank->lthash_mode==FD_BANK_LTHASH_MODE_INBAND ) {
145 2148 : fd_lthash_value_t lthash_prev[1];
146 2148 : fd_lthash_value_t lthash_post[1];
147 2148 : uchar const * pk [2] = { pubkey, pubkey };
148 2148 : uchar const * ow [2] = { prev_owner, owner };
149 2148 : ulong la [2] = { prev_lamports, lamports };
150 2148 : int ex [2] = { prev_executable, executable };
151 2148 : uchar const * da [2] = { prev_data, data };
152 2148 : ulong dl [2] = { prev_data_len, data_len };
153 2148 : fd_lthash_value_t * out[2] = { lthash_prev, lthash_post };
154 2148 : fd_hashes_account_lthash_xn( 2UL, pk, ow, la, ex, da, dl, out );
155 2148 : update_bank_lthash( bank, lthash_prev, lthash_post );
156 2148 : } else if( bank->lthash_mode==FD_BANK_LTHASH_MODE_OOB_RECORD ) {
157 0 : fd_bank_lthash_record( bank, pubkey );
158 0 : }
159 :
160 2148 : fd_hashes_capture_account( pubkey, owner, lamports, executable, data, data_len, bank, capture_ctx );
161 2148 : }
162 :
163 : void
164 : fd_hashes_capture_account( uchar const pubkey[ static FD_HASH_FOOTPRINT ],
165 : uchar const owner[ static FD_HASH_FOOTPRINT ],
166 : ulong lamports,
167 : int executable,
168 : uchar const * data,
169 : ulong data_len,
170 : fd_bank_t * bank,
171 2311 : fd_capture_ctx_t * capture_ctx ) {
172 2311 : if( FD_LIKELY( !capture_ctx ||
173 2311 : !capture_ctx->capture_solcap ||
174 2311 : bank->f.slot<capture_ctx->solcap_start_slot ) ) return;
175 :
176 0 : fd_solana_account_meta_t solana_meta[1];
177 0 : fd_solana_account_meta_init( solana_meta, lamports, owner, executable );
178 0 : fd_capture_link_write_account_update(
179 0 : capture_ctx,
180 0 : capture_ctx->current_txn_idx,
181 0 : (fd_pubkey_t const *)pubkey,
182 0 : solana_meta,
183 0 : bank->f.slot,
184 0 : data,
185 0 : data_len );
186 0 : }
187 :
188 : void
189 : fd_hashes_apply_hard_forks( fd_hash_t * hash,
190 : ulong slot,
191 : ulong parent_slot,
192 : fd_hard_fork_t const * hard_forks,
193 611 : ulong hard_fork_cnt ) {
194 611 : ulong sum = 0UL;
195 611 : for( ulong i=0UL; i<hard_fork_cnt; i++ ) {
196 0 : if( FD_UNLIKELY( parent_slot<hard_forks[ i ].slot && hard_forks[ i ].slot<=slot ) ) sum += hard_forks[ i ].cnt;
197 0 : }
198 :
199 611 : if( FD_UNLIKELY( !sum ) ) return;
200 :
201 0 : ulong sum_le[ 1 ];
202 0 : FD_STORE( ulong, sum_le, sum );
203 :
204 0 : fd_sha256_t sha;
205 0 : fd_sha256_init( &sha );
206 0 : fd_sha256_append( &sha, hash->hash, sizeof(fd_hash_t) );
207 0 : fd_sha256_append( &sha, sum_le, sizeof(ulong) );
208 0 : fd_sha256_fini( &sha, hash->hash );
209 0 : }
|