Skip to content

Commit 5195029

Browse files
s1nazzyalbert
authored andcommitted
core/rawdb: add slow path for getting legacy logs (#23879)
* eth/tracers: add slow path for getting legacy logs * core/rawdb: fix test
1 parent bef4536 commit 5195029

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

core/rawdb/accessors_chain.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,15 +669,20 @@ func deriveLogFields(receipts []*receiptLogs, hash common.Hash, number uint64, t
669669
// ReadLogs retrieves the logs for all transactions in a block. The log fields
670670
// are populated with metadata. In case the receipts or the block body
671671
// are not found, a nil is returned.
672-
func ReadLogs(db ethdb.Reader, hash common.Hash, number uint64) [][]*types.Log {
672+
func ReadLogs(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) [][]*types.Log {
673673
// Retrieve the flattened receipt slice
674674
data := ReadReceiptsRLP(db, hash, number)
675675
if len(data) == 0 {
676676
return nil
677677
}
678678
receipts := []*receiptLogs{}
679679
if err := rlp.DecodeBytes(data, &receipts); err != nil {
680-
log.Error("Invalid receipt array RLP", "hash", hash, "err", err)
680+
// Receipts might be in the legacy format, try decoding that.
681+
// TODO: to be removed after users migrated
682+
if logs := readLegacyLogs(db, hash, number, config); logs != nil {
683+
return logs
684+
}
685+
log.Error("Invalid receipt array RLP", "hash", "err", err)
681686
return nil
682687
}
683688

@@ -697,6 +702,21 @@ func ReadLogs(db ethdb.Reader, hash common.Hash, number uint64) [][]*types.Log {
697702
return logs
698703
}
699704

705+
// readLegacyLogs is a temporary workaround for when trying to read logs
706+
// from a block which has its receipt stored in the legacy format. It'll
707+
// be removed after users have migrated their freezer databases.
708+
func readLegacyLogs(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) [][]*types.Log {
709+
receipts := ReadReceipts(db, hash, number, config)
710+
if receipts == nil {
711+
return nil
712+
}
713+
logs := make([][]*types.Log, len(receipts))
714+
for i, receipt := range receipts {
715+
logs[i] = receipt.Logs
716+
}
717+
return logs
718+
}
719+
700720
// ReadBlock retrieves an entire block corresponding to the hash, assembling it
701721
// back from the stored header and body. If either the header or body could not
702722
// be retrieved nil is returned.

core/rawdb/accessors_chain_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ func TestReadLogs(t *testing.T) {
744744
// Insert the receipt slice into the database and check presence
745745
WriteReceipts(db, hash, 0, receipts)
746746

747-
logs := ReadLogs(db, hash, 0)
747+
logs := ReadLogs(db, hash, 0, params.TestChainConfig)
748748
if len(logs) == 0 {
749749
t.Fatalf("no logs returned")
750750
}

eth/api_backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*typ
188188
if number == nil {
189189
return nil, errors.New("failed to get block number from hash")
190190
}
191-
logs := rawdb.ReadLogs(db, hash, *number)
191+
logs := rawdb.ReadLogs(db, hash, *number, b.eth.blockchain.Config())
192192
if logs == nil {
193193
return nil, errors.New("failed to get logs for block")
194194
}

0 commit comments

Comments
 (0)