Skip to content

Commit 0be6d34

Browse files
committed
finally merged *the missing*
1 parent 0846e15 commit 0be6d34

3 files changed

Lines changed: 52 additions & 15 deletions

File tree

‎core/block_cache.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ func (bc *BlockCache) Get(hash common.Hash) *types.Block {
6666

6767
return nil
6868
}
69+
70+
func (bc *BlockCache) Has(hash common.Hash) bool {
71+
_, ok := bc.blocks[hash]
72+
return ok
73+
}

‎core/events.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type PendingBlockEvent struct {
3838
Logs state.Logs
3939
}
4040

41+
type ChainUncleEvent struct {
42+
Block *types.Block
43+
}
44+
4145
type ChainHeadEvent struct{ Block *types.Block }
4246

4347
// Mining operation events

‎miner/worker.go‎

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type environment struct {
2424
state *state.StateDB
2525
coinbase *state.StateObject
2626
block *types.Block
27-
ancestors *set.Set
27+
family *set.Set
2828
uncles *set.Set
2929
}
3030

@@ -34,13 +34,10 @@ func env(block *types.Block, eth core.Backend) *environment {
3434
totalUsedGas: new(big.Int),
3535
state: state,
3636
block: block,
37-
ancestors: set.New(),
37+
family: set.New(),
3838
uncles: set.New(),
3939
coinbase: state.GetOrNewStateObject(block.Coinbase()),
4040
}
41-
for _, ancestor := range eth.ChainManager().GetAncestors(block, 7) {
42-
env.ancestors.Add(ancestor.Hash())
43-
}
4441

4542
return env
4643
}
@@ -75,17 +72,21 @@ type worker struct {
7572

7673
current *environment
7774

75+
uncleMu sync.Mutex
76+
possibleUncles map[common.Hash]*types.Block
77+
7878
mining bool
7979
}
8080

8181
func newWorker(coinbase common.Address, eth core.Backend) *worker {
8282
return &worker{
83-
eth: eth,
84-
mux: eth.EventMux(),
85-
recv: make(chan Work),
86-
chain: eth.ChainManager(),
87-
proc: eth.BlockProcessor(),
88-
coinbase: coinbase,
83+
eth: eth,
84+
mux: eth.EventMux(),
85+
recv: make(chan Work),
86+
chain: eth.ChainManager(),
87+
proc: eth.BlockProcessor(),
88+
possibleUncles: make(map[common.Hash]*types.Block),
89+
coinbase: coinbase,
8990
}
9091
}
9192

@@ -115,7 +116,7 @@ func (self *worker) register(agent Agent) {
115116
}
116117

117118
func (self *worker) update() {
118-
events := self.mux.Subscribe(core.ChainHeadEvent{}, core.NewMinedBlockEvent{})
119+
events := self.mux.Subscribe(core.ChainHeadEvent{}, core.NewMinedBlockEvent{}, core.ChainSideEvent{})
119120

120121
timer := time.NewTicker(2 * time.Second)
121122

@@ -130,6 +131,10 @@ out:
130131
}
131132
case core.NewMinedBlockEvent:
132133
self.commitNewWork()
134+
case core.ChainSplitEvent:
135+
self.uncleMu.Lock()
136+
self.possibleUncles[ev.Block.Hash()] = ev.Block
137+
self.uncleMu.Unlock()
133138
}
134139
case <-self.quit:
135140
// stop all agents
@@ -145,6 +150,9 @@ out:
145150
events.Unsubscribe()
146151
}
147152

153+
func (self *worker) addUncle(uncle *types.Block) {
154+
}
155+
148156
func (self *worker) wait() {
149157
for {
150158
for work := range self.recv {
@@ -191,6 +199,10 @@ func (self *worker) commitNewWork() {
191199
block := self.chain.NewBlock(self.coinbase)
192200

193201
self.current = env(block, self.eth)
202+
for _, ancestor := range self.chain.GetAncestors(block, 7) {
203+
self.current.family.Add(ancestor.Hash())
204+
}
205+
194206
parent := self.chain.GetBlock(self.current.block.ParentHash())
195207
self.current.coinbase.SetGasPool(core.CalcGasLimit(parent, self.current.block))
196208

@@ -221,6 +233,22 @@ gasLimit:
221233
}
222234
self.eth.TxPool().RemoveSet(remove)
223235

236+
ucount := 0
237+
for hash, uncle := range self.possibleUncles {
238+
if ucount == 2 {
239+
break
240+
}
241+
242+
if err := self.commitUncle(uncle.Header()); err != nil {
243+
minerlogger.Infof("Bad uncle found and will be removed (%x)\n", hash[:4])
244+
minerlogger.Debugln(uncle)
245+
} else {
246+
minerlogger.Infof("Commiting %x as uncle\n", hash[:4])
247+
ucount++
248+
}
249+
}
250+
minerlogger.Infoln("Included %d uncle(s)")
251+
224252
self.current.state.AddBalance(self.coinbase, core.BlockReward)
225253

226254
self.current.state.Update(common.Big0)
@@ -240,12 +268,12 @@ func (self *worker) commitUncle(uncle *types.Header) error {
240268
}
241269
self.current.uncles.Add(uncle.Hash())
242270

243-
if !self.current.ancestors.Has(uncle.ParentHash) {
271+
if !self.current.family.Has(uncle.ParentHash) {
244272
return core.UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4]))
245273
}
246274

247-
if !self.pow.Verify(types.NewBlockWithHeader(uncle)) {
248-
return core.ValidationError("Uncle's nonce is invalid (= %x)", uncle.Nonce)
275+
if self.current.family.Has(uncle.Hash()) {
276+
return core.UncleError(fmt.Sprintf("Uncle already in family (%x)", uncle.Hash()))
249277
}
250278

251279
uncleAccount := self.current.state.GetAccount(uncle.Coinbase)

0 commit comments

Comments
 (0)