Skip to content

Commit 84a4f76

Browse files
committed
uncle validation
1 parent d4d505c commit 84a4f76

4 files changed

Lines changed: 46 additions & 30 deletions

File tree

‎core/block_processor.go‎

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,15 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
176176
state := state.New(parent.Root(), sm.db)
177177

178178
// Block validation
179-
if err = sm.ValidateBlock(block, parent); err != nil {
179+
if err = sm.ValidateHeader(block.Header(), parent.Header()); err != nil {
180180
return
181181
}
182182

183+
// There can be at most two uncles
184+
if len(block.Uncles()) > 2 {
185+
return nil, ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles()))
186+
}
187+
183188
receipts, err := sm.TransitionState(state, parent, block, false)
184189
if err != nil {
185190
return
@@ -238,46 +243,41 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
238243
// Validates the current block. Returns an error if the block was invalid,
239244
// an uncle or anything that isn't on the current block chain.
240245
// Validation validates easy over difficult (dagger takes longer time = difficult)
241-
func (sm *BlockProcessor) ValidateBlock(block, parent *types.Block) error {
242-
if len(block.Header().Extra) > 1024 {
243-
return fmt.Errorf("Block extra data too long (%d)", len(block.Header().Extra))
246+
func (sm *BlockProcessor) ValidateHeader(block, parent *types.Header) error {
247+
if len(block.Extra) > 1024 {
248+
return fmt.Errorf("Block extra data too long (%d)", len(block.Extra))
244249
}
245250

246251
expd := CalcDifficulty(block, parent)
247-
if expd.Cmp(block.Header().Difficulty) != 0 {
248-
return fmt.Errorf("Difficulty check failed for block %v, %v", block.Header().Difficulty, expd)
252+
if expd.Cmp(block.Difficulty) != 0 {
253+
return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd)
249254
}
250255

251256
//expl := CalcGasLimit(parent, block)
252257
//if expl.Cmp(block.Header().GasLimit) != 0 {
253258

254259
// block.gasLimit - parent.gasLimit <= parent.gasLimit / 1024
255-
a := new(big.Int).Sub(block.Header().GasLimit, parent.Header().GasLimit)
256-
b := new(big.Int).Div(parent.Header().GasLimit, big.NewInt(1024))
260+
a := new(big.Int).Sub(block.GasLimit, parent.GasLimit)
261+
b := new(big.Int).Div(parent.GasLimit, big.NewInt(1024))
257262
if a.Cmp(b) > 0 {
258-
return fmt.Errorf("GasLimit check failed for block %v (%v > %v)", block.Header().GasLimit, a, b)
259-
}
260-
261-
// There can be at most one uncle
262-
if len(block.Uncles()) > 1 {
263-
return ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles()))
263+
return fmt.Errorf("GasLimit check failed for block %v (%v > %v)", block.GasLimit, a, b)
264264
}
265265

266-
if block.Time() < parent.Time() {
267-
return ValidationError("Block timestamp not after prev block (%v - %v)", block.Header().Time, parent.Header().Time)
266+
if block.Time < parent.Time {
267+
return ValidationError("Block timestamp not after prev block (%v - %v)", block.Time, parent.Time)
268268
}
269269

270-
if block.Time() > time.Now().Unix() {
270+
if int64(block.Time) > time.Now().Unix() {
271271
return BlockFutureErr
272272
}
273273

274-
if new(big.Int).Sub(block.Number(), parent.Number()).Cmp(big.NewInt(1)) != 0 {
274+
if new(big.Int).Sub(block.Number, parent.Number).Cmp(big.NewInt(1)) != 0 {
275275
return BlockNumberErr
276276
}
277277

278278
// Verify the nonce of the block. Return an error if it's not valid
279-
if !sm.Pow.Verify(block) {
280-
return ValidationError("Block's nonce is invalid (= %x)", block.Header().Nonce)
279+
if !sm.Pow.Verify(types.NewBlockWithHeader(block)) {
280+
return ValidationError("Block's nonce is invalid (= %x)", block.Nonce)
281281
}
282282

283283
return nil
@@ -287,23 +287,39 @@ func (sm *BlockProcessor) AccumulateRewards(statedb *state.StateDB, block, paren
287287
reward := new(big.Int).Set(BlockReward)
288288

289289
ancestors := set.New()
290+
uncles := set.New()
291+
ancestorHeaders := make(map[string]*types.Header)
290292
for _, ancestor := range sm.bc.GetAncestors(block, 7) {
291-
ancestors.Add(string(ancestor.Hash()))
293+
hash := string(ancestor.Hash())
294+
ancestorHeaders[hash] = ancestor.Header()
295+
ancestors.Add(hash)
296+
// Include ancestors uncles in the uncle set. Uncles must be unique.
297+
for _, uncle := range ancestor.Uncles() {
298+
uncles.Add(string(uncle.Hash()))
299+
}
292300
}
293301

294-
uncles := set.New()
295302
uncles.Add(string(block.Hash()))
296303
for _, uncle := range block.Uncles() {
297304
if uncles.Has(string(uncle.Hash())) {
298305
// Error not unique
299306
return UncleError("Uncle not unique")
300307
}
308+
301309
uncles.Add(string(uncle.Hash()))
302310

311+
if ancestors.Has(string(uncle.Hash())) {
312+
return UncleError("Uncle is ancestor")
313+
}
314+
303315
if !ancestors.Has(string(uncle.ParentHash)) {
304316
return UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4]))
305317
}
306318

319+
if err := sm.ValidateHeader(uncle, ancestorHeaders[string(uncle.ParentHash)]); err != nil {
320+
return ValidationError(fmt.Sprintf("%v", err))
321+
}
322+
307323
if !sm.Pow.Verify(types.NewBlockWithHeader(uncle)) {
308324
return ValidationError("Uncle's nonce is invalid (= %x)", uncle.Nonce)
309325
}

‎core/chain_makers.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func newBlockFromParent(addr []byte, parent *types.Block) *types.Block {
6161
block.SetReceipts(nil)
6262

6363
header := block.Header()
64-
header.Difficulty = CalcDifficulty(block, parent)
64+
header.Difficulty = CalcDifficulty(block.Header(), parent.Header())
6565
header.Number = new(big.Int).Add(parent.Header().Number, ethutil.Big1)
6666
header.Time = parent.Header().Time + 10
6767
header.GasLimit = CalcGasLimit(parent, block)

‎core/chain_manager.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ type StateQuery interface {
2828
GetAccount(addr []byte) *state.StateObject
2929
}
3030

31-
func CalcDifficulty(block, parent *types.Block) *big.Int {
31+
func CalcDifficulty(block, parent *types.Header) *big.Int {
3232
diff := new(big.Int)
3333

3434
//adjust := new(big.Int).Rsh(parent.Difficulty(), 10)
3535
//if block.Time() >= parent.Time()+8 {
36-
adjust := new(big.Int).Div(parent.Difficulty(), big.NewInt(2048))
37-
if (block.Time() - parent.Time()) < 8 {
38-
diff.Add(parent.Difficulty(), adjust)
36+
adjust := new(big.Int).Div(parent.Difficulty, big.NewInt(2048))
37+
if (block.Time - parent.Time) < 8 {
38+
diff.Add(parent.Difficulty, adjust)
3939
} else {
40-
diff.Sub(parent.Difficulty(), adjust)
40+
diff.Sub(parent.Difficulty, adjust)
4141
}
4242

4343
return diff
@@ -206,7 +206,7 @@ func (bc *ChainManager) NewBlock(coinbase []byte) *types.Block {
206206
parent := bc.currentBlock
207207
if parent != nil {
208208
header := block.Header()
209-
header.Difficulty = CalcDifficulty(block, parent)
209+
header.Difficulty = CalcDifficulty(block.Header(), parent.Header())
210210
header.Number = new(big.Int).Add(parent.Header().Number, ethutil.Big1)
211211
header.GasLimit = CalcGasLimit(parent, block)
212212

‎core/filter.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (self *Filter) FilterLogs(logs state.Logs) state.Logs {
147147
// Filter the logs for interesting stuff
148148
Logs:
149149
for _, log := range logs {
150-
if !includes(self.address, log.Address()) {
150+
if len(self.address) > 0 && !includes(self.address, log.Address()) {
151151
continue
152152
}
153153

0 commit comments

Comments
 (0)