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