/
sync.go
264 lines (218 loc) · 6.9 KB
/
sync.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
package api
import (
"context"
"encoding/json"
"os"
"sync"
"time"
"github.com/bcext/cashutil"
"github.com/copernet/go-electrum/electrum"
"github.com/copernet/whccommon/log"
common "github.com/copernet/whccommon/model"
"github.com/copernet/whcwallet/config"
"github.com/copernet/whcwallet/logic/ws"
"github.com/copernet/whcwallet/model"
"github.com/jinzhu/gorm"
"github.com/sirupsen/logrus"
)
const (
pingInterval = 5 * time.Second
// Shutdown if ping to server failed five times continuously.
// Because the current program rely on electrum server seriously.
// This system will not supply any most services if connected electrum
// server work ill.
retryLimit = 5
// max retry times fro subscribing bitcoin cash balance update
retrySubscribe = 3
)
var (
// prevent from creating two more connection instances
create sync.Mutex
node *electrum.Node
)
func getNode() *electrum.Node {
create.Lock()
defer create.Unlock()
if node != nil && node.Ping() == nil {
return node
}
n := electrum.NewNode()
if err := n.ConnectTCP(config.GetConf().Electron.Host + ":" + config.GetConf().Electron.Port); err != nil {
logrus.Errorf("create connection to electrum error: %v", err)
// unnecessary to star the application if electrum connection failed
//os.Exit(1)
}
node = n
return node
}
func init() {
// debug mode. close this option in production environment
electrum.DebugMode = true
// keep connected to electrum server
go keepAlive()
tipChan, err := getNode().BlockchainHeadersSubscribe()
if err != nil {
logrus.Errorf("subscribe blockchain tip update from electrum error: %s", err)
}
go func() {
for tip := range tipChan {
ctx := log.NewContext()
log.WithCtx(ctx).Infof("block tip:%s,height:%d", model.UpdateBlockTip, tip.BlockHeight)
err := model.Publish(model.UpdateBlockTip, "tip updated")
//logic.DumpHeaders(ctx, tip)
if err != nil {
logrus.Error("notify whcengine to update blockchain failed")
}
}
}()
c := make(chan []byte)
go func() {
err := model.Subscribe(context.Background(), model.BalanceUpdated, c)
if err != nil {
logrus.Errorf("subscribe channel for wormhole balance error: %s", err)
// panic to exit program because of initial failed.
panic(err)
}
}()
go func() {
for balMsg := range c {
ctx := log.NewContext()
log.WithCtx(ctx).Infof("Receive Message:%s from channel:", balMsg)
var balNotify common.BalanceNotify
err := json.Unmarshal(balMsg, &balNotify)
if err != nil {
log.WithCtx(ctx).Errorf("json unmarshal from redis balance notify failed: %v", err)
continue
}
address, err := cashutil.DecodeAddress(balNotify.Address, config.GetChainParam())
if err != nil {
log.WithCtx(ctx).Errorf("mismatch bitcoin address format: %v, address: %s", err, balNotify.Address)
continue
}
addr := address.EncodeAddress(true)
// prepare for notification
ret, err := model.GetNotificationItem(addr, balNotify.PropertyID, balNotify.TxID)
if err != nil {
if gorm.IsRecordNotFoundError(err) {
log.WithCtx(ctx).Warnf("notification item not found: address: %s", addr)
} else {
log.WithCtx(ctx).Errorf("get notification item failed: %v, %s", err, addr)
}
} else {
err = model.StoreNotification(addr, ret)
if err != nil {
log.WithCtx(ctx).Errorf("store notification item failed: %v, address: %s", err, addr)
}
}
connection, ok := connMgr.GetConnForAddress(addr)
// the client holds this address not online.
if !ok {
log.WithCtx(ctx).Warnf("the client holds this address not online, address: %s", addr)
continue
}
bal, err := GetBalanceFromCache([]string{addr}, true)
if err != nil {
log.WithCtx(ctx).WithField(log.DefaultTraceLabel, connection.Uid).
Errorf("get wormhole balance for address: %s error: %v", addr, err)
continue
}
var msg ws.Message
msg.Address = addr
msg.Symbol = ws.CoinWormhole
msg.Balance = bal[addr]
log.WithCtx(ctx).WithFields(logrus.Fields{
log.DefaultTraceLabel: connection.Uid,
}).Debugf("Subscribe: receive wormhole balance update message from redis, address: %s; balance: %v", msg.Address, msg.Balance)
// notify websocket
connMgr.MsgChan <- msg
}
}()
}
func SyncBalanceForBCH(addr cashutil.Address) {
base58Addr := addr.EncodeAddress(false)
bench32Addr := addr.EncodeAddress(true)
connection, ok := connMgr.GetConnForAddress(bench32Addr)
// the client holds this address not online.
if !ok {
logrus.Infof("the client connection has closed, address: %s", bench32Addr)
return
}
node := getNode()
balChan, err := node.BlockchainAddressSubscribe(base58Addr)
if err != nil {
// retry for subscribing bitcoin cash balance update notification.
for i := 0; i < retrySubscribe; i++ {
balChan, err = node.BlockchainAddressSubscribe(base58Addr)
if err == nil {
break
}
}
if err != nil {
logrus.WithFields(logrus.Fields{
log.DefaultTraceLabel: connection.Uid,
}).Errorf("track bch balance failed: %v; address: %s", err, bench32Addr)
return
}
}
logrus.WithFields(logrus.Fields{
log.DefaultTraceLabel: connection.Uid,
}).Infof("start track bch balance for address: %s", bench32Addr)
go func() {
defer func() {
connection.Close()
logrus.WithFields(logrus.Fields{
log.DefaultTraceLabel: connection.Uid,
}).Infof("goroutine exit for balance track: %s", bench32Addr)
}()
for {
select {
case <-connection.Quit:
logrus.WithFields(logrus.Fields{
log.DefaultTraceLabel: connection.Uid,
}).Infof("stop track bch balance for address: %s", bench32Addr)
return
case <-balChan:
bal, err := getNode().BlockchainAddressGetBalance(base58Addr)
if err != nil {
// only log this error if encountering some error.
// It will request again when the coin confirmed.
// keep the goroutine running.
logrus.WithFields(logrus.Fields{
log.DefaultTraceLabel: connection.Uid,
}).Errorf("get BCH balance from electrum server error: %v; address: %s", err, bench32Addr)
continue
}
// assemble websocket message
var msg ws.Message
msg.Address = bench32Addr
msg.Balance = ws.BCHBalance{
Confirmed: bal.Confirmed.ToBCH(),
Unconfirmed: bal.Unconfirmed.ToBCH(),
}
msg.Symbol = ws.CoinBch
logrus.WithFields(logrus.Fields{
log.DefaultTraceLabel: connection.Uid,
}).Debugf("Subscribe: receive bitcoin-cash balance message from electrum server, address: %s; balance: %v", bench32Addr, msg.Balance)
// update websocket
connMgr.MsgChan <- msg
}
}
}()
}
func keepAlive() {
var reties int
for {
if err := getNode().Ping(); err != nil {
reties++
if reties >= retryLimit {
logrus.Error("Retry to connect to electrum server failed too many times")
// Should find a HA resolution before stop exit program directly
os.Exit(1)
}
logrus.Errorf("Ping to electrum server error: %v", err)
} else if reties != 0 {
reties = 0
}
time.Sleep(pingInterval)
}
}