@@ -528,3 +528,82 @@ func TestCanonicalHashIteration(t *testing.T) {
528528 }
529529 }
530530}
531+
532+ func TestDeriveLogFields (t * testing.T ) {
533+ // Create a few transactions to have receipts for
534+ to2 := common .HexToAddress ("0x2" )
535+ to3 := common .HexToAddress ("0x3" )
536+ txs := types.Transactions {
537+ types .NewTx (& types.LegacyTx {
538+ Nonce : 1 ,
539+ Value : big .NewInt (1 ),
540+ Gas : 1 ,
541+ GasPrice : big .NewInt (1 ),
542+ }),
543+ types .NewTx (& types.LegacyTx {
544+ To : & to2 ,
545+ Nonce : 2 ,
546+ Value : big .NewInt (2 ),
547+ Gas : 2 ,
548+ GasPrice : big .NewInt (2 ),
549+ }),
550+ types .NewTx (& types.AccessListTx {
551+ To : & to3 ,
552+ Nonce : 3 ,
553+ Value : big .NewInt (3 ),
554+ Gas : 3 ,
555+ GasPrice : big .NewInt (3 ),
556+ }),
557+ }
558+ // Create the corresponding receipts
559+ receipts := []* receiptLogs {
560+ {
561+ Logs : []* types.Log {
562+ {Address : common .BytesToAddress ([]byte {0x11 })},
563+ {Address : common .BytesToAddress ([]byte {0x01 , 0x11 })},
564+ },
565+ },
566+ {
567+ Logs : []* types.Log {
568+ {Address : common .BytesToAddress ([]byte {0x22 })},
569+ {Address : common .BytesToAddress ([]byte {0x02 , 0x22 })},
570+ },
571+ },
572+ {
573+ Logs : []* types.Log {
574+ {Address : common .BytesToAddress ([]byte {0x33 })},
575+ {Address : common .BytesToAddress ([]byte {0x03 , 0x33 })},
576+ },
577+ },
578+ }
579+
580+ // Derive log metadata fields
581+ number := big .NewInt (1 )
582+ hash := common .BytesToHash ([]byte {0x03 , 0x14 })
583+ if err := deriveLogFields (receipts , hash , number .Uint64 (), txs ); err != nil {
584+ t .Fatal (err )
585+ }
586+
587+ // Iterate over all the computed fields and check that they're correct
588+ logIndex := uint (0 )
589+ for i := range receipts {
590+ for j := range receipts [i ].Logs {
591+ if receipts [i ].Logs [j ].BlockNumber != number .Uint64 () {
592+ t .Errorf ("receipts[%d].Logs[%d].BlockNumber = %d, want %d" , i , j , receipts [i ].Logs [j ].BlockNumber , number .Uint64 ())
593+ }
594+ if receipts [i ].Logs [j ].BlockHash != hash {
595+ t .Errorf ("receipts[%d].Logs[%d].BlockHash = %s, want %s" , i , j , receipts [i ].Logs [j ].BlockHash .String (), hash .String ())
596+ }
597+ if receipts [i ].Logs [j ].TxHash != txs [i ].Hash () {
598+ t .Errorf ("receipts[%d].Logs[%d].TxHash = %s, want %s" , i , j , receipts [i ].Logs [j ].TxHash .String (), txs [i ].Hash ().String ())
599+ }
600+ if receipts [i ].Logs [j ].TxIndex != uint (i ) {
601+ t .Errorf ("receipts[%d].Logs[%d].TransactionIndex = %d, want %d" , i , j , receipts [i ].Logs [j ].TxIndex , i )
602+ }
603+ if receipts [i ].Logs [j ].Index != logIndex {
604+ t .Errorf ("receipts[%d].Logs[%d].Index = %d, want %d" , i , j , receipts [i ].Logs [j ].Index , logIndex )
605+ }
606+ logIndex ++
607+ }
608+ }
609+ }
0 commit comments