-
Notifications
You must be signed in to change notification settings - Fork 6
/
parse.go
356 lines (324 loc) · 9.7 KB
/
parse.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
// Copyright 2019 The fsn-go-sdk Authors
// This file is part of the fsn-go-sdk library.
//
// The fsn-go-sdk library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The fsn-go-sdk library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the fsn-go-sdk library. If not, see <http://www.gnu.org/licenses/>.
package syncer
import (
"fmt"
"math/big"
"strings"
"sync"
"github.com/fsn-dev/fsn-go-sdk/efsn/common"
"github.com/fsn-dev/fsn-go-sdk/efsn/common/hexutil"
"github.com/fsn-dev/fsn-go-sdk/efsn/core/types"
"github.com/fsn-dev/fsn-go-sdk/efsn/log"
"github.com/fsn-dev/fsn-go-sdk/efsn/rlp"
"github.com/fsn-dev/fsn-go-sdk/efsn/tools"
"github.com/fsn-dev/fsn-go-sdk/mongosync/mongodb"
"gopkg.in/mgo.v2"
)
const (
averageBlockTime uint64 = 13
defaultCoinType string = "FSN"
defaultTxType string = "Origin"
defaultTryTimes int = 3
maxParseBlocks int = 1000
)
func tryDoTimes(name string, f func() error) (err error) {
defer func() {
if errf := recover(); errf != nil {
log.Error("recover from error", "err", errf)
err = fmt.Errorf("recover from error %v", errf)
}
}()
i := 0
for ; i < defaultTryTimes; i++ {
err = f()
if err == nil || mgo.IsDup(err) {
return nil
}
}
log.Warn("tryDoTimes", "name", name, "time", i, "err", err)
return err
}
func (w *Worker) Parse(block *types.Block, receipts types.Receipts) {
msg := &Message{
block: block,
receipts: receipts,
}
w.messageChan <- msg
}
func (w *Worker) startParser(wg *sync.WaitGroup) {
defer wg.Done()
count := 0
wg2 := new(sync.WaitGroup)
for {
select {
case msg := <-w.messageChan:
if msg == nil {
return
}
count++
wg2.Add(2)
// parse block
go w.parseBlock(msg.block, msg.receipts, wg2)
// parse transactions
go w.parseTransactions(msg.block, msg.receipts, wg2)
if count == maxParseBlocks {
count = 0
wg2.Wait() // prevent memory exhausted (when blocks too large)
}
}
}
wg2.Wait()
}
func (w *Worker) parseBlock(block *types.Block, receipts types.Receipts, wg *sync.WaitGroup) {
defer wg.Done()
mb := new(mongodb.MgoBlock)
hash := block.Hash().String()
mb.Key = hash
mb.Number = block.NumberU64()
mb.Hash = hash
mb.ParentHash = block.ParentHash().String()
mb.Nonce = block.Nonce()
mb.Miner = strings.ToLower(block.Coinbase().String())
mb.Difficulty = block.Difficulty().Uint64()
mb.TotalDifficulty = 0
mb.Size = uint64(block.Size())
mb.GasLimit = block.GasLimit()
mb.GasUsed = block.GasUsed()
mb.Timestamp = block.Time().Uint64()
mb.BlockTime = 0
mb.TxCount = block.Transactions().Len()
mb.AvgGasprice = calcAvgGasprice(block.Transactions()).String()
mb.Reward = calcBlockReward(block, receipts).String()
w.parseSnapshot(mb, block.Header())
tryDoTimes("AddBlock "+hash, func() error {
return mongodb.AddBlock(mb, Overwrite)
})
}
func calcAvgGasprice(txs types.Transactions) *big.Int {
txCount := int64(len(txs))
if txCount == 0 {
return big.NewInt(0)
}
sum := big.NewInt(0)
for _, tx := range txs {
sum = sum.Add(sum, tx.GasPrice())
}
return new(big.Int).Div(sum, big.NewInt(txCount))
}
func calcBlockReward(block *types.Block, receipts types.Receipts) *big.Int {
reward := big.NewInt(0)
if block.Number().Sign() > 0 {
reward = tools.GetBlockReward(block, receipts)
}
return reward
}
func (w *Worker) parseSnapshot(mb *mongodb.MgoBlock, header *types.Header) {
// Fusion related
snap, err := tools.NewSnapshotFromHeader(header)
if err != nil {
return
}
mb.SelectedTicket = snap.Selected.String()
mb.TicketNumber = snap.TicketNumber
retreatCount := len(snap.Retreat)
if retreatCount > 0 && header.Number.Sign() > 0 {
prevHeight := new(big.Int).Sub(header.Number, big.NewInt(1))
owners := w.getTicketsOwner(snap.Retreat, prevHeight)
mb.RetreatTickets = make([]string, retreatCount)
mb.RetreatMiners = make([]string, retreatCount)
for i := 0; i < retreatCount; i++ {
mb.RetreatTickets[i] = snap.Retreat[i].String()
mb.RetreatMiners[i] = strings.ToLower(owners[i].String())
}
}
}
func (w *Worker) parseTransactions(block *types.Block, receipts types.Receipts, wg *sync.WaitGroup) {
defer wg.Done()
wg.Add(len(block.Transactions()))
for i, tx := range block.Transactions() {
go w.parseTx(i, tx, block, receipts, wg)
}
}
func (w *Worker) parseTx(i int, tx *types.Transaction, block *types.Block, receipts types.Receipts, wg *sync.WaitGroup) {
defer wg.Done()
mt := new(mongodb.MgoTransaction)
receipt := receipts[i]
hash := tx.Hash().String()
mt.Key = hash
mt.Hash = hash
mt.Nonce = tx.Nonce()
mt.BlockHash = block.Hash().String()
mt.BlockNumber = block.NumberU64()
mt.TransactionIndex = i
mt.From = strings.ToLower(getTxSender(tx).String())
mt.To = "nil"
if tx.To() != nil {
mt.To = strings.ToLower(tx.To().String())
}
txValue := tx.Value()
mt.Value = txValue.String()
if txValue.Sign() > 0 {
mt.ValueInt = new(big.Int).Div(txValue, big.NewInt(1e18)).Uint64()
mt.ValueDec = new(big.Int).Mod(txValue, big.NewInt(1e18)).Uint64()
}
mt.GasLimit = tx.Gas()
mt.GasPrice = tx.GasPrice().String()
if receipt != nil {
mt.GasUsed = receipt.GasUsed
mt.Status = receipt.Status
}
mt.Timestamp = block.Time().Uint64()
txData := tx.Data()
if len(txData) > 0 {
mt.Input = hexutil.Encode(txData)
}
mt.CoinType = defaultCoinType
mt.Type = defaultTxType
if tx.To() == nil {
parseContractCreation(mt, tx, receipt)
} else if common.IsFsnCall(tx.To()) { // Fusion related
parseFsnTx(mt, tx, receipt)
} else {
if receipt != nil && len(receipt.Logs) != 0 {
mt.Log = parseReceiptLogs(receipt.Logs) // mt.Log
parseReceipt(mt, receipt) // erc20, exchange, etc
}
mc, _ := mongodb.FindContract(mt.To)
if mc != nil {
mt.Type = mc.Type
}
}
tryDoTimes("AddTransaction "+hash, func() error {
return mongodb.AddTransaction(mt, Overwrite)
})
}
func hashesToStrings(hashes []common.Hash) []string {
res := make([]string, len(hashes))
for k, v := range hashes {
res[k] = v.String()
}
return res
}
func parseReceiptLogs(rlogs []*types.Log) []map[string]interface{} {
if len(rlogs) == 0 {
return nil
}
logs := make([]map[string]interface{}, len(rlogs))
for k, v := range rlogs {
logs[k] = make(map[string]interface{})
res := logs[k]
res["contract"] = strings.ToLower(v.Address.String())
if len(v.Topics) == 0 {
continue
}
switch v.Topics[0] {
case common.LogFusionAssetReceivedTopic, common.LogFusionAssetSentTopic:
isReceive := v.Topics[0] == common.LogFusionAssetReceivedTopic
res["asset"] = v.Topics[1].String()
addr := strings.ToLower(common.BytesToAddress(v.Topics[2].Bytes()).String())
if isReceive {
res["from"] = addr
res["topic"] = "TimeLockContractReceive"
} else {
res["to"] = addr
res["topic"] = "TimeLockContractSend"
}
res["value"] = common.GetBigInt(v.Data, 0, 32).String()
res["start"] = common.GetBigInt(v.Data, 32, 32).String()
res["end"] = common.GetBigInt(v.Data, 64, 32).String()
res["flag"] = common.GetBigInt(v.Data, 96, 32).String()
default:
res["topics"] = hashesToStrings(v.Topics)
res["data"] = hexutil.Encode(v.Data)
}
}
return logs
}
func parseFsnTx(mt *mongodb.MgoTransaction, tx *types.Transaction, receipt *types.Receipt) {
var fsnCall common.FSNCallParam
rlp.DecodeBytes(tx.Data(), &fsnCall)
mt.Type = fsnCall.Func.Name() // mt.Type
if receipt == nil || len(receipt.Logs) == 0 {
return
}
logData, err := tools.DecodeLogData(receipt.Logs[0].Data)
if err != nil {
return
}
logMap, ok := logData.(map[string]interface{})
if !ok {
return
}
if fsnCall.Func == common.ReportIllegalFunc {
if del, ok := logMap["DeleteTickets"]; ok {
if delstr, ok := del.(string); ok {
deltickets, err := tools.DecodePunishTickets(delstr)
if err == nil {
logMap["DeleteTickets"] = hashesToStrings(deltickets)
}
}
}
}
mt.Log = logMap // mt.Log
if assetID, ok := logMap["AssetID"].(string); ok {
if common.HexToHash(assetID) != common.SystemAssetID {
mt.CoinType = assetID // mt.CoinType
}
}
if _, hasError := logMap["Error"]; hasError {
mt.Status = types.ReceiptStatusFailed // mt.Status
return
}
if fsnCall.Func == common.SendAssetFunc && mt.CoinType == defaultCoinType {
sendAssetParam := common.SendAssetParam{}
rlp.DecodeBytes(fsnCall.Data, &sendAssetParam)
mt.To = strings.ToLower(sendAssetParam.To.String()) // mt.To
txValue := sendAssetParam.Value
mt.Value = txValue.String() // mt.Value
if txValue.Sign() > 0 {
mt.ValueInt = new(big.Int).Div(txValue, big.NewInt(1e18)).Uint64() // mt.ValueInt
mt.ValueDec = new(big.Int).Mod(txValue, big.NewInt(1e18)).Uint64() // mt.ValueDec
}
}
}
func getTxSender(tx *types.Transaction) common.Address {
signer := types.NewEIP155Signer(tx.ChainId())
sender, _ := types.Sender(signer, tx)
return sender
}
func parseContractCreation(mt *mongodb.MgoTransaction, tx *types.Transaction, receipt *types.Receipt) {
mt.Type = "CreateContract"
if receipt == nil {
return
}
contractAddress := strings.ToLower(receipt.ContractAddress.String())
contractType := "unknown"
createCode := tx.Data()
switch {
case tools.IsMbtcContract(createCode):
contractType = "MBTC"
case tools.IsErc20Contract(createCode):
contractType = "ERC20"
}
mc := &mongodb.MgoContract{
Key: contractAddress,
Type: contractType,
}
tryDoTimes("AddContract "+contractAddress, func() error {
return mongodb.AddContract(mc)
})
}