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