forked from bnb-chain/bsc
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: kyrie-yl <[email protected]>
- Loading branch information
Showing
14 changed files
with
463 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
lru "github.com/hashicorp/golang-lru" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/rawdb" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
const verifiedCacheSize = 256 | ||
|
||
type VerifyManager struct { | ||
bc *BlockChain | ||
tasks map[common.Hash]*VerifyTask | ||
peers VerifyPeers | ||
verifiedCache *lru.Cache | ||
allowUntrustedVerify bool | ||
verifyCh chan common.Hash | ||
exitCh chan struct{} | ||
} | ||
|
||
func NewVerifyManager(blockchain *BlockChain) *VerifyManager { | ||
verifiedCache, _ := lru.New(verifiedCacheSize) | ||
vm := &VerifyManager{ | ||
bc: blockchain, | ||
tasks: make(map[common.Hash]*VerifyTask), | ||
verifiedCache: verifiedCache, | ||
verifyCh: make(chan common.Hash), | ||
exitCh: make(chan struct{}), | ||
} | ||
return vm | ||
} | ||
|
||
func (vm *VerifyManager) Start() { | ||
//read disk store to initial verified cache | ||
//load unverified blocks in a normalized chain and start a batch of verify task | ||
header := vm.bc.CurrentHeader() | ||
vm.NewBlockVerifyTask(header) | ||
go vm.mainLoop() | ||
} | ||
|
||
func (vm *VerifyManager) Stop() { | ||
defer close(vm.exitCh) | ||
vm.exitCh <- struct{}{} | ||
} | ||
|
||
func (vm *VerifyManager) mainLoop() { | ||
pruneTicker := time.NewTicker(time.Second) | ||
for { | ||
select { | ||
case hash := <-vm.verifyCh: | ||
vm.cacheBlockVerified(hash) | ||
rawdb.WriteTrustBlockHash(vm.bc.db, hash) | ||
delete(vm.tasks, hash) | ||
case <-pruneTicker.C: | ||
for hash, task := range vm.tasks { | ||
if vm.bc.CurrentHeader().Number.Uint64()-task.blockHeader.Number.Uint64() > 15 { | ||
task.terminalCh <- struct{}{} | ||
delete(vm.tasks, hash) | ||
} | ||
} | ||
case <-vm.exitCh: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (vm *VerifyManager) NewBlockVerifyTask(header *types.Header) { | ||
for i := 0; i < 11; i++ { | ||
hash := header.Hash() | ||
diffLayer := vm.bc.GetTrustedDiffLayer(hash) | ||
//if this block has no diff, there is no need to verify it. | ||
if diffLayer == nil { | ||
continue | ||
} | ||
//if verified cache record that this block has been verified, skip. | ||
if _, ok := vm.verifiedCache.Get(hash); ok { | ||
continue | ||
} | ||
//if verified storage record that this block has been verified, skip. | ||
if rawdb.ReadTrustBlockHash(vm.bc.db, hash) { | ||
vm.cacheBlockVerified(hash) | ||
continue | ||
} | ||
//if there already has a verify task for this block, skip. | ||
if _, ok := vm.tasks[hash]; ok { | ||
continue | ||
} | ||
diffHash, err := GetTrustedDiffHash(diffLayer) | ||
if err != nil { | ||
log.Error("failed to get diff hash", "block", hash, "number", header.Number, "error:", err) | ||
} | ||
verifyTask := NewVerifyTask(diffHash, header, vm.peers, vm.bc.db, vm.verifyCh, vm.allowUntrustedVerify) | ||
vm.tasks[hash] = verifyTask | ||
header = vm.bc.GetHeaderByHash(header.ParentHash) | ||
} | ||
} | ||
|
||
func (vm *VerifyManager) cacheBlockVerified(hash common.Hash) { | ||
if vm.verifiedCache.Len() >= verifiedCacheSize { | ||
vm.verifiedCache.RemoveOldest() | ||
} | ||
vm.verifiedCache.Add(hash, true) | ||
} | ||
|
||
//CheckAncestorVerified function check whether head 11 of this block has been verified. | ||
//If not, the blockchain should stop to insert new block. | ||
func (vm *VerifyManager) CheckAncestorVerified(header *types.Header) bool { | ||
pHeader := header | ||
for i := 0; i < 11; i++ { | ||
pHeader = vm.bc.GetHeaderByHash(pHeader.ParentHash) | ||
} | ||
hash := pHeader.Hash() | ||
if _, ok := vm.verifiedCache.Get(hash); ok { | ||
return true | ||
} else { | ||
return rawdb.ReadTrustBlockHash(vm.bc.db, hash) | ||
} | ||
return false | ||
} | ||
|
||
func (vm *VerifyManager) HandleRootResponse(vr *types.VerifyResult, pid string) error { | ||
if vt, ok := vm.tasks[vr.BlockHash]; ok { | ||
vt.messageCh <- VerifyMessage{verifyResult: vr, peerId: pid} | ||
return nil | ||
} | ||
return fmt.Errorf("") | ||
} |
Oops, something went wrong.