Line data Source code
1 : #include "fd_hashes.h"
2 : #include "fd_bank.h"
3 : #include "../capture/fd_capture_ctx.h"
4 :
5 : void
6 : fd_hashes_account_lthash_simple( uchar const pubkey[ static FD_HASH_FOOTPRINT ],
7 : uchar const owner[ static FD_HASH_FOOTPRINT ],
8 : ulong lamports,
9 : int executable,
10 : uchar const * data,
11 : ulong data_len,
12 27039 : fd_lthash_value_t * lthash_out ) {
13 27039 : fd_lthash_zero( lthash_out );
14 27039 : if( FD_UNLIKELY( !lamports ) ) return;
15 :
16 26535 : uchar executable_flag = !!executable;
17 :
18 26535 : fd_blake3_t b3[1];
19 26535 : fd_blake3_init( b3 );
20 26535 : fd_blake3_append( b3, &lamports, sizeof( ulong ) );
21 26535 : fd_blake3_append( b3, data, data_len );
22 26535 : fd_blake3_append( b3, &executable_flag, sizeof( uchar ) );
23 26535 : fd_blake3_append( b3, owner, FD_HASH_FOOTPRINT );
24 26535 : fd_blake3_append( b3, pubkey, FD_HASH_FOOTPRINT );
25 26535 : fd_blake3_fini_2048( b3, lthash_out->bytes );
26 26535 : }
27 :
28 : void
29 : fd_hashes_hash_bank( fd_lthash_value_t const * lthash,
30 : fd_hash_t const * prev_bank_hash,
31 : fd_hash_t const * last_blockhash,
32 : ulong signature_count,
33 342 : fd_hash_t * hash_out ) {
34 :
35 : /* The bank hash for a slot is a sha256 of two sub-hashes:
36 : sha256(
37 : sha256( previous bank hash, signature count, last PoH blockhash ),
38 : lthash of the accounts modified in this slot
39 : )
40 : */
41 342 : fd_sha256_t sha;
42 342 : fd_sha256_init( &sha );
43 342 : fd_sha256_append( &sha, prev_bank_hash, sizeof( fd_hash_t ) );
44 342 : fd_sha256_append( &sha, (uchar const *) &signature_count, sizeof( ulong ) );
45 342 : fd_sha256_append( &sha, (uchar const *) last_blockhash, sizeof( fd_hash_t ) );
46 342 : fd_sha256_fini( &sha, hash_out->hash );
47 :
48 342 : fd_sha256_init( &sha );
49 342 : fd_sha256_append( &sha, (uchar const *) hash_out->hash, sizeof(fd_hash_t) );
50 342 : fd_sha256_append( &sha, (uchar const *) lthash->bytes, sizeof(fd_lthash_value_t) );
51 342 : fd_sha256_fini( &sha, hash_out->hash );
52 342 : }
53 :
54 : void
55 : fd_hashes_update_simple( fd_lthash_value_t * lthash_post, /* out */
56 : fd_lthash_value_t const * lthash_prev, /* in */
57 : uchar const pubkey[ static FD_HASH_FOOTPRINT ],
58 : uchar const owner[ static FD_HASH_FOOTPRINT ],
59 : ulong lamports,
60 : int executable,
61 : uchar const * data,
62 : ulong data_len,
63 : fd_bank_t * bank,
64 6372 : fd_capture_ctx_t * capture_ctx ) {
65 : /* Compute the new hash of the account */
66 6372 : fd_hashes_account_lthash_simple( pubkey, owner, lamports, executable, data, data_len, lthash_post );
67 :
68 : /* Subtract the old hash of the account from the bank lthash */
69 6372 : fd_lthash_value_t * bank_lthash = fd_bank_lthash_locking_modify( bank );
70 6372 : fd_lthash_sub( bank_lthash, lthash_prev );
71 :
72 : /* Add the new hash of the account to the bank lthash */
73 6372 : fd_lthash_add( bank_lthash, lthash_post );
74 :
75 6372 : fd_bank_lthash_end_locking_modify( bank );
76 :
77 6372 : fd_hashes_capture_account( pubkey, owner, lamports, executable, data, data_len, bank, capture_ctx );
78 6372 : }
79 :
80 : void
81 : fd_hashes_capture_account( uchar const pubkey[ static FD_HASH_FOOTPRINT ],
82 : uchar const owner[ static FD_HASH_FOOTPRINT ],
83 : ulong lamports,
84 : int executable,
85 : uchar const * data,
86 : ulong data_len,
87 : fd_bank_t * bank,
88 6567 : fd_capture_ctx_t * capture_ctx ) {
89 6567 : if( FD_LIKELY( !capture_ctx ||
90 6567 : !capture_ctx->capture_solcap ||
91 6567 : bank->f.slot<capture_ctx->solcap_start_slot ) ) return;
92 :
93 0 : fd_solana_account_meta_t solana_meta[1];
94 0 : fd_solana_account_meta_init( solana_meta, lamports, owner, executable );
95 0 : fd_capture_link_write_account_update(
96 0 : capture_ctx,
97 0 : capture_ctx->current_txn_idx,
98 0 : (fd_pubkey_t const *)pubkey,
99 0 : solana_meta,
100 0 : bank->f.slot,
101 0 : data,
102 0 : data_len );
103 0 : }
104 :
105 : void
106 : fd_hashes_apply_hard_forks( fd_hash_t * hash,
107 : ulong slot,
108 : ulong parent_slot,
109 : fd_hard_fork_t const * hard_forks,
110 15 : ulong hard_fork_cnt ) {
111 15 : ulong sum = 0UL;
112 39 : for( ulong i=0UL; i<hard_fork_cnt; i++ ) {
113 24 : if( FD_UNLIKELY( parent_slot<hard_forks[ i ].slot && hard_forks[ i ].slot<=slot ) ) sum += hard_forks[ i ].cnt;
114 24 : }
115 :
116 15 : if( FD_UNLIKELY( !sum ) ) return;
117 :
118 9 : ulong sum_le[ 1 ];
119 9 : FD_STORE( ulong, sum_le, sum );
120 :
121 9 : fd_sha256_t sha;
122 9 : fd_sha256_init( &sha );
123 9 : fd_sha256_append( &sha, hash->hash, sizeof(fd_hash_t) );
124 9 : fd_sha256_append( &sha, sum_le, sizeof(ulong) );
125 9 : fd_sha256_fini( &sha, hash->hash );
126 9 : }
|