Skip to content

Commit bd01a83

Browse files
authored
Merge pull request #587 from tonkeeper/fix_addressbook
fix the memory leak
2 parents f8e83e2 + 69edc65 commit bd01a83

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

pkg/addressbook/addressbook.go

+46-46
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/tonkeeper/tongo"
1818
"github.com/tonkeeper/tongo/abi"
1919
"github.com/tonkeeper/tongo/tlb"
20+
"github.com/tonkeeper/tongo/ton"
2021
"go.uber.org/zap"
2122
"golang.org/x/exp/maps"
2223
"golang.org/x/exp/slices"
@@ -282,42 +283,62 @@ func (m *manualAddresser) SearchAttachedAccounts(prefix string) []AttachedAccoun
282283
}
283284

284285
// refreshAddresses updates the list of known addresses
285-
func (m *manualAddresser) refreshAddresses(addressPath string) error {
286+
func (m *manualAddresser) refreshAddresses(addressPath, jettonPath string) error {
286287
addresses, err := downloadJson[KnownAddress](addressPath)
287288
if err != nil {
288289
return err
289290
}
290-
knownAccounts := make(map[tongo.AccountID]KnownAddress, len(addresses))
291-
attachedAccounts := make([]AttachedAccount, 0, len(addresses)*3)
292-
for _, item := range addresses {
293-
account, err := tongo.ParseAddress(item.Address)
294-
if err != nil {
295-
continue
296-
}
297-
item.Address = account.ID.ToRaw()
298-
knownAccounts[account.ID] = item
291+
jettons, err := downloadJson[KnownJetton](jettonPath)
292+
if err != nil {
293+
return err
294+
}
295+
knownAccounts := make(map[tongo.AccountID]KnownAddress)
296+
var attachedAccounts []AttachedAccount
297+
process := func(accountID tongo.AccountID, name, image string, accountType AttachedAccountType) error {
299298
// Generate name variants for the account
300-
slugs := GenerateSlugVariants(item.Name)
299+
slugs := GenerateSlugVariants(name)
301300
for _, slug := range slugs {
302301
weight := KnownAccountWeight
303302
// Convert known account to attached account
304-
attachedAccount, err := ConvertAttachedAccount(item.Name, slug, item.Image, account.ID, weight, core.TrustWhitelist, ManualAccountType)
303+
attachedAccount, err := ConvertAttachedAccount(name, slug, image, accountID, weight, core.TrustWhitelist, accountType)
305304
if err != nil {
306305
continue
307306
}
308307
attachedAccounts = append(attachedAccounts, attachedAccount)
309308
}
309+
return nil
310310
}
311+
for _, item := range addresses {
312+
accountID, err := ton.ParseAccountID(item.Address)
313+
if err != nil {
314+
return err
315+
}
316+
item.Address = accountID.ToRaw()
317+
knownAccounts[accountID] = item
318+
err = process(accountID, item.Name, item.Image, ManualAccountType)
319+
if err != nil {
320+
continue
321+
}
322+
}
323+
for _, jetton := range jettons {
324+
accountID, err := ton.ParseAccountID(jetton.Address)
325+
if err != nil {
326+
return err
327+
}
328+
err = process(accountID, jetton.Name, jetton.Image, JettonNameAccountType)
329+
if err != nil {
330+
continue
331+
}
332+
}
333+
// Sort the attached accounts by their normalized names
334+
sort.Slice(attachedAccounts, func(i, j int) bool {
335+
return attachedAccounts[i].Normalized < attachedAccounts[j].Normalized
336+
})
311337

312338
m.mu.Lock()
313-
defer m.mu.Unlock()
314339
m.addresses = knownAccounts
315-
sorted := m.sorted
316-
sorted = append(sorted, attachedAccounts...)
317-
sort.Slice(sorted, func(i, j int) bool {
318-
return sorted[i].Normalized < sorted[j].Normalized
319-
})
320-
m.sorted = sorted
340+
m.sorted = attachedAccounts
341+
m.mu.Unlock()
321342

322343
return nil
323344
}
@@ -346,8 +367,8 @@ func NewAddressBook(logger *zap.Logger, addressPath, jettonPath, collectionPath
346367
}
347368
// Start background refreshers
348369
go Refresher("gg whitelist", time.Hour, 5*time.Minute, logger, book.getGGWhitelist)
349-
go Refresher("addresses", time.Minute*15, 5*time.Minute, logger, func() error { return manual.refreshAddresses(addressPath) })
350-
go Refresher("jettons", time.Minute*15, 5*time.Minute, logger, func() error { return book.refreshJettons(manual, jettonPath) })
370+
go Refresher("addresses", time.Minute*15, 5*time.Minute, logger, func() error { return manual.refreshAddresses(addressPath, jettonPath) })
371+
go Refresher("jettons", time.Minute*15, 5*time.Minute, logger, func() error { return book.refreshJettons(jettonPath) })
351372
go Refresher("collections", time.Minute*15, 5*time.Minute, logger, func() error { return book.refreshCollections(collectionPath) })
352373
book.refreshTfPools(logger) // Refresh tfPools once on initialization as it doesn't need periodic updates
353374

@@ -369,47 +390,26 @@ func Refresher(name string, interval, errorInterval time.Duration, logger *zap.L
369390
}
370391

371392
// refreshJettons fetches and updates the jetton data from the provided URL
372-
func (b *Book) refreshJettons(addresser *manualAddresser, jettonPath string) error {
393+
func (b *Book) refreshJettons(jettonPath string) error {
373394
// Download jettons data
374395
jettons, err := downloadJson[KnownJetton](jettonPath)
375396
if err != nil {
376397
return err
377398
}
378399
knownJettons := make(map[tongo.AccountID]KnownJetton, len(jettons))
379-
var attachedAccounts []AttachedAccount
380400
for _, item := range jettons {
381-
account, err := tongo.ParseAddress(item.Address)
401+
accountID, err := ton.ParseAccountID(item.Address)
382402
if err != nil {
383403
continue
384404
}
385-
item.Address = account.ID.ToRaw()
386-
knownJettons[account.ID] = item
387-
// Generate name variants for the jetton
388-
slugs := GenerateSlugVariants(item.Name)
389-
for _, slug := range slugs {
390-
weight := KnownAccountWeight
391-
// Convert known account to attached account
392-
attachedAccount, err := ConvertAttachedAccount(item.Name, slug, item.Image, account.ID, weight, core.TrustWhitelist, JettonNameAccountType)
393-
if err != nil {
394-
continue
395-
}
396-
attachedAccounts = append(attachedAccounts, attachedAccount)
397-
}
405+
item.Address = accountID.ToRaw()
406+
knownJettons[accountID] = item
398407
}
399408

400409
b.mu.Lock()
401410
b.jettons = knownJettons
402411
b.mu.Unlock()
403412

404-
addresser.mu.Lock()
405-
defer addresser.mu.Unlock()
406-
sorted := addresser.sorted
407-
sorted = append(sorted, attachedAccounts...)
408-
sort.Slice(sorted, func(i, j int) bool {
409-
return sorted[i].Normalized < sorted[j].Normalized
410-
})
411-
addresser.sorted = sorted
412-
413413
return nil
414414
}
415415

0 commit comments

Comments
 (0)