github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/client/init.go (about) 1 package main 2 3 import ( 4 "crypto/rand" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "time" 9 10 "github.com/piotrnar/gocoin/client/common" 11 "github.com/piotrnar/gocoin/lib/btc" 12 "github.com/piotrnar/gocoin/lib/chain" 13 "github.com/piotrnar/gocoin/lib/others/sys" 14 ) 15 16 func host_init() { 17 common.GocoinHomeDir = common.CFG.Datadir + string(os.PathSeparator) 18 19 common.Testnet = common.CFG.Testnet // So chaging this value would will only affect the behaviour after restart 20 if common.CFG.Testnet { // testnet3 21 common.GenesisBlock = btc.NewUint256FromString("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943") 22 common.Magic = [4]byte{0x0B, 0x11, 0x09, 0x07} 23 common.GocoinHomeDir += common.DataSubdir() + string(os.PathSeparator) 24 } else { 25 common.GenesisBlock = btc.NewUint256FromString("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f") 26 common.Magic = [4]byte{0xF9, 0xBE, 0xB4, 0xD9} 27 common.GocoinHomeDir += common.DataSubdir() + string(os.PathSeparator) 28 } 29 30 // Lock the folder 31 os.MkdirAll(common.GocoinHomeDir, 0770) 32 sys.LockDatabaseDir(common.GocoinHomeDir) 33 34 common.SecretKey, _ = ioutil.ReadFile(common.GocoinHomeDir + "authkey") 35 if len(common.SecretKey) != 32 { 36 common.SecretKey = make([]byte, 32) 37 rand.Read(common.SecretKey) 38 ioutil.WriteFile(common.GocoinHomeDir+"authkey", common.SecretKey, 0600) 39 } 40 common.PublicKey = btc.Encodeb58(btc.PublicFromPrivate(common.SecretKey, true)) 41 fmt.Println("Public auth key:", "@"+common.PublicKey) 42 43 __exit := make(chan bool) 44 __done := make(chan bool) 45 go func() { 46 for { 47 select { 48 case s := <-common.KillChan: 49 fmt.Println(s) 50 chain.AbortNow = true 51 case <-__exit: 52 __done <- true 53 return 54 } 55 } 56 }() 57 58 if chain.AbortNow { 59 sys.UnlockDatabaseDir() 60 os.Exit(1) 61 } 62 63 fmt.Print(string(common.LogBuffer.Bytes())) 64 common.LogBuffer = nil 65 66 if btc.EC_Verify == nil { 67 fmt.Println("Using native secp256k1 lib for EC_Verify (consider installing a speedup)") 68 } 69 70 ext := &chain.NewChanOpts{ 71 UTXOVolatileMode: common.FLAG.VolatileUTXO, 72 UndoBlocks: common.FLAG.UndoBlocks, 73 BlockMinedCB: blockMined, BlockUndoneCB: blockUndone, 74 DoNotRescan: true, CompressUTXO: common.CFG.UTXOSave.CompressRecords} 75 76 if ext.UndoBlocks > 0 { 77 ext.BlockUndoneCB = nil // Do not call the callback if undoing blocks as it will panic 78 } 79 80 if common.Testnet { 81 ext.UTXOPrealloc = 19e6 // Around block #2600k 82 } else { 83 ext.UTXOPrealloc = 120e6 // Around block #838k 84 } 85 86 sta := time.Now() 87 common.BlockChain = chain.NewChainExt(common.GocoinHomeDir, common.GenesisBlock, common.FLAG.Rescan, ext, 88 &chain.BlockDBOpts{ 89 MaxCachedBlocks: int(common.CFG.Memory.MaxCachedBlks), 90 MaxDataFileSize: uint64(common.CFG.Memory.MaxDataFileMB) << 20, 91 DataFilesKeep: common.CFG.Memory.DataFilesKeep, 92 DataFilesBackup: common.CFG.Memory.OldDataBackup, 93 CompressOnDisk: common.CFG.Memory.CompressBlockDB}) 94 if chain.AbortNow { 95 fmt.Printf("Blockchain opening aborted after %s seconds\n", time.Now().Sub(sta).String()) 96 common.BlockChain.Close() 97 sys.UnlockDatabaseDir() 98 os.Exit(1) 99 } 100 101 if lb, _ := common.BlockChain.BlockTreeRoot.FindFarthestNode(); lb.Height > common.BlockChain.LastBlock().Height { 102 common.Last.ParseTill = lb 103 } 104 105 common.Last.Block = common.BlockChain.LastBlock() 106 common.Last.Time = time.Unix(int64(common.Last.Block.Timestamp()), 0) 107 if common.Last.Time.After(time.Now()) { 108 common.Last.Time = time.Now() 109 } 110 common.UpdateScriptFlags(0) 111 112 common.LockCfg() 113 common.ApplyLastTrustedBlock() 114 common.UnlockCfg() 115 116 if common.CFG.Memory.FreeAtStart { 117 fmt.Print("Freeing memory... ") 118 sys.FreeMem() 119 fmt.Print("\r \r") 120 } 121 sto := time.Now() 122 123 al, sy := sys.MemUsed() 124 by, _ := common.MemUsed() 125 fmt.Printf("Blockchain open in %s. %d + %d MB of RAM used (%d)\n", 126 sto.Sub(sta).String(), al>>20, by>>20, sy>>20) 127 fmt.Println("Highest known block is", common.Last.Block.Height, "from", 128 common.Last.Time.Format("2006-01-02 15:04:05")) 129 130 common.StartTime = time.Now() 131 __exit <- true 132 _ = <-__done 133 134 }