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

eth: discard gas prices below minGasPrice #1860

Merged
merged 1 commit into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@

#### Orchestrator

- \#1860 Discard low gas prices to prevent insufficient ticket faceValue errors (@kyriediculous)

#### Transcoder
7 changes: 6 additions & 1 deletion cmd/livepeer/livepeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/livepeer/go-livepeer/build"
"github.com/livepeer/go-livepeer/pm"
Expand Down Expand Up @@ -192,6 +193,7 @@ func main() {

type NetworkConfig struct {
ethController string
minGasPrice *big.Int
}

ctx := context.Background()
Expand All @@ -202,6 +204,7 @@ func main() {
},
"mainnet": {
ethController: "0xf96d54e490317c557a967abfa5d6e33006be69b3",
minGasPrice: big.NewInt(int64(params.GWei)),
},
}

Expand All @@ -224,9 +227,11 @@ func main() {
}

// Setting config options based on specified network
var minGasPrice = big.NewInt(0)
if netw, ok := configOptions[*network]; ok {
if *ethController == "" {
*ethController = netw.ethController
minGasPrice = netw.minGasPrice
}
glog.Infof("***Livepeer is running on the %v network: %v***", *network, *ethController)
} else {
Expand Down Expand Up @@ -542,7 +547,7 @@ func main() {

sigVerifier := &pm.DefaultSigVerifier{}
validator := pm.NewValidator(sigVerifier, timeWatcher)
gpm := eth.NewGasPriceMonitor(backend, blockPollingTime)
gpm := eth.NewGasPriceMonitor(backend, blockPollingTime, minGasPrice)
// Start gas price monitor
_, err := gpm.Start(ctx)
if err != nil {
Expand Down
17 changes: 13 additions & 4 deletions eth/gaspricemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@ type GasPriceMonitor struct {
gasPriceMu sync.RWMutex
// gasPrice is the current gas price to be returned to users
gasPrice *big.Int
// minGasPrice is the minimum gas price below which polled values will be discarded
minGasPrice *big.Int

// update is a channel used to send notifications to a listener
// when the gas price is updated
update chan struct{}
}

// NewGasPriceMonitor returns a GasPriceMonitor
func NewGasPriceMonitor(gpo GasPriceOracle, pollingInterval time.Duration) *GasPriceMonitor {
func NewGasPriceMonitor(gpo GasPriceOracle, pollingInterval time.Duration, minGasPrice *big.Int) *GasPriceMonitor {
minGasP := big.NewInt(0)
yondonfu marked this conversation as resolved.
Show resolved Hide resolved
if minGasPrice != nil {
minGasP = minGasPrice
}
return &GasPriceMonitor{
gpo: gpo,
pollingInterval: pollingInterval,
gasPrice: big.NewInt(0),
minGasPrice: minGasP,
}
}

Expand Down Expand Up @@ -124,10 +131,12 @@ func (gpm *GasPriceMonitor) fetchAndUpdateGasPrice(ctx context.Context) error {
return err
}

gpm.updateGasPrice(gasPrice)
if gasPrice.Cmp(gpm.minGasPrice) >= 0 {
gpm.updateGasPrice(gasPrice)

if monitor.Enabled {
monitor.SuggestedGasPrice(gasPrice)
if monitor.Enabled {
monitor.SuggestedGasPrice(gasPrice)
}
}

return nil
Expand Down
18 changes: 14 additions & 4 deletions eth/gaspricemonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestStart(t *testing.T) {
gasPrice := big.NewInt(777)
gpo := newStubGasPriceOracle(gasPrice)

gpm := NewGasPriceMonitor(gpo, 1*time.Hour)
gpm := NewGasPriceMonitor(gpo, 1*time.Hour, big.NewInt(0))

assert := assert.New(t)

Expand Down Expand Up @@ -93,10 +93,11 @@ func TestStart_Polling(t *testing.T) {
gasPrice1 := big.NewInt(777)
gasPrice2 := big.NewInt(555)
gasPrice3 := big.NewInt(888)
gasPrice4 := big.NewInt(1)
gpo := newStubGasPriceOracle(gasPrice1)

pollingInterval := 1 * time.Millisecond
gpm := NewGasPriceMonitor(gpo, pollingInterval)
gpm := NewGasPriceMonitor(gpo, pollingInterval, big.NewInt(10))

assert := assert.New(t)

Expand Down Expand Up @@ -131,14 +132,23 @@ func TestStart_Polling(t *testing.T) {
// There should be more queries now
assert.Greater(gpo.Queries(), queries)
assert.Equal(gasPrice3, gpm.GasPrice())

queries = gpo.Queries()

go func() {
gpo.SetGasPrice(gasPrice4)
}()
time.Sleep(100 * time.Millisecond)
assert.Greater(gpo.Queries(), queries)
yondonfu marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(gasPrice3, gpm.GasPrice())
}

func TestStart_Polling_ContextCancel(t *testing.T) {
gasPrice1 := big.NewInt(777)
gpo := newStubGasPriceOracle(gasPrice1)

pollingInterval := 1 * time.Second
gpm := NewGasPriceMonitor(gpo, pollingInterval)
gpm := NewGasPriceMonitor(gpo, pollingInterval, big.NewInt(0))

ctx, cancel := context.WithCancel(context.Background())
update, err := gpm.Start(ctx)
Expand All @@ -161,7 +171,7 @@ func TestStop(t *testing.T) {
gpo := newStubGasPriceOracle(gasPrice)
gpo.SetGasPrice(gasPrice)

gpm := NewGasPriceMonitor(gpo, 1*time.Hour)
gpm := NewGasPriceMonitor(gpo, 1*time.Hour, big.NewInt(0))

assert := assert.New(t)

Expand Down