chain_makers.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package core
  2. import (
  3. "fmt"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/core/state"
  7. "github.com/ethereum/go-ethereum/core/types"
  8. "github.com/ethereum/go-ethereum/event"
  9. "github.com/ethereum/go-ethereum/pow"
  10. )
  11. // So we can generate blocks easily
  12. type FakePow struct{}
  13. func (f FakePow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte) {
  14. return 0, nil
  15. }
  16. func (f FakePow) Verify(block pow.Block) bool { return true }
  17. func (f FakePow) GetHashrate() int64 { return 0 }
  18. func (f FakePow) Turbo(bool) {}
  19. // So we can deterministically seed different blockchains
  20. var (
  21. CanonicalSeed = 1
  22. ForkSeed = 2
  23. )
  24. // Utility functions for making chains on the fly
  25. // Exposed for sake of testing from other packages (eg. go-ethash)
  26. func MakeBlock(bman *BlockProcessor, parent *types.Block, i int, db common.Database, seed int) *types.Block {
  27. return types.NewBlock(makeHeader(parent, i, db, seed), nil, nil, nil)
  28. }
  29. func MakeChain(bman *BlockProcessor, parent *types.Block, max int, db common.Database, seed int) types.Blocks {
  30. return makeChain(bman, parent, max, db, seed)
  31. }
  32. func NewChainMan(block *types.Block, eventMux *event.TypeMux, db common.Database) *ChainManager {
  33. return newChainManager(block, eventMux, db)
  34. }
  35. func NewBlockProc(db common.Database, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
  36. return newBlockProcessor(db, cman, eventMux)
  37. }
  38. func NewCanonical(n int, db common.Database) (*BlockProcessor, error) {
  39. return newCanonical(n, db)
  40. }
  41. // makeHeader creates the header for a new empty block, simulating
  42. // what miner would do. We seed chains by the first byte of the coinbase.
  43. func makeHeader(parent *types.Block, i int, db common.Database, seed int) *types.Header {
  44. var addr common.Address
  45. addr[0], addr[19] = byte(seed), byte(i) // 'random' coinbase
  46. time := parent.Time() + 10 // block time is fixed at 10 seconds
  47. // ensure that the block's coinbase has the block reward in the state.
  48. state := state.New(parent.Root(), db)
  49. cbase := state.GetOrNewStateObject(addr)
  50. cbase.SetGasLimit(CalcGasLimit(parent))
  51. cbase.AddBalance(BlockReward)
  52. state.Update()
  53. return &types.Header{
  54. Root: state.Root(),
  55. ParentHash: parent.Hash(),
  56. Coinbase: addr,
  57. Difficulty: CalcDifficulty(time, parent.Time(), parent.Difficulty()),
  58. Number: new(big.Int).Add(parent.Number(), common.Big1),
  59. Time: uint64(time),
  60. GasLimit: CalcGasLimit(parent),
  61. }
  62. }
  63. // makeChain creates a valid chain of empty blocks.
  64. func makeChain(bman *BlockProcessor, parent *types.Block, max int, db common.Database, seed int) types.Blocks {
  65. bman.bc.currentBlock = parent
  66. blocks := make(types.Blocks, max)
  67. for i := 0; i < max; i++ {
  68. block := types.NewBlock(makeHeader(parent, i, db, seed), nil, nil, nil)
  69. // Use ProcessWithParent to verify that we have produced a valid block.
  70. _, err := bman.processWithParent(block, parent)
  71. if err != nil {
  72. fmt.Println("process with parent failed", err)
  73. panic(err)
  74. }
  75. block.Td = CalcTD(block, parent)
  76. blocks[i] = block
  77. parent = block
  78. }
  79. return blocks
  80. }
  81. // Create a new chain manager starting from given block
  82. // Effectively a fork factory
  83. func newChainManager(block *types.Block, eventMux *event.TypeMux, db common.Database) *ChainManager {
  84. genesis := GenesisBlock(0, db)
  85. bc := &ChainManager{blockDb: db, stateDb: db, genesisBlock: genesis, eventMux: eventMux, pow: FakePow{}}
  86. bc.txState = state.ManageState(state.New(genesis.Root(), db))
  87. bc.futureBlocks = NewBlockCache(1000)
  88. if block == nil {
  89. bc.Reset()
  90. } else {
  91. bc.currentBlock = block
  92. bc.td = block.Td
  93. }
  94. return bc
  95. }
  96. // block processor with fake pow
  97. func newBlockProcessor(db common.Database, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
  98. chainMan := newChainManager(nil, eventMux, db)
  99. bman := NewBlockProcessor(db, db, FakePow{}, chainMan, eventMux)
  100. return bman
  101. }
  102. // Make a new, deterministic canonical chain by running InsertChain
  103. // on result of makeChain.
  104. func newCanonical(n int, db common.Database) (*BlockProcessor, error) {
  105. eventMux := &event.TypeMux{}
  106. bman := newBlockProcessor(db, newChainManager(nil, eventMux, db), eventMux)
  107. bman.bc.SetProcessor(bman)
  108. parent := bman.bc.CurrentBlock()
  109. if n == 0 {
  110. return bman, nil
  111. }
  112. lchain := makeChain(bman, parent, n, db, CanonicalSeed)
  113. _, err := bman.bc.InsertChain(lchain)
  114. return bman, err
  115. }