github.com/MetalBlockchain/subnet-evm@v0.4.9/core/chain_makers_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2015 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package core
    28  
    29  import (
    30  	"fmt"
    31  	"math/big"
    32  
    33  	"github.com/MetalBlockchain/subnet-evm/consensus/dummy"
    34  	"github.com/MetalBlockchain/subnet-evm/core/rawdb"
    35  	"github.com/MetalBlockchain/subnet-evm/core/types"
    36  	"github.com/MetalBlockchain/subnet-evm/core/vm"
    37  	"github.com/MetalBlockchain/subnet-evm/params"
    38  	"github.com/ethereum/go-ethereum/common"
    39  	"github.com/ethereum/go-ethereum/crypto"
    40  )
    41  
    42  func ExampleGenerateChain() {
    43  	var (
    44  		key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    45  		key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
    46  		key3, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
    47  		addr1   = crypto.PubkeyToAddress(key1.PublicKey)
    48  		addr2   = crypto.PubkeyToAddress(key2.PublicKey)
    49  		addr3   = crypto.PubkeyToAddress(key3.PublicKey)
    50  		db      = rawdb.NewMemoryDatabase()
    51  	)
    52  
    53  	// Ensure that key1 has some funds in the genesis block.
    54  	gspec := &Genesis{
    55  		Config: &params.ChainConfig{HomesteadBlock: new(big.Int)},
    56  		Alloc:  GenesisAlloc{addr1: {Balance: big.NewInt(1000000)}},
    57  	}
    58  	genesis := gspec.MustCommit(db)
    59  
    60  	// This call generates a chain of 3 blocks. The function runs for
    61  	// each block and adds different features to gen based on the
    62  	// block index.
    63  	signer := types.HomesteadSigner{}
    64  	chain, _, err := GenerateChain(gspec.Config, genesis, dummy.NewCoinbaseFaker(), db, 3, 10, func(i int, gen *BlockGen) {
    65  		switch i {
    66  		case 0:
    67  			// In block 1, addr1 sends addr2 some ether.
    68  			tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), params.TxGas, nil, nil), signer, key1)
    69  			gen.AddTx(tx)
    70  		case 1:
    71  			// In block 2, addr1 sends some more ether to addr2.
    72  			// addr2 passes it on to addr3.
    73  			tx1, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(1000), params.TxGas, nil, nil), signer, key1)
    74  			gen.AddTx(tx1)
    75  		case 2:
    76  			tx2, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key2)
    77  			gen.AddTx(tx2)
    78  		}
    79  	})
    80  	if err != nil {
    81  		panic(err)
    82  	}
    83  
    84  	// Import the chain. This runs all block validation rules.
    85  	blockchain, _ := NewBlockChain(db, DefaultCacheConfig, gspec.Config, dummy.NewCoinbaseFaker(), vm.Config{}, common.Hash{})
    86  	defer blockchain.Stop()
    87  
    88  	if i, err := blockchain.InsertChain(chain); err != nil {
    89  		fmt.Printf("insert error (block %d): %v\n", chain[i].NumberU64(), err)
    90  		return
    91  	}
    92  
    93  	state, _ := blockchain.State()
    94  	fmt.Printf("last block: #%d\n", blockchain.CurrentBlock().Number())
    95  	fmt.Println("balance of addr1:", state.GetBalance(addr1))
    96  	fmt.Println("balance of addr2:", state.GetBalance(addr2))
    97  	fmt.Println("balance of addr3:", state.GetBalance(addr3))
    98  	// Expected output has been modified since uncle blocks and block rewards have
    99  	// been removed from the original test.
   100  
   101  	// Output:
   102  	// last block: #3
   103  	// balance of addr1: 989000
   104  	// balance of addr2: 10000
   105  	// balance of addr3: 1000
   106  }