Skip to content

Commit

Permalink
Cleanings
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Jan 29, 2025
1 parent 554c4c2 commit d9b2e9b
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 231 deletions.
265 changes: 64 additions & 201 deletions app/action_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,98 @@ package app
import (
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"time"

coreFile "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
_ "github.com/TrueBlocks/trueblocks-khedra/v2/pkg/env"
"github.com/TrueBlocks/trueblocks-khedra/v2/pkg/types"
"github.com/TrueBlocks/trueblocks-khedra/v2/pkg/validate"
"github.com/TrueBlocks/trueblocks-sdk/v4/services"
"github.com/urfave/cli/v2"
)

func (k *KhedraApp) daemonAction(c *cli.Context) error {
_ = c // linter
fn := types.GetConfigFnNoCreate()
if !coreFile.FileExists(fn) {
return fmt.Errorf("not initialized you must run `khedra init` first")
if err := k.loadConfigIfInitialized(); err != nil {
return err
}
k.logger.Info("Starting khedra daemon...config loaded...")

_, err := k.ConfigMaker()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
for _, ch := range k.config.Chains {
if ch.Enabled {
if !ch.HasValidRpc(4) {
return fmt.Errorf("chain %s has no valid RPC", ch.Name)
}
Progress("Connected to", "chain", ch.Name)
}
}
k.logger.Info("Processing chains...", "chainList", k.config.EnabledChains())
os.Exit(0)

for _, chain := range k.config.Chains {
for _, rpc := range chain.RPCs {
if err := validate.TryConnect(chain.Name, rpc, 5); err != nil {
return err
os.Setenv("XDG_CONFIG_HOME", k.config.General.DataFolder)
os.Setenv("TB_SETTINGS_DEFAULTCHAIN", "mainnet")
os.Setenv("TB_SETTINGS_INDEXPATH", k.config.IndexPath())
os.Setenv("TB_SETTINGS_CACHEPATH", k.config.CachePath())
for key, ch := range k.config.Chains {
if ch.Enabled {
envKey := "TB_CHAINS_" + strings.ToUpper(key) + "_RPCPROVIDER"
os.Setenv(envKey, ch.RPCs[0])
}
}

for _, env := range os.Environ() {
if (strings.HasPrefix(env, "TB_") || strings.HasPrefix(env, "XDG_")) && strings.Contains(env, "=") {
parts := strings.Split(env, "=")
if len(parts) > 1 {
k.logger.Info("environment", parts[0], parts[1])
} else {
k.logger.Info("environment", parts[0], "<empty>")
}
slog.Info("Connected to", "chain", chain.Name, "rpc", rpc)
}
}

fmt.Println("Services...", k.config.ServiceList(true /* enabledOnly */))

for key, ch := range k.config.Chains {
chain := k.chainList.ChainsMap[ch.ChainId]
fmt.Println(colors.Blue, "Chain", key, ch, chain, colors.Off)
fmt.Println("Sleeping 1...")
time.Sleep(3 * time.Second)
}

configFn := filepath.Join(k.config.General.DataFolder, "trueBlocks.toml")
if file.FileExists(configFn) {
fmt.Println("Config file found", configFn)
} else {
fmt.Println("Config file not found", configFn)
}

fmt.Println("Sleeping...")
time.Sleep(2 * time.Second)
os.Exit(0)

var activeServices []services.Servicer
chains := strings.Split(strings.ReplaceAll(k.config.ChainList(false /* enabledOnly */), " ", ""), ",")
chains := strings.Split(strings.ReplaceAll(k.config.EnabledChains(), " ", ""), ",")
scraperSvc := services.NewScrapeService(
k.logger,
k.logger.GetLogger(),
"all",
chains,
k.config.Services["scraper"].Sleep,
k.config.Services["scraper"].BatchSize,
)
monitorSvc := services.NewMonitorService(nil)
apiSvc := services.NewApiService(k.logger)
ipfsSvc := services.NewIpfsService(k.logger)
controlService := services.NewControlService(k.logger)
apiSvc := services.NewApiService(k.logger.GetLogger())
ipfsSvc := services.NewIpfsService(k.logger.GetLogger())
controlService := services.NewControlService(k.logger.GetLogger())
activeServices = append(activeServices, controlService)
activeServices = append(activeServices, scraperSvc)
activeServices = append(activeServices, monitorSvc)
activeServices = append(activeServices, apiSvc)
activeServices = append(activeServices, ipfsSvc)
slog.Info("Starting khedra daemon", "services", len(activeServices))
serviceManager := services.NewServiceManager(activeServices, k.logger)
serviceManager := services.NewServiceManager(activeServices, k.logger.GetLogger())
for _, svc := range activeServices {
if controlSvc, ok := svc.(*services.ControlService); ok {
controlSvc.AttachServiceManager(serviceManager)
Expand All @@ -70,83 +111,11 @@ func (k *KhedraApp) daemonAction(c *cli.Context) error {
return nil
}

/*
SHOW ALL THE TB_KHEDRA_ VARIABLES FOUND
SHOW THE DATA FOLDER
THERE USED TO BE A DIFFERENCE BETWEEN THE INDEXED CHAINS AND THE CHAINS REQUIRING RPCS
TELL THE USER WHICH CHAINS ARE BEING PROCESSED
TELL THE USER WHICH SERVICES ARE BEING STARTED
// If trueBlocks.io file exists, check that it contains records for each enabled chain

// EstablishConfig either reads an existing configuration file or creates it if it doesn't exist.
func (a *App) EstablishConfig() error {
var ok bool
var err error
if a.Config.ConfigPath, ok = os.LookupEnv("TB_NODE_DATADIR"); !ok {
return errors.New("environment variable `TB_NODE_DATADIR` is required but not found")
} else {
if a.Config.ConfigPath, err = cleanDataPath(a.Config.ConfigPath); err != nil {
return err
}
}
a.Logger.Info("data directory", "dataDir", a.Config.ConfigPath)
var targets string
chainStr, ok := os.LookupEnv("TB_NODE_CHAINS")
if !ok {
chainStr, targets = "mainnet", "mainnet"
} else {
if chainStr, targets, err = cleanChainString(chainStr); err != nil {
return err
}
}
a.Logger.Info("configured chains", "chainStr", chainStr, "targets", targets)
a.Config.Targets = strings.Split(targets, ",")
chains := strings.Split(chainStr, ",")
for _, chain := range chains {
key := "TB_NODE_" + strings.ToUpper(chain) + "RPC"
if providerUrl, ok := os.LookupEnv(key); !ok {
msg := fmt.Sprintf("environment variable `%s` is required but not found (implied by TB_NODE_CHAINS=%s)", key, chainStr)
return errors.New(msg)
} else {
providerUrl = strings.Trim(providerUrl, "/")
if !isValidURL(providerUrl) {
return fmt.Errorf("invalid URL for %s: %s", key, providerUrl)
}
a.Config.ProviderMap[chain] = providerUrl
}
}
// // Set the environment trueblocks-core needs
os.Setenv("XDG_CONFIG_HOME", a.Config.ConfigPath)
os.Setenv("TB_SETTINGS_DEFAULTCHAIN", "mainnet")
os.Setenv("TB_SETTINGS_INDEXPATH", a.Config.IndexPath())
os.Setenv("TB_SETTINGS_CACHEPATH", a.Config.CachePath())
for chain, providerUrl := range a.Config.ProviderMap {
envKey := "TB_CHAINS_" + strings.ToUpper(chain) + "_RPCPROVIDER"
os.Setenv(envKey, providerUrl)
}
for _, env := range os.Environ() {
if (strings.HasPrefix(env, "TB_") || strings.HasPrefix(env, "XDG_")) && strings.Contains(env, "=") {
parts := strings.Split(env, "=")
if len(parts) > 1 {
a.Logger.Info("environment", parts[0], parts[1])
} else {
a.Logger.Info("environment", parts[0], "<empty>")
}
}
}
for _, chain := range chains {
providerUrl := a.Config.ProviderMap[chain]
if err := a.tryConnect(chain, providerUrl, 5); err != nil {
return err
} else {
a.Logger.Info("test connection", "result", "okay", "chain", chain, "providerUrl", providerUrl)
}
}
// If trueBlocks.io does not exist create it with a template

/*
configFn := filepath.Join(a.Config.ConfigPath, "trueBlocks.toml")
if file.FileExists(configFn) {
a.Logger.Info("config loaded", "configFile", configFn, "nChains", len(a.Config.ProviderMap))
Expand Down Expand Up @@ -204,110 +173,4 @@ var configTmpl string = `[version]
[chains]{{.ChainDescriptors}}
`
handleService := func(i int, feature Feature) (int, error) {
if hasValue(i) {
if mode, err := validateOnOff(os.Args[i+1]); err == nil {
switch feature {
case Scrape:
a.Scrape = mode
if a.IsOn(Scrape) {
scrapeSvc := services.NewScrapeService(
a.Logger,
string(a.InitMode),
a.Config.Targets,
a.Sleep,
a.BlockCnt,
)
activeServices = append(activeServices, scrapeSvc)
}
case Api:
a.Api = mode
if a.IsOn(Api) {
apiSvc := services.NewApiService(a.Logger)
activeServices = append(activeServices, apiSvc)
}
case Ipfs:
a.Ipfs = mode
if a.IsOn(Ipfs) {
ipfsSvc := services.NewIpfsService(a.Logger)
activeServices = append(activeServices, ipfsSvc)
}
case Monitor:
a.Monitor = mode
if a.IsOn(Monitor) {
monSvc := services.NewMonitorService(a.Logger)
activeServices = append(activeServices, monSvc)
}
}
return i + 1, nil
} else {
return i, fmt.Errorf("parsing --%s: %w", feature.String(), err)
}
}
return i, fmt.Errorf("%w for --%s", ErrMissingArgument, feature.String())
}
controlService := services.NewControlService(a.Logger)
activeServices = append([]services.Servicer{controlService}, activeServices...)
}
FOR RUNNING CORE
os.Setenv("XDG_CONFIG_HOME", a.Config.ConfigPath)
os.Setenv("TB_ SETTINGS_DEFAULTCHAIN", "mainnet")
os.Setenv("TB_ SETTINGS_INDEXPATH", a.Config.IndexPath())
os.Setenv("TB_ SETTINGS_CACHEPATH", a.Config.CachePath())
for chain, providerUrl := range a.Config.ProviderMap {
envKey := "TB_CHAINS_" + strings.ToUpper(chain) + "_RPCPROVIDER"
os.Setenv(envKey, providerUrl)
}
for _, env := range os.Environ() {
if (strings.HasPrefix(env, "TB_") || strings.HasPrefix(env, "XDG_")) && strings.Contains(env, "=") {
parts := strings.Split(env, "=")
if len(parts) > 1 {
a.Logger.Info("environment", parts[0], parts[1])
} else {
a.Logger.Info("environment", parts[0], "<empty>")
}
}
}
for _, chain := range chains {
providerUrl := a.Config.ProviderMap[chain]
if err := a.tryConnect(chain, providerUrl, 5); err != nil {
return err
} else {
a.Logger.Info("test connection", "result", "okay", "chain", chain, "providerUrl", providerUrl)
}
}
USED TO CHECK THAT IF THE USER SPECIFIED A CHAIN IN THE ENV, THEN IT HAD TO EXISTING IN TRUEBLOCKS.TOML
configFn := filepath.Join(a.Config.ConfigPath, "trueBlocks.toml")
if file.FileExists(configFn) {
a.Logger.Info("config loaded", "configFile", configFn, "nChains", len(a.Config.ProviderMap))
// check to make sure the config file has all the chains
contents := file.AsciiFileToString(configFn)
for chain := range a.Config.ProviderMap {
search := "[chains." + chain + "]"
if !strings.Contains(contents, search) {
msg := fmt.Sprintf("config file {%s} does not contain {%s}", configFn, search)
msg = colors.ColoredWith(msg, colors.Red)
return errors.New(msg)
}
}
return nil
}
USED TO ESTABLISH FOLDERS
if err := file.Establish Folder(a.Config.ConfigPath); err != nil {
return err
}
for _, chain := range chains {
chainConfig := filepath.Join(a.Config.ConfigPath, "config", chain)
if err := file.Establish Folder(chainConfig); err != nil {
return err
}
}
WOULD CREATE A MINIMAL TRUEBLOCKS.TOML IF NOT FOUND
*/
2 changes: 1 addition & 1 deletion app/action_init_chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var c1 = wizard.Question{
log.Fatal("chain mainnet not found")
}
copy.RPCs = strings.Split(input, ",")
if !copy.HasValidRpc() {
if !copy.HasValidRpc(2) {
copy.Name = ""
return strings.Join(copy.RPCs, ","), copy, fmt.Errorf(`no rpcs for chain mainnet %w`, wizard.ErrValidate)
}
Expand Down
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type KhedraApp struct {
cli *cli.App
config *types.Config
logger *slog.Logger
logger *types.CustomLogger
chainList *types.ChainList
}

Expand Down Expand Up @@ -52,7 +52,7 @@ func (k *KhedraApp) ConfigMaker() (types.Config, error) {
}
k.config = &cfg
k.logger = types.NewLogger(cfg.Logging)
slog.SetDefault(k.logger)
slog.SetDefault(k.logger.GetLogger())

return cfg, nil
}
14 changes: 14 additions & 0 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"fmt"

coreFile "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
"github.com/TrueBlocks/trueblocks-khedra/v2/pkg/types"
)

Expand Down Expand Up @@ -30,3 +31,16 @@ func LoadConfig() (types.Config, error) {

return cfg, nil
}

func (k *KhedraApp) loadConfigIfInitialized() error {
fn := types.GetConfigFnNoCreate()
if !coreFile.FileExists(fn) {
return fmt.Errorf("not initialized you must run `khedra init` first")
}

if _, err := k.ConfigMaker(); err != nil {
return fmt.Errorf("failed to load config: %w", err)
}

return nil
}
2 changes: 1 addition & 1 deletion app/config_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func validateConfig(cfg types.Config) error {
if len(svcList) == 0 {
return fmt.Errorf("at least one service must be enabled")
}
chList := cfg.ChainList(true /* enabledOnly */)
chList := cfg.EnabledChains()
if len(chList) == 0 {
return fmt.Errorf("at least one chain must be enabled")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func (ch *Chain) IsEnabled() bool {
return ch.Enabled
}

func (ch *Chain) HasValidRpc() bool {
func (ch *Chain) HasValidRpc(tries int) bool {
for _, rpc := range ch.RPCs {
if err := validate.TryConnect(ch.Name, rpc, 2); err == nil {
if err := validate.TryConnect(ch.Name, rpc, tries); err == nil {
return true
}
}
Expand Down
Loading

0 comments on commit d9b2e9b

Please sign in to comment.