您好,登录后才能下订单哦!
本篇内容介绍了“以太坊的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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。