您好,登錄后才能下訂單哦!
本篇內容介紹了“以太坊的BlockChain主要方法是什么”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
package core
github.com/ethereum/go-ethereum/core/BlockChain.go
release 1.8
BlockChain管理具有創世紀數據庫的規范鏈,主要實現管理區塊(BLock)形成/引進鏈,reverts(恢復)、重組鏈
以太坊啟動節點啟動后后,系統中只存在一個BlockChain對象實例、BlockChain是以太坊中的一個類
BlockChain只保存規范鏈的頭區塊
在區塊的創建過程中,可能在短時間內產生一些分叉, 在我們的數據庫里面記錄的其實是一顆區塊樹。我們會認為其中總難度最高的一條路徑認為是我們的規 范的區塊鏈。 有很 多區塊雖然也能形成區塊鏈, 但是不是規范的區塊鏈。
Blockchain管理所有的Block, 讓其組成一個單向鏈表。Headerchain管理所有的Header,也形成一個單向鏈表, Headerchain是Blockchain里面的一部分,HeaderChain在全局范圍內也僅有一個對象
type BlockChain struct { chainConfig *params.ChainConfig // Chain & network configuration cacheConfig *CacheConfig // Cache configuration for pruning db ethdb.Database // Low level persistent database to store final content in triegc *prque.Prque // Priority queue mapping block numbers to tries to gc gcproc time.Duration // Accumulates canonical block processing for trie dumping hc *HeaderChain //包含了區塊頭的區塊鏈 rmLogsFeed event.Feed //刪除消息通知的組件 chainFeed event.Feed //下面是很多消息通知的組件 chainSideFeed event.Feed //分支鏈消息通知的組件 chainHeadFeed event.Feed //頭鏈消息通知的組件 logsFeed event.Feed //日志通知的組件 scope event.SubscriptionScope genesisBlock *types.Block // 創世塊 mu sync.RWMutex // global mutex for locking chain operations 全局互斥鎖操作 chainmu sync.RWMutex // blockchain insertion lock 區塊鏈插入鎖 procmu sync.RWMutex // block processor lock 區塊鏈處理鎖 checkpoint int // checkpoint counts towards the new checkpoint currentBlock atomic.Value // Current head of the block chain 當前的區塊頭 currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!) 當前的快速同步的區塊頭fast-sync方式:快速同步header,然后再跟進header同步全部內容 stateCache state.Database // State database to reuse between imports (contains state cache) bodyCache *lru.Cache // Cache for the most recent block bodies bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format receiptsCache *lru.Cache // Cache for the most recent receipts per block blockCache *lru.Cache // Cache for the most recent entire blocks futureBlocks *lru.Cache // future blocks are blocks added for later processing 暫時還不能插入的區塊存放位置 quit chan struct{} // blockchain quit channel running int32 // running must be called atomically // procInterrupt must be atomically called procInterrupt int32 // interrupt signaler for block processing wg sync.WaitGroup // chain processing wait group for shutting down 實現協程同步,線程信號量控制 engine consensus.Engine//一致性引擎 processor Processor // block processor interface 區塊處理器接口 validator Validator // block and state validator interface 區塊和狀態驗證器接口 vmConfig vm.Config //虛擬機的配置 badBlocks *lru.Cache // Bad block cache 不合法的區塊 shouldPreserve func(*types.Block) bool // Function used to determine whether should preserve the given block. 用于確定是否應該保留給定塊的函數 }
構造,NewBlockChain 使用數據庫里面的可用信息構造了一個初始化好的區塊鏈. 同時初始化了以太坊默認的 驗證器和處理器 (Validator and Processor)
BlockChain對象中,具體任務如下
根據外部參數或默認參數實例化BlockChain類,從數據庫中加載規范鏈狀態到BlockChain中
遍歷badHash列表,如果發現規范鏈中存在badHash列表中的錯誤區塊,則將規范鏈回滾到的錯誤 區塊的父區塊
開啟處理未來區塊的Go線程
func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(block *types.Block) bool) (*BlockChain, error) { if cacheConfig == nil { cacheConfig = &CacheConfig{ TrieNodeLimit: 256 * 1024 * 1024, TrieTimeLimit: 5 * time.Minute, } } bodyCache, _ := lru.New(bodyCacheLimit) bodyRLPCache, _ := lru.New(bodyCacheLimit) receiptsCache, _ := lru.New(receiptsCacheLimit) blockCache, _ := lru.New(blockCacheLimit) futureBlocks, _ := lru.New(maxFutureBlocks) badBlocks, _ := lru.New(badBlockLimit) bc := &BlockChain{ chainConfig: chainConfig, cacheConfig: cacheConfig, db: db, triegc: prque.New(nil), stateCache: state.NewDatabase(db), quit: make(chan struct{}), shouldPreserve: shouldPreserve, bodyCache: bodyCache, bodyRLPCache: bodyRLPCache, receiptsCache: receiptsCache, blockCache: blockCache, futureBlocks: futureBlocks, engine: engine, vmConfig: vmConfig, badBlocks: badBlocks, } bc.SetValidator(NewBlockValidator(chainConfig, bc, engine)) bc.SetProcessor(NewStateProcessor(chainConfig, bc, engine)) var err error bc.hc, err = NewHeaderChain(db, chainConfig, engine, bc.getProcInterrupt)//根據"LastHeader"獲取最新的區塊頭 if err != nil { return nil, err } bc.genesisBlock = bc.GetBlockByNumber(0)//獲取到創世紀塊 if bc.genesisBlock == nil { return nil, ErrNoGenesis } //loadLastState loads the last known chain state from the database, 同時構建 currentBlock currentHeader currentFastBlock if err := bc.loadLastState(); err != nil { return nil, err } // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain for hash := range BadHashes { if header := bc.GetHeaderByHash(hash); header != nil { // get the canonical block corresponding to the offending header's number headerByNumber := bc.GetHeaderByNumber(header.Number.Uint64()) // make sure the headerByNumber (if present) is in our current canonical chain if headerByNumber != nil && headerByNumber.Hash() == header.Hash() { log.Error("Found bad hash, rewinding chain", "number", header.Number, "hash", header.ParentHash) bc.SetHead(header.Number.Uint64() - 1) log.Error("Chain rewind was successful, resuming normal operation") } } } // 啟動一個線程 每隔5秒 處理 futureBlocks 排序 插入block Take ownership of this particular state go bc.update() return bc, nil }
1.以太坊主服務啟動的過程中
geth
-> makeFullNode
-> RegisterEthService
-> eth.New
->core.NewBlockChain
2.命令行
geth importChain
-> util.MakeChain
->core.NewBlockChain
“以太坊的BlockChain主要方法是什么”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。