/
api.go
367 lines (329 loc) · 12.6 KB
/
api.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright 2019 DxChain, All rights reserved.
// Use of this source code is governed by an Apache
// License 2.0 that can be found in the LICENSE file.
package storagehost
import (
"errors"
"fmt"
"github.com/DxChainNetwork/godx/accounts"
"github.com/DxChainNetwork/godx/common"
"github.com/DxChainNetwork/godx/common/unit"
"github.com/DxChainNetwork/godx/storage"
)
// HostPrivateAPI is the api for private usage
type HostPrivateAPI struct {
storageHost *StorageHost
}
// NewHostPrivateAPI is the api to create the host private api
func NewHostPrivateAPI(storageHost *StorageHost) *HostPrivateAPI {
return &HostPrivateAPI{
storageHost: storageHost,
}
}
// Version gives a mock version of the debugapi
func (h *HostPrivateAPI) Version() string {
return Version
}
// SectorSize return the sector size as a basic storage unit of the storage system.
func (h *HostPrivateAPI) SectorSize() uint64 {
return storage.SectorSize
}
// PersistDir print the persist directory of the host
func (h *HostPrivateAPI) PersistDir() string {
return h.storageHost.getPersistDir()
}
// Announce set accepting contracts to true, and then send the announcement
// transaction
func (h *HostPrivateAPI) Announce() string {
if err := h.storageHost.setAcceptContracts(true); err != nil {
return fmt.Sprintf("cannot set AcceptingContracts: %v", err)
}
address, err := h.storageHost.getPaymentAddress()
if err != nil {
return fmt.Sprintf("cannot get the payment address: %v", err)
}
hash, err := h.storageHost.parseAPI.StorageTx.SendHostAnnounceTX(address)
if err != nil {
return fmt.Sprintf("cannot send the announce transaction: %v", err)
}
return fmt.Sprintf("Announcement transaction: %v", hash.Hex())
}
// Folders return all the folders
func (h *HostPrivateAPI) Folders() []storage.HostFolder {
return h.storageHost.StorageManager.Folders()
}
// AvailableSpace return the available spaces of the host
func (h *HostPrivateAPI) AvailableSpace() storage.HostSpace {
return h.storageHost.StorageManager.AvailableSpace()
}
// GetHostConfig return the internal settings of the storage host
func (h *HostPrivateAPI) GetHostConfig() storage.HostIntConfigForDisplay {
// Get the internal setting
config := h.storageHost.getInternalConfig()
// parse the numbers to human readable string
display := storage.HostIntConfigForDisplay{
AcceptingContracts: unit.FormatBool(config.AcceptingContracts),
MaxDownloadBatchSize: unit.FormatStorage(config.MaxDownloadBatchSize, false),
MaxDuration: unit.FormatTime(config.MaxDuration),
MaxReviseBatchSize: unit.FormatStorage(config.MaxReviseBatchSize, false),
WindowSize: unit.FormatTime(config.WindowSize),
PaymentAddress: config.PaymentAddress.String(),
Deposit: unit.FormatCurrency(config.Deposit, "/byte/block"),
DepositBudget: unit.FormatCurrency(config.DepositBudget, "/contract"),
MaxDeposit: unit.FormatCurrency(config.MaxDeposit),
BaseRPCPrice: unit.FormatCurrency(config.BaseRPCPrice),
ContractPrice: unit.FormatCurrency(config.ContractPrice, "/contract"),
DownloadBandwidthPrice: unit.FormatCurrency(config.DownloadBandwidthPrice, "/byte"),
SectorAccessPrice: unit.FormatCurrency(config.SectorAccessPrice, "/sector"),
StoragePrice: unit.FormatCurrency(config.StoragePrice, "/byte/block"),
UploadBandwidthPrice: unit.FormatCurrency(config.UploadBandwidthPrice, "/byte"),
}
return display
}
// GetFinancialMetrics get the financial metrics of the host
func (h *HostPrivateAPI) GetFinancialMetrics() HostFinancialMetricsForDisplay {
fm := h.storageHost.getFinancialMetrics()
display := HostFinancialMetricsForDisplay{
ContractCount: fm.ContractCount,
ContractCompensation: unit.FormatCurrency(fm.ContractCompensation),
PotentialContractCompensation: unit.FormatCurrency(fm.PotentialContractCompensation),
LockedStorageDeposit: unit.FormatCurrency(fm.LockedStorageDeposit),
LostRevenue: unit.FormatCurrency(fm.LostRevenue),
LostStorageDeposit: unit.FormatCurrency(fm.LostStorageDeposit),
PotentialStorageRevenue: unit.FormatCurrency(fm.PotentialStorageRevenue),
RiskedStorageDeposit: unit.FormatCurrency(fm.RiskedStorageDeposit),
StorageRevenue: unit.FormatCurrency(fm.StorageRevenue),
TransactionFeeExpenses: unit.FormatCurrency(fm.TransactionFeeExpenses),
DownloadBandwidthRevenue: unit.FormatCurrency(fm.DownloadBandwidthRevenue),
PotentialDownloadBandwidthRevenue: unit.FormatCurrency(fm.PotentialDownloadBandwidthRevenue),
PotentialUploadBandwidthRevenue: unit.FormatCurrency(fm.PotentialUploadBandwidthRevenue),
UploadBandwidthRevenue: unit.FormatCurrency(fm.UploadBandwidthRevenue),
}
return display
}
//GetPaymentAddress get the account address used to sign the storage contract. If not configured, the first address in the local wallet will be used as the paymentAddress by default.
func (h *HostPrivateAPI) GetPaymentAddress() string {
addr, err := h.storageHost.getPaymentAddress()
if err != nil {
return err.Error()
}
return fmt.Sprintf("Current address: %v", common.Bytes2Hex(addr[:]))
}
// GetProofWindow return the proof window size
func (h *HostPrivateAPI) GetProofWindow() string {
return unit.FormatTime(storage.ProofWindowSize)
}
// AddStorageFolder add a storage folder with a specified size
func (h *HostPrivateAPI) AddStorageFolder(path string, sizeStr string) (string, error) {
size, err := unit.ParseStorage(sizeStr)
if err != nil {
return "", err
}
err = h.storageHost.StorageManager.AddStorageFolder(path, size)
if err != nil {
return "", err
}
return "successfully added the storage folder", nil
}
// ResizeFolder resize the folder to specified size
func (h *HostPrivateAPI) ResizeFolder(folderPath string, sizeStr string) (string, error) {
size, err := unit.ParseStorage(sizeStr)
if err != nil {
return "", err
}
err = h.storageHost.StorageManager.ResizeFolder(folderPath, size)
if err != nil {
return "", err
}
return "successfully resize the storage folder", nil
}
// DeleteFolder delete the folder
func (h *HostPrivateAPI) DeleteFolder(folderPath string) (string, error) {
err := h.storageHost.StorageManager.DeleteFolder(folderPath)
if err != nil {
return "", err
}
return "successfully delete the storage folder", nil
}
// hostSetterCallbacks is the mapping from the field name to the setter function
var hostSetterCallbacks = map[string]func(*HostPrivateAPI, string) error{
"acceptingContracts": (*HostPrivateAPI).setAcceptingContracts,
"maxDownloadBatchSize": (*HostPrivateAPI).setMaxDownloadBatchSize,
"maxDuration": (*HostPrivateAPI).setMaxDuration,
"maxReviseBatchSize": (*HostPrivateAPI).setMaxReviseBatchSize,
"paymentAddress": (*HostPrivateAPI).setPaymentAddress,
"deposit": (*HostPrivateAPI).setDeposit,
"depositBudget": (*HostPrivateAPI).setDepositBudget,
"maxDeposit": (*HostPrivateAPI).setMaxDeposit,
"baseRPCPrice": (*HostPrivateAPI).setBaseRPCPrice,
"contractPrice": (*HostPrivateAPI).setContractPrice,
"downloadBandwidthPrice": (*HostPrivateAPI).setDownloadBandwidthPrice,
"sectorAccessPrice": (*HostPrivateAPI).setSectorAccessPrice,
"storagePrice": (*HostPrivateAPI).setStoragePrice,
"uploadBandwidthPrice": (*HostPrivateAPI).setUploadBandwidthPrice,
}
// SetConfig set the config specified by a mapping of key value pair
func (h *HostPrivateAPI) SetConfig(config map[string]string) (string, error) {
h.storageHost.lock.Lock()
// record the previous config and register the defer function
var err error
prevConfig := h.storageHost.config
defer func() {
// If error happened, revert to the previous config
if err != nil {
h.storageHost.config = prevConfig
}
h.storageHost.lock.Unlock()
}()
// Loops over the user set config and change the host settings
for key, value := range config {
callback, exist := hostSetterCallbacks[key]
if !exist {
err = fmt.Errorf("unknown config variable")
return "", err
}
if err = callback(h, value); err != nil {
return "", err
}
}
// sync the config
if err = h.storageHost.syncConfig(); err != nil {
return "", err
}
return `Successfully set the host config. Next please use
shost.announce()
to broadcast the config changes.
`, nil
}
// setAcceptingContracts set host AcceptingContracts to val specified by valStr
func (h *HostPrivateAPI) setAcceptingContracts(valStr string) error {
val, err := unit.ParseBool(valStr)
if err != nil {
return fmt.Errorf("invalid bool string: %v", err)
}
h.storageHost.config.AcceptingContracts = val
return nil
}
// setMaxDownloadBatchSize set host MaxDownloadBatchSize to value
func (h *HostPrivateAPI) setMaxDownloadBatchSize(valStr string) error {
val, err := unit.ParseStorage(valStr)
if err != nil {
return fmt.Errorf("invalid storage string: %v", err)
}
h.storageHost.config.MaxDownloadBatchSize = val
return nil
}
// setMaxDuration set host MaxDuration to value
func (h *HostPrivateAPI) setMaxDuration(str string) error {
val, err := unit.ParseTime(str)
if err != nil {
return fmt.Errorf("invalid time string: %v", err)
}
h.storageHost.config.MaxDuration = val
return nil
}
// setMaxReviseBatchSize set host MaxReviseBatchSize to value
func (h *HostPrivateAPI) setMaxReviseBatchSize(str string) error {
val, err := unit.ParseStorage(str)
if err != nil {
return fmt.Errorf("invalid size string: %v", err)
}
h.storageHost.config.MaxReviseBatchSize = val
return nil
}
// setPaymentAddress configure the account address used to sign the storage contract,
// which has and can only be the address of the local wallet.
func (h *HostPrivateAPI) setPaymentAddress(addrStr string) error {
addr := common.HexToAddress(addrStr)
account := accounts.Account{Address: addr}
if h.storageHost.am == nil {
return errors.New("storage host has no account manager")
}
_, err := h.storageHost.am.Find(account)
if err != nil {
return errors.New("unknown account")
}
h.storageHost.config.PaymentAddress = addr
return nil
}
// setDeposit set host Deposit to value.
func (h *HostPrivateAPI) setDeposit(str string) error {
wei, err := unit.ParseCurrency(str)
if err != nil {
return fmt.Errorf("invalid currency expression: %v", err)
}
h.storageHost.config.Deposit = wei
return nil
}
// setDepositBudget set host DepositBudget to value
func (h *HostPrivateAPI) setDepositBudget(str string) error {
wei, err := unit.ParseCurrency(str)
if err != nil {
return fmt.Errorf("invalid currency expression: %v", err)
}
h.storageHost.config.DepositBudget = wei
return nil
}
// setMaxDeposit set host MaxDeposit to value
func (h *HostPrivateAPI) setMaxDeposit(str string) error {
wei, err := unit.ParseCurrency(str)
if err != nil {
return fmt.Errorf("invalid currency expression: %v", err)
}
h.storageHost.config.MaxDeposit = wei
return nil
}
// setBaseRPCPrice set host BaseRPCPrice to value
func (h *HostPrivateAPI) setBaseRPCPrice(str string) error {
wei, err := unit.ParseCurrency(str)
if err != nil {
return fmt.Errorf("invalid currency expression: %v", err)
}
h.storageHost.config.BaseRPCPrice = wei
return nil
}
// setContractPrice set host ContractPrice to value
func (h *HostPrivateAPI) setContractPrice(str string) error {
wei, err := unit.ParseCurrency(str)
if err != nil {
return fmt.Errorf("invalid currency expression: %v", err)
}
h.storageHost.config.ContractPrice = wei
return nil
}
// setDownloadBandwidthPrice set host DownloadBandwidthPrice to value
func (h *HostPrivateAPI) setDownloadBandwidthPrice(str string) error {
wei, err := unit.ParseCurrency(str)
if err != nil {
return fmt.Errorf("invalid currency expression: %v", err)
}
h.storageHost.config.DownloadBandwidthPrice = wei
return nil
}
// setSectorAccessPrice set host SectorAccessPrice to value
func (h *HostPrivateAPI) setSectorAccessPrice(str string) error {
wei, err := unit.ParseCurrency(str)
if err != nil {
return fmt.Errorf("invalid currency expression: %v", err)
}
h.storageHost.config.SectorAccessPrice = wei
return nil
}
// setStoragePrice set host StoragePrice to value
func (h *HostPrivateAPI) setStoragePrice(str string) error {
wei, err := unit.ParseCurrency(str)
if err != nil {
return fmt.Errorf("invalid currency expression: %v", err)
}
h.storageHost.config.StoragePrice = wei
return nil
}
// setUploadBandwidthPrice set host UploadBandwidthPrice to value
func (h *HostPrivateAPI) setUploadBandwidthPrice(str string) error {
wei, err := unit.ParseCurrency(str)
if err != nil {
return fmt.Errorf("invalid currency expression: %v", err)
}
h.storageHost.config.UploadBandwidthPrice = wei
return nil
}