From db2c2393f3a25fbc073368e6d06d8334ec83116e Mon Sep 17 00:00:00 2001 From: Igor Crevar Date: Thu, 7 Sep 2023 12:16:49 +0200 Subject: [PATCH] tests --- jsonrpc/eth_blockchain_test.go | 82 ++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 19 deletions(-) diff --git a/jsonrpc/eth_blockchain_test.go b/jsonrpc/eth_blockchain_test.go index 0980026c20..08772dbac8 100644 --- a/jsonrpc/eth_blockchain_test.go +++ b/jsonrpc/eth_blockchain_test.go @@ -2,7 +2,6 @@ package jsonrpc import ( "errors" - "fmt" "math/big" "testing" @@ -280,17 +279,27 @@ func TestEth_Syncing(t *testing.T) { // if price-limit flag is set its value should be returned if it is higher than avg gas price func TestEth_GetPrice_PriceLimitSet(t *testing.T) { - priceLimit := uint64(100333) + const ( + baseFee = uint64(10000) + tipCap = uint64(1000) + priceLimit = uint64(10010) + ) + store := newMockBlockStore() store.blocks = []*types.Block{ { Header: &types.Header{Number: uint64(1)}, }, } + store.maxPriorityFeePerGasFn = func() (*big.Int, error) { + return new(big.Int).SetUint64(tipCap), nil + } + // not using newTestEthEndpoint as we need to set priceLimit eth := newTestEthEndpointWithPriceLimit(store, priceLimit) - t.Run("returns price limit flag value when it is larger than average gas price", func(t *testing.T) { + t.Run("returns price limit flag value when it is larger than MaxPriorityFee+BaseFee", func(t *testing.T) { + store.baseFee = 0 res, err := eth.GasPrice() store.averageGasPrice = 0 assert.NoError(t, err) @@ -299,31 +308,56 @@ func TestEth_GetPrice_PriceLimitSet(t *testing.T) { assert.Equal(t, argUint64(priceLimit), res) }) - // t.Run("returns average gas price when it is larger than set price limit flag", func(t *testing.T) { - // store.averageGasPrice = 500000 - // res, err := eth.GasPrice() - // assert.NoError(t, err) - // assert.NotNil(t, res) + t.Run("returns MaxPriorityFee+BaseFee when it is larger than set price limit flag", func(t *testing.T) { + store.baseFee = baseFee + res, err := eth.GasPrice() + assert.NoError(t, err) + assert.NotNil(t, res) + + assert.Equal(t, argUint64(baseFee+tipCap), res) + }) - // assert.GreaterOrEqual(t, res, argUint64(priceLimit)) - // }) + t.Run("returns error if MaxPriorityFeePerGas returns error", func(t *testing.T) { + store.maxPriorityFeePerGasFn = func() (*big.Int, error) { + return nil, runtime.ErrDepth + } + _, err := eth.GasPrice() + assert.ErrorIs(t, err, runtime.ErrDepth) + }) } -func TestEth_GasPrice(t *testing.T) { +func TestEth_GasPrice_WithoutLondonFork(t *testing.T) { + const priceLimit = 100000 + store := newMockBlockStore() - store.averageGasPrice = 9999 - eth := newTestEthEndpoint(store) + store.forksInTime.London = false store.blocks = []*types.Block{ { Header: &types.Header{Number: uint64(1)}, }, } - res, err := eth.GasPrice() - assert.NoError(t, err) - assert.NotNil(t, res) + eth := newTestEthEndpointWithPriceLimit(store, priceLimit) + + t.Run("priceLimit is greater than averageGasPrice", func(t *testing.T) { + store.averageGasPrice = priceLimit - 100 + + res, err := eth.GasPrice() + + assert.NoError(t, err) + assert.NotNil(t, res) + assert.Equal(t, argUint64(priceLimit), res) + }) + + t.Run("averageGasPrice is greater than priceLimit", func(t *testing.T) { + store.averageGasPrice = priceLimit + 100 + + res, err := eth.GasPrice() - assert.Equal(t, argUint64(store.averageGasPrice), res) + assert.NoError(t, err) + assert.NotNil(t, res) + assert.Equal(t, argUint64(priceLimit+100), res) + }) } func TestEth_Call(t *testing.T) { @@ -419,6 +453,9 @@ type mockBlockStore struct { ethCallError error returnValue []byte forksInTime chain.ForksInTime + baseFee uint64 + + maxPriorityFeePerGasFn func() (*big.Int, error) } func newMockBlockStore() *mockBlockStore { @@ -613,14 +650,21 @@ func (m *mockBlockStore) GetAccount(root types.Hash, addr types.Address) (*Accou } func (m *mockBlockStore) GetBaseFee() uint64 { - return 0 + return m.baseFee } func (m *mockBlockStore) GetForksInTime(block uint64) chain.ForksInTime { - fmt.Println(m.forksInTime) return m.forksInTime } +func (m *mockBlockStore) MaxPriorityFeePerGas() (*big.Int, error) { + if m.maxPriorityFeePerGasFn != nil { + return m.maxPriorityFeePerGasFn() + } + + return big.NewInt(0), nil +} + func newTestBlock(number uint64, hash types.Hash) *types.Block { return &types.Block{ Header: &types.Header{