diff --git a/cmd/skywire-cli/commands/reward/root.go b/cmd/skywire-cli/commands/reward/root.go index 4215870443..00923c4f85 100644 --- a/cmd/skywire-cli/commands/reward/root.go +++ b/cmd/skywire-cli/commands/reward/root.go @@ -107,12 +107,6 @@ var rewardCmd = &cobra.Command{ if output == "" { output = visorconfig.PackageConfig().LocalPath + "/" + visorconfig.RewardFile } - if isDeleteFile { - err := os.Remove(output) - if err != nil { - internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Error deleting file. err=%v", err)) - } - } //print reward address and exit if isRead { dat, err := os.ReadFile(output) //nolint @@ -146,14 +140,23 @@ var rewardCmd = &cobra.Command{ readRewardFile(cmd.Flags()) return } - rewardaddress, err := client.SetRewardAddress(rewardAddress) + + if isDeleteFile { + err := client.DeleteRewardAddress() + if err != nil { + internal.PrintFatalError(cmd.Flags(), err) + } + return + } + + rwdAdd, err := client.SetRewardAddress(rewardAddress) if err != nil { internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err)) //nolint internal.Catch(cmd.Flags(), os.WriteFile(output, []byte(cAddr.String()), 0644)) //nolint readRewardFile(cmd.Flags()) return } - output := fmt.Sprintf("Reward address:\n %s\n", rewardaddress) + output := fmt.Sprintf("Reward address:\n %s\n", rwdAdd) internal.PrintOutput(cmd.Flags(), output, output) }, } diff --git a/cmd/skywire-cli/commands/visor/ping.go b/cmd/skywire-cli/commands/visor/ping.go index 7cdc880b93..d7d577289e 100644 --- a/cmd/skywire-cli/commands/visor/ping.go +++ b/cmd/skywire-cli/commands/visor/ping.go @@ -32,7 +32,7 @@ func init() { var pingCmd = &cobra.Command{ Use: "ping ", Short: "Ping the visor with given pk", - Long: "\n Creates a route with the provided pk as a hop and returns latency on the conn", + Long: "\n Creates a route with the provided pk as a hop and returns latency on the conn", Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { pk := internal.ParsePK(cmd.Flags(), "pk", args[0]) @@ -58,7 +58,7 @@ var pingCmd = &cobra.Command{ var testCmd = &cobra.Command{ Use: "test", Short: "Test the visor with public visors on network", - Long: "\n Creates a route with public visors as a hop and returns latency on the conn", + Long: "\n Creates a route with public visors as a hop and returns latency on the conn", Run: func(cmd *cobra.Command, args []string) { pingConfig := visor.PingConfig{Tries: tries, PcktSize: pcktSize, PubVisCount: pubVisCount} rpcClient, err := clirpc.Client(cmd.Flags()) diff --git a/pkg/visor/api.go b/pkg/visor/api.go index 32069f9efa..f25004f95d 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -58,6 +58,7 @@ type API interface { //reward setting SetRewardAddress(string) (string, error) GetRewardAddress() (string, error) + DeleteRewardAddress() error //app controls App(appName string) (*appserver.AppState, error) @@ -215,6 +216,7 @@ type Summary struct { MinHops uint16 `json:"min_hops"` PersistentTransports []transport.PersistentTransports `json:"persistent_transports"` SkybianBuildVersion string `json:"skybian_build_version"` + RewardAddress string `json:"reward_address"` BuildTag string `json:"build_tag"` PublicAutoconnect bool `json:"public_autoconnect"` } @@ -266,6 +268,11 @@ func (v *Visor) Summary() (*Summary, error) { dmsgStatValue = &dmsgTracker } + rewardAddress, err := v.GetRewardAddress() + if err != nil { + v.log.Warn(err) + } + summary := &Summary{ Overview: overview, Health: health, @@ -275,6 +282,7 @@ func (v *Visor) Summary() (*Summary, error) { PersistentTransports: pts, SkybianBuildVersion: skybianBuildVersion, BuildTag: BuildTag, + RewardAddress: rewardAddress, PublicAutoconnect: v.conf.Transport.PublicAutoconnect, DmsgStats: dmsgStatValue, } @@ -350,6 +358,17 @@ func (v *Visor) SetRewardAddress(p string) (string, error) { // GetRewardAddress implements API. func (v *Visor) GetRewardAddress() (string, error) { path := v.conf.LocalPath + "/" + visorconfig.RewardFile + _, err := os.Stat(path) + if os.IsNotExist(err) { + file, err := os.Create(filepath.Clean(path)) + if err != nil { + return "", fmt.Errorf("failed to create config file. err=%v", err) + } + err = file.Close() + if err != nil { + return "", fmt.Errorf("failed to close config file. err=%v", err) + } + } rConfig, err := os.ReadFile(filepath.Clean(path)) if err != nil { return "", fmt.Errorf("failed to read config file. err=%v", err) @@ -357,6 +376,17 @@ func (v *Visor) GetRewardAddress() (string, error) { return string(rConfig), nil } +// DeleteRewardAddress implements API. +func (v *Visor) DeleteRewardAddress() error { + + path := v.conf.LocalPath + "/" + visorconfig.RewardFile + err := os.Remove(path) + if err != nil { + return fmt.Errorf("Error deleting file. err=%v", err) + } + return nil +} + // Apps implements API. func (v *Visor) Apps() ([]*appserver.AppState, error) { return v.appL.AppStates(), nil diff --git a/pkg/visor/hypervisor.go b/pkg/visor/hypervisor.go index 02f85bbf25..d681bcb1ed 100644 --- a/pkg/visor/hypervisor.go +++ b/pkg/visor/hypervisor.go @@ -268,8 +268,8 @@ func (hv *Hypervisor) makeMux() chi.Router { r.Get("/visors/{pk}/log/rotation", hv.getLogRotationInterval()) r.Put("/visors/{pk}/log/rotation", hv.putLogRotationInterval()) r.Get("/visors/{pk}/reward", hv.getRewardAddress()) - r.Put("/visors/{pk}/reward", hv.putRewardAddres()) - + r.Put("/visors/{pk}/reward", hv.putRewardAddress()) + r.Delete("/visors/{pk}/reward", hv.deleteRewardAddress()) }) }) @@ -1276,13 +1276,24 @@ func (hv *Hypervisor) getLogRotationInterval() http.HandlerFunc { }) } -func (hv *Hypervisor) putRewardAddres() http.HandlerFunc { +func (hv *Hypervisor) getRewardAddress() http.HandlerFunc { + return hv.withCtx(hv.visorCtx, func(w http.ResponseWriter, r *http.Request, ctx *httpCtx) { + pts, err := ctx.API.GetRewardAddress() + if err != nil { + httputil.WriteJSON(w, r, http.StatusInternalServerError, err) + return + } + httputil.WriteJSON(w, r, http.StatusOK, pts) + }) +} + +func (hv *Hypervisor) putRewardAddress() http.HandlerFunc { return hv.withCtx(hv.visorCtx, func(w http.ResponseWriter, r *http.Request, ctx *httpCtx) { var reqBody *rewardconfig.Reward if err := httputil.ReadJSON(r, &reqBody); err != nil { if err != io.EOF { - hv.log(r).Warnf("putRewardAddres request: %v", err) + hv.log(r).Warnf("putRewardAddress request: %v", err) } httputil.WriteJSON(w, r, http.StatusBadRequest, usermanager.ErrMalformedRequest) return @@ -1302,14 +1313,14 @@ func (hv *Hypervisor) putRewardAddres() http.HandlerFunc { }) } -func (hv *Hypervisor) getRewardAddress() http.HandlerFunc { +func (hv *Hypervisor) deleteRewardAddress() http.HandlerFunc { return hv.withCtx(hv.visorCtx, func(w http.ResponseWriter, r *http.Request, ctx *httpCtx) { - pts, err := ctx.API.GetRewardAddress() + err := ctx.API.DeleteRewardAddress() if err != nil { httputil.WriteJSON(w, r, http.StatusInternalServerError, err) return } - httputil.WriteJSON(w, r, http.StatusOK, pts) + httputil.WriteJSON(w, r, http.StatusOK, struct{}{}) }) } diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index 241602b62b..c9a172a8e3 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -113,6 +113,12 @@ func (r *RPC) GetRewardAddress(_ *struct{}, out *string) (err error) { return err } +// DeleteRewardAddress deletes the reward.txt +func (r *RPC) DeleteRewardAddress(_ *struct{}, _ *struct{}) (err error) { + defer rpcutil.LogCall(r.log, "DeleteRewardAddress", nil)(nil, &err) + return r.visor.DeleteRewardAddress() +} + /* <<< APP LOGS >>> */ diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index 50969768be..c84a33b331 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -137,6 +137,11 @@ func (rc *rpcClient) GetRewardAddress() (rConfig string, err error) { return rConfig, err } +// DeleteRewardAddress implements API. +func (rc *rpcClient) DeleteRewardAddress() (err error) { + return rc.Call("DeleteRewardAddress", &struct{}{}, &struct{}{}) +} + // Apps calls Apps. func (rc *rpcClient) Apps() ([]*appserver.AppState, error) { states := make([]*appserver.AppState, 0) @@ -753,6 +758,11 @@ func (mc *mockRPCClient) GetRewardAddress() (string, error) { return "", nil } +// DeleteRewardAddress implements API. +func (mc *mockRPCClient) DeleteRewardAddress() error { + return nil +} + // Apps implements API. func (mc *mockRPCClient) Apps() ([]*appserver.AppState, error) { var apps []*appserver.AppState