chain_makers.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package core
  2. import (
  3. "fmt"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/core/types"
  7. "github.com/ethereum/go-ethereum/event"
  8. "github.com/ethereum/go-ethereum/pow"
  9. "github.com/ethereum/go-ethereum/state"
  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, []byte) {
  14. return 0, nil, 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 NewBlockFromParent(addr common.Address, parent *types.Block) *types.Block {
  27. return newBlockFromParent(addr, parent)
  28. }
  29. func MakeBlock(bman *BlockProcessor, parent *types.Block, i int, db common.Database, seed int) *types.Block {
  30. return makeBlock(bman, parent, i, db, seed)
  31. }
  32. func MakeChain(bman *BlockProcessor, parent *types.Block, max int, db common.Database, seed int) types.Blocks {
  33. return makeChain(bman, parent, max, db, seed)
  34. }
  35. func NewChainMan(block *types.Block, eventMux *event.TypeMux, db common.Database) *ChainManager {
  36. return newChainManager(block, eventMux, db)
  37. }
  38. func NewBlockProc(db common.Database, txpool *TxPool, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
  39. return newBlockProcessor(db, txpool, cman, eventMux)
  40. }
  41. func NewCanonical(n int, db common.Database) (*BlockProcessor, error) {
  42. return newCanonical(n, db)
  43. }
  44. // block time is fixed at 10 seconds
  45. func newBlockFromParent(addr common.Address, parent *types.Block) *types.Block {
  46. block := types.NewBlock(parent.Hash(), addr, parent.Root(), common.BigPow(2, 32), 0, "")
  47. block.SetUncles(nil)
  48. block.SetTransactions(nil)
  49. block.SetReceipts(nil)
  50. header := block.Header()
  51. header.Difficulty = CalcDifficulty(block.Header(), parent.Header())
  52. header.Number = new(big.Int).Add(parent.Header().Number, common.Big1)
  53. header.Time = parent.Header().Time + 10
  54. header.GasLimit = CalcGasLimit(parent, block)
  55. block.Td = parent.Td
  56. return block
  57. }
  58. // Actually make a block by simulating what miner would do
  59. // we seed chains by the first byte of the coinbase
  60. func makeBlock(bman *BlockProcessor, parent *types.Block, i int, db common.Database, seed int) *types.Block {
  61. var addr common.Address
  62. addr[0], addr[19] = byte(seed), byte(i)
  63. block := newBlockFromParent(addr, parent)
  64. state := state.New(block.Root(), db)
  65. cbase := state.GetOrNewStateObject(addr)
  66. cbase.SetGasPool(CalcGasLimit(parent, block))
  67. cbase.AddBalance(BlockReward)
  68. state.Update(common.Big0)
  69. block.SetRoot(state.Root())
  70. return block
  71. }
  72. // Make a chain with real blocks
  73. // Runs ProcessWithParent to get proper state roots
  74. func makeChain(bman *BlockProcessor, parent *types.Block, max int, db common.Database, seed int) types.Blocks {
  75. bman.bc.currentBlock = parent
  76. blocks := make(types.Blocks, max)
  77. for i := 0; i < max; i++ {
  78. block := makeBlock(bman, parent, i, db, seed)
  79. td, err := bman.processWithParent(block, parent)
  80. if err != nil {
  81. fmt.Println("process with parent failed", err)
  82. panic(err)
  83. }
  84. block.Td = td
  85. blocks[i] = block
  86. parent = block
  87. }
  88. return blocks
  89. }
  90. // Create a new chain manager starting from given block
  91. // Effectively a fork factory
  92. func newChainManager(block *types.Block, eventMux *event.TypeMux, db common.Database) *ChainManager {
  93. bc := &ChainManager{blockDb: db, stateDb: db, genesisBlock: GenesisBlock(db), eventMux: eventMux}
  94. if block == nil {
  95. bc.Reset()
  96. } else {
  97. bc.currentBlock = block
  98. bc.td = block.Td
  99. }
  100. return bc
  101. }
  102. // block processor with fake pow
  103. func newBlockProcessor(db common.Database, txpool *TxPool, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
  104. bman := NewBlockProcessor(db, db, FakePow{}, txpool, newChainManager(nil, eventMux, db), eventMux)
  105. return bman
  106. }
  107. // Make a new, deterministic canonical chain by running InsertChain
  108. // on result of makeChain
  109. func newCanonical(n int, db common.Database) (*BlockProcessor, error) {
  110. eventMux := &event.TypeMux{}
  111. txpool := NewTxPool(eventMux)
  112. bman := newBlockProcessor(db, txpool, newChainManager(nil, eventMux, db), eventMux)
  113. bman.bc.SetProcessor(bman)
  114. parent := bman.bc.CurrentBlock()
  115. if n == 0 {
  116. return bman, nil
  117. }
  118. lchain := makeChain(bman, parent, n, db, CanonicalSeed)
  119. err := bman.bc.InsertChain(lchain)
  120. return bman, err
  121. }