-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.go
312 lines (259 loc) · 8.11 KB
/
chain.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
package main
import (
"encoding/json"
"fmt"
"log"
"math"
"net/url"
"os"
"os/signal"
"strconv"
"strings"
"time"
"github.com/gorilla/websocket"
)
type Transaction struct {
Type string `json:"type"`
Hash string `json:"hash"`
BlockHash string `json:"blockHash"`
Timestamp int64 `json:"timestamp"`
Inputs []struct {
OutputRef struct {
Hint int `json:"hint"`
Key string `json:"key"`
} `json:"outputRef"`
UnlockScript string `json:"unlockScript"`
TxHashRef string `json:"txHashRef"`
Address string `json:"address"`
AttoAlphAmount string `json:"attoAlphAmount"`
} `json:"inputs"`
Outputs []struct {
Type string `json:"type"`
Hint int `json:"hint"`
Key string `json:"key"`
AttoAlphAmount string `json:"attoAlphAmount"`
Address string `json:"address"`
Tokens []struct {
ID string `json:"id"`
Amount string `json:"amount"`
} `json:"tokens,omitempty"`
Message string `json:"message"`
Spent string `json:"spent"`
} `json:"outputs"`
GasAmount int `json:"gasAmount"`
GasPrice string `json:"gasPrice"`
ScriptExecutionOk bool `json:"scriptExecutionOk"`
Coinbase bool `json:"coinbase"`
}
type Method string
const (
block_notify Method = "block_notify"
)
type Token struct {
ID string `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals int `json:"decimals"`
Description string `json:"description"`
LogoURI string `json:"logoURI"`
}
type TokenList struct {
NetworkID int `json:"networkId"`
Tokens []Token `json:"tokens"`
}
type Ws struct {
Method Method `json:"method"`
Params struct {
Hash string `json:"hash"`
Timestamp int64 `json:"timestamp"`
ChainFrom int `json:"chainFrom"`
ChainTo int `json:"chainTo"`
Height int `json:"height"`
Deps []string `json:"deps"`
Transactions []struct {
Unsigned struct {
TxID string `json:"txId"`
Version int `json:"version"`
NetworkID int `json:"networkId"`
GasAmount int `json:"gasAmount"`
GasPrice string `json:"gasPrice"`
Inputs []any `json:"inputs"`
FixedOutputs []struct {
Hint int `json:"hint"`
Key string `json:"key"`
AttoAlphAmount string `json:"attoAlphAmount"`
Address string `json:"address"`
Tokens []any `json:"tokens"`
LockTime int64 `json:"lockTime"`
Message string `json:"message"`
} `json:"fixedOutputs"`
} `json:"unsigned"`
ScriptExecutionOk bool `json:"scriptExecutionOk"`
ContractInputs []any `json:"contractInputs"`
GeneratedOutputs []any `json:"generatedOutputs"`
InputSignatures []any `json:"inputSignatures"`
ScriptSignatures []any `json:"scriptSignatures"`
} `json:"transactions"`
Nonce string `json:"nonce"`
Version int `json:"version"`
DepStateHash string `json:"depStateHash"`
TxsHash string `json:"txsHash"`
Target string `json:"target"`
GhostUncles []any `json:"ghostUncles"`
} `json:"params"`
Jsonrpc string `json:"jsonrpc"`
}
const maxRetry = 3600
var done chan interface{}
var interrupt chan os.Signal
// find transactions in each blocks
func getBlocksFullnode(ch chan Tx) {
//interrupt := make(chan os.Signal, 1)
//signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "wss", Host: parameters.WsFullnode, Path: "/events"}
done = make(chan interface{}) // Channel to indicate that the receiverHandler is done
interrupt = make(chan os.Signal) // Channel to listen for interrupt signal to terminate gracefully
signal.Notify(interrupt, os.Interrupt) // Notify the interrupt channel for SIGINT
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("Error connecting to Websocket Server:", err)
}
defer conn.Close()
go receiveHandler(conn, ch)
for {
select {
case <-time.After(time.Duration(10) * time.Millisecond * 1000):
// Send an echo packet every 10 second
err := conn.WriteMessage(websocket.TextMessage, []byte("ping"))
if err != nil {
log.Println("Error during writing to websocket:", err)
return
}
case <-interrupt:
// We received a SIGINT (Ctrl + C). Terminate gracefully...
log.Println("Received SIGINT interrupt signal. Closing all pending connections")
// Close our websocket connection
err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("Error during closing websocket:", err)
return
}
select {
case <-done:
log.Println("Receiver Channel Closed! Exiting....")
case <-time.After(time.Duration(1) * time.Second):
log.Println("Timeout in closing receiving channel. Exiting....")
}
return
}
}
}
func receiveHandler(connection *websocket.Conn, ch chan Tx) {
defer close(done)
for {
_, msg, err := connection.ReadMessage()
if err != nil {
log.Println("Error in receive:", err)
return
}
//log.Printf("Received: %s\n", msg)
var data Ws
json.Unmarshal([]byte(msg), &data)
getTxIdWs(&data, ch)
//log.Printf("%+v", data)
}
}
func getTxIdWs(block *Ws, chTxs chan Tx) {
if block.Method == block_notify {
for _, tx := range block.Params.Transactions {
//fmt.Println(tx.Unsigned.TxID)
// no input mean coinbase tx
if len(tx.Unsigned.Inputs) > 0 {
txId := Tx{id: tx.Unsigned.TxID, groupFrom: block.Params.ChainFrom, groupTo: block.Params.ChainTo}
chTxs <- txId
}
}
}
}
func getTxStateExplorer(txId string, tx *Transaction) bool {
dataBytes, statusCode, err := getHttp(fmt.Sprintf("%s/transactions/%s", parameters.ExplorerApi, txId))
if err != nil { // do not print error if 404
log.Printf("Error get data from explorer\n%s\n", err)
return false
}
if statusCode != 404 && statusCode != 200 {
fmt.Fprintf(os.Stderr, "Error fullnode explorer getting status: %v\n", err)
fmt.Fprintf(os.Stderr, "Status code: %d\n", statusCode)
panic(err)
}
if statusCode == 200 && len(dataBytes) > 0 {
err := json.Unmarshal(dataBytes, &tx)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot unmarshall data, err: %s\n", err)
panic(err)
}
}
if strings.ToLower(tx.Type) == "accepted" {
return true
}
return false
}
func getTxData(txId Tx, chMessages chan Message, wId int) {
var txData Transaction
cntRetry := 0
log.Printf("worker %d check %s\n", wId, txId.id)
for {
if getTxStateExplorer(txId.id, &txData) {
break
}
if cntRetry >= maxRetry {
return
}
cntRetry++
time.Sleep(1 * time.Second)
}
//log.Printf("Input %+v\n", txData)
addressIn := txData.Inputs[0].Address
var addressOut string
for _, output := range txData.Outputs {
addressOut = output.Address
txType := output.Type
if strings.ToLower(txType) == "contractoutput" {
return
}
if strings.ToLower(txType) == "assetoutput" {
attoStrToFloat, err := strconv.ParseFloat(output.AttoAlphAmount, 32)
hintAmountALPH := attoStrToFloat / baseAlph
if hintAmountALPH >= parameters.MinAmountTrigger {
if addressIn != addressOut {
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling BlockflowApi.GetBlockflowBlocks: %v\n", err)
}
chMessages <- Message{addressIn, addressOut, hintAmountALPH, txId.id, Token{}, txId.groupFrom, txId.groupTo}
}
}
if len(output.Tokens) > 0 {
for _, token := range output.Tokens {
if amountTrigger, found := trackTokens[token.ID]; found {
tokenData := searchTokenData(token.ID)
if tokenData.Name == "" {
log.Printf("error cannot found info for token %s", token.ID)
}
tokenAmount, err := strconv.ParseFloat(token.Amount, 64)
if err != nil {
log.Printf("Cannot parse ayin amount, err: %s\n", err)
return
}
decimal := float64(tokenData.Decimals)
amount := tokenAmount / math.Pow(10.0, decimal)
if amount >= float64(amountTrigger) {
if addressIn != addressOut {
chMessages <- Message{addressIn, addressOut, tokenAmount, txId.id, tokenData, txId.groupFrom, txId.groupTo}
}
}
}
}
}
}
}
}