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 14, 2022
1 parent 3bc4262 commit 1af60b3
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 192 deletions.
5 changes: 0 additions & 5 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1675,11 +1675,6 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
if cfg.TriesVerifyMode.NeedRemoteVerify() {
cfg.EnableTrustProtocol = true
}
// If a node sets verify mode but not local, it's a fast node whose difflayer is not integral.
// So fast node should disable diff protocol.
if cfg.TriesVerifyMode != core.LocalVerify {
cfg.DisableDiffProtocol = true
}
}
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheSnapshotFlag.Name) {
cfg.SnapshotCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheSnapshotFlag.Name) / 100
Expand Down
55 changes: 16 additions & 39 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 *remoteVerifyManager
}

// 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)
validator.remoteValidator = NewVerifyManager(blockchain, peers, mode == InsecureVerify)
go validator.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 @@ -87,6 +84,13 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
}
return nil
},
// for fast node which verify trie from remote verify peers, a block's H-11 ancestor should have been verify.
func() error {
if v.remoteValidator != nil && !v.remoteValidator.AncestorVerified(v.bc.GetHeaderByNumber(header.Number.Uint64())) {
return fmt.Errorf("block's ancessor %x has not been verified", block.Hash())
}
return nil
},
}
validateRes := make(chan error, len(validateFuns))
for _, f := range validateFuns {
Expand Down Expand Up @@ -157,35 +161,8 @@ 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) {
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() *remoteVerifyManager {
return v.remoteValidator
}

// CalcGasLimit computes the gas limit of the next block after parent. It aims
Expand Down
8 changes: 2 additions & 6 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 @@ -2122,9 +2121,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
blockWriteTimer.Update(time.Since(substart))
blockInsertTimer.UpdateSince(start)

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

switch status {
case CanonStatTy:
log.Debug("Inserted new block", "number", block.Number(), "hash", block.Hash(),
Expand Down Expand Up @@ -3023,9 +3019,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
9 changes: 0 additions & 9 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ var (
// difflayer database
diffLayerPrefix = []byte("d") // diffLayerPrefix + hash -> diffLayer

// trust block database
trustBlockPrefix = []byte("trust-block-") // trustBlockPrefix + hash -> verify result

preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
configPrefix = []byte("ethereum-config-") // config prefix for the db

Expand All @@ -121,8 +118,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,10 +164,6 @@ 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()...))
}

// headerNumberKey = headerNumberPrefix + hash
func headerNumberKey(hash common.Hash) []byte {
Expand Down
Loading

0 comments on commit 1af60b3

Please sign in to comment.