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