Skip to content

Commit 2a15c97

Browse files
committed
dbft: Implement block verification callback. Close #65
1 parent df01fe2 commit 2a15c97

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

consensus/dbft/chainreader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type ChainHeaderReader interface {
1515
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
1616
HasBlock(hash common.Hash, number uint64) bool
1717
GetBlockByNumber(uint64) *types.Block
18+
VerifyBlock(block *types.Block) error
1819
}
1920

2021
// ChainHeaderWriter is a Blockchain API abstraction needed for proper blockQueue

consensus/dbft/dbft.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,36 @@ func New(config *params.DBFTConfig, _ ethdb.Database) (*DBFT, error) {
519519

520520
return nil
521521
}),
522+
dbft.WithVerifyBlock(func(b block.Block) bool {
523+
ethBlock, ok := b.(*Block)
524+
if !ok {
525+
return false
526+
}
527+
parent := c.chain.CurrentBlock()
528+
if parent.Number.Cmp(ethBlock.header.Number) >= 0 {
529+
log.Warn("proposed block has already outdated",
530+
"current block number", parent.Number.Uint64(),
531+
"proposed block number", ethBlock.header.Number)
532+
return false
533+
}
534+
if c.lastTimestamp > ethBlock.header.Time {
535+
log.Warn("proposed block has small timestamp",
536+
"ts", ethBlock.header.Time,
537+
"last", c.lastTimestamp)
538+
return false
539+
}
540+
res := types.NewBlockWithHeader(ethBlock.header)
541+
// Uncles are always nil in dBFT-like consensus.
542+
res = res.WithBody(ethBlock.transactions, nil)
543+
544+
err := c.chain.VerifyBlock(res)
545+
if err != nil {
546+
log.Warn("proposed block verification failed",
547+
"err", err.Error())
548+
return false
549+
}
550+
return true
551+
}),
522552
dbft.WithBroadcast(func(p payload.ConsensusPayload) {
523553
if err := p.(*Payload).Sign(c.dbft.Priv.(*Signer)); err != nil {
524554
log.Warn("can't sign consensus payload", "error", err)

core/blockchain.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,3 +2605,21 @@ func (bc *BlockChain) SetTrieFlushInterval(interval time.Duration) {
26052605
func (bc *BlockChain) GetTrieFlushInterval() time.Duration {
26062606
return time.Duration(bc.flushInterval.Load())
26072607
}
2608+
2609+
// VerifyBlock checks block state
2610+
func (bc *BlockChain) VerifyBlock(block *types.Block) error {
2611+
parent := bc.GetBlockByHash(block.ParentHash())
2612+
statedb, err := bc.StateAt(parent.Root())
2613+
if err != nil {
2614+
return err
2615+
}
2616+
2617+
receipts, _, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig)
2618+
if err != nil {
2619+
return err
2620+
}
2621+
if err := bc.validator.ValidateState(block, statedb, receipts, usedGas); err != nil {
2622+
return err
2623+
}
2624+
return nil
2625+
}

0 commit comments

Comments
 (0)