github.com/klaytn/klaytn@v1.12.1/blockchain/chain_makers_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from core/chain_makers_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package blockchain 22 23 // TODO-Klaytn-FailedTest Fails because this test assumes Homestead fee. 24 /* 25 import ( 26 "fmt" 27 "math/big" 28 29 "github.com/klaytn/klaytn/consensus/gxhash" 30 "github.com/klaytn/klaytn/blockchain/types" 31 "github.com/klaytn/klaytn/blockchain/vm" 32 "github.com/klaytn/klaytn/crypto" 33 "github.com/klaytn/klaytn/storage/database" 34 "github.com/klaytn/klaytn/params" 35 ) 36 37 func ExampleGenerateChain() { 38 var ( 39 key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 40 key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") 41 key3, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") 42 addr1 = crypto.PubkeyToAddress(key1.PublicKey) 43 addr2 = crypto.PubkeyToAddress(key2.PublicKey) 44 addr3 = crypto.PubkeyToAddress(key3.PublicKey) 45 db = database.NewMemoryDBManager() 46 ) 47 48 // Ensure that key1 has some funds in the genesis block. 49 gspec := &Genesis{ 50 Config: ¶ms.ChainConfig{}, 51 Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(1000000)}}, 52 } 53 genesis := gspec.MustCommit(db) 54 55 // This call generates a chain of 5 blocks. The function runs for 56 // each block and adds different features to gen based on the 57 // block index. 58 signer := types.HomesteadSigner{} 59 chain, _ := GenerateChain(gspec.Config, genesis, gxhash.NewFaker(), db, 5, func(i int, gen *BlockGen) { 60 switch i { 61 case 0: 62 // In block 1, addr1 sends addr2 some KLAY. 63 tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), params.TxGas, nil, nil), signer, key1) 64 gen.AddTx(tx) 65 case 1: 66 // In block 2, addr1 sends some more KLAY to addr2. 67 // addr2 passes it on to addr3. 68 tx1, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(1000), params.TxGas, nil, nil), signer, key1) 69 tx2, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key2) 70 gen.AddTx(tx1) 71 gen.AddTx(tx2) 72 case 2: 73 // Block 3 is empty but was mined by addr3. 74 gen.SetCoinbase(addr3) 75 gen.SetExtra([]byte("yeehaw")) 76 case 3: 77 // Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data). 78 //b2 := gen.PrevBlock(1).Header() 79 //b2.Extra = []byte("foo") 80 //gen.AddUncle(b2) 81 //b3 := gen.PrevBlock(2).Header() 82 //b3.Extra = []byte("foo") 83 //gen.AddUncle(b3) 84 } 85 }) 86 87 // Import the chain. This runs all block validation rules. 88 blockchain, _ := NewBlockChain(db, nil, gspec.Config, gxhash.NewFaker(), vm.Config{}) 89 defer blockchain.Stop() 90 91 if i, err := blockchain.InsertChain(chain); err != nil { 92 fmt.Printf("insert error (block %d): %v\n", chain[i].NumberU64(), err) 93 return 94 } 95 96 state, _ := blockchain.State() 97 fmt.Printf("last block: #%d\n", blockchain.CurrentBlock().Number()) 98 fmt.Println("balance of addr1:", state.GetBalance(addr1)) 99 fmt.Println("balance of addr2:", state.GetBalance(addr2)) 100 fmt.Println("balance of addr3:", state.GetBalance(addr3)) 101 // Output: 102 // last block: #5 103 // balance of addr1: 989000 104 // balance of addr2: 10000 105 // balance of addr3: 19687500000000001000 106 } 107 */