Skip to content

Commit a56f3dc

Browse files
karalabefjl
authored andcommitted
core, ethclient: implement Metropolis EIP 98 (#14750)
Implements ethereum/EIPs#98
1 parent 4735930 commit a56f3dc

4 files changed

Lines changed: 76 additions & 20 deletions

File tree

‎core/state_processor.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,17 @@ func ApplyTransaction(config *params.ChainConfig, bc *BlockChain, author *common
104104
}
105105

106106
// Update the state with pending changes
107+
var root []byte
108+
if config.IsMetropolis(header.Number) {
109+
statedb.Finalise()
110+
} else {
111+
root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
112+
}
107113
usedGas.Add(usedGas, gas)
114+
108115
// Create a new receipt for the transaction, storing the intermediate root and gas used by the tx
109116
// based on the eip phase, we're passing wether the root touch-delete accounts.
110-
root := statedb.IntermediateRoot(config.IsEIP158(header.Number))
111-
receipt := types.NewReceipt(root.Bytes(), usedGas)
117+
receipt := types.NewReceipt(root, usedGas)
112118
receipt.TxHash = tx.Hash()
113119
receipt.GasUsed = new(big.Int).Set(gas)
114120
// if the transaction created a contract, store the creation address in the receipt.

‎core/types/gen_receipt_json.go‎

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/types/receipt.go‎

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
// Receipt represents the results of a transaction.
3232
type Receipt struct {
3333
// Consensus fields
34-
PostState []byte `json:"root" gencodec:"required"`
34+
PostState []byte `json:"root"`
3535
CumulativeGasUsed *big.Int `json:"cumulativeGasUsed" gencodec:"required"`
3636
Bloom Bloom `json:"logsBloom" gencodec:"required"`
3737
Logs []*Log `json:"logs" gencodec:"required"`
@@ -48,35 +48,88 @@ type receiptMarshaling struct {
4848
GasUsed *hexutil.Big
4949
}
5050

51+
// homesteadReceiptRLP contains the receipt's Homestead consensus fields, used
52+
// during RLP serialization.
53+
type homesteadReceiptRLP struct {
54+
PostState []byte
55+
CumulativeGasUsed *big.Int
56+
Bloom Bloom
57+
Logs []*Log
58+
}
59+
60+
// metropolisReceiptRLP contains the receipt's Metropolis consensus fields, used
61+
// during RLP serialization.
62+
type metropolisReceiptRLP struct {
63+
CumulativeGasUsed *big.Int
64+
Bloom Bloom
65+
Logs []*Log
66+
}
67+
5168
// NewReceipt creates a barebone transaction receipt, copying the init fields.
5269
func NewReceipt(root []byte, cumulativeGasUsed *big.Int) *Receipt {
5370
return &Receipt{PostState: common.CopyBytes(root), CumulativeGasUsed: new(big.Int).Set(cumulativeGasUsed)}
5471
}
5572

5673
// EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt
57-
// into an RLP stream.
74+
// into an RLP stream. If no post state is present, metropolis fork is assumed.
5875
func (r *Receipt) EncodeRLP(w io.Writer) error {
59-
return rlp.Encode(w, []interface{}{r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs})
76+
if r.PostState == nil {
77+
return rlp.Encode(w, &metropolisReceiptRLP{r.CumulativeGasUsed, r.Bloom, r.Logs})
78+
}
79+
return rlp.Encode(w, &homesteadReceiptRLP{r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs})
6080
}
6181

6282
// DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
6383
// from an RLP stream.
6484
func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
65-
var receipt struct {
66-
PostState []byte
67-
CumulativeGasUsed *big.Int
68-
Bloom Bloom
69-
Logs []*Log
85+
// Load the raw bytes since we have multiple possible formats
86+
raw, err := s.Raw()
87+
if err != nil {
88+
return err
7089
}
71-
if err := s.Decode(&receipt); err != nil {
90+
list, _, err := rlp.SplitList(raw)
91+
if err != nil {
7292
return err
7393
}
74-
r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs = receipt.PostState, receipt.CumulativeGasUsed, receipt.Bloom, receipt.Logs
75-
return nil
94+
items, err := rlp.CountValues(list)
95+
if err != nil {
96+
return err
97+
}
98+
// Deserialize based on the number of content items
99+
switch items {
100+
case 3:
101+
// Metropolis receipts have 3 components
102+
var metro metropolisReceiptRLP
103+
if err := rlp.DecodeBytes(raw, &metro); err != nil {
104+
return err
105+
}
106+
r.CumulativeGasUsed = metro.CumulativeGasUsed
107+
r.Bloom = metro.Bloom
108+
r.Logs = metro.Logs
109+
return nil
110+
111+
case 4:
112+
// Homestead receipts have 4 components
113+
var home homesteadReceiptRLP
114+
if err := rlp.DecodeBytes(raw, &home); err != nil {
115+
return err
116+
}
117+
r.PostState = home.PostState[:]
118+
r.CumulativeGasUsed = home.CumulativeGasUsed
119+
r.Bloom = home.Bloom
120+
r.Logs = home.Logs
121+
return nil
122+
123+
default:
124+
return fmt.Errorf("invalid receipt components: %v", items)
125+
}
76126
}
77127

78128
// String implements the Stringer interface.
79129
func (r *Receipt) String() string {
130+
if r.PostState == nil {
131+
return fmt.Sprintf("receipt{cgas=%v bloom=%x logs=%v}", r.CumulativeGasUsed, r.Bloom, r.Logs)
132+
}
80133
return fmt.Sprintf("receipt{med=%x cgas=%v bloom=%x logs=%v}", r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs)
81134
}
82135

‎ethclient/ethclient.go‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ func (ec *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*
203203
if err == nil {
204204
if r == nil {
205205
return nil, ethereum.NotFound
206-
} else if len(r.PostState) == 0 {
207-
return nil, fmt.Errorf("server returned receipt without post state")
208206
}
209207
}
210208
return r, err

0 commit comments

Comments
 (0)