@@ -81,6 +81,7 @@ pub struct MetaStorageConfig {
8181 pub username : String ,
8282 pub password : String ,
8383 pub db_name : String ,
84+ pub table_name : String ,
8485}
8586
8687impl Default for MetaStorageConfig {
@@ -93,13 +94,15 @@ impl Default for MetaStorageConfig {
9394 username : String :: new ( ) ,
9495 password : String :: new ( ) ,
9596 db_name : String :: new ( ) ,
97+ table_name : "sol_mainnet_block" . to_string ( ) ,
9698 }
9799 }
98100}
99101
100102#[ derive( Clone ) ]
101103pub struct MetaStorage {
102104 connection : MySQLConnection ,
105+ table_name : String ,
103106}
104107
105108impl MetaStorage {
@@ -124,6 +127,7 @@ impl MetaStorage {
124127 username,
125128 password,
126129 db_name,
130+ table_name,
127131 } = config;
128132 let dsn = format ! ( "mysql://{}:{}@{}:{}/{}" , username, password, host, port, db_name) ;
129133 let connection = MySQLConnection :: new (
@@ -135,6 +139,7 @@ impl MetaStorage {
135139
136140 Ok ( Self {
137141 connection,
142+ table_name,
138143 } )
139144 }
140145
@@ -147,7 +152,7 @@ impl MetaStorage {
147152
148153 // Use `get_first_key` to get the smallest slot
149154 let first_block: Option < u64 > = mysql
150- . get_first_key ( "sol_mainnet_block" , "id" )
155+ . get_first_key ( & self . table_name , "id" )
151156 . await
152157 . map_err ( |e| Error :: StorageBackendError ( Box :: new ( e) ) ) ?;
153158
@@ -162,7 +167,7 @@ impl MetaStorage {
162167
163168 // Use `get_last_key` to get the largest slot
164169 let last_block: Option < u64 > = mysql
165- . get_last_key ( "sol_mainnet_block" , "id" )
170+ . get_last_key ( & self . table_name , "id" )
166171 . await
167172 . map_err ( |e| Error :: StorageBackendError ( Box :: new ( e) ) ) ?;
168173
@@ -187,7 +192,7 @@ impl MetaStorage {
187192 let limit_i64 = limit. map ( |l| l as i64 ) ;
188193
189194 let blocks: Vec < u64 > = mysql
190- . get_row_keys ( "sol_mainnet_block" , Some ( & start_key) , end_key. as_deref ( ) , limit_i64)
195+ . get_row_keys ( & self . table_name , Some ( & start_key) , end_key. as_deref ( ) , limit_i64)
191196 . await ?;
192197 Ok ( blocks. into_iter ( ) . map ( |block| block as Slot ) . collect ( ) )
193198 }
@@ -200,7 +205,7 @@ impl MetaStorage {
200205
201206 // Fetch `PrimitiveDateTime` directly from MySQL
202207 let block_time_primitive: PrimitiveDateTime = mysql
203- . get_single_value :: < PrimitiveDateTime > ( "sol_mainnet_block" , "block_time" , "id" , & key)
208+ . get_single_value :: < PrimitiveDateTime > ( & self . table_name , "block_time" , "id" , & key)
204209 . await
205210 . map_err ( |e| match e {
206211 crate :: mysql:: Error :: RowNotFound => Error :: BlockNotFound ( slot) ,
@@ -227,22 +232,47 @@ impl MetaStorage {
227232 let mysql = self . connection . client ( ) ;
228233
229234 // Fetch the ID of the latest block
230- let latest_block_id: u64 = mysql
231- . get_last_key ( "solana_blocks" , "id" )
235+ let latest_block_id: u64 = match mysql
236+ . get_last_key ( & self . table_name , "id" )
232237 . await
233- . map_err ( |e| Error :: StorageBackendError ( Box :: new ( e) ) ) ?
234- . ok_or_else ( || Error :: BlockNotFound ( 0 ) ) ?; // Handle case where no blocks exist
238+ {
239+ Ok ( Some ( id) ) => id,
240+ Ok ( None ) => {
241+ warn ! ( "No rows in table '{}'; cannot determine latest block id" , self . table_name) ;
242+ return Err ( Error :: BlockNotFound ( 0 ) ) ;
243+ }
244+ Err ( e) => {
245+ error ! ( "Failed to fetch latest block id from '{}': {}" , self . table_name, e) ;
246+ return Err ( Error :: StorageBackendError ( Box :: new ( e) ) ) ;
247+ }
248+ } ;
235249
236250 debug ! ( "Latest block ID fetched: {}" , latest_block_id) ;
237251
238252 // Fetch the block height using the latest block ID
239- let block_height: u64 = mysql
240- . get_single_value :: < u64 > ( "solana_blocks" , "block_height" , "id" , & latest_block_id. to_string ( ) )
253+ let block_height: u64 = match mysql
254+ . get_single_value :: < u64 > ( & self . table_name , "block_height" , "id" , & latest_block_id. to_string ( ) )
241255 . await
242- . map_err ( |e| match e {
243- crate :: mysql:: Error :: RowNotFound => Error :: BlockNotFound ( latest_block_id) ,
244- other => Error :: StorageBackendError ( Box :: new ( other) ) ,
245- } ) ?;
256+ {
257+ Ok ( height) => height,
258+ Err ( crate :: mysql:: Error :: RowNotFound ) => {
259+ warn ! (
260+ "Block height not found for latest block id {} in '{}'" ,
261+ latest_block_id,
262+ self . table_name
263+ ) ;
264+ return Err ( Error :: BlockNotFound ( latest_block_id) ) ;
265+ }
266+ Err ( other) => {
267+ error ! (
268+ "Failed to fetch block_height for id {} from '{}': {}" ,
269+ latest_block_id,
270+ self . table_name,
271+ other
272+ ) ;
273+ return Err ( Error :: StorageBackendError ( Box :: new ( other) ) ) ;
274+ }
275+ } ;
246276
247277 debug ! ( "Latest block Height fetched: {}" , block_height) ;
248278
0 commit comments