-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
397 lines (350 loc) · 10.3 KB
/
setup.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Setup provides utility functions needed to make the test run
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"slices"
"strconv"
"strings"
sdk "github.com/TrueBlocks/trueblocks-core/sdk/v3"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
)
var supportedProviders = []string{
// "chifra",
"key",
"etherscan",
"covalent",
"alchemy",
}
type Comparison struct {
Database *Database
DatabaseFileName string
Providers []string
addressFilePath string
minAppearances int
maxAppearances int
}
// Setup initializes database connection and detects providers
func Setup(addressFile string, dataDir string, databaseFileName string) (c *Comparison) {
var err error
c = &Comparison{
addressFilePath: addressFile,
minAppearances: minAppearances,
maxAppearances: maxAppearances,
}
c.Database, err = NewDatabaseConnection(false, dataDir, databaseFileName)
if err != nil {
log.Fatalln("opening sqlite database:", err)
}
c.detectProviders("mainnet")
if len(c.Providers) == 0 {
log.Fatalln("no providers configured. Please see README")
}
return
}
// detectEnabledProviders goes over the list of providers and checks
// if we have API key for each one. If we do, then the provider is enabled.
func (c *Comparison) detectProviders(chain string) {
detected := make([]string, 0, len(supportedProviders))
var hasChifra bool
var hasKey bool
for _, providerName := range supportedProviders {
switch providerName {
case "chifra":
_, err := exec.LookPath("chifra")
if err == nil {
detected = append(detected, "chifra")
hasChifra = true
} else {
log.Println("you don't have chifra:", err)
}
continue
case "key":
if config.GetChain(chain).KeyEndpoint != "" {
detected = append(detected, "key")
hasKey = true
}
continue
default:
if config.GetKey(providerName).ApiKey != "" {
detected = append(detected, providerName)
}
}
if !hasChifra && !hasKey {
log.Fatalln("either chifra or Key is required")
}
}
c.Providers = detected
}
// stringToSlurpSource translates provider name to TrueBlocks SDK provider ID
// (--source value when using chifra slurp on the command line)
func stringToSlurpSource(name string) (source sdk.SlurpSource) {
switch name {
case "etherscan":
source = sdk.SSEtherscan
case "alchemy":
source = sdk.SSAlchemy
case "covalent":
source = sdk.SSCovalent
case "key":
source = sdk.SSKey
default:
log.Fatalln("unsupported provider", name)
}
return source
}
// DownloadAppearances downloads data from all providers and stores appearances
// in the database
func (c *Comparison) DownloadAppearances() (err error) {
// First we need to filter addresses with too few and too many appearances out
addressChan := make(chan string, 100)
filterByProvider := "key"
if slices.Contains(c.Providers, "chifra") {
filterByProvider = "chifra"
}
log.Println("Filtering by", filterByProvider)
providers := make([]string, 0, len(c.Providers))
for _, provider := range c.Providers {
if provider != filterByProvider {
providers = append(providers, provider)
}
}
go func() {
err = loadAddressesFromFile(c.addressFilePath, addressChan)
if err != nil {
log.Fatalln(err)
}
}()
for address := range addressChan {
log.Println(address)
appearances, ok, err := c.checkAddress(filterByProvider, address)
if err != nil {
return err
}
if !ok {
// Incompatible addresses are saved
log.Println("Address", address, "is incompatible")
_ = c.Database.SaveIncompatibleAddress(address, appearances)
continue
}
// Since we had to fetch appearances from the filtering provider
// (either chifra or Key), we will save them
for _, appearance := range appearances {
appData := AppearanceData{}
appData.Address = appearance.Address
appData.BlockNumber = appearance.BlockNumber
appData.TransactionIndex = appearance.TransactionIndex
// Get reason (where has chifra found the appearance)
appData.Reason, err = getChifraReason(&appearance)
if err != nil {
return err
}
// Check if there was a balance change
appData.BalanceChange, err = getChifraBalanceChange(&appearance)
if err != nil {
return err
}
if err = c.Database.SaveAppearance(filterByProvider, appData); err != nil {
return err
}
}
// Now we download the data from the other providers
for _, provider := range providers {
log.Println("Downloading from", provider, "address", address)
providerTypes := typesByProvider(provider)
for _, providerType := range providerTypes {
opts := sdk.SlurpOptions{
Source: stringToSlurpSource(provider),
Addrs: []string{address},
Parts: providerType,
}
appearances, _, err := opts.SlurpAppearances()
if err != nil {
log.Fatalln("error downloading data from", provider, err)
}
for _, appearance := range appearances {
appData := AppearanceData{}
appData.Address = appearance.Address
appData.BlockNumber = appearance.BlockNumber
appData.TransactionIndex = appearance.TransactionIndex
appData.Reason = providerType.String()
if err = c.Database.SaveAppearance(provider, appData); err != nil {
return err
}
}
}
}
}
return
}
// typesByProvider returns slice of sdk.SlurpParts that are supported by
// the given provider
func typesByProvider(provider string) (slurpTypes []sdk.SlurpParts) {
switch provider {
case "key", "covalent":
slurpTypes = []sdk.SlurpParts{sdk.SLPAll}
case "etherscan":
slurpTypes = []sdk.SlurpParts{
sdk.SPExt, sdk.SPInt, sdk.SPToken, sdk.SPNfts, sdk.SP1155, sdk.SPMiner, sdk.SPUncles, sdk.SPWithdrawals,
}
case "alchemy":
slurpTypes = []sdk.SlurpParts{
sdk.SPExt, sdk.SPInt, sdk.SPToken, sdk.SPNfts, sdk.SP1155,
}
}
return
}
// getChifraReason tells us where chifra has found the appearance (e.g. logs, from, etc.)
func getChifraReason(appearance *types.Appearance) (reason string, err error) {
transactionsOpts := sdk.TransactionsOptions{
TransactionIds: []string{
fmt.Sprintf("%d.%d", appearance.BlockNumber, appearance.TransactionIndex),
},
}
// Get reason
uniqTransactions, _, err := transactionsOpts.TransactionsUniq()
if err != nil {
return
}
for _, transaction := range uniqTransactions {
if transaction.Address != appearance.Address {
continue
}
reason = transaction.Reason
}
return
}
// getChifraBalanceChange returns true if the balance has changed
func getChifraBalanceChange(appearance *types.Appearance) (balanceChange bool, err error) {
stateOpts := sdk.StateOptions{
Addrs: []string{appearance.Address.String()},
Parts: sdk.SPBalance,
Changes: true,
BlockIds: []string{
// Range big enough to capture balance change caused by mining rewards
strconv.FormatInt(int64(appearance.BlockNumber-2), 10),
strconv.FormatInt(int64(appearance.BlockNumber+7), 10),
},
}
state, _, err := stateOpts.State()
if err != nil {
return
}
// there was a balance change
balanceChange = len(state) > 1
return
}
// checkAddress checks if the address appearance count doesn't exceed the limits
func (c *Comparison) checkAddress(provider string, address string) (appearances []types.Appearance, ok bool, err error) {
if provider == "chifra" {
listOpts := &sdk.ListOptions{
Addrs: []string{address},
}
appearances, _, err = listOpts.List()
} else {
opts := sdk.SlurpOptions{
Source: stringToSlurpSource(provider),
Addrs: []string{address},
Parts: sdk.SLPAll,
}
appearances, _, err = opts.SlurpAppearances()
}
// Always return false for addresses having 0 appearances, so they are stored as incompatible
// and not just disappear
if len(appearances) == 0 {
ok = false
return
}
ok = len(appearances) >= c.minAppearances && len(appearances) <= c.maxAppearances
return
}
func loadAddressesFromFile(filePath string, addressChan chan string) (err error) {
var addressFile *os.File
if filePath == "" {
addressFile = os.Stdin
} else {
addressFile, err = os.Open(filePath)
if err != nil {
log.Fatalln("opening addresses file:", err)
}
}
defer close(addressChan)
reader := bufio.NewReader(addressFile)
for {
address, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
// We are done
break
}
log.Fatalln("while processing addresses file:", err)
}
// Drop \n
sanitized := strings.TrimSuffix(address, "\n")
if len(sanitized) == 0 {
continue
}
addressChan <- sanitized
}
return
}
func (c *Comparison) Results() (r *Result, err error) {
r = &Result{
// Number of addresses found by the provider
AddressesBy: make(map[string]int, len(c.Providers)),
// Number of appearances found by the provider
AppearancesBy: make(map[string]int, len(c.Providers)),
// Number of addresses found ONLY by the provider
UniqueAddressesBy: make(map[string]int, len(c.Providers)),
// Number of appearances found ONLY by the provider
UniqueAppearancesBy: make(map[string]int, len(c.Providers)),
// Where did it find them?
GroupedReasonsBy: make(map[string][]GroupedReasons),
// Number of appearances involving balance change
BalanceChangesBy: make(map[string]int),
providers: c.Providers,
}
r.AddressCountTotal, err = c.Database.AddressCountTotal()
if err != nil {
err = fmt.Errorf("getting total appearance count: %w", err)
return
}
for _, provider := range c.Providers {
r.AppearancesBy[provider], err = c.Database.AppearanceCount(provider)
if err != nil {
err = fmt.Errorf("getting appearance count: %w", err)
return
}
r.AddressesBy[provider], err = c.Database.AddressCount(provider)
if err != nil {
err = fmt.Errorf("getting address count: %w", err)
return
}
r.UniqueAppearancesBy[provider], err = c.Database.UniqueAppearanceCount(provider)
if err != nil {
err = fmt.Errorf("getting unique appearance count: %w", err)
return
}
r.GroupedReasonsBy[provider], err = c.Database.UniqueAppearancesGroupedReasons(provider)
if err != nil {
err = fmt.Errorf("getting grouped reasons: %w", err)
return
}
r.BalanceChangesBy[provider], err = c.Database.BalanceChangeCount(provider)
if err != nil {
err = fmt.Errorf("getting balance change: %w", err)
return
}
r.UniqueAddressesBy[provider], err = c.Database.UniqueAddressCount(provider)
if err != nil {
err = fmt.Errorf("getting unique address count: %w", err)
return
}
}
return
}