@@ -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 }
0 commit comments