From d11eea02093aaa9c93b91f544d686cc3df9f8069 Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Sun, 30 May 2021 03:19:55 +0200 Subject: [PATCH 1/5] core: add new eip-1559 tx constraints --- core/error.go | 12 ++++++++++++ core/state_processor_test.go | 20 ++++++++++++++++++++ core/state_transition.go | 12 ++++++++++++ core/tx_pool.go | 11 +++++++---- core/tx_pool_test.go | 20 ++++++++++++++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/core/error.go b/core/error.go index 3d62cb9bcfaf..62bc0568943e 100644 --- a/core/error.go +++ b/core/error.go @@ -72,6 +72,18 @@ var ( // current network configuration. ErrTxTypeNotSupported = types.ErrTxTypeNotSupported + // ErrTipAboveFeeCap is a sanity error to ensure no one is able to specify a + // transaction with a tip higher than the total fee cap. + ErrTipAboveFeeCap = errors.New("tip higher than fee cap") + + // ErrTipVeryHigh is a sanity error to avoid extremely big numbers specified + // in the tip field. + ErrTipVeryHigh = errors.New("tip higher than 2^256-1") + + // ErrFeeCapVeryHigh is a sanity error to avoid extremely big numbers specified + // in the fee cap field. + ErrFeeCapVeryHigh = errors.New("fee cap higher than 2^256-1") + // ErrFeeCapTooLow is returned if the transaction fee cap is less than the // the base fee of the block. ErrFeeCapTooLow = errors.New("fee cap less than block base fee") diff --git a/core/state_processor_test.go b/core/state_processor_test.go index ab446eb0a209..8e55e7075a35 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -89,6 +89,8 @@ func TestStateProcessorErrors(t *testing.T) { ) defer blockchain.Stop() + veryBigNumber := big.NewInt(1) + veryBigNumber.Lsh(veryBigNumber, 300) for i, tt := range []struct { txs []*types.Transaction want string @@ -146,6 +148,24 @@ func TestStateProcessorErrors(t *testing.T) { }, want: "could not apply tx 0 [0x21e9b9015150fc7f6bd5059890a5e1727f2452df285e8a84f4ca61a74c159ded]: fee cap less than block base fee: address 0x71562b71999873DB5b286dF957af199Ec94617F7, feeCap: 0 baseFee: 875000000", }, + { // ErrTipVeryHigh + txs: []*types.Transaction{ + mkDynamicTx(0, common.Address{}, params.TxGas-1000, veryBigNumber, big.NewInt(1)), + }, + want: "could not apply tx 0 [0xf1d416d1f548bb9ea84cdd8d1c6ad370caa8f670b831dd8048f71d52e43c5e2b]: tip higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tip byte length: 38", + }, + { // ErrFeeCapVeryHigh + txs: []*types.Transaction{ + mkDynamicTx(0, common.Address{}, params.TxGas-1000, big.NewInt(1), veryBigNumber), + }, + want: "could not apply tx 0 [0x6d1cba2357c510a18eee03894057c3acaad2db6624080467da3e3a3ba84ec7ca]: fee cap higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, feeCap byte length: 38", + }, + { // ErrTipAboveFeeCap + txs: []*types.Transaction{ + mkDynamicTx(0, common.Address{}, params.TxGas-1000, big.NewInt(2), big.NewInt(1)), + }, + want: "could not apply tx 0 [0xa7ef91f7636e56676ba73f16e14e74d5556d3def48819a77217962580b19339f]: tip higher than fee cap: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tip: 1, feeCap: 2", + }, } { block := GenerateBadBlock(genesis, ethash.NewFaker(), tt.txs, gspec.Config) _, err := blockchain.InsertChain(types.Blocks{block}) diff --git a/core/state_transition.go b/core/state_transition.go index 881d34f4bcd8..ac5d0c69e19f 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -215,6 +215,18 @@ func (st *StateTransition) preCheck() error { } // Make sure that transaction feeCap is greater than the baseFee (post london) if st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) { + if l := len(st.feeCap.Bytes()); l > 32 { + return fmt.Errorf("%w: address %v, feeCap byte length: %d", ErrFeeCapVeryHigh, + st.msg.From().Hex(), l) + } + if l := len(st.tip.Bytes()); l > 32 { + return fmt.Errorf("%w: address %v, tip byte length: %d", ErrTipVeryHigh, + st.msg.From().Hex(), l) + } + if st.feeCap.Cmp(st.tip) < 0 { + return fmt.Errorf("%w: address %v, tip: %s, feeCap: %s", ErrTipAboveFeeCap, + st.msg.From().Hex(), st.feeCap, st.tip) + } // This will panic if baseFee is nil, but basefee presence is verified // as part of header validation. if st.feeCap.Cmp(st.evm.Context.BaseFee) < 0 { diff --git a/core/tx_pool.go b/core/tx_pool.go index 545b7c6263a6..32aa12856c70 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -83,10 +83,6 @@ var ( // than some meaningful limit a user might use. This is not a consensus error // making the transaction invalid, rather a DOS protection. ErrOversizedData = errors.New("oversized data") - - // ErrTipAboveFeeCap is a sanity error to ensure no one is able to specify a - // transaction with a tip higher than the total fee cap. - ErrTipAboveFeeCap = errors.New("tip higher than fee cap") ) var ( @@ -559,6 +555,13 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error { if pool.currentMaxGas < tx.Gas() { return ErrGasLimit } + // Sanity check for extremely large numbers + if len(tx.FeeCap().Bytes()) > 32 { + return ErrFeeCapVeryHigh + } + if len(tx.Tip().Bytes()) > 32 { + return ErrTipVeryHigh + } // Ensure feeCap is less than or equal to tip. if tx.FeeCapIntCmp(tx.Tip()) < 0 { return ErrTipAboveFeeCap diff --git a/core/tx_pool_test.go b/core/tx_pool_test.go index 380ba7167743..b23d40cd4289 100644 --- a/core/tx_pool_test.go +++ b/core/tx_pool_test.go @@ -395,6 +395,26 @@ func TestTransactionTipAboveFeeCap(t *testing.T) { } } +func TestTransactionVeryHighValues(t *testing.T) { + t.Parallel() + + pool, key := setupTxPoolWithConfig(eip1559Config) + defer pool.Stop() + + veryBigNumber := big.NewInt(1) + veryBigNumber.Lsh(veryBigNumber, 300) + + tx := dynamicFeeTx(0, 100, big.NewInt(1), veryBigNumber, key) + if err := pool.AddRemote(tx); err != ErrTipVeryHigh { + t.Error("expected", ErrTipVeryHigh, "got", err) + } + + tx2 := dynamicFeeTx(0, 100, veryBigNumber, big.NewInt(1), key) + if err := pool.AddRemote(tx2); err != ErrFeeCapVeryHigh { + t.Error("expected", ErrFeeCapVeryHigh, "got", err) + } +} + func TestTransactionChainFork(t *testing.T) { t.Parallel() From 1427d37092044c27d817c0e67400508122be0ceb Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Sun, 30 May 2021 11:07:02 +0200 Subject: [PATCH 2/5] core: add missing constraint check --- core/state_processor_test.go | 26 ++++++++++++++++---------- core/state_transition.go | 3 ++- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 8e55e7075a35..7cd009f30f1c 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -66,7 +66,7 @@ func TestStateProcessorErrors(t *testing.T) { Nonce: nonce, Tip: tip, FeeCap: feeCap, - Gas: 0, + Gas: gasLimit, To: &to, Value: big.NewInt(0), }), signer, testKey) @@ -144,27 +144,33 @@ func TestStateProcessorErrors(t *testing.T) { }, { // ErrFeeCapTooLow txs: []*types.Transaction{ - mkDynamicTx(0, common.Address{}, params.TxGas-1000, big.NewInt(0), big.NewInt(0)), + mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(0), big.NewInt(0)), }, - want: "could not apply tx 0 [0x21e9b9015150fc7f6bd5059890a5e1727f2452df285e8a84f4ca61a74c159ded]: fee cap less than block base fee: address 0x71562b71999873DB5b286dF957af199Ec94617F7, feeCap: 0 baseFee: 875000000", + want: "could not apply tx 0 [0xc4ab868fef0c82ae0387b742aee87907f2d0fc528fc6ea0a021459fb0fc4a4a8]: fee cap less than block base fee: address 0x71562b71999873DB5b286dF957af199Ec94617F7, feeCap: 0 baseFee: 875000000", }, { // ErrTipVeryHigh txs: []*types.Transaction{ - mkDynamicTx(0, common.Address{}, params.TxGas-1000, veryBigNumber, big.NewInt(1)), + mkDynamicTx(0, common.Address{}, params.TxGas, veryBigNumber, big.NewInt(1)), }, - want: "could not apply tx 0 [0xf1d416d1f548bb9ea84cdd8d1c6ad370caa8f670b831dd8048f71d52e43c5e2b]: tip higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tip byte length: 38", + want: "could not apply tx 0 [0x56a98c4e7714c63ebd41e56c7ab399e237a690b68139f2a9e3bfeab01ade8473]: tip higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tip byte length: 38", }, { // ErrFeeCapVeryHigh txs: []*types.Transaction{ - mkDynamicTx(0, common.Address{}, params.TxGas-1000, big.NewInt(1), veryBigNumber), + mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(1), veryBigNumber), }, - want: "could not apply tx 0 [0x6d1cba2357c510a18eee03894057c3acaad2db6624080467da3e3a3ba84ec7ca]: fee cap higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, feeCap byte length: 38", + want: "could not apply tx 0 [0x41dcd104694d9ed0cd2a7957707483939eae5f57d8de625f56e75b88a7709ac0]: fee cap higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, feeCap byte length: 38", }, { // ErrTipAboveFeeCap txs: []*types.Transaction{ - mkDynamicTx(0, common.Address{}, params.TxGas-1000, big.NewInt(2), big.NewInt(1)), + mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(2), big.NewInt(1)), + }, + want: "could not apply tx 0 [0xf987a31ff0c71895780a7612f965a0c8b056deb54e020bb44fa478092f14c9b4]: tip higher than fee cap: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tip: 1, feeCap: 2", + }, + { // ErrInsufficientFunds + txs: []*types.Transaction{ + mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(1), big.NewInt(1000000000000000000)), }, - want: "could not apply tx 0 [0xa7ef91f7636e56676ba73f16e14e74d5556d3def48819a77217962580b19339f]: tip higher than fee cap: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tip: 1, feeCap: 2", + want: "could not apply tx 0 [0xbe93a7a024ea94e4156851ceab721dd300abd8509a6fa4216a58152982619973]: insufficient funds for gas * price + value: address 0x71562b71999873DB5b286dF957af199Ec94617F7 have 1000000000000000000 want 21000000000000000000000", }, } { block := GenerateBadBlock(genesis, ethash.NewFaker(), tt.txs, gspec.Config) @@ -214,7 +220,7 @@ func TestStateProcessorErrors(t *testing.T) { txs: []*types.Transaction{ mkDynamicTx(0, common.Address{}, params.TxGas-1000, big.NewInt(0), big.NewInt(0)), }, - want: "could not apply tx 0 [0x21e9b9015150fc7f6bd5059890a5e1727f2452df285e8a84f4ca61a74c159ded]: transaction type not supported", + want: "could not apply tx 0 [0x88626ac0d53cb65308f2416103c62bb1f18b805573d4f96a3640bbbfff13c14f]: transaction type not supported", }, } { block := GenerateBadBlock(genesis, ethash.NewFaker(), tt.txs, gspec.Config) diff --git a/core/state_transition.go b/core/state_transition.go index ac5d0c69e19f..5c52ba090d70 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -188,7 +188,8 @@ func (st *StateTransition) to() common.Address { func (st *StateTransition) buyGas() error { mgval := new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), st.gasPrice) - if have, want := st.state.GetBalance(st.msg.From()), mgval; have.Cmp(want) < 0 { + balanceReq := new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), st.feeCap) + if have, want := st.state.GetBalance(st.msg.From()), balanceReq; have.Cmp(want) < 0 { return fmt.Errorf("%w: address %v have %v want %v", ErrInsufficientFunds, st.msg.From().Hex(), have, want) } if err := st.gp.SubGas(st.msg.Gas()); err != nil { From 1eed6e387e78e1a809c3b8d0a1f41f9851053936 Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Sun, 30 May 2021 11:10:21 +0200 Subject: [PATCH 3/5] core: use bit length check instead of byte length --- core/state_processor_test.go | 4 ++-- core/state_transition.go | 8 ++++---- core/tx_pool.go | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 7cd009f30f1c..cd8941c0537b 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -152,13 +152,13 @@ func TestStateProcessorErrors(t *testing.T) { txs: []*types.Transaction{ mkDynamicTx(0, common.Address{}, params.TxGas, veryBigNumber, big.NewInt(1)), }, - want: "could not apply tx 0 [0x56a98c4e7714c63ebd41e56c7ab399e237a690b68139f2a9e3bfeab01ade8473]: tip higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tip byte length: 38", + want: "could not apply tx 0 [0x56a98c4e7714c63ebd41e56c7ab399e237a690b68139f2a9e3bfeab01ade8473]: tip higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tip bit length: 301", }, { // ErrFeeCapVeryHigh txs: []*types.Transaction{ mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(1), veryBigNumber), }, - want: "could not apply tx 0 [0x41dcd104694d9ed0cd2a7957707483939eae5f57d8de625f56e75b88a7709ac0]: fee cap higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, feeCap byte length: 38", + want: "could not apply tx 0 [0x41dcd104694d9ed0cd2a7957707483939eae5f57d8de625f56e75b88a7709ac0]: fee cap higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, feeCap bit length: 301", }, { // ErrTipAboveFeeCap txs: []*types.Transaction{ diff --git a/core/state_transition.go b/core/state_transition.go index 5c52ba090d70..b4d5030bc09b 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -216,12 +216,12 @@ func (st *StateTransition) preCheck() error { } // Make sure that transaction feeCap is greater than the baseFee (post london) if st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) { - if l := len(st.feeCap.Bytes()); l > 32 { - return fmt.Errorf("%w: address %v, feeCap byte length: %d", ErrFeeCapVeryHigh, + if l := st.feeCap.BitLen(); l > 256 { + return fmt.Errorf("%w: address %v, feeCap bit length: %d", ErrFeeCapVeryHigh, st.msg.From().Hex(), l) } - if l := len(st.tip.Bytes()); l > 32 { - return fmt.Errorf("%w: address %v, tip byte length: %d", ErrTipVeryHigh, + if l := st.tip.BitLen(); l > 256 { + return fmt.Errorf("%w: address %v, tip bit length: %d", ErrTipVeryHigh, st.msg.From().Hex(), l) } if st.feeCap.Cmp(st.tip) < 0 { diff --git a/core/tx_pool.go b/core/tx_pool.go index 32aa12856c70..0b0241aa67c4 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -556,10 +556,10 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error { return ErrGasLimit } // Sanity check for extremely large numbers - if len(tx.FeeCap().Bytes()) > 32 { + if tx.FeeCap().BitLen() > 256 { return ErrFeeCapVeryHigh } - if len(tx.Tip().Bytes()) > 32 { + if tx.Tip().BitLen() > 256 { return ErrTipVeryHigh } // Ensure feeCap is less than or equal to tip. From d368be1a371cdfba8c87874fb55eae45b48f86fd Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Sun, 30 May 2021 12:09:04 +0200 Subject: [PATCH 4/5] core: fixed panic --- core/state_transition.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/state_transition.go b/core/state_transition.go index b4d5030bc09b..0ca7c6884e1b 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -188,7 +188,12 @@ func (st *StateTransition) to() common.Address { func (st *StateTransition) buyGas() error { mgval := new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), st.gasPrice) - balanceReq := new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), st.feeCap) + var balanceReq *big.Int + if st.feeCap != nil { + balanceReq = new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), st.feeCap) + } else { + balanceReq = mgval + } if have, want := st.state.GetBalance(st.msg.From()), balanceReq; have.Cmp(want) < 0 { return fmt.Errorf("%w: address %v have %v want %v", ErrInsufficientFunds, st.msg.From().Hex(), have, want) } From 8b2b3fb9f187003ea2260f641301bec0776c550a Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Sun, 30 May 2021 13:46:08 +0200 Subject: [PATCH 5/5] core: more docs on tests, less allocs in buygas --- core/state_processor_test.go | 31 +++++++++++++++++++++---------- core/state_transition.go | 12 ++++++------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index cd8941c0537b..6c80fb60938b 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -88,9 +88,9 @@ func TestStateProcessorErrors(t *testing.T) { blockchain, _ = NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil) ) defer blockchain.Stop() - - veryBigNumber := big.NewInt(1) - veryBigNumber.Lsh(veryBigNumber, 300) + bigNumber := new(big.Int).SetBytes(common.FromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) + tooBigNumber := new(big.Int).Set(bigNumber) + tooBigNumber.Add(tooBigNumber, common.Big1) for i, tt := range []struct { txs []*types.Transaction want string @@ -150,15 +150,15 @@ func TestStateProcessorErrors(t *testing.T) { }, { // ErrTipVeryHigh txs: []*types.Transaction{ - mkDynamicTx(0, common.Address{}, params.TxGas, veryBigNumber, big.NewInt(1)), + mkDynamicTx(0, common.Address{}, params.TxGas, tooBigNumber, big.NewInt(1)), }, - want: "could not apply tx 0 [0x56a98c4e7714c63ebd41e56c7ab399e237a690b68139f2a9e3bfeab01ade8473]: tip higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tip bit length: 301", + want: "could not apply tx 0 [0x15b8391b9981f266b32f3ab7da564bbeb3d6c21628364ea9b32a21139f89f712]: tip higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tip bit length: 257", }, { // ErrFeeCapVeryHigh txs: []*types.Transaction{ - mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(1), veryBigNumber), + mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(1), tooBigNumber), }, - want: "could not apply tx 0 [0x41dcd104694d9ed0cd2a7957707483939eae5f57d8de625f56e75b88a7709ac0]: fee cap higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, feeCap bit length: 301", + want: "could not apply tx 0 [0x48bc299b83fdb345c57478f239e89814bb3063eb4e4b49f3b6057a69255c16bd]: fee cap higher than 2^256-1: address 0x71562b71999873DB5b286dF957af199Ec94617F7, feeCap bit length: 257", }, { // ErrTipAboveFeeCap txs: []*types.Transaction{ @@ -167,12 +167,23 @@ func TestStateProcessorErrors(t *testing.T) { want: "could not apply tx 0 [0xf987a31ff0c71895780a7612f965a0c8b056deb54e020bb44fa478092f14c9b4]: tip higher than fee cap: address 0x71562b71999873DB5b286dF957af199Ec94617F7, tip: 1, feeCap: 2", }, { // ErrInsufficientFunds + // Available balance: 1000000000000000000 + // Effective cost: 18375000021000 + // FeeCap * gas: 1050000000000000000 + // This test is designed to have the effective cost be covered by the balance, but + // the extended requirement on FeeCap*gas < balance to fail txs: []*types.Transaction{ - mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(1), big.NewInt(1000000000000000000)), + mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(1), big.NewInt(50000000000000)), }, - want: "could not apply tx 0 [0xbe93a7a024ea94e4156851ceab721dd300abd8509a6fa4216a58152982619973]: insufficient funds for gas * price + value: address 0x71562b71999873DB5b286dF957af199Ec94617F7 have 1000000000000000000 want 21000000000000000000000", + want: "could not apply tx 0 [0x413603cd096a87f41b1660d3ed3e27d62e1da78eac138961c0a1314ed43bd129]: insufficient funds for gas * price + value: address 0x71562b71999873DB5b286dF957af199Ec94617F7 have 1000000000000000000 want 1050000000000000000", }, - } { + { // Another ErrInsufficientFunds, this one to ensure that feecap/tip of max u256 is allowed + txs: []*types.Transaction{ + mkDynamicTx(0, common.Address{}, params.TxGas, bigNumber, bigNumber), + }, + want: "could not apply tx 0 [0xd82a0c2519acfeac9a948258c47e784acd20651d9d80f9a1c67b4137651c3a24]: insufficient funds for gas * price + value: address 0x71562b71999873DB5b286dF957af199Ec94617F7 have 1000000000000000000 want 2431633873983640103894990685182446064918669677978451844828609264166175722438635000", + }, + }[8:] { block := GenerateBadBlock(genesis, ethash.NewFaker(), tt.txs, gspec.Config) _, err := blockchain.InsertChain(types.Blocks{block}) if err == nil { diff --git a/core/state_transition.go b/core/state_transition.go index 0ca7c6884e1b..18777d8d4cc0 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -187,14 +187,14 @@ func (st *StateTransition) to() common.Address { } func (st *StateTransition) buyGas() error { - mgval := new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), st.gasPrice) - var balanceReq *big.Int + mgval := new(big.Int).SetUint64(st.msg.Gas()) + mgval = mgval.Mul(mgval, st.gasPrice) + balanceCheck := mgval if st.feeCap != nil { - balanceReq = new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), st.feeCap) - } else { - balanceReq = mgval + balanceCheck = new(big.Int).SetUint64(st.msg.Gas()) + balanceCheck = balanceCheck.Mul(balanceCheck, st.feeCap) } - if have, want := st.state.GetBalance(st.msg.From()), balanceReq; have.Cmp(want) < 0 { + if have, want := st.state.GetBalance(st.msg.From()), balanceCheck; have.Cmp(want) < 0 { return fmt.Errorf("%w: address %v have %v want %v", ErrInsufficientFunds, st.msg.From().Hex(), have, want) } if err := st.gp.SubGas(st.msg.Gas()); err != nil {