Skip to content

Commit

Permalink
resolve comments from second review
Browse files Browse the repository at this point in the history
Signed-off-by: kyrie-yl <yl.on.the.way@gmail.com>
  • Loading branch information
kyrie-yl committed Feb 11, 2022
1 parent 3bc4262 commit 0d80e6f
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 165 deletions.
50 changes: 16 additions & 34 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,26 @@ import (
//
// BlockValidator implements Validator.
type BlockValidator struct {
config *params.ChainConfig // Chain configuration options
bc *BlockChain // Canonical block chain
engine consensus.Engine // Consensus engine used for validating
remoteValidator *VerifyManager
config *params.ChainConfig // Chain configuration options
bc *BlockChain // Canonical block chain
engine consensus.Engine // Consensus engine used for validating
remoteValidator *verifyManager
}

// NewBlockValidator returns a new block validator which is safe for re-use
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine, mode *VerifyMode) *BlockValidator {
func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engine consensus.Engine, mode *VerifyMode, peers verifyPeers) *BlockValidator {
validator := &BlockValidator{
config: config,
engine: engine,
bc: blockchain,
}
if mode.NeedRemoteVerify() {
validator.remoteValidator = NewVerifyManager(blockchain, *mode == InsecureVerify)
remoteValidator := NewVerifyManager(blockchain, peers, *mode == InsecureVerify)
go remoteValidator.mainLoop()
}
return validator
}

func(v *BlockValidator) RemoteVerifyManager() *VerifyManager{
return v.remoteValidator
}

// ValidateBody validates the given block's uncles and verifies the block
// header's transaction and uncle roots. The headers are assumed to be already
// validated at this point.
Expand Down Expand Up @@ -101,6 +98,12 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
return r
}
}
// for fast node which verify trie from remote verify peers, a block's H-11 ancestor should have been verify.
if v.remoteValidator != nil {
if !v.remoteValidator.AncestorVerified(v.bc.GetHeaderByNumber(header.Number.Uint64())) {
return fmt.Errorf("block's ancessor %x has not been verified", block.Hash())
}
}
return nil
}

Expand Down Expand Up @@ -157,35 +160,14 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
return err
}

func (v *BlockValidator) StartRemoteVerify(peers VerifyPeers) {
v.remoteValidator.peers = peers
if v.remoteValidator != nil {
go v.remoteValidator.verifyManagerLoop()
}
}

func (v * BlockValidator) StopRemoteVerify() {
if v.remoteValidator != nil {
v.remoteValidator.Stop()
}
}

func (v * BlockValidator) VerifyBlock(header *types.Header) {
func (v *BlockValidator) VerifyBlockTrie(header *types.Header) {
if v.remoteValidator != nil {
v.remoteValidator.newTaskCh <- header
}
}

// ValidateBlockVerify validate that weather the H-11 ancestor of the block has been verified by peers.
// If not, the blockchain should halt.
func (v * BlockValidator) ValidateBlockVerify(block *types.Block) error {
if v.remoteValidator != nil {
header := block.Header()
if !v.remoteValidator.AncestorVerified(v.bc.GetHeaderByNumber(header.Number.Uint64())) {
return fmt.Errorf("block's ancessor %x has not been verified", block.Hash())
}
}
return nil
func (v *BlockValidator) RemoteVerifyManager() *verifyManager {
return v.remoteValidator
}

// CalcGasLimit computes the gas limit of the next block after parent. It aims
Expand Down
7 changes: 3 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,6 @@ func (bc *BlockChain) Stop() {
close(bc.quit)
bc.StopInsert()
bc.wg.Wait()
bc.validator.StopRemoteVerify()

// Ensure that the entirety of the state snapshot is journalled to disk.
var snapBase common.Hash
Expand Down Expand Up @@ -2123,7 +2122,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
blockInsertTimer.UpdateSince(start)

//Start a routine to verify this block.
bc.validator.VerifyBlock(block.Header())
bc.validator.VerifyBlockTrie(block.Header())

switch status {
case CanonStatTy:
Expand Down Expand Up @@ -3023,9 +3022,9 @@ func EnablePersistDiff(limit uint64) BlockChainOption {
}
}

func EnableBlockValidator(chainConfig *params.ChainConfig, engine consensus.Engine, mode *VerifyMode) BlockChainOption {
func EnableBlockValidator(chainConfig *params.ChainConfig, engine consensus.Engine, mode *VerifyMode, peers verifyPeers) BlockChainOption {
return func(bc *BlockChain) *BlockChain {
bc.validator = NewBlockValidator(chainConfig, bc, engine, mode)
bc.validator = NewBlockValidator(chainConfig, bc, engine, mode, peers)
return bc
}
}
Expand Down
6 changes: 1 addition & 5 deletions core/blockchain_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ func (it *insertIterator) next() (*types.Block, error) {
return it.chain[it.index], it.errors[it.index]
}
// Block header valid, run body validation and return
if err := it.validator.ValidateBody(it.chain[it.index]); err != nil {
return it.chain[it.index], err
}
// Block body valid, run remote verify and return
return it.chain[it.index], it.validator.ValidateBlockVerify(it.chain[it.index])
return it.chain[it.index], it.validator.ValidateBody(it.chain[it.index])
}

// peek returns the next block in the iterator, along with any potential validation
Expand Down
20 changes: 0 additions & 20 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,6 @@ func DeleteCanonicalHash(db ethdb.KeyValueWriter, number uint64) {
}
}

func IsTrustBlock(db ethdb.Reader, hash common.Hash) bool {
data, _ := db.Get(trustBlockHashKey(hash))
if len(data) == 0 {
return false
}
return bytes.Equal(data,[]byte{byteTrue})
}

func MarkTrustBlock(db ethdb.KeyValueWriter, hashkey common.Hash) {
if err := db.Put(trustBlockHashKey(hashkey),[]byte{byteTrue}); err != nil {
log.Crit("Failed to store trust block hash")
}
}

func DeleteTrustBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
if err := db.Delete(trustBlockHashKey(hash)); err != nil {
log.Crit("Failed to delete trust block hash")
}
}

// ReadAllHashes retrieves all the hashes assigned to blocks at a certain heights,
// both canonical and reorged forks included.
func ReadAllHashes(db ethdb.Iteratee, number uint64) []common.Hash {
Expand Down
3 changes: 1 addition & 2 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ const (

// freezerDifficultyTable indicates the name of the freezer total difficulty table.
freezerDifficultyTable = "diffs"
//
byteTrue = 0x01
)

// FreezerNoSnappy configures whether compression is disabled for the ancient-tables.
Expand Down Expand Up @@ -169,6 +167,7 @@ func headerTDKey(number uint64, hash common.Hash) []byte {
func headerHashKey(number uint64) []byte {
return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
}

// trustBlockHashKey = trustBlockPrefix + hash
func trustBlockHashKey(hash common.Hash) []byte {
return append(append(trustBlockPrefix, hash.Bytes()...))
Expand Down
Loading

0 comments on commit 0d80e6f

Please sign in to comment.