@@ -529,6 +529,114 @@ func TestCanonicalHashIteration(t *testing.T) {
529529 }
530530}
531531
532+ type fullLogRLP struct {
533+ Address common.Address
534+ Topics []common.Hash
535+ Data []byte
536+ BlockNumber uint64
537+ TxHash common.Hash
538+ TxIndex uint
539+ BlockHash common.Hash
540+ Index uint
541+ }
542+
543+ func newFullLogRLP (l * types.Log ) * fullLogRLP {
544+ return & fullLogRLP {
545+ Address : l .Address ,
546+ Topics : l .Topics ,
547+ Data : l .Data ,
548+ BlockNumber : l .BlockNumber ,
549+ TxHash : l .TxHash ,
550+ TxIndex : l .TxIndex ,
551+ BlockHash : l .BlockHash ,
552+ Index : l .Index ,
553+ }
554+ }
555+
556+ // Tests that logs associated with a single block can be retrieved.
557+ func TestReadLogs (t * testing.T ) {
558+ db := NewMemoryDatabase ()
559+
560+ // Create a live block since we need metadata to reconstruct the receipt
561+ tx1 := types .NewTransaction (1 , common .HexToAddress ("0x1" ), big .NewInt (1 ), 1 , big .NewInt (1 ), nil )
562+ tx2 := types .NewTransaction (2 , common .HexToAddress ("0x2" ), big .NewInt (2 ), 2 , big .NewInt (2 ), nil )
563+
564+ body := & types.Body {Transactions : types.Transactions {tx1 , tx2 }}
565+
566+ // Create the two receipts to manage afterwards
567+ receipt1 := & types.Receipt {
568+ Status : types .ReceiptStatusFailed ,
569+ CumulativeGasUsed : 1 ,
570+ Logs : []* types.Log {
571+ {Address : common .BytesToAddress ([]byte {0x11 })},
572+ {Address : common .BytesToAddress ([]byte {0x01 , 0x11 })},
573+ },
574+ TxHash : tx1 .Hash (),
575+ ContractAddress : common .BytesToAddress ([]byte {0x01 , 0x11 , 0x11 }),
576+ GasUsed : 111111 ,
577+ }
578+ receipt1 .Bloom = types .CreateBloom (types.Receipts {receipt1 })
579+
580+ receipt2 := & types.Receipt {
581+ PostState : common.Hash {2 }.Bytes (),
582+ CumulativeGasUsed : 2 ,
583+ Logs : []* types.Log {
584+ {Address : common .BytesToAddress ([]byte {0x22 })},
585+ {Address : common .BytesToAddress ([]byte {0x02 , 0x22 })},
586+ },
587+ TxHash : tx2 .Hash (),
588+ ContractAddress : common .BytesToAddress ([]byte {0x02 , 0x22 , 0x22 }),
589+ GasUsed : 222222 ,
590+ }
591+ receipt2 .Bloom = types .CreateBloom (types.Receipts {receipt2 })
592+ receipts := []* types.Receipt {receipt1 , receipt2 }
593+
594+ hash := common .BytesToHash ([]byte {0x03 , 0x14 })
595+ // Check that no receipt entries are in a pristine database
596+ if rs := ReadReceipts (db , hash , 0 , params .TestChainConfig ); len (rs ) != 0 {
597+ t .Fatalf ("non existent receipts returned: %v" , rs )
598+ }
599+ // Insert the body that corresponds to the receipts
600+ WriteBody (db , hash , 0 , body )
601+
602+ // Insert the receipt slice into the database and check presence
603+ WriteReceipts (db , hash , 0 , receipts )
604+
605+ logs := ReadLogs (db , hash , 0 )
606+ if len (logs ) == 0 {
607+ t .Fatalf ("no logs returned" )
608+ }
609+ if have , want := len (logs ), 2 ; have != want {
610+ t .Fatalf ("unexpected number of logs returned, have %d want %d" , have , want )
611+ }
612+ if have , want := len (logs [0 ]), 2 ; have != want {
613+ t .Fatalf ("unexpected number of logs[0] returned, have %d want %d" , have , want )
614+ }
615+ if have , want := len (logs [1 ]), 2 ; have != want {
616+ t .Fatalf ("unexpected number of logs[1] returned, have %d want %d" , have , want )
617+ }
618+
619+ // Fill in log fields so we can compare their rlp encoding
620+ if err := types .Receipts (receipts ).DeriveFields (params .TestChainConfig , hash , 0 , body .Transactions ); err != nil {
621+ t .Fatal (err )
622+ }
623+ for i , pr := range receipts {
624+ for j , pl := range pr .Logs {
625+ rlpHave , err := rlp .EncodeToBytes (newFullLogRLP (logs [i ][j ]))
626+ if err != nil {
627+ t .Fatal (err )
628+ }
629+ rlpWant , err := rlp .EncodeToBytes (newFullLogRLP (pl ))
630+ if err != nil {
631+ t .Fatal (err )
632+ }
633+ if ! bytes .Equal (rlpHave , rlpWant ) {
634+ t .Fatalf ("receipt #%d: receipt mismatch: have %s, want %s" , i , hex .EncodeToString (rlpHave ), hex .EncodeToString (rlpWant ))
635+ }
636+ }
637+ }
638+ }
639+
532640func TestDeriveLogFields (t * testing.T ) {
533641 // Create a few transactions to have receipts for
534642 to2 := common .HexToAddress ("0x2" )
0 commit comments