-
Notifications
You must be signed in to change notification settings - Fork 5
/
flo.go
257 lines (234 loc) · 5.79 KB
/
flo.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
package flo
import (
"context"
"io/ioutil"
"net"
"time"
"github.com/azer/logger"
"github.com/bitspill/flod/chaincfg"
"github.com/bitspill/flod/chaincfg/chainhash"
"github.com/bitspill/flod/flojson"
"github.com/bitspill/flod/rpcclient"
"github.com/bitspill/flod/wire"
"github.com/bitspill/flosig"
"github.com/bitspill/floutil"
"github.com/cloudflare/backoff"
"github.com/pkg/errors"
"github.com/oipwg/oip/config"
"github.com/oipwg/oip/events"
)
var (
clients []*rpcclient.Client
)
func AddCore(host, user, pass string) error {
cfg := &rpcclient.ConnConfig{
Host: host,
User: user,
Pass: pass,
DisableTLS: true,
HTTPPostMode: true,
}
c, err := rpcclient.New(cfg, nil)
clients = append(clients, c)
return err
}
func WaitForFlod(ctx context.Context, host, user, pass string, tls bool) error {
attempts := 0
a := logger.Attrs{"host": host, "attempts": attempts}
b := backoff.NewWithoutJitter(10*time.Minute, 1*time.Second)
t := log.Timer()
defer t.End("WaitForFlod", a)
for {
attempts++
a["attempts"] = attempts
log.Info("attempting connection to flod", a)
err := AddFlod(host, user, pass, tls)
if err != nil {
a["err"] = err
log.Error("unable to connect to flod", a)
delete(a, "err")
c := errors.Cause(err)
if _, ok := c.(*net.OpError); !ok {
// not a network error, something else is wrong
return err
}
// it's a network error, delay and retry
d := b.Duration()
a["delay"] = d
log.Info("delaying connection to flod retry", a)
delete(a, "delay")
select {
case <-ctx.Done():
a["err"] = ctx.Err()
log.Error("context timeout/cancelled", a)
return ctx.Err()
case <-time.After(d):
// loop around for another try
}
} else {
break
}
}
return nil
}
func AddFlod(host, user, pass string, tls bool) error {
var certs []byte
var err error
if tls {
certFile := config.GetFilePath("flod.certFile")
certs, err = ioutil.ReadFile(certFile)
if err != nil {
return errors.Wrap(err, "unable to read rpc.cert")
}
}
ntfnHandlers := rpcclient.NotificationHandlers{
OnFilteredBlockConnected: func(height int32, header *wire.BlockHeader, txns []*floutil.Tx) {
log.Info("Block connected: %v (%d) %v",
header.BlockHash(), height, header.Timestamp)
events.Publish("flo:notify:onFilteredBlockConnected", height, header, txns)
},
OnFilteredBlockDisconnected: func(height int32, header *wire.BlockHeader) {
log.Info("Block disconnected: %v (%d) %v",
header.BlockHash(), height, header.Timestamp)
events.Publish("flo:notify:onFilteredBlockDisconnected", height, header)
},
OnTxAcceptedVerbose: func(txDetails *flojson.TxRawResult) {
log.Info("Incoming TX: %v (Block: %v) floData: %v", txDetails.Txid, txDetails.BlockHash, txDetails.FloData)
events.Publish("flo:notify:onTxAcceptedVerbose", txDetails)
},
}
cfg := &rpcclient.ConnConfig{
Host: host,
Endpoint: "ws",
User: user,
Pass: pass,
DisableTLS: !tls,
Certificates: certs,
}
c, err := rpcclient.New(cfg, &ntfnHandlers)
if err != nil {
return errors.Wrap(err, "unable to create new rpc client")
}
clients = append(clients, c)
return nil
}
func Disconnect() {
if len(clients) == 1 {
clients[0].Disconnect()
} else {
for _, c := range clients {
c.Disconnect()
}
}
}
func GetBlockCount() (blockCount int64, err error) {
err = errors.New("no clients connected")
if len(clients) == 1 {
blockCount, err = clients[0].GetBlockCount()
} else {
for _, c := range clients {
blockCount, err = c.GetBlockCount()
if err == nil {
return
}
}
}
return
}
func BeginNotifyBlocks() (err error) {
err = errors.New("no clients connected")
if len(clients) == 1 {
err = clients[0].NotifyBlocks()
} else {
for _, c := range clients {
err = c.NotifyBlocks()
if err == nil {
return
}
}
}
return
}
func BeginNotifyTransactions() (err error) {
err = errors.New("no clients connected")
if len(clients) == 1 {
err = clients[0].NotifyNewTransactions(true)
} else {
for _, c := range clients {
err = c.NotifyNewTransactions(true)
if err == nil {
return
}
}
}
return
}
func GetFirstClient() *rpcclient.Client {
if len(clients) > 0 {
return clients[0]
}
return nil
}
func GetBlockHash(i int64) (hash *chainhash.Hash, err error) {
err = errors.New("no clients connected")
for _, c := range clients {
hash, err = c.GetBlockHash(i)
if err == nil {
return
}
}
return
}
func GetBlockVerboseTx(hash *chainhash.Hash) (br *flojson.GetBlockVerboseResult, err error) {
err = errors.New("no clients connected")
if len(clients) == 1 {
br, err = clients[0].GetBlockVerboseTx(hash)
} else {
for _, c := range clients {
br, err = c.GetBlockVerboseTx(hash)
if err == nil {
return
}
}
}
return
}
func GetTxVerbose(hash *chainhash.Hash) (tr *flojson.TxRawResult, err error) {
err = errors.New("no clients connected")
if len(clients) == 1 {
tr, err = clients[0].GetRawTransactionVerbose(hash)
} else {
for _, c := range clients {
tr, err = c.GetRawTransactionVerbose(hash)
if err == nil {
return
}
}
}
return
}
func CheckAddress(address string) (bool, error) {
var err error
if config.IsTestnet() {
_, err = floutil.DecodeAddress(address, &chaincfg.TestNet3Params)
} else {
_, err = floutil.DecodeAddress(address, &chaincfg.MainNetParams)
}
if err != nil {
return false, err
}
return true, nil
}
func CheckSignature(address, signature, message string) (bool, error) {
var ok bool
var err error
if config.IsTestnet() {
ok, err = flosig.CheckSignature(address, signature, message, "Florincoin", &chaincfg.TestNet3Params)
} else {
ok, err = flosig.CheckSignature(address, signature, message, "Florincoin", &chaincfg.MainNetParams)
}
if !ok && err == nil {
err = errors.New("bad signature")
}
return ok, err
}