-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmobile-wrapper.go
More file actions
261 lines (213 loc) · 7.55 KB
/
mobile-wrapper.go
File metadata and controls
261 lines (213 loc) · 7.55 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
package mobile
import (
"context"
"errors"
"fmt"
"io"
"strings"
beelite "github.com/Solar-Punk-Ltd/bee-lite"
"github.com/ethersphere/bee/v2/pkg/api"
"github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/swarm"
)
const StringSliceDelimiter = "|"
type MobileNode interface {
BlockchainData() (*BlockchainData, error)
ConnectedPeerCount() int
Download(hash string) (*FileDownloadResult, error)
Shutdown() error
WalletAddress() string
FetchStamps()
GetStampCount() int
GetStamp(index int) *StampData
BuyStamp(amountString string, depthString string, name string, immutable bool) (string, error)
Upload(batchIdHex, filename, contentType string,
act bool,
historyAddressHex string,
encrypt bool,
rLevel byte,
content []byte) (fileUploadResult *FileUploadResult, err error)
}
type MobileNodeImp struct {
beeClient *beelite.Beelite
stampManager *StampManager
uploadManager *UploadManager
}
func StartNode(options *MobileNodeOptions, password string, verbosity string) (MobileNode, error) {
beeliteOptions, err := convert(options)
fmt.Printf("%+v\n", beeliteOptions)
if err != nil {
return nil, err
}
beeClient, err := beelite.Start(beeliteOptions, password, verbosity)
if err != nil {
return nil, err
}
return &MobileNodeImp{beeClient: beeClient, stampManager: NewStampManager(beeClient), uploadManager: NewUploadManager(beeClient)}, nil
}
func convert(options *MobileNodeOptions) (*beelite.LiteOptions, error) {
validateErr := validate(options)
if validateErr != nil {
return nil, validateErr
}
bootNodes := []string{}
if options.Bootnodes != "" {
bootNodes = strings.Split(options.Bootnodes, StringSliceDelimiter)
}
staticNodesOpt := []string{}
if options.StaticNodes != "" {
staticNodesOpt = strings.Split(options.StaticNodes, StringSliceDelimiter)
}
return &beelite.LiteOptions{
FullNodeMode: options.FullNodeMode,
BootnodeMode: options.BootnodeMode,
Bootnodes: bootNodes,
StaticNodes: staticNodesOpt,
DataDir: options.DataDir,
WelcomeMessage: options.WelcomeMessage,
BlockchainRpcEndpoint: options.BlockchainRpcEndpoint,
SwapInitialDeposit: options.SwapInitialDeposit,
PaymentThreshold: options.PaymentThreshold,
SwapEnable: options.SwapEnable,
ChequebookEnable: options.ChequebookEnable,
UsePostageSnapshot: options.UsePostageSnapshot,
Mainnet: options.Mainnet,
NetworkID: uint64(options.NetworkID),
NATAddr: options.NATAddr,
CacheCapacity: uint64(options.CacheCapacity),
DBOpenFilesLimit: uint64(options.DBOpenFilesLimit),
DBWriteBufferSize: uint64(options.DBWriteBufferSize),
DBBlockCacheCapacity: uint64(options.DBBlockCacheCapacity),
DBDisableSeeksCompaction: options.DBDisableSeeksCompaction,
RetrievalCaching: options.RetrievalCaching,
}, nil
}
func validate(options *MobileNodeOptions) error {
if options.NetworkID < 0 {
return errors.New("network ID must be a non-negative integer")
}
if options.CacheCapacity < 0 {
return errors.New("cache capacity must be a non-negative integer")
}
if options.DBOpenFilesLimit < 0 {
return errors.New("cache capacity must be a non-negative integer")
}
if options.DBWriteBufferSize < 0 {
return errors.New("DBWriteBufferSize must be a non-negative integer")
}
if options.DBOpenFilesLimit < 0 {
return errors.New("DBOpenFilesLimit must be a non-negative integer")
}
return nil
}
func (m *MobileNodeImp) Download(hash string) (*FileDownloadResult, error) {
m.beeClient.GetLogger().Info("downloading: ", "hash", hash)
var result *File = nil
if hash == "" {
e := fmt.Errorf("please enter a hash")
return nil, e
}
dlAddr, err := swarm.ParseHexAddress(hash)
if err != nil {
return nil, err
}
ref, fileName, err := m.beeClient.GetBzz(context.Background(), dlAddr, nil, nil, nil)
if err != nil {
if errors.Is(err, beelite.ErrFailedToGetBzzReference) {
return nil, nil
}
m.beeClient.GetLogger().Error(err, "download failed")
return nil, err
}
hash = ""
data, err := io.ReadAll(ref)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
m.beeClient.GetLogger().Info("content not found for hash: ", "hash", hash)
return nil, nil
}
m.beeClient.GetLogger().Error(err, "convert to bytes failed")
return nil, err
}
m.beeClient.GetLogger().Info("download succeeded", "fileName", fileName, "size", len(data))
result = &File{Name: fileName, Data: data}
return &FileDownloadResult{File: result, Stats: nil}, nil
}
func (m *MobileNodeImp) Upload(batchIdHex, filename, contentType string,
act bool,
historyAddressHex string,
encrypt bool,
redundancyLevel byte,
content []byte) (fileUploadResult *FileUploadResult, err error) {
historyAddress := swarm.MustParseHexAddress(historyAddressHex)
reference, newHistoryAddress, err := m.uploadManager.Upload(batchIdHex, filename, contentType, act, historyAddress, encrypt, redundancyLevel, content)
if err != nil {
return nil, err
}
return &FileUploadResult{ReferenceHex: reference.String(), HistoryAddressHex: newHistoryAddress.String(), Stats: nil}, nil
}
// TODO remove later - ReactNative app still using it
func (m *MobileNodeImp) WalletAddress() string {
return m.beeClient.OverlayEthAddress().String()
}
func (m *MobileNodeImp) BlockchainData() (*BlockchainData, error) {
m.beeClient.GetLogger().Info("Getting blockchain data")
chequebookBalance, err := m.getChequebookBalance()
chequebookAddress := m.getChequebookAddr()
if err != nil {
m.beeClient.GetLogger().Error(err, "failed to get blockchain data")
return nil, err
}
m.beeClient.GetLogger().Info("Blockchain data retrieved", "walletAddress", m.beeClient.OverlayEthAddress().String(), "chequebookAddress", chequebookAddress, "chequebookBalance", chequebookBalance)
return &BlockchainData{
WalletAddress: m.beeClient.OverlayEthAddress().String(),
ChequebookAddress: chequebookAddress,
ChequebookBalance: chequebookBalance,
}, nil
}
func (m *MobileNodeImp) ConnectedPeerCount() int {
return m.beeClient.ConnectedPeerCount()
}
func (m *MobileNodeImp) Shutdown() error {
err := m.beeClient.Shutdown()
if err == nil {
m.beeClient.GetLogger().Info("shutdown succeeded")
return nil
}
m.beeClient.GetLogger().Error(err, "shutdown failed")
return err
}
func (m *MobileNodeImp) getChequebookAddr() string {
if m.beeClient.BeeNodeMode() == api.UltraLightMode {
m.beeClient.GetLogger().Info("Node running in ultra-light mode, skipping getChequebookAddr query")
return "N/A"
}
return m.beeClient.ChequebookAddr().String()
}
func (m *MobileNodeImp) getChequebookBalance() (string, error) {
if m.beeClient.BeeNodeMode() == api.UltraLightMode {
m.beeClient.GetLogger().Info("Node running in ultra-light mode, skipping getChequebookBalance query")
return "N/A", nil
}
chequebookBalance, err := m.beeClient.ChequebookBalance()
if err != nil {
m.beeClient.GetLogger().Error(err, "failed to get chequebook balance")
return "", err
}
return chequebookBalance.String(), nil
}
func (m *MobileNodeImp) FetchStamps() {
m.stampManager.GetAllBatches()
}
func (m *MobileNodeImp) GetStampCount() int {
return len(m.stampManager.stamps)
}
func (m *MobileNodeImp) GetStamp(index int) *StampData {
if index < 0 || index >= len(m.stampManager.stamps) {
return nil
}
return m.stampManager.stamps[index]
}
func (m *MobileNodeImp) BuyStamp(amountString string, depthString string, name string, immutable bool) (string, error) {
return m.stampManager.BuyStamp(amountString, depthString, name, immutable)
}