Line data Source code
1 : #include "fd_hashes.h"
2 : #include "fd_bank.h"
3 : #include "../capture/fd_capture_ctx.h"
4 : #include "../capture/fd_solcap_writer.h"
5 : #include "../../ballet/blake3/fd_blake3.h"
6 : #include "../../ballet/lthash/fd_lthash.h"
7 : #include "../../ballet/sha256/fd_sha256.h"
8 :
9 : void
10 : fd_hashes_account_lthash( fd_pubkey_t const * pubkey,
11 : fd_account_meta_t const * account,
12 : uchar const * data,
13 1404 : fd_lthash_value_t * lthash_out ) {
14 1404 : fd_hashes_account_lthash_simple( pubkey->uc,
15 1404 : account->owner,
16 1404 : account->lamports,
17 1404 : account->executable,
18 1404 : data,
19 1404 : account->dlen,
20 1404 : lthash_out );
21 1404 : }
22 :
23 : void
24 : fd_hashes_account_lthash_simple( uchar const pubkey[ static FD_HASH_FOOTPRINT ],
25 : uchar const owner[ static FD_HASH_FOOTPRINT ],
26 : ulong lamports,
27 : uchar executable,
28 : uchar const * data,
29 : ulong data_len,
30 1404 : fd_lthash_value_t * lthash_out ) {
31 1404 : fd_lthash_zero( lthash_out );
32 :
33 : /* Accounts with zero lamports are not included in the hash, so they should always be treated as zero */
34 1404 : if( FD_UNLIKELY( lamports == 0 ) ) {
35 141 : return;
36 141 : }
37 :
38 1263 : uchar executable_flag = executable & 0x1;
39 :
40 1263 : fd_blake3_t b3[1];
41 1263 : fd_blake3_init( b3 );
42 1263 : fd_blake3_append( b3, &lamports, sizeof( ulong ) );
43 1263 : fd_blake3_append( b3, data, data_len );
44 1263 : fd_blake3_append( b3, &executable_flag, sizeof( uchar ) );
45 1263 : fd_blake3_append( b3, owner, FD_HASH_FOOTPRINT );
46 1263 : fd_blake3_append( b3, pubkey, FD_HASH_FOOTPRINT );
47 1263 : fd_blake3_fini_2048( b3, lthash_out->bytes );
48 1263 : }
49 :
50 : void
51 : fd_hashes_hash_bank( fd_lthash_value_t const * lthash,
52 : fd_hash_t const * prev_bank_hash,
53 : fd_hash_t const * last_blockhash,
54 : ulong signature_count,
55 0 : fd_hash_t * hash_out ) {
56 :
57 : /* The bank hash for a slot is a sha256 of two sub-hashes:
58 : sha256(
59 : sha256( previous bank hash, signature count, last PoH blockhash ),
60 : lthash of the accounts modified in this slot
61 : )
62 : */
63 0 : fd_sha256_t sha;
64 0 : fd_sha256_init( &sha );
65 0 : fd_sha256_append( &sha, prev_bank_hash, sizeof( fd_hash_t ) );
66 0 : fd_sha256_append( &sha, (uchar const *) &signature_count, sizeof( ulong ) );
67 0 : fd_sha256_append( &sha, (uchar const *) last_blockhash, sizeof( fd_hash_t ) );
68 0 : fd_sha256_fini( &sha, hash_out->hash );
69 :
70 0 : fd_sha256_init( &sha );
71 0 : fd_sha256_append( &sha, (uchar const *) hash_out->hash, sizeof(fd_hash_t) );
72 0 : fd_sha256_append( &sha, (uchar const *) lthash->bytes, sizeof(fd_lthash_value_t) );
73 0 : fd_sha256_fini( &sha, hash_out->hash );
74 0 : }
75 :
76 : void
77 : fd_hashes_update_lthash1( fd_lthash_value_t * lthash_post, /* out */
78 : fd_lthash_value_t const * lthash_prev, /* in */
79 : fd_pubkey_t const * pubkey,
80 : fd_account_meta_t const * meta,
81 : fd_bank_t * bank,
82 702 : fd_capture_ctx_t * capture_ctx ) {
83 :
84 : /* Hash the new version of the account */
85 702 : fd_hashes_account_lthash( pubkey, meta, fd_account_data( meta ), lthash_post );
86 :
87 : /* Subtract the old hash of the account from the bank lthash */
88 702 : fd_lthash_value_t * bank_lthash = fd_type_pun( fd_bank_lthash_locking_modify( bank ) );
89 702 : fd_lthash_sub( bank_lthash, lthash_prev );
90 :
91 : /* Add the new hash of the account to the bank lthash */
92 702 : fd_lthash_add( bank_lthash, lthash_post );
93 :
94 702 : fd_bank_lthash_end_locking_modify( bank );
95 :
96 702 : if( capture_ctx && capture_ctx->capture &&
97 702 : fd_bank_slot_get( bank )>=capture_ctx->solcap_start_slot ) {
98 0 : fd_solana_account_meta_t solana_meta[1];
99 0 : fd_solana_account_meta_init(
100 0 : solana_meta,
101 0 : meta->lamports,
102 0 : meta->owner,
103 0 : meta->executable
104 0 : );
105 0 : fd_capture_link_write_account_update(
106 0 : capture_ctx,
107 0 : capture_ctx->current_txn_idx,
108 0 : pubkey,
109 0 : solana_meta,
110 0 : fd_bank_slot_get( bank ),
111 0 : fd_account_data( meta ),
112 0 : meta->dlen );
113 0 : }
114 702 : }
|