示例#1
0
文件: blks.go 项目: rollyleal/gocoin
func get_blocks() {
	var bl *btc.Block

	DlStartTime = time.Now()
	BlocksMutex.Lock()
	BlocksComplete = TheBlockChain.BlockTreeEnd.Height
	CurrentBlockHeight := BlocksComplete + 1
	BlocksMutex.Unlock()

	TheBlockChain.DoNotSync = true

	tickSec := time.Tick(time.Second)
	tickDrop := time.Tick(DROP_PEER_EVERY_SEC * time.Second)
	tickStat := time.Tick(6 * time.Second)

	for !GlobalExit() && CurrentBlockHeight <= LastBlockHeight {
		select {
		case <-tickSec:
			cc := open_connection_count()
			if cc > MaxNetworkConns {
				drop_slowest_peers()
			} else if cc < MaxNetworkConns {
				add_new_connections()
			}

		case <-tickStat:
			print_stats()
			usif_prompt()

		case <-tickDrop:
			if open_connection_count() >= MaxNetworkConns {
				drop_slowest_peers()
			}

		case bl = <-BlockQueue:
			bl.Trusted = CurrentBlockHeight <= TrustUpTo
			if OnlyStoreBlocks {
				TheBlockChain.Blocks.BlockAdd(CurrentBlockHeight, bl)
			} else {
				er, _, _ := TheBlockChain.CheckBlock(bl)
				if er != nil {
					fmt.Println("CheckBlock:", er.Error())
					return
				} else {
					bl.LastKnownHeight = CurrentBlockHeight + uint32(len(BlockQueue))
					TheBlockChain.AcceptBlock(bl)
				}
			}
			atomic.StoreUint32(&LastStoredBlock, CurrentBlockHeight)
			atomic.AddUint64(&DlBytesProcessed, uint64(len(bl.Raw)))
			CurrentBlockHeight++

		case <-time.After(100 * time.Millisecond):
			COUNTER("IDLE")
			TheBlockChain.Unspent.Idle()
		}
	}
	TheBlockChain.Sync()
}