@@ -23,49 +23,66 @@ import (
2323
2424 "github.com/ethereum/go-ethereum/core"
2525 "github.com/ethereum/go-ethereum/core/types"
26- "github.com/ethereum/go-ethereum/event"
2726 "github.com/ethereum/go-ethereum/logger"
2827 "github.com/ethereum/go-ethereum/logger/glog"
2928)
3029
31- const gpoProcessPastBlocks = 100
30+ const (
31+ gpoProcessPastBlocks = 100
32+
33+ // for testing
34+ gpoDefaultBaseCorrectionFactor = 110
35+ gpoDefaultMinGasPrice = 10000000000000
36+ )
3237
3338type blockPriceInfo struct {
3439 baseGasPrice * big.Int
3540}
3641
42+ // GasPriceOracle recommends gas prices based on the content of recent
43+ // blocks.
3744type GasPriceOracle struct {
38- eth * Ethereum
39- chain * core.BlockChain
40- events event.Subscription
45+ eth * Ethereum
46+ initOnce sync.Once
47+ minPrice * big.Int
48+ lastBaseMutex sync.Mutex
49+ lastBase * big.Int
50+
51+ // state of listenLoop
4152 blocks map [uint64 ]* blockPriceInfo
4253 firstProcessed , lastProcessed uint64
43- lastBaseMutex sync.Mutex
44- lastBase , minBase * big.Int
54+ minBase * big.Int
55+ }
56+
57+ // NewGasPriceOracle returns a new oracle.
58+ func NewGasPriceOracle (eth * Ethereum ) * GasPriceOracle {
59+ minprice := eth .GpoMinGasPrice
60+ if minprice == nil {
61+ minprice = big .NewInt (gpoDefaultMinGasPrice )
62+ }
63+ minbase := new (big.Int ).Mul (minprice , big .NewInt (100 ))
64+ if eth .GpobaseCorrectionFactor > 0 {
65+ minbase = minbase .Div (minbase , big .NewInt (int64 (eth .GpobaseCorrectionFactor )))
66+ }
67+ return & GasPriceOracle {
68+ eth : eth ,
69+ blocks : make (map [uint64 ]* blockPriceInfo ),
70+ minBase : minbase ,
71+ minPrice : minprice ,
72+ lastBase : minprice ,
73+ }
4574}
4675
47- func NewGasPriceOracle (eth * Ethereum ) (self * GasPriceOracle ) {
48- self = & GasPriceOracle {}
49- self .blocks = make (map [uint64 ]* blockPriceInfo )
50- self .eth = eth
51- self .chain = eth .blockchain
52- self .events = eth .EventMux ().Subscribe (
53- core.ChainEvent {},
54- core.ChainSplitEvent {},
55- )
56-
57- minbase := new (big.Int ).Mul (self .eth .GpoMinGasPrice , big .NewInt (100 ))
58- minbase = minbase .Div (minbase , big .NewInt (int64 (self .eth .GpobaseCorrectionFactor )))
59- self .minBase = minbase
60-
61- self .processPastBlocks ()
62- go self .listenLoop ()
63- return
76+ func (gpo * GasPriceOracle ) init () {
77+ gpo .initOnce .Do (func () {
78+ gpo .processPastBlocks (gpo .eth .BlockChain ())
79+ go gpo .listenLoop ()
80+ })
6481}
6582
66- func (self * GasPriceOracle ) processPastBlocks () {
83+ func (self * GasPriceOracle ) processPastBlocks (chain * core. BlockChain ) {
6784 last := int64 (- 1 )
68- cblock := self . chain .CurrentBlock ()
85+ cblock := chain .CurrentBlock ()
6986 if cblock != nil {
7087 last = int64 (cblock .NumberU64 ())
7188 }
@@ -75,7 +92,7 @@ func (self *GasPriceOracle) processPastBlocks() {
7592 }
7693 self .firstProcessed = uint64 (first )
7794 for i := first ; i <= last ; i ++ {
78- block := self . chain .GetBlockByNumber (uint64 (i ))
95+ block := chain .GetBlockByNumber (uint64 (i ))
7996 if block != nil {
8097 self .processBlock (block )
8198 }
@@ -84,9 +101,10 @@ func (self *GasPriceOracle) processPastBlocks() {
84101}
85102
86103func (self * GasPriceOracle ) listenLoop () {
87- defer self .events .Unsubscribe ()
104+ events := self .eth .EventMux ().Subscribe (core.ChainEvent {}, core.ChainSplitEvent {})
105+ defer events .Unsubscribe ()
88106
89- for event := range self . events .Chan () {
107+ for event := range events .Chan () {
90108 switch event := event .Data .(type ) {
91109 case core.ChainEvent :
92110 self .processBlock (event .Block )
@@ -102,7 +120,7 @@ func (self *GasPriceOracle) processBlock(block *types.Block) {
102120 self .lastProcessed = i
103121 }
104122
105- lastBase := self .eth . GpoMinGasPrice
123+ lastBase := self .minPrice
106124 bpl := self .blocks [i - 1 ]
107125 if bpl != nil {
108126 lastBase = bpl .baseGasPrice
@@ -176,28 +194,19 @@ func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int {
176194 return minPrice
177195}
178196
197+ // SuggestPrice returns the recommended gas price.
179198func (self * GasPriceOracle ) SuggestPrice () * big.Int {
199+ self .init ()
180200 self .lastBaseMutex .Lock ()
181- base := self .lastBase
201+ price := new (big. Int ). Set ( self .lastBase )
182202 self .lastBaseMutex .Unlock ()
183203
184- if base == nil {
185- base = self .eth .GpoMinGasPrice
204+ price .Mul (price , big .NewInt (int64 (self .eth .GpobaseCorrectionFactor )))
205+ price .Div (price , big .NewInt (100 ))
206+ if price .Cmp (self .minPrice ) < 0 {
207+ price .Set (self .minPrice )
208+ } else if self .eth .GpoMaxGasPrice != nil && price .Cmp (self .eth .GpoMaxGasPrice ) > 0 {
209+ price .Set (self .eth .GpoMaxGasPrice )
186210 }
187- if base == nil {
188- return big .NewInt (10000000000000 ) // apparently MinGasPrice is not initialized during some tests
189- }
190-
191- baseCorr := new (big.Int ).Mul (base , big .NewInt (int64 (self .eth .GpobaseCorrectionFactor )))
192- baseCorr .Div (baseCorr , big .NewInt (100 ))
193-
194- if baseCorr .Cmp (self .eth .GpoMinGasPrice ) < 0 {
195- return self .eth .GpoMinGasPrice
196- }
197-
198- if baseCorr .Cmp (self .eth .GpoMaxGasPrice ) > 0 {
199- return self .eth .GpoMaxGasPrice
200- }
201-
202- return baseCorr
211+ return price
203212}
0 commit comments