Skip to content

Commit 821619e

Browse files
committed
core, eth, miner: use pure header validation
1 parent e9a8051 commit 821619e

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
@@ -213,7 +213,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
213213
txs := block.Transactions()
214214

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

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

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

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

376376
if uncle {
377-
if block.Time.Cmp(common.MaxBig) == 1 {
377+
if header.Time.Cmp(common.MaxBig) == 1 {
378378
return BlockTSTooBigErr
379379
}
380380
} else {
381-
if block.Time.Cmp(big.NewInt(time.Now().Unix())) == 1 {
381+
if header.Time.Cmp(big.NewInt(time.Now().Unix())) == 1 {
382382
return BlockFutureErr
383383
}
384384
}
385-
if block.Time.Cmp(parent.Time()) != 1 {
385+
if header.Time.Cmp(parent.Time) != 1 {
386386
return BlockEqualTSErr
387387
}
388388

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

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

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

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

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
@@ -118,7 +118,7 @@ func NewProtocolManager(networkId int, mux *event.TypeMux, txpool txPool, pow po
118118
manager.downloader = downloader.New(manager.eventMux, manager.chainman.HasBlock, manager.chainman.GetBlock, manager.chainman.CurrentBlock, manager.chainman.GetTd, manager.chainman.InsertChain, manager.removePeer)
119119

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

miner/worker.go

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

0 commit comments

Comments
 (0)