-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathblockchain.go
More file actions
622 lines (554 loc) · 17.8 KB
/
Copy pathblockchain.go
File metadata and controls
622 lines (554 loc) · 17.8 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package fulamobile
import (
"context"
"encoding/json"
"fmt"
"math/big"
"strconv"
"strings"
"github.com/functionland/go-fula/blockchain"
wifi "github.com/functionland/go-fula/wap/pkg/wifi"
"github.com/google/uuid"
)
type PluginParam struct {
Name string `json:"name"`
Value string `json:"value"`
}
// AccountExists requests blox at Config.BloxAddr to check if the account exists or not.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) AccountExists(account string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.AccountExists(ctx, c.bloxPid, blockchain.AccountExistsRequest{Account: account})
}
// AccountCreate requests blox at Config.BloxAddr to create a account.
func (c *Client) AccountCreate() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.AccountCreate(ctx, c.bloxPid)
}
// AccountBalance requests blox at Config.BloxAddr to get the balance of the account.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) AccountBalance(account string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.AccountBalance(ctx, c.bloxPid, blockchain.AccountBalanceRequest{Account: account})
}
type AssetsBalanceResponse struct {
Amount uint64 `json:"amount"`
}
// AssetsBalance requests blox at Config.BloxAddr to get the balance of the account.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) AssetsBalance(account string, assetId int, classId int) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
responseBytes, err := c.bl.AssetsBalance(ctx, c.bloxPid, blockchain.AssetsBalanceRequest{Account: account, AssetId: uint64(assetId), ClassId: uint64(classId)})
if err != nil {
return nil, err
}
// Decode the response into the temporary struct
var tempResponse AssetsBalanceResponse
err = json.Unmarshal(responseBytes, &tempResponse)
if err != nil {
return nil, err
}
// Construct a new response with Amount as a string
modifiedResponse := map[string]string{
"amount": strconv.FormatUint(tempResponse.Amount, 10),
}
// Re-encode the modified response to JSON
modifiedResponseBytes, err := json.Marshal(modifiedResponse)
if err != nil {
return nil, err
}
return modifiedResponseBytes, nil
}
// AccountFund requests blox at Config.BloxAddr to fund the account.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) AccountFund(account string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
amountString := "1000000000000000000"
// Create a new big.Int
bigAmount := new(big.Int)
_, ok := bigAmount.SetString(amountString, 10)
if !ok {
err := fmt.Errorf("error: the number %s is not valid", amountString)
return nil, err
}
// Convert big.Int to blockchain.BigInt
amount := blockchain.BigInt{Int: *bigAmount}
return c.bl.AccountFund(ctx, c.bloxPid, blockchain.AccountFundRequest{Amount: amount, To: account})
}
func (c *Client) TransferToFula(amountStr string, walletAccount string, chain string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
// Convert amount from string to uint64
amount, err := strconv.ParseUint(amountStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid amount: %v", err)
}
convertInput := blockchain.TransferToFulaRequest{
Wallet: walletAccount,
Amount: amount, // Use the converted amount
Chain: chain,
}
return c.bl.TransferToFula(ctx, c.bloxPid, convertInput)
}
// PoolJoin requests blox at Config.BloxAddr to join a pool with the id.
// the addr must be a valid multiaddr that includes peer ID.
// Note that this call is only allowed on a user's own blox
func (c *Client) PoolJoin(poolID int) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.PoolJoin(ctx, c.bloxPid, blockchain.PoolJoinRequest{PoolID: poolID, PeerID: c.bloxPid.String()})
}
// PoolJoinWithChain requests blox at Config.BloxAddr to join a pool with the id on a specific chain.
// the addr must be a valid multiaddr that includes peer ID.
// Note that this call is only allowed on a user's own blox
func (c *Client) PoolJoinWithChain(poolID int, chainName string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.PoolJoin(ctx, c.bloxPid, blockchain.PoolJoinRequest{
PoolID: poolID,
PeerID: c.bloxPid.String(),
ChainName: chainName,
})
}
// PoolJoin requests blox at Config.BloxAddr to cancel a join request for a pool with the id.
// the addr must be a valid multiaddr that includes peer ID.
// Note that this call is only allowed on a user's own blox
func (c *Client) PoolCancelJoin(poolID int) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.PoolCancelJoin(ctx, c.bloxPid, blockchain.PoolCancelJoinRequest{PoolID: poolID})
}
// PoolListRequests requests blox at Config.BloxAddr to list the join request for a pool with the id.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) PoolRequests(poolID int) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.PoolRequests(ctx, c.bloxPid, blockchain.PoolRequestsRequest{PoolID: poolID})
}
// PoolList requests blox at Config.BloxAddr to list the pools.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) PoolList() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.PoolList(ctx, c.bloxPid, blockchain.PoolListRequest{})
}
// PoolUserList requests blox at Config.BloxAddr to list the input pool users.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) PoolUserList(poolID int) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.PoolUserList(ctx, c.bloxPid, blockchain.PoolUserListRequest{PoolID: poolID})
}
// PoolLeave requests blox at Config.BloxAddr to leave a pool with the id.
// the addr must be a valid multiaddr that includes peer ID.
// Note that this call is only allowed on a user's own blox
func (c *Client) PoolLeave(poolID int) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.PoolLeave(ctx, c.bloxPid, blockchain.PoolLeaveRequest{PoolID: poolID})
}
// PoolLeaveWithChain requests blox at Config.BloxAddr to leave a pool with the id on a specific chain.
// the addr must be a valid multiaddr that includes peer ID.
// Note that this call is only allowed on a user's own blox
func (c *Client) PoolLeaveWithChain(poolID int, chainName string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.PoolLeave(ctx, c.bloxPid, blockchain.PoolLeaveRequest{
PoolID: poolID,
ChainName: chainName,
})
}
// ManifestAvailable requests blox at Config.BloxAddr to list manifests
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) ManifestAvailable(poolID int) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.ManifestAvailable(ctx, c.bloxPid, blockchain.ManifestAvailableRequest{PoolID: poolID})
}
func (c *Client) BatchUploadManifest(cidsBytes []byte, poolID int, replicationFactor int) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
cidArray := strings.Split(string(cidsBytes), "|")
return c.bl.ManifestBatchUpload(ctx, c.bloxPid, blockchain.ManifestBatchUploadMobileRequest{Cid: cidArray, PoolID: poolID, ReplicationFactor: replicationFactor})
}
func (c *Client) ReplicateInPool(cidsBytes []byte, account string, poolID int) []byte {
ctx, done, err := c.beginOp()
if err != nil {
errJSON := map[string]string{"error": err.Error()}
errBytes, _ := json.Marshal(errJSON)
return errBytes
}
defer done()
cidArray := strings.Split(string(cidsBytes), "|")
responseBytes, err := c.bl.ReplicateInPool(ctx, c.bloxPid, blockchain.ReplicateRequest{Cids: cidArray, Account: account, PoolID: poolID})
if err != nil {
// Convert the error to a JSON object and then to []byte
errJSON := map[string]string{"error": err.Error()}
errBytes, jsonErr := json.Marshal(errJSON)
if jsonErr != nil {
// In case JSON marshaling fails, fallback to a simple byte conversion of the error string
return []byte("Error marshaling error to JSON: " + jsonErr.Error())
}
return errBytes
}
return responseBytes
}
//////////////////////////////////////////////////
/////////////////////HARDWARE/////////////////////
//////////////////////////////////////////////////
// BloxFreeSpace requests the blox avail/used free space information.
func (c *Client) BloxFreeSpace() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.BloxFreeSpace(ctx, c.bloxPid)
}
// EraseBlData requests the blox to erase the data related to blockchain
func (c *Client) EraseBlData() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.EraseBlData(ctx, c.bloxPid)
}
// WifiRemoveall requests the blox to remove all saved wifis
func (c *Client) WifiRemoveall() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.WifiRemoveall(ctx, c.bloxPid)
}
// Reboot requests the blox to reboot
func (c *Client) Reboot() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.Reboot(ctx, c.bloxPid)
}
// Reboot requests the blox to reboot
func (c *Client) DeleteWifi(name string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
// Create the DeleteWifiRequest
req := wifi.DeleteWifiRequest{
ConnectionName: name,
}
return c.bl.DeleteWifi(ctx, c.bloxPid, req)
}
// Reboot requests the blox to reboot
func (c *Client) DisconnectWifi(name string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
// Create the DeleteWifiRequest
req := wifi.DeleteWifiRequest{
ConnectionName: name,
}
return c.bl.DisconnectWifi(ctx, c.bloxPid, req)
}
// Partition requests the blox to partition ssd and nvme
func (c *Client) Partition() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.Partition(ctx, c.bloxPid)
}
// DeleteFulaConfig deletes config.yaml file
func (c *Client) DeleteFulaConfig() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.DeleteFulaConfig(ctx, c.bloxPid)
}
// GetAccount requests blox at Config.BloxAddr to get the balance of the account.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) GetAccount() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.GetAccount(ctx, c.bloxPid)
}
// GetAccount requests blox at Config.BloxAddr to get the balance of the account.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) FetchContainerLogs(ContainerName string, TailCount string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.FetchContainerLogs(ctx, c.bloxPid, wifi.FetchContainerLogsRequest{ContainerName: ContainerName, TailCount: TailCount})
}
func (c *Client) ChatWithAI(aiModel string, userMessage string) ([]byte, error) {
// Gate the initial request so it can't race Shutdown and nil-deref c.bl.
// Use context.TODO (not the op ctx) for the call itself so the streaming
// buffer outlives this function; done() only needs to bound the setup call.
_, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
buffer, err := c.bl.ChatWithAI(context.TODO(), c.bloxPid, wifi.ChatWithAIRequest{
AIModel: aiModel,
UserMessage: userMessage,
})
if err != nil {
return nil, fmt.Errorf("error starting ChatWithAI: %v", err)
}
streamID := uuid.New().String() // Generate a unique stream ID
c.mu.Lock()
c.streams[streamID] = buffer // Store the StreamBuffer in the map
c.mu.Unlock()
return []byte(streamID), nil // Return the stream ID as a byte slice
}
func (c *Client) GetChatChunk(streamID string) (string, error) {
c.mu.Lock()
buffer, ok := c.streams[streamID]
c.mu.Unlock()
if !ok {
return "", fmt.Errorf("invalid stream ID")
}
for {
chunk, err := buffer.GetChunk()
if err != nil { // Stream closed or errored out
c.mu.Lock()
delete(c.streams, streamID) // Remove the stream from the map
c.mu.Unlock()
return "", err
}
// Skip empty chunks and wait for a valid chunk
if chunk != "" {
return chunk, nil
}
}
}
func (c *Client) GetStreamIterator(streamID string) (*StreamIterator, error) {
c.mu.Lock()
buffer, ok := c.streams[streamID]
c.mu.Unlock()
if !ok {
return nil, fmt.Errorf("invalid stream ID")
}
return &StreamIterator{buffer: buffer}, nil
}
// GetAccount requests blox at Config.BloxAddr to get the balance of the account.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) FindBestAndTargetInLogs(NodeContainerName string, TailCount string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.FindBestAndTargetInLogs(ctx, c.bloxPid, wifi.FindBestAndTargetInLogsRequest{NodeContainerName: NodeContainerName, TailCount: TailCount})
}
// GetAccount requests blox at Config.BloxAddr to get the balance of the account.
// the addr must be a valid multiaddr that includes peer ID.
func (c *Client) GetFolderSize(folderPath string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.GetFolderSize(ctx, c.bloxPid, wifi.GetFolderSizeRequest{FolderPath: folderPath})
}
func (c *Client) GetDatastoreSize() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.GetDatastoreSize(ctx, c.bloxPid, wifi.GetDatastoreSizeRequest{})
}
// GetDockerImageBuildDates fetches build dates of all Docker images running on the blox.
func (c *Client) GetDockerImageBuildDates() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.GetDockerImageBuildDates(ctx, c.bloxPid)
}
// GetClusterInfo fetches the ipfs-cluster peer ID and peer name from the blox.
func (c *Client) GetClusterInfo() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.GetClusterInfo(ctx, c.bloxPid)
}
// ListPlugins requests the blox to list all available plugins
func (c *Client) ListPlugins() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.ListPlugins(ctx, c.bloxPid)
}
func (c *Client) ListActivePlugins() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.ListActivePlugins(ctx, c.bloxPid)
}
// InstallPlugin requests the blox to install a specific plugin
func (c *Client) InstallPlugin(pluginName string, params string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.InstallPlugin(ctx, c.bloxPid, pluginName, params)
}
// UninstallPlugin requests the blox to uninstall a specific plugin
func (c *Client) UninstallPlugin(pluginName string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.UninstallPlugin(ctx, c.bloxPid, pluginName)
}
// ShowPluginStatus requests the status of a specific plugin
func (c *Client) ShowPluginStatus(pluginName string, lines int) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.ShowPluginStatus(ctx, pluginName, lines)
}
// InstallPlugin requests the blox to install a specific plugin
func (c *Client) GetInstallOutput(pluginName string, params string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.GetInstallOutput(ctx, c.bloxPid, pluginName, params)
}
// InstallPlugin requests the blox to install a specific plugin
func (c *Client) GetInstallStatus(pluginName string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.GetInstallStatus(ctx, c.bloxPid, pluginName)
}
func (c *Client) UpdatePlugin(pluginName string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.UpdatePlugin(ctx, c.bloxPid, pluginName)
}
//////////////////////////////////////////////////
////////////////////AUTO-PIN//////////////////////
//////////////////////////////////////////////////
// AutoPinPair sends a pairing request to the blox with pinning service credentials.
func (c *Client) AutoPinPair(token string, endpoint string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.AutoPinPair(ctx, c.bloxPid, blockchain.AutoPinPairRequest{
PinningToken: token,
PinningEndpoint: endpoint,
})
}
// AutoPinRefresh refreshes the pinning service token on the blox.
func (c *Client) AutoPinRefresh(token string) ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.AutoPinRefresh(ctx, c.bloxPid, blockchain.AutoPinRefreshRequest{
PinningToken: token,
})
}
// AutoPinUnpair removes auto-pin configuration from the blox.
func (c *Client) AutoPinUnpair() ([]byte, error) {
ctx, done, err := c.beginOp()
if err != nil {
return nil, err
}
defer done()
return c.bl.AutoPinUnpair(ctx, c.bloxPid)
}