Skip to content

Commit fd48e83

Browse files
ramilexen0cte
andcommitted
EIP1559 (#31)
* change transaction options, gasPremium to maxMinerBribePerGas * update fields for eth api * Update cost calculation for transaction * Change gasPremium to maxMinerBribePerGas * update baseFee calculation * fix gasprice calculation for StateTransition * fix fields for RPCTransaction * fix CalcBaseFee * fix transaction fields for test * add additional func computeBaseFee * fix baseFee values for TestCalcBaseFee * Fix transaction cost calculation * add infomation about eip version * use camelCase-style for json tags * remove eip info from "version"-command * update VesionMeta Co-authored-by: Ilnur Galiev <[email protected]>
1 parent e799153 commit fd48e83

File tree

25 files changed

+396
-397
lines changed

25 files changed

+396
-397
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -555,23 +555,23 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
555555
// If we have finalized EIP1559 and do not have a properly formed EIP1559 trx, sub in default values
556556
eip1559 := b.config.IsEIP1559(block.Number())
557557
eip1559Finalized := b.config.IsEIP1559Finalized(block.Number())
558-
if eip1559Finalized && (call.GasPremium == nil || call.FeeCap == nil || call.GasPrice != nil) {
559-
call.GasPremium = big.NewInt(1)
560-
call.FeeCap = big.NewInt(10)
558+
if eip1559Finalized && (call.MaxMinerBribePerGas == nil || call.FeeCapPerGas == nil || call.GasPrice != nil) {
559+
call.MaxMinerBribePerGas = big.NewInt(1)
560+
call.FeeCapPerGas = big.NewInt(10)
561561
call.GasPrice = nil
562562
}
563563
// If we have not activated EIP1559 and do not have a properly formed legacy trx, sub in default values
564-
if !eip1559 && (call.GasPremium != nil || call.FeeCap != nil || call.GasPrice == nil) {
565-
call.GasPremium = nil
566-
call.FeeCap = nil
564+
if !eip1559 && (call.MaxMinerBribePerGas != nil || call.FeeCapPerGas != nil || call.GasPrice == nil) {
565+
call.MaxMinerBribePerGas = nil
566+
call.FeeCapPerGas = nil
567567
call.GasPrice = big.NewInt(1)
568568
}
569569
// If we are in between activation and finalization
570570
if eip1559 && !eip1559Finalized {
571571
// and we have neither a properly formed legacy or EIP1559 transaction, sub in default legacy values
572-
if (call.GasPremium == nil || call.FeeCap == nil && call.GasPrice == nil) || (call.GasPremium != nil || call.FeeCap != nil && call.GasPrice != nil) {
573-
call.GasPremium = nil
574-
call.FeeCap = nil
572+
if (call.MaxMinerBribePerGas == nil || call.FeeCapPerGas == nil && call.GasPrice == nil) || (call.MaxMinerBribePerGas != nil || call.FeeCapPerGas != nil && call.GasPrice != nil) {
573+
call.MaxMinerBribePerGas = nil
574+
call.FeeCapPerGas = nil
575575
call.GasPrice = big.NewInt(1)
576576
}
577577
}
@@ -612,16 +612,16 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
612612
if eip1559 && b.pendingBlock.BaseFee() == nil {
613613
return core.ErrNoBaseFee
614614
}
615-
if eip1559Finalized && (tx.GasPremium() == nil || tx.FeeCap() == nil || tx.GasPrice() != nil) {
615+
if eip1559Finalized && (tx.MaxMinerBribe() == nil || tx.FeeCap() == nil || tx.GasPrice() != nil) {
616616
return core.ErrTxNotEIP1559
617617
}
618-
if !eip1559 && (tx.GasPremium() != nil || tx.FeeCap() != nil || tx.GasPrice() == nil) {
618+
if !eip1559 && (tx.MaxMinerBribe() != nil || tx.FeeCap() != nil || tx.GasPrice() == nil) {
619619
return core.ErrTxIsEIP1559
620620
}
621-
if tx.GasPrice() != nil && (tx.GasPremium() != nil || tx.FeeCap() != nil) {
621+
if tx.GasPrice() != nil && (tx.MaxMinerBribe() != nil || tx.FeeCap() != nil) {
622622
return core.ErrTxSetsLegacyAndEIP1559Fields
623623
}
624-
if tx.GasPrice() == nil && (tx.GasPremium() == nil || tx.FeeCap() == nil) {
624+
if tx.GasPrice() == nil && (tx.MaxMinerBribe() == nil || tx.FeeCap() == nil) {
625625
return core.ErrMissingGasFields
626626
}
627627

@@ -773,16 +773,16 @@ type callMsg struct {
773773
ethereum.CallMsg
774774
}
775775

776-
func (m callMsg) From() common.Address { return m.CallMsg.From }
777-
func (m callMsg) Nonce() uint64 { return 0 }
778-
func (m callMsg) CheckNonce() bool { return false }
779-
func (m callMsg) To() *common.Address { return m.CallMsg.To }
780-
func (m callMsg) GasPrice() *big.Int { return m.CallMsg.GasPrice }
781-
func (m callMsg) Gas() uint64 { return m.CallMsg.Gas }
782-
func (m callMsg) Value() *big.Int { return m.CallMsg.Value }
783-
func (m callMsg) Data() []byte { return m.CallMsg.Data }
784-
func (m callMsg) GasPremium() *big.Int { return m.CallMsg.GasPremium }
785-
func (m callMsg) FeeCap() *big.Int { return m.CallMsg.FeeCap }
776+
func (m callmsg) From() common.Address { return m.CallMsg.From }
777+
func (m callmsg) Nonce() uint64 { return 0 }
778+
func (m callmsg) CheckNonce() bool { return false }
779+
func (m callmsg) To() *common.Address { return m.CallMsg.To }
780+
func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice }
781+
func (m callmsg) Gas() uint64 { return m.CallMsg.Gas }
782+
func (m callmsg) Value() *big.Int { return m.CallMsg.Value }
783+
func (m callmsg) Data() []byte { return m.CallMsg.Data }
784+
func (m callmsg) MaxMinerBribe() *big.Int { return m.CallMsg.MaxMinerBribePerGas }
785+
func (m callmsg) FeeCap() *big.Int { return m.CallMsg.FeeCapPerGas }
786786

787787
// filterBackend implements filters.Backend to support filtering for logs without
788788
// taking bloom-bits acceleration structures into account.

accounts/abi/bind/base.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ type TransactOpts struct {
5353
GasLimit uint64 // Gas limit to set for the transaction execution (0 = estimate)
5454

5555
// If GasPrice, GasPremium, and FeeCap are all nil then we defer to the gas price oracle
56-
GasPrice *big.Int // Gas price to use for the transaction execution
57-
GasPremium *big.Int // Gas premium (tip) to use for EIP1559 transaction execution (
58-
FeeCap *big.Int // Fee cap to use for EIP1559 transaction execution
56+
GasPrice *big.Int // Gas price to use for the transaction execution
57+
MaxMinerBribePerGas *big.Int // Gas premium (tip) to use for EIP1559 transaction execution (
58+
FeeCapPerGas *big.Int // Fee cap to use for EIP1559 transaction execution
5959

6060
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
6161
}
@@ -227,7 +227,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
227227
}
228228
// Figure out the gas allowance and gas price values
229229
gasPrice := opts.GasPrice
230-
if gasPrice == nil && opts.FeeCap == nil && opts.GasPremium == nil {
230+
if gasPrice == nil && opts.FeeCapPerGas == nil && opts.MaxMinerBribePerGas == nil {
231231
gasPrice, err = c.transactor.SuggestGasPrice(ensureContext(opts.Context))
232232
if err != nil {
233233
return nil, fmt.Errorf("failed to suggest gas price: %v", err)
@@ -245,13 +245,13 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
245245
}
246246
// If the contract surely has code (or code is not needed), estimate the transaction
247247
msg := ethereum.CallMsg{
248-
From: opts.From,
249-
To: contract,
250-
GasPrice: gasPrice,
251-
Value: value,
252-
Data: input,
253-
GasPremium: opts.GasPremium,
254-
FeeCap: opts.FeeCap,
248+
From: opts.From,
249+
To: contract,
250+
GasPrice: gasPrice,
251+
Value: value,
252+
Data: input,
253+
MaxMinerBribePerGas: opts.MaxMinerBribePerGas,
254+
FeeCapPerGas: opts.FeeCapPerGas,
255255
}
256256
gasLimit, err = c.transactor.EstimateGas(ensureContext(opts.Context), msg)
257257
if err != nil {
@@ -261,9 +261,9 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
261261
// Create the transaction, sign it and schedule it for execution
262262
var rawTx *types.Transaction
263263
if contract == nil {
264-
rawTx = types.NewContractCreation(nonce, value, gasLimit, gasPrice, input, opts.GasPremium, opts.FeeCap)
264+
rawTx = types.NewContractCreation(nonce, value, gasLimit, gasPrice, input, opts.MaxMinerBribePerGas, opts.FeeCapPerGas)
265265
} else {
266-
rawTx = types.NewTransaction(nonce, c.address, value, gasLimit, gasPrice, input, opts.GasPremium, opts.FeeCap)
266+
rawTx = types.NewTransaction(nonce, c.address, value, gasLimit, gasPrice, input, opts.MaxMinerBribePerGas, opts.FeeCapPerGas)
267267
}
268268
if opts.Signer == nil {
269269
return nil, errors.New("no signer to authorize the transaction with")

accounts/external/backend.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,15 @@ func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transactio
204204
to = &t
205205
}
206206
args := &core.SendTxArgs{
207-
Data: &data,
208-
Nonce: hexutil.Uint64(tx.Nonce()),
209-
Value: hexutil.Big(*tx.Value()),
210-
Gas: hexutil.Uint64(tx.Gas()),
211-
GasPrice: (*hexutil.Big)(tx.GasPrice()),
212-
GasPremium: (*hexutil.Big)(tx.GasPremium()),
213-
FeeCap: (*hexutil.Big)(tx.FeeCap()),
214-
To: to,
215-
From: common.NewMixedcaseAddress(account.Address),
207+
Data: &data,
208+
Nonce: hexutil.Uint64(tx.Nonce()),
209+
Value: hexutil.Big(*tx.Value()),
210+
Gas: hexutil.Uint64(tx.Gas()),
211+
GasPrice: (*hexutil.Big)(tx.GasPrice()),
212+
MaxMinerBribePerGas: (*hexutil.Big)(tx.MaxMinerBribe()),
213+
FeeCapPerGas: (*hexutil.Big)(tx.FeeCap()),
214+
To: to,
215+
From: common.NewMixedcaseAddress(account.Address),
216216
}
217217
var res signTransactionResult
218218
if err := api.client.Call(&res, "account_signTransaction", args); err != nil {

consensus/misc/basefee.go

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package misc
1818

1919
import (
2020
"errors"
21+
2122
"math/big"
2223

2324
"github.com/ethereum/go-ethereum/common"
@@ -59,6 +60,37 @@ func VerifyEIP1559BaseFee(config *params.ChainConfig, header, parent *types.Head
5960
return nil
6061
}
6162

63+
func computeBaseFee(pBaseFee *big.Int, pGasUsed, pGasTarget, denominator uint64) *big.Int {
64+
var baseFee *big.Int
65+
if pGasUsed == pGasTarget {
66+
baseFee = new(big.Int).Set(pBaseFee)
67+
} else if pGasUsed > pGasTarget {
68+
gasDelta := big.NewInt(int64(pGasUsed) - int64(pGasTarget))
69+
feeDelta := math.BigMax(
70+
new(big.Int).Div(
71+
new(big.Int).Mul(pBaseFee, gasDelta),
72+
new(big.Int).Mul(
73+
new(big.Int).SetUint64(pGasTarget),
74+
new(big.Int).SetUint64(denominator),
75+
),
76+
),
77+
big.NewInt(1),
78+
)
79+
baseFee = new(big.Int).Add(pBaseFee, feeDelta)
80+
} else {
81+
gasDelta := big.NewInt(int64(pGasTarget) - int64(pGasUsed))
82+
feeDelta := new(big.Int).Div(
83+
new(big.Int).Mul(pBaseFee, gasDelta),
84+
new(big.Int).Mul(
85+
new(big.Int).SetUint64(pGasTarget),
86+
new(big.Int).SetUint64(denominator),
87+
),
88+
)
89+
baseFee = new(big.Int).Sub(pBaseFee, feeDelta)
90+
}
91+
return baseFee
92+
}
93+
6294
// CalcBaseFee returns the baseFee for the current block provided the parent header and config parameters
6395
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
6496
height := new(big.Int).Add(parent.Number, common.Big1)
@@ -73,42 +105,12 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
73105
return new(big.Int).SetUint64(config.EIP1559.InitialBaseFee)
74106
}
75107

76-
parentBaseFee := parent.BaseFee
77-
parentBlockGasUsed := new(big.Int).SetUint64(parent.GasUsed)
78-
targetGasUsed := new(big.Int).SetUint64(parent.GasLimit)
79-
baseFeeMaxChangeDenominator := new(big.Int).SetUint64(config.EIP1559.EIP1559BaseFeeMaxChangeDenominator)
80-
81-
cmp := parentBlockGasUsed.Cmp(targetGasUsed)
82-
83-
if cmp == 0 {
84-
return targetGasUsed
85-
}
86-
87-
if cmp > 0 {
88-
gasDelta := new(big.Int).Sub(parentBlockGasUsed, targetGasUsed)
89-
feeDelta := math.BigMax(
90-
new(big.Int).Div(
91-
new(big.Int).Div(
92-
new(big.Int).Mul(parentBaseFee, gasDelta),
93-
targetGasUsed,
94-
),
95-
baseFeeMaxChangeDenominator,
96-
),
97-
common.Big1,
98-
)
99-
return new(big.Int).Add(parentBaseFee, feeDelta)
100-
}
101-
102-
gasDelta := new(big.Int).Sub(targetGasUsed, parentBlockGasUsed)
103-
feeDelta := new(big.Int).Div(
104-
new(big.Int).Div(
105-
new(big.Int).Mul(parentBaseFee, gasDelta),
106-
targetGasUsed,
107-
),
108-
baseFeeMaxChangeDenominator,
108+
return computeBaseFee(
109+
parent.BaseFee,
110+
parent.GasUsed,
111+
parent.GasLimit,
112+
config.EIP1559.EIP1559BaseFeeMaxChangeDenominator,
109113
)
110-
111-
return new(big.Int).Sub(parentBaseFee, feeDelta)
112114
}
113115

114116
// CalcEIP1559GasTarget returns the EIP1559GasTarget at the current height and header.GasLimit

consensus/misc/basefee_test.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func TestCalcBaseFee(t *testing.T) {
155155
big.NewInt(1000000000),
156156
1000000,
157157
10000000,
158-
big.NewInt(1125000000),
158+
big.NewInt(2125000000),
159159
},
160160
{
161161
params.EIP1559ChainConfig,
@@ -165,7 +165,7 @@ func TestCalcBaseFee(t *testing.T) {
165165
big.NewInt(1000000000),
166166
500000,
167167
10000000,
168-
big.NewInt(1125000000),
168+
big.NewInt(3375000000),
169169
},
170170
{
171171
params.EIP1559ChainConfig,
@@ -175,7 +175,7 @@ func TestCalcBaseFee(t *testing.T) {
175175
big.NewInt(1000000000),
176176
1000000,
177177
10000000,
178-
big.NewInt(1125000000),
178+
big.NewInt(2125000000),
179179
},
180180
{
181181
params.EIP1559ChainConfig,
@@ -217,15 +217,15 @@ func TestCalcBaseFee(t *testing.T) {
217217
10000000,
218218
big.NewInt(1013888888),
219219
},
220-
{
220+
{ // 10
221221
params.EIP1559ChainConfig,
222222
big.NewInt(1000),
223223
1000,
224224
big.NewInt(2000),
225225
big.NewInt(1000000000),
226226
10000000,
227227
10000000,
228-
big.NewInt(999999999), // baseFee diff is -1 when usage == target
228+
big.NewInt(1000000000), // baseFee diff is -1 when usage == target
229229
},
230230
{
231231
params.EIP1559ChainConfig,
@@ -235,7 +235,7 @@ func TestCalcBaseFee(t *testing.T) {
235235
big.NewInt(1000000000),
236236
11000000,
237237
10000000,
238-
big.NewInt(988636363),
238+
big.NewInt(988636364),
239239
},
240240
{
241241
params.EIP1559ChainConfig,
@@ -245,7 +245,7 @@ func TestCalcBaseFee(t *testing.T) {
245245
big.NewInt(900000000),
246246
1000000,
247247
10000000,
248-
big.NewInt(1012500000),
248+
big.NewInt(1912500000),
249249
},
250250
{
251251
params.EIP1559ChainConfig,
@@ -255,7 +255,7 @@ func TestCalcBaseFee(t *testing.T) {
255255
big.NewInt(1100000000),
256256
1000000,
257257
10000000,
258-
big.NewInt(1237500000),
258+
big.NewInt(2337500000),
259259
},
260260
{
261261
params.EIP1559ChainConfig,
@@ -265,7 +265,7 @@ func TestCalcBaseFee(t *testing.T) {
265265
big.NewInt(1200000000),
266266
1000000,
267267
10000000,
268-
big.NewInt(1350000000),
268+
big.NewInt(2550000000),
269269
},
270270
{
271271
params.EIP1559ChainConfig,
@@ -328,7 +328,7 @@ func TestCalcBaseFee(t *testing.T) {
328328
big.NewInt(0),
329329
1000000000000000,
330330
1,
331-
big.NewInt(1),
331+
big.NewInt(0),
332332
},
333333
// parent gas usage == parent gas limit
334334
// parent baseFee == 0
@@ -355,11 +355,8 @@ func TestCalcBaseFee(t *testing.T) {
355355
big.NewInt(1),
356356
1,
357357
1000000000000000,
358-
big.NewInt(2),
358+
big.NewInt(125000000000000),
359359
},
360-
// parent gas usage <<<< parent gas limit
361-
// parent baseFee == 1
362-
// as expected, decrement by 1
363360
{
364361
params.EIP1559ChainConfig,
365362
big.NewInt(1000),
@@ -368,11 +365,8 @@ func TestCalcBaseFee(t *testing.T) {
368365
big.NewInt(1),
369366
1000000000000000,
370367
1,
371-
big.NewInt(0),
368+
big.NewInt(1),
372369
},
373-
// parent gas usage == parent gas limit
374-
// parent baseFee == 1
375-
// as expected, decrement by 1 when gas usage equals the gas target
376370
{
377371
params.EIP1559ChainConfig,
378372
big.NewInt(1000),
@@ -381,7 +375,7 @@ func TestCalcBaseFee(t *testing.T) {
381375
big.NewInt(1),
382376
1,
383377
1,
384-
big.NewInt(0),
378+
big.NewInt(1),
385379
},
386380
}
387381
for i, test := range testConditions {

0 commit comments

Comments
 (0)