本篇內(nèi)容介紹了“以太坊的BlockChain主要方法是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)建站專注于枝江網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供枝江營銷型網(wǎng)站建設(shè),枝江網(wǎng)站制作、枝江網(wǎng)頁設(shè)計(jì)、枝江網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)服務(wù),打造枝江網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供枝江網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
package core
github.com/ethereum/go-ethereum/core/BlockChain.go
release 1.8
BlockChain管理具有創(chuàng)世紀(jì)數(shù)據(jù)庫的規(guī)范鏈,主要實(shí)現(xiàn)管理區(qū)塊(BLock)形成/引進(jìn)鏈,reverts(恢復(fù))、重組鏈
以太坊啟動(dòng)節(jié)點(diǎn)啟動(dòng)后后,系統(tǒng)中只存在一個(gè)BlockChain對象實(shí)例、BlockChain是以太坊中的一個(gè)類
BlockChain只保存規(guī)范鏈的頭區(qū)塊
在區(qū)塊的創(chuàng)建過程中,可能在短時(shí)間內(nèi)產(chǎn)生一些分叉, 在我們的數(shù)據(jù)庫里面記錄的其實(shí)是一顆區(qū)塊樹。我們會(huì)認(rèn)為其中總難度最高的一條路徑認(rèn)為是我們的規(guī) 范的區(qū)塊鏈。 有很 多區(qū)塊雖然也能形成區(qū)塊鏈, 但是不是規(guī)范的區(qū)塊鏈。
Blockchain管理所有的Block, 讓其組成一個(gè)單向鏈表。Headerchain管理所有的Header,也形成一個(gè)單向鏈表, Headerchain是Blockchain里面的一部分,HeaderChain在全局范圍內(nèi)也僅有一個(gè)對象
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 //包含了區(qū)塊頭的區(qū)塊鏈 rmLogsFeed event.Feed //刪除消息通知的組件 chainFeed event.Feed //下面是很多消息通知的組件 chainSideFeed event.Feed //分支鏈消息通知的組件 chainHeadFeed event.Feed //頭鏈消息通知的組件 logsFeed event.Feed //日志通知的組件 scope event.SubscriptionScope genesisBlock *types.Block // 創(chuàng)世塊 mu sync.RWMutex // global mutex for locking chain operations 全局互斥鎖操作 chainmu sync.RWMutex // blockchain insertion lock 區(qū)塊鏈插入鎖 procmu sync.RWMutex // block processor lock 區(qū)塊鏈處理鎖 checkpoint int // checkpoint counts towards the new checkpoint currentBlock atomic.Value // Current head of the block chain 當(dāng)前的區(qū)塊頭 currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!) 當(dāng)前的快速同步的區(qū)塊頭fast-sync方式:快速同步header,然后再跟進(jìn)header同步全部內(nèi)容 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 暫時(shí)還不能插入的區(qū)塊存放位置 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 實(shí)現(xiàn)協(xié)程同步,線程信號(hào)量控制 engine consensus.Engine//一致性引擎 processor Processor // block processor interface 區(qū)塊處理器接口 validator Validator // block and state validator interface 區(qū)塊和狀態(tài)驗(yàn)證器接口 vmConfig vm.Config //虛擬機(jī)的配置 badBlocks *lru.Cache // Bad block cache 不合法的區(qū)塊 shouldPreserve func(*types.Block) bool // Function used to determine whether should preserve the given block. 用于確定是否應(yīng)該保留給定塊的函數(shù) }
構(gòu)造,NewBlockChain 使用數(shù)據(jù)庫里面的可用信息構(gòu)造了一個(gè)初始化好的區(qū)塊鏈. 同時(shí)初始化了以太坊默認(rèn)的 驗(yàn)證器和處理器 (Validator and Processor)
BlockChain對象中,具體任務(wù)如下
根據(jù)外部參數(shù)或默認(rèn)參數(shù)實(shí)例化BlockChain類,從數(shù)據(jù)庫中加載規(guī)范鏈狀態(tài)到BlockChain中
遍歷badHash列表,如果發(fā)現(xiàn)規(guī)范鏈中存在badHash列表中的錯(cuò)誤區(qū)塊,則將規(guī)范鏈回滾到的錯(cuò)誤 區(qū)塊的父區(qū)塊
開啟處理未來區(qū)塊的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)//根據(jù)"LastHeader"獲取最新的區(qū)塊頭 if err != nil { return nil, err } bc.genesisBlock = bc.GetBlockByNumber(0)//獲取到創(chuàng)世紀(jì)塊 if bc.genesisBlock == nil { return nil, ErrNoGenesis } //loadLastState loads the last known chain state from the database, 同時(shí)構(gòu)建 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") } } } // 啟動(dòng)一個(gè)線程 每隔5秒 處理 futureBlocks 排序 插入block Take ownership of this particular state go bc.update() return bc, nil }
1.以太坊主服務(wù)啟動(dòng)的過程中
geth
-> makeFullNode
-> RegisterEthService
-> eth.New
->core.NewBlockChain
2.命令行
geth importChain
-> util.MakeChain
->core.NewBlockChain
“以太坊的BlockChain主要方法是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!