From 1b59621f1b169e1bd2a5822830d30e89f4875bec Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Mon, 26 Oct 2020 14:53:52 +0300 Subject: [PATCH 01/16] change transaction options, gasPremium to maxMinerBribePerGas --- accounts/abi/bind/backends/simulated.go | 46 +++--- accounts/abi/bind/base.go | 26 ++-- accounts/external/backend.go | 18 +-- core/state_transition.go | 13 +- core/tx_list.go | 18 +-- core/tx_pool.go | 10 +- core/types/gen_tx_json.go | 60 ++++---- core/types/transaction.go | 189 ++++++++++++------------ core/types/transaction_signing.go | 8 +- eth/gasprice/gasprice.go | 12 +- ethclient/ethclient.go | 8 +- interfaces.go | 16 +- internal/ethapi/api.go | 70 ++++----- light/txpool.go | 14 +- miner/worker.go | 8 +- signer/core/api.go | 4 +- signer/core/types.go | 8 +- 17 files changed, 262 insertions(+), 266 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 9392e74bcf83..19764ea7fd92 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -555,23 +555,23 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM // If we have finalized EIP1559 and do not have a properly formed EIP1559 trx, sub in default values eip1559 := b.config.IsEIP1559(block.Number()) eip1559Finalized := b.config.IsEIP1559Finalized(block.Number()) - if eip1559Finalized && (call.GasPremium == nil || call.FeeCap == nil || call.GasPrice != nil) { - call.GasPremium = big.NewInt(1) - call.FeeCap = big.NewInt(10) + if eip1559Finalized && (call.MaxMinerBribePerGas == nil || call.FeeCapPerGas == nil || call.GasPrice != nil) { + call.MaxMinerBribePerGas = big.NewInt(1) + call.FeeCapPerGas = big.NewInt(10) call.GasPrice = nil } // If we have not activated EIP1559 and do not have a properly formed legacy trx, sub in default values - if !eip1559 && (call.GasPremium != nil || call.FeeCap != nil || call.GasPrice == nil) { - call.GasPremium = nil - call.FeeCap = nil + if !eip1559 && (call.MaxMinerBribePerGas != nil || call.FeeCapPerGas != nil || call.GasPrice == nil) { + call.MaxMinerBribePerGas = nil + call.FeeCapPerGas = nil call.GasPrice = big.NewInt(1) } // If we are in between activation and finalization if eip1559 && !eip1559Finalized { // and we have neither a properly formed legacy or EIP1559 transaction, sub in default legacy values - if (call.GasPremium == nil || call.FeeCap == nil && call.GasPrice == nil) || (call.GasPremium != nil || call.FeeCap != nil && call.GasPrice != nil) { - call.GasPremium = nil - call.FeeCap = nil + if (call.MaxMinerBribePerGas == nil || call.FeeCapPerGas == nil && call.GasPrice == nil) || (call.MaxMinerBribePerGas != nil || call.FeeCapPerGas != nil && call.GasPrice != nil) { + call.MaxMinerBribePerGas = nil + call.FeeCapPerGas = nil call.GasPrice = big.NewInt(1) } } @@ -612,16 +612,16 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa if eip1559 && b.pendingBlock.BaseFee() == nil { return core.ErrNoBaseFee } - if eip1559Finalized && (tx.GasPremium() == nil || tx.FeeCap() == nil || tx.GasPrice() != nil) { + if eip1559Finalized && (tx.MaxMinerBribe() == nil || tx.FeeCap() == nil || tx.GasPrice() != nil) { return core.ErrTxNotEIP1559 } - if !eip1559 && (tx.GasPremium() != nil || tx.FeeCap() != nil || tx.GasPrice() == nil) { + if !eip1559 && (tx.MaxMinerBribe() != nil || tx.FeeCap() != nil || tx.GasPrice() == nil) { return core.ErrTxIsEIP1559 } - if tx.GasPrice() != nil && (tx.GasPremium() != nil || tx.FeeCap() != nil) { + if tx.GasPrice() != nil && (tx.MaxMinerBribe() != nil || tx.FeeCap() != nil) { return core.ErrTxSetsLegacyAndEIP1559Fields } - if tx.GasPrice() == nil && (tx.GasPremium() == nil || tx.FeeCap() == nil) { + if tx.GasPrice() == nil && (tx.MaxMinerBribe() == nil || tx.FeeCap() == nil) { return core.ErrMissingGasFields } @@ -771,16 +771,16 @@ type callmsg struct { ethereum.CallMsg } -func (m callmsg) From() common.Address { return m.CallMsg.From } -func (m callmsg) Nonce() uint64 { return 0 } -func (m callmsg) CheckNonce() bool { return false } -func (m callmsg) To() *common.Address { return m.CallMsg.To } -func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice } -func (m callmsg) Gas() uint64 { return m.CallMsg.Gas } -func (m callmsg) Value() *big.Int { return m.CallMsg.Value } -func (m callmsg) Data() []byte { return m.CallMsg.Data } -func (m callmsg) GasPremium() *big.Int { return m.CallMsg.GasPremium } -func (m callmsg) FeeCap() *big.Int { return m.CallMsg.FeeCap } +func (m callmsg) From() common.Address { return m.CallMsg.From } +func (m callmsg) Nonce() uint64 { return 0 } +func (m callmsg) CheckNonce() bool { return false } +func (m callmsg) To() *common.Address { return m.CallMsg.To } +func (m callmsg) GasPrice() *big.Int { return m.CallMsg.GasPrice } +func (m callmsg) Gas() uint64 { return m.CallMsg.Gas } +func (m callmsg) Value() *big.Int { return m.CallMsg.Value } +func (m callmsg) Data() []byte { return m.CallMsg.Data } +func (m callmsg) MaxMinerBribe() *big.Int { return m.CallMsg.MaxMinerBribePerGas } +func (m callmsg) FeeCap() *big.Int { return m.CallMsg.FeeCapPerGas } // filterBackend implements filters.Backend to support filtering for logs without // taking bloom-bits acceleration structures into account. diff --git a/accounts/abi/bind/base.go b/accounts/abi/bind/base.go index eff108b33ce8..c9ce66c67cdf 100644 --- a/accounts/abi/bind/base.go +++ b/accounts/abi/bind/base.go @@ -53,9 +53,9 @@ type TransactOpts struct { GasLimit uint64 // Gas limit to set for the transaction execution (0 = estimate) // If GasPrice, GasPremium, and FeeCap are all nil then we defer to the gas price oracle - GasPrice *big.Int // Gas price to use for the transaction execution - GasPremium *big.Int // Gas premium (tip) to use for EIP1559 transaction execution ( - FeeCap *big.Int // Fee cap to use for EIP1559 transaction execution + GasPrice *big.Int // Gas price to use for the transaction execution + MaxMinerBribePerGas *big.Int // Gas premium (tip) to use for EIP1559 transaction execution ( + FeeCapPerGas *big.Int // Fee cap to use for EIP1559 transaction execution Context context.Context // Network context to support cancellation and timeouts (nil = no timeout) } @@ -217,7 +217,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i } // Figure out the gas allowance and gas price values gasPrice := opts.GasPrice - if gasPrice == nil && opts.FeeCap == nil && opts.GasPremium == nil { + if gasPrice == nil && opts.FeeCapPerGas == nil && opts.MaxMinerBribePerGas == nil { gasPrice, err = c.transactor.SuggestGasPrice(ensureContext(opts.Context)) if err != nil { return nil, fmt.Errorf("failed to suggest gas price: %v", err) @@ -235,13 +235,13 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i } // If the contract surely has code (or code is not needed), estimate the transaction msg := ethereum.CallMsg{ - From: opts.From, - To: contract, - GasPrice: gasPrice, - Value: value, - Data: input, - GasPremium: opts.GasPremium, - FeeCap: opts.FeeCap, + From: opts.From, + To: contract, + GasPrice: gasPrice, + Value: value, + Data: input, + MaxMinerBribePerGas: opts.MaxMinerBribePerGas, + FeeCapPerGas: opts.FeeCapPerGas, } gasLimit, err = c.transactor.EstimateGas(ensureContext(opts.Context), msg) if err != nil { @@ -251,9 +251,9 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i // Create the transaction, sign it and schedule it for execution var rawTx *types.Transaction if contract == nil { - rawTx = types.NewContractCreation(nonce, value, gasLimit, gasPrice, input, opts.GasPremium, opts.FeeCap) + rawTx = types.NewContractCreation(nonce, value, gasLimit, gasPrice, input, opts.MaxMinerBribePerGas, opts.FeeCapPerGas) } else { - rawTx = types.NewTransaction(nonce, c.address, value, gasLimit, gasPrice, input, opts.GasPremium, opts.FeeCap) + rawTx = types.NewTransaction(nonce, c.address, value, gasLimit, gasPrice, input, opts.MaxMinerBribePerGas, opts.FeeCapPerGas) } if opts.Signer == nil { return nil, errors.New("no signer to authorize the transaction with") diff --git a/accounts/external/backend.go b/accounts/external/backend.go index c8b33d79843c..4d01ade85a9b 100644 --- a/accounts/external/backend.go +++ b/accounts/external/backend.go @@ -200,15 +200,15 @@ func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transactio to = &t } args := &core.SendTxArgs{ - Data: &data, - Nonce: hexutil.Uint64(tx.Nonce()), - Value: hexutil.Big(*tx.Value()), - Gas: hexutil.Uint64(tx.Gas()), - GasPrice: (*hexutil.Big)(tx.GasPrice()), - GasPremium: (*hexutil.Big)(tx.GasPremium()), - FeeCap: (*hexutil.Big)(tx.FeeCap()), - To: to, - From: common.NewMixedcaseAddress(account.Address), + Data: &data, + Nonce: hexutil.Uint64(tx.Nonce()), + Value: hexutil.Big(*tx.Value()), + Gas: hexutil.Uint64(tx.Gas()), + GasPrice: (*hexutil.Big)(tx.GasPrice()), + MaxMinerBribePerGas: (*hexutil.Big)(tx.MaxMinerBribe()), + FeeCapPerGas: (*hexutil.Big)(tx.FeeCap()), + To: to, + From: common.NewMixedcaseAddress(account.Address), } if err := api.client.Call(&res, "account_signTransaction", args); err != nil { return nil, err diff --git a/core/state_transition.go b/core/state_transition.go index 8dfdbceb56a8..66498e68bac9 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -76,7 +76,7 @@ type Message interface { CheckNonce() bool Data() []byte - GasPremium() *big.Int + MaxMinerBribe() *big.Int FeeCap() *big.Int } @@ -154,7 +154,7 @@ func IntrinsicGas(data []byte, contractCreation, isHomestead bool, isEIP2028 boo // NewStateTransition initialises and returns a new state transition object. func NewStateTransition(evm *vm.EVM, msg Message, gp, gp1559 *GasPool) *StateTransition { - isEIP1559 := evm.ChainConfig().IsEIP1559(evm.BlockNumber) && msg.GasPrice() == nil && msg.GasPremium() != nil && msg.FeeCap() != nil && evm.BaseFee != nil && gp1559 != nil + isEIP1559 := evm.ChainConfig().IsEIP1559(evm.BlockNumber) && msg.GasPrice() == nil && msg.MaxMinerBribe() != nil && msg.FeeCap() != nil && evm.BaseFee != nil && gp1559 != nil st := &StateTransition{ gp: gp, gp1559: gp1559, @@ -167,8 +167,9 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp, gp1559 *GasPool) *StateTra isEIP1559: isEIP1559, } if isEIP1559 { + //ToDo1559: check it // EP1559 gasPrice = min(BASEFEE + tx.fee_premium, tx.fee_cap) - st.eip1559GasPrice = new(big.Int).Add(evm.BaseFee, msg.GasPremium()) + st.eip1559GasPrice = new(big.Int).Add(evm.BaseFee, msg.MaxMinerBribe()) if st.eip1559GasPrice.Cmp(msg.FeeCap()) > 0 { st.eip1559GasPrice.Set(msg.FeeCap()) } @@ -248,11 +249,11 @@ func (st *StateTransition) preCheck() error { return ErrTxNotEIP1559 } // If we are before the EIP1559 activation block, throw an error if we have EIP1559 fields or do not have a GasPrice - if !st.evm.ChainConfig().IsEIP1559(st.evm.BlockNumber) && (st.msg.GasPremium() != nil || st.msg.FeeCap() != nil || st.gp1559 != nil || st.evm.BaseFee != nil || st.msg.GasPrice() == nil) { + if !st.evm.ChainConfig().IsEIP1559(st.evm.BlockNumber) && (st.msg.MaxMinerBribe() != nil || st.msg.FeeCap() != nil || st.gp1559 != nil || st.evm.BaseFee != nil || st.msg.GasPrice() == nil) { return ErrTxIsEIP1559 } // If transaction has both legacy and EIP1559 fields, throw an error - if (st.msg.GasPremium() != nil || st.msg.FeeCap() != nil) && st.msg.GasPrice() != nil { + if (st.msg.MaxMinerBribe() != nil || st.msg.FeeCap() != nil) && st.msg.GasPrice() != nil { return ErrTxSetsLegacyAndEIP1559Fields } // We need a BaseFee if we are past EIP1559 activation @@ -260,7 +261,7 @@ func (st *StateTransition) preCheck() error { return ErrNoBaseFee } // We need either a GasPrice or a FeeCap and GasPremium to be set - if st.msg.GasPrice() == nil && (st.msg.GasPremium() == nil || st.msg.FeeCap() == nil) { + if st.msg.GasPrice() == nil && (st.msg.MaxMinerBribe() == nil || st.msg.FeeCap() == nil) { return ErrMissingGasFields } // If it is an EIp1559 transaction, make sure the derived gasPrice is >= baseFee diff --git a/core/tx_list.go b/core/tx_list.go index 99af5f6bdc3e..a77fb5cfb4f2 100644 --- a/core/tx_list.go +++ b/core/tx_list.go @@ -265,7 +265,7 @@ func (l *txList) Add(tx *types.Transaction, priceBump uint64, baseFee *big.Int) } } else { eip1559 = true - newGasPrice := new(big.Int).Add(baseFee, tx.GasPremium()) + newGasPrice := new(big.Int).Add(baseFee, tx.MaxMinerBribe()) if newGasPrice.Cmp(tx.FeeCap()) > 0 { newGasPrice.Set(tx.FeeCap()) } @@ -274,7 +274,7 @@ func (l *txList) Add(tx *types.Transaction, priceBump uint64, baseFee *big.Int) } } } else { - oldGasPrice := new(big.Int).Add(baseFee, old.GasPremium()) + oldGasPrice := new(big.Int).Add(baseFee, old.MaxMinerBribe()) if oldGasPrice.Cmp(old.FeeCap()) > 0 { oldGasPrice.Set(old.FeeCap()) } @@ -285,7 +285,7 @@ func (l *txList) Add(tx *types.Transaction, priceBump uint64, baseFee *big.Int) } } else { eip1559 = true - newGasPrice := new(big.Int).Add(baseFee, tx.GasPremium()) + newGasPrice := new(big.Int).Add(baseFee, tx.MaxMinerBribe()) if newGasPrice.Cmp(tx.FeeCap()) > 0 { newGasPrice.Set(tx.FeeCap()) } @@ -335,7 +335,7 @@ func (l *txList) Filter(costLimit *big.Int, legacyGasLimit, eip1559GasLimit uint l.eip1559GasCap = eip1559GasLimit // Filter out all the transactions above the account's funds removed := l.txs.Filter(func(tx *types.Transaction) bool { - return tx.Cost(baseFee).Cmp(costLimit) > 0 || (tx.GasPrice() != nil && tx.Gas() > legacyGasLimit) || (tx.GasPremium()) != nil && tx.Gas() > eip1559GasLimit + return tx.Cost(baseFee).Cmp(costLimit) > 0 || (tx.GasPrice() != nil && tx.Gas() > legacyGasLimit) || (tx.MaxMinerBribe()) != nil && tx.Gas() > eip1559GasLimit }) // If the list was strict, filter anything above the lowest nonce @@ -418,13 +418,13 @@ func (h priceHeap) Less(i, j int) bool { iPrice := h.txs[i].GasPrice() jPrice := h.txs[j].GasPrice() if iPrice == nil { - iPrice = new(big.Int).Add(h.baseFee, h.txs[i].GasPremium()) + iPrice = new(big.Int).Add(h.baseFee, h.txs[i].MaxMinerBribe()) if iPrice.Cmp(h.txs[i].FeeCap()) > 0 { iPrice.Set(h.txs[i].FeeCap()) } } if jPrice == nil { - jPrice = new(big.Int).Add(h.baseFee, h.txs[j].GasPremium()) + jPrice = new(big.Int).Add(h.baseFee, h.txs[j].MaxMinerBribe()) if jPrice.Cmp(h.txs[j].FeeCap()) > 0 { jPrice.Set(h.txs[j].FeeCap()) } @@ -513,7 +513,7 @@ func (l *txPricedList) Cap(threshold *big.Int, local *accountSet) types.Transact // Stop the discards if we've reached the threshold gasPrice := tx.GasPrice() if gasPrice == nil { - gasPrice = new(big.Int).Add(l.items.baseFee, tx.GasPremium()) + gasPrice = new(big.Int).Add(l.items.baseFee, tx.MaxMinerBribe()) if gasPrice.Cmp(tx.FeeCap()) > 0 { gasPrice.Set(tx.FeeCap()) } @@ -560,14 +560,14 @@ func (l *txPricedList) Underpriced(tx *types.Transaction, local *accountSet) boo cheapest := l.items.txs[0] cheapestPrice := cheapest.GasPrice() if cheapestPrice == nil { - cheapestPrice = new(big.Int).Add(l.items.baseFee, cheapest.GasPremium()) + cheapestPrice = new(big.Int).Add(l.items.baseFee, cheapest.MaxMinerBribe()) if cheapestPrice.Cmp(cheapest.FeeCap()) > 0 { cheapestPrice.Set(cheapest.FeeCap()) } } txPrice := tx.GasPrice() if txPrice == nil { - txPrice = new(big.Int).Add(l.items.baseFee, tx.GasPremium()) + txPrice = new(big.Int).Add(l.items.baseFee, tx.MaxMinerBribe()) if txPrice.Cmp(tx.FeeCap()) > 0 { txPrice.Set(tx.FeeCap()) } diff --git a/core/tx_pool.go b/core/tx_pool.go index 4a36c42f42ed..45b65680149e 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -548,16 +548,16 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error { if eip1559 && pool.chain.CurrentBlock().BaseFee() == nil { return ErrNoBaseFee } - if eip1559Finalized && (tx.GasPremium() == nil || tx.FeeCap() == nil || tx.GasPrice() != nil) { + if eip1559Finalized && (tx.MaxMinerBribe() == nil || tx.FeeCap() == nil || tx.GasPrice() != nil) { return ErrTxNotEIP1559 } - if !eip1559 && (tx.GasPremium() != nil || tx.FeeCap() != nil || tx.GasPrice() == nil) { + if !eip1559 && (tx.MaxMinerBribe() != nil || tx.FeeCap() != nil || tx.GasPrice() == nil) { return ErrTxIsEIP1559 } - if tx.GasPrice() != nil && (tx.GasPremium() != nil || tx.FeeCap() != nil) { + if tx.GasPrice() != nil && (tx.MaxMinerBribe() != nil || tx.FeeCap() != nil) { return ErrTxSetsLegacyAndEIP1559Fields } - if tx.GasPrice() == nil && (tx.GasPremium() == nil || tx.FeeCap() == nil) { + if tx.GasPrice() == nil && (tx.MaxMinerBribe() == nil || tx.FeeCap() == nil) { return ErrMissingGasFields } // Set the gasPrice to the tx.GasPrice() if it is non nil (legacy transaction) @@ -565,7 +565,7 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error { if tx.GasPrice() != nil { gasPrice = tx.GasPrice() } else { // Derive the gasPrice from the tx.GasPremium() and tx.FeeCap() (EIP1559 transaction) - gasPrice = new(big.Int).Add(pool.chain.CurrentBlock().BaseFee(), tx.GasPremium()) + gasPrice = new(big.Int).Add(pool.chain.CurrentBlock().BaseFee(), tx.MaxMinerBribe()) if gasPrice.Cmp(tx.FeeCap()) > 0 { gasPrice.Set(tx.FeeCap()) } diff --git a/core/types/gen_tx_json.go b/core/types/gen_tx_json.go index 1778180e7004..fd2d43f67b8f 100644 --- a/core/types/gen_tx_json.go +++ b/core/types/gen_tx_json.go @@ -16,18 +16,18 @@ var _ = (*txdataMarshaling)(nil) // MarshalJSON marshals as JSON. func (t txdata) MarshalJSON() ([]byte, error) { type txdata struct { - AccountNonce hexutil.Uint64 `json:"nonce" gencodec:"required"` - Price *hexutil.Big `json:"gasPrice"` - GasLimit hexutil.Uint64 `json:"gas" gencodec:"required"` - Recipient *common.Address `json:"to" rlp:"nil"` - Amount *hexutil.Big `json:"value" gencodec:"required"` - Payload hexutil.Bytes `json:"input" gencodec:"required"` - GasPremium *hexutil.Big `json:"gasPremium" rlp:"nil"` - FeeCap *hexutil.Big `json:"feeCap" rlp:"nil"` - V *hexutil.Big `json:"v" gencodec:"required"` - R *hexutil.Big `json:"r" gencodec:"required"` - S *hexutil.Big `json:"s" gencodec:"required"` - Hash *common.Hash `json:"hash" rlp:"-"` + AccountNonce hexutil.Uint64 `json:"nonce" gencodec:"required"` + Price *hexutil.Big `json:"gasPrice"` + GasLimit hexutil.Uint64 `json:"gas" gencodec:"required"` + Recipient *common.Address `json:"to" rlp:"nil"` + Amount *hexutil.Big `json:"value" gencodec:"required"` + Payload hexutil.Bytes `json:"input" gencodec:"required"` + MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas" rlp:"nil"` + FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas" rlp:"nil"` + V *hexutil.Big `json:"v" gencodec:"required"` + R *hexutil.Big `json:"r" gencodec:"required"` + S *hexutil.Big `json:"s" gencodec:"required"` + Hash *common.Hash `json:"hash" rlp:"-"` } var enc txdata enc.AccountNonce = hexutil.Uint64(t.AccountNonce) @@ -36,8 +36,8 @@ func (t txdata) MarshalJSON() ([]byte, error) { enc.Recipient = t.Recipient enc.Amount = (*hexutil.Big)(t.Amount) enc.Payload = t.Payload - enc.GasPremium = (*hexutil.Big)(t.GasPremium) - enc.FeeCap = (*hexutil.Big)(t.FeeCap) + enc.MaxMinerBribePerGas = (*hexutil.Big)(t.MaxMinerBribePerGas) + enc.FeeCapPerGas = (*hexutil.Big)(t.FeeCapPerGas) enc.V = (*hexutil.Big)(t.V) enc.R = (*hexutil.Big)(t.R) enc.S = (*hexutil.Big)(t.S) @@ -48,18 +48,18 @@ func (t txdata) MarshalJSON() ([]byte, error) { // UnmarshalJSON unmarshals from JSON. func (t *txdata) UnmarshalJSON(input []byte) error { type txdata struct { - AccountNonce *hexutil.Uint64 `json:"nonce" gencodec:"required"` - Price *hexutil.Big `json:"gasPrice"` - GasLimit *hexutil.Uint64 `json:"gas" gencodec:"required"` - Recipient *common.Address `json:"to" rlp:"nil"` - Amount *hexutil.Big `json:"value" gencodec:"required"` - Payload *hexutil.Bytes `json:"input" gencodec:"required"` - GasPremium *hexutil.Big `json:"gasPremium" rlp:"nil"` - FeeCap *hexutil.Big `json:"feeCap" rlp:"nil"` - V *hexutil.Big `json:"v" gencodec:"required"` - R *hexutil.Big `json:"r" gencodec:"required"` - S *hexutil.Big `json:"s" gencodec:"required"` - Hash *common.Hash `json:"hash" rlp:"-"` + AccountNonce *hexutil.Uint64 `json:"nonce" gencodec:"required"` + Price *hexutil.Big `json:"gasPrice"` + GasLimit *hexutil.Uint64 `json:"gas" gencodec:"required"` + Recipient *common.Address `json:"to" rlp:"nil"` + Amount *hexutil.Big `json:"value" gencodec:"required"` + Payload *hexutil.Bytes `json:"input" gencodec:"required"` + MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas" rlp:"nil"` + FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas" rlp:"nil"` + V *hexutil.Big `json:"v" gencodec:"required"` + R *hexutil.Big `json:"r" gencodec:"required"` + S *hexutil.Big `json:"s" gencodec:"required"` + Hash *common.Hash `json:"hash" rlp:"-"` } var dec txdata if err := json.Unmarshal(input, &dec); err != nil { @@ -87,11 +87,11 @@ func (t *txdata) UnmarshalJSON(input []byte) error { return errors.New("missing required field 'input' for txdata") } t.Payload = *dec.Payload - if dec.GasPremium != nil { - t.GasPremium = (*big.Int)(dec.GasPremium) + if dec.MaxMinerBribePerGas != nil { + t.MaxMinerBribePerGas = (*big.Int)(dec.MaxMinerBribePerGas) } - if dec.FeeCap != nil { - t.FeeCap = (*big.Int)(dec.FeeCap) + if dec.FeeCapPerGas != nil { + t.FeeCapPerGas = (*big.Int)(dec.FeeCapPerGas) } if dec.V == nil { return errors.New("missing required field 'v' for txdata") diff --git a/core/types/transaction.go b/core/types/transaction.go index 9a2aa1f61fce..a7c0beb82f6f 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -52,8 +52,8 @@ type txdata struct { Payload []byte `json:"input" gencodec:"required"` // EIP1559 gas values - GasPremium *big.Int `json:"gasPremium" rlp:"nil"` // nil means legacy transaction - FeeCap *big.Int `json:"feeCap" rlp:"nil"` // nil means legacy transaction + MaxMinerBribePerGas *big.Int `json:"max_miner_bribe_per_gas" rlp:"nil"` + FeeCapPerGas *big.Int `json:"fee_cap_per_gas" rlp:"nil"` // Signature values V *big.Int `json:"v" gencodec:"required"` @@ -77,15 +77,15 @@ type txdataMarshaling struct { S *hexutil.Big } -func NewTransaction(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, gasPremium, feeCap *big.Int) *Transaction { - return newTransaction(nonce, &to, amount, gasLimit, gasPrice, data, gasPremium, feeCap) +func NewTransaction(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, maxMinerBribePerGas, feeCapPerGas *big.Int) *Transaction { + return newTransaction(nonce, &to, amount, gasLimit, gasPrice, data, maxMinerBribePerGas, feeCapPerGas) } -func NewContractCreation(nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, gasPremium, feeCap *big.Int) *Transaction { - return newTransaction(nonce, nil, amount, gasLimit, gasPrice, data, gasPremium, feeCap) +func NewContractCreation(nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, maxMinerBribePerGas, feeCapPerGas *big.Int) *Transaction { + return newTransaction(nonce, nil, amount, gasLimit, gasPrice, data, maxMinerBribePerGas, feeCapPerGas) } -func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, gasPremium, feeCap *big.Int) *Transaction { +func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, maxMinerBribePerGas, feeCapPerGas *big.Int) *Transaction { if len(data) > 0 { data = common.CopyBytes(data) } @@ -105,19 +105,12 @@ func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit if gasPrice != nil { d.Price = gasPrice } - if gasPremium != nil { - d.GasPremium = gasPremium + if maxMinerBribePerGas != nil { + d.MaxMinerBribePerGas = maxMinerBribePerGas } - if feeCap != nil { - d.FeeCap = feeCap + if feeCapPerGas != nil { + d.FeeCapPerGas = feeCapPerGas } - if gasPremium != nil { - d.GasPremium = gasPremium - } - if feeCap != nil { - d.FeeCap = feeCap - } - return &Transaction{data: d} } @@ -142,7 +135,7 @@ func isProtectedV(V *big.Int) bool { // EncodeRLP implements rlp.Encoder func (tx *Transaction) EncodeRLP(w io.Writer) error { - if tx.data.FeeCap == nil || tx.data.GasPremium == nil { + if tx.data.FeeCapPerGas == nil || tx.data.MaxMinerBribePerGas == nil { return rlp.Encode(w, []interface{}{ tx.data.AccountNonce, tx.data.Price, @@ -203,12 +196,12 @@ func (tx *Transaction) DecodeRLP(stream *rlp.Stream) error { if err = stream.Decode(payload); err != nil { return err } - gasPremium := new(big.Int) - if err = stream.Decode(gasPremium); err != nil { + maxMinerBribePerGas := new(big.Int) + if err = stream.Decode(maxMinerBribePerGas); err != nil { return err } - feeCap := new(big.Int) - if err = stream.Decode(feeCap); err != nil { + feeCapPerGas := new(big.Int) + if err = stream.Decode(feeCapPerGas); err != nil { return err } v := new(big.Int) @@ -225,8 +218,8 @@ func (tx *Transaction) DecodeRLP(stream *rlp.Stream) error { Recipient: recipient, Amount: amount, Payload: *payload, - V: gasPremium, - R: feeCap, + V: maxMinerBribePerGas, + R: feeCapPerGas, S: v, } tx.size.Store(common.StorageSize(rlp.ListSize(size))) @@ -249,17 +242,17 @@ func (tx *Transaction) DecodeRLP(stream *rlp.Stream) error { return err } tx.data = txdata{ - AccountNonce: *accountNonce, - Price: nil, - GasLimit: *gasLimit, - Recipient: recipient, - Amount: amount, - Payload: *payload, - GasPremium: gasPremium, - FeeCap: feeCap, - V: v, - R: r, - S: s, + AccountNonce: *accountNonce, + Price: nil, + GasLimit: *gasLimit, + Recipient: recipient, + Amount: amount, + Payload: *payload, + MaxMinerBribePerGas: maxMinerBribePerGas, + FeeCapPerGas: feeCapPerGas, + V: v, + R: r, + S: s, } tx.size.Store(common.StorageSize(rlp.ListSize(size))) return nil @@ -298,14 +291,14 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error { return nil } -func (tx *Transaction) Data() []byte { return common.CopyBytes(tx.data.Payload) } -func (tx *Transaction) Gas() uint64 { return tx.data.GasLimit } -func (tx *Transaction) GasPrice() *big.Int { return tx.data.Price } -func (tx *Transaction) Value() *big.Int { return new(big.Int).Set(tx.data.Amount) } -func (tx *Transaction) Nonce() uint64 { return tx.data.AccountNonce } -func (tx *Transaction) CheckNonce() bool { return true } -func (tx *Transaction) GasPremium() *big.Int { return tx.data.GasPremium } -func (tx *Transaction) FeeCap() *big.Int { return tx.data.FeeCap } +func (tx *Transaction) Data() []byte { return common.CopyBytes(tx.data.Payload) } +func (tx *Transaction) Gas() uint64 { return tx.data.GasLimit } +func (tx *Transaction) GasPrice() *big.Int { return tx.data.Price } +func (tx *Transaction) Value() *big.Int { return new(big.Int).Set(tx.data.Amount) } +func (tx *Transaction) Nonce() uint64 { return tx.data.AccountNonce } +func (tx *Transaction) CheckNonce() bool { return true } +func (tx *Transaction) MaxMinerBribe() *big.Int { return tx.data.MaxMinerBribePerGas } +func (tx *Transaction) FeeCap() *big.Int { return tx.data.FeeCapPerGas } // To returns the recipient address of the transaction. // It returns nil if the transaction is a contract creation. @@ -347,15 +340,15 @@ func (tx *Transaction) Size() common.StorageSize { // XXX Rename message to something less arbitrary? func (tx *Transaction) AsMessage(s Signer) (Message, error) { msg := Message{ - nonce: tx.data.AccountNonce, - gasLimit: tx.data.GasLimit, - gasPrice: tx.data.Price, - to: tx.data.Recipient, - amount: tx.data.Amount, - data: tx.data.Payload, - checkNonce: true, - gasPremium: tx.data.GasPremium, - feeCap: tx.data.FeeCap, + nonce: tx.data.AccountNonce, + gasLimit: tx.data.GasLimit, + gasPrice: tx.data.Price, + to: tx.data.Recipient, + amount: tx.data.Amount, + data: tx.data.Payload, + checkNonce: true, + maxMinerBribePerGas: tx.data.MaxMinerBribePerGas, + feeCapPerGas: tx.data.FeeCapPerGas, } var err error @@ -382,10 +375,11 @@ func (tx *Transaction) Cost(baseFee *big.Int) *big.Int { total.Add(total, tx.data.Amount) return total } - if baseFee != nil && tx.data.GasPremium != nil && tx.data.FeeCap != nil { - eip1559GasPrice := new(big.Int).Add(baseFee, tx.data.GasPremium) - if eip1559GasPrice.Cmp(tx.data.FeeCap) > 0 { - eip1559GasPrice.Set(tx.data.FeeCap) + //ToDo1559: check it + if baseFee != nil && tx.data.MaxMinerBribePerGas != nil && tx.data.FeeCapPerGas != nil { + eip1559GasPrice := new(big.Int).Add(baseFee, tx.data.MaxMinerBribePerGas) + if eip1559GasPrice.Cmp(tx.data.FeeCapPerGas) > 0 { + eip1559GasPrice.Set(tx.data.FeeCapPerGas) } total := new(big.Int).Mul(eip1559GasPrice, new(big.Int).SetUint64(tx.data.GasLimit)) total.Add(total, tx.data.Amount) @@ -451,20 +445,21 @@ type TxByPrice struct { func (s TxByPrice) Len() int { return len(s.txs) } +//ToDo1559: check it // Note that this returns true if j is less than i, as the ordering needs to be from highest price to lowest func (s TxByPrice) Less(i, j int) bool { iPrice := s.txs[i].data.Price jPrice := s.txs[j].data.Price if iPrice == nil { - iPrice = new(big.Int).Add(s.baseFee, s.txs[i].data.GasPremium) - if iPrice.Cmp(s.txs[i].data.FeeCap) > 0 { - iPrice.Set(s.txs[i].data.FeeCap) + iPrice = new(big.Int).Add(s.baseFee, s.txs[i].data.MaxMinerBribePerGas) + if iPrice.Cmp(s.txs[i].data.FeeCapPerGas) > 0 { + iPrice.Set(s.txs[i].data.FeeCapPerGas) } } if jPrice == nil { - jPrice = new(big.Int).Add(s.baseFee, s.txs[j].data.GasPremium) - if jPrice.Cmp(s.txs[j].data.FeeCap) > 0 { - jPrice.Set(s.txs[j].data.FeeCap) + jPrice = new(big.Int).Add(s.baseFee, s.txs[j].data.MaxMinerBribePerGas) + if jPrice.Cmp(s.txs[j].data.FeeCapPerGas) > 0 { + jPrice.Set(s.txs[j].data.FeeCapPerGas) } } return iPrice.Cmp(jPrice) > 0 @@ -552,41 +547,41 @@ func (t *TransactionsByPriceAndNonce) Pop() { // // NOTE: In a future PR this will be removed. type Message struct { - to *common.Address - from common.Address - nonce uint64 - amount *big.Int - gasLimit uint64 - gasPrice *big.Int - data []byte - checkNonce bool - gasPremium *big.Int - feeCap *big.Int + to *common.Address + from common.Address + nonce uint64 + amount *big.Int + gasLimit uint64 + gasPrice *big.Int + data []byte + checkNonce bool + maxMinerBribePerGas *big.Int + feeCapPerGas *big.Int } // NewMessage creates and returns a new message -func NewMessage(from common.Address, to *common.Address, nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, checkNonce bool, gasPremium, feeCap *big.Int) Message { +func NewMessage(from common.Address, to *common.Address, nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, checkNonce bool, maxMinerBribePerGas, feeCapPerGas *big.Int) Message { return Message{ - from: from, - to: to, - nonce: nonce, - amount: amount, - gasLimit: gasLimit, - gasPrice: gasPrice, - data: data, - checkNonce: checkNonce, - gasPremium: gasPremium, - feeCap: feeCap, - } -} - -func (m Message) From() common.Address { return m.from } -func (m Message) To() *common.Address { return m.to } -func (m Message) GasPrice() *big.Int { return m.gasPrice } -func (m Message) Value() *big.Int { return m.amount } -func (m Message) Gas() uint64 { return m.gasLimit } -func (m Message) Nonce() uint64 { return m.nonce } -func (m Message) Data() []byte { return m.data } -func (m Message) CheckNonce() bool { return m.checkNonce } -func (m Message) GasPremium() *big.Int { return m.gasPremium } -func (m Message) FeeCap() *big.Int { return m.feeCap } + from: from, + to: to, + nonce: nonce, + amount: amount, + gasLimit: gasLimit, + gasPrice: gasPrice, + data: data, + checkNonce: checkNonce, + maxMinerBribePerGas: maxMinerBribePerGas, + feeCapPerGas: feeCapPerGas, + } +} + +func (m Message) From() common.Address { return m.from } +func (m Message) To() *common.Address { return m.to } +func (m Message) GasPrice() *big.Int { return m.gasPrice } +func (m Message) Value() *big.Int { return m.amount } +func (m Message) Gas() uint64 { return m.gasLimit } +func (m Message) Nonce() uint64 { return m.nonce } +func (m Message) Data() []byte { return m.data } +func (m Message) CheckNonce() bool { return m.checkNonce } +func (m Message) MaxMinerBribe() *big.Int { return m.maxMinerBribePerGas } +func (m Message) FeeCap() *big.Int { return m.feeCapPerGas } diff --git a/core/types/transaction_signing.go b/core/types/transaction_signing.go index c4f23b3bb195..bd4d66e33f3f 100644 --- a/core/types/transaction_signing.go +++ b/core/types/transaction_signing.go @@ -161,8 +161,8 @@ func (s EIP155Signer) Hash(tx *Transaction) common.Hash { tx.data.Amount, tx.data.Payload, } - if tx.data.GasPremium != nil && tx.data.FeeCap != nil { - txFields = append(txFields, tx.data.GasPremium, tx.data.FeeCap) + if tx.data.MaxMinerBribePerGas != nil && tx.data.FeeCapPerGas != nil { + txFields = append(txFields, tx.data.MaxMinerBribePerGas, tx.data.FeeCapPerGas) } txFields = append(txFields, s.chainId, uint(0), uint(0)) return rlpHash(txFields) @@ -217,8 +217,8 @@ func (fs FrontierSigner) Hash(tx *Transaction) common.Hash { tx.data.Amount, tx.data.Payload, } - if tx.data.GasPremium != nil && tx.data.FeeCap != nil { - txFields = append(txFields, tx.data.GasPremium, tx.data.FeeCap) + if tx.data.MaxMinerBribePerGas != nil && tx.data.FeeCapPerGas != nil { + txFields = append(txFields, tx.data.MaxMinerBribePerGas, tx.data.FeeCapPerGas) } return rlpHash(txFields) } diff --git a/eth/gasprice/gasprice.go b/eth/gasprice/gasprice.go index bbe5c86c0d6c..0e1088ae1371 100644 --- a/eth/gasprice/gasprice.go +++ b/eth/gasprice/gasprice.go @@ -345,13 +345,13 @@ func (t *transactionsByGasPrice) Less(i, j int) bool { iPrice := t.txs[i].GasPrice() jPrice := t.txs[j].GasPrice() if iPrice == nil { - iPrice = new(big.Int).Add(t.baseFee, t.txs[i].GasPremium()) + iPrice = new(big.Int).Add(t.baseFee, t.txs[i].MaxMinerBribe()) if iPrice.Cmp(t.txs[i].FeeCap()) > 0 { iPrice.Set(t.txs[i].FeeCap()) } } if jPrice == nil { - jPrice = new(big.Int).Add(t.baseFee, t.txs[j].GasPremium()) + jPrice = new(big.Int).Add(t.baseFee, t.txs[j].MaxMinerBribe()) if jPrice.Cmp(t.txs[j].FeeCap()) > 0 { jPrice.Set(t.txs[j].FeeCap()) } @@ -367,8 +367,8 @@ type transactionsByGasPremium struct { func (t *transactionsByGasPremium) Len() int { return len(t.txs) } func (t *transactionsByGasPremium) Swap(i, j int) { t.txs[i], t.txs[j] = t.txs[j], t.txs[i] } func (t *transactionsByGasPremium) Less(i, j int) bool { - iPremium := t.txs[i].GasPremium() - jPremium := t.txs[j].GasPremium() + iPremium := t.txs[i].MaxMinerBribe() + jPremium := t.txs[j].MaxMinerBribe() if iPremium == nil { iPremium = new(big.Int).Sub(t.txs[i].GasPrice(), t.baseFee) if iPremium.Cmp(common.Big0) < 0 { @@ -423,7 +423,7 @@ func (gpo *Oracle) getBlockPrices(ctx context.Context, signer types.Signer, bloc } price := tx.GasPrice() if price == nil { - price = new(big.Int).Add(block.BaseFee(), tx.GasPremium()) + price = new(big.Int).Add(block.BaseFee(), tx.MaxMinerBribe()) if price.Cmp(tx.FeeCap()) > 0 { price.Set(tx.FeeCap()) } @@ -455,7 +455,7 @@ func (gpo *Oracle) getBlockPremiums(ctx context.Context, signer types.Signer, bl if err != nil || sender == block.Coinbase() { continue } - premium := tx.GasPremium() + premium := tx.MaxMinerBribe() if premium == nil { premium = new(big.Int).Sub(tx.GasPrice(), block.BaseFee()) if premium.Cmp(common.Big0) < 0 { diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index b8d0b39b32b9..1844539492ae 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -564,11 +564,11 @@ func toCallArg(msg ethereum.CallMsg) interface{} { if msg.GasPrice != nil { arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice) } - if msg.GasPremium != nil { - arg["gasPremium"] = (*hexutil.Big)(msg.GasPremium) + if msg.MaxMinerBribePerGas != nil { + arg["max_miner_bribe_per_gas"] = (*hexutil.Big)(msg.MaxMinerBribePerGas) } - if msg.FeeCap != nil { - arg["feeCap"] = (*hexutil.Big)(msg.FeeCap) + if msg.FeeCapPerGas != nil { + arg["fee_cap_per_gas"] = (*hexutil.Big)(msg.FeeCapPerGas) } return arg } diff --git a/interfaces.go b/interfaces.go index 14a32a96f5ed..e646ae2f7957 100644 --- a/interfaces.go +++ b/interfaces.go @@ -113,14 +113,14 @@ type ChainSyncReader interface { // CallMsg contains parameters for contract calls. type CallMsg struct { - From common.Address // the sender of the 'transaction' - To *common.Address // the destination contract (nil for contract creation) - Gas uint64 // if 0, the call executes with near-infinite gas - GasPrice *big.Int // wei <-> gas exchange ratio - Value *big.Int // amount of wei sent along with the call - Data []byte // input data, usually an ABI-encoded contract method invocation - GasPremium *big.Int // EIP1559 gas premium paid to miners (excess of the basefee) - FeeCap *big.Int // Max amount of gas we can use for this trx execution + From common.Address // the sender of the 'transaction' + To *common.Address // the destination contract (nil for contract creation) + Gas uint64 // if 0, the call executes with near-infinite gas + GasPrice *big.Int // wei <-> gas exchange ratio + Value *big.Int // amount of wei sent along with the call + Data []byte // input data, usually an ABI-encoded contract method invocation + MaxMinerBribePerGas *big.Int // EIP1559 gas premium paid to miners (excess of the basefee) + FeeCapPerGas *big.Int // Max amount of gas we can use for this trx execution } // A ContractCaller provides contract calls, essentially transactions that are executed by diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 9a48fc957790..021253f19723 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1229,22 +1229,22 @@ func (s *PublicBlockChainAPI) rpcMarshalBlock(b *types.Block, inclTx bool, fullT // RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction type RPCTransaction struct { - BlockHash *common.Hash `json:"blockHash"` - BlockNumber *hexutil.Big `json:"blockNumber"` - From common.Address `json:"from"` - Gas hexutil.Uint64 `json:"gas"` - GasPrice *hexutil.Big `json:"gasPrice"` - Hash common.Hash `json:"hash"` - Input hexutil.Bytes `json:"input"` - Nonce hexutil.Uint64 `json:"nonce"` - To *common.Address `json:"to"` - TransactionIndex *hexutil.Uint64 `json:"transactionIndex"` - Value *hexutil.Big `json:"value"` - GasPremium *hexutil.Big `json:"gasPremium"` - FeeCap *hexutil.Big `json:"feeCap"` - V *hexutil.Big `json:"v"` - R *hexutil.Big `json:"r"` - S *hexutil.Big `json:"s"` + BlockHash *common.Hash `json:"blockHash"` + BlockNumber *hexutil.Big `json:"blockNumber"` + From common.Address `json:"from"` + Gas hexutil.Uint64 `json:"gas"` + GasPrice *hexutil.Big `json:"gasPrice"` + Hash common.Hash `json:"hash"` + Input hexutil.Bytes `json:"input"` + Nonce hexutil.Uint64 `json:"nonce"` + To *common.Address `json:"to"` + TransactionIndex *hexutil.Uint64 `json:"transactionIndex"` + Value *hexutil.Big `json:"value"` + MaxMinerBribePerGas *hexutil.Big `json:"gasPremium"` + FeeCapPerGas *hexutil.Big `json:"feeCap"` + V *hexutil.Big `json:"v"` + R *hexutil.Big `json:"r"` + S *hexutil.Big `json:"s"` } // newRPCTransaction returns a transaction that will serialize to the RPC @@ -1258,19 +1258,19 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber v, r, s := tx.RawSignatureValues() result := &RPCTransaction{ - From: from, - Gas: hexutil.Uint64(tx.Gas()), - GasPrice: (*hexutil.Big)(tx.GasPrice()), - Hash: tx.Hash(), - Input: hexutil.Bytes(tx.Data()), - Nonce: hexutil.Uint64(tx.Nonce()), - To: tx.To(), - Value: (*hexutil.Big)(tx.Value()), - GasPremium: (*hexutil.Big)(tx.GasPremium()), - FeeCap: (*hexutil.Big)(tx.FeeCap()), - V: (*hexutil.Big)(v), - R: (*hexutil.Big)(r), - S: (*hexutil.Big)(s), + From: from, + Gas: hexutil.Uint64(tx.Gas()), + GasPrice: (*hexutil.Big)(tx.GasPrice()), + Hash: tx.Hash(), + Input: hexutil.Bytes(tx.Data()), + Nonce: hexutil.Uint64(tx.Nonce()), + To: tx.To(), + Value: (*hexutil.Big)(tx.Value()), + MaxMinerBribePerGas: (*hexutil.Big)(tx.MaxMinerBribe()), + FeeCapPerGas: (*hexutil.Big)(tx.FeeCap()), + V: (*hexutil.Big)(v), + R: (*hexutil.Big)(r), + S: (*hexutil.Big)(s), } if blockHash != (common.Hash{}) { result.BlockHash = &blockHash @@ -1698,20 +1698,20 @@ func (s *PublicTransactionPoolAPI) SendRawTransaction(ctx context.Context, encod if eip1559 && s.b.CurrentBlock().BaseFee() == nil { return common.Hash{}, core.ErrNoBaseFee } - if eip1559Finalized && (tx.GasPremium() == nil || tx.FeeCap() == nil || tx.GasPrice() != nil) { + if eip1559Finalized && (tx.MaxMinerBribe() == nil || tx.FeeCap() == nil || tx.GasPrice() != nil) { return common.Hash{}, core.ErrTxNotEIP1559 } - if !eip1559 && (tx.GasPremium() != nil || tx.FeeCap() != nil || tx.GasPrice() == nil) { + if !eip1559 && (tx.MaxMinerBribe() != nil || tx.FeeCap() != nil || tx.GasPrice() == nil) { return common.Hash{}, core.ErrTxIsEIP1559 } - if tx.GasPrice() != nil && (tx.GasPremium() != nil || tx.FeeCap() != nil) { + if tx.GasPrice() != nil && (tx.MaxMinerBribe() != nil || tx.FeeCap() != nil) { return common.Hash{}, core.ErrTxSetsLegacyAndEIP1559Fields } - if tx.GasPrice() == nil && (tx.GasPremium() == nil || tx.FeeCap() == nil) { + if tx.GasPrice() == nil && (tx.MaxMinerBribe() == nil || tx.FeeCap() == nil) { return common.Hash{}, core.ErrMissingGasFields } - if tx.GasPremium() != nil { - gasPrice := new(big.Int).Add(s.b.CurrentBlock().BaseFee(), tx.GasPremium()) + if tx.MaxMinerBribe() != nil { + gasPrice := new(big.Int).Add(s.b.CurrentBlock().BaseFee(), tx.MaxMinerBribe()) if gasPrice.Cmp(tx.FeeCap()) > 0 { gasPrice.Set(tx.FeeCap()) } diff --git a/light/txpool.go b/light/txpool.go index 0826649b59c0..87e9cd727109 100644 --- a/light/txpool.go +++ b/light/txpool.go @@ -353,16 +353,16 @@ func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error if eip1559 && header.BaseFee == nil { return core.ErrNoBaseFee } - if eip1559Finalized && (tx.GasPremium() == nil || tx.FeeCap() == nil || tx.GasPrice() != nil) { + if eip1559Finalized && (tx.MaxMinerBribe() == nil || tx.FeeCap() == nil || tx.GasPrice() != nil) { return core.ErrTxNotEIP1559 } - if !eip1559 && (tx.GasPremium() != nil || tx.FeeCap() != nil || tx.GasPrice() == nil) { + if !eip1559 && (tx.MaxMinerBribe() != nil || tx.FeeCap() != nil || tx.GasPrice() == nil) { return core.ErrTxIsEIP1559 } - if tx.GasPrice() != nil && (tx.GasPremium() != nil || tx.FeeCap() != nil) { + if tx.GasPrice() != nil && (tx.MaxMinerBribe() != nil || tx.FeeCap() != nil) { return core.ErrTxSetsLegacyAndEIP1559Fields } - if tx.GasPrice() == nil && (tx.GasPremium() == nil || tx.FeeCap() == nil) { + if tx.GasPrice() == nil && (tx.MaxMinerBribe() == nil || tx.FeeCap() == nil) { return core.ErrMissingGasFields } @@ -392,13 +392,13 @@ func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error if tx.GasPrice() != nil && legacyGasLimit < tx.Gas() { return core.ErrLegacyGasLimit } - if tx.GasPremium() != nil && eip1559GasLimit < tx.Gas() { + if tx.MaxMinerBribe() != nil && eip1559GasLimit < tx.Gas() { return core.ErrEIP1559GasLimit } // Derive the gasPrice from the tx.GasPremium() and tx.FeeCap() (EIP1559 transaction) to ensure it is greater than BaseFee - if tx.GasPremium() != nil { - gasPrice := new(big.Int).Add(pool.chain.CurrentHeader().BaseFee, tx.GasPremium()) + if tx.MaxMinerBribe() != nil { + gasPrice := new(big.Int).Add(pool.chain.CurrentHeader().BaseFee, tx.MaxMinerBribe()) if gasPrice.Cmp(tx.FeeCap()) > 0 { gasPrice.Set(tx.FeeCap()) } diff --git a/miner/worker.go b/miner/worker.go index 93ac83d158ea..437d054057ef 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -778,10 +778,10 @@ func (w *worker) commitTransactions(txs *types.TransactionsByPriceAndNonce, coin // Set which gasPool to use based on the type of transaction eip1559 := false var gp *core.GasPool - if w.current.gp1559 != nil && tx.GasPrice() == nil && tx.GasPremium() != nil && tx.FeeCap() != nil { + if w.current.gp1559 != nil && tx.GasPrice() == nil && tx.MaxMinerBribe() != nil && tx.FeeCap() != nil { gp = w.current.gp1559 eip1559 = true - } else if w.current.gasPool != nil && tx.GasPremium() == nil && tx.FeeCap() == nil && tx.GasPrice() != nil { + } else if w.current.gasPool != nil && tx.MaxMinerBribe() == nil && tx.FeeCap() == nil && tx.GasPrice() != nil { gp = w.current.gasPool } else { log.Error("Transaction does not conform with expected format (legacy or EIP1559)") @@ -1057,8 +1057,8 @@ func (w *worker) commit(uncles []*types.Header, interval func(), update bool, st for i, tx := range block.Transactions() { if tx.GasPrice() != nil { feesWei.Add(feesWei, new(big.Int).Mul(new(big.Int).SetUint64(receipts[i].GasUsed), tx.GasPrice())) - } else if tx.GasPremium() != nil && tx.FeeCap() != nil { - gasPrice := new(big.Int).Add(block.BaseFee(), tx.GasPremium()) + } else if tx.MaxMinerBribe() != nil && tx.FeeCap() != nil { + gasPrice := new(big.Int).Add(block.BaseFee(), tx.MaxMinerBribe()) if gasPrice.Cmp(tx.FeeCap()) > 0 { gasPrice.Set(tx.FeeCap()) } diff --git a/signer/core/api.go b/signer/core/api.go index 61d4359e3095..dbf80de9aebe 100644 --- a/signer/core/api.go +++ b/signer/core/api.go @@ -465,7 +465,7 @@ func logDiff(original *SignTxRequest, new *SignTxResponse) bool { modified = true log.Info("GasPrice changed by UI", "was", g0, "is", g1) } - gp0, gp1 := (*big.Int)(original.Transaction.GasPremium), (*big.Int)(new.Transaction.GasPremium) + gp0, gp1 := (*big.Int)(original.Transaction.MaxMinerBribePerGas), (*big.Int)(new.Transaction.MaxMinerBribePerGas) if gp0 == nil || gp1 == nil { if gp0 != gp1 { modified = true @@ -475,7 +475,7 @@ func logDiff(original *SignTxRequest, new *SignTxResponse) bool { modified = true log.Info("GasPremium changed by UI", "was", gp0, "is", gp1) } - f0, f1 := (*big.Int)(original.Transaction.FeeCap), (*big.Int)(new.Transaction.FeeCap) + f0, f1 := (*big.Int)(original.Transaction.FeeCapPerGas), (*big.Int)(new.Transaction.FeeCapPerGas) if f0 == nil || f1 == nil { if f0 != f1 { modified = true diff --git a/signer/core/types.go b/signer/core/types.go index af74beb6cbb6..4980765b289c 100644 --- a/signer/core/types.go +++ b/signer/core/types.go @@ -77,8 +77,8 @@ type SendTxArgs struct { Data *hexutil.Bytes `json:"data"` Input *hexutil.Bytes `json:"input,omitempty"` // EIP1559 fields - GasPremium *hexutil.Big `json:"gasPremium"` - FeeCap *hexutil.Big `json:"feeCap"` + MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas"` + FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas"` } func (args SendTxArgs) String() string { @@ -97,7 +97,7 @@ func (args *SendTxArgs) toTransaction() *types.Transaction { input = *args.Input } if args.To == nil { - return types.NewContractCreation(uint64(args.Nonce), (*big.Int)(&args.Value), uint64(args.Gas), (*big.Int)(args.GasPrice), input, (*big.Int)(args.GasPremium), (*big.Int)(args.FeeCap)) + return types.NewContractCreation(uint64(args.Nonce), (*big.Int)(&args.Value), uint64(args.Gas), (*big.Int)(args.GasPrice), input, (*big.Int)(args.MaxMinerBribePerGas), (*big.Int)(args.FeeCapPerGas)) } - return types.NewTransaction(uint64(args.Nonce), args.To.Address(), (*big.Int)(&args.Value), (uint64)(args.Gas), (*big.Int)(args.GasPrice), input, (*big.Int)(args.GasPremium), (*big.Int)(args.FeeCap)) + return types.NewTransaction(uint64(args.Nonce), args.To.Address(), (*big.Int)(&args.Value), (uint64)(args.Gas), (*big.Int)(args.GasPrice), input, (*big.Int)(args.MaxMinerBribePerGas), (*big.Int)(args.FeeCapPerGas)) } From 1caf8e9d60e89b8e7785f3b30dd615a933b2e271 Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Wed, 28 Oct 2020 18:27:33 +0300 Subject: [PATCH 02/16] update fields for eth api --- internal/ethapi/api.go | 82 +++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 021253f19723..5577cad4bed1 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -399,7 +399,7 @@ func (s *PrivateAccountAPI) SignTransaction(ctx context.Context, args SendTxArgs if args.Gas == nil { return nil, fmt.Errorf("gas not specified") } - if args.GasPrice == nil && (args.GasPremium == nil || args.FeeCap == nil) { + if args.GasPrice == nil && (args.MaxMinerBribePerGas == nil || args.FeeCapPerGas == nil) { return nil, fmt.Errorf("gasPrice or gasPremium+feeCap not specified") } if args.Nonce == nil { @@ -748,14 +748,14 @@ func (s *PublicBlockChainAPI) GetStorageAt(ctx context.Context, address common.A // CallArgs represents the arguments for a call. type CallArgs struct { - From *common.Address `json:"from"` - To *common.Address `json:"to"` - Gas *hexutil.Uint64 `json:"gas"` - GasPrice *hexutil.Big `json:"gasPrice"` - Value *hexutil.Big `json:"value"` - Data *hexutil.Bytes `json:"data"` - GasPremium *hexutil.Big `json:"gasPremium"` - FeeCap *hexutil.Big `json:"feeCap"` + From *common.Address `json:"from"` + To *common.Address `json:"to"` + Gas *hexutil.Uint64 `json:"gas"` + GasPrice *hexutil.Big `json:"gasPrice"` + Value *hexutil.Big `json:"value"` + Data *hexutil.Bytes `json:"data"` + MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas"` + FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas"` } // ToMessage converts CallArgs to the Message type used by the core evm @@ -777,7 +777,7 @@ func (args *CallArgs) ToMessage(globalGasCap *big.Int) types.Message { } var gasPrice *big.Int - if args.GasPremium == nil { + if args.MaxMinerBribePerGas == nil { gasPrice = new(big.Int).SetUint64(defaultGasPrice) if args.GasPrice != nil { gasPrice = args.GasPrice.ToInt() @@ -795,7 +795,7 @@ func (args *CallArgs) ToMessage(globalGasCap *big.Int) types.Message { } // Create new call message - msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, data, false, (*big.Int)(args.GasPremium), (*big.Int)(args.FeeCap)) + msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, data, false, (*big.Int)(args.MaxMinerBribePerGas), (*big.Int)(args.FeeCapPerGas)) return msg } @@ -822,25 +822,25 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.Blo if eip1559 && b.CurrentBlock().BaseFee() == nil { return nil, core.ErrNoBaseFee } - if eip1559Finalized && (args.GasPremium == nil || args.FeeCap == nil || args.GasPrice != nil) { + if eip1559Finalized && (args.MaxMinerBribePerGas == nil || args.FeeCapPerGas == nil || args.GasPrice != nil) { return nil, core.ErrTxNotEIP1559 } - if !eip1559 && (args.GasPremium != nil || args.FeeCap != nil || args.GasPrice == nil) { + if !eip1559 && (args.MaxMinerBribePerGas != nil || args.FeeCapPerGas != nil || args.GasPrice == nil) { return nil, core.ErrTxIsEIP1559 } - if args.GasPrice != nil && (args.GasPremium != nil || args.FeeCap != nil) { + if args.GasPrice != nil && (args.MaxMinerBribePerGas != nil || args.FeeCapPerGas != nil) { return nil, core.ErrTxSetsLegacyAndEIP1559Fields } - if args.FeeCap != nil && args.GasPremium == nil { + if args.FeeCapPerGas != nil && args.MaxMinerBribePerGas == nil { return nil, errors.New("if FeeCap is set, GasPremium must be set") } - if args.GasPremium != nil { - if args.FeeCap == nil { + if args.MaxMinerBribePerGas != nil { + if args.FeeCapPerGas == nil { return nil, errors.New("if GasPremium is set, FeeCap must be set") } - gasPrice := new(big.Int).Add(b.CurrentBlock().BaseFee(), args.GasPremium.ToInt()) - if gasPrice.Cmp(args.FeeCap.ToInt()) > 0 { - gasPrice.Set(args.FeeCap.ToInt()) + gasPrice := new(big.Int).Add(b.CurrentBlock().BaseFee(), args.MaxMinerBribePerGas.ToInt()) + if gasPrice.Cmp(args.FeeCapPerGas.ToInt()) > 0 { + gasPrice.Set(args.FeeCapPerGas.ToInt()) } if gasPrice.Cmp(b.CurrentBlock().BaseFee()) < 0 { return nil, core.ErrEIP1559GasPriceLessThanBaseFee @@ -1507,8 +1507,8 @@ type SendTxArgs struct { Data *hexutil.Bytes `json:"data"` Input *hexutil.Bytes `json:"input"` // EIP1559 fields - GasPremium *hexutil.Big `json:"gasPremium"` - FeeCap *hexutil.Big `json:"feeCap"` + MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas"` + FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas"` } // setDefaults is a helper function that fills in default values for unspecified tx fields. @@ -1519,25 +1519,25 @@ func (args *SendTxArgs) setDefaults(ctx context.Context, b Backend) error { if eip1559 && b.CurrentBlock().BaseFee() == nil { return core.ErrNoBaseFee } - if eip1559Finalized && (args.GasPremium == nil || args.FeeCap == nil || args.GasPrice != nil) { + if eip1559Finalized && (args.MaxMinerBribePerGas == nil || args.FeeCapPerGas == nil || args.GasPrice != nil) { return core.ErrTxNotEIP1559 } - if !eip1559 && (args.GasPremium != nil || args.FeeCap != nil || args.GasPrice == nil) { + if !eip1559 && (args.MaxMinerBribePerGas != nil || args.FeeCapPerGas != nil || args.GasPrice == nil) { return core.ErrTxIsEIP1559 } - if args.GasPrice != nil && (args.GasPremium != nil || args.FeeCap != nil) { + if args.GasPrice != nil && (args.MaxMinerBribePerGas != nil || args.FeeCapPerGas != nil) { return core.ErrTxSetsLegacyAndEIP1559Fields } - if args.FeeCap != nil && args.GasPremium == nil { + if args.FeeCapPerGas != nil && args.MaxMinerBribePerGas == nil { return errors.New("if FeeCap is set, GasPremium must be set") } - if args.GasPremium != nil { - if args.FeeCap == nil { + if args.MaxMinerBribePerGas != nil { + if args.FeeCapPerGas == nil { return errors.New("if GasPremium is set, FeeCap must be set") } - gasPrice := new(big.Int).Add(b.CurrentBlock().BaseFee(), args.GasPremium.ToInt()) - if gasPrice.Cmp(args.FeeCap.ToInt()) > 0 { - gasPrice.Set(args.FeeCap.ToInt()) + gasPrice := new(big.Int).Add(b.CurrentBlock().BaseFee(), args.MaxMinerBribePerGas.ToInt()) + if gasPrice.Cmp(args.FeeCapPerGas.ToInt()) > 0 { + gasPrice.Set(args.FeeCapPerGas.ToInt()) } if gasPrice.Cmp(b.CurrentBlock().BaseFee()) < 0 { return core.ErrEIP1559GasPriceLessThanBaseFee @@ -1545,7 +1545,7 @@ func (args *SendTxArgs) setDefaults(ctx context.Context, b Backend) error { } // If EIP1559 is activated but not finalized and neither a GasPrice, GasPremium, or FeeCap are provided default to suggesting a GasPrice - if args.GasPrice == nil && args.GasPremium == nil { + if args.GasPrice == nil && args.MaxMinerBribePerGas == nil { price, err := b.SuggestPrice(ctx) if err != nil { return err @@ -1586,13 +1586,13 @@ func (args *SendTxArgs) setDefaults(ctx context.Context, b Backend) error { input = args.Data } callArgs := CallArgs{ - From: &args.From, // From shouldn't be nil - To: args.To, - GasPrice: args.GasPrice, - Value: args.Value, - Data: input, - GasPremium: args.GasPremium, - FeeCap: args.FeeCap, + From: &args.From, // From shouldn't be nil + To: args.To, + GasPrice: args.GasPrice, + Value: args.Value, + Data: input, + MaxMinerBribePerGas: args.MaxMinerBribePerGas, + FeeCapPerGas: args.FeeCapPerGas, } pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) estimated, err := DoEstimateGas(ctx, b, callArgs, pendingBlockNr, b.RPCGasCap()) @@ -1613,9 +1613,9 @@ func (args *SendTxArgs) toTransaction() *types.Transaction { input = *args.Data } if args.To == nil { - return types.NewContractCreation(uint64(*args.Nonce), (*big.Int)(args.Value), uint64(*args.Gas), (*big.Int)(args.GasPrice), input, (*big.Int)(args.GasPremium), (*big.Int)(args.FeeCap)) + return types.NewContractCreation(uint64(*args.Nonce), (*big.Int)(args.Value), uint64(*args.Gas), (*big.Int)(args.GasPrice), input, (*big.Int)(args.MaxMinerBribePerGas), (*big.Int)(args.FeeCapPerGas)) } - return types.NewTransaction(uint64(*args.Nonce), *args.To, (*big.Int)(args.Value), uint64(*args.Gas), (*big.Int)(args.GasPrice), input, (*big.Int)(args.GasPremium), (*big.Int)(args.FeeCap)) + return types.NewTransaction(uint64(*args.Nonce), *args.To, (*big.Int)(args.Value), uint64(*args.Gas), (*big.Int)(args.GasPrice), input, (*big.Int)(args.MaxMinerBribePerGas), (*big.Int)(args.FeeCapPerGas)) } // SubmitTransaction is a helper function that submits tx to txPool and logs a message. From 910c123b3125df80df77479cde348988ce2be65e Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Wed, 28 Oct 2020 18:28:14 +0300 Subject: [PATCH 03/16] Update cost calculation for transaction --- core/state_transition.go | 14 +++++------ core/types/transaction.go | 49 ++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/core/state_transition.go b/core/state_transition.go index 66498e68bac9..97ab112dc810 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -18,10 +18,10 @@ package core import ( "errors" - "math" "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/params" ) @@ -167,12 +167,12 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp, gp1559 *GasPool) *StateTra isEIP1559: isEIP1559, } if isEIP1559 { - //ToDo1559: check it - // EP1559 gasPrice = min(BASEFEE + tx.fee_premium, tx.fee_cap) - st.eip1559GasPrice = new(big.Int).Add(evm.BaseFee, msg.MaxMinerBribe()) - if st.eip1559GasPrice.Cmp(msg.FeeCap()) > 0 { - st.eip1559GasPrice.Set(msg.FeeCap()) - } + // bribe is capped such that base fee is filled first + // bribe_per_gas = min(transaction.max_miner_bribe_per_gas, transaction.fee_cap_per_gas - block.base_fee) + st.eip1559GasPrice = math.BigMin( + new(big.Int).Set(msg.MaxMinerBribe()), + new(big.Int).Sub(msg.FeeCap(), evm.BaseFee), + ) } return st } diff --git a/core/types/transaction.go b/core/types/transaction.go index a7c0beb82f6f..6d20d3d342eb 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -25,6 +25,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" ) @@ -65,16 +66,16 @@ type txdata struct { } type txdataMarshaling struct { - AccountNonce hexutil.Uint64 - Price *hexutil.Big - GasLimit hexutil.Uint64 - Amount *hexutil.Big - Payload hexutil.Bytes - GasPremium *hexutil.Big - FeeCap *hexutil.Big - V *hexutil.Big - R *hexutil.Big - S *hexutil.Big + AccountNonce hexutil.Uint64 + Price *hexutil.Big + GasLimit hexutil.Uint64 + Amount *hexutil.Big + Payload hexutil.Bytes + MaxMinerBribePerGas *hexutil.Big + FeeCapPerGas *hexutil.Big + V *hexutil.Big + R *hexutil.Big + S *hexutil.Big } func NewTransaction(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, maxMinerBribePerGas, feeCapPerGas *big.Int) *Transaction { @@ -370,22 +371,22 @@ func (tx *Transaction) WithSignature(signer Signer, sig []byte) (*Transaction, e // Cost returns amount + gasprice * gaslimit. func (tx *Transaction) Cost(baseFee *big.Int) *big.Int { + gasPrice := new(big.Int) if tx.data.Price != nil { - total := new(big.Int).Mul(tx.data.Price, new(big.Int).SetUint64(tx.data.GasLimit)) - total.Add(total, tx.data.Amount) - return total - } - //ToDo1559: check it - if baseFee != nil && tx.data.MaxMinerBribePerGas != nil && tx.data.FeeCapPerGas != nil { - eip1559GasPrice := new(big.Int).Add(baseFee, tx.data.MaxMinerBribePerGas) - if eip1559GasPrice.Cmp(tx.data.FeeCapPerGas) > 0 { - eip1559GasPrice.Set(tx.data.FeeCapPerGas) - } - total := new(big.Int).Mul(eip1559GasPrice, new(big.Int).SetUint64(tx.data.GasLimit)) - total.Add(total, tx.data.Amount) - return total + gasPrice.Set(tx.data.Price) + } else if baseFee != nil && tx.data.MaxMinerBribePerGas != nil && tx.data.FeeCapPerGas != nil { + gasPrice.Set(math.BigMin( + new(big.Int).Set(tx.data.MaxMinerBribePerGas), + new(big.Int).Sub(tx.data.FeeCapPerGas, baseFee), + )) + } else { + return nil } - return nil + gasLimit := new(big.Int).SetUint64(tx.data.GasLimit) + total := new(big.Int) + total.Mul(gasLimit, gasPrice) + total.Add(total, tx.data.Amount) + return total } // RawSignatureValues returns the V, R, S signature values of the transaction. From 93343b26ce8f785b2e16f547c9007e9d16a04740 Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Wed, 28 Oct 2020 19:05:15 +0300 Subject: [PATCH 04/16] Change gasPremium to maxMinerBribePerGas --- mobile/ethereum.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mobile/ethereum.go b/mobile/ethereum.go index 258ecfa8b637..a5c9c5e78f80 100644 --- a/mobile/ethereum.go +++ b/mobile/ethereum.go @@ -58,8 +58,8 @@ func (msg *CallMsg) GetTo() *Address { } return nil } -func (msg *CallMsg) GetGasPremium() *BigInt { return &BigInt{msg.msg.GasPremium} } -func (msg *CallMsg) GetFeeCap() *BigInt { return &BigInt{msg.msg.FeeCap} } +func (msg *CallMsg) GetMaxMinerBribe() *BigInt { return &BigInt{msg.msg.MaxMinerBribePerGas} } +func (msg *CallMsg) GetFeeCap() *BigInt { return &BigInt{msg.msg.FeeCapPerGas} } func (msg *CallMsg) SetFrom(address *Address) { msg.msg.From = address.address } func (msg *CallMsg) SetGas(gas int64) { msg.msg.Gas = uint64(gas) } @@ -73,8 +73,10 @@ func (msg *CallMsg) SetTo(address *Address) { } msg.msg.To = &address.address } -func (msg *CallMsg) SetGasPremium(gasPremium *BigInt) { msg.msg.GasPremium = gasPremium.bigint } -func (msg *CallMsg) SetFeeCap(feeCap *BigInt) { msg.msg.FeeCap = feeCap.bigint } +func (msg *CallMsg) SetMaxMinerBribe(maxMinerBribe *BigInt) { + msg.msg.MaxMinerBribePerGas = maxMinerBribe.bigint +} +func (msg *CallMsg) SetFeeCap(feeCap *BigInt) { msg.msg.FeeCapPerGas = feeCap.bigint } // SyncProgress gives progress indications when the node is synchronising with // the Ethereum network. From 967f8993bca677e123bfd2ae0e84c598f7d994be Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Wed, 28 Oct 2020 19:05:36 +0300 Subject: [PATCH 05/16] update baseFee calculation --- consensus/misc/basefee.go | 43 +++++++++++++++------------------------ 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/consensus/misc/basefee.go b/consensus/misc/basefee.go index 3b918d74a0ff..585896d1d3f3 100644 --- a/consensus/misc/basefee.go +++ b/consensus/misc/basefee.go @@ -73,41 +73,30 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { return new(big.Int).SetUint64(config.EIP1559.InitialBaseFee) } - parentBaseFee := parent.BaseFee - parentBlockGasUsed := new(big.Int).SetUint64(parent.GasUsed) - targetGasUsed := new(big.Int).SetUint64(parent.GasLimit) - baseFeeMaxChangeDenominator := new(big.Int).SetUint64(config.EIP1559.EIP1559BaseFeeMaxChangeDenominator) - - cmp := parentBlockGasUsed.Cmp(targetGasUsed) + parentBaseFee := new(big.Int).Set(parent.BaseFee) + parentGasUsed := new(big.Int).SetUint64(parent.GasUsed) + parentGasTarget := new(big.Int).SetUint64(parent.GasLimit) + feeDenominator := new(big.Int).SetUint64(config.EIP1559.EIP1559BaseFeeMaxChangeDenominator) + cmp := parentGasUsed.Cmp(parentGasTarget) if cmp == 0 { - return targetGasUsed + return parentBaseFee } if cmp > 0 { - gasDelta := new(big.Int).Sub(parentBlockGasUsed, targetGasUsed) - feeDelta := math.BigMax( - new(big.Int).Div( - new(big.Int).Div( - new(big.Int).Mul(parentBaseFee, gasDelta), - targetGasUsed, - ), - baseFeeMaxChangeDenominator, - ), - common.Big1, - ) + gasDelta := new(big.Int).Sub(parentGasUsed, parentGasTarget) + delta := new(big.Int) + delta.Mul(parentBaseFee, gasDelta) + delta.Div(delta, parentGasTarget) + delta.Div(delta, feeDenominator) + feeDelta := math.BigMax(parentBaseFee, big.NewInt(1)) return new(big.Int).Add(parentBaseFee, feeDelta) } - gasDelta := new(big.Int).Sub(targetGasUsed, parentBlockGasUsed) - feeDelta := new(big.Int).Div( - new(big.Int).Div( - new(big.Int).Mul(parentBaseFee, gasDelta), - targetGasUsed, - ), - baseFeeMaxChangeDenominator, - ) - + gasDelta := new(big.Int).Sub(parentGasTarget, parentGasUsed) + feeDelta := new(big.Int).Mul(parentBaseFee, gasDelta) + feeDelta.Div(feeDelta, parentGasTarget) + feeDelta.Div(feeDelta, feeDenominator) return new(big.Int).Sub(parentBaseFee, feeDelta) } From 56c397578e788b8abbaec55c05f5d1b10ce9a4a8 Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Tue, 3 Nov 2020 16:20:45 +0300 Subject: [PATCH 06/16] fix gasprice calculation for StateTransition --- core/state_transition.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/core/state_transition.go b/core/state_transition.go index 97ab112dc810..a1dd378f3045 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -167,12 +167,14 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp, gp1559 *GasPool) *StateTra isEIP1559: isEIP1559, } if isEIP1559 { - // bribe is capped such that base fee is filled first + // # bribe is capped such that base fee is filled first // bribe_per_gas = min(transaction.max_miner_bribe_per_gas, transaction.fee_cap_per_gas - block.base_fee) - st.eip1559GasPrice = math.BigMin( + // # signer pays both the bribe and the base fee + // effective_gas_price = bribe_per_gas + block.base_fee + st.eip1559GasPrice = new(big.Int).Add(evm.BaseFee, math.BigMin( new(big.Int).Set(msg.MaxMinerBribe()), new(big.Int).Sub(msg.FeeCap(), evm.BaseFee), - ) + )) } return st } From bd1b100c2f9c38cedf825f02c536a969f7e7c0f6 Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Tue, 3 Nov 2020 16:21:15 +0300 Subject: [PATCH 07/16] fix fields for RPCTransaction --- internal/ethapi/api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 5577cad4bed1..dfbf1978b927 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1240,8 +1240,8 @@ type RPCTransaction struct { To *common.Address `json:"to"` TransactionIndex *hexutil.Uint64 `json:"transactionIndex"` Value *hexutil.Big `json:"value"` - MaxMinerBribePerGas *hexutil.Big `json:"gasPremium"` - FeeCapPerGas *hexutil.Big `json:"feeCap"` + MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas"` + FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas"` V *hexutil.Big `json:"v"` R *hexutil.Big `json:"r"` S *hexutil.Big `json:"s"` From b7e2d0aaa0b41e62f226033bfe2faa7c8d306154 Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Tue, 3 Nov 2020 16:44:08 +0300 Subject: [PATCH 08/16] fix CalcBaseFee --- consensus/misc/basefee.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/misc/basefee.go b/consensus/misc/basefee.go index 585896d1d3f3..a7383ca76c49 100644 --- a/consensus/misc/basefee.go +++ b/consensus/misc/basefee.go @@ -89,7 +89,7 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { delta.Mul(parentBaseFee, gasDelta) delta.Div(delta, parentGasTarget) delta.Div(delta, feeDenominator) - feeDelta := math.BigMax(parentBaseFee, big.NewInt(1)) + feeDelta := math.BigMax(delta, big.NewInt(1)) return new(big.Int).Add(parentBaseFee, feeDelta) } From a77d34f5d0136e8084ffef4d904b8f9c566568dd Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Tue, 3 Nov 2020 16:44:59 +0300 Subject: [PATCH 09/16] fix transaction fields for test --- core/chain_makers_test.go | 12 ++++++------ core/tx_pool_test.go | 4 ++-- core/types/transaction_test.go | 8 ++++---- signer/core/api_test.go | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/core/chain_makers_test.go b/core/chain_makers_test.go index 475404bf0d23..f198b56a722c 100644 --- a/core/chain_makers_test.go +++ b/core/chain_makers_test.go @@ -245,8 +245,8 @@ func generateChainDuringTransition(t *testing.T) { if state.GetBalance(addr1).Uint64() != 989000 { t.Fatalf("expected balance of addr1 to equal %d got %d", 989000, state.GetBalance(addr1).Uint64()) } - if state.GetBalance(addr2).Uint64() != 4901403728000 { - t.Fatalf("expected balance of addr2 to equal %d got %d", 4901403728000, state.GetBalance(addr2).Uint64()) + if state.GetBalance(addr2).Uint64() != 4911639338000 { + t.Fatalf("expected balance of addr2 to equal %d got %d", 4911639338000, state.GetBalance(addr2).Uint64()) } // This value is different because the test config we use has Constantinople active (uses ConstantinopleBlockReward) bal, _ := new(big.Int).SetString("7875000000000001000", 10) @@ -389,11 +389,11 @@ func generateChainAfterFinalization2(t *testing.T) { if blockchain.CurrentBlock().Number().Uint64() != 5 { t.Fatalf("expected last block to equal %d got %d", 5, blockchain.CurrentBlock().Number().Uint64()) } - if state.GetBalance(addr1).Uint64() != 7536639348000 { - t.Fatalf("expected balance of addr1 to equal %d got %d", 7536639348000, state.GetBalance(addr1).Uint64()) + if state.GetBalance(addr1).Uint64() != 7536639327000 { + t.Fatalf("expected balance of addr1 to equal %d got %d", 7536639327000, state.GetBalance(addr1).Uint64()) } - if state.GetBalance(addr2).Uint64() != 4911639359000 { - t.Fatalf("expected balance of addr2 to equal %d got %d", 4911639359000, state.GetBalance(addr2).Uint64()) + if state.GetBalance(addr2).Uint64() != 4911639338000 { + t.Fatalf("expected balance of addr2 to equal %d got %d", 4911639338000, state.GetBalance(addr2).Uint64()) } // This value is different than in TestGenerateChain because the test config we use has Constantinople active (uses ConstantinopleBlockReward) bal, _ := new(big.Int).SetString("7875000000000001000", 10) diff --git a/core/tx_pool_test.go b/core/tx_pool_test.go index 746488b62dcb..3662728c345d 100644 --- a/core/tx_pool_test.go +++ b/core/tx_pool_test.go @@ -451,7 +451,7 @@ func TestInvalidTransactionsEIP1559(t *testing.T) { } fakeBaseFee := big.NewInt(5) - eip1559GasPrice := new(big.Int).Add(fakeBaseFee, tx.GasPremium()) + eip1559GasPrice := new(big.Int).Add(fakeBaseFee, tx.MaxMinerBribe()) balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(new(big.Int).SetUint64(tx.Gas()), eip1559GasPrice)) pool.currentState.AddBalance(from, balance) @@ -518,7 +518,7 @@ func TestInvalidTransactionsEIP1559Finalized(t *testing.T) { } fakeBaseFee := big.NewInt(5) - eip1559GasPrice := new(big.Int).Add(fakeBaseFee, tx.GasPremium()) + eip1559GasPrice := new(big.Int).Add(fakeBaseFee, tx.MaxMinerBribe()) balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(new(big.Int).SetUint64(tx.Gas()), eip1559GasPrice)) pool.currentState.AddBalance(from, balance) diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index 08472c427eff..91ef0386fd78 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -152,10 +152,10 @@ func TestEIP1159TransactionDecode(t *testing.T) { if err != nil { t.Fatal(err) } - if tx.data.FeeCap == nil || tx.data.FeeCap.Cmp(eip1559Tx.data.FeeCap) != 0 { + if tx.data.FeeCapPerGas == nil || tx.data.FeeCapPerGas.Cmp(eip1559Tx.data.FeeCapPerGas) != 0 { t.Fatal("unexpected FeeCap") } - if tx.data.GasPremium == nil || tx.data.GasPremium.Cmp(eip1559Tx.data.GasPremium) != 0 { + if tx.data.MaxMinerBribePerGas == nil || tx.data.MaxMinerBribePerGas.Cmp(eip1559Tx.data.MaxMinerBribePerGas) != 0 { t.Fatal("unexpected GasPremium") } if tx.data.Price != nil { @@ -370,13 +370,13 @@ func TestTransactionPriceNonceSort(t *testing.T) { iPrice := txi.GasPrice() nextPrice := next.GasPrice() if iPrice == nil { - iPrice = new(big.Int).Add(baseFee, txi.GasPremium()) + iPrice = new(big.Int).Add(baseFee, txi.MaxMinerBribe()) if iPrice.Cmp(txi.FeeCap()) > 0 { iPrice.Set(txi.FeeCap()) } } if nextPrice == nil { - nextPrice = new(big.Int).Add(baseFee, next.GasPremium()) + nextPrice = new(big.Int).Add(baseFee, next.MaxMinerBribe()) if nextPrice.Cmp(next.FeeCap()) > 0 { nextPrice.Set(next.FeeCap()) } diff --git a/signer/core/api_test.go b/signer/core/api_test.go index 52037b557b4d..cd984e0e3808 100644 --- a/signer/core/api_test.go +++ b/signer/core/api_test.go @@ -237,8 +237,8 @@ func mkTestTx(from common.MixedcaseAddress, eip1559 bool) core.SendTxArgs { Data: &data, Nonce: nonce} if eip1559 { - tx.GasPremium = (*hexutil.Big)(big.NewInt(1000000000)) - tx.FeeCap = (*hexutil.Big)(big.NewInt(2000000000)) + tx.MaxMinerBribePerGas = (*hexutil.Big)(big.NewInt(1000000000)) + tx.FeeCapPerGas = (*hexutil.Big)(big.NewInt(2000000000)) } else { tx.GasPrice = (*hexutil.Big)(big.NewInt(2000000000)) } From 64f95c06915cd543596247eaf656e549c114741d Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Wed, 4 Nov 2020 01:30:55 +0300 Subject: [PATCH 10/16] add additional func computeBaseFee --- consensus/misc/basefee.go | 63 +++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/consensus/misc/basefee.go b/consensus/misc/basefee.go index a7383ca76c49..ee165a949535 100644 --- a/consensus/misc/basefee.go +++ b/consensus/misc/basefee.go @@ -18,6 +18,7 @@ package misc import ( "errors" + "math/big" "github.com/ethereum/go-ethereum/common" @@ -59,6 +60,37 @@ func VerifyEIP1559BaseFee(config *params.ChainConfig, header, parent *types.Head return nil } +func computeBaseFee(pBaseFee *big.Int, pGasUsed, pGasTarget, denominator uint64) *big.Int { + var baseFee *big.Int + if pGasUsed == pGasTarget { + baseFee = new(big.Int).Set(pBaseFee) + } else if pGasUsed > pGasTarget { + gasDelta := big.NewInt(int64(pGasUsed) - int64(pGasTarget)) + feeDelta := math.BigMax( + new(big.Int).Div( + new(big.Int).Mul(pBaseFee, gasDelta), + new(big.Int).Mul( + new(big.Int).SetUint64(pGasTarget), + new(big.Int).SetUint64(denominator), + ), + ), + big.NewInt(1), + ) + baseFee = new(big.Int).Add(pBaseFee, feeDelta) + } else { + gasDelta := big.NewInt(int64(pGasTarget) - int64(pGasUsed)) + feeDelta := new(big.Int).Div( + new(big.Int).Mul(pBaseFee, gasDelta), + new(big.Int).Mul( + new(big.Int).SetUint64(pGasTarget), + new(big.Int).SetUint64(denominator), + ), + ) + baseFee = new(big.Int).Sub(pBaseFee, feeDelta) + } + return baseFee +} + // CalcBaseFee returns the baseFee for the current block provided the parent header and config parameters func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { height := new(big.Int).Add(parent.Number, common.Big1) @@ -73,31 +105,12 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { return new(big.Int).SetUint64(config.EIP1559.InitialBaseFee) } - parentBaseFee := new(big.Int).Set(parent.BaseFee) - parentGasUsed := new(big.Int).SetUint64(parent.GasUsed) - parentGasTarget := new(big.Int).SetUint64(parent.GasLimit) - feeDenominator := new(big.Int).SetUint64(config.EIP1559.EIP1559BaseFeeMaxChangeDenominator) - cmp := parentGasUsed.Cmp(parentGasTarget) - - if cmp == 0 { - return parentBaseFee - } - - if cmp > 0 { - gasDelta := new(big.Int).Sub(parentGasUsed, parentGasTarget) - delta := new(big.Int) - delta.Mul(parentBaseFee, gasDelta) - delta.Div(delta, parentGasTarget) - delta.Div(delta, feeDenominator) - feeDelta := math.BigMax(delta, big.NewInt(1)) - return new(big.Int).Add(parentBaseFee, feeDelta) - } - - gasDelta := new(big.Int).Sub(parentGasTarget, parentGasUsed) - feeDelta := new(big.Int).Mul(parentBaseFee, gasDelta) - feeDelta.Div(feeDelta, parentGasTarget) - feeDelta.Div(feeDelta, feeDenominator) - return new(big.Int).Sub(parentBaseFee, feeDelta) + return computeBaseFee( + parent.BaseFee, + parent.GasUsed, + parent.GasLimit, + config.EIP1559.EIP1559BaseFeeMaxChangeDenominator, + ) } // CalcEIP1559GasTarget returns the EIP1559GasTarget at the current height and header.GasLimit From 0903be3c1f15dfae5e05b1984989ff04cd3e37be Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Wed, 4 Nov 2020 01:32:07 +0300 Subject: [PATCH 11/16] fix baseFee values for TestCalcBaseFee --- consensus/misc/basefee_test.go | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/consensus/misc/basefee_test.go b/consensus/misc/basefee_test.go index fd5b355b49ee..5617e0a476ea 100644 --- a/consensus/misc/basefee_test.go +++ b/consensus/misc/basefee_test.go @@ -155,7 +155,7 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(1000000000), 1000000, 10000000, - big.NewInt(1125000000), + big.NewInt(2125000000), }, { params.EIP1559ChainConfig, @@ -165,7 +165,7 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(1000000000), 500000, 10000000, - big.NewInt(1125000000), + big.NewInt(3375000000), }, { params.EIP1559ChainConfig, @@ -175,7 +175,7 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(1000000000), 1000000, 10000000, - big.NewInt(1125000000), + big.NewInt(2125000000), }, { params.EIP1559ChainConfig, @@ -217,7 +217,7 @@ func TestCalcBaseFee(t *testing.T) { 10000000, big.NewInt(1013888888), }, - { + { // 10 params.EIP1559ChainConfig, big.NewInt(1000), 1000, @@ -225,7 +225,7 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(1000000000), 10000000, 10000000, - big.NewInt(999999999), // baseFee diff is -1 when usage == target + big.NewInt(1000000000), // baseFee diff is -1 when usage == target }, { params.EIP1559ChainConfig, @@ -235,7 +235,7 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(1000000000), 11000000, 10000000, - big.NewInt(988636363), + big.NewInt(988636364), }, { params.EIP1559ChainConfig, @@ -245,7 +245,7 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(900000000), 1000000, 10000000, - big.NewInt(1012500000), + big.NewInt(1912500000), }, { params.EIP1559ChainConfig, @@ -255,7 +255,7 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(1100000000), 1000000, 10000000, - big.NewInt(1237500000), + big.NewInt(2337500000), }, { params.EIP1559ChainConfig, @@ -265,7 +265,7 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(1200000000), 1000000, 10000000, - big.NewInt(1350000000), + big.NewInt(2550000000), }, { params.EIP1559ChainConfig, @@ -328,7 +328,7 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(0), 1000000000000000, 1, - big.NewInt(1), + big.NewInt(0), }, // parent gas usage == parent gas limit // parent baseFee == 0 @@ -355,11 +355,8 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(1), 1, 1000000000000000, - big.NewInt(2), + big.NewInt(125000000000000), }, - // parent gas usage <<<< parent gas limit - // parent baseFee == 1 - // as expected, decrement by 1 { params.EIP1559ChainConfig, big.NewInt(1000), @@ -368,11 +365,8 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(1), 1000000000000000, 1, - big.NewInt(0), + big.NewInt(1), }, - // parent gas usage == parent gas limit - // parent baseFee == 1 - // as expected, decrement by 1 when gas usage equals the gas target { params.EIP1559ChainConfig, big.NewInt(1000), @@ -381,7 +375,7 @@ func TestCalcBaseFee(t *testing.T) { big.NewInt(1), 1, 1, - big.NewInt(0), + big.NewInt(1), }, } for i, test := range testConditions { From 26756e9dfdbc137bf3baf25277bdb15537cba4a2 Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Wed, 4 Nov 2020 17:58:42 +0300 Subject: [PATCH 12/16] Fix transaction cost calculation --- core/types/transaction.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/core/types/transaction.go b/core/types/transaction.go index 6d20d3d342eb..09d66df3b090 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -371,22 +371,25 @@ func (tx *Transaction) WithSignature(signer Signer, sig []byte) (*Transaction, e // Cost returns amount + gasprice * gaslimit. func (tx *Transaction) Cost(baseFee *big.Int) *big.Int { - gasPrice := new(big.Int) if tx.data.Price != nil { - gasPrice.Set(tx.data.Price) - } else if baseFee != nil && tx.data.MaxMinerBribePerGas != nil && tx.data.FeeCapPerGas != nil { - gasPrice.Set(math.BigMin( + total := new(big.Int).Mul(tx.data.Price, new(big.Int).SetUint64(tx.data.GasLimit)) + total.Add(total, tx.data.Amount) + return total + } + if baseFee != nil && tx.data.MaxMinerBribePerGas != nil && tx.data.FeeCapPerGas != nil { + // # bribe is capped such that base fee is filled first + // bribe_per_gas = min(transaction.max_miner_bribe_per_gas, transaction.fee_cap_per_gas - block.base_fee) + // # signer pays both the bribe and the base fee + // effective_gas_price = bribe_per_gas + block.base_fee + eip1559GasPrice := new(big.Int).Add(baseFee, math.BigMin( new(big.Int).Set(tx.data.MaxMinerBribePerGas), new(big.Int).Sub(tx.data.FeeCapPerGas, baseFee), )) - } else { - return nil + total := new(big.Int).Mul(eip1559GasPrice, new(big.Int).SetUint64(tx.data.GasLimit)) + total.Add(total, tx.data.Amount) + return total } - gasLimit := new(big.Int).SetUint64(tx.data.GasLimit) - total := new(big.Int) - total.Mul(gasLimit, gasPrice) - total.Add(total, tx.data.Amount) - return total + return nil } // RawSignatureValues returns the V, R, S signature values of the transaction. From 08989db7d4b6f50cdc523fa76d69eb127771504c Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Wed, 4 Nov 2020 18:03:39 +0300 Subject: [PATCH 13/16] add infomation about eip version --- cmd/geth/misccmd.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/geth/misccmd.go b/cmd/geth/misccmd.go index 0e7ee965133c..38729ecfca98 100644 --- a/cmd/geth/misccmd.go +++ b/cmd/geth/misccmd.go @@ -115,6 +115,7 @@ func version(ctx *cli.Context) error { if gitDate != "" { fmt.Println("Git Commit Date:", gitDate) } + fmt.Println("EIP1559: 970c594cd3") fmt.Println("Architecture:", runtime.GOARCH) fmt.Println("Protocol Versions:", eth.ProtocolVersions) fmt.Println("Go Version:", runtime.Version()) From 4d70c96dc3b7c2edd650cc94f64d12138cbaddcd Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Wed, 4 Nov 2020 21:39:02 +0300 Subject: [PATCH 14/16] use camelCase-style for json tags --- core/types/gen_tx_json.go | 8 ++++---- core/types/transaction.go | 4 ++-- ethclient/ethclient.go | 4 ++-- internal/ethapi/api.go | 12 ++++++------ signer/core/types.go | 4 ++-- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/core/types/gen_tx_json.go b/core/types/gen_tx_json.go index fd2d43f67b8f..a4c93724964c 100644 --- a/core/types/gen_tx_json.go +++ b/core/types/gen_tx_json.go @@ -22,8 +22,8 @@ func (t txdata) MarshalJSON() ([]byte, error) { Recipient *common.Address `json:"to" rlp:"nil"` Amount *hexutil.Big `json:"value" gencodec:"required"` Payload hexutil.Bytes `json:"input" gencodec:"required"` - MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas" rlp:"nil"` - FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas" rlp:"nil"` + MaxMinerBribePerGas *hexutil.Big `json:"maxMinerBribePerGas" rlp:"nil"` + FeeCapPerGas *hexutil.Big `json:"feeCapPerGas" rlp:"nil"` V *hexutil.Big `json:"v" gencodec:"required"` R *hexutil.Big `json:"r" gencodec:"required"` S *hexutil.Big `json:"s" gencodec:"required"` @@ -54,8 +54,8 @@ func (t *txdata) UnmarshalJSON(input []byte) error { Recipient *common.Address `json:"to" rlp:"nil"` Amount *hexutil.Big `json:"value" gencodec:"required"` Payload *hexutil.Bytes `json:"input" gencodec:"required"` - MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas" rlp:"nil"` - FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas" rlp:"nil"` + MaxMinerBribePerGas *hexutil.Big `json:"maxMinerBribePerGas" rlp:"nil"` + FeeCapPerGas *hexutil.Big `json:"feeCapPerGas" rlp:"nil"` V *hexutil.Big `json:"v" gencodec:"required"` R *hexutil.Big `json:"r" gencodec:"required"` S *hexutil.Big `json:"s" gencodec:"required"` diff --git a/core/types/transaction.go b/core/types/transaction.go index 09d66df3b090..4c5d0ca27200 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -53,8 +53,8 @@ type txdata struct { Payload []byte `json:"input" gencodec:"required"` // EIP1559 gas values - MaxMinerBribePerGas *big.Int `json:"max_miner_bribe_per_gas" rlp:"nil"` - FeeCapPerGas *big.Int `json:"fee_cap_per_gas" rlp:"nil"` + MaxMinerBribePerGas *big.Int `json:"maxMinerBribePerGas" rlp:"nil"` + FeeCapPerGas *big.Int `json:"feeCapPerGas" rlp:"nil"` // Signature values V *big.Int `json:"v" gencodec:"required"` diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 1844539492ae..13e74c91c2d1 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -565,10 +565,10 @@ func toCallArg(msg ethereum.CallMsg) interface{} { arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice) } if msg.MaxMinerBribePerGas != nil { - arg["max_miner_bribe_per_gas"] = (*hexutil.Big)(msg.MaxMinerBribePerGas) + arg["maxMinerBribePerGas"] = (*hexutil.Big)(msg.MaxMinerBribePerGas) } if msg.FeeCapPerGas != nil { - arg["fee_cap_per_gas"] = (*hexutil.Big)(msg.FeeCapPerGas) + arg["feeCapPerGas"] = (*hexutil.Big)(msg.FeeCapPerGas) } return arg } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index dfbf1978b927..e0172be36f4e 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -754,8 +754,8 @@ type CallArgs struct { GasPrice *hexutil.Big `json:"gasPrice"` Value *hexutil.Big `json:"value"` Data *hexutil.Bytes `json:"data"` - MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas"` - FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas"` + MaxMinerBribePerGas *hexutil.Big `json:"maxMinerBribePerGas"` + FeeCapPerGas *hexutil.Big `json:"feeCapPerGas"` } // ToMessage converts CallArgs to the Message type used by the core evm @@ -1240,8 +1240,8 @@ type RPCTransaction struct { To *common.Address `json:"to"` TransactionIndex *hexutil.Uint64 `json:"transactionIndex"` Value *hexutil.Big `json:"value"` - MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas"` - FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas"` + MaxMinerBribePerGas *hexutil.Big `json:"maxMinerBribePerGas"` + FeeCapPerGas *hexutil.Big `json:"feeCapPerGas"` V *hexutil.Big `json:"v"` R *hexutil.Big `json:"r"` S *hexutil.Big `json:"s"` @@ -1507,8 +1507,8 @@ type SendTxArgs struct { Data *hexutil.Bytes `json:"data"` Input *hexutil.Bytes `json:"input"` // EIP1559 fields - MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas"` - FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas"` + MaxMinerBribePerGas *hexutil.Big `json:"maxMinerBribePerGas"` + FeeCapPerGas *hexutil.Big `json:"feeCapPerGas"` } // setDefaults is a helper function that fills in default values for unspecified tx fields. diff --git a/signer/core/types.go b/signer/core/types.go index 4980765b289c..4497ac47bfcf 100644 --- a/signer/core/types.go +++ b/signer/core/types.go @@ -77,8 +77,8 @@ type SendTxArgs struct { Data *hexutil.Bytes `json:"data"` Input *hexutil.Bytes `json:"input,omitempty"` // EIP1559 fields - MaxMinerBribePerGas *hexutil.Big `json:"max_miner_bribe_per_gas"` - FeeCapPerGas *hexutil.Big `json:"fee_cap_per_gas"` + MaxMinerBribePerGas *hexutil.Big `json:"maxMinerBribePerGas"` + FeeCapPerGas *hexutil.Big `json:"feeCapPerGas"` } func (args SendTxArgs) String() string { From 2c13ddc647432107200d04a871b8934ccfd8cfe0 Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Wed, 4 Nov 2020 21:41:20 +0300 Subject: [PATCH 15/16] remove eip info from "version"-command --- cmd/geth/misccmd.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/geth/misccmd.go b/cmd/geth/misccmd.go index 38729ecfca98..0e7ee965133c 100644 --- a/cmd/geth/misccmd.go +++ b/cmd/geth/misccmd.go @@ -115,7 +115,6 @@ func version(ctx *cli.Context) error { if gitDate != "" { fmt.Println("Git Commit Date:", gitDate) } - fmt.Println("EIP1559: 970c594cd3") fmt.Println("Architecture:", runtime.GOARCH) fmt.Println("Protocol Versions:", eth.ProtocolVersions) fmt.Println("Go Version:", runtime.Version()) From 5702a4673ff99cd62df271d0aa1a3cb524d8cd02 Mon Sep 17 00:00:00 2001 From: Ilnur Galiev Date: Wed, 4 Nov 2020 21:43:37 +0300 Subject: [PATCH 16/16] update VesionMeta --- params/version.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/params/version.go b/params/version.go index af519f02b108..35312f964d30 100644 --- a/params/version.go +++ b/params/version.go @@ -21,10 +21,10 @@ import ( ) const ( - VersionMajor = 1 // Major version component of the current release - VersionMinor = 9 // Minor version component of the current release - VersionPatch = 15 // Patch version component of the current release - VersionMeta = "stable" // Version metadata to append to the version string + VersionMajor = 1 // Major version component of the current release + VersionMinor = 9 // Minor version component of the current release + VersionPatch = 15 // Patch version component of the current release + VersionMeta = "eip1559-970c594cd3" // Version metadata to append to the version string ) // Version holds the textual version string.