github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/core/chain_makers_test.go (about) 1 package core 2 3 import ( 4 "fmt" 5 "math/big" 6 7 "github.com/neatlab/neatio/chain/core/rawdb" 8 "github.com/neatlab/neatio/chain/core/types" 9 "github.com/neatlab/neatio/chain/core/vm" 10 "github.com/neatlab/neatio/params" 11 "github.com/neatlab/neatio/utilities/crypto" 12 ) 13 14 func ExampleGenerateChain() { 15 var ( 16 key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 17 key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") 18 key3, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") 19 addr1 = crypto.PubkeyToAddress(key1.PublicKey) 20 addr2 = crypto.PubkeyToAddress(key2.PublicKey) 21 addr3 = crypto.PubkeyToAddress(key3.PublicKey) 22 db = rawdb.NewMemoryDatabase() 23 ) 24 25 gspec := &Genesis{ 26 Config: ¶ms.ChainConfig{HomesteadBlock: new(big.Int)}, 27 Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(1000000)}}, 28 } 29 genesis := gspec.MustCommit(db) 30 31 signer := types.HomesteadSigner{} 32 chain, _ := GenerateChain(gspec.Config, genesis, nil, db, 5, func(i int, gen *BlockGen) { 33 switch i { 34 case 0: 35 36 tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), params.TxGas, nil, nil), signer, key1) 37 gen.AddTx(tx) 38 case 1: 39 40 tx1, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(1000), params.TxGas, nil, nil), signer, key1) 41 tx2, _ := types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr3, big.NewInt(1000), params.TxGas, nil, nil), signer, key2) 42 gen.AddTx(tx1) 43 gen.AddTx(tx2) 44 case 2: 45 46 gen.SetCoinbase(addr3) 47 gen.SetExtra([]byte("yeehaw")) 48 case 3: 49 50 b2 := gen.PrevBlock(1).Header() 51 b2.Extra = []byte("foo") 52 gen.AddUncle(b2) 53 b3 := gen.PrevBlock(2).Header() 54 b3.Extra = []byte("foo") 55 gen.AddUncle(b3) 56 } 57 }) 58 59 blockchain, _ := NewBlockChain(db, nil, gspec.Config, nil, vm.Config{}, nil) 60 defer blockchain.Stop() 61 62 if i, err := blockchain.InsertChain(chain); err != nil { 63 fmt.Printf("insert error (block %d): %v\n", chain[i].NumberU64(), err) 64 return 65 } 66 67 state, _ := blockchain.State() 68 fmt.Printf("last block: #%d\n", blockchain.CurrentBlock().Number()) 69 fmt.Println("balance of addr1:", state.GetBalance(addr1)) 70 fmt.Println("balance of addr2:", state.GetBalance(addr2)) 71 fmt.Println("balance of addr3:", state.GetBalance(addr3)) 72 73 }