diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index cd6d41f..420124c 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -215,7 +215,41 @@ func prependProtocol(addr string) string { return "https://" + addr } +// checkHealth checks the health of the blockchain by querying the /health endpoint. +// It returns an error if the blockchain is currently syncing. +func (bl *FxBlockchain) checkHealth(ctx context.Context) error { + endpoint := prependProtocol(bl.blockchainEndPoint) + healthAddr := endpoint + "/health" + healthReq, err := http.NewRequestWithContext(ctx, "POST", healthAddr, nil) + if err != nil { + return err + } + healthResp, err := bl.ch.Do(healthReq) + if err != nil { + return err + } + defer healthResp.Body.Close() + + var healthCheckResponse struct { + IsSyncing bool `json:"is_syncing"` + Peers int `json:"peers"` + ShouldHavePeers bool `json:"should_have_peers"` + } + if err := json.NewDecoder(healthResp.Body).Decode(&healthCheckResponse); err != nil { + return err + } + if healthCheckResponse.IsSyncing { + return fmt.Errorf("the chain is syncing, you can see the progress in pools screen") + } + return nil +} + func (bl *FxBlockchain) callBlockchain(ctx context.Context, method string, action string, p interface{}) ([]byte, int, error) { + // Check blockchain health before proceeding + if err := bl.checkHealth(ctx); err != nil { + return nil, http.StatusFailedDependency, err // Use 424 as the status code for a syncing blockchain + } + endpoint := prependProtocol(bl.blockchainEndPoint) addr := endpoint + "/" + strings.Replace(action, "-", "/", -1) diff --git a/blox/blox.go b/blox/blox.go index 4f29bbf..9f8b26f 100644 --- a/blox/blox.go +++ b/blox/blox.go @@ -748,6 +748,10 @@ func (p *Blox) Start(ctx context.Context) error { // Log the occurrence of the specific error but do not continue log.Warnw("Attempt to store with an account that is already a storer", "err", err, "p.topicName", p.topicName, "availableLinks", availableLinks) } else if strings.Contains(err.Error(), "Transaction is outdated") { + log.Error("Transaction is outdated") + continue + } else if strings.Contains(err.Error(), "syncing") { + log.Error("The chain is syncing") continue } else { // For any other error, log and continue