Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix eth_gasPrice RPC method #1883

Merged
merged 6 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions jsonrpc/eth_blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/0xPolygon/polygon-edge/blockchain"
"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/helper/progress"
"github.com/0xPolygon/polygon-edge/state/runtime"
Expand Down Expand Up @@ -280,6 +281,11 @@ func TestEth_Syncing(t *testing.T) {
func TestEth_GetPrice_PriceLimitSet(t *testing.T) {
priceLimit := uint64(100333)
store := newMockBlockStore()
store.blocks = []*types.Block{
{
Header: &types.Header{Number: uint64(1)},
},
}
// not using newTestEthEndpoint as we need to set priceLimit
eth := newTestEthEndpointWithPriceLimit(store, priceLimit)

Expand All @@ -306,6 +312,11 @@ func TestEth_GasPrice(t *testing.T) {
store := newMockBlockStore()
store.averageGasPrice = 9999
eth := newTestEthEndpoint(store)
store.blocks = []*types.Block{
{
Header: &types.Header{Number: uint64(1)},
},
}

res, err := eth.GasPrice()
assert.NoError(t, err)
Expand Down Expand Up @@ -598,6 +609,14 @@ func (m *mockBlockStore) GetAccount(root types.Hash, addr types.Address) (*Accou
return &Account{Nonce: 0}, nil
}

func (m *mockBlockStore) GetBaseFee() uint64 {
return 0
}

func (m *mockBlockStore) GetForksInTime(block uint64) chain.ForksInTime {
return chain.ForksInTime{London: false}
}

func newTestBlock(number uint64, hash types.Hash) *types.Block {
return &types.Block{
Header: &types.Header{
Expand Down
14 changes: 13 additions & 1 deletion jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type ethTxPoolStore interface {

// GetNonce returns the next nonce for this address
GetNonce(addr types.Address) uint64

// returns the current base fee of TxPool
GetBaseFee() uint64
}

type Account struct {
Expand Down Expand Up @@ -393,10 +396,19 @@ func (e *Eth) GetStorageAt(
// GasPrice returns the average gas price based on the last x blocks
// taking into consideration operator defined price limit
func (e *Eth) GasPrice() (interface{}, error) {
// Return --price-limit flag defined value if it is greater than avgGasPrice/baseFee+priorityFee
if e.store.GetForksInTime(e.store.Header().Number).London {
priorityFee, err := e.store.MaxPriorityFeePerGas()
if err != nil {
return nil, err
}

return argUint64(common.Max(e.priceLimit, priorityFee.Uint64()+e.store.GetBaseFee())), nil
}

// Fetch average gas price in uint64
avgGasPrice := e.store.GetAvgGasPrice().Uint64()

// Return --price-limit flag defined value if it is greater than avgGasPrice
return argUint64(common.Max(e.priceLimit, avgGasPrice)), nil
}

Expand Down
Loading