/
asset_scraper.go
349 lines (299 loc) · 8.68 KB
/
asset_scraper.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
package scraper
import (
"fmt"
"io/ioutil"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/BurntSushi/toml"
horizonclient "github.com/AnneNamuli/go-stellar/clients/horizonclient"
hProtocol "github.com/AnneNamuli/go-stellar/protocols/horizon"
"github.com/AnneNamuli/go-stellar/services/ticker/internal/utils"
"github.com/AnneNamuli/go-stellar/support/errors"
)
// shouldDiscardAsset maps the criteria for discarding an asset from the asset index
func shouldDiscardAsset(asset hProtocol.AssetStat, shouldValidateTOML bool) bool {
if asset.Amount == "" {
return true
}
f, _ := strconv.ParseFloat(asset.Amount, 64)
if f == 0.0 {
return true
}
// [StellarX Ticker]: assets need at least some adoption to show up
if asset.NumAccounts < 10 {
return true
}
if asset.Code == "REMOVE" {
return true
}
// [StellarX Ticker]: assets with at least 100 accounts get a pass,
// even with toml issues
if asset.NumAccounts >= 100 {
return false
}
if shouldValidateTOML {
if asset.Links.Toml.Href == "" {
return true
}
// [StellarX Ticker]: TOML files should be hosted on HTTPS
if !strings.HasPrefix(asset.Links.Toml.Href, "https://") {
return true
}
}
return false
}
// decodeTOMLIssuer decodes retrieved TOML issuer data into a TOMLIssuer struct
func decodeTOMLIssuer(tomlData string) (issuer TOMLIssuer, err error) {
_, err = toml.Decode(tomlData, &issuer)
return
}
// fetchTOMLData fetches the TOML data for a given hProtocol.AssetStat
func fetchTOMLData(asset hProtocol.AssetStat) (data string, err error) {
tomlURL := asset.Links.Toml.Href
if tomlURL == "" {
err = errors.New("Asset does not have a TOML URL")
return
}
timeout := time.Duration(10 * time.Second)
client := http.Client{
Timeout: timeout,
}
req, err := http.NewRequest("GET", tomlURL, nil)
if err != nil {
err = errors.Wrap(err, "invalid URL or request")
return
}
req.Header.Set("User-Agent", "Stellar Ticker v1.0")
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
data = string(body)
return
}
func domainsMatch(tomlURL *url.URL, orgURL *url.URL) bool {
tomlDomainParts := strings.Split(tomlURL.Host, ".")
orgDomainParts := strings.Split(orgURL.Host, ".")
if len(orgDomainParts) < len(tomlDomainParts) {
// Org can only be a subdomain if it has more (or equal)
// pieces than the TOML domain
return false
}
lenDiff := len(orgDomainParts) - len(tomlDomainParts)
orgDomainParts = orgDomainParts[lenDiff:]
orgRootDomain := strings.Join(orgDomainParts, ".")
return tomlURL.Host == orgRootDomain
}
// isDomainVerified performs some checking to ensure we can trust the Asset's domain
func isDomainVerified(orgURL string, tomlURL string, hasCurrency bool) bool {
if tomlURL == "" {
return false
}
parsedTomlURL, err := url.Parse(tomlURL)
if err != nil || parsedTomlURL.Scheme != "https" {
return false
}
if !hasCurrency {
return false
}
if orgURL == "" {
// if no orgURL is provided, we'll simply use tomlURL, so no need
// for domain verification
return true
}
parsedOrgURL, err := url.Parse(orgURL)
if err != nil || parsedOrgURL.Scheme != "https" {
return false
}
if !domainsMatch(parsedTomlURL, parsedOrgURL) {
return false
}
return true
}
// makeTomlAsset aggregates Horizon Data with TOML Data
func makeFinalAsset(
asset hProtocol.AssetStat,
issuer TOMLIssuer,
errors []error,
) (t FinalAsset, err error) {
amount, err := strconv.ParseFloat(asset.Amount, 64)
if err != nil {
return
}
t = FinalAsset{
Type: asset.Type,
Code: asset.Code,
Issuer: asset.Issuer,
NumAccounts: asset.NumAccounts,
AuthRequired: asset.Flags.AuthRequired,
AuthRevocable: asset.Flags.AuthRevocable,
Amount: amount,
IssuerDetails: issuer,
}
t.IssuerDetails.TOMLURL = asset.Links.Toml.Href
hasCurrency := false
for _, currency := range t.IssuerDetails.Currencies {
if currency.Code == asset.Code && currency.Issuer == asset.Issuer {
hasCurrency = true
t.AnchorAsset = currency.AnchorAsset
t.AnchorAssetType = currency.AnchorAssetType
t.DisplayDecimals = currency.DisplayDecimals
t.Name = currency.Name
t.Desc = currency.Desc
t.Conditions = currency.Conditions
t.IsAssetAnchored = currency.IsAssetAnchored
t.FixedNumber = currency.FixedNumber
t.MaxNumber = currency.MaxNumber
t.IsUnlimited = currency.IsUnlimited
t.RedemptionInstructions = currency.RedemptionInstructions
t.CollateralAddresses = currency.CollateralAddresses
t.CollateralAddressSignatures = currency.CollateralAddressSignatures
t.Status = currency.Status
break
}
}
t.AssetControlledByDomain = isDomainVerified(
t.IssuerDetails.Documentation.OrgURL,
asset.Links.Toml.Href,
hasCurrency,
)
if !hasCurrency {
t.AssetControlledByDomain = false
}
now := time.Now()
if len(errors) > 0 {
t.Error = fmt.Sprintf("%v", errors)
t.IsValid = false
} else {
t.LastValid = now
t.IsValid = true
}
t.LastChecked = now
t.AnchorAssetType = strings.ToLower(t.AnchorAssetType)
return
}
// processAsset merges data from an AssetStat with data retrieved from its corresponding TOML file
func processAsset(asset hProtocol.AssetStat, shouldValidateTOML bool) (FinalAsset, error) {
var errors []error
var issuer TOMLIssuer
if shouldValidateTOML {
tomlData, err := fetchTOMLData(asset)
if err != nil {
errors = append(errors, err)
}
issuer, err = decodeTOMLIssuer(tomlData)
if err != nil {
errors = append(errors, err)
}
}
return makeFinalAsset(asset, issuer, errors)
}
// parallelProcessAssets filters the assets that don't match the shouldDiscardAsset criteria.
// The TOML validation is performed in parallel to improve performance.
func (c *ScraperConfig) parallelProcessAssets(assets []hProtocol.AssetStat, parallelism int) (cleanAssets []FinalAsset, numTrash int) {
queue := make(chan FinalAsset, parallelism)
shouldValidateTOML := c.Client != horizonclient.DefaultTestNetClient // TOMLs shouldn't be validated on TestNet
var mutex = &sync.Mutex{}
var wg sync.WaitGroup
numAssets := len(assets)
chunkSize := int(math.Ceil(float64(numAssets) / float64(parallelism)))
wg.Add(numAssets)
// The assets are divided into chunks of chunkSize, and each goroutine is responsible
// for cleaning up one chunk
for i := 0; i < parallelism; i++ {
go func(start int) {
end := start + chunkSize
if end > numAssets {
end = numAssets
}
for j := start; j < end; j++ {
if !shouldDiscardAsset(assets[j], shouldValidateTOML) {
finalAsset, err := processAsset(assets[j], shouldValidateTOML)
if err != nil {
mutex.Lock()
numTrash++
mutex.Unlock()
// Invalid assets are also sent to the queue to preserve
// the WaitGroup count
queue <- FinalAsset{IsTrash: true}
continue
}
queue <- finalAsset
} else {
mutex.Lock()
numTrash++
mutex.Unlock()
// Discarded assets are also sent to the queue to preserve
// the WaitGroup count
queue <- FinalAsset{IsTrash: true}
}
}
}(i * chunkSize)
}
// Whenever a new asset is sent to the channel, it is appended to the cleanAssets
// slice. This does not preserve the original order, but shouldn't be an issue
// in this case.
go func() {
count := 0
for t := range queue {
count++
if !t.IsTrash {
cleanAssets = append(cleanAssets, t)
}
c.Logger.Debugln("Total assets processed:", count)
wg.Done()
}
}()
wg.Wait()
close(queue)
return
}
// retrieveAssets retrieves existing assets from the Horizon API. If limit=0, will fetch all assets.
func (c *ScraperConfig) retrieveAssets(limit int) (assets []hProtocol.AssetStat, err error) {
r := horizonclient.AssetRequest{Limit: 200}
assetsPage, err := c.Client.Assets(r)
if err != nil {
return
}
c.Logger.Infoln("Fetching assets from Horizon")
for assetsPage.Links.Next.Href != assetsPage.Links.Self.Href {
err = utils.Retry(5, 5*time.Second, c.Logger, func() error {
assetsPage, err = c.Client.Assets(r)
if err != nil {
c.Logger.Infoln("Horizon rate limit reached!")
}
return err
})
if err != nil {
return
}
assets = append(assets, assetsPage.Embedded.Records...)
if limit != 0 { // for performance reasons, only perform these additional checks when limit != 0
numAssets := len(assets)
if numAssets >= limit {
diff := numAssets - limit
assets = assets[0 : numAssets-diff]
break
}
}
nextURL := assetsPage.Links.Next.Href
n, err := nextCursor(nextURL)
if err != nil {
return assets, err
}
c.Logger.Debugln("Cursor currently at:", n)
r = horizonclient.AssetRequest{Limit: 200, Cursor: n}
}
c.Logger.Infof("Fetched: %d assets\n", len(assets))
return
}