-
Notifications
You must be signed in to change notification settings - Fork 5
/
nepa.go
216 lines (167 loc) Β· 6.17 KB
/
nepa.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
package nepa
import (
"context"
"sync"
"time"
"github.com/benleb/gloomberg/internal"
"github.com/benleb/gloomberg/internal/gbl"
"github.com/benleb/gloomberg/internal/nemo/gloomberg"
"github.com/benleb/gloomberg/internal/nemo/totra"
"github.com/benleb/gloomberg/internal/pusu"
"github.com/benleb/gloomberg/internal/style"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/spf13/viper"
)
type (
NodeID int
NePa struct {
// channels to receive new blocks or logs from the chain/subscriptions
newHeads chan *types.Header
newLogs chan types.Log
// workQueue chan any
Transactions chan *types.Transaction
QueueTokenTransactions chan *totra.TokenTransaction
gb *gloomberg.Gloomberg
knownTransactions map[common.Hash]bool
knownTransactionsMu *sync.RWMutex
}
)
func NewNePa(gb *gloomberg.Gloomberg, queueTokenTransactions chan *totra.TokenTransaction) *NePa {
if queueTokenTransactions == nil {
queueTokenTransactions = make(chan *totra.TokenTransaction, 10240)
}
// create new np
np := &NePa{
newHeads: make(chan *types.Header, 10240),
newLogs: make(chan types.Log, 10240),
Transactions: make(chan *types.Transaction, 10240),
QueueTokenTransactions: queueTokenTransactions,
knownTransactions: make(map[common.Hash]bool, 0),
knownTransactionsMu: &sync.RWMutex{},
gb: gb,
}
return np
}
func (np *NePa) Run() {
// handle received logs
for workerID := 1; workerID <= viper.GetInt("server.workers.newLogHandler"); workerID++ {
go np.newLogHandler()
}
// // if preferred (formerly 'local') nodes are available, we just subscribe to them
// var ethNodes []*nodes.Node
// if len(np.gb.Nodes.GetLocalNodes()) > 0 {
// ethNodes = np.gb.Nodes.GetLocalNodes()
// } else {
// ethNodes = *np.gb.Nodes
// }
// subscribe
subscribedTo, err := np.gb.ProviderPool.Subscribe(np.newLogs)
if err != nil {
gbl.Log.Fatalf("β subscribing to logs failed: %s", err)
return
}
gbl.Log.Infof("βοΈ subscribed to logs via %d nodes", subscribedTo)
// for _, node := range ethNodes {
// // subscribe to all logs with "Tranfer" or "TransferSingle" as first topic
// if _, err := node.SubscribeToAllTransfers(np.newLogs); err != nil {
// gbl.Log.Warnf("subscribe to topic TransferSingle via node %d failed: %s", node.NodeID, err)
// } else {
// gbl.Log.Infof("βοΈ subscribed to all transfer topics via node %s", style.Bold(node.Name))
// }
// }
//
// pubsub subscribe to sales
if viper.GetBool("pubsub.sales.subscribe") {
gbl.Log.Infof("π subscribing to sales via redis on channel %s", internal.PubSubChannelSales)
for workerID := 1; workerID <= viper.GetInt("server.workers.subscription_logs"); workerID++ {
go pusu.SubscribeToSales(np.gb, internal.PubSubChannelSales, np.QueueTokenTransactions)
}
}
select {}
}
// newLogHandler handles new logs from an ethNode and fetches the complete tx for it.
func (np *NePa) newLogHandler() {
gbl.Log.Debugf("𧱠starting newLogHandler")
for log := range np.newLogs {
// filter logs without topics
if len(log.Topics) == 0 {
continue
}
// filter logs without erc721/1155 transfer (len(topics) == 4)
if len(log.Topics) != 4 {
continue
}
// skip if we already processed this logs tx
np.knownTransactionsMu.Lock()
known, ok := np.knownTransactions[log.TxHash]
np.knownTransactionsMu.Unlock()
if known && ok {
// we already know this transaction
gbl.Log.Debugf("already known log/transaction: %s", style.BoldStyle.Render(log.TxHash.String()))
continue
}
np.knownTransactionsMu.Lock()
np.knownTransactions[log.TxHash] = true
np.knownTransactionsMu.Unlock()
//
// get the transaction
tx, err := np.gb.ProviderPool.TransactionByHash(context.Background(), log.TxHash)
if err != nil {
gbl.Log.Infof("β getting %s failed: %s", style.TerminalLink("https://etherscan.io/tx/"+log.TxHash.String(), "transaction"), err)
continue
} else if tx == nil {
gbl.Log.Infof("β %s is nil", style.TerminalLink("https://etherscan.io/tx/"+log.TxHash.String(), "transaction"))
continue
}
// filter contract creation transactions (`to` is the zeroAddress)
if tx.To() == nil {
continue
}
// np.Transactions <- tx
//
// get the receipt including the logs
receipt, err := np.gb.ProviderPool.TransactionReceipt(context.Background(), tx.Hash())
if err != nil {
gbl.Log.Infof("βοΈ error getting %s receipt: %s", style.TerminalLink("https://etherscan.io/tx/"+tx.Hash().String(), "transaction"), err)
continue
} else if receipt == nil {
gbl.Log.Infof("βοΈ %s receipt is nil", style.TerminalLink("https://etherscan.io/tx/"+tx.Hash().String(), "transaction"))
continue
}
gbl.Log.Debugf("𧱠queue: %d | txLogFetcher - logs: %d", len(np.Transactions), len(receipt.Logs))
//
// create a TokenTransaction
if ttx := totra.NewTokenTransaction(tx, receipt, np.gb.ProviderPool); ttx != nil && ttx.IsMovingNFTs() {
np.QueueTokenTransactions <- ttx
// publish ttx via redis
if viper.GetBool("pubsub.sales.publish") {
go pusu.Publish(np.gb, internal.PubSubChannelSales, ttx)
}
}
np.gb.ProviderPool.LastLogReceivedAt = time.Now()
}
}
// func (np *NePa) txHandler() {
// gbl.Log.Info("𧱠starting txLogFetcher worker")
// for tx := range np.Transactions {
// receipt, err := np.gb.Nodes.TransactionReceipt(context.Background(), tx.Hash())
// if err != nil {
// gbl.Log.Infof("βοΈ error getting %s receipt: %s", style.TerminalLink("https://etherscan.io/tx/"+tx.Hash().String(), "transaction"), err)
// continue
// } else if receipt == nil {
// gbl.Log.Infof("βοΈ %s receipt is nil", style.TerminalLink("https://etherscan.io/tx/"+tx.Hash().String(), "transaction"))
// continue
// }
// gbl.Log.Debugf("𧱠queue: %d | txLogFetcher - logs: %d", len(np.Transactions), len(receipt.Logs))
// //
// // create a TokenTransaction
// if ttx := totra.NewTokenTransaction(tx, receipt, np.gb.Nodes.GetRandomNode()); ttx != nil && ttx.IsMovingNFTs() {
// np.QueueTokenTransactions <- ttx
// // publish ttx via redis
// if viper.GetBool("pubsub.sales.publish") {
// go pusu.Publish(np.gb, internal.PubSubChannelSales, ttx)
// }
// }
// }
// }