@@ -3,6 +3,7 @@ package storage
33import (
44 "context"
55 "crypto/tls"
6+ "database/sql"
67 "encoding/json"
78 "fmt"
89 "math/big"
@@ -110,7 +111,7 @@ func (c *ClickHouseConnector) insertTransactions(txs *[]common.Transaction) erro
110111 query := `
111112 INSERT INTO ` + c .cfg .Database + `.transactions (
112113 chain_id, hash, nonce, block_hash, block_number, block_timestamp, transaction_index,
113- from_address, to_address, value, gas, gas_price, data, max_fee_per_gas, max_priority_fee_per_gas,
114+ from_address, to_address, value, gas, gas_price, data, function_selector, max_fee_per_gas, max_priority_fee_per_gas,
114115 transaction_type, r, s, v, access_list
115116 )
116117 `
@@ -133,6 +134,7 @@ func (c *ClickHouseConnector) insertTransactions(txs *[]common.Transaction) erro
133134 tx .Gas ,
134135 tx .GasPrice ,
135136 tx .Data ,
137+ tx .FunctionSelector ,
136138 tx .MaxFeePerGas ,
137139 tx .MaxPriorityFeePerGas ,
138140 tx .TransactionType ,
@@ -490,28 +492,36 @@ func scanLog(rows driver.Rows) (common.Log, error) {
490492}
491493
492494func (c * ClickHouseConnector ) GetMaxBlockNumber (chainId * big.Int ) (maxBlockNumber * big.Int , err error ) {
493- query := fmt .Sprintf ("SELECT max( number) FROM %s.blocks WHERE is_deleted = 0" , c .cfg .Database )
495+ query := fmt .Sprintf ("SELECT number FROM %s.blocks WHERE is_deleted = 0" , c .cfg .Database )
494496 if chainId .Sign () > 0 {
495497 query += fmt .Sprintf (" AND chain_id = %s" , chainId .String ())
496498 }
499+ query += " ORDER BY number DESC LIMIT 1"
497500 err = c .conn .QueryRow (context .Background (), query ).Scan (& maxBlockNumber )
498501 if err != nil {
502+ if err == sql .ErrNoRows {
503+ return big .NewInt (0 ), nil
504+ }
499505 return nil , err
500506 }
501507 zLog .Debug ().Msgf ("Max block number in main storage is: %s" , maxBlockNumber .String ())
502508 return maxBlockNumber , nil
503509}
504510
505511func (c * ClickHouseConnector ) GetLastStagedBlockNumber (chainId * big.Int , rangeEnd * big.Int ) (maxBlockNumber * big.Int , err error ) {
506- query := fmt .Sprintf ("SELECT max( block_number) FROM %s.block_data WHERE is_deleted = 0" , c .cfg .Database )
512+ query := fmt .Sprintf ("SELECT block_number FROM %s.block_data WHERE is_deleted = 0" , c .cfg .Database )
507513 if chainId .Sign () > 0 {
508514 query += fmt .Sprintf (" AND chain_id = %s" , chainId .String ())
509515 }
510516 if rangeEnd .Sign () > 0 {
511517 query += fmt .Sprintf (" AND block_number <= %s" , rangeEnd .String ())
512518 }
519+ query += " ORDER BY block_number DESC LIMIT 1"
513520 err = c .conn .QueryRow (context .Background (), query ).Scan (& maxBlockNumber )
514521 if err != nil {
522+ if err == sql .ErrNoRows {
523+ return big .NewInt (0 ), nil
524+ }
515525 return nil , err
516526 }
517527 return maxBlockNumber , nil
0 commit comments