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 0 : 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 : fd_flamenco_boot( &argc, &argv );
32 :
33 : /* Command line handling */
34 :
35 : for( int i=1; i<argc; i++ ) {
36 : if( 0==strcmp( argv[i], "--help" ) ) {
37 : usage();
38 : return 0;
39 : }
40 : }
41 :
42 : char const * _page_sz = fd_env_strip_cmdline_cstr ( &argc, &argv, "--page-sz", NULL, "gigantic" );
43 : ulong page_cnt = fd_env_strip_cmdline_ulong( &argc, &argv, "--page-cnt", NULL, 2UL );
44 : ulong scratch_mb = fd_env_strip_cmdline_ulong( &argc, &argv, "--scratch-mb", NULL, 1024UL );
45 : char const * type = fd_env_strip_cmdline_cstr ( &argc, &argv, "--type", NULL, NULL );
46 : char const * file = fd_env_strip_cmdline_cstr ( &argc, &argv, "--file", NULL, NULL );
47 :
48 : if ((NULL == type) || (NULL == file)) {
49 : usage();
50 : return 0;
51 : }
52 :
53 : ulong page_sz = fd_cstr_to_shmem_page_sz( _page_sz );
54 :
55 : fd_wksp_t * wksp = fd_wksp_new_anonymous( page_sz, page_cnt, fd_log_cpu_id(), "wksp", 0UL );
56 : if( FD_UNLIKELY( !wksp ) ) FD_LOG_ERR(( "fd_wksp_new_anonymous() failed" ));
57 :
58 : /* Create scratch allocator */
59 :
60 : ulong smax = scratch_mb << 20;
61 : void * smem = fd_wksp_alloc_laddr( wksp, fd_scratch_smem_align(), smax, 1UL );
62 : if( FD_UNLIKELY( !smem ) ) FD_LOG_ERR(( "Failed to alloc scratch mem" ));
63 : # define SCRATCH_DEPTH (4UL)
64 : ulong fmem[ SCRATCH_DEPTH ] __attribute((aligned(FD_SCRATCH_FMEM_ALIGN)));
65 : fd_scratch_attach( smem, fmem, smax, SCRATCH_DEPTH );
66 : fd_scratch_push();
67 :
68 : /* Read file */
69 :
70 : uchar * data;
71 : ulong data_sz;
72 : do {
73 : /* Open and stat file */
74 : int fd = open( file, O_RDONLY );
75 : FD_TEST( fd>=0 );
76 : struct stat statbuf[1];
77 : FD_TEST( 0==fstat( fd, statbuf ) );
78 : data_sz = (ulong)statbuf->st_size;
79 :
80 : /* Allocate scratch buffer for file */
81 : FD_TEST( fd_scratch_alloc_is_safe( /* align */ 1UL, data_sz ) );
82 : data = fd_scratch_alloc( /* align */ 1UL, data_sz );
83 :
84 : /* Copy file into memory */
85 : FD_TEST( (ssize_t)data_sz == read( fd, data, data_sz ) );
86 : FD_TEST( 0==close( fd ) );
87 : } while(0);
88 :
89 : /* Decode file */
90 :
91 : fd_bincode_decode_ctx_t decode = {
92 : .data = data,
93 : .dataend = data + data_sz,
94 : };
95 :
96 : fd_flamenco_yaml_t * yaml =
97 : fd_flamenco_yaml_init( fd_flamenco_yaml_new(
98 : fd_scratch_alloc( fd_flamenco_yaml_align(), fd_flamenco_yaml_footprint() ) ),
99 : stdout );
100 :
101 : fd_types_vt_t const * f = fd_types_vt_by_name( type, strlen( type ) );
102 : if( FD_UNLIKELY( !f ) ) FD_LOG_ERR (( "lookup for %s failed", type ));
103 :
104 : ulong total_sz = 0UL;
105 : int err = f->decode_footprint( &decode, &total_sz );
106 : if( FD_UNLIKELY( err!=0 ) ) return err;
107 :
108 : uchar * d = fd_scratch_alloc( f->align, total_sz );
109 :
110 : f->decode( d, &decode );
111 :
112 : f->walk( yaml, d, fd_flamenco_yaml_walk, NULL, 0U );
113 :
114 : fd_scratch_pop();
115 : fd_scratch_detach( NULL );
116 : fd_flamenco_halt();
117 : fd_halt();
118 : return 0;
119 : }
|