github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/names/regular.go (about)

     1  package names
     2  
     3  import (
     4  	"io"
     5  	"sync"
     6  
     7  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    10  )
    11  
    12  var regularNamesLoaded = false
    13  var regularNames = map[base.Address]types.Name{}
    14  var regularNamesMutex sync.Mutex
    15  
    16  // loadRegularMap loads the regular names from the cache
    17  func loadRegularMap(chain string, terms []string, parts types.Parts, ret *map[base.Address]types.Name) error {
    18  	if regularNamesLoaded {
    19  		for _, name := range regularNames {
    20  			if doSearch(&name, terms, parts) {
    21  				name.Parts = types.Regular
    22  				if existing, ok := (*ret)[name.Address]; ok {
    23  					name.Parts |= existing.Parts
    24  				}
    25  				(*ret)[name.Address] = name
    26  			}
    27  		}
    28  		return nil
    29  	}
    30  
    31  	regularNamesMutex.Lock()
    32  	defer func() {
    33  		regularNamesLoaded = true
    34  		regularNamesMutex.Unlock()
    35  	}()
    36  
    37  	db, err := openDatabaseForRead(chain, DatabaseRegular)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	defer db.Close()
    42  
    43  	reader, err := NewNameReader(db)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	for {
    49  		name, err := reader.Read()
    50  		if err == io.EOF {
    51  			break
    52  		}
    53  		if err != nil {
    54  			logger.Fatal(err)
    55  		}
    56  		regularNames[name.Address] = name
    57  		if doSearch(&name, terms, parts) {
    58  			name.Parts = types.Regular
    59  			if existing, ok := (*ret)[name.Address]; ok {
    60  				name.Parts |= existing.Parts
    61  			}
    62  			(*ret)[name.Address] = name
    63  		}
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  // loadKnownBadresses loads the known bad addresses from the cache
    70  func loadKnownBadresses(unused string, terms []string, parts types.Parts, ret *map[base.Address]types.Name) error {
    71  	_ = unused // linter
    72  	knownBadAddresses := []types.Name{
    73  		{
    74  			Address: base.PrefundSender,
    75  			Name:    "PrefundSender",
    76  			Tags:    "75-Baddress",
    77  		},
    78  		{
    79  			Address: base.BlockRewardSender,
    80  			Name:    "BlockRewardSender",
    81  			Tags:    "75-Baddress",
    82  		},
    83  		{
    84  			Address: base.UncleRewardSender,
    85  			Name:    "UncleRewardSender",
    86  			Tags:    "75-Baddress",
    87  		},
    88  		{
    89  			Address: base.WithdrawalSender,
    90  			Name:    "WithdrawalSender",
    91  			Tags:    "75-Baddress",
    92  		},
    93  	}
    94  	for _, name := range knownBadAddresses {
    95  		if doSearch(&name, terms, parts) {
    96  			name.Parts = types.Baddress
    97  			(*ret)[name.Address] = name
    98  		}
    99  	}
   100  	return nil
   101  }