@@ -670,3 +670,82 @@ func makeTestReceipts(n int, nPerBlock int) []types.Receipts {
670670 }
671671 return allReceipts
672672}
673+
674+ func TestDeriveLogFields (t * testing.T ) {
675+ // Create a few transactions to have receipts for
676+ to2 := common .HexToAddress ("0x2" )
677+ to3 := common .HexToAddress ("0x3" )
678+ txs := types.Transactions {
679+ types .NewTx (& types.LegacyTx {
680+ Nonce : 1 ,
681+ Value : big .NewInt (1 ),
682+ Gas : 1 ,
683+ GasPrice : big .NewInt (1 ),
684+ }),
685+ types .NewTx (& types.LegacyTx {
686+ To : & to2 ,
687+ Nonce : 2 ,
688+ Value : big .NewInt (2 ),
689+ Gas : 2 ,
690+ GasPrice : big .NewInt (2 ),
691+ }),
692+ types .NewTx (& types.AccessListTx {
693+ To : & to3 ,
694+ Nonce : 3 ,
695+ Value : big .NewInt (3 ),
696+ Gas : 3 ,
697+ GasPrice : big .NewInt (3 ),
698+ }),
699+ }
700+ // Create the corresponding receipts
701+ receipts := []* receiptLogs {
702+ {
703+ Logs : []* types.Log {
704+ {Address : common .BytesToAddress ([]byte {0x11 })},
705+ {Address : common .BytesToAddress ([]byte {0x01 , 0x11 })},
706+ },
707+ },
708+ {
709+ Logs : []* types.Log {
710+ {Address : common .BytesToAddress ([]byte {0x22 })},
711+ {Address : common .BytesToAddress ([]byte {0x02 , 0x22 })},
712+ },
713+ },
714+ {
715+ Logs : []* types.Log {
716+ {Address : common .BytesToAddress ([]byte {0x33 })},
717+ {Address : common .BytesToAddress ([]byte {0x03 , 0x33 })},
718+ },
719+ },
720+ }
721+
722+ // Derive log metadata fields
723+ number := big .NewInt (1 )
724+ hash := common .BytesToHash ([]byte {0x03 , 0x14 })
725+ if err := deriveLogFields (receipts , hash , number .Uint64 (), txs ); err != nil {
726+ t .Fatal (err )
727+ }
728+
729+ // Iterate over all the computed fields and check that they're correct
730+ logIndex := uint (0 )
731+ for i := range receipts {
732+ for j := range receipts [i ].Logs {
733+ if receipts [i ].Logs [j ].BlockNumber != number .Uint64 () {
734+ t .Errorf ("receipts[%d].Logs[%d].BlockNumber = %d, want %d" , i , j , receipts [i ].Logs [j ].BlockNumber , number .Uint64 ())
735+ }
736+ if receipts [i ].Logs [j ].BlockHash != hash {
737+ t .Errorf ("receipts[%d].Logs[%d].BlockHash = %s, want %s" , i , j , receipts [i ].Logs [j ].BlockHash .String (), hash .String ())
738+ }
739+ if receipts [i ].Logs [j ].TxHash != txs [i ].Hash () {
740+ t .Errorf ("receipts[%d].Logs[%d].TxHash = %s, want %s" , i , j , receipts [i ].Logs [j ].TxHash .String (), txs [i ].Hash ().String ())
741+ }
742+ if receipts [i ].Logs [j ].TxIndex != uint (i ) {
743+ t .Errorf ("receipts[%d].Logs[%d].TransactionIndex = %d, want %d" , i , j , receipts [i ].Logs [j ].TxIndex , i )
744+ }
745+ if receipts [i ].Logs [j ].Index != logIndex {
746+ t .Errorf ("receipts[%d].Logs[%d].Index = %d, want %d" , i , j , receipts [i ].Logs [j ].Index , logIndex )
747+ }
748+ logIndex ++
749+ }
750+ }
751+ }
0 commit comments