Skip to content

Commit dc3a937

Browse files
committed
logging for possible uncles
1 parent 524f819 commit dc3a937

4 files changed

Lines changed: 44 additions & 25 deletions

File tree

‎core/block_processor.go‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,15 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
166166
// Create a new state based on the parent's root (e.g., create copy)
167167
state := state.New(parent.Root(), sm.db)
168168

169+
// track (possible) uncle block
170+
var uncle bool
169171
// Block validation
170172
if err = sm.ValidateHeader(block.Header(), parent.Header()); err != nil {
171-
return
173+
if err != BlockEqualTSErr {
174+
return
175+
}
176+
err = nil
177+
uncle = true
172178
}
173179

174180
// There can be at most two uncles
@@ -223,14 +229,22 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
223229
td = CalculateTD(block, parent)
224230
// Sync the current block's state to the database
225231
state.Sync()
226-
// Remove transactions from the pool
227-
sm.txpool.RemoveSet(block.Transactions())
232+
233+
if !uncle {
234+
// Remove transactions from the pool
235+
sm.txpool.RemoveSet(block.Transactions())
236+
}
228237

229238
for _, tx := range block.Transactions() {
230239
putTx(sm.extraDb, tx)
231240
}
232241

233-
chainlogger.Infof("processed block #%d (%x...)\n", header.Number, block.Hash().Bytes()[0:4])
242+
if uncle {
243+
chainlogger.Infof("found possible uncle block #%d (%x...)\n", header.Number, block.Hash().Bytes()[0:4])
244+
return td, nil, BlockEqualTSErr
245+
} else {
246+
chainlogger.Infof("processed block #%d (%x...)\n", header.Number, block.Hash().Bytes()[0:4])
247+
}
234248

235249
return td, state.Logs(), nil
236250
}
@@ -255,10 +269,6 @@ func (sm *BlockProcessor) ValidateHeader(block, parent *types.Header) error {
255269
return fmt.Errorf("GasLimit check failed for block %v (%v > %v)", block.GasLimit, a, b)
256270
}
257271

258-
if block.Time <= parent.Time {
259-
return ValidationError("Block timestamp equal or less than previous block (%v - %v)", block.Time, parent.Time)
260-
}
261-
262272
if int64(block.Time) > time.Now().Unix() {
263273
return BlockFutureErr
264274
}
@@ -272,6 +282,10 @@ func (sm *BlockProcessor) ValidateHeader(block, parent *types.Header) error {
272282
return ValidationError("Block's nonce is invalid (= %x)", block.Nonce)
273283
}
274284

285+
if block.Time <= parent.Time {
286+
return BlockEqualTSErr //ValidationError("Block timestamp equal or less than previous block (%v - %v)", block.Time, parent.Time)
287+
}
288+
275289
return nil
276290
}
277291

@@ -307,14 +321,10 @@ func (sm *BlockProcessor) AccumulateRewards(statedb *state.StateDB, block, paren
307321
return UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4]))
308322
}
309323

310-
if err := sm.ValidateHeader(uncle, ancestorHeaders[uncle.ParentHash]); err != nil {
324+
if err := sm.ValidateHeader(uncle, ancestorHeaders[uncle.ParentHash]); err != nil && err != BlockEqualTSErr {
311325
return ValidationError(fmt.Sprintf("%v", err))
312326
}
313327

314-
if !sm.Pow.Verify(types.NewBlockWithHeader(uncle)) {
315-
return ValidationError("Uncle's nonce is invalid (= %x)", uncle.Nonce)
316-
}
317-
318328
r := new(big.Int)
319329
r.Mul(BlockReward, big.NewInt(15)).Div(r, big.NewInt(16))
320330

‎core/chain_manager.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,11 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
447447
continue
448448
}
449449

450+
if err == BlockEqualTSErr {
451+
queue[i] = ChainSideEvent{block, logs}
452+
continue
453+
}
454+
450455
h := block.Header()
451456
chainlogger.Infof("INVALID block #%v (%x)\n", h.Number, h.Hash().Bytes()[:4])
452457
chainlogger.Infoln(err)

‎core/error.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
)
1010

1111
var (
12-
BlockNumberErr = errors.New("block number invalid")
13-
BlockFutureErr = errors.New("block time is in the future")
12+
BlockNumberErr = errors.New("block number invalid")
13+
BlockFutureErr = errors.New("block time is in the future")
14+
BlockEqualTSErr = errors.New("block time stamp equal to previous")
1415
)
1516

1617
// Parent error. In case a parent is unknown this error will be thrown

‎miner/worker.go‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ out:
131131
}
132132
case core.NewMinedBlockEvent:
133133
self.commitNewWork()
134-
case core.ChainSplitEvent:
134+
case core.ChainSideEvent:
135135
self.uncleMu.Lock()
136136
self.possibleUncles[ev.Block.Hash()] = ev.Block
137137
self.uncleMu.Unlock()
@@ -170,6 +170,10 @@ func (self *worker) wait() {
170170
})
171171

172172
if err := self.chain.InsertChain(types.Blocks{self.current.block}); err == nil {
173+
for _, uncle := range self.current.block.Uncles() {
174+
delete(self.possibleUncles, uncle.Hash())
175+
}
176+
173177
self.mux.Post(core.NewMinedBlockEvent{self.current.block})
174178
} else {
175179
self.commitNewWork()
@@ -233,21 +237,22 @@ gasLimit:
233237
}
234238
self.eth.TxPool().RemoveSet(remove)
235239

236-
ucount := 0
240+
var uncles []*types.Header
237241
for hash, uncle := range self.possibleUncles {
238-
if ucount == 2 {
242+
if len(uncles) == 2 {
239243
break
240244
}
241245

242246
if err := self.commitUncle(uncle.Header()); err != nil {
243247
minerlogger.Infof("Bad uncle found and will be removed (%x)\n", hash[:4])
244248
minerlogger.Debugln(uncle)
245249
} else {
246-
minerlogger.Infof("Commiting %x as uncle\n", hash[:4])
247-
ucount++
250+
minerlogger.Infof("commiting %x as uncle\n", hash[:4])
251+
uncles = append(uncles, uncle.Header())
248252
}
249253
}
250-
minerlogger.Infoln("Included %d uncle(s)")
254+
minerlogger.Infoln("Included", len(uncles), "uncle(s)")
255+
self.current.block.SetUncles(uncles)
251256

252257
self.current.state.AddBalance(self.coinbase, core.BlockReward)
253258

@@ -276,10 +281,8 @@ func (self *worker) commitUncle(uncle *types.Header) error {
276281
return core.UncleError(fmt.Sprintf("Uncle already in family (%x)", uncle.Hash()))
277282
}
278283

279-
uncleAccount := self.current.state.GetAccount(uncle.Coinbase)
280-
uncleAccount.AddBalance(uncleReward)
281-
282-
self.current.coinbase.AddBalance(uncleReward)
284+
self.current.state.AddBalance(uncle.Coinbase, uncleReward)
285+
self.current.state.AddBalance(self.coinbase, inclusionReward)
283286

284287
return nil
285288
}

0 commit comments

Comments
 (0)