diff --git a/README.md b/README.md index feaeb6f..d37e817 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,30 @@ $ curl "localhost:8080/v1/bx/bacab66e7cbbb950c8b80facef94c5f0bf478ee3ab8ac270f54 } ``` +## Getting bx by id: +```bash +$ curl "localhost:8080/v1/status" +{ + "epoch": 91, + "bxid": [ + 91, + 12017682 + ], + "txid": [ + 91, + 12017336 + ], + "quorum": [ + 91, + 12017688 + ], + "tick": [ + 91, + 12017336 + ] +} +``` + ## Getting latest tick info: ```bash $ curl "localhost:8080/v1/tick-info" @@ -142,19 +166,6 @@ $ curl "localhost:8080/v1/tick-transactions/11922277" ] ``` -## Getting tx status: -```bash -$ curl "localhost:8080/v1/get-tx-status" --json '{"tick": 11400055, "digest": "c5cea11f54ca18317aef20287e3b33b2e0c9a6c94aeec91c30fe793be1d27fec"}' -{ - "current_tick_of_node": 11406937, - "tick": 11400055, - "money_flew": false, - "executed": true, - "not_found": false, - "hex_digest": "c5cea11f54ca18317aef20287e3b33b2e0c9a6c94aeec91c30fe793be1d27fec" -} -``` - ## Send raw tx: ```bash $ curl "localhost:8080/v1/send-raw-tx" --json '{"hex_raw_tx": "C872E68E1C0ECCCE3BC6A87BC32E187C59BBA99AB81D7CC37E7D22F7423672A70E4EAF16A2218457BA8B46991B5CCA63E65AE65FF65C575A06743E40E8DA982A0100000000000000C60DAE0000000000A1C0B21A5C15D72275F7968D30A4F0520075F85A0232E180A5FC6C0137CC414F402404CF40773F444A25BCF30B6455B18A18FF7DD105F3223EECA8C566781A00"}' diff --git a/app/server/handlers/handlers.go b/app/server/handlers/handlers.go index bd21040..cc02ad6 100644 --- a/app/server/handlers/handlers.go +++ b/app/server/handlers/handlers.go @@ -26,10 +26,13 @@ func New(shutdown chan os.Signal, log *log.Logger, pool *nodes.Pool, osclient *o txH := txHandler{pool: pool, opensearchClient: osclient} app.Handle(http.MethodPost, "/v1/send-raw-tx", txH.SendRawTx) - app.Handle(http.MethodPost, "/v1/get-tx-status", txH.GetTxStatus) + //app.Handle(http.MethodPost, "/v1/get-tx-status", txH.GetTxStatus) app.Handle(http.MethodGet, "/v1/tx/:txID", txH.GetTx) app.Handle(http.MethodGet, "/v1/bx/:bxID", txH.GetBx) + sh := statusHandler{opensearchClient: osclient} + app.Handle(http.MethodGet, "/v1/status", sh.GetStatus) + return app } diff --git a/app/server/handlers/status.go b/app/server/handlers/status.go index 5ac8282..043b3f0 100644 --- a/app/server/handlers/status.go +++ b/app/server/handlers/status.go @@ -1 +1,22 @@ package handlers + +import ( + "context" + "github.com/pkg/errors" + "github.com/qubic/qubic-http/external/opensearch" + "github.com/qubic/qubic-http/foundation/web" + "net/http" +) + +type statusHandler struct { + opensearchClient *opensearch.Client +} + +func (h *statusHandler) GetStatus(ctx context.Context, w http.ResponseWriter, r *http.Request) error { + status, err := h.opensearchClient.GetStatus(ctx) + if err != nil { + return errors.Wrap(err, "getting status from opensearch") + } + + return web.Respond(ctx, w, status, http.StatusOK) +} diff --git a/external/opensearch/models.go b/external/opensearch/models.go index 4484fb6..5cfa297 100644 --- a/external/opensearch/models.go +++ b/external/opensearch/models.go @@ -41,3 +41,11 @@ type BxResponse struct { Destination string `json:"dest"` Amount string `json:"amount"` } + +type StatusResponse struct { + Epoch int `json:"epoch"` + BxID []int `json:"bxid"` + TxID []int `json:"txid"` + Quorum []int `json:"quorum"` + Tick []int `json:"tick"` +} diff --git a/external/opensearch/opensearch.go b/external/opensearch/opensearch.go index 0b18c55..222cc4e 100644 --- a/external/opensearch/opensearch.go +++ b/external/opensearch/opensearch.go @@ -59,6 +59,18 @@ func (c *Client) GetBx(ctx context.Context, id string) (BxResponse, error) { return bx, nil } +func (c *Client) GetStatus(ctx context.Context) (StatusResponse, error) { + url := c.Host + "/status/_doc/api" + + var status StatusResponse + err := c.performRequest(ctx,url, http.MethodGet, nil, http.StatusOK, &status) + if err != nil { + return StatusResponse{}, errors.Wrap(err, "performing request") + } + + return status, nil +} + func (c *Client) performRequest(ctx context.Context, url string, method string, payload io.Reader, expectedStatusCode int, responseDest interface{}) error { req, err := http.NewRequestWithContext(ctx, method, url, payload) if err != nil {