func (g *Gateway) save() error { var nodes []modules.NetAddress for node := range g.nodes { nodes = append(nodes, node) } return persist.SaveFile(persistMetadata, nodes, filepath.Join(g.persistDir, "nodes.json")) }
// save stores the current renter data to disk. func (r *Renter) save() error { var files []file for _, file := range r.files { files = append(files, *file) } return persist.SaveFile(saveMetadata, files, filepath.Join(r.saveDir, PersistFilename)) }
// save stores all of the persist data to disk. func (h *Host) save() error { p := persistence{ // RPC Metrics. ErroredCalls: atomic.LoadUint64(&h.atomicErroredCalls), UnrecognizedCalls: atomic.LoadUint64(&h.atomicUnrecognizedCalls), DownloadCalls: atomic.LoadUint64(&h.atomicDownloadCalls), RenewCalls: atomic.LoadUint64(&h.atomicRenewCalls), ReviseCalls: atomic.LoadUint64(&h.atomicReviseCalls), SettingsCalls: atomic.LoadUint64(&h.atomicSettingsCalls), UploadCalls: atomic.LoadUint64(&h.atomicUploadCalls), // Consensus Tracking. BlockHeight: h.blockHeight, RecentChange: h.recentChange, // Host Identity. NetAddress: h.netAddress, PublicKey: h.publicKey, SecretKey: h.secretKey, // File Management. Obligations: h.getObligations(), // Statistics. FileCounter: h.fileCounter, LostRevenue: h.lostRevenue, Revenue: h.revenue, // Utilities. Settings: h.settings, } return persist.SaveFile(persistMetadata, p, filepath.Join(h.persistDir, settingsFile)) }
// TestPersistCompat04 checks that the compatibility loader for version 0.4.x // obligations is functioning. func TestPersistCompat04(t *testing.T) { if testing.Short() { t.SkipNow() } ht, err := newHostTester("TestPersistCompat04") if err != nil { t.Fatal(err) } // Upload a file and then save the host as a compatibility host. _, err = ht.uploadFile("TestPersistCompat04 - 1", renewDisabled) if err != nil { t.Fatal(err) } // Mine a block so that the file contract ends up in the blockchain. _, err = ht.miner.AddBlock() if err != nil { t.Fatal(err) } err = ht.host.Close() if err != nil { t.Fatal(err) } c04h := ht.buildCompat04Host() if err != nil { t.Fatal(err) } // Save the compatibility file, replacing the usual file with an old // format. err = persist.SaveFile(compat04Metadata, c04h, filepath.Join(ht.host.persistDir, settingsFile)) if err != nil { t.Fatal(err) } // Re-open the host, which will be loading from the compatibility file. rebootHost, err := New(ht.cs, ht.tpool, ht.wallet, ":0", filepath.Join(ht.persistDir, modules.HostDir)) if err != nil { t.Fatal(err) } if len(rebootHost.obligationsByID) != 1 { t.Fatal(len(rebootHost.obligationsByID)) } // Mine until the storage proof goes through, and the obligation gets // cleared. for i := types.BlockHeight(0); i <= testUploadDuration+confirmationRequirement+defaultWindowSize; i++ { _, err := ht.miner.AddBlock() if err != nil { t.Fatal(err) } } if len(rebootHost.obligationsByID) != 0 { t.Error("obligations did not clear") } if rebootHost.revenue.IsZero() { t.Error("host is reporting no revenue after doing a compatibility storage proof") } }
// save saves the hostdb persistence data to disk. func (hdb *HostDB) save() error { var data struct { Contracts []hostContract } for _, hc := range hdb.contracts { data.Contracts = append(data.Contracts, hc) } return persist.SaveFile(saveMetadata, data, filepath.Join(hdb.persistDir, persistFilename)) }
// save stores the current renter data to disk. func (r *Renter) save() error { data := struct { Contracts map[string]types.FileContract Entropy [32]byte }{make(map[string]types.FileContract), r.entropy} // Convert renter's contract map to a JSON-friendly type. for id, fc := range r.contracts { b, _ := id.MarshalJSON() data.Contracts[string(b)] = fc } return persist.SaveFile(saveMetadata, data, filepath.Join(r.persistDir, PersistFilename)) }
func (h *Host) save() error { sHost := savedHost{ SpaceRemaining: h.spaceRemaining, FileCounter: h.fileCounter, Profit: h.profit, HostSettings: h.HostSettings, Obligations: make([]contractObligation, 0, len(h.obligationsByID)), } for _, obligation := range h.obligationsByID { sHost.Obligations = append(sHost.Obligations, obligation) } return persist.SaveFile(persistMetadata, sHost, filepath.Join(h.saveDir, "settings.json")) }
// recoverSeed integrates a recovery seed into the wallet. func (w *Wallet) recoverSeed(masterKey crypto.TwofishKey, seed modules.Seed) error { // Check that the seed is not already known. for _, wSeed := range w.seeds { if seed == wSeed { return errKnownSeed } } // Encrypt the seed and save the seed file. var sfuid SeedFileUID _, err := rand.Read(sfuid[:]) if err != nil { return err } sek := seedFileEncryptionKey(masterKey, sfuid) plaintextVerification := make([]byte, encryptionVerificationLen) encryptionVerification, err := sek.EncryptBytes(plaintextVerification) if err != nil { return err } cryptSeed, err := sek.EncryptBytes(seed[:]) if err != nil { return err } seedFilename := filepath.Join(w.persistDir, seedFilePrefix+persist.RandomSuffix()+seedFileSuffix) seedFile := SeedFile{ SeedFileUID: sfuid, EncryptionVerification: encryptionVerification, Seed: cryptSeed, } err = persist.SaveFile(seedMetadata, seedFile, seedFilename) if err != nil { return err } // Add the seed file to the wallet's set of tracked seeds and save the // wallet settings. w.settings.AuxiliarySeedFiles = append(w.settings.AuxiliarySeedFiles, seedFile) err = w.saveSettings() if err != nil { return err } w.integrateSeed(seed) return nil }
func (h *Host) save() error { sHost := savedHost{ SpaceRemaining: h.spaceRemaining, FileCounter: h.fileCounter, Profit: h.profit, HostSettings: h.HostSettings, Obligations: make([]contractObligation, 0, len(h.obligationsByID)), SecretKey: h.secretKey, PublicKey: h.publicKey, } for _, ob := range h.obligationsByID { // to avoid race conditions involving the obligation's mutex, copy it // manually into a new object obcopy := contractObligation{ID: ob.ID, FileContract: ob.FileContract, LastRevisionTxn: ob.LastRevisionTxn} sHost.Obligations = append(sHost.Obligations, obcopy) } return persist.SaveFile(persistMetadata, sHost, filepath.Join(h.persistDir, "settings.json")) }
// createSeed creates a wallet seed and encrypts it using a key derived from // the master key, then addds it to the wallet as the primary seed, while // making a disk backup. func (w *Wallet) createSeed(masterKey crypto.TwofishKey, seed modules.Seed) error { // Derive the key used to encrypt the seed file, and create the encryption // verification object. var sfuid SeedFileUID _, err := rand.Read(sfuid[:]) if err != nil { return err } sfek := seedFileEncryptionKey(masterKey, sfuid) plaintextVerification := make([]byte, encryptionVerificationLen) encryptionVerification, err := sfek.EncryptBytes(plaintextVerification) if err != nil { return err } // Encrypt the seed and save the seed file. seedName := seedFilePrefix + persist.RandomSuffix() + seedFileSuffix filename := filepath.Join(w.persistDir, seedName) cryptSeed, err := sfek.EncryptBytes(seed[:]) if err != nil { return err } w.primarySeed = seed w.settings.PrimarySeedFile = SeedFile{ SeedFileUID: sfuid, EncryptionVerification: encryptionVerification, Seed: cryptSeed, } w.settings.PrimarySeedProgress = 0 err = persist.SaveFile(seedMetadata, &w.settings.PrimarySeedFile, filename) if err != nil { return err } err = w.saveSettings() if err != nil { return err } return nil }
// encryptAndSaveSeedFile encrypts and saves a seed file. func (w *Wallet) encryptAndSaveSeedFile(masterKey crypto.TwofishKey, seed modules.Seed) (SeedFile, error) { var sf SeedFile _, err := rand.Read(sf.UID[:]) if err != nil { return SeedFile{}, err } sek := uidEncryptionKey(masterKey, sf.UID) plaintextVerification := make([]byte, encryptionVerificationLen) sf.EncryptionVerification, err = sek.EncryptBytes(plaintextVerification) if err != nil { return SeedFile{}, err } sf.Seed, err = sek.EncryptBytes(seed[:]) if err != nil { return SeedFile{}, err } seedFilename := filepath.Join(w.persistDir, seedFilePrefix+persist.RandomSuffix()+seedFileSuffix) err = persist.SaveFile(seedMetadata, sf, seedFilename) if err != nil { return SeedFile{}, err } return sf, nil }
func (p *stdPersist) save(data contractorPersist) error { return persist.SaveFile(p.meta, data, p.filename) }
// saveSettings writes the wallet's settings to the wallet's settings file, // replacing the existing file. func (w *Wallet) saveSettings() error { return persist.SaveFile(settingsMetadata, w.persist, filepath.Join(w.persistDir, settingsFile)) }
// save stores all of the persistent data of the storage manager to disk. func (sm *StorageManager) save() error { return persist.SaveFile(persistMetadata, sm.persistData(), filepath.Join(sm.persistDir, settingsFile)) }
// createBackup creates a backup file at the desired filepath. func (w *Wallet) createBackup(backupFilepath string) error { return persist.SaveFile(settingsMetadata, w.persist, backupFilepath) }
// save stores the current renter data to disk. func (r *Renter) save() error { data := struct { Tracking map[string]trackedFile }{r.tracking} return persist.SaveFile(saveMetadata, data, filepath.Join(r.persistDir, PersistFilename)) }
// save stores the Gateway's persistent data on disk. func (g *Gateway) save() error { return persist.SaveFile(persistMetadata, g.persistData(), filepath.Join(g.persistDir, nodesFile)) }
// save saves the miner persistence to disk. func (m *Miner) save() error { return persist.SaveFile(settingsMetadata, m.persist, filepath.Join(m.persistDir, settingsFile)) }
// save stores all of the persist data to disk. func (h *Host) save() error { return persist.SaveFile(persistMetadata, h.persistData(), filepath.Join(h.persistDir, settingsFile)) }