This repository was archived by the owner on Dec 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode-service.go
151 lines (128 loc) · 3.9 KB
/
node-service.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
/*
Copyright 2018 Idealnaya rabota LLC
Licensed under Multy.io license.
See LICENSE for details
*/
package node
import (
"fmt"
"net"
"sync"
"time"
"github.com/Multy-io/Multy-ETH-node-service/eth"
pb "github.com/Multy-io/Multy-ETH-node-service/node-streamer"
"github.com/Multy-io/Multy-ETH-node-service/streamer"
"github.com/Multy-io/Multy-back/store"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/jekabolt/slf"
_ "github.com/jekabolt/slflog"
"google.golang.org/grpc"
)
var log = slf.WithContext("NodeClient").WithCaller(slf.CallerShort)
// NodeClient is a main struct of service
type NodeClient struct {
Config *Configuration
Instance *eth.Client
GRPCserver *streamer.Server
Clients *sync.Map // address to userid
CliMultisig *eth.Multisig
}
// Init initializes Multy instance
func (nc *NodeClient) Init(conf *Configuration) (*NodeClient, error) {
resyncUrl := fetchResyncUrl(conf.NetworkID)
conf.ResyncUrl = resyncUrl
nc = &NodeClient{
Config: conf,
}
var usersData sync.Map
usersData.Store("address", store.AddressExtended{
UserID: "kek",
WalletIndex: 1,
AddressIndex: 2,
})
// initail initialization of clients data
nc.Clients = &usersData
//TODO: init contract clients
multisig := eth.Multisig{
FactoryAddress: conf.MultisigFactory,
UsersContracts: sync.Map{},
}
multisig.UsersContracts.Store("0x7d2d50791f839aea9b3ebe2c1dfd4dea13bc12ca", "0x116FfA11DD8829524767f561da5d33D3D170E17d")
// initail initialization of clients contracts data
nc.CliMultisig = &multisig
log.Infof("Users data initialization done")
// init gRPC server
lis, err := net.Listen("tcp", conf.GrpcPort)
if err != nil {
return nil, fmt.Errorf("failed to listen: %v", err.Error())
}
// Creates a new gRPC server
ethCli := eth.NewClient(&conf.EthConf, nc.Clients, nc.CliMultisig)
if err != nil {
return nil, fmt.Errorf("eth.NewClient initialization: %s", err.Error())
}
log.Infof("ETH client initialization done")
nc.Instance = ethCli
// Dial to abi client to reach smart contracts methods
ABIconn, err := ethclient.Dial(conf.AbiClientUrl)
if err != nil {
log.Fatalf("Failed to connect to infura %v", err)
}
s := grpc.NewServer()
srv := streamer.Server{
UsersData: nc.Clients,
EthCli: nc.Instance,
Info: &conf.ServiceInfo,
Multisig: nc.CliMultisig,
NetworkID: conf.NetworkID,
ResyncUrl: resyncUrl,
EtherscanAPIKey: conf.EtherscanAPIKey,
EtherscanAPIURL: conf.EtherscanAPIURL,
ABIcli: ABIconn,
GRPCserver: s,
Listener: lis,
ReloadChan: make(chan struct{}),
}
nc.GRPCserver = &srv
pb.RegisterNodeCommunicationsServer(s, &srv)
go s.Serve(lis)
go WathReload(srv.ReloadChan, nc)
return nc, nil
}
func fetchResyncUrl(networkid int) string {
switch networkid {
case 4:
return "http://api-rinkeby.etherscan.io/api?sort=asc&endblock=99999999&startblock=0&address="
case 3:
return "http://api-ropsten.etherscan.io/api?sort=asc&endblock=99999999&startblock=0&address="
case 1:
return "http://api.etherscan.io/api?sort=asc&endblock=99999999&startblock=0&address="
default:
return "http://api.etherscan.io/api?sort=asc&endblock=99999999&startblock=0&address="
}
}
func WathReload(reload chan struct{}, cli *NodeClient) {
// func WathReload(reload chan struct{}, s *grpc.Server, srv *streamer.Server, lis net.Listener, conf *Configuration) {
for {
select {
case _ = <-reload:
ticker := time.NewTicker(1000 * time.Millisecond)
err := cli.GRPCserver.Listener.Close()
if err != nil {
log.Errorf("WathReload:lis.Close %v", err.Error())
}
cli.GRPCserver.GRPCserver.Stop()
log.Debugf("WathReload:Successfully stopped")
for _ = range ticker.C {
close(cli.Instance.RPCStream)
_, err := cli.Init(cli.Config)
if err != nil {
log.Errorf("WathReload:Init %v ", err)
continue
}
log.Debugf("WathReload:Successfully reloaded")
return
}
}
}
}