From 3bf2e8af2bb34fcc1a2b9f1caaec0fbb0ecc2de4 Mon Sep 17 00:00:00 2001 From: PeepoFrog Date: Fri, 7 Jun 2024 22:32:09 +0300 Subject: [PATCH] feat(start_cmd): Added start handler --- src/shidai/internal/commands/commands.go | 29 +++++++ .../interx_helper/interx_helper.go | 80 +++++++++++++++++++ .../sekai_helper/sekai_helper.go | 4 +- 3 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 src/shidai/internal/interx_handler/interx_helper/interx_helper.go diff --git a/src/shidai/internal/commands/commands.go b/src/shidai/internal/commands/commands.go index 8f7b1d9..2b55fc4 100644 --- a/src/shidai/internal/commands/commands.go +++ b/src/shidai/internal/commands/commands.go @@ -8,6 +8,7 @@ import ( "strconv" interxhandler "github.com/kiracore/sekin/src/shidai/internal/interx_handler" + interxhelper "github.com/kiracore/sekin/src/shidai/internal/interx_handler/interx_helper" "github.com/kiracore/sekin/src/shidai/internal/logger" mnemonicmanager "github.com/kiracore/sekin/src/shidai/internal/mnemonic_manager" sekaihandler "github.com/kiracore/sekin/src/shidai/internal/sekai_handler" @@ -41,6 +42,7 @@ var ( CommandHandlers = map[string]HandlerFunc{ "join": handleJoinCommand, "status": handleStatusCommand, + "start": handleStartComamnd, } ) @@ -128,6 +130,10 @@ func handleJoinCommand(args map[string]interface{}) (string, error) { if err != nil { return "", fmt.Errorf("unable to start interx: %w", err) } + err = interxhelper.CheckInterxStart(ctx) + if err != nil { + return "", err + } // Example of using the IP, and similar for other fields // This function would contain the logic specific to handling a join command return fmt.Sprintf("Join command processed for IP: %s", ip), nil @@ -143,3 +149,26 @@ func handleStatusCommand(args map[string]interface{}) (string, error) { return "", nil } + +func handleStartComamnd(args map[string]interface{}) (string, error) { + err := sekaihandler.StartSekai() + if err != nil { + return "", fmt.Errorf("unable to start sekai: %w", err) + } + ctx := context.Background() + + err = sekaihelper.CheckSekaiStart(ctx) + if err != nil { + return "", err + } + + err = interxhandler.StartInterx() + if err != nil { + return "", fmt.Errorf("unable to start interx: %w", err) + } + err = interxhelper.CheckInterxStart(ctx) + if err != nil { + return "", err + } + return "Sekai and Interx started successfully", nil +} diff --git a/src/shidai/internal/interx_handler/interx_helper/interx_helper.go b/src/shidai/internal/interx_handler/interx_helper/interx_helper.go new file mode 100644 index 0000000..8a7abc1 --- /dev/null +++ b/src/shidai/internal/interx_handler/interx_helper/interx_helper.go @@ -0,0 +1,80 @@ +package interxhelper + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "strconv" + "time" + + "github.com/kiracore/sekin/src/shidai/internal/logger" + "github.com/kiracore/sekin/src/shidai/internal/types" + "github.com/kiracore/sekin/src/shidai/internal/types/endpoints/interx" + "go.uber.org/zap" +) + +var log = logger.GetLogger() + +func CheckInterxStart(ctx context.Context) error { + timeout := time.Second * 60 + log.Debug("Checking if interx is started with timeout ", zap.Duration("timeout", timeout)) + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + + for { + select { + case <-ctx.Done(): + return ctx.Err() + default: + status, err := GetInterxStatus(ctx, types.INTERX_CONTAINER_ADDRESS, strconv.Itoa(types.DEFAULT_INTERX_PORT)) + if err != nil { + log.Warn("ERROR when getting interx status:", zap.Error(err)) + time.Sleep(time.Second) + continue + } + latestBlock, err := strconv.Atoi(status.InterxInfo.LatestBlockHeight) + log.Debug("Latest block:", zap.Int("latestBlock", latestBlock)) + if err != nil { + log.Warn("ERROR when converting latest block to string", zap.Error(err)) + continue + // return err + } + if latestBlock > 0 { + return nil + } + } + } + return nil +} + +func GetInterxStatus(ctx context.Context, ip, port string) (*interx.Status, error) { + client := &http.Client{} + ctxWithTO, c := context.WithTimeout(ctx, time.Second*10) + defer c() + // log.Printf("Getting net_info from: %v", ip) + url := fmt.Sprintf("http://%v:%v/api/status", ip, port) + req, err := http.NewRequestWithContext(ctxWithTO, http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + b, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + var nodeStatus interx.Status + err = json.Unmarshal(b, &nodeStatus) + if err != nil { + return nil, err + } + return &nodeStatus, nil +} diff --git a/src/shidai/internal/sekai_handler/sekai_helper/sekai_helper.go b/src/shidai/internal/sekai_handler/sekai_helper/sekai_helper.go index 6ab3a09..b5f0b2e 100644 --- a/src/shidai/internal/sekai_handler/sekai_helper/sekai_helper.go +++ b/src/shidai/internal/sekai_handler/sekai_helper/sekai_helper.go @@ -51,11 +51,10 @@ func CheckSekaiStart(ctx context.Context) error { case <-ctx.Done(): return ctx.Err() default: - status, err := GetSekaidStatus(ctx, types.SEKAI_CONTAINER_ADDRESS, "26657") if err != nil { log.Warn("ERROR when getting sekai status:", zap.Error(err)) - // return err + time.Sleep(time.Second) continue } latestBlock, err := strconv.Atoi(status.Result.SyncInfo.LatestBlockHeight) @@ -68,7 +67,6 @@ func CheckSekaiStart(ctx context.Context) error { if latestBlock > 0 { return nil } - time.Sleep(time.Second) } } }