-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathblock_hash_handler.go
89 lines (74 loc) · 2.04 KB
/
block_hash_handler.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
package ethNotification
import (
"context"
"log"
"time"
)
type blockHashHandler struct {
engine *Engine
hash string
}
func NewBlockHashHandler(e *Engine, hash string) blockHashHandler {
return blockHashHandler{
engine: e,
hash: hash,
}
}
func (hdl blockHashHandler) Handle() error {
blockInfo, err := hdl.fetchInfoBlock()
if err != nil {
log.Println("Block error: ", err.Error())
return err
}
if blockInfo == nil {
return nil
}
if hdl.engine.isAllowPendingTx {
hdl.hanlePushWithCache(blockInfo)
} else {
hdl.hanlePushWithoutCache(blockInfo)
}
return nil
}
func (hdl blockHashHandler) fetchInfoBlock() (*Block, error) {
time.Sleep(2 * time.Second)
blockInfo := Block{}
err := hdl.engine.cB.CallContext(context.Background(), &blockInfo, "eth_getBlockByHash", hdl.hash, true)
if err != nil {
return nil, err
}
return &blockInfo, nil
}
func (hdl blockHashHandler) fetchTransactionsInBlock(b *Block) error {
batchElems := generateTxReceiptBatchElements(b)
return hdl.engine.cB.BatchCallContext(context.Background(), batchElems)
}
func (hdl blockHashHandler) hanlePushWithCache(b *Block) {
err := hdl.fetchTransactionsInBlock(b)
if err != nil {
log.Println("Error fetch tx from block: ", err)
return
}
b.Transactions = updateTransactionFromReceipt(hdl.engine.tokenDataSource, b.Transactions)
for i, tran := range b.Transactions {
cd, err := hdl.engine.cacheData.Get(tran.Hash)
if err == nil {
go func(tx *Transaction) {
hdl.engine.pushMessage(tx, cd.WalletSubscribers)
hdl.engine.cacheData.Remove(tx.Hash)
}(&b.Transactions[i])
}
}
}
func (hdl blockHashHandler) hanlePushWithoutCache(b *Block) {
err := hdl.fetchTransactionsInBlock(b)
if err != nil {
log.Println("Error fetch tx from block: ", err)
return
}
b.Transactions = updateTransactionFromReceipt(hdl.engine.tokenDataSource, b.Transactions)
walletSubscribersRes := hdl.engine.dataSource.FindWalletSubscribers(b.Transactions)
for _, wsr := range walletSubscribersRes {
go hdl.engine.pushMessage(wsr.Transaction, wsr.Subscribers)
}
}