-
Notifications
You must be signed in to change notification settings - Fork 225
/
gasprice_update.go
81 lines (69 loc) · 2.43 KB
/
gasprice_update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package evm
import (
"math/big"
"sync"
"time"
"github.com/ava-labs/subnet-evm/params"
)
type gasPriceUpdater struct {
setter gasPriceSetter
chainConfig *params.ChainConfig
shutdownChan <-chan struct{}
wg *sync.WaitGroup
}
type gasPriceSetter interface {
SetGasPrice(price *big.Int)
SetMinFee(price *big.Int)
}
// handleGasPriceUpdates creates and runs an instance of
func (vm *VM) handleGasPriceUpdates() {
gpu := &gasPriceUpdater{
setter: vm.txPool,
chainConfig: vm.chainConfig,
shutdownChan: vm.shutdownChan,
wg: &vm.shutdownWg,
}
gpu.start()
}
// start handles the appropriate gas price and minimum fee updates required by [gpu.chainConfig]
func (gpu *gasPriceUpdater) start() {
// Updates to the minimum gas price as of Subnet EVM if it's already in effect or starts a goroutine to enable it at the correct time
if disabled := gpu.handleUpdate(gpu.setter.SetGasPrice, gpu.chainConfig.SubnetEVMTimestamp, big.NewInt(0)); disabled {
return
}
minBaseFee := gpu.chainConfig.FeeConfig.MinBaseFee
// Updates to the minimum gas price as of Subnet EVM if it's already in effect or starts a goroutine to enable it at the correct time
gpu.handleUpdate(gpu.setter.SetMinFee, gpu.chainConfig.SubnetEVMTimestamp, minBaseFee)
}
// handleUpdate handles calling update(price) at the appropriate time based on
// the value of [timestamp].
// 1) If [timestamp] is nil, update is never called
// 2) If [timestamp] has already passed, update is called immediately
// 3) [timestamp] is some time in the future, starts a goroutine that will call update(price) at the time
// given by [timestamp].
func (gpu *gasPriceUpdater) handleUpdate(update func(price *big.Int), timestamp *big.Int, price *big.Int) bool {
if timestamp == nil {
return true
}
currentTime := time.Now()
upgradeTime := time.Unix(timestamp.Int64(), 0)
if currentTime.After(upgradeTime) {
update(price)
} else {
gpu.wg.Add(1)
go gpu.updatePrice(update, time.Until(upgradeTime), price)
}
return false
}
// updatePrice calls update(updatedPrice) after waiting for [duration] or shuts down early
// if the [shutdownChan] is closed.
func (gpu *gasPriceUpdater) updatePrice(update func(price *big.Int), duration time.Duration, updatedPrice *big.Int) {
defer gpu.wg.Done()
select {
case <-time.After(duration):
update(updatedPrice)
case <-gpu.shutdownChan:
}
}