func get_blocks() { var bl *btc.Block BlocksInProgress = make(map[[32]byte]*one_bip) BlocksCached = make(map[uint32]*btc.Block) //fmt.Println("opening connections") DlStartTime = time.Now() BlocksComplete = TheBlockChain.BlockTreeEnd.Height BlocksIndex = BlocksComplete SetDoBlocks(true) ct := time.Now().Unix() lastdrop := ct laststat := ct TheBlockChain.DoNotSync = true var blks2do []*btc.Block for GetDoBlocks() { BlocksMutex.Lock() if BlocksComplete >= LastBlockHeight { BlocksMutex.Unlock() break } for { bl = BlocksCached[BlocksComplete+1] if bl == nil { break } BlocksComplete++ if BlocksComplete > BlocksIndex { BlocksIndex = BlocksComplete } bl.Trusted = BlocksComplete <= TrustUpTo if OnlyStoreBlocks { TheBlockChain.Blocks.BlockAdd(BlocksComplete, bl) } else { blks2do = append(blks2do, bl) } atomic.AddUint64(&DlBytesProcesses, uint64(len(bl.Raw))) delete(BlocksCached, BlocksComplete) BlocksCachedSize -= uint(len(bl.Raw)) } BlocksMutex.Unlock() if len(blks2do) > 0 { for idx := range blks2do { er, _, _ := TheBlockChain.CheckBlock(blks2do[idx]) if er != nil { fmt.Println(er.Error()) return } blks2do[idx].LastKnownHeight = BlocksComplete TheBlockChain.AcceptBlock(blks2do[idx]) } blks2do = nil } else { TheBlockChain.Unspent.Idle() COUNTER("IDLE") } time.Sleep(1e8) ct = time.Now().Unix() if open_connection_count() > MaxNetworkConns { drop_slowest_peers() } else { // drop slowest peers once for awhile occ := MaxNetworkConns if occ > 0 { occ = 1200 / occ // For 20 open connections: drop one per minute if occ < 3 { occ = 3 // .. drop not more often then once sper 3 seconds } if ct-lastdrop > int64(occ) { lastdrop = ct drop_slowest_peers() } } } add_new_connections() if ct-laststat >= 5 { laststat = ct print_stats() usif_prompt() } } }
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.") }
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.") }