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