Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GetRewardAddress API #1438

Merged
merged 4 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions cmd/skywire-cli/commands/reward/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/skywire-cli/commands/visor/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
var pingCmd = &cobra.Command{
Use: "ping <pk>",
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])
Expand All @@ -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())
Expand Down
30 changes: 30 additions & 0 deletions pkg/visor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
Expand Down Expand Up @@ -350,13 +358,35 @@ 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)
}
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
Expand Down
25 changes: 18 additions & 7 deletions pkg/visor/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})

Expand Down Expand Up @@ -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
Expand All @@ -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{}{})
})
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/visor/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 >>>
*/
Expand Down
10 changes: 10 additions & 0 deletions pkg/visor/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down