Skip to content

Commit

Permalink
Decode legacy receipts with legacy LogForStorage struct
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhdn2 committed Jul 13, 2023
1 parent 71341a0 commit b5ff4fc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
37 changes: 37 additions & 0 deletions core/types/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,40 @@ func (l *Log) DecodeRLP(s *rlp.Stream) error {
func (l *Log) String() string {
return fmt.Sprintf(`log: %x %x %x %x %d %x %d`, l.Address, l.Topics, l.Data, l.TxHash, l.TxIndex, l.BlockHash, l.Index)
}

// LogForStorage is a wrapper around a Log that flattens and parses the entire content of
// a log including non-consensus fields.
type LogForStorage Log

// EncodeRLP implements rlp.Encoder.
func (l *LogForStorage) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, rlpStorageLog{
Address: l.Address,
Topics: l.Topics,
Data: l.Data,
BlockNumber: l.BlockNumber,
TxHash: l.TxHash,
TxIndex: l.TxIndex,
BlockHash: l.BlockHash,
Index: l.Index,
})
}

// DecodeRLP implements rlp.Decoder.
func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error {
var dec rlpStorageLog
err := s.Decode(&dec)
if err == nil {
*l = LogForStorage{
Address: dec.Address,
Topics: dec.Topics,
Data: dec.Data,
BlockNumber: dec.BlockNumber,
TxHash: dec.TxHash,
TxIndex: dec.TxIndex,
BlockHash: dec.BlockHash,
Index: dec.Index,
}
}
return err
}
7 changes: 5 additions & 2 deletions core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type legacyStoredReceiptRLP struct {
Bloom Bloom
TxHash common.Hash
ContractAddress common.Address
Logs []*Log
Logs []*LogForStorage
GasUsed uint64
}

Expand Down Expand Up @@ -328,7 +328,10 @@ func decodeLegacyStoredReceiptRLP(r *ReceiptForStorage, blob []byte) error {
r.TxHash = stored.TxHash
r.ContractAddress = stored.ContractAddress
r.GasUsed = stored.GasUsed
r.Logs = stored.Logs
r.Logs = make([]*Log, len(stored.Logs))
for i, log := range stored.Logs {
r.Logs[i] = (*Log)(log)
}
r.Bloom = CreateBloom(Receipts{(*Receipt)(r)})

return nil
Expand Down

0 comments on commit b5ff4fc

Please sign in to comment.