Skip to content

Commit 58fbcaa

Browse files
committed
Merge pull request #1810 from karalabe/pure-header-verifications-2
core, eth, miner: use pure header validation
2 parents 985b5f2 + 821619e commit 58fbcaa

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

core/block_processor.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
214214
txs := block.Transactions()
215215

216216
// Block validation
217-
if err = ValidateHeader(sm.Pow, header, parent, false, false); err != nil {
217+
if err = ValidateHeader(sm.Pow, header, parent.Header(), false, false); err != nil {
218218
return
219219
}
220220

@@ -338,7 +338,7 @@ func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *ty
338338
return UncleError("uncle[%d](%x)'s parent is not ancestor (%x)", i, hash[:4], uncle.ParentHash[0:4])
339339
}
340340

341-
if err := ValidateHeader(sm.Pow, uncle, ancestors[uncle.ParentHash], true, true); err != nil {
341+
if err := ValidateHeader(sm.Pow, uncle, ancestors[uncle.ParentHash].Header(), true, true); err != nil {
342342
return ValidationError(fmt.Sprintf("uncle[%d](%x) header invalid: %v", i, hash[:4], err))
343343
}
344344
}
@@ -368,52 +368,50 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err erro
368368
}
369369

370370
// See YP section 4.3.4. "Block Header Validity"
371-
// Validates a block. Returns an error if the block is invalid.
372-
func ValidateHeader(pow pow.PoW, block *types.Header, parent *types.Block, checkPow, uncle bool) error {
373-
if big.NewInt(int64(len(block.Extra))).Cmp(params.MaximumExtraDataSize) == 1 {
374-
return fmt.Errorf("Block extra data too long (%d)", len(block.Extra))
371+
// Validates a header. Returns an error if the header is invalid.
372+
func ValidateHeader(pow pow.PoW, header *types.Header, parent *types.Header, checkPow, uncle bool) error {
373+
if big.NewInt(int64(len(header.Extra))).Cmp(params.MaximumExtraDataSize) == 1 {
374+
return fmt.Errorf("Header extra data too long (%d)", len(header.Extra))
375375
}
376376

377377
if uncle {
378-
if block.Time.Cmp(common.MaxBig) == 1 {
378+
if header.Time.Cmp(common.MaxBig) == 1 {
379379
return BlockTSTooBigErr
380380
}
381381
} else {
382-
if block.Time.Cmp(big.NewInt(time.Now().Unix())) == 1 {
382+
if header.Time.Cmp(big.NewInt(time.Now().Unix())) == 1 {
383383
return BlockFutureErr
384384
}
385385
}
386-
if block.Time.Cmp(parent.Time()) != 1 {
386+
if header.Time.Cmp(parent.Time) != 1 {
387387
return BlockEqualTSErr
388388
}
389389

390-
expd := CalcDifficulty(block.Time.Uint64(), parent.Time().Uint64(), parent.Number(), parent.Difficulty())
391-
if expd.Cmp(block.Difficulty) != 0 {
392-
return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd)
390+
expd := CalcDifficulty(header.Time.Uint64(), parent.Time.Uint64(), parent.Number, parent.Difficulty)
391+
if expd.Cmp(header.Difficulty) != 0 {
392+
return fmt.Errorf("Difficulty check failed for header %v, %v", header.Difficulty, expd)
393393
}
394394

395-
var a, b *big.Int
396-
a = parent.GasLimit()
397-
a = a.Sub(a, block.GasLimit)
395+
a := new(big.Int).Set(parent.GasLimit)
396+
a = a.Sub(a, header.GasLimit)
398397
a.Abs(a)
399-
b = parent.GasLimit()
398+
b := new(big.Int).Set(parent.GasLimit)
400399
b = b.Div(b, params.GasLimitBoundDivisor)
401-
if !(a.Cmp(b) < 0) || (block.GasLimit.Cmp(params.MinGasLimit) == -1) {
402-
return fmt.Errorf("GasLimit check failed for block %v (%v > %v)", block.GasLimit, a, b)
400+
if !(a.Cmp(b) < 0) || (header.GasLimit.Cmp(params.MinGasLimit) == -1) {
401+
return fmt.Errorf("GasLimit check failed for header %v (%v > %v)", header.GasLimit, a, b)
403402
}
404403

405-
num := parent.Number()
406-
num.Sub(block.Number, num)
404+
num := new(big.Int).Set(parent.Number)
405+
num.Sub(header.Number, num)
407406
if num.Cmp(big.NewInt(1)) != 0 {
408407
return BlockNumberErr
409408
}
410409

411410
if checkPow {
412-
// Verify the nonce of the block. Return an error if it's not valid
413-
if !pow.Verify(types.NewBlockWithHeader(block)) {
414-
return ValidationError("Block's nonce is invalid (= %x)", block.Nonce)
411+
// Verify the nonce of the header. Return an error if it's not valid
412+
if !pow.Verify(types.NewBlockWithHeader(header)) {
413+
return ValidationError("Header's nonce is invalid (= %x)", header.Nonce)
415414
}
416415
}
417-
418416
return nil
419417
}

core/block_processor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ func TestNumber(t *testing.T) {
4848
statedb := state.New(chain.Genesis().Root(), chain.chainDb)
4949
header := makeHeader(chain.Genesis(), statedb)
5050
header.Number = big.NewInt(3)
51-
err := ValidateHeader(pow, header, chain.Genesis(), false, false)
51+
err := ValidateHeader(pow, header, chain.Genesis().Header(), false, false)
5252
if err != BlockNumberErr {
5353
t.Errorf("expected block number error, got %q", err)
5454
}
5555

5656
header = makeHeader(chain.Genesis(), statedb)
57-
err = ValidateHeader(pow, header, chain.Genesis(), false, false)
57+
err = ValidateHeader(pow, header, chain.Genesis().Header(), false, false)
5858
if err == BlockNumberErr {
5959
t.Errorf("didn't expect block number error")
6060
}

eth/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func NewProtocolManager(networkId int, mux *event.TypeMux, txpool txPool, pow po
119119
manager.downloader = downloader.New(manager.eventMux, manager.chainman.HasBlock, manager.chainman.GetBlock, manager.chainman.CurrentBlock, manager.chainman.GetTd, manager.chainman.InsertChain, manager.removePeer)
120120

121121
validator := func(block *types.Block, parent *types.Block) error {
122-
return core.ValidateHeader(pow, block.Header(), parent, true, false)
122+
return core.ValidateHeader(pow, block.Header(), parent.Header(), true, false)
123123
}
124124
heighter := func() uint64 {
125125
return manager.chainman.CurrentBlock().NumberU64()

miner/worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func (self *worker) wait() {
279279
glog.V(logger.Error).Infoln("Invalid block found during mining")
280280
continue
281281
}
282-
if err := core.ValidateHeader(self.eth.BlockProcessor().Pow, block.Header(), parent, true, false); err != nil && err != core.BlockFutureErr {
282+
if err := core.ValidateHeader(self.eth.BlockProcessor().Pow, block.Header(), parent.Header(), true, false); err != nil && err != core.BlockFutureErr {
283283
glog.V(logger.Error).Infoln("Invalid header on mined block:", err)
284284
continue
285285
}

0 commit comments

Comments
 (0)