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