forked from NebulousLabs/Sia
/
persist.go
253 lines (228 loc) · 8.84 KB
/
persist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package host
import (
"os"
"path/filepath"
"sync/atomic"
"github.com/NebulousLabs/Sia/crypto"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/persist"
"github.com/NebulousLabs/Sia/types"
"github.com/NebulousLabs/bolt"
)
// persistence is the data that is kept when the host is restarted.
type persistence struct {
// RPC Metrics.
DownloadCalls uint64 `json:"downloadcalls"`
ErroredCalls uint64 `json:"erroredcalls"`
FormContractCalls uint64 `json:"formcontractcalls"`
RenewCalls uint64 `json:"renewcalls"`
ReviseCalls uint64 `json:"revisecalls"`
RecentRevisionCalls uint64 `json:"recentrevisioncalls"`
SettingsCalls uint64 `json:"settingscalls"`
UnrecognizedCalls uint64 `json:"unrecognizedcalls"`
// Consensus Tracking.
BlockHeight types.BlockHeight `json:"blockheight"`
RecentChange modules.ConsensusChangeID `json:"recentchange"`
// Host Identity.
Announced bool `json:"announced"`
AutoAddress modules.NetAddress `json:"autoaddress"`
FinancialMetrics modules.HostFinancialMetrics `json:"financialmetrics"`
PublicKey types.SiaPublicKey `json:"publickey"`
RevisionNumber uint64 `json:"revisionnumber"`
SecretKey crypto.SecretKey `json:"secretkey"`
Settings modules.HostInternalSettings `json:"settings"`
UnlockHash types.UnlockHash `json:"unlockhash"`
}
// persistData returns the data in the Host that will be saved to disk.
func (h *Host) persistData() persistence {
return persistence{
// RPC Metrics.
DownloadCalls: atomic.LoadUint64(&h.atomicDownloadCalls),
ErroredCalls: atomic.LoadUint64(&h.atomicErroredCalls),
FormContractCalls: atomic.LoadUint64(&h.atomicFormContractCalls),
RenewCalls: atomic.LoadUint64(&h.atomicRenewCalls),
ReviseCalls: atomic.LoadUint64(&h.atomicReviseCalls),
RecentRevisionCalls: atomic.LoadUint64(&h.atomicRecentRevisionCalls),
SettingsCalls: atomic.LoadUint64(&h.atomicSettingsCalls),
UnrecognizedCalls: atomic.LoadUint64(&h.atomicUnrecognizedCalls),
// Consensus Tracking.
BlockHeight: h.blockHeight,
RecentChange: h.recentChange,
// Host Identity.
Announced: h.announced,
AutoAddress: h.autoAddress,
FinancialMetrics: h.financialMetrics,
PublicKey: h.publicKey,
RevisionNumber: h.revisionNumber,
SecretKey: h.secretKey,
Settings: h.settings,
UnlockHash: h.unlockHash,
}
}
// establishDefaults configures the default settings for the host, overwriting
// any existing settings.
func (h *Host) establishDefaults() error {
// Configure the settings object.
h.settings = modules.HostInternalSettings{
MaxDownloadBatchSize: uint64(defaultMaxDownloadBatchSize),
MaxDuration: defaultMaxDuration,
MaxReviseBatchSize: uint64(defaultMaxReviseBatchSize),
WindowSize: defaultWindowSize,
Collateral: defaultCollateral,
CollateralBudget: defaultCollateralBudget,
MaxCollateral: defaultMaxCollateral,
MinStoragePrice: defaultStoragePrice,
MinContractPrice: defaultContractPrice,
MinDownloadBandwidthPrice: defaultDownloadBandwidthPrice,
MinUploadBandwidthPrice: defaultUploadBandwidthPrice,
}
// Generate signing key, for revising contracts.
sk, pk, err := crypto.GenerateKeyPair()
if err != nil {
return err
}
h.secretKey = sk
h.publicKey = types.SiaPublicKey{
Algorithm: types.SignatureEd25519,
Key: pk[:],
}
// Subscribe to the consensus set.
err = h.initConsensusSubscription()
if err != nil {
return err
}
return h.save()
}
// initDB will check that the database has been initialized and if not, will
// initialize the database.
func (h *Host) initDB() error {
return h.db.Update(func(tx *bolt.Tx) error {
// The storage obligation bucket does not exist, which means the
// database needs to be initialized. Create the database buckets.
buckets := [][]byte{
bucketActionItems,
bucketStorageObligations,
}
for _, bucket := range buckets {
_, err := tx.CreateBucketIfNotExists(bucket)
if err != nil {
return err
}
}
return nil
})
}
// load loads the Hosts's persistent data from disk.
func (h *Host) load() error {
p := new(persistence)
err := h.dependencies.loadFile(persistMetadata, p, filepath.Join(h.persistDir, settingsFile))
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.atomicDownloadCalls, p.DownloadCalls)
atomic.StoreUint64(&h.atomicErroredCalls, p.ErroredCalls)
atomic.StoreUint64(&h.atomicFormContractCalls, p.FormContractCalls)
atomic.StoreUint64(&h.atomicRenewCalls, p.RenewCalls)
atomic.StoreUint64(&h.atomicReviseCalls, p.ReviseCalls)
atomic.StoreUint64(&h.atomicRecentRevisionCalls, p.RecentRevisionCalls)
atomic.StoreUint64(&h.atomicSettingsCalls, p.SettingsCalls)
atomic.StoreUint64(&h.atomicUnrecognizedCalls, p.UnrecognizedCalls)
// Copy over consensus tracking.
h.blockHeight = p.BlockHeight
h.recentChange = p.RecentChange
// Copy over host identity.
h.announced = p.Announced
h.autoAddress = p.AutoAddress
if err := p.AutoAddress.IsValid(); err != nil {
h.log.Printf("WARN: AutoAddress '%v' loaded from persist is invalid: %v", p.AutoAddress, err)
h.autoAddress = ""
}
h.financialMetrics = p.FinancialMetrics
h.publicKey = p.PublicKey
h.revisionNumber = p.RevisionNumber
h.secretKey = p.SecretKey
h.settings = p.Settings
if err := p.Settings.NetAddress.IsValid(); err != nil {
h.log.Printf("WARN: NetAddress '%v' loaded from persist is invalid: %v", p.Settings.NetAddress, err)
h.settings.NetAddress = ""
}
h.unlockHash = p.UnlockHash
// Get the number of storage obligations by looking at the storage
// obligation database.
err = h.db.View(func(tx *bolt.Tx) error {
h.financialMetrics.ContractCount = uint64(tx.Bucket(bucketStorageObligations).Stats().KeyN)
return nil
})
if err != nil {
return err
}
// COMPAT v1.0.0
//
// Load compatibility fields which may have data leftover. This call should
// only be relevant the first time the user loads the host after upgrading
// from v0.6.0 to v1.0.0.
err = h.loadCompat(p)
if err != nil {
return err
}
err = h.initConsensusSubscription()
if err != nil {
return err
}
return nil
}
// loadCompat loads fields that have changed names or otherwise broken
// compatibility with previous versions, enabling users to upgrade without
// unexpected loss of data.
//
// COMPAT v1.0.0
//
// A spelling error in pre-1.0 versions means that, if this is the first time
// running after an upgrade, the misspelled field needs to be transfered over.
func (h *Host) loadCompat(p *persistence) error {
var compatPersistence struct {
FinancialMetrics struct {
PotentialStorageRevenue types.Currency `json:"potentialerevenue"`
}
Settings struct {
MinContractPrice types.Currency `json:"contractprice"`
MinDownloadBandwidthPrice types.Currency `json:"minimumdownloadbandwidthprice"`
MinStoragePrice types.Currency `json:"storageprice"`
MinUploadBandwidthPrice types.Currency `json:"minimumuploadbandwidthprice"`
}
}
err := h.dependencies.loadFile(persistMetadata, &compatPersistence, filepath.Join(h.persistDir, settingsFile))
if err != nil {
return err
}
// Load the compat values, but only if the compat values are non-zero and
// the real values are zero.
if !compatPersistence.FinancialMetrics.PotentialStorageRevenue.IsZero() && p.FinancialMetrics.PotentialStorageRevenue.IsZero() {
h.financialMetrics.PotentialStorageRevenue = compatPersistence.FinancialMetrics.PotentialStorageRevenue
}
if !compatPersistence.Settings.MinContractPrice.IsZero() && p.Settings.MinContractPrice.IsZero() {
h.settings.MinContractPrice = compatPersistence.Settings.MinContractPrice
}
if !compatPersistence.Settings.MinDownloadBandwidthPrice.IsZero() && p.Settings.MinDownloadBandwidthPrice.IsZero() {
h.settings.MinDownloadBandwidthPrice = compatPersistence.Settings.MinDownloadBandwidthPrice
}
if !compatPersistence.Settings.MinStoragePrice.IsZero() && p.Settings.MinStoragePrice.IsZero() {
h.settings.MinStoragePrice = compatPersistence.Settings.MinStoragePrice
}
if !compatPersistence.Settings.MinUploadBandwidthPrice.IsZero() && p.Settings.MinUploadBandwidthPrice.IsZero() {
h.settings.MinUploadBandwidthPrice = compatPersistence.Settings.MinUploadBandwidthPrice
}
return nil
}
// 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))
}
// saveSync stores all of the persist data to disk and then syncs to disk.
func (h *Host) saveSync() error {
return persist.SaveFileSync(persistMetadata, h.persistData(), filepath.Join(h.persistDir, settingsFile))
}