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 : 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+1+FD_BASE58_ENCODED_32_LEN+4+1 ];
303 0 : int res = snprintf( path, sizeof(path), "%s-%s.bin", FD_BASE58_ENC_32_ALLOCA( entry->key ), FD_BASE58_ENC_32_ALLOCA( entry->hash ) );
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-%s.bin\n"
383 0 : " (%s) data: %s/%s-%s.bin\n"
384 0 : " vimdiff <(xxd '%s/%s-%s.bin') <(xxd '%s/%s-%s.bin')\n",
385 0 : diff->file_paths[0], diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[0]->key ), FD_BASE58_ENC_32_ALLOCA( entry[0]->hash ),
386 0 : diff->file_paths[1], diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[1]->key ), FD_BASE58_ENC_32_ALLOCA( entry[1]->hash ),
387 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[0]->key ), FD_BASE58_ENC_32_ALLOCA( entry[0]->hash ),
388 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[1]->key ), FD_BASE58_ENC_32_ALLOCA( entry[1]->hash ) );
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+1+FD_BASE58_ENCODED_32_LEN+4+1 ];
396 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) );
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-%s.yml' '%s/%s-%s.yml'\n",
412 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[0]->key ), FD_BASE58_ENC_32_ALLOCA( entry[0]->hash ),
413 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry[1]->key ), FD_BASE58_ENC_32_ALLOCA( entry[1]->hash ) );
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 : else
450 0 : printf( " (both files ) owner: %s\n", FD_BASE58_ENC_32_ALLOCA( meta[0].owner ) );
451 : /* Even if the owner matches, still print it for convenience */
452 0 : if( meta[0].lamports != meta[1].lamports )
453 0 : printf( " (%s) lamports: %lu\n"
454 0 : " (%s) lamports: %lu\n",
455 0 : diff->file_paths[0], meta[0].lamports,
456 0 : diff->file_paths[1], meta[1].lamports );
457 0 : if( meta[0].data_sz != meta[1].data_sz )
458 0 : printf( " (%s) data_sz: %lu\n"
459 0 : " (%s) data_sz: %lu\n",
460 0 : diff->file_paths[0], meta[0].data_sz,
461 0 : diff->file_paths[1], meta[1].data_sz );
462 0 : if( meta[0].slot != meta[1].slot )
463 0 : printf( " (%s) slot: %lu\n"
464 0 : " (%s) slot: %lu\n",
465 0 : diff->file_paths[0], meta[0].slot,
466 0 : diff->file_paths[1], meta[1].slot );
467 0 : if( meta[0].executable != meta[1].executable )
468 0 : printf( " (%s) executable: %d\n"
469 0 : " (%s) executable: %d\n",
470 0 : diff->file_paths[0], meta[0].executable,
471 0 : diff->file_paths[1], meta[1].executable );
472 0 : if( ( (meta[0].data_sz != 0UL) | fd_solcap_includes_account_data( &meta[0] ) )
473 0 : | ( (meta[1].data_sz != 0UL) | fd_solcap_includes_account_data( &meta[1] ) ) )
474 0 : fd_solcap_diff_account_data( diff, meta, entry, data_goff );
475 :
476 : /* Restore file offsets */
477 0 : for( ulong i=0UL; i<2UL; i++ ) {
478 0 : if( FD_UNLIKELY( 0!=fseek( diff->iter[ i ].stream, orig_off[ i ], SEEK_SET ) ) )
479 0 : FD_LOG_ERR(( "fseek failed (%d-%s)", errno, strerror( errno ) ));
480 0 : }
481 0 : }
482 :
483 : /* fd_solcap_diff_missing_account is like fd_solcap_diff_account but in
484 : the case that either side of the account is missing entirely. */
485 :
486 : static void
487 : fd_solcap_diff_missing_account( fd_solcap_differ_t * diff,
488 : fd_solcap_account_tbl_t const * const entry,
489 : ulong const acc_tbl_goff,
490 0 : FILE * stream ) {
491 :
492 : /* Remember current file offset */
493 0 : long orig_off = ftell( stream );
494 0 : if( FD_UNLIKELY( orig_off<0L ) )
495 0 : FD_LOG_ERR(( "ftell failed (%d-%s)", errno, strerror( errno ) ));
496 :
497 : /* Read account meta */
498 0 : fd_solcap_AccountMeta meta[1];
499 0 : ulong data_goff[1];
500 0 : int err = fd_solcap_find_account( stream, meta, data_goff, entry, acc_tbl_goff );
501 0 : FD_TEST( err==0 );
502 :
503 0 : printf(
504 0 : " lamports: %lu\n"
505 0 : " data_sz: %lu\n"
506 0 : " owner: %s\n"
507 0 : " slot: %lu\n"
508 0 : " executable: %d\n",
509 0 : meta->lamports,
510 0 : meta->data_sz,
511 0 : FD_BASE58_ENC_32_ALLOCA( meta->owner ),
512 0 : meta->slot,
513 0 : meta->executable
514 0 : );
515 :
516 : /* Dump account data to file */
517 0 : if( diff->verbose >= 4 ) {
518 :
519 : /* TODO: Remove hardcoded account size check */
520 0 : FD_TEST( meta->data_sz <= 8388608 );
521 :
522 0 : FD_SCRATCH_SCOPE_BEGIN {
523 0 : void * acc_data = fd_scratch_alloc( 1UL, meta->data_sz );
524 :
525 : /* Rewind capture stream */
526 0 : FD_TEST( 0==fseek(stream, (long)*data_goff, SEEK_SET ) );
527 :
528 : /* Copy data */
529 0 : FD_TEST( meta->data_sz == fread( acc_data, 1UL, meta->data_sz, stream ) );
530 :
531 0 : fd_solcap_dump_account_data( diff, meta, entry, acc_data );
532 :
533 : /* Inform user */
534 0 : printf( " data: %s/%s-%s.bin\n"
535 0 : " xxd '%s/%s-%s.bin'\n"
536 0 : " explorer: 'https://explorer.solana.com/block/%lu?accountFilter=%s&filter=all'",
537 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry->key ), FD_BASE58_ENC_32_ALLOCA( entry->hash ),
538 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry->key ), FD_BASE58_ENC_32_ALLOCA( entry->hash ),
539 0 : meta->slot, FD_BASE58_ENC_32_ALLOCA( entry->key ) );
540 :
541 0 : #pragma GCC diagnostic push
542 0 : #pragma GCC diagnostic ignored "-Wnonnull"
543 0 : if( fd_solcap_can_pretty_print( meta->owner, entry->key ) ) {
544 0 : #pragma GCC diagnostic pop
545 : /* Create YAML file */
546 0 : char path[ FD_BASE58_ENCODED_32_LEN+1+FD_BASE58_ENCODED_32_LEN+4+1 ];
547 0 : int res = snprintf( path, sizeof(path), "%s-%s.yml", FD_BASE58_ENC_32_ALLOCA( entry->key ), FD_BASE58_ENC_32_ALLOCA( entry->hash ) );
548 0 : FD_TEST( (res>0) & (res<(int)sizeof(path)) );
549 0 : int fd = openat( diff->dump_dir_fd, path, O_CREAT|O_WRONLY|O_TRUNC, 0666 );
550 0 : if( FD_UNLIKELY( fd<0 ) )
551 0 : FD_LOG_ERR(( "openat(%d,%s) failed (%d-%s)",
552 0 : diff->dump_dir_fd, path, errno, strerror( errno ) ));
553 :
554 : /* Write YAML file */
555 0 : FILE * file = fdopen( fd, "wb" );
556 0 : fd_solcap_account_pretty_print( entry->key, meta->owner, acc_data, meta->data_sz, file );
557 0 : fclose( file ); /* closes fd */
558 :
559 : /* Inform user */
560 0 : printf( " cat '%s/%s-%s.yml'\n",
561 0 : diff->dump_dir, FD_BASE58_ENC_32_ALLOCA( entry->key ), FD_BASE58_ENC_32_ALLOCA( entry->hash ) );
562 0 : }
563 0 : } FD_SCRATCH_SCOPE_END;
564 0 : }
565 0 : }
566 :
567 : /* fd_solcap_diff_account_tbl detects and prints differences in the
568 : accounts that were hashed into the account delta hash. */
569 :
570 : static void
571 0 : fd_solcap_diff_account_tbl( fd_solcap_differ_t * diff ) {
572 :
573 : /* Read and sort tables */
574 :
575 0 : fd_solcap_account_tbl_t * tbl [2];
576 0 : fd_solcap_account_tbl_t * tbl_end[2];
577 0 : ulong chunk_goff[2];
578 0 : for( ulong i=0UL; i<2UL; i++ ) {
579 0 : if( diff->preimage[i].account_table_coff == 0L ) {
580 0 : FD_LOG_WARNING(( "Missing accounts table in capture" ));
581 0 : return;
582 0 : }
583 0 : chunk_goff[i] = (ulong)( (long)diff->iter[i].chunk_off + diff->preimage[i].account_table_coff );
584 :
585 : /* Read table meta and seek to table */
586 0 : FILE * stream = diff->iter[i].stream;
587 0 : fd_solcap_AccountTableMeta meta[1];
588 0 : int err = fd_solcap_find_account_table( stream, meta, chunk_goff[i] );
589 0 : FD_TEST( err==0 );
590 :
591 0 : if( FD_UNLIKELY( meta->account_table_cnt > INT_MAX ) ) {
592 0 : FD_LOG_WARNING(( "Too many accounts in capture" ));
593 0 : return;
594 0 : }
595 :
596 : /* Allocate table */
597 0 : ulong tbl_cnt = meta->account_table_cnt;
598 0 : ulong tbl_align = alignof(fd_solcap_account_tbl_t);
599 0 : ulong tbl_sz = tbl_cnt * sizeof(fd_solcap_account_tbl_t);
600 0 : FD_TEST( fd_scratch_alloc_is_safe( tbl_align, tbl_sz ) );
601 0 : tbl [i] = fd_scratch_alloc( tbl_align, tbl_sz );
602 0 : tbl_end[i] = tbl[i] + tbl_cnt;
603 :
604 : /* Read table */
605 0 : FD_TEST( tbl_cnt==fread( tbl[i], sizeof(fd_solcap_account_tbl_t), tbl_cnt, stream ) );
606 :
607 : /* Sort table */
608 0 : sort_account_tbl_inplace( tbl[i], tbl_cnt );
609 0 : }
610 :
611 : /* Walk tables in parallel */
612 :
613 0 : for(;;) {
614 0 : fd_solcap_account_tbl_t * a = tbl[0];
615 0 : fd_solcap_account_tbl_t * b = tbl[1];
616 :
617 0 : if( a==tbl_end[0] ) break;
618 0 : if( b==tbl_end[1] ) break;
619 :
620 0 : int key_cmp = memcmp( a->key, b->key, 32UL );
621 0 : if( key_cmp==0 ) {
622 0 : int hash_cmp = memcmp( a->hash, b->hash, 32UL );
623 0 : if( hash_cmp!=0 ) {
624 0 : printf( "\n (in both files) account: %s\n"
625 0 : " (%s) hash: %s\n"
626 0 : " (%s) hash: %s\n",
627 0 : FD_BASE58_ENC_32_ALLOCA( a->key ),
628 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( a->hash ),
629 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( b->hash ) );
630 :
631 0 : if( diff->verbose >= 3 )
632 0 : fd_solcap_diff_account( diff, (fd_solcap_account_tbl_t const * const *)tbl, chunk_goff );
633 0 : }
634 :
635 0 : tbl[0]++;
636 0 : tbl[1]++;
637 0 : continue;
638 0 : }
639 :
640 0 : if( key_cmp<0 ) {
641 0 : printf( "\n (%s) account: %s\n", diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( a->key ) );
642 0 : if( diff->verbose >= 3 )
643 0 : fd_solcap_diff_missing_account( diff, tbl[0], chunk_goff[0], diff->iter[0].stream );
644 0 : tbl[0]++;
645 0 : continue;
646 0 : }
647 :
648 0 : if( key_cmp>0 ) {
649 0 : printf( "\n (%s) account: %s\n", diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( b->key ) );
650 0 : if( diff->verbose >= 3 )
651 0 : fd_solcap_diff_missing_account( diff, tbl[1], chunk_goff[1], diff->iter[1].stream );
652 0 : tbl[1]++;
653 0 : continue;
654 0 : }
655 0 : }
656 0 : while( tbl[0]!=tbl_end[0] ) {
657 0 : printf( "\n (%s) account: %s\n", diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( tbl[0]->key ) );
658 0 : if( diff->verbose >= 3 )
659 0 : fd_solcap_diff_missing_account( diff, tbl[0], chunk_goff[0], diff->iter[0].stream );
660 0 : tbl[0]++;
661 0 : }
662 0 : while( tbl[1]!=tbl_end[1] ) {
663 0 : printf( "\n (%s) account: %s\n", diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( tbl[1]->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 : }
668 :
669 0 : }
670 :
671 : /* fd_solcap_diff_bank detects bank hash mismatches and prints a
672 : human-readable description of the root cause to stdout. Returns 0
673 : if bank hashes match, 1 if a mismatch was detected. */
674 :
675 : static int
676 0 : fd_solcap_diff_bank( fd_solcap_differ_t * diff ) {
677 :
678 0 : fd_solcap_BankPreimage const * pre = diff->preimage;
679 :
680 0 : FD_TEST( pre[0].slot == pre[1].slot );
681 0 : if( 0==memcmp( &pre[0], &pre[1], sizeof(fd_solcap_BankPreimage) ) )
682 0 : return 0;
683 :
684 0 : printf( "\nbank hash mismatch at slot=%lu\n", pre[0].slot );
685 :
686 0 : printf( "(%s) bank_hash: %s\n"
687 0 : "(%s) bank_hash: %s\n",
688 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( pre[0].bank_hash ),
689 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( pre[1].bank_hash ) );
690 :
691 0 : int only_account_mismatch = 0;
692 0 : if( 0!=memcmp( pre[0].account_delta_hash, pre[1].account_delta_hash, 32UL ) ) {
693 0 : only_account_mismatch = 1;
694 0 : printf( "(%s) account_delta_hash: %s\n"
695 0 : "(%s) account_delta_hash: %s\n",
696 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( pre[0].account_delta_hash ),
697 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( pre[1].account_delta_hash ) );
698 0 : }
699 0 : if( 0!=memcmp( pre[0].accounts_lt_hash_checksum, pre[1].accounts_lt_hash_checksum, 32UL ) ) {
700 0 : only_account_mismatch = 1;
701 0 : printf( "(%s) accounts_lt_hash_checksum: %s\n"
702 0 : "(%s) accounts_lt_hash_checksum: %s\n",
703 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( pre[0].accounts_lt_hash_checksum ),
704 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( pre[1].accounts_lt_hash_checksum ) );
705 0 : }
706 0 : if( 0!=memcmp( pre[0].prev_bank_hash, pre[1].prev_bank_hash, 32UL ) ) {
707 0 : only_account_mismatch = 0;
708 0 : printf( "(%s) prev_bank_hash: %s\n"
709 0 : "(%s) prev_bank_hash: %s\n",
710 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( pre[0].prev_bank_hash ),
711 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( pre[1].prev_bank_hash ) );
712 0 : }
713 0 : if( 0!=memcmp( pre[0].poh_hash, pre[1].poh_hash, 32UL ) ) {
714 0 : only_account_mismatch = 0;
715 0 : printf( "(%s) poh_hash: %s\n"
716 0 : "(%s) poh_hash: %s\n",
717 0 : diff->file_paths[0], FD_BASE58_ENC_32_ALLOCA( pre[0].poh_hash ),
718 0 : diff->file_paths[1], FD_BASE58_ENC_32_ALLOCA( pre[1].poh_hash ) );
719 0 : }
720 0 : if( pre[0].signature_cnt != pre[1].signature_cnt ) {
721 0 : only_account_mismatch = 0;
722 0 : printf( "(%s) signature_cnt: %lu\n"
723 0 : "(%s) signature_cnt: %lu\n",
724 0 : diff->file_paths[0], pre[0].signature_cnt,
725 0 : diff->file_paths[1], pre[1].signature_cnt );
726 0 : }
727 0 : if( pre[0].account_cnt != pre[1].account_cnt ) {
728 0 : printf( "(%s) account_cnt: %lu\n"
729 0 : "(%s) account_cnt: %lu\n",
730 0 : diff->file_paths[0], pre[0].account_cnt,
731 0 : diff->file_paths[1], pre[1].account_cnt );
732 0 : }
733 0 : printf( "\n" );
734 :
735 0 : if( only_account_mismatch && diff->verbose >= 2 ) {
736 0 : fd_scratch_push();
737 0 : fd_solcap_diff_account_tbl( diff );
738 0 : fd_scratch_pop();
739 0 : }
740 :
741 0 : return 1;
742 0 : }
743 :
744 : /* Diffs two transaction results with each other. */
745 : static void
746 0 : fd_solcap_transaction_fd_diff( fd_solcap_txn_differ_t * txn_differ ) {
747 0 : if ( FD_UNLIKELY( memcmp( txn_differ->transaction[0].txn_sig,
748 0 : txn_differ->transaction[1].txn_sig, 32UL ) != 0 ) ) {
749 : /* Transactions don't line up. */
750 0 : FD_LOG_WARNING(( "Transaction signatures are different for slot=%lu, signature=(%s != %s)."
751 0 : "It is possible that either the transactions are out of order or some transactions are missing.",
752 0 : txn_differ->transaction[0].slot,
753 0 : FD_BASE58_ENC_32_ALLOCA( txn_differ->transaction[0].txn_sig ),
754 0 : FD_BASE58_ENC_32_ALLOCA( txn_differ->transaction[1].txn_sig ) ));
755 0 : }
756 0 : else {
757 0 : bool diff_txns = txn_differ->transaction[0].fd_txn_err != txn_differ->transaction[1].fd_txn_err;
758 0 : bool diff_cus = txn_differ->transaction[0].fd_cus_used != txn_differ->transaction[1].fd_cus_used;
759 0 : bool diff_instr_err_idx = txn_differ->transaction[0].instr_err_idx != txn_differ->transaction[1].instr_err_idx;
760 0 : if ( diff_txns || diff_cus ) {
761 0 : printf(
762 0 : "\nslot: %lu\n"
763 0 : "txn_sig: '%s'\n",
764 0 : txn_differ->transaction[0].slot,
765 0 : FD_BASE58_ENC_64_ALLOCA( txn_differ->transaction[0].txn_sig) );
766 0 : }
767 0 : if ( diff_txns ) {
768 0 : printf(
769 0 : " (+) txn_err: %d\n"
770 0 : " (-) txn_err: %d\n",
771 0 : txn_differ->transaction[0].fd_txn_err,
772 0 : txn_differ->transaction[1].fd_txn_err );
773 0 : }
774 0 : if ( diff_cus ) {
775 0 : printf(
776 0 : " (+) cus_used: %lu\n"
777 0 : " (-) cus_used: %lu\n",
778 0 : txn_differ->transaction[0].fd_cus_used,
779 0 : txn_differ->transaction[1].fd_cus_used );
780 0 : }
781 0 : if ( diff_instr_err_idx ) {
782 0 : printf(
783 0 : " (+) instr_err_idx: %d\n"
784 0 : " (-) instr_err_idx: %d\n",
785 0 : txn_differ->transaction[0].instr_err_idx,
786 0 : txn_differ->transaction[1].instr_err_idx );
787 0 : }
788 0 : }
789 0 : }
790 :
791 : /* Diffs firedancer transaction result with solana's result iff it is included
792 : in the solcap. The solana result comes from rocksdb. This diff is generated
793 : from just one fd_solcap_Transaction object */
794 : static void
795 : fd_solcap_transaction_solana_diff( fd_solcap_Transaction * transaction,
796 : ulong start_slot,
797 0 : ulong end_slot ) {
798 0 : if ( transaction->slot < start_slot || transaction->slot > end_slot ) {
799 0 : return;
800 0 : }
801 :
802 0 : if ( transaction->solana_txn_err == ULONG_MAX && transaction->solana_cus_used == ULONG_MAX ) {
803 : /* If solana_txn_err and solana_cus_used are both not populated, don't print diff */
804 0 : return;
805 0 : } else if ( transaction->solana_txn_err == ULONG_MAX ) {
806 : /* Print diff if the solana_txn_err is not set (txn executed successfully) */
807 0 : transaction->solana_txn_err = 0;
808 0 : }
809 :
810 : /* Only print a diff if cus or transaction result is different */
811 0 : if( !!( transaction->fd_txn_err ) != !!( transaction->solana_txn_err ) ||
812 0 : transaction->fd_cus_used != transaction->solana_cus_used ) {
813 0 : printf(
814 0 : "slot: %lu\n"
815 0 : "txn_sig: '%s'\n"
816 0 : " (+) txn_err: %d\n"
817 0 : " (-) solana_txn_err: %lu\n"
818 0 : " (+) cus_used: %lu\n"
819 0 : " (-) solana_cus_used: %lu\n"
820 0 : " instr_err_idx: %d\n"
821 0 : " explorer: 'https://explorer.solana.com/tx/%s'\n"
822 0 : " solscan: 'https://solscan.io/tx/%s'\n"
823 0 : " solanafm: 'https://solana.fm/tx/%s'\n",
824 0 : transaction->slot,
825 0 : FD_BASE58_ENC_64_ALLOCA( transaction->txn_sig ),
826 0 : transaction->fd_txn_err,
827 0 : transaction->solana_txn_err,
828 0 : transaction->fd_cus_used,
829 0 : transaction->solana_cus_used,
830 0 : transaction->instr_err_idx,
831 0 : FD_BASE58_ENC_64_ALLOCA( transaction->txn_sig ),
832 0 : FD_BASE58_ENC_64_ALLOCA( transaction->txn_sig ),
833 0 : FD_BASE58_ENC_64_ALLOCA( transaction->txn_sig ) );
834 0 : }
835 0 : }
836 :
837 : static void
838 0 : fd_solcap_get_transaction_from_iter( fd_solcap_txn_differ_t * differ, ulong idx ) {
839 0 : if ( fd_solcap_chunk_iter_done( &differ->iter[idx] ) )
840 0 : return;
841 :
842 0 : fd_solcap_chunk_t const * chunk = fd_solcap_chunk_iter_item( &differ->iter[idx] );
843 :
844 0 : if( FD_UNLIKELY( 0!=fseek( differ->file[idx], differ->chunk_gaddr[idx] + (long)chunk->meta_coff, SEEK_SET ) ) ) {
845 0 : FD_LOG_ERR(( "fseek transaction meta failed (%d-%s)", errno, strerror( errno ) ));
846 0 : }
847 0 : if( FD_UNLIKELY( chunk->meta_sz != fread( &differ->meta_buf[idx], 1UL, chunk->meta_sz, differ->file[idx] ) ) ) {
848 0 : FD_LOG_ERR(( "fread transaction meta failed (%d-%s)", errno, strerror( errno ) ));
849 0 : }
850 :
851 0 : pb_istream_t stream = pb_istream_from_buffer( differ->meta_buf[idx], chunk->meta_sz );
852 0 : if( FD_UNLIKELY( !pb_decode( &stream, fd_solcap_Transaction_fields, &differ->transaction[idx] ) ) ) {
853 0 : FD_LOG_HEXDUMP_DEBUG(( "transaction meta", differ->meta_buf[idx], chunk->meta_sz ));
854 0 : FD_LOG_ERR(( "pb_decode transaction meta failed (%s)", PB_GET_ERROR(&stream) ));
855 0 : }
856 0 : }
857 :
858 : static void
859 0 : fd_solcap_transaction_iter( fd_solcap_txn_differ_t * txn_differ, ulong idx ) {
860 0 : while ( !fd_solcap_chunk_iter_done( &txn_differ->iter[idx] ) ) {
861 0 : txn_differ->chunk_gaddr[idx] = fd_solcap_chunk_iter_find( &txn_differ->iter[idx], FD_SOLCAP_V1_TRXN_MAGIC );
862 0 : fd_solcap_get_transaction_from_iter( txn_differ, idx );
863 0 : fd_solcap_transaction_solana_diff( &txn_differ->transaction[idx], 0, ULONG_MAX );
864 0 : }
865 0 : }
866 :
867 : static void
868 0 : fd_solcap_txn_differ_advance( fd_solcap_txn_differ_t * txn_differ ) {
869 0 : while ( !fd_solcap_chunk_iter_done( &txn_differ->iter[0] ) &&
870 0 : !fd_solcap_chunk_iter_done( &txn_differ->iter[1] ) ) {
871 : /* Diff transactions against both solana result (rocksdb) and against each other */
872 0 : fd_solcap_transaction_fd_diff( txn_differ );
873 0 : fd_solcap_transaction_solana_diff( &txn_differ->transaction[0], 0, ULONG_MAX );
874 0 : txn_differ->chunk_gaddr[0] = fd_solcap_chunk_iter_find( &txn_differ->iter[0], FD_SOLCAP_V1_TRXN_MAGIC );
875 0 : txn_differ->chunk_gaddr[1] = fd_solcap_chunk_iter_find( &txn_differ->iter[1], FD_SOLCAP_V1_TRXN_MAGIC );
876 0 : fd_solcap_get_transaction_from_iter( txn_differ, 0 );
877 0 : fd_solcap_get_transaction_from_iter( txn_differ, 1 );
878 0 : }
879 0 : }
880 :
881 0 : static void fd_solcap_txn_differ_sync( fd_solcap_txn_differ_t * txn_differ ) {
882 : /* Find first transaction for both files */
883 0 : for( int i=0; i<2; i++ ) {
884 0 : txn_differ->chunk_gaddr[i] = fd_solcap_chunk_iter_find( &txn_differ->iter[i], FD_SOLCAP_V1_TRXN_MAGIC );
885 0 : if( FD_UNLIKELY( txn_differ->chunk_gaddr[i] < 0L ) ) {
886 0 : int err = fd_solcap_chunk_iter_err( &txn_differ->iter[i] );
887 0 : if( err == 0 ) break;
888 0 : FD_LOG_ERR(( "fd_solcap_chunk_iter_next() failed (%d-%s)", err, strerror( err ) ));
889 0 : }
890 0 : }
891 :
892 : /* Get first transaction on both */
893 0 : fd_solcap_get_transaction_from_iter( txn_differ, 0 );
894 0 : fd_solcap_get_transaction_from_iter( txn_differ, 1 );
895 :
896 0 : for(;;) {
897 : /* If one is done but not the other, iterate through the rest of the
898 : transactions in order to generate a diff against solana's transactions */
899 0 : if( fd_solcap_chunk_iter_done( &txn_differ->iter[0] ) ) {
900 0 : fd_solcap_transaction_iter( txn_differ, 1 );
901 0 : break;
902 0 : } else if( fd_solcap_chunk_iter_done( &txn_differ->iter[1] ) ) {
903 0 : fd_solcap_transaction_iter( txn_differ, 0 );
904 0 : break;
905 0 : }
906 :
907 : /* Otherwise, try to sync up the two files, printing any solana diffs along the way */
908 0 : if( txn_differ->transaction[0].slot == txn_differ->transaction[1].slot ) {
909 0 : fd_solcap_txn_differ_advance( txn_differ );
910 0 : } else if( txn_differ->transaction[0].slot < txn_differ->transaction[1].slot ) {
911 : /* Advance index 0 only */
912 0 : fd_solcap_transaction_solana_diff( &txn_differ->transaction[0], 0, ULONG_MAX );
913 0 : txn_differ->chunk_gaddr[0] = fd_solcap_chunk_iter_find( &txn_differ->iter[0], FD_SOLCAP_V1_TRXN_MAGIC );
914 0 : fd_solcap_get_transaction_from_iter( txn_differ, 0 );
915 0 : } else if( txn_differ->transaction[1].slot < txn_differ->transaction[0].slot ) {
916 : /* Advance index 1 only */
917 0 : fd_solcap_transaction_solana_diff( &txn_differ->transaction[1], 0, ULONG_MAX );
918 0 : txn_differ->chunk_gaddr[1] = fd_solcap_chunk_iter_find( &txn_differ->iter[1], FD_SOLCAP_V1_TRXN_MAGIC );
919 0 : fd_solcap_get_transaction_from_iter( txn_differ, 1 );
920 0 : }
921 0 : }
922 0 : }
923 :
924 0 : static void fd_solcap_transaction_diff( FILE * file_zero, FILE * file_one ) {
925 :
926 0 : if ( FD_UNLIKELY( fseek( file_zero, 0, SEEK_SET ) != 0 ) ) {
927 0 : FD_LOG_ERR(( "fseek to start of file failed (%d-%s)", errno, strerror( errno ) ));
928 0 : }
929 0 : if ( FD_UNLIKELY( fseek( file_one, 0, SEEK_SET ) != 0 ) ) {
930 0 : FD_LOG_ERR(( "fseek to start of file failed (%d-%s)", errno, strerror( errno ) ));
931 0 : }
932 :
933 : /* Read file header and seek to first chunk to begin interation */
934 0 : fd_solcap_fhdr_t fhdr_zero[1];
935 0 : fd_solcap_fhdr_t fhdr_one[1];
936 0 : ulong n_zero = fread( fhdr_zero, sizeof(fd_solcap_fhdr_t), 1UL, file_zero );
937 0 : ulong n_one = fread( fhdr_one, sizeof(fd_solcap_fhdr_t), 1UL, file_one );
938 :
939 0 : if ( FD_UNLIKELY( n_zero != 1UL ) ) {
940 0 : FD_LOG_ERR(( "fread file header failed (%d-%s)", errno, strerror( errno ) ));
941 0 : }
942 0 : if ( FD_UNLIKELY( n_one != 1UL ) ) {
943 0 : FD_LOG_ERR(( "fread file header failed (%d-%s)", errno, strerror( errno ) ));
944 0 : }
945 0 : int err;
946 0 : err = fseek( file_zero, (long)fhdr_zero->chunk0_foff - (long)sizeof(fd_solcap_fhdr_t), SEEK_CUR );
947 0 : if( FD_UNLIKELY( err < 0L ) ) {
948 0 : FD_LOG_ERR(( "fseek chunk0 failed (%d-%s)", errno, strerror( errno ) ));
949 0 : }
950 0 : err = fseek( file_one, (long)fhdr_one->chunk0_foff - (long)sizeof(fd_solcap_fhdr_t), SEEK_CUR );
951 0 : if( FD_UNLIKELY( err < 0L ) ) {
952 0 : FD_LOG_ERR(( "fseek chunk0 failed (%d-%s)", errno, strerror( errno ) ));
953 0 : }
954 :
955 : /* Setup txn_differ */
956 0 : fd_solcap_txn_differ_t txn_differ;
957 0 : txn_differ.file[0] = file_zero;
958 0 : txn_differ.file[1] = file_one;
959 0 : fd_solcap_chunk_iter_new( &txn_differ.iter[0], file_zero );
960 0 : fd_solcap_chunk_iter_new( &txn_differ.iter[1], file_one );
961 :
962 : /* Iterate and diff throught the transactions */
963 0 : fd_solcap_txn_differ_sync( &txn_differ );
964 0 : }
965 :
966 : void
967 0 : fd_solcap_one_file_transaction_diff( FILE * file, ulong start_slot, ulong end_slot ) {
968 : /* Open and read the header */
969 0 : fd_solcap_fhdr_t fhdr[1];
970 0 : ulong n = fread( fhdr, sizeof(fd_solcap_fhdr_t), 1UL, file );
971 0 : if( FD_UNLIKELY( n!=1UL ) ) {
972 0 : FD_LOG_ERR(( "fread file header failed (%d-%s)", errno, strerror( errno ) ));
973 0 : }
974 :
975 : /* Seek to the first chunk */
976 0 : int err = fseek( file, (long)fhdr->chunk0_foff - (long)sizeof(fd_solcap_fhdr_t), SEEK_CUR );
977 0 : if( FD_UNLIKELY( err<0L ) ) {
978 0 : FD_LOG_ERR(( "fseek chunk0 failed (%d-%s)", errno, strerror( errno ) ));
979 0 : }
980 :
981 : /* Iterate through the chunks diffing the trnasactions */
982 0 : fd_solcap_chunk_iter_t iter[1];
983 0 : fd_solcap_chunk_iter_new( iter, file );
984 0 : while( !fd_solcap_chunk_iter_done( iter ) ) {
985 0 : long chunk_gaddr = fd_solcap_chunk_iter_find( iter, FD_SOLCAP_V1_TRXN_MAGIC );
986 0 : if( FD_UNLIKELY( chunk_gaddr<0L ) ) {
987 0 : int err = fd_solcap_chunk_iter_err( iter );
988 0 : if( err==0 ) break;
989 0 : FD_LOG_ERR(( "fd_solcap_chunk_iter_next() failed (%d-%s)", err, strerror( err ) ));
990 0 : }
991 :
992 0 : fd_solcap_chunk_t const * chunk = fd_solcap_chunk_iter_item( iter );
993 0 : if( FD_UNLIKELY( !chunk ) ) FD_LOG_ERR(( "fd_solcap_chunk_item() failed" ));
994 :
995 : /* Read transaction meta */
996 :
997 0 : uchar meta_buf[ 128UL ];
998 0 : if( FD_UNLIKELY( 0!=fseek( file, chunk_gaddr + (long)chunk->meta_coff, SEEK_SET ) ) ) {
999 0 : FD_LOG_ERR(( "fseek transaction meta failed (%d-%s)", errno, strerror( errno ) ));
1000 0 : }
1001 0 : if( FD_UNLIKELY( chunk->meta_sz != fread( meta_buf, 1UL, chunk->meta_sz, file ) ) ) {
1002 0 : FD_LOG_ERR(( "fread transaction meta failed (%d-%s)", errno, strerror( errno ) ));
1003 0 : }
1004 :
1005 : /* Deserialize transaction meta */
1006 :
1007 0 : pb_istream_t stream = pb_istream_from_buffer( meta_buf, chunk->meta_sz );
1008 :
1009 0 : fd_solcap_Transaction meta;
1010 0 : if( FD_UNLIKELY( !pb_decode( &stream, fd_solcap_Transaction_fields, &meta ) ) ) {
1011 0 : FD_LOG_HEXDUMP_DEBUG(( "transaction meta", meta_buf, chunk->meta_sz ));
1012 0 : FD_LOG_ERR(( "pb_decode transaction meta failed (%s)", PB_GET_ERROR(&stream) ));
1013 0 : }
1014 :
1015 0 : if( meta.slot < start_slot || meta.slot > end_slot ) {
1016 0 : continue;
1017 0 : }
1018 :
1019 0 : fd_solcap_transaction_solana_diff( &meta, start_slot, end_slot );
1020 0 : }
1021 0 : }
1022 :
1023 : static void
1024 0 : usage( void ) {
1025 : fprintf( stderr,
1026 0 : "Usage: fd_solcap_diff [options] {FILE1} {FILE2}\n"
1027 0 : "\n"
1028 0 : "Imports a runtime capture file from JSON.\n"
1029 0 : "\n"
1030 0 : "Options:\n"
1031 0 : " --page-sz {gigantic|huge|normal} Page size\n"
1032 0 : " --page-cnt {count} Page count\n"
1033 0 : " --scratch-mb 1024 Scratch mem MiB\n"
1034 0 : " -v 1 Diff verbosity\n"
1035 0 : " --dump-dir {dir} Dump directory\n"
1036 0 : " --start-slot {slot} Start slot\n"
1037 0 : " --end-slot {slot} End slot\n"
1038 0 : "\n" );
1039 0 : }
1040 :
1041 : int
1042 : main( int argc,
1043 : char ** argv ) {
1044 : fd_boot( &argc, &argv );
1045 :
1046 : /* Command line handling */
1047 :
1048 : for( int i=1; i<argc; i++ ) {
1049 : if( 0==strcmp( argv[i], "--help" ) ) {
1050 : usage();
1051 : return 0;
1052 : }
1053 : }
1054 :
1055 : char const * _page_sz = fd_env_strip_cmdline_cstr ( &argc, &argv, "--page-sz", NULL, "gigantic" );
1056 : ulong page_cnt = fd_env_strip_cmdline_ulong( &argc, &argv, "--page-cnt", NULL, 2UL );
1057 : ulong scratch_mb = fd_env_strip_cmdline_ulong( &argc, &argv, "--scratch-mb", NULL, 1024UL );
1058 : int verbose = fd_env_strip_cmdline_int ( &argc, &argv, "-v", NULL, 1 );
1059 : char const * dump_dir = fd_env_strip_cmdline_cstr ( &argc, &argv, "--dump-dir", NULL, "dump" );
1060 : ulong start_slot = fd_env_strip_cmdline_ulong( &argc, &argv, "--start-slot", NULL, 0UL );
1061 : ulong end_slot = fd_env_strip_cmdline_ulong( &argc, &argv, "--end-slot", NULL, ULONG_MAX );
1062 :
1063 : ulong page_sz = fd_cstr_to_shmem_page_sz( _page_sz );
1064 : if( FD_UNLIKELY( !page_sz ) ) FD_LOG_ERR(( "unsupported --page-sz" ));
1065 :
1066 : char const * cap_path[2] = {0};
1067 : int caps_found = 0;
1068 :
1069 : for( int i=1; i<argc; i++ ) {
1070 : if( 0==strncmp( argv[i], "--", 2 ) ) continue;
1071 : if( caps_found>=2 ) { usage(); return 1; }
1072 : cap_path[ caps_found++ ] = argv[i];
1073 : }
1074 :
1075 : if( caps_found==1 ) { /* Support one file being passed in to see transaction diff */
1076 : cap_path[ caps_found++ ] = cap_path[ 0 ];
1077 : }
1078 : else if( caps_found!=2 ) {
1079 : fprintf( stderr, "ERROR: expected 2 arguments, got %d\n", argc-1 );
1080 : usage();
1081 : return 1;
1082 : }
1083 :
1084 : /* Acquire workspace */
1085 :
1086 : fd_wksp_t * wksp = fd_wksp_new_anonymous( page_sz, page_cnt, fd_log_cpu_id(), "wksp", 0UL );
1087 : if( FD_UNLIKELY( !wksp ) ) FD_LOG_ERR(( "fd_wksp_new_anonymous() failed" ));
1088 :
1089 : /* Create scratch allocator */
1090 :
1091 : ulong smax = scratch_mb << 20;
1092 : void * smem = fd_wksp_alloc_laddr( wksp, fd_scratch_smem_align(), smax, 1UL );
1093 : if( FD_UNLIKELY( !smem ) ) FD_LOG_ERR(( "Failed to alloc scratch mem" ));
1094 :
1095 : # define SCRATCH_DEPTH (4UL)
1096 : ulong fmem[ SCRATCH_DEPTH ] __attribute((aligned(FD_SCRATCH_FMEM_ALIGN)));
1097 :
1098 : fd_scratch_attach( smem, fmem, smax, SCRATCH_DEPTH );
1099 :
1100 : /* Open capture files for reading */
1101 :
1102 : FILE * cap_file[2] = {0};
1103 : cap_file[0] = fopen( cap_path[0], "rb" );
1104 : cap_file[1] = strcmp( cap_path[0], cap_path[1] ) ? fopen( cap_path[1], "rb" ) : cap_file[0];
1105 :
1106 : if ( FD_UNLIKELY( !cap_file[0] ) )
1107 : FD_LOG_ERR(( "fopen failed (%d-%s) on file=%s", errno, strerror( errno ), cap_path[0] ));
1108 : if ( FD_UNLIKELY( !cap_file[1] ) )
1109 : FD_LOG_ERR(( "fopen failed (%d-%s) on file=%s", errno, strerror( errno ), cap_path[1] ));
1110 :
1111 : /* Create dump dir */
1112 :
1113 : if( mkdir( dump_dir, 0777 )<0 && errno!=EEXIST )
1114 : FD_LOG_ERR(( "mkdir failed (%d-%s)", errno, strerror( errno ) ));
1115 : int dump_dir_fd = open( dump_dir, O_DIRECTORY );
1116 : if( FD_UNLIKELY( dump_dir_fd<0 ) )
1117 : FD_LOG_ERR(( "open(%s) failed (%d-%s)", dump_dir, errno, strerror( errno ) ));
1118 :
1119 : /* Handle the one file case before diffing for accounts/hashes */
1120 : if( cap_file[0] == cap_file[1] ) {
1121 : FD_LOG_NOTICE(( "Only one file was passed in. Will only print transaction diffs." ));
1122 : fd_solcap_one_file_transaction_diff( cap_file[0], start_slot, end_slot );
1123 :
1124 : /* Cleanup*/
1125 : close( dump_dir_fd );
1126 : fclose( cap_file[0] );
1127 : FD_TEST( fd_scratch_frame_used()==0UL );
1128 : fd_wksp_free_laddr( fd_scratch_detach( NULL ) );
1129 : fd_halt();
1130 : return 0;
1131 : }
1132 :
1133 : /* Create differ */
1134 :
1135 : fd_solcap_differ_t diff[1];
1136 :
1137 : /* Copy over up to last 16 chars */
1138 : char file_name_zero[SOLCAP_FILE_NAME_LEN + 1];
1139 : char file_name_one[SOLCAP_FILE_NAME_LEN + 1];
1140 : char * normalized_file_paths[2] = {file_name_zero, file_name_one};
1141 : normalize_filename( cap_path[0], normalized_file_paths[0], '+' );
1142 : normalize_filename( cap_path[1], normalized_file_paths[1], '-' );
1143 :
1144 : printf( "++%s\n", normalized_file_paths[0] );
1145 : printf( "--%s\n\n", normalized_file_paths[1] );
1146 :
1147 : if( FD_UNLIKELY( !fd_solcap_differ_new( diff, cap_file, (const char **)normalized_file_paths ) ) )
1148 : return 1;
1149 : diff->verbose = verbose;
1150 : diff->dump_dir = dump_dir;
1151 : diff->dump_dir_fd = dump_dir_fd;
1152 : int res = fd_solcap_differ_sync( diff, start_slot, end_slot );
1153 : if( res <0 ) FD_LOG_ERR(( "fd_solcap_differ_sync failed (%d-%s)",
1154 : -res, strerror( -res ) ));
1155 : if( res==0 ) FD_LOG_ERR(( "Captures don't share any slots" ));
1156 :
1157 : /* Diff each block for accounts and hashes */
1158 :
1159 : for(;;) {
1160 : if( FD_UNLIKELY( fd_solcap_diff_bank( diff ) ) ) break;
1161 : printf( "Slot %10lu: OK\n", diff->preimage[0].slot );
1162 : /* Advance to next slot. */
1163 : int res = fd_solcap_differ_sync( diff, start_slot, end_slot );
1164 : if( FD_UNLIKELY( res<0 ) )
1165 : FD_LOG_ERR(( "fd_solcap_differ_sync failed (%d-%s)",
1166 : -res, strerror( -res ) ));
1167 : if( res==0 ) break;
1168 : }
1169 :
1170 : /* Check both files for transaction and produce a diff if possible. If both
1171 : files contain transaction info, this will produce a diff between the two
1172 : files. If one of the files contains the solana transaction info, then we
1173 : will also print the diffs between the solana and firedancer execution. */
1174 : if ( verbose >= 3 ) {
1175 : printf( "\nTransaction diffs:\n" );
1176 : fd_solcap_transaction_diff( cap_file[0], cap_file[1] );
1177 : }
1178 :
1179 : /* Cleanup */
1180 :
1181 : close( dump_dir_fd );
1182 : fclose( cap_file[1] );
1183 : fclose( cap_file[0] );
1184 : FD_TEST( fd_scratch_frame_used()==0UL );
1185 : fd_wksp_free_laddr( fd_scratch_detach( NULL ) );
1186 : fd_halt();
1187 : return 0;
1188 : }
|