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() }
// This function either appends a new block at the end of the existing chain // in which case it also applies all the transactions to the unspent database. // If the block does is not the heighest, it is added to the chain, but maked // as an orphan - its transaction will be verified only if the chain would swap // to its branch later on. func (ch *Chain) AcceptBlock(bl *btc.Block) (e error) { prevblk, ok := ch.BlockIndex[btc.NewUint256(bl.ParentHash()).BIdx()] if !ok { panic("This should not happen") } // create new BlockTreeNode cur := new(BlockTreeNode) cur.BlockHash = bl.Hash cur.Parent = prevblk cur.Height = prevblk.Height + 1 cur.BlockSize = uint32(len(bl.Raw)) cur.TxCount = uint32(bl.TxCount) copy(cur.BlockHeader[:], bl.Raw[:80]) // Add this block to the block index ch.BlockIndexAccess.Lock() prevblk.addChild(cur) ch.BlockIndex[cur.BlockHash.BIdx()] = cur ch.BlockIndexAccess.Unlock() if ch.BlockTreeEnd == prevblk { // The head of out chain - apply the transactions var changes *BlockChanges changes, e = ch.ProcessBlockTransactions(bl, cur.Height, bl.LastKnownHeight) if e != nil { // ProcessBlockTransactions failed, so trash the block. println("ProcessBlockTransactions ", cur.BlockHash.String(), cur.Height, e.Error()) ch.BlockIndexAccess.Lock() cur.Parent.delChild(cur) delete(ch.BlockIndex, cur.BlockHash.BIdx()) ch.BlockIndexAccess.Unlock() } else { // ProcessBlockTransactions succeeded, so save the block as "trusted". bl.Trusted = true ch.Blocks.BlockAdd(cur.Height, bl) // Apply the block's trabnsactions to the unspent database: ch.Unspent.CommitBlockTxs(changes, bl.Hash.Hash[:]) if !ch.DoNotSync { ch.Blocks.Sync() } ch.BlockTreeEnd = cur // Advance the head } } else { // The block's parent is not the current head of the chain... // Save the block, though do not makt it as "trusted" just yet ch.Blocks.BlockAdd(cur.Height, bl) // If it has a bigger height than the current head, // ... move the coin state into a new branch. if cur.Height > ch.BlockTreeEnd.Height { ch.MoveToBlock(cur) } } return }
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 := chain.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.") }