@@ -136,7 +136,7 @@ func (sdi *StateDiffIndexer) PushBlock(block *types.Block, receipts types.Receip
136136 t = time .Now ()
137137
138138 // Publish and index header, collect headerID
139- var headerID int64
139+ var headerID string
140140 headerID , err = sdi .processHeader (blockTx , block .Header (), headerNode , reward , totalDifficulty )
141141 if err != nil {
142142 return nil , err
@@ -181,7 +181,7 @@ func (sdi *StateDiffIndexer) PushBlock(block *types.Block, receipts types.Receip
181181
182182// processHeader publishes and indexes a header IPLD in Postgres
183183// it returns the headerID
184- func (sdi * StateDiffIndexer ) processHeader (tx * BatchTx , header * types.Header , headerNode node.Node , reward , td * big.Int ) (int64 , error ) {
184+ func (sdi * StateDiffIndexer ) processHeader (tx * BatchTx , header * types.Header , headerNode node.Node , reward , td * big.Int ) (string , error ) {
185185 tx .cacheIPLD (headerNode )
186186
187187 var baseFee * int64
@@ -190,12 +190,13 @@ func (sdi *StateDiffIndexer) processHeader(tx *BatchTx, header *types.Header, he
190190 * baseFee = header .BaseFee .Int64 ()
191191 }
192192
193+ headerID := header .Hash ().String ()
193194 mod := models.HeaderModel {
194195 CID : headerNode .Cid ().String (),
195196 MhKey : shared .MultihashKeyFromCID (headerNode .Cid ()),
196197 ParentHash : header .ParentHash .String (),
197198 BlockNumber : header .Number .String (),
198- BlockHash : header . Hash (). String () ,
199+ BlockHash : headerID ,
199200 TotalDifficulty : td .String (),
200201 Reward : reward .String (),
201202 Bloom : header .Bloom .Bytes (),
@@ -207,11 +208,11 @@ func (sdi *StateDiffIndexer) processHeader(tx *BatchTx, header *types.Header, he
207208 BaseFee : baseFee ,
208209 }
209210 _ , err := fmt .Fprintf (sdi .dump , "%+v\r \n " , mod )
210- return 0 , err
211+ return headerID , err
211212}
212213
213214// processUncles publishes and indexes uncle IPLDs in Postgres
214- func (sdi * StateDiffIndexer ) processUncles (tx * BatchTx , headerID int64 , blockNumber uint64 , uncleNodes []* ipld2.EthHeader ) error {
215+ func (sdi * StateDiffIndexer ) processUncles (tx * BatchTx , headerID string , blockNumber uint64 , uncleNodes []* ipld2.EthHeader ) error {
215216 // publish and index uncles
216217 for _ , uncleNode := range uncleNodes {
217218 tx .cacheIPLD (uncleNode )
@@ -223,6 +224,7 @@ func (sdi *StateDiffIndexer) processUncles(tx *BatchTx, headerID int64, blockNum
223224 uncleReward = shared .CalcUncleMinerReward (blockNumber , uncleNode .Number .Uint64 ())
224225 }
225226 uncle := models.UncleModel {
227+ HeaderID : headerID ,
226228 CID : uncleNode .Cid ().String (),
227229 MhKey : shared .MultihashKeyFromCID (uncleNode .Cid ()),
228230 ParentHash : uncleNode .ParentHash .String (),
@@ -238,7 +240,7 @@ func (sdi *StateDiffIndexer) processUncles(tx *BatchTx, headerID int64, blockNum
238240
239241// processArgs bundles arguments to processReceiptsAndTxs
240242type processArgs struct {
241- headerID int64
243+ headerID string
242244 blockNumber * big.Int
243245 receipts types.Receipts
244246 txs types.Transactions
@@ -263,59 +265,24 @@ func (sdi *StateDiffIndexer) processReceiptsAndTxs(tx *BatchTx, args processArgs
263265 tx .cacheIPLD (txNode )
264266
265267 // Indexing
266- // extract topic and contract data from the receipt for indexing
267- mappedContracts := make (map [string ]bool ) // use map to avoid duplicate addresses
268- logDataSet := make ([]* models.LogsModel , len (receipt .Logs ))
269- for idx , l := range receipt .Logs {
270- topicSet := make ([]string , 4 )
271- for ti , topic := range l .Topics {
272- topicSet [ti ] = topic .Hex ()
273- }
274-
275- if ! args.logLeafNodeCIDs [i ][idx ].Defined () {
276- return fmt .Errorf ("invalid log cid" )
277- }
278-
279- mappedContracts [l .Address .String ()] = true
280- logDataSet [idx ] = & models.LogsModel {
281- Address : l .Address .String (),
282- Index : int64 (l .Index ),
283- Data : l .Data ,
284- LeafCID : args.logLeafNodeCIDs [i ][idx ].String (),
285- LeafMhKey : shared .MultihashKeyFromCID (args.logLeafNodeCIDs [i ][idx ]),
286- Topic0 : topicSet [0 ],
287- Topic1 : topicSet [1 ],
288- Topic2 : topicSet [2 ],
289- Topic3 : topicSet [3 ],
290- }
291- }
292- // these are the contracts seen in the logs
293- logContracts := make ([]string , 0 , len (mappedContracts ))
294- for addr := range mappedContracts {
295- logContracts = append (logContracts , addr )
296- }
297- // this is the contract address if this receipt is for a contract creation tx
298- contract := shared .HandleZeroAddr (receipt .ContractAddress )
299- var contractHash string
300- if contract != "" {
301- contractHash = crypto .Keccak256Hash (common .HexToAddress (contract ).Bytes ()).String ()
302- }
303- // index tx first so that the receipt can reference it by FK
268+ // index tx
304269 trx := args .txs [i ]
270+ trxID := trx .Hash ().String ()
305271 // derive sender for the tx that corresponds with this receipt
306272 from , err := types .Sender (signer , trx )
307273 if err != nil {
308274 return fmt .Errorf ("error deriving tx sender: %v" , err )
309275 }
310276 txModel := models.TxModel {
311- Dst : shared .HandleZeroAddrPointer (trx .To ()),
312- Src : shared .HandleZeroAddr (from ),
313- TxHash : trx .Hash ().String (),
314- Index : int64 (i ),
315- Data : trx .Data (),
316- CID : txNode .Cid ().String (),
317- MhKey : shared .MultihashKeyFromCID (txNode .Cid ()),
318- Type : trx .Type (),
277+ HeaderID : args .headerID ,
278+ Dst : shared .HandleZeroAddrPointer (trx .To ()),
279+ Src : shared .HandleZeroAddr (from ),
280+ TxHash : trxID ,
281+ Index : int64 (i ),
282+ Data : trx .Data (),
283+ CID : txNode .Cid ().String (),
284+ MhKey : shared .MultihashKeyFromCID (txNode .Cid ()),
285+ Type : trx .Type (),
319286 }
320287 if _ , err := fmt .Fprintf (sdi .dump , "%+v\r \n " , txModel ); err != nil {
321288 return err
@@ -328,6 +295,7 @@ func (sdi *StateDiffIndexer) processReceiptsAndTxs(tx *BatchTx, args processArgs
328295 storageKeys [k ] = storageKey .Hex ()
329296 }
330297 accessListElementModel := models.AccessListElementModel {
298+ TxID : trxID ,
331299 Index : int64 (j ),
332300 Address : accessListElement .Address .Hex (),
333301 StorageKeys : storageKeys ,
@@ -337,12 +305,20 @@ func (sdi *StateDiffIndexer) processReceiptsAndTxs(tx *BatchTx, args processArgs
337305 }
338306 }
339307
308+ // this is the contract address if this receipt is for a contract creation tx
309+ contract := shared .HandleZeroAddr (receipt .ContractAddress )
310+ var contractHash string
311+ if contract != "" {
312+ contractHash = crypto .Keccak256Hash (common .HexToAddress (contract ).Bytes ()).String ()
313+ }
314+
340315 // index the receipt
341316 if ! args .rctLeafNodeCIDs [i ].Defined () {
342317 return fmt .Errorf ("invalid receipt leaf node cid" )
343318 }
344319
345320 rctModel := & models.ReceiptModel {
321+ TxID : trxID ,
346322 Contract : contract ,
347323 ContractHash : contractHash ,
348324 LeafCID : args .rctLeafNodeCIDs [i ].String (),
@@ -359,6 +335,31 @@ func (sdi *StateDiffIndexer) processReceiptsAndTxs(tx *BatchTx, args processArgs
359335 return err
360336 }
361337
338+ logDataSet := make ([]* models.LogsModel , len (receipt .Logs ))
339+ for idx , l := range receipt .Logs {
340+ topicSet := make ([]string , 4 )
341+ for ti , topic := range l .Topics {
342+ topicSet [ti ] = topic .Hex ()
343+ }
344+
345+ if ! args.logLeafNodeCIDs [i ][idx ].Defined () {
346+ return fmt .Errorf ("invalid log cid" )
347+ }
348+
349+ logDataSet [idx ] = & models.LogsModel {
350+ ReceiptID : trxID ,
351+ Address : l .Address .String (),
352+ Index : int64 (l .Index ),
353+ Data : l .Data ,
354+ LeafCID : args.logLeafNodeCIDs [i ][idx ].String (),
355+ LeafMhKey : shared .MultihashKeyFromCID (args.logLeafNodeCIDs [i ][idx ]),
356+ Topic0 : topicSet [0 ],
357+ Topic1 : topicSet [1 ],
358+ Topic2 : topicSet [2 ],
359+ Topic3 : topicSet [3 ],
360+ }
361+ }
362+
362363 if _ , err := fmt .Fprintf (sdi .dump , "%+v\r \n " , logDataSet ); err != nil {
363364 return err
364365 }
@@ -374,7 +375,7 @@ func (sdi *StateDiffIndexer) processReceiptsAndTxs(tx *BatchTx, args processArgs
374375}
375376
376377// PushStateNode publishes and indexes a state diff node object (including any child storage nodes) in the IPLD sql
377- func (sdi * StateDiffIndexer ) PushStateNode (batch interfaces.Batch , stateNode sdtypes.StateNode ) error {
378+ func (sdi * StateDiffIndexer ) PushStateNode (batch interfaces.Batch , stateNode sdtypes.StateNode , headerID string ) error {
378379 tx , ok := batch .(* BatchTx )
379380 if ! ok {
380381 return fmt .Errorf ("sql batch is expected to be of type %T, got %T" , & BatchTx {}, batch )
@@ -384,6 +385,7 @@ func (sdi *StateDiffIndexer) PushStateNode(batch interfaces.Batch, stateNode sdt
384385 // short circuit if it is a Removed node
385386 // this assumes the db has been initialized and a public.blocks entry for the Removed node is present
386387 stateModel := models.StateNodeModel {
388+ HeaderID : headerID ,
387389 Path : stateNode .Path ,
388390 StateKey : common .BytesToHash (stateNode .LeafKey ).String (),
389391 CID : shared .RemovedNodeStateCID ,
@@ -398,6 +400,7 @@ func (sdi *StateDiffIndexer) PushStateNode(batch interfaces.Batch, stateNode sdt
398400 return fmt .Errorf ("error generating and cacheing state node IPLD: %v" , err )
399401 }
400402 stateModel := models.StateNodeModel {
403+ HeaderID : headerID ,
401404 Path : stateNode .Path ,
402405 StateKey : common .BytesToHash (stateNode .LeafKey ).String (),
403406 CID : stateCIDStr ,
@@ -422,6 +425,8 @@ func (sdi *StateDiffIndexer) PushStateNode(batch interfaces.Batch, stateNode sdt
422425 return fmt .Errorf ("error decoding state account rlp: %s" , err .Error ())
423426 }
424427 accountModel := models.StateAccountModel {
428+ HeaderID : headerID ,
429+ StatePath : stateNode .Path ,
425430 Balance : account .Balance .String (),
426431 Nonce : account .Nonce ,
427432 CodeHash : account .CodeHash ,
@@ -437,6 +442,8 @@ func (sdi *StateDiffIndexer) PushStateNode(batch interfaces.Batch, stateNode sdt
437442 // short circuit if it is a Removed node
438443 // this assumes the db has been initialized and a public.blocks entry for the Removed node is present
439444 storageModel := models.StorageNodeModel {
445+ HeaderID : headerID ,
446+ StatePath : stateNode .Path ,
440447 Path : storageNode .Path ,
441448 StorageKey : common .BytesToHash (storageNode .LeafKey ).String (),
442449 CID : shared .RemovedNodeStorageCID ,
@@ -453,6 +460,8 @@ func (sdi *StateDiffIndexer) PushStateNode(batch interfaces.Batch, stateNode sdt
453460 return fmt .Errorf ("error generating and cacheing storage node IPLD: %v" , err )
454461 }
455462 storageModel := models.StorageNodeModel {
463+ HeaderID : headerID ,
464+ StatePath : stateNode .Path ,
456465 Path : storageNode .Path ,
457466 StorageKey : common .BytesToHash (storageNode .LeafKey ).String (),
458467 CID : storageCIDStr ,
@@ -482,7 +491,7 @@ func (sdi *StateDiffIndexer) PushCodeAndCodeHash(batch interfaces.Batch, codeAnd
482491 return nil
483492}
484493
485- // Close satisfied io.Closer
494+ // Close satisfies io.Closer
486495func (sdi * StateDiffIndexer ) Close () error {
487496 return sdi .dump .Close ()
488497}
0 commit comments