Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar committed Sep 7, 2023
1 parent 16a7fa0 commit 10886b8
Showing 1 changed file with 69 additions and 23 deletions.
92 changes: 69 additions & 23 deletions jsonrpc/eth_blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jsonrpc

import (
"errors"
"fmt"
"math/big"
"testing"

Expand Down Expand Up @@ -278,52 +277,89 @@ 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)
func TestEth_GasPrice_WithLondonFork(t *testing.T) {
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)
assert.NotNil(t, res)

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)
})

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.GreaterOrEqual(t, res, argUint64(priceLimit))
// })
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

assert.Equal(t, argUint64(store.averageGasPrice), res)
res, err := eth.GasPrice()

assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, argUint64(priceLimit+100), res)
})
}

func TestEth_Call(t *testing.T) {
Expand Down Expand Up @@ -419,6 +455,9 @@ type mockBlockStore struct {
ethCallError error
returnValue []byte
forksInTime chain.ForksInTime
baseFee uint64

maxPriorityFeePerGasFn func() (*big.Int, error)
}

func newMockBlockStore() *mockBlockStore {
Expand Down Expand Up @@ -613,14 +652,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{
Expand Down

0 comments on commit 10886b8

Please sign in to comment.