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

     1  package prefunds
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
     9  )
    10  
    11  var prefundNamesLoaded = false
    12  var prefundNames = map[base.Address]types.Name{}
    13  var prefundNamesMutex sync.Mutex
    14  
    15  // LoadPrefundMap loads the prefund names from file if not already loaded or from the cache
    16  // if it is. A pointer to the data is returned, so the caller should not modify it.
    17  func LoadPrefundMap(chain string, thePath string) (*map[base.Address]types.Name, error) {
    18  	if prefundNamesLoaded {
    19  		return &prefundNames, nil
    20  	}
    21  
    22  	prefundNamesMutex.Lock()
    23  	defer func() {
    24  		prefundNamesLoaded = true
    25  		prefundNamesMutex.Unlock()
    26  	}()
    27  
    28  	if prefunds, err := LoadPrefunds(chain, thePath, nil); err != nil {
    29  		return nil, err
    30  	} else {
    31  		for i, prefund := range prefunds {
    32  			n := types.Name{
    33  				Tags:      "80-Prefund",
    34  				Address:   prefund.Address,
    35  				Name:      "Prefund_" + fmt.Sprintf("%04d", i),
    36  				Source:    "Genesis",
    37  				IsPrefund: true,
    38  				Prefund:   prefund.Balance,
    39  			}
    40  			prefundNames[n.Address] = n
    41  		}
    42  	}
    43  
    44  	return &prefundNames, nil
    45  }