-
Notifications
You must be signed in to change notification settings - Fork 225
/
fee_info_provider.go
145 lines (132 loc) · 4.98 KB
/
fee_info_provider.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// (c) 2019-2022, Ava Labs, Inc.
//
// This file is a derived work, based on the go-ethereum library whose original
// notices appear below.
//
// It is distributed under a license compatible with the licensing terms of the
// original code from which it is derived.
//
// Much love to the original authors for their work.
// **********
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package gasprice
import (
"context"
"math/big"
"github.com/ava-labs/subnet-evm/core"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/rpc"
lru "github.com/hashicorp/golang-lru"
)
// additional slots in the header cache to allow processing queries
// for previous blocks (with full number of blocks desired) if new
// blocks are added concurrently.
const feeCacheExtraSlots = 5
type feeInfoProvider struct {
cache *lru.Cache
backend OracleBackend
// [minGasUsed] ensures we don't recommend users pay non-zero tips when other
// users are paying a tip to unnecessarily expedite block production.
minGasUsed uint64
newHeaderAdded func() // callback used in tests
}
// feeInfo is the type of data stored in feeInfoProvider's cache.
type feeInfo struct {
baseFee, tip *big.Int // baseFee and min. suggested tip for tx to be included in the block
timestamp uint64 // timestamp of the block header
}
// newFeeInfoProvider returns a bounded buffer with [size] slots to
// store [*feeInfo] for the most recently accepted blocks.
func newFeeInfoProvider(backend OracleBackend, minGasUsed uint64, size int) (*feeInfoProvider, error) {
fc := &feeInfoProvider{
backend: backend,
minGasUsed: minGasUsed,
}
if size == 0 {
// if size is zero, we return early as there is no
// reason for a goroutine to subscribe to the chain's
// accepted event.
fc.cache, _ = lru.New(size)
return fc, nil
}
fc.cache, _ = lru.New(size + feeCacheExtraSlots)
// subscribe to the chain accepted event
acceptedEvent := make(chan core.ChainEvent, 1)
backend.SubscribeChainAcceptedEvent(acceptedEvent)
go func() {
for ev := range acceptedEvent {
fc.addHeader(context.Background(), ev.Block.Header())
if fc.newHeaderAdded != nil {
fc.newHeaderAdded()
}
}
}()
return fc, fc.populateCache(size)
}
// addHeader processes header into a feeInfo struct and caches the result.
func (f *feeInfoProvider) addHeader(ctx context.Context, header *types.Header) (*feeInfo, error) {
feeInfo := &feeInfo{
timestamp: header.Time,
baseFee: header.BaseFee,
}
// Don't bias the estimate with blocks containing a limited number of transactions paying to
// expedite block production.
var err error
if f.minGasUsed <= header.GasUsed {
// Compute minimum required tip to be included in previous block
//
// NOTE: Using this approach, we will never recommend that the caller
// provides a non-zero tip unless some block is produced faster than the
// target rate (which could only occur if some set of callers manually override the
// suggested tip). In the future, we may wish to start suggesting a non-zero
// tip when most blocks are full otherwise callers may observe an unexpected
// delay in transaction inclusion.
feeInfo.tip, err = f.backend.MinRequiredTip(ctx, header)
}
f.cache.Add(header.Number.Uint64(), feeInfo)
return feeInfo, err
}
// get returns the feeInfo for block with [number] if present in the cache
// and a boolean representing if it was found.
func (f *feeInfoProvider) get(number uint64) (*feeInfo, bool) {
// Note: use Peek on LRU to use it as a bounded buffer.
feeInfoIntf, ok := f.cache.Peek(number)
if ok {
return feeInfoIntf.(*feeInfo), ok
}
return nil, ok
}
// populateCache populates [f] with [size] blocks up to last accepted.
// Note: assumes [size] is greater than zero.
func (f *feeInfoProvider) populateCache(size int) error {
lastAccepted := f.backend.LastAcceptedBlock().NumberU64()
lowerBlockNumber := uint64(0)
if uint64(size-1) <= lastAccepted { // Note: "size-1" because we need a total of size blocks.
lowerBlockNumber = lastAccepted - uint64(size-1)
}
for i := lowerBlockNumber; i <= lastAccepted; i++ {
header, err := f.backend.HeaderByNumber(context.Background(), rpc.BlockNumber(i))
if err != nil {
return err
}
_, err = f.addHeader(context.Background(), header)
if err != nil {
return err
}
}
return nil
}