1717package indexer
1818
1919import (
20+ "fmt"
21+
2022 "github.com/jmoiron/sqlx"
2123
2224 "github.com/ethereum/go-ethereum/common"
2325 "github.com/ethereum/go-ethereum/statediff/indexer/models"
2426 "github.com/ethereum/go-ethereum/statediff/indexer/postgres"
25- "github.com/ethereum/go-ethereum/statediff/indexer/shared"
2627)
2728
2829var (
@@ -49,36 +50,19 @@ func (in *PostgresCIDWriter) upsertHeaderCID(tx *sqlx.Tx, header models.HeaderMo
4950 RETURNING id` ,
5051 header .BlockNumber , header .BlockHash , header .ParentHash , header .CID , header .TotalDifficulty , in .db .NodeID , header .Reward , header .StateRoot , header .TxRoot ,
5152 header .RctRoot , header .UncleRoot , header .Bloom , header .Timestamp , header .MhKey , 1 ).Scan (& headerID )
52- if err = = nil {
53- indexerMetrics . blocks . Inc ( 1 )
53+ if err ! = nil {
54+ return 0 , fmt . Errorf ( "error upserting header_cids entry: %v" , err )
5455 }
55- return headerID , err
56+ indexerMetrics .blocks .Inc (1 )
57+ return headerID , nil
5658}
5759
5860func (in * PostgresCIDWriter ) upsertUncleCID (tx * sqlx.Tx , uncle models.UncleModel , headerID int64 ) error {
5961 _ , err := tx .Exec (`INSERT INTO eth.uncle_cids (block_hash, header_id, parent_hash, cid, reward, mh_key) VALUES ($1, $2, $3, $4, $5, $6)
6062 ON CONFLICT (header_id, block_hash) DO UPDATE SET (parent_hash, cid, reward, mh_key) = ($3, $4, $5, $6)` ,
6163 uncle .BlockHash , headerID , uncle .ParentHash , uncle .CID , uncle .Reward , uncle .MhKey )
62- return err
63- }
64-
65- func (in * PostgresCIDWriter ) upsertTransactionAndReceiptCIDs (tx * sqlx.Tx , payload shared.CIDPayload , headerID int64 ) error {
66- for _ , trxCidMeta := range payload .TransactionCIDs {
67- var txID int64
68- err := tx .QueryRowx (`INSERT INTO eth.transaction_cids (header_id, tx_hash, cid, dst, src, index, mh_key, tx_data) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
69- ON CONFLICT (header_id, tx_hash) DO UPDATE SET (cid, dst, src, index, mh_key, tx_data) = ($3, $4, $5, $6, $7, $8)
70- RETURNING id` ,
71- headerID , trxCidMeta .TxHash , trxCidMeta .CID , trxCidMeta .Dst , trxCidMeta .Src , trxCidMeta .Index , trxCidMeta .MhKey , trxCidMeta .Data ).Scan (& txID )
72- if err != nil {
73- return err
74- }
75- indexerMetrics .transactions .Inc (1 )
76- receiptCidMeta , ok := payload .ReceiptCIDs [common .HexToHash (trxCidMeta .TxHash )]
77- if ok {
78- if err := in .upsertReceiptCID (tx , receiptCidMeta , txID ); err != nil {
79- return err
80- }
81- }
64+ if err != nil {
65+ return fmt .Errorf ("error upserting uncle_cids entry: %v" , err )
8266 }
8367 return nil
8468}
@@ -89,20 +73,22 @@ func (in *PostgresCIDWriter) upsertTransactionCID(tx *sqlx.Tx, transaction model
8973 ON CONFLICT (header_id, tx_hash) DO UPDATE SET (cid, dst, src, index, mh_key, tx_data) = ($3, $4, $5, $6, $7, $8)
9074 RETURNING id` ,
9175 headerID , transaction .TxHash , transaction .CID , transaction .Dst , transaction .Src , transaction .Index , transaction .MhKey , transaction .Data ).Scan (& txID )
92- if err = = nil {
93- indexerMetrics . transactions . Inc ( 1 )
76+ if err ! = nil {
77+ return 0 , fmt . Errorf ( "error upserting transaction_cids entry: %v" , err )
9478 }
95- return txID , err
79+ indexerMetrics .transactions .Inc (1 )
80+ return txID , nil
9681}
9782
9883func (in * PostgresCIDWriter ) upsertReceiptCID (tx * sqlx.Tx , rct models.ReceiptModel , txID int64 ) error {
9984 _ , err := tx .Exec (`INSERT INTO eth.receipt_cids (tx_id, cid, contract, contract_hash, topic0s, topic1s, topic2s, topic3s, log_contracts, mh_key, post_state, post_status) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
10085 ON CONFLICT (tx_id) DO UPDATE SET (cid, contract, contract_hash, topic0s, topic1s, topic2s, topic3s, log_contracts, mh_key, post_state, post_status) = ($2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)` ,
10186 txID , rct .CID , rct .Contract , rct .ContractHash , rct .Topic0s , rct .Topic1s , rct .Topic2s , rct .Topic3s , rct .LogContracts , rct .MhKey , rct .PostState , rct .PostStatus )
102- if err = = nil {
103- indexerMetrics . receipts . Inc ( 1 )
87+ if err ! = nil {
88+ return fmt . Errorf ( "error upserting receipt_cids entry: %v" , err )
10489 }
105- return err
90+ indexerMetrics .receipts .Inc (1 )
91+ return nil
10692}
10793
10894func (in * PostgresCIDWriter ) upsertStateCID (tx * sqlx.Tx , stateNode models.StateNodeModel , headerID int64 ) (int64 , error ) {
@@ -115,14 +101,20 @@ func (in *PostgresCIDWriter) upsertStateCID(tx *sqlx.Tx, stateNode models.StateN
115101 ON CONFLICT (header_id, state_path) DO UPDATE SET (state_leaf_key, cid, node_type, diff, mh_key) = ($2, $3, $5, $6, $7)
116102 RETURNING id` ,
117103 headerID , stateKey , stateNode .CID , stateNode .Path , stateNode .NodeType , true , stateNode .MhKey ).Scan (& stateID )
118- return stateID , err
104+ if err != nil {
105+ return 0 , fmt .Errorf ("error upserting state_cids entry: %v" , err )
106+ }
107+ return stateID , nil
119108}
120109
121110func (in * PostgresCIDWriter ) upsertStateAccount (tx * sqlx.Tx , stateAccount models.StateAccountModel , stateID int64 ) error {
122111 _ , err := tx .Exec (`INSERT INTO eth.state_accounts (state_id, balance, nonce, code_hash, storage_root) VALUES ($1, $2, $3, $4, $5)
123112 ON CONFLICT (state_id) DO UPDATE SET (balance, nonce, code_hash, storage_root) = ($2, $3, $4, $5)` ,
124113 stateID , stateAccount .Balance , stateAccount .Nonce , stateAccount .CodeHash , stateAccount .StorageRoot )
125- return err
114+ if err != nil {
115+ return fmt .Errorf ("error upserting state_accounts entry: %v" , err )
116+ }
117+ return nil
126118}
127119
128120func (in * PostgresCIDWriter ) upsertStorageCID (tx * sqlx.Tx , storageCID models.StorageNodeModel , stateID int64 ) error {
@@ -133,5 +125,8 @@ func (in *PostgresCIDWriter) upsertStorageCID(tx *sqlx.Tx, storageCID models.Sto
133125 _ , err := tx .Exec (`INSERT INTO eth.storage_cids (state_id, storage_leaf_key, cid, storage_path, node_type, diff, mh_key) VALUES ($1, $2, $3, $4, $5, $6, $7)
134126 ON CONFLICT (state_id, storage_path) DO UPDATE SET (storage_leaf_key, cid, node_type, diff, mh_key) = ($2, $3, $5, $6, $7)` ,
135127 stateID , storageKey , storageCID .CID , storageCID .Path , storageCID .NodeType , true , storageCID .MhKey )
136- return err
128+ if err != nil {
129+ return fmt .Errorf ("error upserting storage_cids entry: %v" , err )
130+ }
131+ return nil
137132}
0 commit comments