Line data Source code
1 : // --file d2.bin --type vote_state_versioned
2 :
3 : #include "../fd_flamenco.h"
4 : #include "../types/fd_types.h"
5 : #include "../types/fd_types_yaml.h"
6 : #include "../types/fd_types_reflect.h"
7 :
8 : #include <stdio.h>
9 : #include <stdlib.h>
10 : #include <sys/stat.h> /* mkdir(2) */
11 : #include <fcntl.h> /* open(2) */
12 : #include <unistd.h> /* close(2) */
13 :
14 : static void
15 0 : usage( void ) {
16 : fprintf( stderr,
17 0 : "Usage: fd_solcap_dump --type <type> --file {FILE}\n"
18 0 : "\n"
19 0 : "dumps the contents of an account.\n"
20 0 : "\n"
21 0 : "Options:\n"
22 0 : " --file name filename to read\n"
23 0 : " --type <type> type of the data\n"
24 0 : "\n" );
25 0 : }
26 :
27 : int
28 : main( int argc,
29 : char ** argv ) {
30 : fd_boot( &argc, &argv );
31 :
32 : /* Command line handling */
33 :
34 : for( int i=1; i<argc; i++ ) {
35 : if( 0==strcmp( argv[i], "--help" ) ) {
36 : usage();
37 : return 0;
38 : }
39 : }
40 :
41 : char const * _page_sz = fd_env_strip_cmdline_cstr ( &argc, &argv, "--page-sz", NULL, "gigantic" );
42 : ulong page_cnt = fd_env_strip_cmdline_ulong( &argc, &argv, "--page-cnt", NULL, 2UL );
43 : ulong scratch_mb = fd_env_strip_cmdline_ulong( &argc, &argv, "--scratch-mb", NULL, 1024UL );
44 : char const * type = fd_env_strip_cmdline_cstr ( &argc, &argv, "--type", NULL, NULL );
45 : char const * file = fd_env_strip_cmdline_cstr ( &argc, &argv, "--file", NULL, NULL );
46 :
47 : if ((NULL == type) || (NULL == file)) {
48 : usage();
49 : return 0;
50 : }
51 :
52 : ulong page_sz = fd_cstr_to_shmem_page_sz( _page_sz );
53 :
54 : fd_wksp_t * wksp = fd_wksp_new_anonymous( page_sz, page_cnt, fd_log_cpu_id(), "wksp", 0UL );
55 : if( FD_UNLIKELY( !wksp ) ) FD_LOG_ERR(( "fd_wksp_new_anonymous() failed" ));
56 :
57 : /* Create scratch allocator */
58 :
59 : ulong smax = scratch_mb << 20;
60 : void * smem = fd_wksp_alloc_laddr( wksp, fd_scratch_smem_align(), smax, 1UL );
61 : if( FD_UNLIKELY( !smem ) ) FD_LOG_ERR(( "Failed to alloc scratch mem" ));
62 : # define SCRATCH_DEPTH (4UL)
63 : ulong fmem[ SCRATCH_DEPTH ] __attribute((aligned(FD_SCRATCH_FMEM_ALIGN)));
64 : fd_scratch_attach( smem, fmem, smax, SCRATCH_DEPTH );
65 : fd_scratch_push();
66 :
67 : /* Read file */
68 :
69 : uchar * data;
70 : ulong data_sz;
71 : do {
72 : /* Open and stat file */
73 : int fd = open( file, O_RDONLY );
74 : FD_TEST( fd>=0 );
75 : struct stat statbuf[1];
76 : FD_TEST( 0==fstat( fd, statbuf ) );
77 : data_sz = (ulong)statbuf->st_size;
78 :
79 : /* Allocate scratch buffer for file */
80 : FD_TEST( fd_scratch_alloc_is_safe( /* align */ 1UL, data_sz ) );
81 : data = fd_scratch_alloc( /* align */ 1UL, data_sz );
82 :
83 : /* Copy file into memory */
84 : FD_TEST( (ssize_t)data_sz == read( fd, data, data_sz ) );
85 : FD_TEST( 0==close( fd ) );
86 : } while(0);
87 :
88 : /* Decode file */
89 :
90 : fd_bincode_decode_ctx_t decode = {
91 : .data = data,
92 : .dataend = data + data_sz,
93 : };
94 :
95 : fd_flamenco_yaml_t * yaml =
96 : fd_flamenco_yaml_init( fd_flamenco_yaml_new(
97 : fd_scratch_alloc( fd_flamenco_yaml_align(), fd_flamenco_yaml_footprint() ) ),
98 : stdout );
99 :
100 : fd_types_vt_t const * f = fd_types_vt_by_name( type, strlen( type ) );
101 : if( FD_UNLIKELY( !f ) ) FD_LOG_ERR (( "lookup for %s failed", type ));
102 :
103 : ulong total_sz = 0UL;
104 : int err = f->decode_footprint( &decode, &total_sz );
105 : if( FD_UNLIKELY( err!=0 ) ) return err;
106 :
107 : uchar * d = fd_scratch_alloc( f->align, total_sz );
108 :
109 : f->decode( d, &decode );
110 :
111 : f->walk( yaml, d, fd_flamenco_yaml_walk, NULL, 0U, 0U );
112 :
113 : fd_scratch_pop();
114 : fd_scratch_detach( NULL );
115 : fd_halt();
116 : return 0;
117 : }
|