示例#1
0
文件: hdrs.go 项目: vancsj/gocoin
func download_headers() {
	os.RemoveAll("tmp/")
	MemBlockChain = chain.NewChain("tmp/", TheBlockChain.BlockTreeEnd.BlockHash, false)
	defer os.RemoveAll("tmp/")

	MemBlockChain.Genesis = GenesisBlock
	*MemBlockChain.BlockTreeRoot = *TheBlockChain.BlockTreeEnd
	fmt.Println("Loaded chain has height", MemBlockChain.BlockTreeRoot.Height,
		MemBlockChain.BlockTreeRoot.BlockHash.String())

	get_headers()
	fmt.Println("AllHeadersDone after", time.Now().Sub(StartTime).String())

	AddrMutex.Lock()
	for !GlobalExit && len(AddrDatbase) < 60 {
		fmt.Println(len(AddrDatbase), "known peers at the moment - wait for more...")
		AddrMutex.Unlock()
		time.Sleep(3e9)
		AddrMutex.Lock()
	}
	AddrMutex.Unlock()

	BlocksToGet = make(map[uint32][32]byte, LastBlockHeight)
	for n := LastBlock.node; ; n = n.Parent {
		BlocksToGet[n.Height] = n.BlockHash.Hash
		if n.Height == TheBlockChain.BlockTreeEnd.Height {
			break
		}
	}

	close_all_connections()

	MemBlockChain.Close()
	MemBlockChain = nil
}
示例#2
0
文件: hdrs.go 项目: liudch/gocoin
func download_headers() {
	os.RemoveAll("tmp/")
	MemBlockChain = chain.NewChain("tmp/", TheBlockChain.BlockTreeEnd.BlockHash, false)
	defer os.RemoveAll("tmp/")

	MemBlockChain.Genesis = GenesisBlock
	*MemBlockChain.BlockTreeRoot = *TheBlockChain.BlockTreeEnd
	fmt.Println("Loaded chain has height", MemBlockChain.BlockTreeRoot.Height,
		MemBlockChain.BlockTreeRoot.BlockHash.String())

	atomic.StoreUint32(&LastStoredBlock, MemBlockChain.BlockTreeRoot.Height)

	fmt.Println("Downloading headers...")
	get_headers()

	if GlobalExit() {
		fmt.Println("Fetching headers aborted")
		return
	}
	fmt.Println("AllHeadersDone after", time.Now().Sub(StartTime).String())

	BlocksMutex.Lock()
	LastBlock.Mutex.Lock()
	BlocksToGet = make(map[uint32][32]byte, LastBlockHeight)
	for n := LastBlock.node; ; n = n.Parent {
		BlocksToGet[n.Height] = n.BlockHash.Hash
		if n.Height == TheBlockChain.BlockTreeEnd.Height {
			break
		}
	}
	LastBlockHeight = LastBlock.node.Height
	LastBlock.Mutex.Unlock()
	BlocksMutex.Unlock()

	SetAllHeadersDone(true)
	mark_all_hdrs_done()

	MemBlockChainMutex.Lock()
	MemBlockChain.Close()
	MemBlockChain = nil
	MemBlockChainMutex.Unlock()
}
示例#3
0
文件: main.go 项目: vancsj/gocoin
func open_blockchain() (abort bool) {
	// Disable Ctrl+C
	killchan := make(chan os.Signal)
	signal.Notify(killchan, os.Interrupt, os.Kill)
	fmt.Println("Opening blockchain... (Ctrl-C to interrupt)")
	__exit := make(chan bool)
	go func() {
		for {
			select {
			case s := <-killchan:
				fmt.Println(s)
				abort = true
				chain.AbortNow = true
			case <-__exit:
				return
			}
		}
	}()
	TheBlockChain = chain.NewChain(GocoinHomeDir, GenesisBlock, false)
	__exit <- true
	return
}
示例#4
0
func import_blockchain(dir string) {
	BlockDatabase := blockdb.NewBlockDB(dir, Magic)
	chain := chain.NewChain(GocoinHomeDir, GenesisBlock, false)

	var bl *btc.Block
	var er error
	var dat []byte
	var totbytes, perbytes uint64

	chain.DoNotSync = true

	fmt.Println("Be patient while importing Satoshi's database... ")
	start := time.Now().UnixNano()
	prv := start
	for {
		now := time.Now().UnixNano()
		if now-prv >= 10e9 {
			stat(now-start, now-prv, totbytes, perbytes, chain.BlockTreeEnd.Height)
			prv = now // show progress each 10 seconds
			perbytes = 0
		}

		dat, er = BlockDatabase.FetchNextBlock()
		if dat == nil || er != nil {
			println("END of DB file")
			break
		}

		bl, er = btc.NewBlock(dat[:])
		if er != nil {
			println("Block inconsistent:", er.Error())
			break
		}

		bl.Trusted = Trust

		er, _, _ = chain.CheckBlock(bl)

		if er != nil {
			if er.Error() != "Genesis" {
				println("CheckBlock failed:", er.Error())
				os.Exit(1) // Such a thing should not happen, so let's better abort here.
			}
			continue
		}

		er = chain.AcceptBlock(bl)
		if er != nil {
			println("AcceptBlock failed:", er.Error())
			os.Exit(1) // Such a thing should not happen, so let's better abort here.
		}

		totbytes += uint64(len(bl.Raw))
		perbytes += uint64(len(bl.Raw))
	}

	stop := time.Now().UnixNano()
	stat(stop-start, stop-prv, totbytes, perbytes, chain.BlockTreeEnd.Height)

	fmt.Println("Satoshi's database import finished in", (stop-start)/1e9, "seconds")

	fmt.Println("Now saving the new database...")
	chain.Save()
	chain.Close()
	fmt.Println("Database saved. No more imports should be needed.")
}