Пример #1
0
func download_headers() {
	os.RemoveAll("tmp/")
	MemBlockChain = btc.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
func download_headers() {
	os.RemoveAll("tmp/")
	MemBlockChain = btc.NewChain("tmp/", TheBlockChain.BlockTreeEnd.BlockHash, false)
	defer os.RemoveAll("tmp/")

	*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())

	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
}
Пример #3
0
func host_init() {
	BtcRootDir := BitcoinHome()

	if *datadir == "" {
		GocoinHomeDir = BtcRootDir + "gocoin/"
	} else {
		GocoinHomeDir = *datadir + "/"
	}

	if *testnet { // testnet3
		DefaultTcpPort = 18333
		GenesisBlock = btc.NewUint256FromString("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943")
		Magic = [4]byte{0x0B, 0x11, 0x09, 0x07}
		GocoinHomeDir += "tstnet/"
		AddrVersion = 0x6f
		BtcRootDir += "testnet3/"
	} else {
		DefaultTcpPort = 8333
		GenesisBlock = btc.NewUint256FromString("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")
		Magic = [4]byte{0xF9, 0xBE, 0xB4, 0xD9}
		GocoinHomeDir += "btcnet/"
		AddrVersion = 0x00
	}

	fi, e := os.Stat(GocoinHomeDir + "blockchain.idx")
	if e != nil || fi.Size() < 100 {
		os.RemoveAll(GocoinHomeDir)
		fmt.Println("You seem to be running Gocoin for the fist time on this PC")
		fi, e = os.Stat(BtcRootDir + "blocks/blk00000.dat")
		if e == nil && fi.Size() > 1024*1024 {
			fmt.Println("There is a database from Satoshi client on your disk...")
			if ask_yes_no("Go you want to import this database into Gocoin?") {
				import_blockchain(BtcRootDir + "blocks")
			}
		}
	}

	fmt.Println("Opening blockchain...")
	sta := time.Now().UnixNano()
	BlockChain = btc.NewChain(GocoinHomeDir, GenesisBlock, *rescan)
	sto := time.Now().UnixNano()
	fmt.Printf("Blockchain open in %.3f seconds\n", float64(sto-sta)/1e9)
	if *nosync {
		BlockChain.DoNotSync = true
		fmt.Println("Syncing is disabled. Switch it on with 'sync' command")
	}
}
Пример #4
0
func main() {
	StartTime = time.Now()

	GenesisBlock := btc.NewUint256FromString("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")
	Magic = [4]byte{0xF9, 0xBE, 0xB4, 0xD9}
	GocoinHomeDir = "btcnet" + string(os.PathSeparator)

	BlockChain = btc.NewChain(GocoinHomeDir, GenesisBlock, false)
	if btc.AbortNow || BlockChain == nil {
		return
	}

	new_connection("129.132.230.71")
	//new_connection("198.12.127.2")
	//new_connection("85.17.239.32")
	//new_connection("94.23.228.130")
	//new_connection("129.132.230.75")
	//new_connection("178.63.63.214")
	get_headers()
	return
}
Пример #5
0
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
				btc.AbortNow = true
			case <-__exit:
				return
			}
		}
	}()
	TheBlockChain = btc.NewChain(GocoinHomeDir, GenesisBlock, false)
	__exit <- true
	return
}
Пример #6
0
func host_init() {
	var e error
	BtcRootDir := utils.BitcoinHome()

	if common.CFG.Datadir == "" {
		common.GocoinHomeDir = BtcRootDir + "gocoin" + string(os.PathSeparator)
	} else {
		common.GocoinHomeDir = common.CFG.Datadir + string(os.PathSeparator)
	}

	if common.CFG.Testnet { // testnet3
		common.GenesisBlock = btc.NewUint256FromString("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943")
		common.Magic = [4]byte{0x0B, 0x11, 0x09, 0x07}
		common.GocoinHomeDir += "tstnet" + string(os.PathSeparator)
		common.AddrVersion = 0x6f
		BtcRootDir += "testnet3" + string(os.PathSeparator)
		network.AlertPubKey, _ = hex.DecodeString("04302390343f91cc401d56d68b123028bf52e5fca1939df127f63c6467cdf9c8e2c14b61104cf817d0b780da337893ecc4aaff1309e536162dabbdb45200ca2b0a")
		common.MaxPeersNeeded = 100
	} else {
		common.GenesisBlock = btc.NewUint256FromString("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")
		common.Magic = [4]byte{0xF9, 0xBE, 0xB4, 0xD9}
		common.GocoinHomeDir += "btcnet" + string(os.PathSeparator)
		common.AddrVersion = 0x00
		network.AlertPubKey, _ = hex.DecodeString("04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284")
		common.MaxPeersNeeded = 1000
	}

	// Lock the folder
	os.MkdirAll(common.GocoinHomeDir, 0770)
	os.MkdirAll(common.GocoinHomeDir+"wallet", 0770)
	LockDatabaseDir()

	fi, e := os.Stat(common.GocoinHomeDir + "blockchain.idx")
	if e != nil {
		os.RemoveAll(common.GocoinHomeDir)
		fmt.Println("You seem to be running Gocoin for the fist time on this PC")
		fi, e = os.Stat(BtcRootDir + "blocks/blk00000.dat")
		if e == nil && fi.Size() > 1024*1024 {
			fmt.Println("There is a database from Satoshi client on your disk...")
			if textui.AskYesNo("Do you want to import this database into Gocoin?") {
				import_blockchain(BtcRootDir + "blocks")
			}
		}
	}

	fmt.Println("Opening blockchain... (Ctrl-C to interrupt)")

	__exit := make(chan bool)
	__done := make(chan bool)
	go func() {
		for {
			select {
			case s := <-killchan:
				fmt.Println(s)
				btc.AbortNow = true
			case <-__exit:
				__done <- true
				return
			}
		}
	}()
	sta := time.Now().UnixNano()
	common.BlockChain = btc.NewChain(common.GocoinHomeDir, common.GenesisBlock, common.FLAG.Rescan)
	sto := time.Now().UnixNano()
	if btc.AbortNow {
		fmt.Printf("Blockchain opening aborted after %.3f seconds\n", float64(sto-sta)/1e9)
		UnlockDatabaseDir()
		os.Exit(1)
	}
	fmt.Printf("Blockchain open in %.3f seconds\n", float64(sto-sta)/1e9)
	common.BlockChain.Unspent.SetTxNotify(wallet.TxNotify)
	common.StartTime = time.Now()
	__exit <- true
	_ = <-__done
}
Пример #7
0
func import_blockchain(dir string) {
	trust := !textui.AskYesNo("Do you want to verify scripts while importing (will be slow)?")

	BlockDatabase := blockdb.NewBlockDB(dir, common.Magic)
	chain := btc.NewChain(common.GocoinHomeDir, common.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.Sync()
	chain.Save()
	chain.Close()
	fmt.Println("Database saved. No more imports should be needed.")
	fmt.Println("It is advised to close and restart the node now, to free some mem.")
}
Пример #8
0
func import_blockchain(dir string) {
	trust := !ask_yes_no("Go you want to verify scripts while importing (will be slow)?")

	BlockDatabase := blockdb.NewBlockDB(dir, Magic)
	chain := btc.NewChain(GocoinHomeDir, GenesisBlock, false)

	var bl *btc.Block
	var er error
	var dat []byte
	var totbytes 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 {
			prv = now // show progress each 10 seconds
			stat(now-start, totbytes, chain.BlockTreeEnd.Height)
		}

		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 {
			println("CheckBlock failed:", er.Error())
			continue
		}

		er = chain.AcceptBlock(bl)
		if er != nil {
			println("AcceptBlock failed:", er.Error())
			continue
		}

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

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

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

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