github.com/theQRL/go-zond@v0.1.1/zond/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/theQRL/go-zond/common"
    26  	"github.com/theQRL/go-zond/consensus/ethash"
    27  	"github.com/theQRL/go-zond/core"
    28  	"github.com/theQRL/go-zond/core/rawdb"
    29  	"github.com/theQRL/go-zond/core/types"
    30  	"github.com/theQRL/go-zond/core/vm"
    31  	"github.com/theQRL/go-zond/crypto"
    32  	"github.com/theQRL/go-zond/event"
    33  	"github.com/theQRL/go-zond/params"
    34  	"github.com/theQRL/go-zond/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.EarliestBlockNumber {
    49  		number = 0
    50  	}
    51  	if number == rpc.FinalizedBlockNumber {
    52  		return b.chain.CurrentFinalBlock(), nil
    53  	}
    54  	if number == rpc.SafeBlockNumber {
    55  		return b.chain.CurrentSafeBlock(), nil
    56  	}
    57  	if number == rpc.LatestBlockNumber {
    58  		number = testHead
    59  	}
    60  	if number == rpc.PendingBlockNumber {
    61  		if b.pending {
    62  			number = testHead + 1
    63  		} else {
    64  			return nil, nil
    65  		}
    66  	}
    67  	return b.chain.GetHeaderByNumber(uint64(number)), nil
    68  }
    69  
    70  func (b *testBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
    71  	if number > testHead {
    72  		return nil, nil
    73  	}
    74  	if number == rpc.EarliestBlockNumber {
    75  		number = 0
    76  	}
    77  	if number == rpc.FinalizedBlockNumber {
    78  		number = rpc.BlockNumber(b.chain.CurrentFinalBlock().Number.Uint64())
    79  	}
    80  	if number == rpc.SafeBlockNumber {
    81  		number = rpc.BlockNumber(b.chain.CurrentSafeBlock().Number.Uint64())
    82  	}
    83  	if number == rpc.LatestBlockNumber {
    84  		number = testHead
    85  	}
    86  	if number == rpc.PendingBlockNumber {
    87  		if b.pending {
    88  			number = testHead + 1
    89  		} else {
    90  			return nil, nil
    91  		}
    92  	}
    93  	return b.chain.GetBlockByNumber(uint64(number)), nil
    94  }
    95  
    96  func (b *testBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
    97  	return b.chain.GetReceiptsByHash(hash), nil
    98  }
    99  
   100  func (b *testBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
   101  	if b.pending {
   102  		block := b.chain.GetBlockByNumber(testHead + 1)
   103  		return block, b.chain.GetReceiptsByHash(block.Hash())
   104  	}
   105  	return nil, nil
   106  }
   107  
   108  func (b *testBackend) ChainConfig() *params.ChainConfig {
   109  	return b.chain.Config()
   110  }
   111  
   112  func (b *testBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
   113  	return nil
   114  }
   115  
   116  func (b *testBackend) teardown() {
   117  	b.chain.Stop()
   118  }
   119  
   120  // newTestBackend creates a test backend. OBS: don't forget to invoke tearDown
   121  // after use, otherwise the blockchain instance will mem-leak via goroutines.
   122  func newTestBackend(t *testing.T, londonBlock *big.Int, pending bool) *testBackend {
   123  	var (
   124  		key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
   125  		addr   = crypto.PubkeyToAddress(key.PublicKey)
   126  		config = *params.TestChainConfig // needs copy because it is modified below
   127  		gspec  = &core.Genesis{
   128  			Config: &config,
   129  			Alloc:  core.GenesisAlloc{addr: {Balance: big.NewInt(math.MaxInt64)}},
   130  		}
   131  		signer = types.LatestSigner(gspec.Config)
   132  	)
   133  	config.LondonBlock = londonBlock
   134  	config.ArrowGlacierBlock = londonBlock
   135  	config.GrayGlacierBlock = londonBlock
   136  	config.TerminalTotalDifficulty = common.Big0
   137  	engine := ethash.NewFaker()
   138  
   139  	// Generate testing blocks
   140  	_, blocks, _ := core.GenerateChainWithGenesis(gspec, engine, testHead+1, func(i int, b *core.BlockGen) {
   141  		b.SetCoinbase(common.Address{1})
   142  
   143  		var txdata types.TxData
   144  		if londonBlock != nil && b.Number().Cmp(londonBlock) >= 0 {
   145  			txdata = &types.DynamicFeeTx{
   146  				ChainID:   gspec.Config.ChainID,
   147  				Nonce:     b.TxNonce(addr),
   148  				To:        &common.Address{},
   149  				Gas:       30000,
   150  				GasFeeCap: big.NewInt(100 * params.GWei),
   151  				GasTipCap: big.NewInt(int64(i+1) * params.GWei),
   152  				Data:      []byte{},
   153  			}
   154  		} else {
   155  			txdata = &types.LegacyTx{
   156  				Nonce:    b.TxNonce(addr),
   157  				To:       &common.Address{},
   158  				Gas:      21000,
   159  				GasPrice: big.NewInt(int64(i+1) * params.GWei),
   160  				Value:    big.NewInt(100),
   161  				Data:     []byte{},
   162  			}
   163  		}
   164  		b.AddTx(types.MustSignNewTx(key, signer, txdata))
   165  	})
   166  	// Construct testing chain
   167  	chain, err := core.NewBlockChain(rawdb.NewMemoryDatabase(), &core.CacheConfig{TrieCleanNoPrefetch: true}, gspec, nil, engine, vm.Config{}, nil, nil)
   168  	if err != nil {
   169  		t.Fatalf("Failed to create local chain, %v", err)
   170  	}
   171  	chain.InsertChain(blocks)
   172  	chain.SetFinalized(chain.GetBlockByNumber(25).Header())
   173  	chain.SetSafe(chain.GetBlockByNumber(25).Header())
   174  	return &testBackend{chain: chain, pending: pending}
   175  }
   176  
   177  func (b *testBackend) CurrentHeader() *types.Header {
   178  	return b.chain.CurrentHeader()
   179  }
   180  
   181  func (b *testBackend) GetBlockByNumber(number uint64) *types.Block {
   182  	return b.chain.GetBlockByNumber(number)
   183  }
   184  
   185  func TestSuggestTipCap(t *testing.T) {
   186  	config := Config{
   187  		Blocks:     3,
   188  		Percentile: 60,
   189  		Default:    big.NewInt(params.GWei),
   190  	}
   191  	var cases = []struct {
   192  		fork   *big.Int // London fork number
   193  		expect *big.Int // Expected gasprice suggestion
   194  	}{
   195  		{nil, big.NewInt(params.GWei * int64(30))},
   196  		{big.NewInt(0), big.NewInt(params.GWei * int64(30))},  // Fork point in genesis
   197  		{big.NewInt(1), big.NewInt(params.GWei * int64(30))},  // Fork point in first block
   198  		{big.NewInt(32), big.NewInt(params.GWei * int64(30))}, // Fork point in last block
   199  		{big.NewInt(33), big.NewInt(params.GWei * int64(30))}, // Fork point in the future
   200  	}
   201  	for _, c := range cases {
   202  		backend := newTestBackend(t, c.fork, false)
   203  		oracle := NewOracle(backend, config)
   204  
   205  		// The gas price sampled is: 32G, 31G, 30G, 29G, 28G, 27G
   206  		got, err := oracle.SuggestTipCap(context.Background())
   207  		backend.teardown()
   208  		if err != nil {
   209  			t.Fatalf("Failed to retrieve recommended gas price: %v", err)
   210  		}
   211  		if got.Cmp(c.expect) != 0 {
   212  			t.Fatalf("Gas price mismatch, want %d, got %d", c.expect, got)
   213  		}
   214  	}
   215  }