chain_makers_test.go 3.5 KB

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