chain_makers_test.go 2.8 KB

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