diff --git a/gasprice/gasprice.go b/gasprice/gasprice.go index c84f0a3f96..a800a26002 100644 --- a/gasprice/gasprice.go +++ b/gasprice/gasprice.go @@ -42,8 +42,8 @@ type Config struct { IgnorePrice *big.Int } -// Backend is the interface representing blockchain and consensus backend -type Backend interface { +// Blockchain is the interface representing blockchain +type Blockchain interface { GetBlockByHash(hash types.Hash, full bool) (*types.Block, bool) Header() *types.Header Config() *chain.Params @@ -73,7 +73,7 @@ type GasHelper struct { // when collecting transactions ignorePrice *big.Int // backend is an abstraction of blockchain - backend Backend + backend Blockchain // lastHeaderHash is the last header for which maxPriorityFeePerGas was returned lastHeaderHash types.Hash @@ -81,7 +81,7 @@ type GasHelper struct { } // NewGasHelper is the constructor function for GasHelper struct -func NewGasHelper(config *Config, backend Backend) *GasHelper { +func NewGasHelper(config *Config, backend Blockchain) *GasHelper { pricePercentile := config.PricePercentile if pricePercentile > 100 { pricePercentile = 100 @@ -199,7 +199,9 @@ func (g *GasHelper) MaxPriorityFeePerGas() (*big.Int, error) { if len(allPrices) > 0 { // sort prices from lowest to highest - sort.Sort(bigIntSorted(allPrices)) + sort.Slice(allPrices, func(i, j int) bool { + return allPrices[i].Cmp(allPrices[j]) < 0 + }) // take the biggest price that is in the configured percentage // by default it's 60, so it will take the price on that percentage // of all prices in the array @@ -250,19 +252,3 @@ func (t *txSortedByEffectiveTip) Less(i, j int) bool { return tip1.Cmp(tip2) < 0 } - -// bigIntSorted sorts big int slice from lowest to highest value -type bigIntSorted []*big.Int - -// Len is implementation of sort.Interface -func (b bigIntSorted) Len() int { return len(b) } - -// Less is implementation of sort.Interface -func (b bigIntSorted) Less(i, j int) bool { - return b[i].Cmp(b[j]) < 0 -} - -// Swap is implementation of sort.Interface -func (b bigIntSorted) Swap(i, j int) { - b[i], b[j] = b[j], b[i] -} diff --git a/gasprice/gasprice_test.go b/gasprice/gasprice_test.go index 666d4ae312..f4ff7bbde3 100644 --- a/gasprice/gasprice_test.go +++ b/gasprice/gasprice_test.go @@ -23,12 +23,12 @@ func TestGasHelper_MaxPriorityFeePerGas(t *testing.T) { Name string Expected *big.Int Error bool - GetBackend func() Backend + GetBackend func() Blockchain }{ { Name: "Chain just started", Expected: DefaultGasHelperConfig.LastPrice, - GetBackend: func() Backend { + GetBackend: func() Blockchain { genesis := &types.Header{ Number: 0, Hash: types.StringToHash("genesis"), @@ -46,7 +46,7 @@ func TestGasHelper_MaxPriorityFeePerGas(t *testing.T) { { Name: "Block does not exist", Error: true, - GetBackend: func() Backend { + GetBackend: func() Blockchain { header := &types.Header{ Number: 0, Hash: types.StringToHash("some header"), @@ -61,7 +61,7 @@ func TestGasHelper_MaxPriorityFeePerGas(t *testing.T) { { Name: "Empty blocks", Expected: DefaultGasHelperConfig.LastPrice, // should return last (default) price - GetBackend: func() Backend { + GetBackend: func() Blockchain { backend := createTestBlocks(t, 10) return backend @@ -70,7 +70,7 @@ func TestGasHelper_MaxPriorityFeePerGas(t *testing.T) { { Name: "All transactions by miner", Expected: DefaultGasHelperConfig.LastPrice, // should return last (default) price - GetBackend: func() Backend { + GetBackend: func() Blockchain { backend := createTestBlocks(t, 10) rand.Seed(time.Now().UTC().UnixNano()) @@ -106,7 +106,7 @@ func TestGasHelper_MaxPriorityFeePerGas(t *testing.T) { { Name: "All transactions have small effective tip", Expected: DefaultGasHelperConfig.LastPrice, // should return last (default) price - GetBackend: func() Backend { + GetBackend: func() Blockchain { backend := createTestBlocks(t, 10) createTestTxs(t, backend, 3, 1) @@ -117,7 +117,7 @@ func TestGasHelper_MaxPriorityFeePerGas(t *testing.T) { Name: "Number of blocks in chain smaller than numOfBlocksToCheck", Expected: DefaultGasHelperConfig.LastPrice.Mul( DefaultGasHelperConfig.LastPrice, big.NewInt(2)), // at least two times of default last price - GetBackend: func() Backend { + GetBackend: func() Blockchain { backend := createTestBlocks(t, 10) createTestTxs(t, backend, 3, 200) @@ -128,7 +128,7 @@ func TestGasHelper_MaxPriorityFeePerGas(t *testing.T) { Name: "Number of blocks in chain higher than numOfBlocksToCheck", Expected: DefaultGasHelperConfig.LastPrice.Mul( DefaultGasHelperConfig.LastPrice, big.NewInt(2)), // at least two times of default last price - GetBackend: func() Backend { + GetBackend: func() Blockchain { backend := createTestBlocks(t, 30) createTestTxs(t, backend, 3, 200) @@ -139,7 +139,7 @@ func TestGasHelper_MaxPriorityFeePerGas(t *testing.T) { Name: "Not enough transactions in first 20 blocks, so read some more blocks", Expected: DefaultGasHelperConfig.LastPrice.Mul( DefaultGasHelperConfig.LastPrice, big.NewInt(2)), // at least two times of default last price - GetBackend: func() Backend { + GetBackend: func() Blockchain { backend := createTestBlocks(t, 50) createTestTxs(t, backend, 1, 200) @@ -233,7 +233,7 @@ func createTestTxs(t *testing.T, backend *backendMock, numOfTxsPerBlock, txCap i } } -var _ Backend = (*backendMock)(nil) +var _ Blockchain = (*backendMock)(nil) type backendMock struct { mock.Mock