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