以太坊miner/unconfirmed源码中文分析

//是一个数据结构,用来跟踪用户本地的挖矿信息的,比如挖出了一个块,那么等待足够的后续区块确认之后(5个),再查看本地挖矿的区块是否包含在规范的区块链内部。
// headerRetriever is used by the unconfirmed block set to verify whether a previously
// mined block is part of the canonical chain or not.
//由未确认的块组使用,以验证先前挖掘的块是否是规范链的一部分
type headerRetriever interface {
	// GetHeaderByNumber retrieves the canonical header associated with a block number.
	GetHeaderByNumber(number uint64) *types.Header
}
//是本地挖掘区块的一个小的元数据的集合,用来放入未确认的集合用来追踪本地挖掘的区块是否被包含进入规范的区块链
// unconfirmedBlock is a small collection of metadata about a locally mined block
// that is placed into a unconfirmed set for canonical chain inclusion tracking.
type unconfirmedBlock struct {
	index uint64
	hash  common.Hash
}

// unconfirmedBlocks implements a data structure to maintain locally mined blocks
// have have not yet reached enough maturity to guarantee chain inclusion. It is
// used by the miner to provide logs to the user when a previously mined block
// has a high enough guarantee to not be reorged out of the canonical chain.
//实现了一个数据结构,用来管理本地挖掘的区块,这些区块还没有达到足够的信任度来证明他们已经被规范的区块链接受。
// 它用来给矿工提供信息,以便他们了解他们之前挖到的区块是否被包含进入了规范的区块链。
type unconfirmedBlocks struct {
	chain  headerRetriever // Blockchain to verify canonical status through需要验证的区块链 用这个接口来获取当前的规范的区块头信息
	depth  uint            // Depth after which to discard previous blocks 经过多少个区块之后丢弃之前的区块
	blocks *ring.Ring      // Block infos to allow canonical chain cross checks 区块信息,以允许规范链交叉检查
	lock   sync.RWMutex    // Protects the fields from concurrent access
}
//返回新的数据结构来跟踪当前未确认的块。
// newUnconfirmedBlocks returns new data structure to track currently unconfirmed blocks.
func newUnconfirmedBlocks(chain headerRetriever, depth uint) *unconfirmedBlocks {
	return &unconfirmedBlocks{
		chain: chain,
		depth: depth,
	}
}
//插入跟踪区块, 当矿工挖到一个区块的时候调用, index是区块的高度, hash是区块的hash值。
// Insert adds a new block to the set of unconfirmed ones.
func (set *unconfirmedBlocks) Insert(index uint64, hash common.Hash) {
	// If a new block was mined locally, shift out any old enough blocks
	//如果一个本地的区块挖到了,那么移出已经超过depth的区块
	set.Shift(index)

	// Create the new item as its own ring
	// 循环队列的操作
	item := ring.New(1)
	item.Value = &unconfirmedBlock{
		index: index,
		hash:  hash,
	}
	// Set as the initial ring or append to the end
	set.lock.Lock()
	defer set.lock.Unlock()

	if set.blocks == nil {
		set.blocks = item
	} else {
		 移动到循环队列的最后一个元素插入item
		set.blocks.Move(-1).Link(item)
	}
	// Display a log for the user to notify of a new mined block unconfirmed
	log.Info("                                    

你可能感兴趣的:(miner)