Skip to content

Commit

Permalink
use config.GasStation instead of config.API #3718 (#3721)
Browse files Browse the repository at this point in the history
Co-authored-by: dustinxie <dahuaxie@gmail.com>
  • Loading branch information
envestcc and dustinxie authored Dec 20, 2022
1 parent 3ed15c6 commit 83548f2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func newCoreService(
cfg: cfg,
registry: registry,
chainListener: NewChainListener(500),
gs: gasstation.NewGasStation(chain, dao, cfg),
gs: gasstation.NewGasStation(chain, dao, cfg.GasStation),
readCache: NewReadCache(),
}

Expand Down
18 changes: 9 additions & 9 deletions gasstation/gasstattion.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ type SimulateFunc func(context.Context, address.Address, *action.Execution, evm.
type GasStation struct {
bc blockchain.Blockchain
dao BlockDAO
cfg config.API
cfg config.GasStation
}

// NewGasStation creates a new gas station
func NewGasStation(bc blockchain.Blockchain, dao BlockDAO, cfg config.API) *GasStation {
func NewGasStation(bc blockchain.Blockchain, dao BlockDAO, cfg config.GasStation) *GasStation {
return &GasStation{
bc: bc,
dao: dao,
Expand All @@ -51,14 +51,14 @@ func (gs *GasStation) SuggestGasPrice() (uint64, error) {
tip := gs.bc.TipHeight()

endBlockHeight := uint64(0)
if tip > uint64(gs.cfg.GasStation.SuggestBlockWindow) {
endBlockHeight = tip - uint64(gs.cfg.GasStation.SuggestBlockWindow)
if tip > uint64(gs.cfg.SuggestBlockWindow) {
endBlockHeight = tip - uint64(gs.cfg.SuggestBlockWindow)
}

for height := tip; height > endBlockHeight; height-- {
blk, err := gs.dao.GetBlockByHeight(height)
if err != nil {
return gs.cfg.GasStation.DefaultGas, err
return gs.cfg.DefaultGas, err
}
if len(blk.Actions) == 0 {
continue
Expand All @@ -80,12 +80,12 @@ func (gs *GasStation) SuggestGasPrice() (uint64, error) {

if len(smallestPrices) == 0 {
// return default price
return gs.cfg.GasStation.DefaultGas, nil
return gs.cfg.DefaultGas, nil
}
sort.Sort(bigIntArray(smallestPrices))
gasPrice := smallestPrices[(len(smallestPrices)-1)*gs.cfg.GasStation.Percentile/100].Uint64()
if gasPrice < gs.cfg.GasStation.DefaultGas {
gasPrice = gs.cfg.GasStation.DefaultGas
gasPrice := smallestPrices[(len(smallestPrices)-1)*gs.cfg.Percentile/100].Uint64()
if gasPrice < gs.cfg.DefaultGas {
gasPrice = gs.cfg.DefaultGas
}
return gasPrice, nil
}
Expand Down
8 changes: 4 additions & 4 deletions gasstation/gasstattion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

func TestNewGasStation(t *testing.T) {
require := require.New(t)
require.NotNil(NewGasStation(nil, nil, config.Default.API))
require.NotNil(NewGasStation(nil, nil, config.Default.API.GasStation))
}
func TestSuggestGasPriceForUserAction(t *testing.T) {
ctx := context.Background()
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestSuggestGasPriceForUserAction(t *testing.T) {
height := bc.TipHeight()
fmt.Printf("Open blockchain pass, height = %d\n", height)

gs := NewGasStation(bc, blkMemDao, cfg.API)
gs := NewGasStation(bc, blkMemDao, cfg.API.GasStation)
require.NotNil(t, gs)

gp, err := gs.SuggestGasPrice()
Expand Down Expand Up @@ -164,12 +164,12 @@ func TestSuggestGasPriceForSystemAction(t *testing.T) {
height := bc.TipHeight()
fmt.Printf("Open blockchain pass, height = %d\n", height)

gs := NewGasStation(bc, blkMemDao, cfg.API)
gs := NewGasStation(bc, blkMemDao, cfg.API.GasStation)
require.NotNil(t, gs)

gp, err := gs.SuggestGasPrice()
fmt.Println(gp)
require.NoError(t, err)
// i from 10 to 29,gasprice for 20 to 39,60%*20+20=31
require.Equal(t, gs.cfg.GasStation.DefaultGas, gp)
require.Equal(t, gs.cfg.DefaultGas, gp)
}
2 changes: 1 addition & 1 deletion ioctl/cmd/action/stake2add.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2022 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is didslaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.

Expand Down

0 comments on commit 83548f2

Please sign in to comment.