예제 #1
0
파일: persist.go 프로젝트: mantyr/Sia
func (h *Host) load() error {
	var sHost savedHost
	err := persist.LoadFile(persistMetadata, &sHost, filepath.Join(h.persistDir, "settings.json"))
	if err != nil {
		return err
	}

	h.spaceRemaining = sHost.HostSettings.TotalStorage
	h.fileCounter = sHost.FileCounter
	h.HostSettings = sHost.HostSettings
	h.profit = sHost.Profit
	// recreate maps
	for i := range sHost.Obligations {
		obligation := &sHost.Obligations[i] // both maps should use same pointer
		height := obligation.FileContract.WindowStart + StorageProofReorgDepth
		h.obligationsByHeight[height] = append(h.obligationsByHeight[height], obligation)
		h.obligationsByID[obligation.ID] = obligation
		// update spaceRemaining
		h.spaceRemaining -= int64(obligation.LastRevisionTxn.FileContractRevisions[0].NewFileSize)
	}
	h.secretKey = sHost.SecretKey
	h.publicKey = sHost.PublicKey

	return nil
}
예제 #2
0
파일: persist.go 프로젝트: mantyr/Sia
// load fetches the saved renter data from disk.
func (r *Renter) load() error {
	// Load all files found in renter directory.
	dir, err := os.Open(r.persistDir) // TODO: store in a subdir?
	if err != nil {
		return err
	}
	defer dir.Close()
	filenames, err := dir.Readdirnames(-1)
	if err != nil {
		return err
	}
	for _, path := range filenames {
		// Skip non-sia files.
		if filepath.Ext(path) != ShareExtension {
			continue
		}
		file, err := os.Open(filepath.Join(r.persistDir, path))
		if err != nil {
			// maybe just skip?
			return err
		}
		_, err = r.loadSharedFiles(file)
		file.Close() // defer is probably a bad idea
		if err != nil {
			// maybe just skip?
			return err
		}
	}

	// Load contracts, repair set, and entropy.
	data := struct {
		Contracts map[string]types.FileContract
		Tracking  map[string]trackedFile
		Repairing map[string]string // COMPATv0.4.8
		Entropy   [32]byte
	}{}
	err = persist.LoadFile(saveMetadata, &data, filepath.Join(r.persistDir, PersistFilename))
	if err != nil {
		return err
	}
	if data.Tracking != nil {
		r.tracking = data.Tracking
	} else if data.Repairing != nil {
		// COMPATv0.4.8
		for nick, path := range data.Repairing {
			// these files will be renewed indefinitely
			r.tracking[nick] = trackedFile{RepairPath: path, EndHeight: 0}
		}
	}
	r.entropy = data.Entropy
	var fcid types.FileContractID
	for id, fc := range data.Contracts {
		fcid.UnmarshalJSON([]byte(id))
		r.contracts[fcid] = fc
	}

	return nil
}
예제 #3
0
파일: persist.go 프로젝트: Mingling94/Sia
// load fetches the saved renter data from disk.
func (r *Renter) load() error {
	// Recursively load all files found in renter directory. Errors
	// encountered during loading are logged, but are not considered fatal.
	err := filepath.Walk(r.persistDir, func(path string, info os.FileInfo, err error) error {
		// This error is non-nil if filepath.Walk couldn't stat a file or
		// folder. This generally indicates a serious error.
		if err != nil {
			return err
		}

		// Skip folders and non-sia files.
		if info.IsDir() || filepath.Ext(path) != ShareExtension {
			return nil
		}

		// Open the file.
		file, err := os.Open(path)
		if err != nil {
			r.log.Println("ERROR: could not open .sia file:", err)
			return nil
		}
		defer file.Close()

		// Load the file contents into the renter.
		_, err = r.loadSharedFiles(file)
		if err != nil {
			r.log.Println("ERROR: could not load .sia file:", err)
			return nil
		}
		return nil
	})
	if err != nil {
		return err
	}

	// Load contracts, repair set, and entropy.
	data := struct {
		Tracking  map[string]trackedFile
		Repairing map[string]string // COMPATv0.4.8
	}{}
	err = persist.LoadFile(saveMetadata, &data, filepath.Join(r.persistDir, PersistFilename))
	if err != nil {
		return err
	}
	if data.Tracking != nil {
		r.tracking = data.Tracking
	} else if data.Repairing != nil {
		// COMPATv0.4.8
		for nick, path := range data.Repairing {
			// these files will be renewed indefinitely
			r.tracking[nick] = trackedFile{RepairPath: path, Renew: true}
		}
	}

	return nil
}
예제 #4
0
파일: persist.go 프로젝트: cfromknecht/Sia
func (g *Gateway) load() error {
	var nodes []modules.NetAddress
	err := persist.LoadFile(persistMetadata, &nodes, filepath.Join(g.persistDir, "nodes.json"))
	if err != nil {
		return err
	}
	for _, node := range nodes {
		g.addNode(node)
	}
	return nil
}
예제 #5
0
파일: persist.go 프로젝트: mm3/Sia
// load fetches the saved renter data from disk.
func (r *Renter) load() error {
	var files []file
	err := persist.LoadFile(saveMetadata, &files, filepath.Join(r.saveDir, PersistFilename))
	if err != nil {
		return err
	}
	for i := range files {
		files[i].renter = r
		r.files[files[i].Name] = &files[i]
	}
	return nil
}
예제 #6
0
파일: persist.go 프로젝트: Mingling94/Sia
// load extrats the save data from disk and populates the host.
func (h *Host) load() error {
	p := new(persistence)
	err := persist.LoadFile(persistMetadata, p, filepath.Join(h.persistDir, "settings.json"))
	if err == persist.ErrBadVersion {
		// COMPATv0.4.8 - try the compatibility loader.
		return h.compatibilityLoad()
	} else if os.IsNotExist(err) {
		// This is the host's first run, set up the default values.
		return h.establishDefaults()
	} else if err != nil {
		return err
	}

	// Copy over consensus tracking.
	h.blockHeight = p.BlockHeight
	h.recentChange = p.RecentChange

	// Copy over host identity.
	h.netAddress = p.NetAddress
	h.publicKey = p.PublicKey
	h.secretKey = p.SecretKey

	// Copy over the file management. The space remaining is recalculated from
	// disk instead of being saved, to maximize the potential usefulness of
	// restarting Sia as a means of eliminating unkonwn errors.
	h.fileCounter = p.FileCounter
	h.spaceRemaining = p.Settings.TotalStorage
	h.loadObligations(p.Obligations)

	// Copy over statistics.
	h.revenue = p.Revenue
	h.lostRevenue = p.LostRevenue

	// Copy over rpc tracking.
	atomic.StoreUint64(&h.atomicErroredCalls, p.ErroredCalls)
	atomic.StoreUint64(&h.atomicUnrecognizedCalls, p.UnrecognizedCalls)
	atomic.StoreUint64(&h.atomicDownloadCalls, p.DownloadCalls)
	atomic.StoreUint64(&h.atomicRenewCalls, p.RenewCalls)
	atomic.StoreUint64(&h.atomicReviseCalls, p.ReviseCalls)
	atomic.StoreUint64(&h.atomicSettingsCalls, p.SettingsCalls)
	atomic.StoreUint64(&h.atomicUploadCalls, p.UploadCalls)

	// Utilities.
	h.settings = p.Settings

	// Subscribe to the consensus set.
	err = h.initConsensusSubscription()
	if err != nil {
		return err
	}

	return nil
}
예제 #7
0
파일: persist.go 프로젝트: cfromknecht/Sia
// load loads the hostdb persistence data from disk.
func (hdb *HostDB) load() error {
	var data struct {
		Contracts []hostContract
	}
	err := persist.LoadFile(saveMetadata, &data, filepath.Join(hdb.persistDir, persistFilename))
	if err != nil {
		return err
	}
	for _, hc := range data.Contracts {
		hdb.contracts[hc.ID] = hc
	}
	return nil
}
예제 #8
0
파일: persist.go 프로젝트: CSSZiegler/Sia
// load loads the Gateway's persistent data from disk.
func (g *Gateway) load() error {
	var nodes []modules.NetAddress
	err := persist.LoadFile(persistMetadata, &nodes, filepath.Join(g.persistDir, nodesFile))
	if err != nil {
		return err
	}
	for _, node := range nodes {
		err := g.addNode(node)
		if err != nil {
			g.log.Printf("WARN: error loading node '%v' from persist: %v", node, err)
		}
	}
	return nil
}
예제 #9
0
파일: persist.go 프로젝트: zoutaiqi/Sia
// load fetches the saved renter data from disk.
func (r *Renter) load() error {
	// Load all files found in renter directory.
	dir, err := os.Open(r.persistDir) // TODO: store in a subdir?
	if err != nil {
		return err
	}
	filenames, err := dir.Readdirnames(-1)
	if err != nil {
		return err
	}
	for _, path := range filenames {
		// Skip non-sia files.
		if filepath.Ext(path) != ShareExtension {
			continue
		}
		file, err := os.Open(filepath.Join(r.persistDir, path))
		if err != nil {
			// maybe just skip?
			return err
		}
		_, err = r.loadSharedFiles(file)
		if err != nil {
			// maybe just skip?
			return err
		}
	}

	// Load contracts, repair set, and entropy.
	data := struct {
		Contracts map[string]types.FileContract
		Repairing map[string]string
		Entropy   [32]byte
	}{}
	err = persist.LoadFile(saveMetadata, &data, filepath.Join(r.persistDir, PersistFilename))
	if err != nil {
		return err
	}
	r.repairSet = data.Repairing
	r.entropy = data.Entropy
	var fcid types.FileContractID
	for id, fc := range data.Contracts {
		fcid.UnmarshalJSON([]byte(id))
		r.contracts[fcid] = fc
	}

	return nil
}
예제 #10
0
파일: persist.go 프로젝트: mm3/Sia
func (h *Host) load() error {
	var sHost savedHost
	err := persist.LoadFile(persistMetadata, &sHost, filepath.Join(h.saveDir, "settings.json"))
	if err != nil {
		return err
	}

	h.spaceRemaining = sHost.HostSettings.TotalStorage
	h.fileCounter = sHost.FileCounter
	h.HostSettings = sHost.HostSettings
	h.profit = sHost.Profit
	// recreate maps
	for _, obligation := range sHost.Obligations {
		height := obligation.FileContract.WindowStart + StorageProofReorgDepth
		h.obligationsByHeight[height] = append(h.obligationsByHeight[height], obligation)
		h.obligationsByID[obligation.ID] = obligation
		// update spaceRemaining
		h.spaceRemaining -= int64(obligation.FileContract.FileSize)
	}

	return nil
}
예제 #11
0
// loadFile allows the host to load a persistence structure form disk.
func (productionDependencies) loadFile(m persist.Metadata, i interface{}, s string) error {
	return persist.LoadFile(m, i, s)
}
예제 #12
0
func (p *stdPersist) load(data *contractorPersist) error {
	return persist.LoadFile(p.meta, data, p.filename)
}
예제 #13
0
파일: persist.go 프로젝트: mantyr/Sia
// loadSettings reads the wallet's settings from the wallet's settings file,
// overwriting the settings object in memory. loadSettings should only be
// called at startup.
func (w *Wallet) loadSettings() error {
	return persist.LoadFile(settingsMetadata, &w.persist, filepath.Join(w.persistDir, settingsFile))
}
예제 #14
0
파일: persist.go 프로젝트: cfromknecht/Sia
// load loads the miner persistence from disk.
func (m *Miner) load() error {
	return persist.LoadFile(settingsMetadata, &m.persist, filepath.Join(m.persistDir, settingsFile))
}
예제 #15
0
파일: persist.go 프로젝트: zzmjohn/Sia
// load extrats the save data from disk and populates the host.
func (h *Host) load() error {
	p := new(persistence)
	err := persist.LoadFile(persistMetadata, p, filepath.Join(h.persistDir, "settings.json"))
	if err == persist.ErrBadVersion {
		// COMPATv0.4.8 - try the compatibility loader.
		return h.compatibilityLoad()
	} else if os.IsNotExist(err) {
		// There is no host.json file, set up sane defaults.
		return h.establishDefaults()
	} else if err != nil {
		return err
	}

	// Copy over rpc tracking.
	atomic.StoreUint64(&h.atomicErroredCalls, p.ErroredCalls)
	atomic.StoreUint64(&h.atomicUnrecognizedCalls, p.UnrecognizedCalls)
	atomic.StoreUint64(&h.atomicDownloadCalls, p.DownloadCalls)
	atomic.StoreUint64(&h.atomicRenewCalls, p.RenewCalls)
	atomic.StoreUint64(&h.atomicReviseCalls, p.ReviseCalls)
	atomic.StoreUint64(&h.atomicSettingsCalls, p.SettingsCalls)
	atomic.StoreUint64(&h.atomicUploadCalls, p.UploadCalls)

	// Copy over consensus tracking.
	h.blockHeight = p.BlockHeight
	h.recentChange = p.RecentChange

	// Copy over host identity.
	h.netAddress = p.NetAddress
	h.publicKey = p.PublicKey
	h.secretKey = p.SecretKey

	// Copy over statistics.
	h.revenue = p.Revenue
	h.lostRevenue = p.LostRevenue

	// Utilities.
	h.settings = p.Settings

	// Copy over the file management. The space remaining is recalculated from
	// disk instead of being saved, to maximize the potential usefulness of
	// restarting Sia as a means of eliminating unkonwn errors.
	h.fileCounter = p.FileCounter
	h.spaceRemaining = p.Settings.TotalStorage

	// Copy over the obligations and then subscribe to the consensus set.
	for _, obligation := range p.Obligations {
		// Store the obligation in the obligations list.
		h.obligationsByID[obligation.ID] = obligation

		// Update spaceRemaining to account for the storage held by this
		// obligation.
		h.spaceRemaining -= int64(obligation.fileSize())

		// Update anticipated revenue to reflect the revenue in this file
		// contract.
		h.anticipatedRevenue = h.anticipatedRevenue.Add(obligation.value())
	}
	err = h.initConsensusSubscription()
	if err != nil {
		return err
	}
	for _, obligation := range h.obligationsByID {
		h.handleActionItem(obligation)
	}
	return nil
}
예제 #16
0
// compatibilityLoad tries to load the file as a compatible version.
func (h *Host) compatibilityLoad() error {
	// Try loading the file as a 0.4 file.
	c04h := new(compat04Host)
	err := persist.LoadFile(compat04Metadata, c04h, filepath.Join(h.persistDir, "settings.json"))
	if err != nil {
		// 0.4.x is the only backwards compatibility provided. File could not
		// be loaded.
		return err
	}

	// Copy over host identity.
	h.publicKey = c04h.PublicKey
	h.secretKey = c04h.SecretKey

	// Copy file management, including providing compatibility for old
	// obligations.
	h.fileCounter = int64(c04h.FileCounter)
	h.spaceRemaining = c04h.HostSettings.TotalStorage
	upgradedObligations := h.loadCompat04Obligations(c04h.Obligations)

	// Copy over statistics.
	h.revenue = c04h.Profit

	// Copy over utilities.
	h.settings = c04h.HostSettings
	// AcceptingContracts should be true by default
	h.settings.AcceptingContracts = true

	// Subscribe to the consensus set.
	if build.DEBUG && h.recentChange != (modules.ConsensusChangeID{}) {
		panic("compatibility loading is not starting from blank consensus?")
	}
	// initConsensusSubscription will scan through the consensus set and set
	// 'OriginConfirmed' and 'RevisionConfirmed' when the accociated file
	// contract and file contract revisions are found.
	err = h.initConsensusSubscription()
	if err != nil {
		return err
	}

	// Remove all obligations that have not had their origin transactions
	// confirmed on the blockchain, and add action items for the rest.
	for _, uo := range upgradedObligations {
		if !uo.OriginConfirmed {
			// Because there is no transaction on the blockchain, and because
			// the old host did not keep the transaction, it's highly unlikely
			// that a transaction will appear - the obligation should be
			// removed.
			h.removeObligation(uo, obligationUnconfirmed)
			continue
		}
		// Check that the file Merkle root matches the Merkle root found in the
		// blockchain.
		file, err := os.Open(uo.Path)
		if err != nil {
			h.log.Println("Compatibility contract file could not be opened.")
			h.removeObligation(uo, obligationFailed)
			continue
		}
		merkleRoot, err := crypto.ReaderMerkleRoot(file)
		file.Close() // Close the file after use, to prevent buildup if the loop iterates many times.
		if err != nil {
			h.log.Println("Compatibility contract file could not be checksummed")
			h.removeObligation(uo, obligationFailed)
			continue
		}
		if merkleRoot != uo.merkleRoot() {
			h.log.Println("Compatibility contract file has the wrong merkle root")
			h.removeObligation(uo, obligationFailed)
			continue
		}
	}
	return nil
}