// Seal implements consensus.Engine, attempting to find a nonce that satisfies
// the block's difficulty requirements.
Seal implements consensus.Engine, attempting to find a nonce that satisfies
the block's difficulty requirements.
// Seal implements consensus . Engine attempting to find a nonce that satisfies // the block ' s difficulty requirements .
func (amhash *Amhash) Seal(chain consensus.ChainReader, header *types.Header, stop <-chan struct{}, isBroadcastNode bool) (*types.Header, error) {
	log.INFO("seal", "挖矿", "开始", "高度", header.Number.Uint64())
	defer log.INFO("seal", "挖矿", "结束", "高度", header.Number.Uint64())
	curHeader := types.CopyHeader(header)
	aiHash, stopped, err := amhash.aiMineProcess(chain, curHeader, stop, isBroadcastNode)
	if err != nil {
		return nil, err
	}
	if stopped {
		return nil, nil
	}
	curHeader.AIHash = aiHash
	abort := make(chan struct{})
	found := make(chan *types.Header)
	amhash.lock.Lock()
	threads := amhash.threads
	if amhash.rand == nil {
		seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
		if err != nil {
			amhash.lock.Unlock()
			return nil, err
		}
		amhash.rand = rand.New(rand.NewSource(seed.Int64()))
	}
	amhash.lock.Unlock()
	threads = runtime.NumCPU()
	if isBroadcastNode {
		threads = 1
	}
	var pend sync.WaitGroup
	for i := 0; i < threads; i++ {
		pend.Add(1)
		go func(id int, nonce uint64) {
			defer pend.Done()
			amhash.mine(curHeader, id, nonce, abort, found, isBroadcastNode)
		}(i, uint64(amhash.rand.Int63()))
	}
	var result *types.Header
	select {
	case <-stop:
		log.INFO("SEALER", "Sealer receive stop mine, curHeader", curHeader.HashNoSignsAndNonce().TerminalString())
		close(abort)
	case result = <-found:
		close(abort)
	case <-amhash.update:
		close(abort)
		pend.Wait()
		return amhash.Seal(chain, curHeader, stop, isBroadcastNode)
	}
	pend.Wait()
	return result, nil
}
func ( amhash * Amhash ) Seal ( chain consensus . ChainReader , header * types . Header , stop <- chan struct { } , isBroadcastNode bool ) ( * types . Header , error ) { log . INFO ( " " , " 开 始 , h a m b r.Uint 6 4()) defer log . INFO ( " " , " 结 束 , h a m b r.Uint 6 4()) curHeader := types . CopyHeader ( header ) aiHash , stopped , err := amhash . aiMineProcess ( chain , curHeader , stop , isBroadcastNode ) if err != nil { return nil , err } if stopped { return nil , nil } curHeader . AIHash = aiHash abort := make ( chan struct { } ) found := make ( chan * types . Header ) amhash . lock . Lock ( ) threads := amhash . threads if amhash . rand == nil { seed , err := crand . Int ( crand . Reader , big . NewInt ( math . MaxInt64 ) ) if err != nil { amhash . lock . Unlock ( ) return nil , err } amhash . rand = rand . New ( rand . NewSource ( seed . Int64 ( ) ) ) } amhash . lock . Unlock ( ) threads = runtime . NumCPU ( ) if isBroadcastNode { threads = 1 } var pend sync . WaitGroup for i := 0 ; i < threads ; i ++ { pend . Add ( 1 ) go func ( id int , nonce uint64 ) { defer pend . Done ( ) amhash . mine ( curHeader , id , nonce , abort , found , isBroadcastNode ) } ( i , uint64 ( amhash . rand . Int63 ( ) ) ) } var result * types . Header select { case <- stop : log . INFO ( " " , " " , curHeader . HashNoSignsAndNonce ( ) . TerminalString ( ) ) close ( abort ) case result = <- found : close ( abort ) case <- amhash . update : close ( abort ) pend . Wait ( ) return amhash . Seal ( chain , curHeader , stop , isBroadcastNode ) } pend . Wait ( ) return result , nil }
Seal implements consensus.Engine, attempting to find a nonce that satisfies
the block's difficulty requirements.
Seal implements consensus . Engine attempting to find a nonce that satisfies the block ' s difficulty requirements .
// start ai mining first // Create a runner and the multiple search threads it directs // Wait until sealing is terminated or a nonce is found // Outside abort, stop all miner threads // One of the threads found a block, abort all others // Thread count was changed on user request, restart // Wait for all miners to terminate and return the block