From cb9f8a02211d82d68d7e87d9b9da97936fa63e7c Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Fri, 6 Sep 2024 13:08:41 +0300 Subject: [PATCH] Fix crash on eth_estimateGas due to nil Value --- api/models.go | 2 +- api/models_test.go | 12 ++++++++++++ models/transaction_test.go | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/api/models.go b/api/models.go index 140044bb6..1aaba88a4 100644 --- a/api/models.go +++ b/api/models.go @@ -63,7 +63,7 @@ func (txArgs TransactionArgs) Validate() error { // e.g. https://github.com/onflow/go-ethereum/issues/16106. if txDataLen == 0 { // Prevent sending ether into black hole (show stopper) - if txArgs.Value.ToInt().Cmp(big.NewInt(0)) > 0 { + if txArgs.Value != nil && txArgs.Value.ToInt().Cmp(big.NewInt(0)) > 0 { return errs.NewInvalidTransactionError( errors.New("transaction will create a contract with value but empty code"), ) diff --git a/api/models_test.go b/api/models_test.go index 0d050c8e1..2c4b024fc 100644 --- a/api/models_test.go +++ b/api/models_test.go @@ -80,6 +80,18 @@ func TestValidateTransaction(t *testing.T) { valid: false, errMsg: "transaction will create a contract with empty code", }, + "create empty contract (nil value)": { + txArgs: TransactionArgs{ + Nonce: (*hexutil.Uint64)(&nonce), + To: nil, + Value: nil, + Gas: (*hexutil.Uint64)(&gasLimit), + GasPrice: (*hexutil.Big)(big.NewInt(0)), + Data: &hexutil.Bytes{}, + }, + valid: false, + errMsg: "transaction will create a contract with empty code", + }, "create empty contract (with value)": { txArgs: TransactionArgs{ Nonce: (*hexutil.Uint64)(&nonce), diff --git a/models/transaction_test.go b/models/transaction_test.go index 71d0c1034..3e11e1261 100644 --- a/models/transaction_test.go +++ b/models/transaction_test.go @@ -331,6 +331,20 @@ func TestValidateTransaction(t *testing.T) { valid: false, errMsg: "transaction will create a contract with empty code", }, + "create empty contract (nil value)": { + tx: gethTypes.NewTx( + &gethTypes.LegacyTx{ + Nonce: 1, + To: nil, + Value: nil, + Gas: 53_000, + GasPrice: big.NewInt(0), + Data: []byte{}, + }, + ), + valid: false, + errMsg: "transaction will create a contract with empty code", + }, "create empty contract (with value)": { tx: gethTypes.NewTx( &gethTypes.LegacyTx{