-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminer_wallet_handler.go
352 lines (307 loc) · 11.8 KB
/
miner_wallet_handler.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
package main
import (
"context"
alephium "github.com/alephium/go-sdk"
"github.com/sirupsen/logrus"
"net/http"
"strings"
"time"
)
type miningHandler struct {
alephiumClient *alephium.APIClient
walletName string
walletPassword string
walletMnemonic string
walletMnemonicPassphrase string
printMnemonic bool
log *logrus.Logger
}
func newMiningHandler(alephiumClient *alephium.APIClient, walletName string, walletPassword string,
walletMnemonic string, walletMnemonicPassphrase string, printMnemonic bool,
log *logrus.Logger) (*miningHandler, error) {
handler := &miningHandler{
alephiumClient: alephiumClient,
walletName: walletName,
walletPassword: walletPassword,
walletMnemonic: walletMnemonic,
walletMnemonicPassphrase: walletMnemonicPassphrase,
printMnemonic: printMnemonic,
log: log,
}
return handler, nil
}
func hasSameAddresses(minerAddresses *alephium.MinerAddresses, walletAddresses *alephium.Addresses) bool {
if len(minerAddresses.Addresses) <= 1 || len(minerAddresses.Addresses) != len(walletAddresses.Addresses) {
return false
}
for _, minerAddress := range minerAddresses.Addresses {
found := false
for _, walletAddress := range walletAddresses.Addresses {
if walletAddress.Address == minerAddress {
found = true
}
}
if !found {
return false
}
}
return true
}
func (h *miningHandler) createAndUnlockWallet(ctx context.Context, log *logrus.Entry) (*alephium.WalletStatus, error) {
walletFound, err := checkWalletExist(ctx, h.alephiumClient, h.walletName, log)
if err != nil {
h.log.WithError(err).Debugf("Got an error calling CheckWalletExist endpoint %s", h.alephiumClient.GetConfig().Host)
return nil, err
}
var wallet *alephium.WalletStatus
if !walletFound {
h.log.Infof("Wallet %s not found, creating or restoring it now.", h.walletName)
if h.walletMnemonic != "" {
restoredWallet, err := restoreWallet(ctx, h.alephiumClient, h.walletName, h.walletPassword, h.walletMnemonic,
h.walletMnemonicPassphrase, true, log)
if err != nil {
h.log.WithError(err).Debugf("Got an error calling wallet restore endpoint %v", h.alephiumClient.GetConfig().Host)
return nil, err
}
wallet, err = getWalletStatus(ctx, h.alephiumClient, restoredWallet.WalletName, log)
if err != nil {
h.log.WithError(err).Debugf("Got an error calling wallet status after a restore, wallet restoration probably didn't work...")
return nil, err
}
} else {
createdWallet, err := createWallet(ctx, h.alephiumClient, h.walletName, h.walletPassword,
h.walletMnemonicPassphrase, true, log)
if err != nil {
h.log.WithError(err).Debugf("Got an error calling wallet create endpoint %v. Err = %v", h.alephiumClient.GetConfig().Host)
return nil, err
}
if h.printMnemonic {
h.log.Infof("[SENSITIVE] The mnemonic of the newly created wallet is [ %s ]. This mnemonic will never be printed again, make sure you write them down somewhere!",
createdWallet.Mnemonic)
}
wallet, err = getWalletStatus(ctx, h.alephiumClient, createdWallet.WalletName, log)
if err != nil {
h.log.WithError(err).Debugf("Got an error calling wallet status after a create, wallet creation probably didn't work...")
return nil, err
}
}
} else {
wallet, err = getWalletStatus(ctx, h.alephiumClient, h.walletName, log)
if err != nil {
h.log.WithError(err).Debugf("Got an error calling wallet status...")
return nil, err
}
}
if wallet.Locked {
err := unlockWallet(ctx, h.alephiumClient, h.walletName, h.walletPassword, h.walletMnemonicPassphrase, log)
if err != nil {
h.log.WithError(err).Debugf("Got an error while unlocking the wallet %s", h.walletName)
return wallet, err
}
}
return wallet, nil
}
func (h *miningHandler) updateMinersAddresses(ctx context.Context, log *logrus.Entry) error {
minerAddresses, err := getMinersAddresses(ctx, h.alephiumClient, log)
if err != nil && !strings.HasPrefix(err.Error(), "Miner addresses are not set up") {
h.log.WithError(err).Debugf("Got an error calling miners addresses. Err = %v", err)
return err
}
walletAddresses, err := getWalletAddresses(ctx, h.alephiumClient, h.walletName, log)
if err != nil {
h.log.WithError(err).Debugf("Got an error calling wallet addresses")
return err
}
if !hasSameAddresses(minerAddresses, walletAddresses) {
h.log.Debugf("Current miner addresses %v", minerAddresses)
h.log.Debugf("Mining wallet addresses %v", walletAddresses)
err = updateMinerAddresses(ctx, h.alephiumClient, GetAddressesAsString(walletAddresses.Addresses), log)
if err != nil {
h.log.WithError(err).Debugf("Got an error calling update miners addresses")
return err
}
}
return nil
}
func (h *miningHandler) waitForNodeInSync(ctx context.Context, log *logrus.Entry) error {
_, err := WaitUntilSyncedWithAtLeastOnePeer(ctx, h.alephiumClient, 30*time.Second, log)
if err != nil {
h.log.WithError(err).Debugf("Got an error waiting for the node to be in sync with peers")
return err
}
return nil
}
// ensureMiningWalletAndNodeMining
func (h *miningHandler) ensureMiningWalletAndNodeMining(ctx context.Context, log *logrus.Entry) error {
for range time.Tick(5 * time.Minute) {
err := h.updateMinersAddresses(ctx, log)
if err != nil {
h.log.Fatalf("Got an error while updating miners addresses. Err = %v", err)
return err
}
err = h.waitForNodeInSync(ctx, log)
if err != nil {
h.log.Fatalf("Got an error while waiting for the node to be in sync with peers. Err = %v", err)
return err
}
}
return nil
}
func checkWalletExist(ctx context.Context, alephiumClient *alephium.APIClient, walletName string, log *logrus.Entry) (bool, error) {
walletNameRequest := alephiumClient.WalletsApi.GetWalletsWalletName(ctx, walletName)
_, resp, err := walletNameRequest.Execute()
if err != nil && (resp == nil || resp.StatusCode != http.StatusNotFound) {
log.WithError(err).Debugf("Got an error while checking if the wallet %s exists", walletName)
return false, err
}
if err != nil && resp != nil && resp.StatusCode == http.StatusNotFound {
return false, nil
}
return true, nil
}
func restoreWallet(ctx context.Context, alephiumClient *alephium.APIClient,
walletName, walletPassword, walletMnemonic, walletMnemonicPassphrase string,
isMiner bool, log *logrus.Entry) (*alephium.WalletRestoreResult, error) {
walletRestore := alephium.NewWalletRestore(walletPassword, walletMnemonic, walletName)
walletRestore.SetIsMiner(isMiner)
if walletMnemonicPassphrase != "" {
walletRestore.SetMnemonicPassphrase(walletMnemonicPassphrase)
}
walletRestoreReq := alephiumClient.WalletsApi.PutWallets(ctx).WalletRestore(*walletRestore)
walletRestoreRes, _, err := walletRestoreReq.Execute()
if err != nil {
log.WithError(err).Debugf("Got an error while restoring wallet %s", walletName)
return nil, err
}
return walletRestoreRes, nil
}
func createWallet(ctx context.Context, alephiumClient *alephium.APIClient,
walletName, walletPassword, walletMnemonicPassphrase string,
isMiner bool, log *logrus.Entry) (*alephium.WalletCreationResult, error) {
walletCreation := alephium.NewWalletCreation(walletPassword, walletName)
walletCreation.SetIsMiner(isMiner)
if walletMnemonicPassphrase != "" {
walletCreation.SetMnemonicPassphrase(walletMnemonicPassphrase)
}
walletCreationReq := alephiumClient.WalletsApi.PostWallets(ctx).WalletCreation(*walletCreation)
walletCreationRes, _, err := walletCreationReq.Execute()
if err != nil {
log.WithError(err).Debugf("Got an error while restoring wallet %s", walletName)
return nil, err
}
return walletCreationRes, nil
}
func unlockWallet(ctx context.Context, alephiumClient *alephium.APIClient,
walletName, walletPassword, walletMnemonicPassphrase string,
log *logrus.Entry) error {
walletUnlock := alephium.NewWalletUnlock(walletPassword)
if walletMnemonicPassphrase != "" {
walletUnlock.SetMnemonicPassphrase(walletMnemonicPassphrase)
}
walletUnlockReq := alephiumClient.WalletsApi.PostWalletsWalletNameUnlock(ctx, walletName).
WalletUnlock(*walletUnlock)
_, err := walletUnlockReq.Execute()
if err != nil {
log.WithError(err).Debugf("Got an error while unlocking wallet %s", walletName)
return err
}
return nil
}
func getWalletStatus(ctx context.Context, alephiumClient *alephium.APIClient,
walletName string, log *logrus.Entry) (*alephium.WalletStatus, error) {
walletNameRequest := alephiumClient.WalletsApi.GetWalletsWalletName(ctx, walletName)
wallet, _, err := walletNameRequest.Execute()
if err != nil {
log.WithError(err).Debugf("Got an error while calling wallet status %s", walletName)
return nil, err
}
return wallet, nil
}
func getWalletAddresses(ctx context.Context, alephiumClient *alephium.APIClient,
walletName string, log *logrus.Entry) (*alephium.Addresses, error) {
walletAddressesReq := alephiumClient.WalletsApi.GetWalletsWalletNameAddresses(ctx, walletName)
walletAddresses, _, err := walletAddressesReq.Execute()
if err != nil {
log.WithError(err).Debugf("Got an error while calling get wallet addresses %s", walletName)
return nil, err
}
return walletAddresses, nil
}
func getMinersAddresses(ctx context.Context, alephiumClient *alephium.APIClient,
log *logrus.Entry) (*alephium.MinerAddresses, error) {
minerAddressesReq := alephiumClient.MinersApi.GetMinersAddresses(ctx)
minerAddresses, _, err := minerAddressesReq.Execute()
if err != nil {
log.WithError(err).Debugf("Got an error while calling miner addresses")
return nil, err
}
return minerAddresses, nil
}
func updateMinerAddresses(ctx context.Context, alephiumClient *alephium.APIClient,
addresses []string, log *logrus.Entry) error {
minerAddresses := alephium.NewMinerAddresses(addresses)
minerAddressesReq := alephiumClient.MinersApi.PutMinersAddresses(ctx).MinerAddresses(*minerAddresses)
_, err := minerAddressesReq.Execute()
if err != nil {
log.WithError(err).Debugf("Got an error while updatring miner addresses")
return err
}
return nil
}
func GetAddressesAsString(walletAddresses []alephium.AddressInfo) []string {
addresses := make([]string, 0, len(walletAddresses))
for _, wa := range walletAddresses {
addresses = append(addresses, wa.Address)
}
return addresses
}
func getInterCliquePeerInfo(ctx context.Context, alephiumClient *alephium.APIClient, log *logrus.Entry) ([]alephium.InterCliquePeerInfo, error) {
info, _, err := alephiumClient.InfosApi.GetInfosInterCliquePeerInfo(ctx).Execute()
if err != nil {
log.WithError(err).Debugf("Got an error while calling intercliquepeerinfo")
return []alephium.InterCliquePeerInfo{}, err
}
return info, err
}
// IsSyncedWithAtLeastOnePeer checks if the clique is connected with at least one clique
// or the list of peers is empty
func IsSyncedWithAtLeastOnePeer(peers []alephium.InterCliquePeerInfo) bool {
atLeastOneSynced := false
for _, peer := range peers {
if peer.IsSynced {
atLeastOneSynced = true
}
}
return atLeastOneSynced || len(peers) == 0
}
// WaitUntilSyncedWithAtLeastOnePeer waits until the clique is connected to at least one clique
// or the context is done.
func WaitUntilSyncedWithAtLeastOnePeer(ctx context.Context, alephiumClient *alephium.APIClient, sleeptime time.Duration, log *logrus.Entry) (bool, error) {
for isSynced := false; ; {
select {
case <-ctx.Done():
return false, ctx.Err()
default:
}
var err error
isSynced, err = IsSynced(ctx, alephiumClient, log)
if err != nil {
return false, err
}
if isSynced {
return true, nil
} else {
log.Debugf("Not sync'ed yet, sleeping %s", sleeptime)
time.Sleep(sleeptime)
}
}
}
// IsSynced checks if the clique is synced
func IsSynced(ctx context.Context, alephiumClient *alephium.APIClient, log *logrus.Entry) (bool, error) {
peers, err := getInterCliquePeerInfo(ctx, alephiumClient, log)
if err != nil {
return false, err
}
isSynced := IsSyncedWithAtLeastOnePeer(peers)
return isSynced, nil
}