Skip to content

Commit

Permalink
Comments fix
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Jun 21, 2023
1 parent 30b53b3 commit 67ff422
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
28 changes: 7 additions & 21 deletions gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,15 +73,15 @@ 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

lock sync.Mutex
}

// 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
}
20 changes: 10 additions & 10 deletions gasprice/gasprice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand All @@ -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
Expand All @@ -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())

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 67ff422

Please sign in to comment.