Skip to content

Commit bfd1c0c

Browse files
committed
core, eth, internal, les: RPC methods and fields for EIP 1559 (ethereum#22964)
1 parent 4f0317c commit bfd1c0c

File tree

15 files changed

+534
-196
lines changed

15 files changed

+534
-196
lines changed

core/types/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type Header struct {
8383
Penalties []byte `json:"penalties" gencodec:"required"`
8484

8585
// BaseFee was added by EIP-1559 and is ignored in legacy headers.
86-
BaseFee *big.Int `json:"baseFee" rlp:"optional"`
86+
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
8787
}
8888

8989
// field type overrides for gencodec

core/types/gen_header_json.go

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

core/types/transaction_marshalling.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Copyright 2021 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
117
package types
218

319
import (
@@ -16,8 +32,6 @@ type txJSON struct {
1632
// Common transaction fields:
1733
Nonce *hexutil.Uint64 `json:"nonce"`
1834
GasPrice *hexutil.Big `json:"gasPrice"`
19-
FeeCap *hexutil.Big `json:"feeCap"`
20-
Tip *hexutil.Big `json:"tip"`
2135
MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"`
2236
MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"`
2337
Gas *hexutil.Uint64 `json:"gas"`
@@ -72,8 +86,8 @@ func (t *Transaction) MarshalJSON() ([]byte, error) {
7286
enc.AccessList = &tx.AccessList
7387
enc.Nonce = (*hexutil.Uint64)(&tx.Nonce)
7488
enc.Gas = (*hexutil.Uint64)(&tx.Gas)
75-
enc.FeeCap = (*hexutil.Big)(tx.FeeCap)
76-
enc.Tip = (*hexutil.Big)(tx.Tip)
89+
enc.MaxFeePerGas = (*hexutil.Big)(tx.FeeCap)
90+
enc.MaxPriorityFeePerGas = (*hexutil.Big)(tx.Tip)
7791
enc.Value = (*hexutil.Big)(tx.Value)
7892
enc.Data = (*hexutil.Bytes)(&tx.Data)
7993
enc.To = t.To()
@@ -210,26 +224,14 @@ func (t *Transaction) UnmarshalJSON(input []byte) error {
210224
return errors.New("missing required field 'nonce' in transaction")
211225
}
212226
itx.Nonce = uint64(*dec.Nonce)
213-
switch {
214-
case dec.Tip == nil && dec.MaxPriorityFeePerGas == nil:
215-
return errors.New("at least one of 'tip' or 'maxPriorityFeePerGas' must be defined")
216-
case dec.Tip != nil && dec.MaxPriorityFeePerGas != nil:
217-
return errors.New("only one of 'tip' or 'maxPriorityFeePerGas' may be defined")
218-
case dec.Tip != nil && dec.MaxPriorityFeePerGas == nil:
219-
itx.Tip = (*big.Int)(dec.Tip)
220-
case dec.Tip == nil && dec.MaxPriorityFeePerGas != nil:
221-
itx.Tip = (*big.Int)(dec.MaxPriorityFeePerGas)
222-
}
223-
switch {
224-
case dec.FeeCap == nil && dec.MaxFeePerGas == nil:
225-
return errors.New("at least one of 'feeCap' or 'maxFeePerGas' must be defined")
226-
case dec.FeeCap != nil && dec.MaxFeePerGas != nil:
227-
return errors.New("only one of 'feeCap' or 'maxFeePerGas' may be defined")
228-
case dec.FeeCap != nil && dec.MaxFeePerGas == nil:
229-
itx.FeeCap = (*big.Int)(dec.FeeCap)
230-
case dec.FeeCap == nil && dec.MaxFeePerGas != nil:
231-
itx.FeeCap = (*big.Int)(dec.MaxFeePerGas)
227+
if dec.MaxPriorityFeePerGas == nil {
228+
return errors.New("missing required field 'maxPriorityFeePerGas' for txdata")
229+
}
230+
itx.Tip = (*big.Int)(dec.MaxPriorityFeePerGas)
231+
if dec.MaxFeePerGas == nil {
232+
return errors.New("missing required field 'maxFeePerGas' for txdata")
232233
}
234+
itx.FeeCap = (*big.Int)(dec.MaxFeePerGas)
233235
if dec.Gas == nil {
234236
return errors.New("missing required field 'gas' for txdata")
235237
}

eth/api_backend.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ func (b *EthApiBackend) ProtocolVersion() int {
344344
return b.eth.EthVersion()
345345
}
346346

347-
func (b *EthApiBackend) SuggestPrice(ctx context.Context) (*big.Int, error) {
348-
return b.gpo.SuggestPrice(ctx)
347+
func (b *EthApiBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
348+
return b.gpo.SuggestTipCap(ctx)
349349
}
350350

351351
func (b *EthApiBackend) ChainDb() ethdb.Database {
@@ -393,6 +393,10 @@ func (b *EthApiBackend) GetEngine() consensus.Engine {
393393
return b.eth.engine
394394
}
395395

396+
func (b *EthApiBackend) CurrentHeader() *types.Header {
397+
return b.eth.blockchain.CurrentHeader()
398+
}
399+
396400
func (b *EthApiBackend) StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, checkLive bool) (*state.StateDB, error) {
397401
return b.eth.stateAtBlock(block, reexec, base, checkLive)
398402
}

eth/api_tracer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,10 @@ func (api *PrivateDebugAPI) TraceCall(ctx context.Context, args ethapi.Transacti
691691
}
692692
}
693693
// Execute the trace
694-
msg := args.ToMessage(api.eth.ApiBackend, block.Number(), api.eth.ApiBackend.RPCGasCap())
694+
msg, err := args.ToMessage(api.eth.ApiBackend, block.Number(), api.eth.ApiBackend.RPCGasCap(), block.BaseFee())
695+
if err != nil {
696+
return nil, err
697+
}
695698
vmctx := core.NewEVMBlockContext(block.Header(), api.eth.blockchain, nil)
696699
var traceConfig *TraceConfig
697700
if config != nil {

eth/gasprice/gasprice.go

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ import (
3131

3232
const sampleNumber = 3 // Number of transactions sampled in a block
3333

34-
var DefaultMaxPrice = big.NewInt(500 * params.GWei)
35-
var DefaultIgnorePrice = big.NewInt(2 * params.Wei)
34+
var (
35+
DefaultMaxPrice = big.NewInt(500 * params.GWei)
36+
DefaultIgnorePrice = big.NewInt(2 * params.Wei)
37+
)
3638

3739
type Config struct {
3840
Blocks int
@@ -103,8 +105,13 @@ func NewOracle(backend OracleBackend, params Config) *Oracle {
103105
}
104106
}
105107

106-
// SuggestPrice returns the recommended gas price.
107-
func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
108+
// SuggestTipCap returns a tip cap so that newly created transaction can have a
109+
// very high chance to be included in the following blocks.
110+
//
111+
// Note, for legacy transactions and the legacy eth_gasPrice RPC call, it will be
112+
// necessary to add the basefee to the returned number to fall back to the legacy
113+
// behavior.
114+
func (gpo *Oracle) SuggestTipCap(ctx context.Context) (*big.Int, error) {
108115
head, _ := gpo.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
109116
headHash := head.Hash()
110117

@@ -113,7 +120,7 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
113120
lastHead, lastPrice := gpo.lastHead, gpo.lastPrice
114121
gpo.cacheLock.RUnlock()
115122
if headHash == lastHead {
116-
return lastPrice, nil
123+
return new(big.Int).Set(lastPrice), nil
117124
}
118125
gpo.fetchLock.Lock()
119126
defer gpo.fetchLock.Unlock()
@@ -123,17 +130,17 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
123130
lastHead, lastPrice = gpo.lastHead, gpo.lastPrice
124131
gpo.cacheLock.RUnlock()
125132
if headHash == lastHead {
126-
return lastPrice, nil
133+
return new(big.Int).Set(lastPrice), nil
127134
}
128135
var (
129136
sent, exp int
130137
number = head.Number.Uint64()
131-
result = make(chan getBlockPricesResult, gpo.checkBlocks)
138+
result = make(chan results, gpo.checkBlocks)
132139
quit = make(chan struct{})
133-
txPrices []*big.Int
140+
results []*big.Int
134141
)
135142
for sent < gpo.checkBlocks && number > 0 {
136-
go gpo.getBlockPrices(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(number))), number, sampleNumber, gpo.ignorePrice, result, quit)
143+
go gpo.getBlockValues(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(number))), number, sampleNumber, gpo.ignorePrice, result, quit)
137144
sent++
138145
exp++
139146
number--
@@ -142,93 +149,116 @@ func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
142149
res := <-result
143150
if res.err != nil {
144151
close(quit)
145-
return lastPrice, res.err
152+
return new(big.Int).Set(lastPrice), res.err
146153
}
147154
exp--
148155
// Nothing returned. There are two special cases here:
149156
// - The block is empty
150157
// - All the transactions included are sent by the miner itself.
151158
// In these cases, use the latest calculated price for samping.
152-
if len(res.prices) == 0 {
153-
res.prices = []*big.Int{lastPrice}
159+
if len(res.values) == 0 {
160+
res.values = []*big.Int{lastPrice}
154161
}
155162
// Besides, in order to collect enough data for sampling, if nothing
156163
// meaningful returned, try to query more blocks. But the maximum
157164
// is 2*checkBlocks.
158-
if len(res.prices) == 1 && len(txPrices)+1+exp < gpo.checkBlocks*2 && number > 0 {
159-
go gpo.getBlockPrices(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(number))), number, sampleNumber, gpo.ignorePrice, result, quit)
165+
if len(res.values) == 1 && len(results)+1+exp < gpo.checkBlocks*2 && number > 0 {
166+
go gpo.getBlockValues(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(number))), number, sampleNumber, gpo.ignorePrice, result, quit)
160167
sent++
161168
exp++
162169
number--
163170
}
164-
txPrices = append(txPrices, res.prices...)
171+
results = append(results, res.values...)
165172
}
166173
price := lastPrice
167-
if len(txPrices) > 0 {
168-
sort.Sort(bigIntArray(txPrices))
169-
price = txPrices[(len(txPrices)-1)*gpo.percentile/100]
174+
if len(results) > 0 {
175+
sort.Sort(bigIntArray(results))
176+
price = results[(len(results)-1)*gpo.percentile/100]
170177
}
171178
if price.Cmp(gpo.maxPrice) > 0 {
172179
price = new(big.Int).Set(gpo.maxPrice)
173180
}
174181

175-
// Check gas price min.
176-
minGasPrice := common.GetMinGasPrice(head.Number)
177-
if price.Cmp(minGasPrice) < 0 {
178-
price = new(big.Int).Set(minGasPrice)
182+
// Check min gas price for non-eip1559 block
183+
if head.BaseFee == nil {
184+
minGasPrice := common.GetMinGasPrice(head.Number)
185+
if price.Cmp(minGasPrice) < 0 {
186+
price = new(big.Int).Set(minGasPrice)
187+
}
179188
}
180189

181190
gpo.cacheLock.Lock()
182191
gpo.lastHead = headHash
183192
gpo.lastPrice = price
184193
gpo.cacheLock.Unlock()
185-
return price, nil
194+
195+
return new(big.Int).Set(price), nil
186196
}
187197

188-
type getBlockPricesResult struct {
189-
prices []*big.Int
198+
type results struct {
199+
values []*big.Int
190200
err error
191201
}
192202

193-
type transactionsByGasPrice []*types.Transaction
203+
type txSorter struct {
204+
txs []*types.Transaction
205+
baseFee *big.Int
206+
}
207+
208+
func newSorter(txs []*types.Transaction, baseFee *big.Int) *txSorter {
209+
return &txSorter{
210+
txs: txs,
211+
baseFee: baseFee,
212+
}
213+
}
194214

195-
func (t transactionsByGasPrice) Len() int { return len(t) }
196-
func (t transactionsByGasPrice) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
197-
func (t transactionsByGasPrice) Less(i, j int) bool { return t[i].FeeCapCmp(t[j]) < 0 }
215+
func (s *txSorter) Len() int { return len(s.txs) }
216+
func (s *txSorter) Swap(i, j int) {
217+
s.txs[i], s.txs[j] = s.txs[j], s.txs[i]
218+
}
219+
func (s *txSorter) Less(i, j int) bool {
220+
// It's okay to discard the error because a tx would never be
221+
// accepted into a block with an invalid effective tip.
222+
tip1, _ := s.txs[i].EffectiveTip(s.baseFee)
223+
tip2, _ := s.txs[j].EffectiveTip(s.baseFee)
224+
return tip1.Cmp(tip2) < 0
225+
}
198226

199227
// getBlockPrices calculates the lowest transaction gas price in a given block
200228
// and sends it to the result channel. If the block is empty or all transactions
201229
// are sent by the miner itself(it doesn't make any sense to include this kind of
202230
// transaction prices for sampling), nil gasprice is returned.
203-
func (gpo *Oracle) getBlockPrices(ctx context.Context, signer types.Signer, blockNum uint64, limit int, ignoreUnder *big.Int, result chan getBlockPricesResult, quit chan struct{}) {
231+
func (gpo *Oracle) getBlockValues(ctx context.Context, signer types.Signer, blockNum uint64, limit int, ignoreUnder *big.Int, result chan results, quit chan struct{}) {
204232
block, err := gpo.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum))
205233
if block == nil {
206234
select {
207-
case result <- getBlockPricesResult{nil, err}:
235+
case result <- results{nil, err}:
208236
case <-quit:
209237
}
210238
return
211239
}
212-
blockTxs := block.Transactions()
213-
txs := make([]*types.Transaction, len(blockTxs))
214-
copy(txs, blockTxs)
215-
sort.Sort(transactionsByGasPrice(txs))
240+
// Sort the transaction by effective tip in ascending sort.
241+
txs := make([]*types.Transaction, len(block.Transactions()))
242+
copy(txs, block.Transactions())
243+
sorter := newSorter(txs, block.BaseFee())
244+
sort.Sort(sorter)
216245

217246
var prices []*big.Int
218-
for _, tx := range txs {
219-
if ignoreUnder != nil && tx.GasPrice().Cmp(ignoreUnder) == -1 {
247+
for _, tx := range sorter.txs {
248+
tip, _ := tx.EffectiveTip(block.BaseFee())
249+
if ignoreUnder != nil && tip.Cmp(ignoreUnder) == -1 {
220250
continue
221251
}
222252
sender, err := types.Sender(signer, tx)
223253
if err == nil && sender != block.Coinbase() {
224-
prices = append(prices, tx.GasPrice())
254+
prices = append(prices, tip)
225255
if len(prices) >= limit {
226256
break
227257
}
228258
}
229259
}
230260
select {
231-
case result <- getBlockPricesResult{prices, nil}:
261+
case result <- results{prices, nil}:
232262
case <-quit:
233263
}
234264
}

0 commit comments

Comments
 (0)