gitlab.com/flarenetwork/coreth@v0.1.1/chain/test_chain.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package chain
     5  
     6  import (
     7  	"crypto/rand"
     8  	"math/big"
     9  	"testing"
    10  
    11  	"github.com/ethereum/go-ethereum/common"
    12  	"github.com/ethereum/go-ethereum/common/hexutil"
    13  	"gitlab.com/flarenetwork/coreth/accounts/keystore"
    14  	"gitlab.com/flarenetwork/coreth/consensus/dummy"
    15  	"gitlab.com/flarenetwork/coreth/core"
    16  	"gitlab.com/flarenetwork/coreth/core/rawdb"
    17  	"gitlab.com/flarenetwork/coreth/core/types"
    18  	"gitlab.com/flarenetwork/coreth/eth"
    19  	"gitlab.com/flarenetwork/coreth/eth/ethconfig"
    20  	"gitlab.com/flarenetwork/coreth/node"
    21  	"gitlab.com/flarenetwork/coreth/params"
    22  )
    23  
    24  var (
    25  	basicTxGasLimit       = 21000
    26  	fundedKey, bob, alice *keystore.Key
    27  	initialBalance        = big.NewInt(1000000000000000000)
    28  	chainID               = big.NewInt(1)
    29  	value                 = big.NewInt(1000000000000)
    30  	gasLimit              = 1000000
    31  	gasPrice              = big.NewInt(params.LaunchMinGasPrice)
    32  )
    33  
    34  func init() {
    35  	genKey, err := keystore.NewKey(rand.Reader)
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  	fundedKey = genKey
    40  	genKey, err = keystore.NewKey(rand.Reader)
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	bob = genKey
    45  	genKey, err = keystore.NewKey(rand.Reader)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	alice = genKey
    50  }
    51  
    52  func NewDefaultChain(t *testing.T) (*ETHChain, chan core.NewTxPoolHeadEvent, <-chan core.NewTxsEvent) {
    53  	// configure the chain
    54  	config := ethconfig.NewDefaultConfig()
    55  	chainConfig := &params.ChainConfig{
    56  		ChainID:             chainID,
    57  		HomesteadBlock:      big.NewInt(0),
    58  		DAOForkBlock:        big.NewInt(0),
    59  		DAOForkSupport:      true,
    60  		EIP150Block:         big.NewInt(0),
    61  		EIP150Hash:          common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
    62  		EIP155Block:         big.NewInt(0),
    63  		EIP158Block:         big.NewInt(0),
    64  		ByzantiumBlock:      big.NewInt(0),
    65  		ConstantinopleBlock: big.NewInt(0),
    66  		PetersburgBlock:     big.NewInt(0),
    67  		IstanbulBlock:       big.NewInt(0),
    68  	}
    69  
    70  	config.Genesis = &core.Genesis{
    71  		Config:     chainConfig,
    72  		Nonce:      0,
    73  		Number:     0,
    74  		ExtraData:  hexutil.MustDecode("0x00"),
    75  		GasLimit:   100000000,
    76  		Difficulty: big.NewInt(0),
    77  		Alloc:      core.GenesisAlloc{fundedKey.Address: {Balance: initialBalance}},
    78  	}
    79  
    80  	var (
    81  		chain *ETHChain
    82  		err   error
    83  	)
    84  	chain, err = NewETHChain(
    85  		&config,
    86  		&node.Config{},
    87  		rawdb.NewMemoryDatabase(),
    88  		eth.DefaultSettings,
    89  		new(dummy.ConsensusCallbacks),
    90  		common.Hash{},
    91  	)
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	newTxPoolHeadChan := make(chan core.NewTxPoolHeadEvent, 1)
    97  	chain.GetTxPool().SubscribeNewHeadEvent(newTxPoolHeadChan)
    98  
    99  	txSubmitCh := chain.GetTxSubmitCh()
   100  	return chain, newTxPoolHeadChan, txSubmitCh
   101  }
   102  
   103  // insertAndAccept inserts [block] into [chain], sets the chains preference to it
   104  // and then Accepts it.
   105  func insertAndAccept(t *testing.T, chain *ETHChain, block *types.Block) {
   106  	if err := chain.InsertBlock(block); err != nil {
   107  		t.Fatal(err)
   108  	}
   109  	if err := chain.SetPreference(block); err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	if err := chain.Accept(block); err != nil {
   113  		t.Fatal(err)
   114  	}
   115  }
   116  
   117  func insertAndSetPreference(t *testing.T, chain *ETHChain, block *types.Block) {
   118  	if err := chain.InsertBlock(block); err != nil {
   119  		t.Fatal(err)
   120  	}
   121  	if err := chain.SetPreference(block); err != nil {
   122  		t.Fatal(err)
   123  	}
   124  }