github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/core/chain_makers_test.go (about)

     1  // Copyright 2015 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 core
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  
    23  	"github.com/vntchain/go-vnt/consensus/mock"
    24  	"github.com/vntchain/go-vnt/core/types"
    25  	"github.com/vntchain/go-vnt/core/vm"
    26  	"github.com/vntchain/go-vnt/crypto"
    27  	"github.com/vntchain/go-vnt/params"
    28  	"github.com/vntchain/go-vnt/vntdb"
    29  )
    30  
    31  func ExampleGenerateChain() {
    32  	var (
    33  		key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    34  		key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
    35  		key3, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
    36  		addr1   = crypto.PubkeyToAddress(key1.PublicKey)
    37  		addr2   = crypto.PubkeyToAddress(key2.PublicKey)
    38  		addr3   = crypto.PubkeyToAddress(key3.PublicKey)
    39  		db      = vntdb.NewMemDatabase()
    40  
    41  		activeKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f292")
    42  		activeAddr   = crypto.PubkeyToAddress(activeKey.PublicKey)
    43  	)
    44  
    45  	// Ensure that key1 has some funds in the genesis block.
    46  	gspec := &Genesis{
    47  		Config: &params.ChainConfig{ChainID: big.NewInt(1), HubbleBlock: new(big.Int)},
    48  		Alloc:  GenesisAlloc{addr1: {Balance: big.NewInt(1000000)}, activeAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10e9), big.NewInt(1e18))}},
    49  	}
    50  	signer := types.NewHubbleSigner(gspec.Config.ChainID)
    51  
    52  	genesis := gspec.MustCommit(db)
    53  
    54  	// This call generates a chain of 5 blocks. The function runs for
    55  	// each block and adds different features to gen based on the
    56  	// block index.
    57  	chain, _ := GenerateChain(gspec.Config, genesis, mock.NewMock(), db, 6, func(i int, gen *BlockGen) {
    58  		switch i {
    59  		case 1:
    60  			// In block 2, addr1 sends addr2 some ether.
    61  			tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), params.TxGas, nil, nil), signer, key1)
    62  			gen.AddTx(tx)
    63  		case 2:
    64  			// In block 3, addr1 sends some more ether to addr2.
    65  			// addr2 passes it on to addr3.
    66  			tx1, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(1000), params.TxGas, nil, nil), signer, key1)
    67  			tx2, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key2)
    68  			gen.AddTx(tx1)
    69  			gen.AddTx(tx2)
    70  		case 3:
    71  			// Block 4 is empty but was mined by addr3.
    72  			gen.SetCoinbase(addr3)
    73  			gen.SetExtra([]byte("yeehaw"))
    74  		}
    75  	})
    76  
    77  	// Import the chain. This runs all block validation rules.
    78  	blockchain, _ := NewBlockChain(db, nil, gspec.Config, mock.NewMock(), vm.Config{})
    79  	defer blockchain.Stop()
    80  
    81  	if i, err := blockchain.InsertChain(chain); err != nil {
    82  		fmt.Printf("insert error (block %d): %v\n", chain[i].NumberU64(), err)
    83  		return
    84  	}
    85  
    86  	state, _ := blockchain.State()
    87  	fmt.Printf("last block: #%d\n", blockchain.CurrentBlock().Number())
    88  	fmt.Println("balance of addr1:", state.GetBalance(addr1))
    89  	fmt.Println("balance of addr2:", state.GetBalance(addr2))
    90  	fmt.Println("balance of addr3:", state.GetBalance(addr3))
    91  	// Output:
    92  	// last block: #6
    93  	// balance of addr1: 989000
    94  	// balance of addr2: 10000
    95  	// balance of addr3: 15000000000000001000
    96  }