github.com/daethereum/go-dae@v2.2.3+incompatible/eth/gasprice/gasprice_test.go (about)

     1  // Copyright 2020 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package gasprice
    18  
    19  import (
    20  	"context"
    21  	"math"
    22  	"math/big"
    23  	"testing"
    24  
    25  	"github.com/daethereum/go-dae/common"
    26  	"github.com/daethereum/go-dae/consensus/ethash"
    27  	"github.com/daethereum/go-dae/core"
    28  	"github.com/daethereum/go-dae/core/rawdb"
    29  	"github.com/daethereum/go-dae/core/types"
    30  	"github.com/daethereum/go-dae/core/vm"
    31  	"github.com/daethereum/go-dae/crypto"
    32  	"github.com/daethereum/go-dae/event"
    33  	"github.com/daethereum/go-dae/params"
    34  	"github.com/daethereum/go-dae/rpc"
    35  )
    36  
    37  const testHead = 32
    38  
    39  type testBackend struct {
    40  	chain   *core.BlockChain
    41  	pending bool // pending block available
    42  }
    43  
    44  func (b *testBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
    45  	if number > testHead {
    46  		return nil, nil
    47  	}
    48  	if number == rpc.LatestBlockNumber {
    49  		number = testHead
    50  	}
    51  	if number == rpc.PendingBlockNumber {
    52  		if b.pending {
    53  			number = testHead + 1
    54  		} else {
    55  			return nil, nil
    56  		}
    57  	}
    58  	return b.chain.GetHeaderByNumber(uint64(number)), nil
    59  }
    60  
    61  func (b *testBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
    62  	if number > testHead {
    63  		return nil, nil
    64  	}
    65  	if number == rpc.LatestBlockNumber {
    66  		number = testHead
    67  	}
    68  	if number == rpc.PendingBlockNumber {
    69  		if b.pending {
    70  			number = testHead + 1
    71  		} else {
    72  			return nil, nil
    73  		}
    74  	}
    75  	return b.chain.GetBlockByNumber(uint64(number)), nil
    76  }
    77  
    78  func (b *testBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
    79  	return b.chain.GetReceiptsByHash(hash), nil
    80  }
    81  
    82  func (b *testBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
    83  	if b.pending {
    84  		block := b.chain.GetBlockByNumber(testHead + 1)
    85  		return block, b.chain.GetReceiptsByHash(block.Hash())
    86  	}
    87  	return nil, nil
    88  }
    89  
    90  func (b *testBackend) ChainConfig() *params.ChainConfig {
    91  	return b.chain.Config()
    92  }
    93  
    94  func (b *testBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
    95  	return nil
    96  }
    97  
    98  func newTestBackend(t *testing.T, londonBlock *big.Int, pending bool) *testBackend {
    99  	var (
   100  		key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
   101  		addr   = crypto.PubkeyToAddress(key.PublicKey)
   102  		config = *params.TestChainConfig // needs copy because it is modified below
   103  		gspec  = &core.Genesis{
   104  			Config: &config,
   105  			Alloc:  core.GenesisAlloc{addr: {Balance: big.NewInt(math.MaxInt64)}},
   106  		}
   107  		signer = types.LatestSigner(gspec.Config)
   108  	)
   109  	config.LondonBlock = londonBlock
   110  	config.ArrowGlacierBlock = londonBlock
   111  	config.GrayGlacierBlock = londonBlock
   112  	engine := ethash.NewFaker()
   113  	db := rawdb.NewMemoryDatabase()
   114  	genesis, err := gspec.Commit(db)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  	// Generate testing blocks
   119  	blocks, _ := core.GenerateChain(gspec.Config, genesis, engine, db, testHead+1, func(i int, b *core.BlockGen) {
   120  		b.SetCoinbase(common.Address{1})
   121  
   122  		var txdata types.TxData
   123  		if londonBlock != nil && b.Number().Cmp(londonBlock) >= 0 {
   124  			txdata = &types.DynamicFeeTx{
   125  				ChainID:   gspec.Config.ChainID,
   126  				Nonce:     b.TxNonce(addr),
   127  				To:        &common.Address{},
   128  				Gas:       30000,
   129  				GasFeeCap: big.NewInt(100 * params.GWei),
   130  				GasTipCap: big.NewInt(int64(i+1) * params.GWei),
   131  				Data:      []byte{},
   132  			}
   133  		} else {
   134  			txdata = &types.LegacyTx{
   135  				Nonce:    b.TxNonce(addr),
   136  				To:       &common.Address{},
   137  				Gas:      21000,
   138  				GasPrice: big.NewInt(int64(i+1) * params.GWei),
   139  				Value:    big.NewInt(100),
   140  				Data:     []byte{},
   141  			}
   142  		}
   143  		b.AddTx(types.MustSignNewTx(key, signer, txdata))
   144  	})
   145  	// Construct testing chain
   146  	diskdb := rawdb.NewMemoryDatabase()
   147  	gspec.Commit(diskdb)
   148  	chain, err := core.NewBlockChain(diskdb, &core.CacheConfig{TrieCleanNoPrefetch: true}, gspec.Config, engine, vm.Config{}, nil, nil)
   149  	if err != nil {
   150  		t.Fatalf("Failed to create local chain, %v", err)
   151  	}
   152  	chain.InsertChain(blocks)
   153  	return &testBackend{chain: chain, pending: pending}
   154  }
   155  
   156  func (b *testBackend) CurrentHeader() *types.Header {
   157  	return b.chain.CurrentHeader()
   158  }
   159  
   160  func (b *testBackend) GetBlockByNumber(number uint64) *types.Block {
   161  	return b.chain.GetBlockByNumber(number)
   162  }
   163  
   164  func TestSuggestTipCap(t *testing.T) {
   165  	config := Config{
   166  		Blocks:     3,
   167  		Percentile: 60,
   168  		Default:    big.NewInt(params.GWei),
   169  	}
   170  	var cases = []struct {
   171  		fork   *big.Int // London fork number
   172  		expect *big.Int // Expected gasprice suggestion
   173  	}{
   174  		{nil, big.NewInt(params.GWei * int64(30))},
   175  		{big.NewInt(0), big.NewInt(params.GWei * int64(30))},  // Fork point in genesis
   176  		{big.NewInt(1), big.NewInt(params.GWei * int64(30))},  // Fork point in first block
   177  		{big.NewInt(32), big.NewInt(params.GWei * int64(30))}, // Fork point in last block
   178  		{big.NewInt(33), big.NewInt(params.GWei * int64(30))}, // Fork point in the future
   179  	}
   180  	for _, c := range cases {
   181  		backend := newTestBackend(t, c.fork, false)
   182  		oracle := NewOracle(backend, config)
   183  
   184  		// The gas price sampled is: 32G, 31G, 30G, 29G, 28G, 27G
   185  		got, err := oracle.SuggestTipCap(context.Background())
   186  		if err != nil {
   187  			t.Fatalf("Failed to retrieve recommended gas price: %v", err)
   188  		}
   189  		if got.Cmp(c.expect) != 0 {
   190  			t.Fatalf("Gas price mismatch, want %d, got %d", c.expect, got)
   191  		}
   192  	}
   193  }