Skip to content

Commit

Permalink
avg gas price respect the gas limit config (dogechain-lab#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarianShawn committed Sep 19, 2022
1 parent e56d55f commit cf53f77
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 14 deletions.
11 changes: 10 additions & 1 deletion jsonrpc/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Dispatcher struct {
endpoints endpoints
chainID uint64
jsonRPCBatchLengthLimit uint64
priceLimit uint64
}

func newDispatcher(
Expand All @@ -53,11 +54,13 @@ func newDispatcher(
chainID uint64,
jsonRPCBatchLengthLimit uint64,
blockRangeLimit uint64,
priceLimit uint64,
) *Dispatcher {
d := &Dispatcher{
logger: logger.Named("dispatcher"),
chainID: chainID,
jsonRPCBatchLengthLimit: jsonRPCBatchLengthLimit,
priceLimit: priceLimit,
}

if store != nil {
Expand All @@ -71,7 +74,13 @@ func newDispatcher(
}

func (d *Dispatcher) registerEndpoints(store JSONRPCStore) {
d.endpoints.Eth = &Eth{d.logger, store, d.chainID, d.filterManager}
d.endpoints.Eth = &Eth{
logger: d.logger,
store: store,
chainID: d.chainID,
filterManager: d.filterManager,
priceLimit: d.priceLimit,
}
d.endpoints.Net = &Net{store, d.chainID}
d.endpoints.Web3 = &Web3{}
d.endpoints.TxPool = &TxPool{store}
Expand Down
14 changes: 7 additions & 7 deletions jsonrpc/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func expectBatchJSONResult(data []byte, v interface{}) error {
func TestDispatcher_HandleWebsocketConnection_EthSubscribe(t *testing.T) {
t.Run("clients should be able to receive \"newHeads\" event thru eth_subscribe", func(t *testing.T) {
store := newMockStore()
dispatcher := newDispatcher(hclog.NewNullLogger(), store, 0, 0, 0)
dispatcher := newDispatcher(hclog.NewNullLogger(), store, 0, 0, 0, 0)

mockConnection := &mockWsConn{
msgCh: make(chan []byte, 1),
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestDispatcher_HandleWebsocketConnection_EthSubscribe(t *testing.T) {

func TestDispatcher_WebsocketConnection_RequestFormats(t *testing.T) {
store := newMockStore()
dispatcher := newDispatcher(hclog.NewNullLogger(), store, 0, 0, 0)
dispatcher := newDispatcher(hclog.NewNullLogger(), store, 0, 0, 0, 0)

mockConnection := &mockWsConn{
msgCh: make(chan []byte, 1),
Expand Down Expand Up @@ -196,7 +196,7 @@ func (m *mockService) Filter(f LogQuery) (interface{}, error) {
func TestDispatcherFuncDecode(t *testing.T) {
srv := &mockService{msgCh: make(chan interface{}, 10)}

dispatcher := newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 0, 0)
dispatcher := newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 0, 0, 0)
dispatcher.registerService("mock", srv)

handleReq := func(typ string, msg string) interface{} {
Expand Down Expand Up @@ -278,7 +278,7 @@ func TestDispatcherBatchRequest(t *testing.T) {
{
"leading-whitespace",
"test with leading whitespace (\" \\t\\n\\n\\r\\)",
newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 0, 0),
newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 0, 0, 0),
append([]byte{0x20, 0x20, 0x09, 0x0A, 0x0A, 0x0D}, []byte(`[
{"id":1,"jsonrpc":"2.0","method":"eth_getBalance","params":["0x1", true]},
{"id":2,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x2", true]},
Expand All @@ -294,7 +294,7 @@ func TestDispatcherBatchRequest(t *testing.T) {
{
"valid-batch-req",
"test with batch req length within batchRequestLengthLimit",
newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 0, 0),
newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 0, 0, 0),
[]byte(`[
{"id":1,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]},
{"id":2,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]},
Expand All @@ -314,7 +314,7 @@ func TestDispatcherBatchRequest(t *testing.T) {
{
"invalid-batch-req",
"test with batch req length exceeding batchRequestLengthLimit",
newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 3, 1000),
newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 3, 1000, 0),
[]byte(`[
{"id":1,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]},
{"id":2,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]},
Expand All @@ -328,7 +328,7 @@ func TestDispatcherBatchRequest(t *testing.T) {
{
"no-limits",
"test when limits are not set",
newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 0, 0),
newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 0, 0, 0),
[]byte(`[
{"id":1,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]},
{"id":2,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true]},
Expand Down
8 changes: 7 additions & 1 deletion jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Eth struct {
store ethStore
chainID uint64
filterManager *FilterManager
priceLimit uint64
}

var (
Expand Down Expand Up @@ -435,8 +436,13 @@ func (e *Eth) GetStorageAt(
func (e *Eth) GasPrice() (interface{}, error) {
// var avgGasPrice string
// Grab the average gas price and convert it to a hex value
priceLimit := new(big.Int).SetUint64(e.priceLimit)
minGasPrice, _ := new(big.Int).SetString(defaultMinGasPrice, 0)

if priceLimit.Cmp(minGasPrice) == -1 {
priceLimit = minGasPrice
}

// if e.store.GetAvgGasPrice().Cmp(minGasPrice) == -1 {
// avgGasPrice = hex.EncodeBig(minGasPrice)
// } else {
Expand All @@ -445,7 +451,7 @@ func (e *Eth) GasPrice() (interface{}, error) {

// return avgGasPrice, nil

return hex.EncodeBig(minGasPrice), nil
return hex.EncodeBig(priceLimit), nil
}

// Call executes a smart contract call using the transaction object data
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/eth_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,5 @@ func TestEth_GetNextNonce(t *testing.T) {
}

func newTestEthEndpoint(store ethStore) *Eth {
return &Eth{hclog.NewNullLogger(), store, 100, nil}
return &Eth{hclog.NewNullLogger(), store, 100, nil, 0}
}
11 changes: 9 additions & 2 deletions jsonrpc/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,22 @@ type Config struct {
BatchLengthLimit uint64
BlockRangeLimit uint64
EnableWS bool
PriceLimit uint64
}

// NewJSONRPC returns the JSONRPC http server
func NewJSONRPC(logger hclog.Logger, config *Config) (*JSONRPC, error) {
srv := &JSONRPC{
logger: logger.Named("jsonrpc"),
config: config,
dispatcher: newDispatcher(logger, config.Store, config.ChainID,
config.BatchLengthLimit, config.BlockRangeLimit),
dispatcher: newDispatcher(
logger,
config.Store,
config.ChainID,
config.BatchLengthLimit,
config.BlockRangeLimit,
config.PriceLimit,
),
}

// start http server
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/web3_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestWeb3EndpointSha3(t *testing.T) {
dispatcher := newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 20, 1000)
dispatcher := newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 20, 1000, 0)

resp, err := dispatcher.Handle([]byte(`{
"method": "web3_sha3",
Expand All @@ -25,7 +25,7 @@ func TestWeb3EndpointSha3(t *testing.T) {
}

func TestWeb3EndpointClientVersion(t *testing.T) {
dispatcher := newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 20, 1000)
dispatcher := newDispatcher(hclog.NewNullLogger(), newMockStore(), 0, 20, 1000, 0)

resp, err := dispatcher.Handle([]byte(`{
"method": "web3_clientVersion",
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ func (s *Server) setupJSONRPC() error {
BatchLengthLimit: s.config.JSONRPC.BatchLengthLimit,
BlockRangeLimit: s.config.JSONRPC.BlockRangeLimit,
EnableWS: s.config.JSONRPC.EnableWS,
PriceLimit: s.config.PriceLimit,
}

srv, err := jsonrpc.NewJSONRPC(s.logger, conf)
Expand Down

0 comments on commit cf53f77

Please sign in to comment.