2015-02-27 23:05:03 +02:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-03 18:55:23 +02:00
|
|
|
"math/big"
|
|
|
|
|
2015-03-16 12:27:38 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-19 17:19:54 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-02-27 23:05:03 +02:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/pow"
|
|
|
|
"github.com/ethereum/go-ethereum/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
// So we can generate blocks easily
|
|
|
|
type FakePow struct{}
|
|
|
|
|
2015-03-03 22:04:31 +02:00
|
|
|
func (f FakePow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte, []byte) {
|
|
|
|
return 0, nil, nil
|
2015-02-28 21:58:37 +02:00
|
|
|
}
|
|
|
|
func (f FakePow) Verify(block pow.Block) bool { return true }
|
|
|
|
func (f FakePow) GetHashrate() int64 { return 0 }
|
|
|
|
func (f FakePow) Turbo(bool) {}
|
|
|
|
|
|
|
|
// So we can deterministically seed different blockchains
|
|
|
|
var (
|
|
|
|
CanonicalSeed = 1
|
|
|
|
ForkSeed = 2
|
|
|
|
)
|
2015-02-27 23:05:03 +02:00
|
|
|
|
2015-02-28 21:58:37 +02:00
|
|
|
// Utility functions for making chains on the fly
|
|
|
|
// Exposed for sake of testing from other packages (eg. go-ethash)
|
2015-02-27 23:05:03 +02:00
|
|
|
func NewBlockFromParent(addr []byte, parent *types.Block) *types.Block {
|
|
|
|
return newBlockFromParent(addr, parent)
|
|
|
|
}
|
|
|
|
|
2015-03-16 12:27:38 +02:00
|
|
|
func MakeBlock(bman *BlockProcessor, parent *types.Block, i int, db common.Database, seed int) *types.Block {
|
2015-02-28 21:58:37 +02:00
|
|
|
return makeBlock(bman, parent, i, db, seed)
|
2015-02-27 23:05:03 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 12:27:38 +02:00
|
|
|
func MakeChain(bman *BlockProcessor, parent *types.Block, max int, db common.Database, seed int) types.Blocks {
|
2015-02-28 21:58:37 +02:00
|
|
|
return makeChain(bman, parent, max, db, seed)
|
2015-02-27 23:05:03 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 12:27:38 +02:00
|
|
|
func NewChainMan(block *types.Block, eventMux *event.TypeMux, db common.Database) *ChainManager {
|
2015-02-27 23:05:03 +02:00
|
|
|
return newChainManager(block, eventMux, db)
|
|
|
|
}
|
|
|
|
|
2015-03-16 12:27:38 +02:00
|
|
|
func NewBlockProc(db common.Database, txpool *TxPool, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
|
2015-02-27 23:05:03 +02:00
|
|
|
return newBlockProcessor(db, txpool, cman, eventMux)
|
|
|
|
}
|
|
|
|
|
2015-03-16 12:27:38 +02:00
|
|
|
func NewCanonical(n int, db common.Database) (*BlockProcessor, error) {
|
2015-02-27 23:05:03 +02:00
|
|
|
return newCanonical(n, db)
|
|
|
|
}
|
|
|
|
|
2015-02-28 21:58:37 +02:00
|
|
|
// block time is fixed at 10 seconds
|
2015-02-27 23:05:03 +02:00
|
|
|
func newBlockFromParent(addr []byte, parent *types.Block) *types.Block {
|
2015-03-16 12:27:38 +02:00
|
|
|
block := types.NewBlock(parent.Hash(), addr, parent.Root(), common.BigPow(2, 32), 0, "")
|
2015-02-27 23:05:03 +02:00
|
|
|
block.SetUncles(nil)
|
|
|
|
block.SetTransactions(nil)
|
|
|
|
block.SetReceipts(nil)
|
|
|
|
|
|
|
|
header := block.Header()
|
2015-03-04 11:49:56 +02:00
|
|
|
header.Difficulty = CalcDifficulty(block.Header(), parent.Header())
|
2015-03-16 12:27:38 +02:00
|
|
|
header.Number = new(big.Int).Add(parent.Header().Number, common.Big1)
|
2015-02-28 21:58:37 +02:00
|
|
|
header.Time = parent.Header().Time + 10
|
2015-02-27 23:05:03 +02:00
|
|
|
header.GasLimit = CalcGasLimit(parent, block)
|
|
|
|
|
|
|
|
block.Td = parent.Td
|
|
|
|
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually make a block by simulating what miner would do
|
2015-02-28 21:58:37 +02:00
|
|
|
// we seed chains by the first byte of the coinbase
|
2015-03-16 12:27:38 +02:00
|
|
|
func makeBlock(bman *BlockProcessor, parent *types.Block, i int, db common.Database, seed int) *types.Block {
|
|
|
|
addr := common.LeftPadBytes([]byte{byte(i)}, 20)
|
2015-02-28 21:58:37 +02:00
|
|
|
addr[0] = byte(seed)
|
2015-02-27 23:05:03 +02:00
|
|
|
block := newBlockFromParent(addr, parent)
|
|
|
|
state := state.New(block.Root(), db)
|
|
|
|
cbase := state.GetOrNewStateObject(addr)
|
|
|
|
cbase.SetGasPool(CalcGasLimit(parent, block))
|
|
|
|
cbase.AddBalance(BlockReward)
|
2015-03-16 12:27:38 +02:00
|
|
|
state.Update(common.Big0)
|
2015-02-27 23:05:03 +02:00
|
|
|
block.SetRoot(state.Root())
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a chain with real blocks
|
|
|
|
// Runs ProcessWithParent to get proper state roots
|
2015-03-16 12:27:38 +02:00
|
|
|
func makeChain(bman *BlockProcessor, parent *types.Block, max int, db common.Database, seed int) types.Blocks {
|
2015-02-27 23:05:03 +02:00
|
|
|
bman.bc.currentBlock = parent
|
|
|
|
blocks := make(types.Blocks, max)
|
|
|
|
for i := 0; i < max; i++ {
|
2015-02-28 21:58:37 +02:00
|
|
|
block := makeBlock(bman, parent, i, db, seed)
|
2015-03-19 17:19:54 +02:00
|
|
|
td, _, err := bman.processWithParent(block, parent)
|
2015-02-27 23:05:03 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("process with parent failed", err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
block.Td = td
|
|
|
|
blocks[i] = block
|
|
|
|
parent = block
|
|
|
|
}
|
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new chain manager starting from given block
|
|
|
|
// Effectively a fork factory
|
2015-03-16 12:27:38 +02:00
|
|
|
func newChainManager(block *types.Block, eventMux *event.TypeMux, db common.Database) *ChainManager {
|
2015-03-06 19:26:16 +02:00
|
|
|
bc := &ChainManager{blockDb: db, stateDb: db, genesisBlock: GenesisBlock(db), eventMux: eventMux}
|
2015-02-27 23:05:03 +02:00
|
|
|
if block == nil {
|
|
|
|
bc.Reset()
|
|
|
|
} else {
|
|
|
|
bc.currentBlock = block
|
|
|
|
bc.td = block.Td
|
|
|
|
}
|
|
|
|
return bc
|
|
|
|
}
|
|
|
|
|
|
|
|
// block processor with fake pow
|
2015-03-16 12:27:38 +02:00
|
|
|
func newBlockProcessor(db common.Database, txpool *TxPool, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
|
2015-03-12 15:50:35 +02:00
|
|
|
bman := NewBlockProcessor(db, db, FakePow{}, txpool, newChainManager(nil, eventMux, db), eventMux)
|
2015-02-27 23:05:03 +02:00
|
|
|
return bman
|
|
|
|
}
|
|
|
|
|
2015-02-28 21:58:37 +02:00
|
|
|
// Make a new, deterministic canonical chain by running InsertChain
|
2015-02-27 23:05:03 +02:00
|
|
|
// on result of makeChain
|
2015-03-16 12:27:38 +02:00
|
|
|
func newCanonical(n int, db common.Database) (*BlockProcessor, error) {
|
2015-02-27 23:05:03 +02:00
|
|
|
eventMux := &event.TypeMux{}
|
|
|
|
txpool := NewTxPool(eventMux)
|
|
|
|
|
|
|
|
bman := newBlockProcessor(db, txpool, newChainManager(nil, eventMux, db), eventMux)
|
|
|
|
bman.bc.SetProcessor(bman)
|
|
|
|
parent := bman.bc.CurrentBlock()
|
|
|
|
if n == 0 {
|
|
|
|
return bman, nil
|
|
|
|
}
|
2015-02-28 21:58:37 +02:00
|
|
|
lchain := makeChain(bman, parent, n, db, CanonicalSeed)
|
|
|
|
err := bman.bc.InsertChain(lchain)
|
|
|
|
return bman, err
|
2015-02-27 23:05:03 +02:00
|
|
|
}
|