Line data Source code
1 : #include "../fd_flamenco.h"
2 : #include "fd_solcap_proto.h"
3 : #include "fd_solcap_reader.h"
4 : #include "fd_solcap.pb.h"
5 : #include "../../ballet/base58/fd_base58.h"
6 : #include "../types/fd_types.h"
7 : #include "../types/fd_types_yaml.h"
8 : #include "../../ballet/nanopb/pb_decode.h"
9 :
10 : #include <errno.h>
11 : #include <stdio.h>
12 : #include <sys/stat.h> /* mkdir(2) */
13 : #include <fcntl.h> /* open(2) */
14 : #include <unistd.h> /* close(2) */
15 :
16 : /* TODO: Ugly -- These should not be hard coded! */
17 0 : #define SOLCAP_FILE_NAME_LEN (13UL)
18 0 : #define SOLCAP_SUFFIX_LEN (7UL) /* .solcap */
19 :
20 : #if FD_USING_GCC && __GNUC__ >= 15
21 : #pragma GCC diagnostic ignored "-Wunterminated-string-initialization"
22 : #endif
23 :
24 : static const uchar
25 : _vote_program_address[ 32 ] =
26 : "\x07\x61\x48\x1d\x35\x74\x74\xbb\x7c\x4d\x76\x24\xeb\xd3\xbd\xb3"
27 : "\xd8\x35\x5e\x73\xd1\x10\x43\xfc\x0d\xa3\x53\x80\x00\x00\x00\x00";
28 :
29 : static const uchar
30 : _stake_program_address[ 32 ] =
31 : "\x06\xa1\xd8\x17\x91\x37\x54\x2a\x98\x34\x37\xbd\xfe\x2a\x7a\xb2"
32 : "\x55\x7f\x53\x5c\x8a\x78\x72\x2b\x68\xa4\x9d\xc0\x00\x00\x00\x00";
33 :
34 : static void
35 0 : normalize_filename( const char * original_str, char * file_name, char prefix ) {
36 : /* We either need to truncate if too long or pad if too short (16 chars) */
37 :
38 0 : file_name[0] = prefix;
39 0 : ulong original_str_len = strlen( original_str ) - SOLCAP_SUFFIX_LEN + 1;
40 0 : if ( original_str_len <= SOLCAP_FILE_NAME_LEN ) {
41 0 : fd_memcpy( file_name + 1, original_str, original_str_len );
42 0 : for ( ulong i = original_str_len; i < SOLCAP_FILE_NAME_LEN; i++ ) {
43 0 : file_name[ i ] = ' ';
44 0 : }
45 0 : }
46 0 : else {
47 0 : ulong start_idx = original_str_len - SOLCAP_FILE_NAME_LEN;
48 0 : fd_memcpy( file_name + 1, original_str + start_idx, SOLCAP_FILE_NAME_LEN );
49 :
50 0 : }
51 0 : file_name[ SOLCAP_FILE_NAME_LEN ] = '\0';
52 0 : }
53 :
54 : /* Define routines for sorting the bank hash account delta accounts.
55 : The solcap format does not mandate accounts to be sorted. */
56 :
57 : static inline int
58 : fd_solcap_account_tbl_lt( fd_solcap_account_tbl_t const * a,
59 0 : fd_solcap_account_tbl_t const * b ) {
60 0 : return memcmp( a->key, b->key, 32UL ) < 0;
61 0 : }
62 : #define SORT_NAME sort_account_tbl
63 0 : #define SORT_KEY_T fd_solcap_account_tbl_t
64 0 : #define SORT_BEFORE(a,b) fd_solcap_account_tbl_lt( &(a), &(b) )
65 : #include "../../util/tmpl/fd_sort.c"
66 :
67 : /* TODO this differ is currently a separate file, but it would make
68 : sense to move/copy it to fd_ledger. Doing so would enable
69 : a fast feedback cycle wherein a developer supplies the expected
70 : (Labs) capture to fd_ledger, then automatically runs the
71 : differ after each execution. */
72 :
73 : struct fd_solcap_differ {
74 : fd_solcap_chunk_iter_t iter [2];
75 : fd_solcap_BankPreimage preimage[2];
76 :
77 : int verbose;
78 : int dump_dir_fd;
79 : char const * dump_dir;
80 : char const * file_paths[2];
81 : };
82 :
83 : typedef struct fd_solcap_differ fd_solcap_differ_t;
84 :
85 : struct fd_solcap_txn_differ {
86 : FILE * file[2];
87 : fd_solcap_chunk_iter_t iter[2];
88 : long chunk_gaddr[2];
89 : fd_solcap_Transaction transaction[2];
90 : uchar meta_buf[128][2];
91 : };
92 :
93 : typedef struct fd_solcap_txn_differ fd_solcap_txn_differ_t;
94 :
95 : static fd_solcap_differ_t *
96 : fd_solcap_differ_new( fd_solcap_differ_t * diff,
97 : FILE * streams[2],
98 0 : const char * cap_path[2] ) {
99 :
100 : /* Attach to capture files */
101 :
102 0 : for( ulong i=0UL; i<2UL; i++ ) {
103 0 : FILE * stream = streams[i];
104 :
105 : /* Set file names */
106 0 : diff->file_paths[i] = cap_path[i];
107 :
108 : /* Read file header */
109 0 : fd_solcap_fhdr_t hdr[1];
110 0 : if( FD_UNLIKELY( 1UL!=fread( hdr, sizeof(fd_solcap_fhdr_t), 1UL, stream ) ) ) {
111 0 : FD_LOG_WARNING(( "Failed to read file=%s header (%d-%s)",
112 0 : diff->file_paths[i], errno, strerror( errno ) ));
113 0 : return NULL;
114 0 : }
115 :
116 : /* Seek to first chunk */
117 0 : long skip = ( (long)hdr->chunk0_foff - (long)sizeof(fd_solcap_fhdr_t) );
118 0 : if( FD_UNLIKELY( 0!=fseek( stream, skip, SEEK_CUR ) ) ) {
119 0 : FD_LOG_WARNING(( "Failed to seek to first chunk (%d-%s)", errno, strerror( errno ) ));
120 0 : return NULL;
121 0 : }
122 :
123 0 : if( FD_UNLIKELY( !fd_solcap_chunk_iter_new( &diff->iter[i], stream ) ) )
124 0 : FD_LOG_CRIT(( "fd_solcap_chunk_iter_new() failed" ));
125 0 : }
126 :
127 0 : return diff;
128 0 : }
129 :
130 : /* fd_solcap_differ_advance seeks an iterator to the next bank hash.
131 : idx identifies the iterator. Returns 1 on success, 0 if end-of-file
132 : reached, and negated errno-like on failure. */
133 :
134 : static int
135 : fd_solcap_differ_advance( fd_solcap_differ_t * diff,
136 0 : ulong idx ) { /* [0,2) */
137 :
138 0 : fd_solcap_chunk_iter_t * iter = &diff->iter [ idx ];
139 0 : fd_solcap_BankPreimage * preimage = &diff->preimage[ idx ];
140 :
141 0 : long off = fd_solcap_chunk_iter_find( iter, FD_SOLCAP_V1_BANK_MAGIC );
142 0 : if( FD_UNLIKELY( off<0L ) )
143 0 : return fd_solcap_chunk_iter_err( iter );
144 :
145 0 : int err = fd_solcap_read_bank_preimage( iter->stream, iter->chunk_off, preimage, &iter->chunk );
146 0 : if( FD_UNLIKELY( err!=0 ) ) return -err;
147 0 : return 1;
148 0 : }
149 :
150 : /* fd_solcap_differ_sync synchronizes the given two iterators such that
151 : both point to the lowest common slot number. Returns 1 on success
152 : and 0 if no common slot was found. Negative values are negated
153 : errno-like. */
154 :
155 : static int
156 0 : fd_solcap_differ_sync( fd_solcap_differ_t * diff, ulong start_slot, ulong end_slot ) {
157 :
158 : /* Seek to first bank preimage object */
159 :
160 0 : for( ulong i=0UL; i<2UL; i++ ) {
161 0 : int res = fd_solcap_differ_advance( diff, i );
162 0 : if( FD_UNLIKELY( res!=1 ) ) return res;
163 0 : }
164 :
165 0 : ulong prev_slot0 = diff->preimage[ 0 ].slot;
166 0 : ulong prev_slot1 = diff->preimage[ 1 ].slot;
167 :
168 0 : for(;;) {
169 0 : ulong slot0 = diff->preimage[ 0 ].slot;
170 0 : ulong slot1 = diff->preimage[ 1 ].slot;
171 :
172 : /* Handle cases where slot is skipped in one or the other */
173 0 : if ( FD_UNLIKELY( prev_slot0 < slot1 && slot0 > slot1 ) ) {
174 0 : FD_LOG_WARNING(("Slot range (%lu,%lu) skipped in file=%s\n",
175 0 : prev_slot0, slot0, diff->file_paths[0]));
176 0 : }
177 0 : else if ( FD_UNLIKELY( prev_slot1 < slot0 && slot1 > slot0 ) ) {
178 0 : FD_LOG_WARNING(("Slot range (%lu,%lu) skipped in file=%s\n",
179 0 : prev_slot1, slot1, diff->file_paths[1]));
180 0 : }
181 :
182 0 : if( slot0 == slot1 ) {
183 0 : if ( slot0 < start_slot ) {
184 0 : int res;
185 0 : res = fd_solcap_differ_advance( diff, 0 );
186 0 : if( FD_UNLIKELY( res <= 0 ) ) return res;
187 0 : res = fd_solcap_differ_advance( diff, 1 );
188 0 : if( FD_UNLIKELY( res <= 0 ) ) return res;
189 0 : }
190 0 : else if ( slot0 > end_slot ) {
191 0 : return 0;
192 0 : }
193 0 : else {
194 0 : return 1;
195 0 : }
196 0 : }
197 0 : else {
198 0 : ulong idx = slot0>slot1;
199 0 : int res = fd_solcap_differ_advance( diff, idx );
200 0 : if( FD_UNLIKELY( res<=0 ) ) return res;
201 0 : }
202 0 : prev_slot0 = slot0;
203 0 : prev_slot1 = slot1;
204 0 : }
205 :
206 0 : return 0;
207 0 : }
208 :
209 : static int
210 : fd_solcap_can_pretty_print( uchar const owner [ static 32 ],
211 0 : uchar const pubkey[ static 32 ] ) {
212 :
213 : /* TODO clean up */
214 0 : uchar _sysvar_clock[ 32 ];
215 0 : fd_base58_decode_32( "SysvarC1ock11111111111111111111111111111111", _sysvar_clock );
216 0 : uchar _sysvar_rent[ 32 ];
217 0 : fd_base58_decode_32( "SysvarRent111111111111111111111111111111111", _sysvar_rent );
218 0 : uchar _sysvar_epoch_rewards[ 32 ];
219 0 : fd_base58_decode_32( "SysvarEpochRewards1111111111111111111111111", _sysvar_epoch_rewards );
220 0 : uchar _sysvar_stake_history[ 32 ];
221 0 : fd_base58_decode_32( "SysvarStakeHistory1111111111111111111111111", _sysvar_stake_history );
222 :
223 0 : if( 0==memcmp( owner, _vote_program_address, 32UL ) )
224 0 : return 1;
225 0 : if( 0==memcmp( owner, _stake_program_address, 32UL ) )
226 0 : return 1;
227 :
228 0 : if( 0==memcmp( pubkey, _sysvar_clock, 32UL ) )
229 0 : return 1;
230 0 : if( 0==memcmp( pubkey, _sysvar_rent, 32UL ) )
231 0 : return 1;
232 0 : if( 0==memcmp( pubkey, _sysvar_epoch_rewards, 32UL ) )
233 0 : return 1;
234 0 : if( 0==memcmp( pubkey, _sysvar_stake_history, 32UL ) )
235 0 : return 1;
236 0 : return 0;
237 0 : }
238 :
239 : static int
240 : fd_solcap_account_pretty_print( uchar const pubkey[ static 32 ],
241 : uchar const owner[ static 32 ],
242 : uchar const * data,
243 : ulong data_sz,
244 0 : FILE * file ) {
245 :
246 0 : FD_SCRATCH_SCOPE_BEGIN {
247 :
248 0 : fd_flamenco_yaml_t * yaml =
249 0 : fd_flamenco_yaml_init( fd_flamenco_yaml_new(
250 0 : fd_scratch_alloc( fd_flamenco_yaml_align(), fd_flamenco_yaml_footprint() ) ),
251 0 : file );
252 0 : FD_TEST( yaml );
253 :
254 : /* TODO clean up */
255 0 : uchar _sysvar_clock[ 32 ];
256 0 : fd_base58_decode_32( "SysvarC1ock11111111111111111111111111111111", _sysvar_clock );
257 0 : uchar _sysvar_rent[ 32 ];
258 0 : fd_base58_decode_32( "SysvarRent111111111111111111111111111111111", _sysvar_rent );
259 0 : uchar _sysvar_epoch_rewards[ 32 ];
260 0 : fd_base58_decode_32( "SysvarEpochRewards1111111111111111111111111", _sysvar_epoch_rewards );
261 0 : uchar _sysvar_stake_history[ 32 ];
262 0 : fd_base58_decode_32( "SysvarStakeHistory1111111111111111111111111", _sysvar_stake_history );
263 :
264 0 : if( 0==memcmp( owner, _vote_program_address, 32UL ) ) {
265 0 : fd_vote_state_versioned_t * vote_state = fd_bincode_decode_scratch( vote_state_versioned, data, data_sz, NULL );
266 0 : if( FD_UNLIKELY( !vote_state ) ) return -ENOMEM;
267 0 : fd_vote_state_versioned_walk( yaml, vote_state, fd_flamenco_yaml_walk, NULL, 0U );
268 0 : } else if( 0==memcmp( owner, _stake_program_address, 32UL ) ) {
269 0 : fd_stake_state_v2_t * stake_state = fd_bincode_decode_scratch( stake_state_v2, data, data_sz, NULL );
270 0 : if( FD_UNLIKELY( !stake_state ) ) return -ENOMEM;
271 0 : fd_stake_state_v2_walk( yaml, stake_state, fd_flamenco_yaml_walk, NULL, 0U );
272 0 : } else if( 0==memcmp( pubkey, _sysvar_clock, 32UL ) ) {
273 0 : fd_sol_sysvar_clock_t * clock = fd_bincode_decode_scratch( sol_sysvar_clock, data, data_sz, NULL );
274 0 : if( FD_UNLIKELY( !clock ) ) return -ENOMEM;
275 0 : fd_sol_sysvar_clock_walk( yaml, clock, fd_flamenco_yaml_walk, NULL, 0U );
276 0 : } else if( 0==memcmp( pubkey, _sysvar_rent, 32UL ) ) {
277 0 : fd_rent_t * rent = fd_bincode_decode_scratch( rent, data, data_sz, NULL );
278 0 : if( FD_UNLIKELY( !rent ) ) return -ENOMEM;
279 0 : fd_rent_walk( yaml, rent, fd_flamenco_yaml_walk, NULL, 0U );
280 0 : } else if( 0==memcmp( pubkey, _sysvar_epoch_rewards, 32UL ) ) {
281 0 : fd_sysvar_epoch_rewards_t * epoch_rewards = fd_bincode_decode_scratch( sysvar_epoch_rewards, data, data_sz, NULL );
282 0 : if( FD_UNLIKELY( !epoch_rewards ) ) return -ENOMEM;
283 0 : fd_sysvar_epoch_rewards_walk( yaml, epoch_rewards, fd_flamenco_yaml_walk, NULL, 0U );
284 0 : } else if( 0==memcmp( pubkey, _sysvar_stake_history, 32UL ) ) {
285 0 : fd_stake_history_t * stake_history = fd_bincode_decode_scratch( stake_history, data, data_sz, NULL );
286 0 : if( FD_UNLIKELY( !stake_history ) ) return -ENOMEM;
287 0 : fd_stake_history_walk( yaml, stake_history, fd_flamenco_yaml_walk, NULL, 0U );
288 0 : }
289 :
290 0 : int err = ferror( file );
291 0 : if( FD_UNLIKELY( err!=0 ) ) return err;
292 :
293 : /* No need to destroy structures, using fd_scratch allocator */
294 :
295 0 : fd_flamenco_yaml_delete( yaml );
296 0 : return 0;
297 0 : } FD_SCRATCH_SCOPE_END;
298 0 : }
299 :
300 : /* fd_solcap_dump_account_data writes a binary file containing exactly
301 : the given account's content. */
302 :
303 : static void
304 : fd_solcap_dump_account_data( fd_solcap_differ_t * diff,
305 : fd_solcap_AccountMeta const * meta,
306 : fd_solcap_account_tbl_t const * entry,
307 0 : void const * acc_data ) {
308 : /* Create dump file */
309 0 : char path[ FD_BASE58_ENCODED_32_LEN+1+FD_BASE58_ENCODED_32_LEN+4+1 ];
310 0 : int res = snprintf( path, sizeof(path), "%s-%s.bin", FD_BASE58_ENC_32_ALLOCA( entry->key ), FD_BASE58_ENC_32_ALLOCA( entry->hash ) );
311 0 : FD_TEST( (res>0) & (res<(int)sizeof(path)) );
312 0 : int fd = openat( diff->dump_dir_fd, path, O_CREAT|O_WRONLY|O_TRUNC, 0666 );
313 0 : if( FD_UNLIKELY( fd<0 ) )
314 0 : FD_LOG_ERR(( "openat(%d,%s) failed (%d-%s)",
315 0 : diff->dump_dir_fd, path, errno, strerror( errno ) ));
316 :
317 : /* Write dump file */
318 0 : FILE * file = fdopen( fd, "wb" );
319 0 : FD_TEST( meta->data_sz == fwrite( acc_data, 1UL, meta->data_sz, file ) );
320 0 : fclose( file ); /* Closes fd */
321 0 : }
322 :
323 : static void
324 : fd_solcap_diff_account_data( fd_solcap_differ_t * diff,
325 : fd_solcap_AccountMeta const meta [ static 2 ],
326 : fd_solcap_account_tbl_t const * const entry [ static 2 ],
327 0 : ulong const data_goff[ static 2 ] ) {\
328 0 :
329 : /* Streaming diff */
330 0 : int data_eq = meta[0].data_sz == meta[1].data_sz;
331 0 : if( data_eq ) {
332 0 : for( ulong i=0UL; i<2UL; i++ ) {
333 0 : if ( data_goff[i] == ULONG_MAX ) {
334 0 : continue;
335 0 : }
336 0 : FD_TEST( 0==fseek( diff->iter[ i ].stream, (long)data_goff[i], SEEK_SET ) );
337 0 : }
338 :
339 0 : for( ulong off=0UL; off<meta[0].data_sz; ) {
340 0 : # define BUFSZ (512UL)
341 :
342 : /* Read chunks */
343 0 : uchar buf[2][ BUFSZ ];
344 0 : ulong sz = fd_ulong_min( BUFSZ, meta[0].data_sz-off );
345 0 : for( ulong i=0UL; i<2UL; i++ )
346 0 : FD_TEST( sz==fread( &buf[i], 1UL, sz, diff->iter[i].stream ) );
347 :
348 : /* Compare chunks */
349 0 : data_eq = 0==memcmp( buf[0], buf[1], sz );
350 0 : if( !data_eq ) break;
351 :
352 0 : off += sz;
353 0 : # undef BUFSZ
354 0 : }
355 0 : }
356 0 : if( data_eq ) return;
357 :
358 : /* Dump account data to file */
359 0 : if( diff->verbose >= 4 ) {
360 :
361 : /* TODO: Remove hardcoded account size check */
362 0 : FD_TEST( meta[0].data_sz <= 1048576 );
363 0 : FD_TEST( meta[1].data_sz <= 1048576 );
364 :
365 0 : FD_SCRATCH_SCOPE_BEGIN {
366 0 : void * acc_data[2];
367 0 : acc_data[0] = fd_scratch_alloc( 1UL, meta[0].data_sz );
368 0 : acc_data[1] = fd_scratch_alloc( 1UL, meta[1].data_sz );
369 0 : fd_memset( acc_data[0], 0, meta[0].data_sz );
370 0 : fd_memset( acc_data[1], 0, meta[1].data_sz );
371 :
372 0 : for( ulong i=0UL; i<2UL; i++ ) {
373 0 : if ( data_goff[i] == ULONG_MAX ) {
374 0 : continue;
375 0 : }
376 :
377 : /* Rewind capture stream */
378 0 : FD_TEST( 0==fseek( diff->iter[ i ].stream, (long)data_goff[i], SEEK_SET ) );
379 :
380 : /* Copy data */
381 0 : FD_TEST( meta[i].data_sz == fread( acc_data[i], 1UL, meta[i].data_sz, diff->iter[i].stream ) );
382 0 : }
383 :
384 0 : for( ulong i=0; i<2; i++ ) {
385 0 : fd_solcap_dump_account_data( diff, meta+i, entry[i], acc_data[i] );
386 0 : }
387 :
388 : /* Inform user */
389 0 : printf( " (%s) data: %s/%s-%s.bin\n"
390 0 : " (%s) data: %s/%s-%s.bin\n"
391 0 : " vimdiff <(xxd '%s/%s-%s.bin') <(xxd '%s/%s-%s.bin')\n",
392 0 : diff->file_paths[0], diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[0]->key ), FD_BASE58_ENC_32_ALLOCA( entry[0]->hash ),
393 0 : diff->file_paths[1], diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[1]->key ), FD_BASE58_ENC_32_ALLOCA( entry[1]->hash ),
394 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[0]->key ), FD_BASE58_ENC_32_ALLOCA( entry[0]->hash ),
395 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[1]->key ), FD_BASE58_ENC_32_ALLOCA( entry[1]->hash ) );
396 :
397 0 : if( fd_solcap_can_pretty_print( meta[0].owner, entry[0]->key )
398 0 : & fd_solcap_can_pretty_print( meta[1].owner, entry[1]->key ) ) {
399 :
400 0 : for( ulong i=0UL; i<2UL; i++ ) {
401 : /* Create YAML file */
402 0 : char path[ FD_BASE58_ENCODED_32_LEN+1+FD_BASE58_ENCODED_32_LEN+4+1 ];
403 0 : int res = snprintf( path, sizeof(path), "%s-%s.yml", FD_BASE58_ENC_32_ALLOCA( entry[i]->key ), FD_BASE58_ENC_32_ALLOCA( entry[i]->hash) );
404 0 : FD_TEST( (res>0) & (res<(int)sizeof(path)) );
405 0 : int fd = openat( diff->dump_dir_fd, path, O_CREAT|O_WRONLY|O_TRUNC, 0666 );
406 0 : if( FD_UNLIKELY( fd<0 ) )
407 0 : FD_LOG_ERR(( "openat(%d,%s) failed (%d-%s)",
408 0 : diff->dump_dir_fd, path, errno, strerror( errno ) ));
409 :
410 : /* Write YAML file */
411 0 : FILE * file = fdopen( fd, "wb" );
412 0 : fd_solcap_account_pretty_print( entry[i]->key, meta[i].owner, acc_data[i], meta[i].data_sz, file );
413 0 : fclose( file ); /* closes fd */
414 0 : }
415 :
416 :
417 : /* Inform user */
418 0 : printf( " vimdiff '%s/%s-%s.yml' '%s/%s-%s.yml'\n",
419 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[0]->key ), FD_BASE58_ENC_32_ALLOCA( entry[0]->hash ),
420 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[1]->key ), FD_BASE58_ENC_32_ALLOCA( entry[1]->hash ) );
421 :
422 0 : }
423 0 : } FD_SCRATCH_SCOPE_END;
424 0 : }
425 0 : }
426 :
427 : /* fd_solcap_diff_account prints further details about a mismatch
428 : between two accounts. Preserves stream cursors. */
429 :
430 : static void
431 : fd_solcap_diff_account( fd_solcap_differ_t * diff,
432 : fd_solcap_account_tbl_t const * const entry [ static 2 ],
433 0 : ulong const acc_tbl_goff[ static 2 ] ) {
434 :
435 : /* Remember current file offsets (should probably just use readat) */
436 0 : long orig_off[ 2 ];
437 0 : for( ulong i=0UL; i<2UL; i++ ) {
438 0 : orig_off[ i ] = ftell( diff->iter[ i ].stream );
439 0 : if( FD_UNLIKELY( orig_off[ i ]<0L ) )
440 0 : FD_LOG_ERR(( "ftell failed (%d-%s)", errno, strerror( errno ) ));
441 0 : }
442 :
443 : /* Read account meta */
444 0 : fd_solcap_AccountMeta meta[2];
445 0 : ulong data_goff[2] = {ULONG_MAX, ULONG_MAX};
446 0 : for( ulong i=0UL; i<2UL; i++ ) {
447 0 : FILE * stream = diff->iter[ i ].stream;
448 0 : int err = fd_solcap_find_account( stream, meta+i, &data_goff[i], entry[i], acc_tbl_goff[i] );
449 0 : FD_TEST( err==0 );
450 0 : }
451 0 : if( 0!=memcmp( meta[0].owner, meta[1].owner, 32UL ) )
452 0 : printf( "%s owner: %s\n"
453 0 : "%s owner: %s\n",
454 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( meta[0].owner ),
455 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( meta[1].owner ) );
456 0 : else
457 0 : printf( " (both files ) owner: %s\n", FD_BASE58_ENC_32_ALLOCA( meta[0].owner ) );
458 : /* Even if the owner matches, still print it for convenience */
459 0 : if( meta[0].lamports != meta[1].lamports )
460 0 : printf( " (%s) lamports: %lu\n"
461 0 : " (%s) lamports: %lu\n",
462 0 : diff->file_paths[0], meta[0].lamports,
463 0 : diff->file_paths[1], meta[1].lamports );
464 0 : if( meta[0].data_sz != meta[1].data_sz )
465 0 : printf( " (%s) data_sz: %lu\n"
466 0 : " (%s) data_sz: %lu\n",
467 0 : diff->file_paths[0], meta[0].data_sz,
468 0 : diff->file_paths[1], meta[1].data_sz );
469 0 : if( meta[0].slot != meta[1].slot )
470 0 : printf( " (%s) slot: %lu\n"
471 0 : " (%s) slot: %lu\n",
472 0 : diff->file_paths[0], meta[0].slot,
473 0 : diff->file_paths[1], meta[1].slot );
474 0 : if( meta[0].rent_epoch != meta[1].rent_epoch )
475 0 : printf( " (%s) rent_epoch: %lu\n"
476 0 : " (%s) rent_epoch: %lu\n",
477 0 : diff->file_paths[0], meta[0].rent_epoch,
478 0 : diff->file_paths[1], meta[1].rent_epoch );
479 0 : if( meta[0].executable != meta[1].executable )
480 0 : printf( " (%s) executable: %d\n"
481 0 : " (%s) executable: %d\n",
482 0 : diff->file_paths[0], meta[0].executable,
483 0 : diff->file_paths[1], meta[1].executable );
484 0 : if( ( (meta[0].data_sz != 0UL) | fd_solcap_includes_account_data( &meta[0] ) )
485 0 : | ( (meta[1].data_sz != 0UL) | fd_solcap_includes_account_data( &meta[1] ) ) )
486 0 : fd_solcap_diff_account_data( diff, meta, entry, data_goff );
487 :
488 : /* Restore file offsets */
489 0 : for( ulong i=0UL; i<2UL; i++ ) {
490 0 : if( FD_UNLIKELY( 0!=fseek( diff->iter[ i ].stream, orig_off[ i ], SEEK_SET ) ) )
491 0 : FD_LOG_ERR(( "fseek failed (%d-%s)", errno, strerror( errno ) ));
492 0 : }
493 0 : }
494 :
495 : /* fd_solcap_diff_missing_account is like fd_solcap_diff_account but in
496 : the case that either side of the account is missing entirely. */
497 :
498 : static void
499 : fd_solcap_diff_missing_account( fd_solcap_differ_t * diff,
500 : fd_solcap_account_tbl_t const * const entry,
501 : ulong const acc_tbl_goff,
502 0 : FILE * stream ) {
503 :
504 : /* Remember current file offset */
505 0 : long orig_off = ftell( stream );
506 0 : if( FD_UNLIKELY( orig_off<0L ) )
507 0 : FD_LOG_ERR(( "ftell failed (%d-%s)", errno, strerror( errno ) ));
508 :
509 : /* Read account meta */
510 0 : fd_solcap_AccountMeta meta[1];
511 0 : ulong data_goff[1];
512 0 : int err = fd_solcap_find_account( stream, meta, data_goff, entry, acc_tbl_goff );
513 0 : FD_TEST( err==0 );
514 :
515 0 : printf(
516 0 : " lamports: %lu\n"
517 0 : " data_sz: %lu\n"
518 0 : " owner: %s\n"
519 0 : " slot: %lu\n"
520 0 : " rent_epoch: %lu\n"
521 0 : " executable: %d\n",
522 0 : meta->lamports,
523 0 : meta->data_sz,
524 0 : FD_BASE58_ENC_32_ALLOCA( meta->owner ),
525 0 : meta->slot,
526 0 : meta->rent_epoch,
527 0 : meta->executable
528 0 : );
529 :
530 : /* Dump account data to file */
531 0 : if( diff->verbose >= 4 ) {
532 :
533 : /* TODO: Remove hardcoded account size check */
534 0 : FD_TEST( meta->data_sz <= 8388608 );
535 :
536 0 : FD_SCRATCH_SCOPE_BEGIN {
537 0 : void * acc_data = fd_scratch_alloc( 1UL, meta->data_sz );
538 :
539 : /* Rewind capture stream */
540 0 : FD_TEST( 0==fseek(stream, (long)*data_goff, SEEK_SET ) );
541 :
542 : /* Copy data */
543 0 : FD_TEST( meta->data_sz == fread( acc_data, 1UL, meta->data_sz, stream ) );
544 :
545 0 : fd_solcap_dump_account_data( diff, meta, entry, acc_data );
546 :
547 : /* Inform user */
548 0 : printf( " data: %s/%s-%s.bin\n"
549 0 : " xxd '%s/%s-%s.bin'\n"
550 0 : " explorer: 'https://explorer.solana.com/block/%lu?accountFilter=%s&filter=all'",
551 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry->key ), FD_BASE58_ENC_32_ALLOCA( entry->hash ),
552 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry->key ), FD_BASE58_ENC_32_ALLOCA( entry->hash ),
553 0 : meta->slot, FD_BASE58_ENC_32_ALLOCA( entry->key ) );
554 :
555 0 : #pragma GCC diagnostic push
556 0 : #pragma GCC diagnostic ignored "-Wnonnull"
557 0 : if( fd_solcap_can_pretty_print( meta->owner, entry->key ) ) {
558 0 : #pragma GCC diagnostic pop
559 : /* Create YAML file */
560 0 : char path[ FD_BASE58_ENCODED_32_LEN+1+FD_BASE58_ENCODED_32_LEN+4+1 ];
561 0 : int res = snprintf( path, sizeof(path), "%s-%s.yml", FD_BASE58_ENC_32_ALLOCA( entry->key ), FD_BASE58_ENC_32_ALLOCA( entry->hash ) );
562 0 : FD_TEST( (res>0) & (res<(int)sizeof(path)) );
563 0 : int fd = openat( diff->dump_dir_fd, path, O_CREAT|O_WRONLY|O_TRUNC, 0666 );
564 0 : if( FD_UNLIKELY( fd<0 ) )
565 0 : FD_LOG_ERR(( "openat(%d,%s) failed (%d-%s)",
566 0 : diff->dump_dir_fd, path, errno, strerror( errno ) ));
567 :
568 : /* Write YAML file */
569 0 : FILE * file = fdopen( fd, "wb" );
570 0 : fd_solcap_account_pretty_print( entry->key, meta->owner, acc_data, meta->data_sz, file );
571 0 : fclose( file ); /* closes fd */
572 :
573 : /* Inform user */
574 0 : printf( " cat '%s/%s-%s.yml'\n",
575 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry->key ), FD_BASE58_ENC_32_ALLOCA( entry->hash ) );
576 0 : }
577 0 : } FD_SCRATCH_SCOPE_END;
578 0 : }
579 0 : }
580 :
581 : /* fd_solcap_diff_account_tbl detects and prints differences in the
582 : accounts that were hashed into the account delta hash. */
583 :
584 : static void
585 0 : fd_solcap_diff_account_tbl( fd_solcap_differ_t * diff ) {
586 :
587 : /* Read and sort tables */
588 :
589 0 : fd_solcap_account_tbl_t * tbl [2];
590 0 : fd_solcap_account_tbl_t * tbl_end[2];
591 0 : ulong chunk_goff[2];
592 0 : for( ulong i=0UL; i<2UL; i++ ) {
593 0 : if( diff->preimage[i].account_table_coff == 0L ) {
594 0 : FD_LOG_WARNING(( "Missing accounts table in capture" ));
595 0 : return;
596 0 : }
597 0 : chunk_goff[i] = (ulong)( (long)diff->iter[i].chunk_off + diff->preimage[i].account_table_coff );
598 :
599 : /* Read table meta and seek to table */
600 0 : FILE * stream = diff->iter[i].stream;
601 0 : fd_solcap_AccountTableMeta meta[1];
602 0 : int err = fd_solcap_find_account_table( stream, meta, chunk_goff[i] );
603 0 : FD_TEST( err==0 );
604 :
605 0 : if( FD_UNLIKELY( meta->account_table_cnt > INT_MAX ) ) {
606 0 : FD_LOG_WARNING(( "Too many accounts in capture" ));
607 0 : return;
608 0 : }
609 :
610 : /* Allocate table */
611 0 : ulong tbl_cnt = meta->account_table_cnt;
612 0 : ulong tbl_align = alignof(fd_solcap_account_tbl_t);
613 0 : ulong tbl_sz = tbl_cnt * sizeof(fd_solcap_account_tbl_t);
614 0 : FD_TEST( fd_scratch_alloc_is_safe( tbl_align, tbl_sz ) );
615 0 : tbl [i] = fd_scratch_alloc( tbl_align, tbl_sz );
616 0 : tbl_end[i] = tbl[i] + tbl_cnt;
617 :
618 : /* Read table */
619 0 : FD_TEST( tbl_cnt==fread( tbl[i], sizeof(fd_solcap_account_tbl_t), tbl_cnt, stream ) );
620 :
621 : /* Sort table */
622 0 : sort_account_tbl_inplace( tbl[i], tbl_cnt );
623 0 : }
624 :
625 : /* Walk tables in parallel */
626 :
627 0 : for(;;) {
628 0 : fd_solcap_account_tbl_t * a = tbl[0];
629 0 : fd_solcap_account_tbl_t * b = tbl[1];
630 :
631 0 : if( a==tbl_end[0] ) break;
632 0 : if( b==tbl_end[1] ) break;
633 :
634 0 : int key_cmp = memcmp( a->key, b->key, 32UL );
635 0 : if( key_cmp==0 ) {
636 0 : int hash_cmp = memcmp( a->hash, b->hash, 32UL );
637 0 : if( hash_cmp!=0 ) {
638 0 : printf( "\n (in both files) account: %s\n"
639 0 : " (%s) hash: %s\n"
640 0 : " (%s) hash: %s\n",
641 0 : FD_BASE58_ENC_32_ALLOCA( a->key ),
642 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( a->hash ),
643 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( b->hash ) );
644 :
645 0 : if( diff->verbose >= 3 )
646 0 : fd_solcap_diff_account( diff, (fd_solcap_account_tbl_t const * const *)tbl, chunk_goff );
647 0 : }
648 :
649 0 : tbl[0]++;
650 0 : tbl[1]++;
651 0 : continue;
652 0 : }
653 :
654 0 : if( key_cmp<0 ) {
655 0 : printf( "\n (%s) account: %s\n", diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( a->key ) );
656 0 : if( diff->verbose >= 3 )
657 0 : fd_solcap_diff_missing_account( diff, tbl[0], chunk_goff[0], diff->iter[0].stream );
658 0 : tbl[0]++;
659 0 : continue;
660 0 : }
661 :
662 0 : if( key_cmp>0 ) {
663 0 : printf( "\n (%s) account: %s\n", diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( b->key ) );
664 0 : if( diff->verbose >= 3 )
665 0 : fd_solcap_diff_missing_account( diff, tbl[1], chunk_goff[1], diff->iter[1].stream );
666 0 : tbl[1]++;
667 0 : continue;
668 0 : }
669 0 : }
670 0 : while( tbl[0]!=tbl_end[0] ) {
671 0 : printf( "\n (%s) account: %s\n", diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( tbl[0]->key ) );
672 0 : if( diff->verbose >= 3 )
673 0 : fd_solcap_diff_missing_account( diff, tbl[0], chunk_goff[0], diff->iter[0].stream );
674 0 : tbl[0]++;
675 0 : }
676 0 : while( tbl[1]!=tbl_end[1] ) {
677 0 : printf( "\n (%s) account: %s\n", diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( tbl[1]->key ) );
678 0 : if( diff->verbose >= 3 )
679 0 : fd_solcap_diff_missing_account( diff, tbl[1], chunk_goff[1], diff->iter[1].stream );
680 0 : tbl[1]++;
681 0 : }
682 :
683 0 : }
684 :
685 : /* fd_solcap_diff_bank detects bank hash mismatches and prints a
686 : human-readable description of the root cause to stdout. Returns 0
687 : if bank hashes match, 1 if a mismatch was detected. */
688 :
689 : static int
690 0 : fd_solcap_diff_bank( fd_solcap_differ_t * diff ) {
691 :
692 0 : fd_solcap_BankPreimage const * pre = diff->preimage;
693 :
694 0 : FD_TEST( pre[0].slot == pre[1].slot );
695 0 : if( 0==memcmp( &pre[0], &pre[1], sizeof(fd_solcap_BankPreimage) ) )
696 0 : return 0;
697 :
698 0 : printf( "\nbank hash mismatch at slot=%lu\n", pre[0].slot );
699 :
700 0 : printf( "(%s) bank_hash: %s\n"
701 0 : "(%s) bank_hash: %s\n",
702 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( pre[0].bank_hash ),
703 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( pre[1].bank_hash ) );
704 :
705 0 : int only_account_mismatch = 0;
706 0 : if( 0!=memcmp( pre[0].account_delta_hash, pre[1].account_delta_hash, 32UL ) ) {
707 0 : only_account_mismatch = 1;
708 0 : printf( "(%s) account_delta_hash: %s\n"
709 0 : "(%s) account_delta_hash: %s\n",
710 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( pre[0].account_delta_hash ),
711 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( pre[1].account_delta_hash ) );
712 0 : }
713 0 : if( 0!=memcmp( pre[0].accounts_lt_hash_checksum, pre[1].accounts_lt_hash_checksum, 32UL ) ) {
714 0 : only_account_mismatch = 1;
715 0 : printf( "(%s) accounts_lt_hash_checksum: %s\n"
716 0 : "(%s) accounts_lt_hash_checksum: %s\n",
717 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( pre[0].accounts_lt_hash_checksum ),
718 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( pre[1].accounts_lt_hash_checksum ) );
719 0 : }
720 0 : if( 0!=memcmp( pre[0].prev_bank_hash, pre[1].prev_bank_hash, 32UL ) ) {
721 0 : only_account_mismatch = 0;
722 0 : printf( "(%s) prev_bank_hash: %s\n"
723 0 : "(%s) prev_bank_hash: %s\n",
724 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( pre[0].prev_bank_hash ),
725 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( pre[1].prev_bank_hash ) );
726 0 : }
727 0 : if( 0!=memcmp( pre[0].poh_hash, pre[1].poh_hash, 32UL ) ) {
728 0 : only_account_mismatch = 0;
729 0 : printf( "(%s) poh_hash: %s\n"
730 0 : "(%s) poh_hash: %s\n",
731 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( pre[0].poh_hash ),
732 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( pre[1].poh_hash ) );
733 0 : }
734 0 : if( pre[0].signature_cnt != pre[1].signature_cnt ) {
735 0 : only_account_mismatch = 0;
736 0 : printf( "(%s) signature_cnt: %lu\n"
737 0 : "(%s) signature_cnt: %lu\n",
738 0 : diff->file_paths[0], pre[0].signature_cnt,
739 0 : diff->file_paths[1], pre[1].signature_cnt );
740 0 : }
741 0 : if( pre[0].account_cnt != pre[1].account_cnt ) {
742 0 : printf( "(%s) account_cnt: %lu\n"
743 0 : "(%s) account_cnt: %lu\n",
744 0 : diff->file_paths[0], pre[0].account_cnt,
745 0 : diff->file_paths[1], pre[1].account_cnt );
746 0 : }
747 0 : printf( "\n" );
748 :
749 0 : if( only_account_mismatch && diff->verbose >= 2 ) {
750 0 : fd_scratch_push();
751 0 : fd_solcap_diff_account_tbl( diff );
752 0 : fd_scratch_pop();
753 0 : }
754 :
755 0 : return 1;
756 0 : }
757 :
758 : /* Diffs two transaction results with each other. */
759 : static void
760 0 : fd_solcap_transaction_fd_diff( fd_solcap_txn_differ_t * txn_differ ) {
761 0 : if ( FD_UNLIKELY( memcmp( txn_differ->transaction[0].txn_sig,
762 0 : txn_differ->transaction[1].txn_sig, 32UL ) != 0 ) ) {
763 : /* Transactions don't line up. */
764 0 : FD_LOG_WARNING(( "Transaction signatures are different for slot=%lu, signature=(%s != %s)."
765 0 : "It is possible that either the transactions are out of order or some transactions are missing.",
766 0 : txn_differ->transaction[0].slot,
767 0 : FD_BASE58_ENC_32_ALLOCA( txn_differ->transaction[0].txn_sig ),
768 0 : FD_BASE58_ENC_32_ALLOCA( txn_differ->transaction[1].txn_sig ) ));
769 0 : }
770 0 : else {
771 0 : bool diff_txns = txn_differ->transaction[0].fd_txn_err != txn_differ->transaction[1].fd_txn_err;
772 0 : bool diff_cus = txn_differ->transaction[0].fd_cus_used != txn_differ->transaction[1].fd_cus_used;
773 0 : bool diff_instr_err_idx = txn_differ->transaction[0].instr_err_idx != txn_differ->transaction[1].instr_err_idx;
774 0 : if ( diff_txns || diff_cus ) {
775 0 : printf(
776 0 : "\nslot: %lu\n"
777 0 : "txn_sig: '%s'\n",
778 0 : txn_differ->transaction[0].slot,
779 0 : FD_BASE58_ENC_64_ALLOCA( txn_differ->transaction[0].txn_sig) );
780 0 : }
781 0 : if ( diff_txns ) {
782 0 : printf(
783 0 : " (+) txn_err: %d\n"
784 0 : " (-) txn_err: %d\n",
785 0 : txn_differ->transaction[0].fd_txn_err,
786 0 : txn_differ->transaction[1].fd_txn_err );
787 0 : }
788 0 : if ( diff_cus ) {
789 0 : printf(
790 0 : " (+) cus_used: %lu\n"
791 0 : " (-) cus_used: %lu\n",
792 0 : txn_differ->transaction[0].fd_cus_used,
793 0 : txn_differ->transaction[1].fd_cus_used );
794 0 : }
795 0 : if ( diff_instr_err_idx ) {
796 0 : printf(
797 0 : " (+) instr_err_idx: %d\n"
798 0 : " (-) instr_err_idx: %d\n",
799 0 : txn_differ->transaction[0].instr_err_idx,
800 0 : txn_differ->transaction[1].instr_err_idx );
801 0 : }
802 0 : }
803 0 : }
804 :
805 : /* Diffs firedancer transaction result with solana's result iff it is included
806 : in the solcap. The solana result comes from rocksdb. This diff is generated
807 : from just one fd_solcap_Transaction object */
808 : static void
809 : fd_solcap_transaction_solana_diff( fd_solcap_Transaction * transaction,
810 : ulong start_slot,
811 0 : ulong end_slot ) {
812 0 : if ( transaction->slot < start_slot || transaction->slot > end_slot ) {
813 0 : return;
814 0 : }
815 :
816 0 : if ( transaction->solana_txn_err == ULONG_MAX && transaction->solana_cus_used == ULONG_MAX ) {
817 : /* If solana_txn_err and solana_cus_used are both not populated, don't print diff */
818 0 : return;
819 0 : } else if ( transaction->solana_txn_err == ULONG_MAX ) {
820 : /* Print diff if the solana_txn_err is not set (txn executed successfully) */
821 0 : transaction->solana_txn_err = 0;
822 0 : }
823 :
824 : /* Only print a diff if cus or transaction result is different */
825 0 : if( !!( transaction->fd_txn_err ) != !!( transaction->solana_txn_err ) ||
826 0 : transaction->fd_cus_used != transaction->solana_cus_used ) {
827 0 : printf(
828 0 : "slot: %lu\n"
829 0 : "txn_sig: '%s'\n"
830 0 : " (+) txn_err: %d\n"
831 0 : " (-) solana_txn_err: %lu\n"
832 0 : " (+) cus_used: %lu\n"
833 0 : " (-) solana_cus_used: %lu\n"
834 0 : " instr_err_idx: %d\n"
835 0 : " explorer: 'https://explorer.solana.com/tx/%s'\n"
836 0 : " solscan: 'https://solscan.io/tx/%s'\n"
837 0 : " solanafm: 'https://solana.fm/tx/%s'\n",
838 0 : transaction->slot,
839 0 : FD_BASE58_ENC_64_ALLOCA( transaction->txn_sig ),
840 0 : transaction->fd_txn_err,
841 0 : transaction->solana_txn_err,
842 0 : transaction->fd_cus_used,
843 0 : transaction->solana_cus_used,
844 0 : transaction->instr_err_idx,
845 0 : FD_BASE58_ENC_64_ALLOCA( transaction->txn_sig ),
846 0 : FD_BASE58_ENC_64_ALLOCA( transaction->txn_sig ),
847 0 : FD_BASE58_ENC_64_ALLOCA( transaction->txn_sig ) );
848 0 : }
849 0 : }
850 :
851 : static void
852 0 : fd_solcap_get_transaction_from_iter( fd_solcap_txn_differ_t * differ, ulong idx ) {
853 0 : if ( fd_solcap_chunk_iter_done( &differ->iter[idx] ) )
854 0 : return;
855 :
856 0 : fd_solcap_chunk_t const * chunk = fd_solcap_chunk_iter_item( &differ->iter[idx] );
857 :
858 0 : if( FD_UNLIKELY( 0!=fseek( differ->file[idx], differ->chunk_gaddr[idx] + (long)chunk->meta_coff, SEEK_SET ) ) ) {
859 0 : FD_LOG_ERR(( "fseek transaction meta failed (%d-%s)", errno, strerror( errno ) ));
860 0 : }
861 0 : if( FD_UNLIKELY( chunk->meta_sz != fread( &differ->meta_buf[idx], 1UL, chunk->meta_sz, differ->file[idx] ) ) ) {
862 0 : FD_LOG_ERR(( "fread transaction meta failed (%d-%s)", errno, strerror( errno ) ));
863 0 : }
864 :
865 0 : pb_istream_t stream = pb_istream_from_buffer( differ->meta_buf[idx], chunk->meta_sz );
866 0 : if( FD_UNLIKELY( !pb_decode( &stream, fd_solcap_Transaction_fields, &differ->transaction[idx] ) ) ) {
867 0 : FD_LOG_HEXDUMP_DEBUG(( "transaction meta", differ->meta_buf[idx], chunk->meta_sz ));
868 0 : FD_LOG_ERR(( "pb_decode transaction meta failed (%s)", PB_GET_ERROR(&stream) ));
869 0 : }
870 0 : }
871 :
872 : static void
873 0 : fd_solcap_transaction_iter( fd_solcap_txn_differ_t * txn_differ, ulong idx ) {
874 0 : while ( !fd_solcap_chunk_iter_done( &txn_differ->iter[idx] ) ) {
875 0 : txn_differ->chunk_gaddr[idx] = fd_solcap_chunk_iter_find( &txn_differ->iter[idx], FD_SOLCAP_V1_TRXN_MAGIC );
876 0 : fd_solcap_get_transaction_from_iter( txn_differ, idx );
877 0 : fd_solcap_transaction_solana_diff( &txn_differ->transaction[idx], 0, ULONG_MAX );
878 0 : }
879 0 : }
880 :
881 : static void
882 0 : fd_solcap_txn_differ_advance( fd_solcap_txn_differ_t * txn_differ ) {
883 0 : while ( !fd_solcap_chunk_iter_done( &txn_differ->iter[0] ) &&
884 0 : !fd_solcap_chunk_iter_done( &txn_differ->iter[1] ) ) {
885 : /* Diff transactions against both solana result (rocksdb) and against each other */
886 0 : fd_solcap_transaction_fd_diff( txn_differ );
887 0 : fd_solcap_transaction_solana_diff( &txn_differ->transaction[0], 0, ULONG_MAX );
888 0 : txn_differ->chunk_gaddr[0] = fd_solcap_chunk_iter_find( &txn_differ->iter[0], FD_SOLCAP_V1_TRXN_MAGIC );
889 0 : txn_differ->chunk_gaddr[1] = fd_solcap_chunk_iter_find( &txn_differ->iter[1], FD_SOLCAP_V1_TRXN_MAGIC );
890 0 : fd_solcap_get_transaction_from_iter( txn_differ, 0 );
891 0 : fd_solcap_get_transaction_from_iter( txn_differ, 1 );
892 0 : }
893 0 : }
894 :
895 0 : static void fd_solcap_txn_differ_sync( fd_solcap_txn_differ_t * txn_differ ) {
896 : /* Find first transaction for both files */
897 0 : for( int i=0; i<2; i++ ) {
898 0 : txn_differ->chunk_gaddr[i] = fd_solcap_chunk_iter_find( &txn_differ->iter[i], FD_SOLCAP_V1_TRXN_MAGIC );
899 0 : if( FD_UNLIKELY( txn_differ->chunk_gaddr[i] < 0L ) ) {
900 0 : int err = fd_solcap_chunk_iter_err( &txn_differ->iter[i] );
901 0 : if( err == 0 ) break;
902 0 : FD_LOG_ERR(( "fd_solcap_chunk_iter_next() failed (%d-%s)", err, strerror( err ) ));
903 0 : }
904 0 : }
905 :
906 : /* Get first transaction on both */
907 0 : fd_solcap_get_transaction_from_iter( txn_differ, 0 );
908 0 : fd_solcap_get_transaction_from_iter( txn_differ, 1 );
909 :
910 0 : for(;;) {
911 : /* If one is done but not the other, iterate through the rest of the
912 : transactions in order to generate a diff against solana's transactions */
913 0 : if( fd_solcap_chunk_iter_done( &txn_differ->iter[0] ) ) {
914 0 : fd_solcap_transaction_iter( txn_differ, 1 );
915 0 : break;
916 0 : } else if( fd_solcap_chunk_iter_done( &txn_differ->iter[1] ) ) {
917 0 : fd_solcap_transaction_iter( txn_differ, 0 );
918 0 : break;
919 0 : }
920 :
921 : /* Otherwise, try to sync up the two files, printing any solana diffs along the way */
922 0 : if( txn_differ->transaction[0].slot == txn_differ->transaction[1].slot ) {
923 0 : fd_solcap_txn_differ_advance( txn_differ );
924 0 : } else if( txn_differ->transaction[0].slot < txn_differ->transaction[1].slot ) {
925 : /* Advance index 0 only */
926 0 : fd_solcap_transaction_solana_diff( &txn_differ->transaction[0], 0, ULONG_MAX );
927 0 : txn_differ->chunk_gaddr[0] = fd_solcap_chunk_iter_find( &txn_differ->iter[0], FD_SOLCAP_V1_TRXN_MAGIC );
928 0 : fd_solcap_get_transaction_from_iter( txn_differ, 0 );
929 0 : } else if( txn_differ->transaction[1].slot < txn_differ->transaction[0].slot ) {
930 : /* Advance index 1 only */
931 0 : fd_solcap_transaction_solana_diff( &txn_differ->transaction[1], 0, ULONG_MAX );
932 0 : txn_differ->chunk_gaddr[1] = fd_solcap_chunk_iter_find( &txn_differ->iter[1], FD_SOLCAP_V1_TRXN_MAGIC );
933 0 : fd_solcap_get_transaction_from_iter( txn_differ, 1 );
934 0 : }
935 0 : }
936 0 : }
937 :
938 0 : static void fd_solcap_transaction_diff( FILE * file_zero, FILE * file_one ) {
939 :
940 0 : if ( FD_UNLIKELY( fseek( file_zero, 0, SEEK_SET ) != 0 ) ) {
941 0 : FD_LOG_ERR(( "fseek to start of file failed (%d-%s)", errno, strerror( errno ) ));
942 0 : }
943 0 : if ( FD_UNLIKELY( fseek( file_one, 0, SEEK_SET ) != 0 ) ) {
944 0 : FD_LOG_ERR(( "fseek to start of file failed (%d-%s)", errno, strerror( errno ) ));
945 0 : }
946 :
947 : /* Read file header and seek to first chunk to begin interation */
948 0 : fd_solcap_fhdr_t fhdr_zero[1];
949 0 : fd_solcap_fhdr_t fhdr_one[1];
950 0 : ulong n_zero = fread( fhdr_zero, sizeof(fd_solcap_fhdr_t), 1UL, file_zero );
951 0 : ulong n_one = fread( fhdr_one, sizeof(fd_solcap_fhdr_t), 1UL, file_one );
952 :
953 0 : if ( FD_UNLIKELY( n_zero != 1UL ) ) {
954 0 : FD_LOG_ERR(( "fread file header failed (%d-%s)", errno, strerror( errno ) ));
955 0 : }
956 0 : if ( FD_UNLIKELY( n_one != 1UL ) ) {
957 0 : FD_LOG_ERR(( "fread file header failed (%d-%s)", errno, strerror( errno ) ));
958 0 : }
959 0 : int err;
960 0 : err = fseek( file_zero, (long)fhdr_zero->chunk0_foff - (long)sizeof(fd_solcap_fhdr_t), SEEK_CUR );
961 0 : if( FD_UNLIKELY( err < 0L ) ) {
962 0 : FD_LOG_ERR(( "fseek chunk0 failed (%d-%s)", errno, strerror( errno ) ));
963 0 : }
964 0 : err = fseek( file_one, (long)fhdr_one->chunk0_foff - (long)sizeof(fd_solcap_fhdr_t), SEEK_CUR );
965 0 : if( FD_UNLIKELY( err < 0L ) ) {
966 0 : FD_LOG_ERR(( "fseek chunk0 failed (%d-%s)", errno, strerror( errno ) ));
967 0 : }
968 :
969 : /* Setup txn_differ */
970 0 : fd_solcap_txn_differ_t txn_differ;
971 0 : txn_differ.file[0] = file_zero;
972 0 : txn_differ.file[1] = file_one;
973 0 : fd_solcap_chunk_iter_new( &txn_differ.iter[0], file_zero );
974 0 : fd_solcap_chunk_iter_new( &txn_differ.iter[1], file_one );
975 :
976 : /* Iterate and diff throught the transactions */
977 0 : fd_solcap_txn_differ_sync( &txn_differ );
978 0 : }
979 :
980 : void
981 0 : fd_solcap_one_file_transaction_diff( FILE * file, ulong start_slot, ulong end_slot ) {
982 : /* Open and read the header */
983 0 : fd_solcap_fhdr_t fhdr[1];
984 0 : ulong n = fread( fhdr, sizeof(fd_solcap_fhdr_t), 1UL, file );
985 0 : if( FD_UNLIKELY( n!=1UL ) ) {
986 0 : FD_LOG_ERR(( "fread file header failed (%d-%s)", errno, strerror( errno ) ));
987 0 : }
988 :
989 : /* Seek to the first chunk */
990 0 : int err = fseek( file, (long)fhdr->chunk0_foff - (long)sizeof(fd_solcap_fhdr_t), SEEK_CUR );
991 0 : if( FD_UNLIKELY( err<0L ) ) {
992 0 : FD_LOG_ERR(( "fseek chunk0 failed (%d-%s)", errno, strerror( errno ) ));
993 0 : }
994 :
995 : /* Iterate through the chunks diffing the trnasactions */
996 0 : fd_solcap_chunk_iter_t iter[1];
997 0 : fd_solcap_chunk_iter_new( iter, file );
998 0 : while( !fd_solcap_chunk_iter_done( iter ) ) {
999 0 : long chunk_gaddr = fd_solcap_chunk_iter_find( iter, FD_SOLCAP_V1_TRXN_MAGIC );
1000 0 : if( FD_UNLIKELY( chunk_gaddr<0L ) ) {
1001 0 : int err = fd_solcap_chunk_iter_err( iter );
1002 0 : if( err==0 ) break;
1003 0 : FD_LOG_ERR(( "fd_solcap_chunk_iter_next() failed (%d-%s)", err, strerror( err ) ));
1004 0 : }
1005 :
1006 0 : fd_solcap_chunk_t const * chunk = fd_solcap_chunk_iter_item( iter );
1007 0 : if( FD_UNLIKELY( !chunk ) ) FD_LOG_ERR(( "fd_solcap_chunk_item() failed" ));
1008 :
1009 : /* Read transaction meta */
1010 :
1011 0 : uchar meta_buf[ 128UL ];
1012 0 : if( FD_UNLIKELY( 0!=fseek( file, chunk_gaddr + (long)chunk->meta_coff, SEEK_SET ) ) ) {
1013 0 : FD_LOG_ERR(( "fseek transaction meta failed (%d-%s)", errno, strerror( errno ) ));
1014 0 : }
1015 0 : if( FD_UNLIKELY( chunk->meta_sz != fread( meta_buf, 1UL, chunk->meta_sz, file ) ) ) {
1016 0 : FD_LOG_ERR(( "fread transaction meta failed (%d-%s)", errno, strerror( errno ) ));
1017 0 : }
1018 :
1019 : /* Deserialize transaction meta */
1020 :
1021 0 : pb_istream_t stream = pb_istream_from_buffer( meta_buf, chunk->meta_sz );
1022 :
1023 0 : fd_solcap_Transaction meta;
1024 0 : if( FD_UNLIKELY( !pb_decode( &stream, fd_solcap_Transaction_fields, &meta ) ) ) {
1025 0 : FD_LOG_HEXDUMP_DEBUG(( "transaction meta", meta_buf, chunk->meta_sz ));
1026 0 : FD_LOG_ERR(( "pb_decode transaction meta failed (%s)", PB_GET_ERROR(&stream) ));
1027 0 : }
1028 :
1029 0 : if( meta.slot < start_slot || meta.slot > end_slot ) {
1030 0 : continue;
1031 0 : }
1032 :
1033 0 : fd_solcap_transaction_solana_diff( &meta, start_slot, end_slot );
1034 0 : }
1035 0 : }
1036 :
1037 : static void
1038 0 : usage( void ) {
1039 0 : fprintf( stderr,
1040 0 : "Usage: fd_solcap_diff [options] {FILE1} {FILE2}\n"
1041 0 : "\n"
1042 0 : "Imports a runtime capture file from JSON.\n"
1043 0 : "\n"
1044 0 : "Options:\n"
1045 0 : " --page-sz {gigantic|huge|normal} Page size\n"
1046 0 : " --page-cnt {count} Page count\n"
1047 0 : " --scratch-mb 1024 Scratch mem MiB\n"
1048 0 : " -v 1 Diff verbosity\n"
1049 0 : " --dump-dir {dir} Dump directory\n"
1050 0 : " --start-slot {slot} Start slot\n"
1051 0 : " --end-slot {slot} End slot\n"
1052 0 : "\n" );
1053 0 : }
1054 :
1055 : int
1056 : main( int argc,
1057 : char ** argv ) {
1058 : fd_boot( &argc, &argv );
1059 : fd_flamenco_boot( &argc, &argv );
1060 :
1061 : /* Command line handling */
1062 :
1063 : for( int i=1; i<argc; i++ ) {
1064 : if( 0==strcmp( argv[i], "--help" ) ) {
1065 : usage();
1066 : return 0;
1067 : }
1068 : }
1069 :
1070 : char const * _page_sz = fd_env_strip_cmdline_cstr ( &argc, &argv, "--page-sz", NULL, "gigantic" );
1071 : ulong page_cnt = fd_env_strip_cmdline_ulong( &argc, &argv, "--page-cnt", NULL, 2UL );
1072 : ulong scratch_mb = fd_env_strip_cmdline_ulong( &argc, &argv, "--scratch-mb", NULL, 1024UL );
1073 : int verbose = fd_env_strip_cmdline_int ( &argc, &argv, "-v", NULL, 1 );
1074 : char const * dump_dir = fd_env_strip_cmdline_cstr ( &argc, &argv, "--dump-dir", NULL, "dump" );
1075 : ulong start_slot = fd_env_strip_cmdline_ulong( &argc, &argv, "--start-slot", NULL, 0UL );
1076 : ulong end_slot = fd_env_strip_cmdline_ulong( &argc, &argv, "--end-slot", NULL, ULONG_MAX );
1077 :
1078 : ulong page_sz = fd_cstr_to_shmem_page_sz( _page_sz );
1079 : if( FD_UNLIKELY( !page_sz ) ) FD_LOG_ERR(( "unsupported --page-sz" ));
1080 :
1081 : char const * cap_path[2] = {0};
1082 : int caps_found = 0;
1083 :
1084 : for( int i=1; i<argc; i++ ) {
1085 : if( 0==strncmp( argv[i], "--", 2 ) ) continue;
1086 : if( caps_found>=2 ) { usage(); return 1; }
1087 : cap_path[ caps_found++ ] = argv[i];
1088 : }
1089 :
1090 : if( caps_found==1 ) { /* Support one file being passed in to see transaction diff */
1091 : cap_path[ caps_found++ ] = cap_path[ 0 ];
1092 : }
1093 : else if( caps_found!=2 ) {
1094 : fprintf( stderr, "ERROR: expected 2 arguments, got %d\n", argc-1 );
1095 : usage();
1096 : return 1;
1097 : }
1098 :
1099 : /* Acquire workspace */
1100 :
1101 : fd_wksp_t * wksp = fd_wksp_new_anonymous( page_sz, page_cnt, fd_log_cpu_id(), "wksp", 0UL );
1102 : if( FD_UNLIKELY( !wksp ) ) FD_LOG_ERR(( "fd_wksp_new_anonymous() failed" ));
1103 :
1104 : /* Create scratch allocator */
1105 :
1106 : ulong smax = scratch_mb << 20;
1107 : void * smem = fd_wksp_alloc_laddr( wksp, fd_scratch_smem_align(), smax, 1UL );
1108 : if( FD_UNLIKELY( !smem ) ) FD_LOG_ERR(( "Failed to alloc scratch mem" ));
1109 :
1110 : # define SCRATCH_DEPTH (4UL)
1111 : ulong fmem[ SCRATCH_DEPTH ] __attribute((aligned(FD_SCRATCH_FMEM_ALIGN)));
1112 :
1113 : fd_scratch_attach( smem, fmem, smax, SCRATCH_DEPTH );
1114 :
1115 : /* Open capture files for reading */
1116 :
1117 : FILE * cap_file[2] = {0};
1118 : cap_file[0] = fopen( cap_path[0], "rb" );
1119 : cap_file[1] = strcmp( cap_path[0], cap_path[1] ) ? fopen( cap_path[1], "rb" ) : cap_file[0];
1120 :
1121 : if ( FD_UNLIKELY( !cap_file[0] ) )
1122 : FD_LOG_ERR(( "fopen failed (%d-%s) on file=%s", errno, strerror( errno ), cap_path[0] ));
1123 : if ( FD_UNLIKELY( !cap_file[1] ) )
1124 : FD_LOG_ERR(( "fopen failed (%d-%s) on file=%s", errno, strerror( errno ), cap_path[1] ));
1125 :
1126 : /* Create dump dir */
1127 :
1128 : if( mkdir( dump_dir, 0777 )<0 && errno!=EEXIST )
1129 : FD_LOG_ERR(( "mkdir failed (%d-%s)", errno, strerror( errno ) ));
1130 : int dump_dir_fd = open( dump_dir, O_DIRECTORY );
1131 : if( FD_UNLIKELY( dump_dir_fd<0 ) )
1132 : FD_LOG_ERR(( "open(%s) failed (%d-%s)", dump_dir, errno, strerror( errno ) ));
1133 :
1134 : /* Handle the one file case before diffing for accounts/hashes */
1135 : if( cap_file[0] == cap_file[1] ) {
1136 : FD_LOG_NOTICE(( "Only one file was passed in. Will only print transaction diffs." ));
1137 : fd_solcap_one_file_transaction_diff( cap_file[0], start_slot, end_slot );
1138 :
1139 : /* Cleanup*/
1140 : close( dump_dir_fd );
1141 : fclose( cap_file[0] );
1142 : FD_TEST( fd_scratch_frame_used()==0UL );
1143 : fd_wksp_free_laddr( fd_scratch_detach( NULL ) );
1144 : fd_flamenco_halt();
1145 : fd_halt();
1146 : return 0;
1147 : }
1148 :
1149 : /* Create differ */
1150 :
1151 : fd_solcap_differ_t diff[1];
1152 :
1153 : /* Copy over up to last 16 chars */
1154 : char file_name_zero[SOLCAP_FILE_NAME_LEN + 1];
1155 : char file_name_one[SOLCAP_FILE_NAME_LEN + 1];
1156 : char * normalized_file_paths[2] = {file_name_zero, file_name_one};
1157 : normalize_filename( cap_path[0], normalized_file_paths[0], '+' );
1158 : normalize_filename( cap_path[1], normalized_file_paths[1], '-' );
1159 :
1160 : printf( "++%s\n", normalized_file_paths[0] );
1161 : printf( "--%s\n\n", normalized_file_paths[1] );
1162 :
1163 : if( FD_UNLIKELY( !fd_solcap_differ_new( diff, cap_file, (const char **)normalized_file_paths ) ) )
1164 : return 1;
1165 : diff->verbose = verbose;
1166 : diff->dump_dir = dump_dir;
1167 : diff->dump_dir_fd = dump_dir_fd;
1168 : int res = fd_solcap_differ_sync( diff, start_slot, end_slot );
1169 : if( res <0 ) FD_LOG_ERR(( "fd_solcap_differ_sync failed (%d-%s)",
1170 : -res, strerror( -res ) ));
1171 : if( res==0 ) FD_LOG_ERR(( "Captures don't share any slots" ));
1172 :
1173 : /* Diff each block for accounts and hashes */
1174 :
1175 : for(;;) {
1176 : if( FD_UNLIKELY( fd_solcap_diff_bank( diff ) ) ) break;
1177 : printf( "Slot %10lu: OK\n", diff->preimage[0].slot );
1178 : /* Advance to next slot. */
1179 : int res = fd_solcap_differ_sync( diff, start_slot, end_slot );
1180 : if( FD_UNLIKELY( res<0 ) )
1181 : FD_LOG_ERR(( "fd_solcap_differ_sync failed (%d-%s)",
1182 : -res, strerror( -res ) ));
1183 : if( res==0 ) break;
1184 : }
1185 :
1186 : /* Check both files for transaction and produce a diff if possible. If both
1187 : files contain transaction info, this will produce a diff between the two
1188 : files. If one of the files contains the solana transaction info, then we
1189 : will also print the diffs between the solana and firedancer execution. */
1190 : if ( verbose >= 3 ) {
1191 : printf( "\nTransaction diffs:\n" );
1192 : fd_solcap_transaction_diff( cap_file[0], cap_file[1] );
1193 : }
1194 :
1195 : /* Cleanup */
1196 :
1197 : close( dump_dir_fd );
1198 : fclose( cap_file[1] );
1199 : fclose( cap_file[0] );
1200 : FD_TEST( fd_scratch_frame_used()==0UL );
1201 : fd_wksp_free_laddr( fd_scratch_detach( NULL ) );
1202 : fd_flamenco_halt();
1203 : fd_halt();
1204 : return 0;
1205 : }
|