以太坊源码BlockChain类

  1. 功能:管理区块链,实现增、改、查操作
  2. 以太坊启动后,系统中只存在一个BlockChain实例
  3. 文件位置:core-->blockchain.go
  4. 数据结构:

以太坊源码BlockChain类_第1张图片

Processor:处理器

Validator:验证

consensus:共识

type BlockChain struct {
	chainConfig *params.ChainConfig // 链和网络配置
	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 // 当前区块链的区块头
	currentFastBlock atomic.Value // 快速同步的区块头

	stateCache   state.Database // 状态缓存
	bodyCache    *lru.Cache     // 区块体缓存
	bodyRLPCache *lru.Cache     // Cache for the most recent block bodies in RLP encoded format
	blockCache   *lru.Cache     // 最近区块缓存
	futureBlocks *lru.Cache     // 未来区块缓存

	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 // 区块处理接口
	validator Validator // 区块和状态处理接口
	vmConfig  vm.Config

	badBlocks *lru.Cache // 坏区块
}
type HeaderChain struct {
	config *params.ChainConfig

	chainDb       ethdb.Database
	genesisHeader *types.Header

	currentHeader     atomic.Value // 当前区块头链的区块头
	currentHeaderHash common.Hash  // 当前区块头链的区块头的Hash

	headerCache *lru.Cache // 最新区块头缓存
	tdCache     *lru.Cache // 最新区块总难度
	numberCache *lru.Cache // 最新区块的区块号

	procInterrupt func() bool

	rand   *mrand.Rand
	engine consensus.Engine
}

BlockChain管理区块链:

  • BlockChain只保存规范链的头区块
  • 其他区块都保存在数据库中

 

你可能感兴趣的:(区块链)