-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmulty-back.go
executable file
·373 lines (327 loc) · 11.5 KB
/
multy-back.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
368
369
370
371
372
373
/*
Copyright 2018 Idealnaya rabota LLC
Licensed under Multy.io license.
See LICENSE for details
*/
package multyback
import (
"context"
"encoding/json"
"fmt"
"github.com/Multy-io/Multy-back/exchanger"
"io/ioutil"
"os"
"strings"
// exchanger "github.com/Multy-io/Multy-back-exchange-service"
btcpb "github.com/Multy-io/Multy-BTC-node-service/node-streamer"
ethpb "github.com/Multy-io/Multy-ETH-node-service/node-streamer"
"github.com/Multy-io/Multy-back/btc"
"github.com/Multy-io/Multy-back/client"
"github.com/Multy-io/Multy-back/currencies"
"github.com/Multy-io/Multy-back/eth"
"github.com/Multy-io/Multy-back/store"
"github.com/gin-gonic/gin"
"github.com/jekabolt/slf"
)
var (
log = slf.WithContext("multy-back").WithCaller(slf.CallerShort)
)
const (
defaultServerAddress = "0.0.0.0:6678"
version = "v1"
)
const (
EventConnection = "connection"
EventInitialAdd = "allUsers"
EventResyncAddress = "resync"
EventSendRawTx = "sendRaw"
EventAddNewAddress = "newUser"
Room = "node"
)
// Multy is a main struct of service
type Multy struct {
config *Configuration
clientPool *client.SocketIOConnectedPool
route *gin.Engine
userStore store.UserStore
restClient *client.RestClient
firebaseClient *client.FirebaseClient
BTC *btc.BTCConn
ETH *eth.ETHConn
ExchangerFactory *exchanger.FactoryExchanger
}
// Init initializes Multy instance
func Init(conf *Configuration) (*Multy, error) {
multy := &Multy{
config: conf,
ExchangerFactory: &exchanger.FactoryExchanger{},
}
// DB initialization
userStore, err := store.InitUserStore(conf.Database)
if err != nil {
return nil, fmt.Errorf("DB initialization: %s on port %s", err.Error(), conf.Database.Address)
}
multy.userStore = userStore
log.Infof("UserStore initialization done on %s √", conf.Database)
//BTC
btcCli, err := btc.InitHandlers(&conf.Database, conf.SupportedNodes, conf.NSQAddress)
if err != nil {
return nil, fmt.Errorf("Init: btc.InitHandlers: %s", err.Error())
}
btcVer, err := btcCli.CliMain.ServiceInfo(context.Background(), &btcpb.Empty{})
multy.BTC = btcCli
log.Infof(" BTC initialization done on %v √", btcVer)
// ETH
ethCli, err := eth.InitHandlers(&conf.Database, conf.SupportedNodes, conf.NSQAddress)
if err != nil {
return nil, fmt.Errorf("Init: btc.InitHandlers: %s", err.Error())
}
ethVer, err := ethCli.CliMain.ServiceInfo(context.Background(), ðpb.Empty{})
multy.ETH = ethCli
log.Infof(" ETH initialization done on %v √", ethVer)
//users data set
sv, err := multy.SetUserData(multy.userStore, conf.SupportedNodes)
if err != nil {
return nil, fmt.Errorf("Init: multy.SetUserData: %s", err.Error())
}
log.Infof("Users data initialization done √")
log.Debugf("Server versions %v", sv)
// REST handlers
if err = multy.initHttpRoutes(conf); err != nil {
return nil, fmt.Errorf("Router initialization: %s", err.Error())
}
err = multy.ConfigureExchangers()
if err != nil {
return nil, fmt.Errorf("Failed to configure exchangers, [%s]", err.Error())
}
return multy, nil
}
func (m *Multy) ConfigureExchangers() error {
m.ExchangerFactory.SetExchangersConfig(m.config.Exchangers)
for _, exchangerConfig := range m.config.Exchangers {
if exchangerConfig.IsActive {
// exchanger warm-up
_, err := m.ExchangerFactory.GetExchanger(exchangerConfig.Name)
if err != nil {
log.Errorf("Failed to initialize [%s] exchanger, [%s]", exchangerConfig.Name, err.Error())
}
log.Infof("Exchanger: name [%s] init completed", exchangerConfig.Name)
}
}
return nil
}
// SetUserData make initial userdata to node service
func (m *Multy) SetUserData(userStore store.UserStore, ct []store.CoinType) ([]store.ServiceInfo, error) {
servicesInfo := []store.ServiceInfo{}
for _, conCred := range ct {
usersData, err := userStore.FindUserDataChain(conCred.СurrencyID, conCred.NetworkID)
if err != nil {
return servicesInfo, fmt.Errorf("SetUserData: userStore.FindUserDataChain: curID :%d netID :%d err =%s", conCred.СurrencyID, conCred.NetworkID, err.Error())
}
if len(usersData) == 0 {
log.Infof("Empty userdata")
}
usersContracts, err := userStore.FindUsersContractsChain(conCred.СurrencyID, conCred.NetworkID)
if err != nil {
return servicesInfo, fmt.Errorf("SetUserData: userStore.FindUsersContractsChain: curID :%d netID :%d err =%s", conCred.СurrencyID, conCred.NetworkID, err.Error())
}
if len(usersData) == 0 {
log.Infof("Empty userscontracts")
}
switch conCred.СurrencyID {
case currencies.Bitcoin:
var cli btcpb.NodeCommunicationsClient
switch conCred.NetworkID {
case currencies.Main:
cli = m.BTC.CliMain
case currencies.Test:
cli = m.BTC.CliTest
default:
log.Errorf("setGRPCHandlers: wrong networkID:")
}
//TODO: Re State
go m.restoreState(conCred, cli)
genUd := btcpb.UsersData{
Map: map[string]*btcpb.AddressExtended{},
}
for address, ex := range usersData {
genUd.Map[address] = &btcpb.AddressExtended{
UserID: ex.UserID,
WalletIndex: int32(ex.WalletIndex),
AddressIndex: int32(ex.AddressIndex),
}
}
resp, err := cli.EventInitialAdd(context.Background(), &genUd)
if err != nil {
return servicesInfo, fmt.Errorf("SetUserData: btcCli.CliMain.EventInitialAdd: curID :%d netID :%d err =%s", conCred.СurrencyID, conCred.NetworkID, err.Error())
}
log.Debugf("Btc EventInitialAdd: resp: %s", resp.Message)
sv, err := cli.ServiceInfo(context.Background(), &btcpb.Empty{})
if err != nil {
return servicesInfo, fmt.Errorf("SetUserData: cli.ServiceInfo: curID :%d netID :%d err =%s", conCred.СurrencyID, conCred.NetworkID, err.Error())
}
servicesInfo = append(servicesInfo, store.ServiceInfo{
Branch: sv.Branch,
Commit: sv.Commit,
Buildtime: sv.Buildtime,
Lasttag: sv.Lasttag,
})
case currencies.Ether:
var cli ethpb.NodeCommunicationsClient
switch conCred.NetworkID {
case currencies.ETHMain:
cli = m.ETH.CliMain
case currencies.ETHTest:
cli = m.ETH.CliTest
default:
log.Errorf("setGRPCHandlers: wrong networkID:")
}
//TODO: Restore state
go m.restoreState(conCred, cli)
genUd := ethpb.UsersData{
Map: map[string]*ethpb.AddressExtended{},
UsersContracts: usersContracts,
}
for address, ex := range usersData {
if ex.WalletIndex == -1 {
genUd.Map[address] = ðpb.AddressExtended{
UserID: "imported",
WalletIndex: int32(ex.WalletIndex),
AddressIndex: int32(ex.AddressIndex),
}
} else {
genUd.Map[address] = ðpb.AddressExtended{
UserID: ex.UserID,
WalletIndex: int32(ex.WalletIndex),
AddressIndex: int32(ex.AddressIndex),
}
}
}
resp, err := cli.EventInitialAdd(context.Background(), &genUd)
if err != nil {
return servicesInfo, fmt.Errorf("SetUserData: Ether.EventInitialAdd: curID :%d netID :%d err =%s", conCred.СurrencyID, conCred.NetworkID, err.Error())
}
log.Debugf("Ether cli.EventInitialAdd: resp: %s", resp.Message)
sv, err := cli.ServiceInfo(context.Background(), ðpb.Empty{})
if err != nil {
return servicesInfo, fmt.Errorf("SetUserData: cli.ServiceInfo: curID :%d netID :%d err =%s", conCred.СurrencyID, conCred.NetworkID, err.Error())
}
servicesInfo = append(servicesInfo, store.ServiceInfo{
Branch: sv.Branch,
Commit: sv.Commit,
Buildtime: sv.Buildtime,
Lasttag: sv.Lasttag,
})
}
}
return nil, nil
}
// initRoutes initialize client communication services
// - http
// - socketio
// - firebase
func (multy *Multy) initHttpRoutes(conf *Configuration) error {
router := gin.Default()
multy.route = router
gin.SetMode(gin.DebugMode)
f, err := os.OpenFile("../currencies/erc20tokens.json", os.O_RDONLY, os.FileMode(0644))
// f, err := os.OpenFile("/currencies/erc20tokens.json")
if err != nil {
return err
}
bs, err := ioutil.ReadAll(f)
if err != nil {
return err
}
tokenList := store.VerifiedTokenList{}
_ = json.Unmarshal(bs, &tokenList)
restClient, err := client.SetRestHandlers(
multy.userStore,
router,
conf.DonationAddresses,
multy.BTC,
multy.ETH,
conf.MultyVerison,
conf.Secretkey,
conf.MobileVersions,
tokenList,
conf.BrowserDefault,
multy.ExchangerFactory,
)
if err != nil {
return err
}
multy.restClient = restClient
// socketIO server initialization. server -> mobile client
socketIORoute := router.Group("/socketio")
socketIOPool, err := client.SetSocketIOHandlers(multy.restClient, multy.BTC, multy.ETH, socketIORoute, conf.SocketioAddr, conf.NSQAddress, multy.userStore)
if err != nil {
return err
}
multy.clientPool = socketIOPool
multy.ETH.WsServer = multy.clientPool.Server
multy.BTC.WsServer = multy.clientPool.Server
firebaseClient, err := client.InitFirebaseConn(&conf.Firebase, multy.route, conf.NSQAddress)
if err != nil {
return err
}
multy.firebaseClient = firebaseClient
return nil
}
// Run runs service
func (multy *Multy) Run() error {
log.Info("Running server")
multy.route.Run(multy.config.RestAddress)
return nil
}
func fetchCoinType(coinTypes []store.CoinType, currencyID, networkID int) (*store.CoinType, error) {
for _, ct := range coinTypes {
if ct.СurrencyID == currencyID && ct.NetworkID == networkID {
return &ct, nil
}
}
return nil, fmt.Errorf("fetchCoinType: no such coin in config")
}
func (m *Multy) restoreState(coinType store.CoinType, ncClient interface{}) {
if coinType.AccuracyRange > 0 {
var height int64
height, err := m.userStore.FetchLastSyncBlockState(coinType.NetworkID, coinType.СurrencyID)
if err != nil {
log.Warnf("SetUserData: btcCli.CliMain.cli.FetchLastSyncBlockState: curID :%d netID :%d err =%s", coinType.СurrencyID, coinType.NetworkID, err.Error())
}
log.Debugf("Last height recorded %v last trusted block %v netid:%v curid", height, height-int64(coinType.AccuracyRange), coinType.NetworkID, coinType.СurrencyID)
height = height - int64(coinType.AccuracyRange)
if height > 0 {
switch coinType.СurrencyID {
case currencies.Ether:
var cli ethpb.NodeCommunicationsClient
cli = ncClient.(ethpb.NodeCommunicationsClient)
rp, err := cli.SyncState(context.Background(), ðpb.BlockHeight{Height: height})
if err != nil {
log.Errorf("SetUserData: restoreState:cli.SyncState: curID :%d netID :%d err =%s", coinType.СurrencyID, coinType.NetworkID, err.Error())
}
if strings.Contains("err:", rp.GetMessage()) {
log.Errorf("SetUserData: Contains err : curID :%d netID :%d err =%s", coinType.СurrencyID, coinType.NetworkID, err.Error())
}
log.Debugf("Restored state processing on curid =%v netid =%v", coinType.СurrencyID, coinType.NetworkID)
case currencies.Bitcoin:
var cli btcpb.NodeCommunicationsClient
cli = ncClient.(btcpb.NodeCommunicationsClient)
rp, err := cli.SyncState(context.Background(), &btcpb.BlockHeight{Height: height})
if err != nil {
log.Errorf("SetUserData: btcCli.CliMain.cli.SyncState: curID :%d netID :%d err =%s", coinType.СurrencyID, coinType.NetworkID, err.Error())
}
if strings.Contains("err:", rp.GetMessage()) {
log.Errorf("SetUserData: Contains err : curID :%d netID :%d err =%s", coinType.СurrencyID, coinType.NetworkID, err.Error())
}
log.Debugf("Restored state processing on curid =%v netid =%v", coinType.СurrencyID, coinType.NetworkID)
default:
log.Errorf("setGRPCHandlers: wrong СurrencyID")
}
} else {
log.Errorf("FetchLastSyncBlockState : h > 0")
}
} else {
log.Warnf("Restore last state is disabled for c = %v n = %v ", coinType.СurrencyID, coinType.NetworkID)
}
}