@@ -17,9 +17,13 @@ use serde::de::DeserializeOwned;
1717use serde:: Serialize ;
1818use serde_json:: Value ;
1919
20- use super :: { AccountBalance , DataContainer , DatabaseClient , CHAIN_ID_KEY , GENESIS_BALANCES_KEY } ;
20+ use super :: {
21+ AccountBalance , CertifiedBlock , DataContainer , DatabaseClient , CHAIN_ID_KEY ,
22+ GENESIS_BALANCES_KEY ,
23+ } ;
2124
2225const BQ_BLOCKS_TABLE_ID : & str = "blocks" ;
26+ const BQ_CERTIFIED_BLOCKS_TABLE_ID : & str = "certified_blocks" ;
2327const BQ_TRANSACTIONS_TABLE_ID : & str = "transactions" ;
2428const BQ_KEY_VALUE_TABLE_ID : & str = "key_value_data" ;
2529
@@ -139,6 +143,13 @@ impl BigQueryDbClient {
139143 TableFieldSchema :: json( "body" ) ,
140144 ] ,
141145 ) ,
146+ (
147+ BQ_CERTIFIED_BLOCKS_TABLE_ID ,
148+ vec ! [
149+ TableFieldSchema :: integer( "id" ) ,
150+ TableFieldSchema :: json( "certified_response" ) ,
151+ ] ,
152+ ) ,
142153 (
143154 BQ_TRANSACTIONS_TABLE_ID ,
144155 vec ! [
@@ -481,6 +492,36 @@ impl DatabaseClient for BigQueryDbClient {
481492 . await
482493 }
483494
495+ async fn insert_certified_block_data ( & self , block : CertifiedBlock ) -> anyhow:: Result < ( ) > {
496+ let hash = block. data . hash . to_hex_str ( ) ;
497+ let block_row = CertifiedBlockRow {
498+ id : block. data . number . 0 . as_u64 ( ) ,
499+ certified_response : serde_json:: to_value ( block) . expect ( "Failed to serialize block" ) ,
500+ } ;
501+
502+ let rows = TableDataInsertAllRequestRows {
503+ insert_id : Some ( hash) ,
504+ json : serde_json:: to_value ( block_row) . expect ( "Failed to serialize block" ) ,
505+ } ;
506+
507+ self . insert_batch_data ( BQ_CERTIFIED_BLOCKS_TABLE_ID , vec ! [ rows] )
508+ . await
509+ }
510+
511+ async fn get_last_certified_block_data ( & self ) -> anyhow:: Result < CertifiedBlock > {
512+ let query_request = QueryRequest {
513+ query_parameters : None ,
514+ query : format ! (
515+ "SELECT certified_response FROM `{project_id}.{dataset_id}.{table_id}` ORDER BY id DESC LIMIT 1" ,
516+ project_id = self . project_id,
517+ dataset_id = self . dataset_id,
518+ table_id = BQ_CERTIFIED_BLOCKS_TABLE_ID , ) ,
519+ ..Default :: default ( )
520+ } ;
521+
522+ self . query_one ( query_request) . await
523+ }
524+
484525 async fn get_transaction ( & self , tx_hash : H256 ) -> anyhow:: Result < Transaction > {
485526 let query_request = QueryRequest {
486527 query_parameters : Some ( vec ! [
@@ -517,6 +558,13 @@ struct BlockRow {
517558 body : Value ,
518559}
519560
561+ #[ derive( Debug , Serialize ) ]
562+ pub struct CertifiedBlockRow {
563+ id : u64 ,
564+ #[ serde( serialize_with = "serialize_json_as_string" ) ]
565+ certified_response : Value ,
566+ }
567+
520568#[ derive( Debug , Serialize , Clone ) ]
521569struct ExeResultRow {
522570 tx_hash : String ,
0 commit comments