In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the main methods of BlockChain in Ethernet Square". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
BlockChain location:
Package core
Github.com/ethereum/go-ethereum/core/BlockChain.go
Release 1.8
Action
BlockChain management has the standard chain of Genesis database, which mainly realizes management block (BLock) formation / introduction chain, reverts (recovery) and reorganization chain.
After the startup node of Ethernet Square, there is only one instance of BlockChain object in the system, and BlockChain is a class in Ethernet Square.
BlockChain saves only the header block of the specification chain
Specification chain
In the process of creating blocks, some bifurcations may occur in a short period of time, and what is actually recorded in our database is a block tree. We would think that one of the most difficult paths is the block chain of our norm. Although many blocks can also form a block chain, it is not a standard block chain.
Blockchain data structure
Blockchain manages all the Block to form an one-way linked list. Headerchain manages all Header and forms an one-way linked list. Headerchain is part of Blockchain, and HeaderChain has only one object in the global scope.
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 / / contains the block chain of the block head RmLogsFeed event.Feed / delete message notification component chainFeed event.Feed / the following is the component of many message notification chainSideFeed event.Feed / / branch chain message notification component chainHeadFeed event.Feed / / header chain message notification component logsFeed event.Feed / / log notification component scope event.SubscriptionScope genesisBlock * types.Block / / Genesis block mu sync.RWMutex / / global mutex for locking chain operations global mutex operation chainmu sync.RWMutex / / blockchain insertion lock block chain insertion lock procmu sync.RWMutex / / block processor lock block chain processing lock checkpoint int / / checkpoint counts towards the new checkpoint currentBlock atomic.Value / / Current head of the blockchain current block header currentFastBlock atomic .value / / Current head of the fast-sync chain (may be above the block chain!) The current fast synchronizing block head fast-sync mode: fast synchronizing header Then follow up with header to synchronize all content 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 Block storage location 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 to achieve synchronization Thread semaphores control engine consensus.Engine// consistency engine processor Processor / / block processor interface block processor interface validator Validator / / block and state validator interface block and state verifier interface vmConfig vm.Config / / virtual machine configuration badBlocks * lru.Cache / / Bad block cache illegal block shouldPreserve func (* Types.Block) bool / / Function used to determine whether should preserve the given block. Function used to determine whether a given block should be retained} main method NewBlockChain
Construction, NewBlockChain uses the information available in the database to construct an initialized block chain. At the same time, the default validator and processor of Ethernet Fong (Validator and Processor) are initialized.
In the BlockChain object, the specific tasks are as follows
Instantiate the BlockChain class based on external or default parameters and load the canonical chain state from the database into BlockChain
Traversing the badHash list, if an error block in the badHash list is found in the specification chain, the parent block of the error block to which the specification chain is rolled back
Start the Go thread that processes the future block
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) / / get the latest block header if err! = nil {return nil based on "LastHeader" Err} bc.genesisBlock = bc.GetBlockByNumber (0) / / get to Genesis Block if bc.genesisBlock = = nil {return nil, ErrNoGenesis} / / loadLastState loads the last known chain state from the database Also build 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 ")} / / start a thread to process futureBlocks sorting and insert block Take ownership of this particular state go bc.update () return bc, nil} NewBlockChain function call time every 5 seconds:
1. In the process of starting the ethernet master service
Geth
-> makeFullNode
-> RegisterEthService
-> eth.New
-> core.NewBlockChain
two。 Command Lin
Geth importChain
-> util.MakeChain
-> core.NewBlockChain
This is the end of the content of "what are the main methods of BlockChain in Ethernet Square". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.