Skip to content

Commit

Permalink
core/types: add legacy receipt deserialization (ethereum#28)
Browse files Browse the repository at this point in the history
Receipts in `l2geth` are stored with L1 fee data to prevent
the need of having an archive node for historical L1 fee data.

https://github.com/ethereum-optimism/optimism/blob/91a80d287237032a47d68d8809b1dc67ad15293f/l2geth/core/types/receipt.go#L247

This ports the deserialization of the legacy `l2geth` style
serialization to `op-geth`. This is required for nodes that upgrade
using the db migration tool.
  • Loading branch information
tynes authored Nov 30, 2022
1 parent 9facc18 commit 62fc04f
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ type v3StoredReceiptRLP struct {
GasUsed uint64
}

// LegacyOptimismStoredReceiptRLP is the pre bedrock storage encoding of a
// receipt. It will only exist in the database if it was migrated using the
// migration tool. Nodes that sync using snap-sync will not have any of these
// entries.
type LegacyOptimismStoredReceiptRLP struct {
PostStateOrStatus []byte
CumulativeGasUsed uint64
Logs []*LogForStorage
L1GasUsed *big.Int
L1GasPrice *big.Int
L1Fee *big.Int
FeeScalar string
}

// NewReceipt creates a barebone transaction receipt, copying the init fields.
// Deprecated: create receipts using a struct literal instead.
func NewReceipt(root []byte, failed bool, cumulativeGasUsed uint64) *Receipt {
Expand Down Expand Up @@ -315,12 +329,48 @@ func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error {
if err := decodeStoredReceiptRLP(r, blob); err == nil {
return nil
}
if err := decodeLegacyOptimismReceiptRLP(r, blob); err == nil {
return nil
}
if err := decodeV3StoredReceiptRLP(r, blob); err == nil {
return nil
}

return decodeV4StoredReceiptRLP(r, blob)
}

func decodeLegacyOptimismReceiptRLP(r *ReceiptForStorage, blob []byte) error {
var stored LegacyOptimismStoredReceiptRLP
if err := rlp.DecodeBytes(blob, &stored); err != nil {
return err
}
if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil {
return err
}
r.CumulativeGasUsed = stored.CumulativeGasUsed
r.Logs = make([]*Log, len(stored.Logs))
for i, log := range stored.Logs {
r.Logs[i] = (*Log)(log)
}
r.Bloom = CreateBloom(Receipts{(*Receipt)(r)})

// UsingOVM
scalar := new(big.Float)
if stored.FeeScalar != "" {
var ok bool
scalar, ok = scalar.SetString(stored.FeeScalar)
if !ok {
return errors.New("cannot parse fee scalar")
}
}
r.L1GasUsed = stored.L1GasUsed
r.L1GasPrice = stored.L1GasPrice
r.L1Fee = stored.L1Fee
r.FeeScalar = scalar

return nil
}

func decodeStoredReceiptRLP(r *ReceiptForStorage, blob []byte) error {
var stored storedReceiptRLP
if err := rlp.DecodeBytes(blob, &stored); err != nil {
Expand Down

0 comments on commit 62fc04f

Please sign in to comment.