diff --git a/client/http_server.go b/client/http_server.go index 95245df..be50205 100644 --- a/client/http_server.go +++ b/client/http_server.go @@ -8,7 +8,6 @@ import ( "io/ioutil" "log" "net/http" - "time" "github.com/depools/dc4bc/client/types" @@ -80,6 +79,9 @@ func (c *BaseClient) StartHTTPServer(listenAddr string) error { mux.HandleFunc("/startDKG", c.startDKGHandler) mux.HandleFunc("/proposeSignMessage", c.proposeSignDataHandler) + mux.HandleFunc("/saveOffset", c.saveOffsetHandler) + mux.HandleFunc("/getOffset", c.getOffsetHandler) + mux.HandleFunc("/getFSMDump", c.getFSMDumpHandler) mux.HandleFunc("/getFSMList", c.getFSMList) @@ -138,6 +140,47 @@ func (c *BaseClient) getPubkeyHandler(w http.ResponseWriter, r *http.Request) { successResponse(w, c.GetPubKey()) } +func (c *BaseClient) getOffsetHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + errorResponse(w, http.StatusBadRequest, "Wrong HTTP method") + return + } + offset, err := c.state.LoadOffset() + if err != nil { + errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to load offset: %v", err)) + return + } + successResponse(w, offset) +} + +func (c *BaseClient) saveOffsetHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + errorResponse(w, http.StatusBadRequest, "Wrong HTTP method") + return + } + reqBytes, err := ioutil.ReadAll(r.Body) + if err != nil { + errorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to read request body: %v", err)) + return + } + defer r.Body.Close() + + var req map[string]uint64 + if err = json.Unmarshal(reqBytes, &req); err != nil { + errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to unmarshal request: %v", err)) + return + } + if _, ok := req["offset"]; !ok { + errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("offset cannot be null: %v", err)) + return + } + if err = c.state.SaveOffset(req["offset"]); err != nil { + errorResponse(w, http.StatusInternalServerError, fmt.Sprintf("failed to save offset: %v", err)) + return + } + successResponse(w, "ok") +} + func (c *BaseClient) sendMessageHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { errorResponse(w, http.StatusBadRequest, "Wrong HTTP method") diff --git a/cmd/dc4bc_cli/main.go b/cmd/dc4bc_cli/main.go index 0508b83..ab1fcfc 100644 --- a/cmd/dc4bc_cli/main.go +++ b/cmd/dc4bc_cli/main.go @@ -13,6 +13,7 @@ import ( "net/http" "path/filepath" "sort" + "strconv" "strings" "time" @@ -58,6 +59,8 @@ func main() { getHashOfStartDKGCommand(), getSignaturesCommand(), getSignatureCommand(), + saveOffsetCommand(), + getOffsetCommand(), getFSMStatusCommand(), getFSMListCommand(), ) @@ -329,6 +332,62 @@ func getPubKeyCommand() *cobra.Command { } } +func saveOffsetCommand() *cobra.Command { + return &cobra.Command{ + Use: "save_offset [offset]", + Short: "saves a new offset for a storage", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + listenAddr, err := cmd.Flags().GetString(flagListenAddr) + if err != nil { + return fmt.Errorf("failed to read configuration: %v", err) + } + + offset, err := strconv.ParseUint(args[0], 10, 64) + if err != nil { + return fmt.Errorf("failed to parse uint: %w", err) + } + req := map[string]uint64{"offset": offset} + data, err := json.Marshal(req) + if err != nil { + return fmt.Errorf("failed to create request: %w", err) + } + resp, err := rawPostRequest(fmt.Sprintf("http://%s/saveOffset", listenAddr), "application/json", data) + if err != nil { + return fmt.Errorf("failed to save offset: %w", err) + } + if resp.ErrorMessage != "" { + return fmt.Errorf("failed to save offset: %v", resp.ErrorMessage) + } + fmt.Println(resp.Result.(string)) + return nil + }, + } +} + +func getOffsetCommand() *cobra.Command { + return &cobra.Command{ + Use: "get_offset", + Short: "returns a current offset for the storage", + RunE: func(cmd *cobra.Command, args []string) error { + listenAddr, err := cmd.Flags().GetString(flagListenAddr) + if err != nil { + return fmt.Errorf("failed to read configuration: %v", err) + } + + resp, err := rawGetRequest(fmt.Sprintf("http://%s//getOffset", listenAddr)) + if err != nil { + return fmt.Errorf("failed to get offset: %w", err) + } + if resp.ErrorMessage != "" { + return fmt.Errorf("failed to get offset: %v", resp.ErrorMessage) + } + fmt.Println(uint64(resp.Result.(float64))) + return nil + }, + } +} + func getUsernameCommand() *cobra.Command { return &cobra.Command{ Use: "get_username",