chain_makers_test.go 3.8 KB

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