Skip to content

Commit

Permalink
eth,server: move max gas price logic to gas price monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
kyriediculous committed Jun 18, 2021
1 parent b12a24d commit d4886f6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
21 changes: 6 additions & 15 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ type Backend interface {
ethereum.LogFilterer
ethereum.ChainReader
ChainID(ctx context.Context) (*big.Int, error)
MaxGasPrice() *big.Int
SetMaxGasPrice(gp *big.Int)
GasPriceMonitor() *GasPriceMonitor
}

type backend struct {
Expand All @@ -55,7 +54,6 @@ type backend struct {
gpm *GasPriceMonitor

sync.RWMutex
maxGasPrice *big.Int
}

func NewBackend(client *ethclient.Client, signer types.Signer, gpm *GasPriceMonitor) Backend {
Expand Down Expand Up @@ -95,27 +93,20 @@ func (b *backend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {

// Use the gas price monitor instead of the ethereum client to suggest a gas price
gp := b.gpm.GasPrice()
maxGp := b.gpm.MaxGasPrice()

if b.maxGasPrice != nil && gp.Cmp(b.maxGasPrice) > 0 {
if maxGp != nil && gp.Cmp(maxGp) > 0 {
return nil, fmt.Errorf("current gas price exceeds maximum gas price max=%v GWei current=%v GWei",
FromWei(b.maxGasPrice, params.GWei),
FromWei(maxGp, params.GWei),
FromWei(gp, params.GWei),
)
}

return gp, nil
}

func (b *backend) SetMaxGasPrice(gp *big.Int) {
b.Lock()
defer b.Unlock()
b.maxGasPrice = gp
}

func (b *backend) MaxGasPrice() *big.Int {
b.RLock()
defer b.RUnlock()
return b.maxGasPrice
func (b *backend) GasPriceMonitor() *GasPriceMonitor {
return b.gpm
}

type txLog struct {
Expand Down
7 changes: 0 additions & 7 deletions eth/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,3 @@ func TestSendTransaction_SendErr_DontUpdateNonce(t *testing.T) {

assert.Equal(t, nonceLockBefore.nonce, nonceLockAfter.nonce)
}

func TestBackend_SetMaxGasPrice(t *testing.T) {
gp := big.NewInt(10)
backend := &backend{}
backend.SetMaxGasPrice(gp)
assert.Equal(t, gp, backend.MaxGasPrice())
}
14 changes: 14 additions & 0 deletions eth/gaspricemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type GasPriceMonitor struct {
gasPrice *big.Int
// minGasPrice is the minimum gas price below which polled values will be discarded
minGasPrice *big.Int
// maxGasPrice is the max acceptable gas price defined by the user
maxGasPrice *big.Int

// update is a channel used to send notifications to a listener
// when the gas price is updated
Expand Down Expand Up @@ -125,6 +127,18 @@ func (gpm *GasPriceMonitor) Stop() error {
return nil
}

func (gpm *GasPriceMonitor) SetMaxGasPrice(gp *big.Int) {
gpm.gasPriceMu.Lock()
defer gpm.gasPriceMu.Unlock()
gpm.maxGasPrice = gp
}

func (gpm *GasPriceMonitor) MaxGasPrice() *big.Int {
gpm.gasPriceMu.RLock()
defer gpm.gasPriceMu.RUnlock()
return gpm.maxGasPrice
}

func (gpm *GasPriceMonitor) fetchAndUpdateGasPrice(ctx context.Context) error {
gasPrice, err := gpm.gpo.SuggestGasPrice(ctx)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions eth/gaspricemonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,10 @@ func TestStop(t *testing.T) {
_, ok := (<-gpm.update)
assert.False(ok)
}

func TestSetMaxGasPrice(t *testing.T) {
gp := big.NewInt(10)
gpm := &GasPriceMonitor{}
gpm.SetMaxGasPrice(gp)
assert.Equal(t, gp, gpm.MaxGasPrice())
}
4 changes: 2 additions & 2 deletions server/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ func (s *LivepeerServer) cliWebServerHandlers(bindAddr string) *http.ServeMux {
respondWith400(w, err.Error())
return
}
gprice := b.MaxGasPrice()
gprice := b.GasPriceMonitor().MaxGasPrice()
if gprice == nil {
w.Write([]byte(""))
} else {
Expand Down Expand Up @@ -1175,7 +1175,7 @@ func (s *LivepeerServer) cliWebServerHandlers(bindAddr string) *http.ServeMux {
respondWith400(w, err.Error())
return
}
b.SetMaxGasPrice(gprice)
b.GasPriceMonitor().SetMaxGasPrice(gprice)
})

mux.Handle("/currentBlock", currentBlockHandler(s.LivepeerNode.Database))
Expand Down

0 comments on commit d4886f6

Please sign in to comment.