Skip to content

Commit

Permalink
not making blockchain calls possible if chain is syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Apr 6, 2024
1 parent 37a75dd commit 45349b1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
34 changes: 34 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions blox/blox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 45349b1

Please sign in to comment.