forked from NebulousLabs/Sia
/
persist.go
206 lines (185 loc) · 6.38 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
package host
import (
"encoding/json"
"os"
"path/filepath"
"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/crypto"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/persist"
"github.com/NebulousLabs/Sia/types"
"github.com/coreos/bbolt"
)
// persistence is the data that is kept when the host is restarted.
type persistence struct {
// 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{
// 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 := crypto.GenerateKeyPair()
h.secretKey = sk
h.publicKey = types.Ed25519PublicKey(pk)
// Subscribe to the consensus set.
err := h.initConsensusSubscription()
if err != nil {
return err
}
return nil
}
// loadPersistObject will take a persist object and copy the data into the
// host.
func (h *Host) loadPersistObject(p *persistence) {
// 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
}
// initDB will check that the database has been initialized and if not, will
// initialize the database.
func (h *Host) initDB() (err error) {
// Open the host's database and set up the stop function to close it.
h.db, err = h.dependencies.OpenDatabase(dbMetadata, filepath.Join(h.persistDir, dbFilename))
if err != nil {
return err
}
h.tg.AfterStop(func() {
err = h.db.Close()
if err != nil {
h.log.Println("Could not close the database:", err)
}
})
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 {
// Initialize the host database.
err := h.initDB()
if err != nil {
err = build.ExtendErr("Could not initialize database:", err)
h.log.Println(err)
return err
}
// Load the old persistence object from disk. Simple task if the version is
// the most recent version, but older versions need to be updated to the
// more recent structures.
p := new(persistence)
err = h.dependencies.LoadFile(persistMetadata, p, filepath.Join(h.persistDir, settingsFile))
if err == nil {
// Copy in the persistence.
h.loadPersistObject(p)
} else if os.IsNotExist(err) {
// There is no host.json file, set up sane defaults.
return h.establishDefaults()
} else if err == persist.ErrBadVersion {
// Attempt an upgrade from V112 to V120.
err = h.upgradeFromV112ToV120()
if err != nil {
return err
}
} else if err != nil {
return err
}
// Get the contract count and locked collateral by observing all of the incomplete
// storage obligations in the database.
// TODO: both contract count and locked collateral are not correctly updated during
// contract renewals. This leads to an offset to the real value over time.
h.financialMetrics.ContractCount = 0
h.financialMetrics.LockedStorageCollateral = types.NewCurrency64(0)
err = h.db.View(func(tx *bolt.Tx) error {
cursor := tx.Bucket(bucketStorageObligations).Cursor()
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
var so storageObligation
err := json.Unmarshal(v, &so)
if err != nil {
return err
}
if so.ObligationStatus == obligationUnresolved {
h.financialMetrics.ContractCount++
h.financialMetrics.LockedStorageCollateral = h.financialMetrics.LockedStorageCollateral.Add(so.LockedCollateral)
}
}
return nil
})
if err != nil {
return err
}
return h.initConsensusSubscription()
}
// saveSync stores all of the persist data to disk and then syncs to disk.
func (h *Host) saveSync() error {
return persist.SaveJSON(persistMetadata, h.persistData(), filepath.Join(h.persistDir, settingsFile))
}