Line data Source code
1 : #include "fd_footer_cache.h"
2 :
3 : ulong
4 0 : fd_footer_cache_align( void ) {
5 0 : return FD_FOOTER_CACHE_ALIGN;
6 0 : }
7 :
8 : ulong
9 0 : fd_footer_cache_footprint( void ) {
10 0 : return sizeof(fd_footer_cache_t);
11 0 : }
12 :
13 : void *
14 0 : fd_footer_cache_new( void * shmem ) {
15 0 : if( FD_UNLIKELY( !shmem ) ) {
16 0 : FD_LOG_WARNING(( "NULL shmem" ));
17 0 : return NULL;
18 0 : }
19 :
20 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_footer_cache_align() ) ) ) {
21 0 : FD_LOG_WARNING(( "misaligned shmem" ));
22 0 : return NULL;
23 0 : }
24 :
25 0 : fd_footer_cache_t * cache = (fd_footer_cache_t *)shmem;
26 0 : fd_memset( cache, 0, sizeof(fd_footer_cache_t) );
27 :
28 0 : FD_COMPILER_MFENCE();
29 0 : FD_VOLATILE( cache->magic ) = FD_FOOTER_CACHE_MAGIC;
30 0 : FD_COMPILER_MFENCE();
31 :
32 0 : return shmem;
33 0 : }
34 :
35 : fd_footer_cache_t *
36 0 : fd_footer_cache_join( void * shcache ) {
37 0 : if( FD_UNLIKELY( !shcache ) ) {
38 0 : FD_LOG_WARNING(( "NULL shcache" ));
39 0 : return NULL;
40 0 : }
41 :
42 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shcache, fd_footer_cache_align() ) ) ) {
43 0 : FD_LOG_WARNING(( "misaligned shcache" ));
44 0 : return NULL;
45 0 : }
46 :
47 0 : fd_footer_cache_t * cache = (fd_footer_cache_t *)shcache;
48 :
49 0 : if( FD_UNLIKELY( cache->magic!=FD_FOOTER_CACHE_MAGIC ) ) {
50 0 : FD_LOG_WARNING(( "bad magic" ));
51 0 : return NULL;
52 0 : }
53 :
54 0 : return cache;
55 0 : }
56 :
57 : void
58 : fd_footer_cache_hash_msg( fd_footer_cache_t * cache,
59 : uchar const * msg,
60 : ulong msg_sz,
61 0 : blst_p2_affine * h ) {
62 0 : blst_p2_affine const * hit = fd_footer_cache_hash_query( cache, msg, msg_sz );
63 0 : if( hit ) { *h = *hit; return; }
64 0 : fd_bls_hash_msg( msg, msg_sz, h );
65 0 : fd_footer_cache_hash_insert( cache, msg, msg_sz, h );
66 0 : }
67 :
68 : void
69 : fd_footer_cache_hash_insert( fd_footer_cache_t * cache,
70 : uchar const * msg,
71 : ulong msg_sz,
72 0 : blst_p2_affine const * h ) {
73 0 : FD_TEST( msg_sz && msg_sz<=FD_FOOTER_CACHE_MSG_MAX );
74 0 : ulong i = cache->next++ % FD_FOOTER_CACHE_MSG_CNT;
75 0 : cache->e[ i ].sz = msg_sz;
76 0 : memcpy( cache->e[ i ].msg, msg, msg_sz );
77 0 : cache->e[ i ].h = *h;
78 0 : }
79 :
80 : fd_lthash_value_t const *
81 : fd_footer_cache_memo_query( fd_footer_cache_memo_t const * memo,
82 : uchar const pubkey[ static FD_HASH_FOOTPRINT ],
83 : uchar const owner[ static FD_HASH_FOOTPRINT ],
84 : ulong lamports,
85 : int executable,
86 : uchar const * data,
87 0 : ulong data_len ) {
88 0 : int hit = lamports && memo->lamports==lamports && memo->data_len==data_len && memo->executable==!!executable &&
89 0 : fd_memeq( memo->pubkey, pubkey, 32UL ) && fd_memeq( memo->owner, owner, 32UL ) &&
90 0 : fd_memeq( memo->data, data, data_len );
91 0 : return hit ? &memo->lthash : NULL;
92 0 : }
93 :
94 : int
95 : fd_footer_cache_memo_insert( fd_footer_cache_memo_t * memo,
96 : uchar const pubkey[ static FD_HASH_FOOTPRINT ],
97 : uchar const owner[ static FD_HASH_FOOTPRINT ],
98 : ulong lamports,
99 : int executable,
100 : uchar const * data,
101 0 : ulong data_len ) {
102 0 : if( FD_UNLIKELY( !lamports || data_len>FD_FOOTER_CACHE_MEMO_DATA_MAX ) ) {
103 0 : memo->lamports = 0UL;
104 0 : return 0;
105 0 : }
106 0 : fd_memcpy( memo->pubkey, pubkey, 32UL );
107 0 : fd_memcpy( memo->owner, owner, 32UL );
108 0 : memo->lamports = lamports;
109 0 : memo->data_len = data_len;
110 0 : memo->executable = !!executable;
111 0 : fd_memcpy( memo->data, data, data_len );
112 0 : return 1;
113 0 : }
114 :
115 : blst_p2_affine const *
116 : fd_footer_cache_hash_query( fd_footer_cache_t const * cache,
117 : uchar const * msg,
118 0 : ulong msg_sz ) {
119 0 : FD_TEST( msg_sz && msg_sz<=FD_FOOTER_CACHE_MSG_MAX );
120 0 : for( ulong i=0UL; i<FD_FOOTER_CACHE_MSG_CNT; i++ ) {
121 0 : if( cache->e[ i ].sz==msg_sz && !memcmp( cache->e[ i ].msg, msg, msg_sz ) ) return &cache->e[ i ].h;
122 0 : }
123 0 : return NULL;
124 0 : }
|