Skip to content

Commit

Permalink
temporary fix for pool join
Browse files Browse the repository at this point in the history
due to chain sync some members cannot join pool. calling api.fula directly
  • Loading branch information
ehsan6sha committed Aug 26, 2024
1 parent 6b15da4 commit 0130dc1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion blockchain/bl_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ func (bl *FxBlockchain) HandlePoolJoin(method string, action string, from peer.I
}

//TODO: Ensure it is optimized for long-running calls
//TODO: replace callBlockchainWithSeedTemporary with callBlockchain after fixing sync chain issue
ctx, cancel := context.WithTimeout(r.Context(), time.Second*time.Duration(bl.timeout))
defer cancel()
response, statusCode, err := bl.callBlockchain(ctx, method, action, &req)
response, statusCode, err := bl.callBlockchainWithSeedTemporary(ctx, method, action, &req)
if err != nil {
poolID := req.PoolID
poolIDStr := strconv.Itoa(poolID)
Expand Down
45 changes: 45 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,51 @@ func (bl *FxBlockchain) callBlockchain(ctx context.Context, method string, actio
return b, resp.StatusCode, nil
}

// TODO: The below method should be removed after fixing hte syncing issue with blockchain
func (bl *FxBlockchain) callBlockchainWithSeedTemporary(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("api.node3.functionyard.fula.network")
addr := endpoint + "/" + strings.Replace(action, "-", "/", -1)

// Use the bufPool and reqPool to reuse bytes.Buffer and http.Request objects
buf := bl.bufPool.Get().(*bytes.Buffer)
req := bl.reqPool.Get().(*http.Request)
defer func() {
bl.putBuf(buf)
bl.putReq(req)
}()

preparedRequest := bl.PlugSeedIfNeeded(ctx, action, p)
if err := json.NewEncoder(buf).Encode(preparedRequest); err != nil {
return nil, 0, err
}
req, err := http.NewRequestWithContext(ctx, method, addr, buf)
if err != nil {
return nil, 0, err
}

req.Header.Set("Content-Type", "application/json")

resp, err := bl.ch.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()

var bufRes bytes.Buffer
_, err = io.Copy(&bufRes, resp.Body)
if err != nil {
return nil, resp.StatusCode, err
}
b := bufRes.Bytes()

return b, resp.StatusCode, nil
}

func (bl *FxBlockchain) PlugSeedIfNeeded(ctx context.Context, action string, req interface{}) interface{} {
switch action {
case actionSeeded, actionAccountExists, actionAccountFund, actionPoolCreate, actionPoolJoin, actionPoolCancelJoin, actionPoolVote, actionPoolLeave, actionManifestUpload, actionManifestStore, actionManifestRemove, actionManifestRemoveStorer, actionManifestRemoveStored, actionManifestBatchUpload, actionManifestBatchStore:
Expand Down

0 comments on commit 0130dc1

Please sign in to comment.