github.com/m3shine/gochain@v2.2.26+incompatible/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 "context" 21 "fmt" 22 "math/big" 23 24 "github.com/gochain-io/gochain/consensus/clique" 25 "github.com/gochain-io/gochain/core/types" 26 "github.com/gochain-io/gochain/core/vm" 27 "github.com/gochain-io/gochain/crypto" 28 "github.com/gochain-io/gochain/ethdb" 29 "github.com/gochain-io/gochain/params" 30 ) 31 32 func ExampleGenerateChain() { 33 ctx := context.Background() 34 var ( 35 key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 36 key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") 37 key3, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") 38 addr1 = crypto.PubkeyToAddress(key1.PublicKey) 39 addr2 = crypto.PubkeyToAddress(key2.PublicKey) 40 addr3 = crypto.PubkeyToAddress(key3.PublicKey) 41 db = ethdb.NewMemDatabase() 42 ) 43 44 // Ensure that key1 has some funds in the genesis block. 45 gspec := &Genesis{ 46 Config: ¶ms.ChainConfig{HomesteadBlock: new(big.Int), Clique: params.DefaultCliqueConfig()}, 47 Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(1000000)}}, 48 } 49 genesis := gspec.MustCommit(db) 50 engine := clique.NewFaker() 51 52 // This call generates a chain of 5 blocks. The function runs for 53 // each block and adds different features to gen based on the 54 // block index. 55 signer := types.HomesteadSigner{} 56 chain, _ := GenerateChain(ctx, gspec.Config, genesis, engine, db, 5, func(ctx context.Context, i int, gen *BlockGen) { 57 switch i { 58 case 0: 59 // In block 1, addr1 sends addr2 some ether. 60 tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), params.TxGas, nil, nil), signer, key1) 61 gen.AddTx(ctx, tx) 62 case 1: 63 // In block 2, addr1 sends some more ether to addr2. 64 // addr2 passes it on to addr3. 65 tx1, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(1000), params.TxGas, nil, nil), signer, key1) 66 tx2, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key2) 67 gen.AddTx(ctx, tx1) 68 gen.AddTx(ctx, tx2) 69 case 2: 70 // Block 3 is empty but was mined by addr3. 71 gen.SetCoinbase(addr3) 72 gen.SetExtra([]byte("yeehaw")) 73 case 3: 74 // Block 4 includes modified extra data. 75 b2 := gen.PrevBlock(1).Header() 76 b2.Extra = []byte("foo") 77 b3 := gen.PrevBlock(2).Header() 78 b3.Extra = []byte("foo") 79 } 80 }) 81 82 // Import the chain. This runs all block validation rules. 83 blockchain, _ := NewBlockChain(ctx, db, nil, gspec.Config, engine, vm.Config{}) 84 defer blockchain.Stop() 85 86 if i, err := blockchain.InsertChain(ctx, chain); err != nil { 87 fmt.Printf("insert error (block %d): %v\n", chain[i].NumberU64(), err) 88 return 89 } 90 91 state, _ := blockchain.State() 92 fmt.Printf("last block: #%d\n", blockchain.CurrentBlock().Number()) 93 fmt.Println("balance of addr1:", state.GetBalance(addr1)) 94 fmt.Println("balance of addr2:", state.GetBalance(addr2)) 95 fmt.Println("balance of addr3:", state.GetBalance(addr3)) 96 // Output: 97 // last block: #5 98 // balance of addr1: 989000 99 // balance of addr2: 10000 100 // balance of addr3: 7000000000000001000 101 }