From 0130dc16c7178afc9c001972d5b19925c3dbb786 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Mon, 26 Aug 2024 18:11:03 -0400 Subject: [PATCH] temporary fix for pool join due to chain sync some members cannot join pool. calling api.fula directly --- blockchain/bl_pool.go | 3 ++- blockchain/blockchain.go | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/blockchain/bl_pool.go b/blockchain/bl_pool.go index f0d85e4..6ea8375 100644 --- a/blockchain/bl_pool.go +++ b/blockchain/bl_pool.go @@ -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) diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 420124c..dd4ef94 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -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: