gitlab.com/jokerrs1/Sia@v1.3.2/modules/renter/contractor/persist.go (about)

     1  package contractor
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/NebulousLabs/Sia/crypto"
     8  	"github.com/NebulousLabs/Sia/modules"
     9  	"github.com/NebulousLabs/Sia/modules/renter/proto"
    10  	"github.com/NebulousLabs/Sia/persist"
    11  	"github.com/NebulousLabs/Sia/types"
    12  )
    13  
    14  // contractorPersist defines what Contractor data persists across sessions.
    15  type contractorPersist struct {
    16  	Allowance     modules.Allowance         `json:"allowance"`
    17  	BlockHeight   types.BlockHeight         `json:"blockheight"`
    18  	CurrentPeriod types.BlockHeight         `json:"currentperiod"`
    19  	LastChange    modules.ConsensusChangeID `json:"lastchange"`
    20  	OldContracts  []modules.RenterContract  `json:"oldcontracts"`
    21  	RenewedIDs    map[string]string         `json:"renewedids"`
    22  }
    23  
    24  // persistData returns the data in the Contractor that will be saved to disk.
    25  func (c *Contractor) persistData() contractorPersist {
    26  	data := contractorPersist{
    27  		Allowance:     c.allowance,
    28  		BlockHeight:   c.blockHeight,
    29  		CurrentPeriod: c.currentPeriod,
    30  		LastChange:    c.lastChange,
    31  		RenewedIDs:    make(map[string]string),
    32  	}
    33  	for _, contract := range c.oldContracts {
    34  		data.OldContracts = append(data.OldContracts, contract)
    35  	}
    36  	for oldID, newID := range c.renewedIDs {
    37  		data.RenewedIDs[oldID.String()] = newID.String()
    38  	}
    39  	return data
    40  }
    41  
    42  // load loads the Contractor persistence data from disk.
    43  func (c *Contractor) load() error {
    44  	var data contractorPersist
    45  	err := c.persist.load(&data)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	c.allowance = data.Allowance
    50  	c.blockHeight = data.BlockHeight
    51  	c.currentPeriod = data.CurrentPeriod
    52  	c.lastChange = data.LastChange
    53  	for _, contract := range data.OldContracts {
    54  		c.oldContracts[contract.ID] = contract
    55  	}
    56  	for oldString, newString := range data.RenewedIDs {
    57  		var oldHash, newHash crypto.Hash
    58  		oldHash.LoadString(oldString)
    59  		newHash.LoadString(newString)
    60  		c.renewedIDs[types.FileContractID(oldHash)] = types.FileContractID(newHash)
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  // save saves the Contractor persistence data to disk.
    67  func (c *Contractor) save() error {
    68  	return c.persist.save(c.persistData())
    69  }
    70  
    71  // saveSync saves the Contractor persistence data to disk and then syncs to disk.
    72  func (c *Contractor) saveSync() error {
    73  	return c.persist.save(c.persistData())
    74  }
    75  
    76  // convertPersist converts the pre-v1.3.1 contractor persist formats to the new
    77  // formats.
    78  func convertPersist(dir string) error {
    79  	// Try loading v1.3.1 persist. If it has the correct version number, no
    80  	// further action is necessary.
    81  	persistPath := filepath.Join(dir, "contractor.json")
    82  	err := persist.LoadJSON(persistMeta, nil, persistPath)
    83  	if err == nil {
    84  		return nil
    85  	}
    86  
    87  	// Try loading v1.3.0 persist (journal).
    88  	journalPath := filepath.Join(dir, "contractor.journal")
    89  	if _, err := os.Stat(journalPath); os.IsNotExist(err) {
    90  		// no journal file found; assume this is a fresh install
    91  		return nil
    92  	}
    93  	var p journalPersist
    94  	j, err := openJournal(journalPath, &p)
    95  	if err != nil {
    96  		return err
    97  	}
    98  	j.Close()
    99  	// convert to v1.3.1 format and save
   100  	data := contractorPersist{
   101  		Allowance:     p.Allowance,
   102  		BlockHeight:   p.BlockHeight,
   103  		CurrentPeriod: p.CurrentPeriod,
   104  		LastChange:    p.LastChange,
   105  		RenewedIDs:    p.RenewedIDs,
   106  	}
   107  	for _, c := range p.OldContracts {
   108  		data.OldContracts = append(data.OldContracts, modules.RenterContract{
   109  			ID:               c.ID,
   110  			HostPublicKey:    c.HostPublicKey,
   111  			StartHeight:      c.StartHeight,
   112  			EndHeight:        c.EndHeight(),
   113  			RenterFunds:      c.RenterFunds(),
   114  			DownloadSpending: c.DownloadSpending,
   115  			StorageSpending:  c.StorageSpending,
   116  			UploadSpending:   c.UploadSpending,
   117  			TotalCost:        c.TotalCost,
   118  			ContractFee:      c.ContractFee,
   119  			TxnFee:           c.TxnFee,
   120  			SiafundFee:       c.SiafundFee,
   121  		})
   122  	}
   123  	err = persist.SaveJSON(persistMeta, data, persistPath)
   124  	if err != nil {
   125  		return err
   126  	}
   127  
   128  	// create the contracts directory if it does not yet exist
   129  	cs, err := proto.NewContractSet(filepath.Join(dir, "contracts"))
   130  	if err != nil {
   131  		return err
   132  	}
   133  	defer cs.Close()
   134  
   135  	// convert contracts to contract files
   136  	for _, c := range p.Contracts {
   137  		cachedRev := p.CachedRevisions[c.ID.String()]
   138  		if err := cs.ConvertV130Contract(c, cachedRev); err != nil {
   139  			return err
   140  		}
   141  	}
   142  
   143  	// delete the journal file
   144  	os.RemoveAll(journalPath)
   145  	return nil
   146  }