github.com/core-coin/go-core/v2@v2.1.9/core/chain_makers_test.go (about)

     1  // Copyright 2015 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core 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/core-coin/go-core/v2/consensus/cryptore"
    24  
    25  	"github.com/core-coin/go-core/v2/core/rawdb"
    26  	"github.com/core-coin/go-core/v2/core/types"
    27  	"github.com/core-coin/go-core/v2/core/vm"
    28  	"github.com/core-coin/go-core/v2/crypto"
    29  	"github.com/core-coin/go-core/v2/params"
    30  )
    31  
    32  func ExampleGenerateChain() {
    33  	var (
    34  		key1, _ = crypto.UnmarshalPrivateKeyHex("89bdfaa2b6f9c30b94ee98fec96c58ff8507fabf49d36a6267e6cb5516eaa2a9e854eccc041f9f67e109d0eb4f653586855355c5b2b87bb313")
    35  		key2, _ = crypto.UnmarshalPrivateKeyHex("ab856a9af6b0b651dd2f43b5e12193652ec1701c4da6f1c0d2a366ac4b9dabc9433ef09e41ca129552bd2c029086d9b03604de872a3b343204")
    36  		key3, _ = crypto.UnmarshalPrivateKeyHex("c0b711eea422df26d5ffdcaae35fe0527cf647c5ce62d3efb5e09a0e14fc8afe57fac1a5daa330bc10bfa1d3db11e172a822dcfffb86a0b26d")
    37  		db      = rawdb.NewMemoryDatabase()
    38  	)
    39  
    40  	// Ensure that key1 has some funds in the genesis block.
    41  	gspec := &Genesis{
    42  		Config: &params.ChainConfig{NetworkID: big.NewInt(1)},
    43  		Alloc:  GenesisAlloc{key1.Address(): {Balance: big.NewInt(1000000)}},
    44  	}
    45  	genesis := gspec.MustCommit(db)
    46  
    47  	// This call generates a chain of 5 blocks. The function runs for
    48  	// each block and adds different features to gen based on the
    49  	// block index.
    50  	signer := types.NewNucleusSigner(big.NewInt(1))
    51  	chain, _ := GenerateChain(gspec.Config, genesis, cryptore.NewFaker(), db, 5, func(i int, gen *BlockGen) {
    52  		switch i {
    53  		case 0:
    54  			// In block 1, key1.Address() sends key2.Address() some core.
    55  			tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(key1.Address()), key2.Address(), big.NewInt(10000), params.TxEnergy, nil, nil), signer, key1)
    56  			gen.AddTx(tx)
    57  		case 1:
    58  			// In block 2, key1.Address() sends some more core to key2.Address().
    59  			// key2.Address() passes it on to key3.Address().
    60  			tx1, _ := types.SignTx(types.NewTransaction(gen.TxNonce(key1.Address()), key2.Address(), big.NewInt(1000), params.TxEnergy, nil, nil), signer, key1)
    61  			tx2, _ := types.SignTx(types.NewTransaction(gen.TxNonce(key2.Address()), key3.Address(), big.NewInt(1000), params.TxEnergy, nil, nil), signer, key2)
    62  			gen.AddTx(tx1)
    63  			gen.AddTx(tx2)
    64  		case 2:
    65  			// Block 3 is empty but was mined by key3.Address().
    66  			gen.SetCoinbase(key3.Address())
    67  			gen.SetExtra([]byte("yeehaw"))
    68  		case 3:
    69  			// Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data).
    70  			b2 := gen.PrevBlock(1).Header()
    71  			b2.Extra = []byte("foo")
    72  			gen.AddUncle(b2)
    73  			b3 := gen.PrevBlock(2).Header()
    74  			b3.Extra = []byte("foo")
    75  			gen.AddUncle(b3)
    76  		}
    77  	})
    78  
    79  	// Import the chain. This runs all block validation rules.
    80  	blockchain, _ := NewBlockChain(db, nil, gspec.Config, cryptore.NewFaker(), vm.Config{}, nil, nil)
    81  	defer blockchain.Stop()
    82  
    83  	if i, err := blockchain.InsertChain(chain); err != nil {
    84  		fmt.Printf("insert error (block %d): %v\n", chain[i].NumberU64(), err)
    85  		return
    86  	}
    87  
    88  	state, _ := blockchain.State()
    89  	fmt.Printf("last block: #%d\n", blockchain.CurrentBlock().Number())
    90  	fmt.Println("balance of key1.Address():", state.GetBalance(key1.Address()))
    91  	fmt.Println("balance of key2.Address():", state.GetBalance(key2.Address()))
    92  	fmt.Println("balance of key3.Address():", state.GetBalance(key3.Address()))
    93  	// Output:
    94  	// last block: #5
    95  	// balance of key1.Address(): 989000
    96  	// balance of key2.Address(): 10000
    97  	// balance of key3.Address(): 19687500000000001000
    98  }