finally merged *the missing*

This commit is contained in:
obscuren 2015-03-23 12:12:49 +01:00
parent 0846e15667
commit 0be6d34048
3 changed files with 52 additions and 15 deletions

View File

@ -66,3 +66,8 @@ func (bc *BlockCache) Get(hash common.Hash) *types.Block {
return nil return nil
} }
func (bc *BlockCache) Has(hash common.Hash) bool {
_, ok := bc.blocks[hash]
return ok
}

View File

@ -38,6 +38,10 @@ type PendingBlockEvent struct {
Logs state.Logs Logs state.Logs
} }
type ChainUncleEvent struct {
Block *types.Block
}
type ChainHeadEvent struct{ Block *types.Block } type ChainHeadEvent struct{ Block *types.Block }
// Mining operation events // Mining operation events

View File

@ -24,7 +24,7 @@ type environment struct {
state *state.StateDB state *state.StateDB
coinbase *state.StateObject coinbase *state.StateObject
block *types.Block block *types.Block
ancestors *set.Set family *set.Set
uncles *set.Set uncles *set.Set
} }
@ -34,13 +34,10 @@ func env(block *types.Block, eth core.Backend) *environment {
totalUsedGas: new(big.Int), totalUsedGas: new(big.Int),
state: state, state: state,
block: block, block: block,
ancestors: set.New(), family: set.New(),
uncles: set.New(), uncles: set.New(),
coinbase: state.GetOrNewStateObject(block.Coinbase()), coinbase: state.GetOrNewStateObject(block.Coinbase()),
} }
for _, ancestor := range eth.ChainManager().GetAncestors(block, 7) {
env.ancestors.Add(ancestor.Hash())
}
return env return env
} }
@ -75,6 +72,9 @@ type worker struct {
current *environment current *environment
uncleMu sync.Mutex
possibleUncles map[common.Hash]*types.Block
mining bool mining bool
} }
@ -85,6 +85,7 @@ func newWorker(coinbase common.Address, eth core.Backend) *worker {
recv: make(chan Work), recv: make(chan Work),
chain: eth.ChainManager(), chain: eth.ChainManager(),
proc: eth.BlockProcessor(), proc: eth.BlockProcessor(),
possibleUncles: make(map[common.Hash]*types.Block),
coinbase: coinbase, coinbase: coinbase,
} }
} }
@ -115,7 +116,7 @@ func (self *worker) register(agent Agent) {
} }
func (self *worker) update() { func (self *worker) update() {
events := self.mux.Subscribe(core.ChainHeadEvent{}, core.NewMinedBlockEvent{}) events := self.mux.Subscribe(core.ChainHeadEvent{}, core.NewMinedBlockEvent{}, core.ChainSideEvent{})
timer := time.NewTicker(2 * time.Second) timer := time.NewTicker(2 * time.Second)
@ -130,6 +131,10 @@ out:
} }
case core.NewMinedBlockEvent: case core.NewMinedBlockEvent:
self.commitNewWork() self.commitNewWork()
case core.ChainSplitEvent:
self.uncleMu.Lock()
self.possibleUncles[ev.Block.Hash()] = ev.Block
self.uncleMu.Unlock()
} }
case <-self.quit: case <-self.quit:
// stop all agents // stop all agents
@ -145,6 +150,9 @@ out:
events.Unsubscribe() events.Unsubscribe()
} }
func (self *worker) addUncle(uncle *types.Block) {
}
func (self *worker) wait() { func (self *worker) wait() {
for { for {
for work := range self.recv { for work := range self.recv {
@ -191,6 +199,10 @@ func (self *worker) commitNewWork() {
block := self.chain.NewBlock(self.coinbase) block := self.chain.NewBlock(self.coinbase)
self.current = env(block, self.eth) self.current = env(block, self.eth)
for _, ancestor := range self.chain.GetAncestors(block, 7) {
self.current.family.Add(ancestor.Hash())
}
parent := self.chain.GetBlock(self.current.block.ParentHash()) parent := self.chain.GetBlock(self.current.block.ParentHash())
self.current.coinbase.SetGasPool(core.CalcGasLimit(parent, self.current.block)) self.current.coinbase.SetGasPool(core.CalcGasLimit(parent, self.current.block))
@ -221,6 +233,22 @@ gasLimit:
} }
self.eth.TxPool().RemoveSet(remove) self.eth.TxPool().RemoveSet(remove)
ucount := 0
for hash, uncle := range self.possibleUncles {
if ucount == 2 {
break
}
if err := self.commitUncle(uncle.Header()); err != nil {
minerlogger.Infof("Bad uncle found and will be removed (%x)\n", hash[:4])
minerlogger.Debugln(uncle)
} else {
minerlogger.Infof("Commiting %x as uncle\n", hash[:4])
ucount++
}
}
minerlogger.Infoln("Included %d uncle(s)")
self.current.state.AddBalance(self.coinbase, core.BlockReward) self.current.state.AddBalance(self.coinbase, core.BlockReward)
self.current.state.Update(common.Big0) self.current.state.Update(common.Big0)
@ -240,12 +268,12 @@ func (self *worker) commitUncle(uncle *types.Header) error {
} }
self.current.uncles.Add(uncle.Hash()) self.current.uncles.Add(uncle.Hash())
if !self.current.ancestors.Has(uncle.ParentHash) { if !self.current.family.Has(uncle.ParentHash) {
return core.UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4])) return core.UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4]))
} }
if !self.pow.Verify(types.NewBlockWithHeader(uncle)) { if self.current.family.Has(uncle.Hash()) {
return core.ValidationError("Uncle's nonce is invalid (= %x)", uncle.Nonce) return core.UncleError(fmt.Sprintf("Uncle already in family (%x)", uncle.Hash()))
} }
uncleAccount := self.current.state.GetAccount(uncle.Coinbase) uncleAccount := self.current.state.GetAccount(uncle.Coinbase)