Skip to content

Commit

Permalink
fix survey & reward calculation (#1856)
Browse files Browse the repository at this point in the history
* fix survey generation by the visor

* fix variable initialization

* fix type

* fix type

* fix context

* change logging message

* update reward calculation

* make format

* fix skywire cli survey

* fix various errors

* fix errors on mac & windows
  • Loading branch information
0pcom authored Jul 13, 2024
1 parent 99de0a1 commit b5414f6
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 96 deletions.
8 changes: 6 additions & 2 deletions cmd/skywire-cli/commands/rewards/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ Fetch uptimes: skywire-cli ut > ut.txt`,
var grrInfos []nodeinfo
for _, pk := range res {
nodeInfo := fmt.Sprintf("%s/%s/node-info.json", hwSurveyPath, pk)
tpsn, tpsnErr := script.File(fmt.Sprintf("%s/%s/tp.json", tpsnSurveyPath, pk)).JQ(`.`).String() //nolint
ip, _ := script.File(nodeInfo).JQ(`."ip.skycoin.com".ip_address`).Replace(" ", "").Replace(`"`, "").String() //nolint
tpsn, tpsnErr := script.File(fmt.Sprintf("%s/%s/tp.json", tpsnSurveyPath, pk)).JQ(`.`).String() //nolint
ip, _ := script.File(nodeInfo).JQ(`.ip_address`).Replace(" ", "").Replace(`"`, "").String() //nolint
ip = strings.TrimRight(ip, "\n")
if ip == "" {
ip, _ = script.File(nodeInfo).JQ(`."ip.skycoin.com".ip_address`).Replace(" ", "").Replace(`"`, "").String() //nolint
ip = strings.TrimRight(ip, "\n")
}
sky, _ := script.File(nodeInfo).JQ(".skycoin_address").Replace(" ", "").Replace(`"`, "").String() //nolint
sky = strings.TrimRight(sky, "\n")
arch, _ := script.File(nodeInfo).JQ(".go_arch").Replace(" ", "").Replace(`"`, "").String() //nolint
Expand Down
75 changes: 59 additions & 16 deletions cmd/skywire-cli/commands/survey/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
package clisurvey

import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"

"github.com/skycoin/dmsg/pkg/disc"
"github.com/skycoin/dmsg/pkg/dmsg"
"github.com/spf13/cobra"

"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/cmdutil"
"github.com/skycoin/skywire-utilities/pkg/logging"
"github.com/skycoin/skywire-utilities/pkg/skyenv"
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
Expand All @@ -32,7 +38,7 @@ var (
func init() {
surveyCmd.Flags().SortFlags = false
surveyCmd.Flags().StringVarP(&confPath, "config", "c", "", "optionl config file to use (i.e.: "+visorconfig.ConfigName+")")
surveyCmd.Flags().StringVar(&dmsgDisc, "dmsg-disc", skyenv.DmsgDiscAddr, "value of dmsg discovery")
surveyCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "D", skyenv.DmsgDiscAddr, "value of dmsg discovery")
// surveyCmd.Flags().StringVarP(&confArg, "confarg", "C", "", "supply config as argument")
// surveyCmd.Flags().BoolVarP(&stdin, "stdin", "n", false, "read config from stdin")
if _, err := os.Stat(visorconfig.SkywirePath + "/" + visorconfig.ConfigJSON); err == nil {
Expand Down Expand Up @@ -64,19 +70,8 @@ var surveyCmd = &cobra.Command{
if usr {
confPath = visorconfig.HomePath() + "/" + visorconfig.ConfigName
}
// if confPath != "" && confArg != "" {
// log.Fatal("cannot specify both --config, -c and --confarg, -C")
// }
// if confPath != "" && stdin {
// log.Fatal("cannot specify both --config, -c and --stdin, -n")
// }
// if stdin && confArg != "" {
// log.Fatal("cannot specify both --confarg, -C and --stdin, -n")
// }
// if stdin || confArg != "" || confPath != "" {

if confPath != "" {
// conf = initConfig()
confJSON, err := os.ReadFile(confPath) //nolint
if err != nil {
log.WithError(err).Fatal("Failed to read config file")
Expand All @@ -86,10 +81,10 @@ var surveyCmd = &cobra.Command{
log.WithError(err).Fatal("Failed to unmarshal old config json")
}
}
if conf != nil {
dmsgDisc = conf.Dmsg.Discovery
}
survey, err := visorconfig.SystemSurvey(dmsgDisc)
// if conf != nil {
// dmsgDisc = conf.Dmsg.Discovery
// }
survey, err := visorconfig.SystemSurvey()
if err != nil {
internal.Catch(cmd.Flags(), fmt.Errorf("Failed to generate system survey: %v", err))
}
Expand All @@ -111,10 +106,58 @@ var surveyCmd = &cobra.Command{
survey.ServicesURLs.StunServers = conf.StunServers
//survey.DmsgServers = v.dmsgC.ConnectedServersPK()
}
if dmsgDisc != "" {
ipAddr, err := FetchIP(dmsgDisc)
if err == nil {
survey.IPAddr = ipAddr
}
}

s, err := json.MarshalIndent(survey, "", "\t")
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Could not marshal json: %v", err))
}
fmt.Printf("%s", s)
},
}

// FetchIP fetches the ip address by dmsg servers
func FetchIP(dmsgDisc string) (string, error) {
log := logging.MustGetLogger("ip_skycoin_fetch_dmsg")
ctx, cancel := cmdutil.SignalContext(context.Background(), nil)
defer cancel()

pk, sk := cipher.GenerateKeyPair()

dmsgC, closeDmsg, err := startDmsg(ctx, log, pk, sk, dmsgDisc)
if err != nil {
return "", fmt.Errorf("failed to start dmsg")
}
defer closeDmsg()

ip, err := dmsgC.LookupIP(ctx, nil)
return ip.String(), err
}

func startDmsg(ctx context.Context, log *logging.Logger, pk cipher.PubKey, sk cipher.SecKey, dmsgDisc string) (dmsgC *dmsg.Client, stop func(), err error) {
dmsgC = dmsg.NewClient(pk, sk, disc.NewHTTP(dmsgDisc, &http.Client{}, log), &dmsg.Config{MinSessions: dmsg.DefaultMinSessions})
go dmsgC.Serve(context.Background())

stop = func() {
err := dmsgC.Close()
log.WithError(err).Debug("Disconnected from dmsg network.")
fmt.Printf("\n")
}
log.WithField("public_key", pk.String()).WithField("dmsg_disc", dmsgDisc).
Debug("Connecting to dmsg network...")

select {
case <-ctx.Done():
stop()
return nil, nil, ctx.Err()

case <-dmsgC.Ready():
log.Debug("Dmsg network ready.")
return dmsgC, stop, nil
}
}
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,6 @@ github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrf
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/skycoin/dmsg v1.3.22-0.20240702162730-1c2fcecd4bb6 h1:q+KeZyHWctCahSUbZ6b+iy1VQNHVKN2YZc7J2R0VIHw=
github.com/skycoin/dmsg v1.3.22-0.20240702162730-1c2fcecd4bb6/go.mod h1:72MC0HFDxKYqMLZ2RWGY/ZDNFq6965SP1PIrKlYqaiQ=
github.com/skycoin/dmsg v1.3.24 h1:aDa36UucXcWQCnHri1q5UKAVydomGDg28osBWlpUoOI=
github.com/skycoin/dmsg v1.3.24/go.mod h1:72MC0HFDxKYqMLZ2RWGY/ZDNFq6965SP1PIrKlYqaiQ=
github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6 h1:1Nc5EBY6pjfw1kwW0duwyG+7WliWz5u9kgk1h5MnLuA=
Expand Down
17 changes: 15 additions & 2 deletions pkg/visor/survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package visor

import (
"context"
"encoding/json"
"os"
"strings"
Expand Down Expand Up @@ -32,7 +33,7 @@ func GenerateSurvey(v *Visor, log *logging.Logger, routine bool) {
log.Info("Skycoin reward address: ", cAddr.String())
//generate the system survey
pathutil.EnsureDir(v.conf.LocalPath) //nolint
survey, err := visconf.SystemSurvey(v.conf.Dmsg.Discovery)
survey, err := visconf.SystemSurvey()
if err != nil {
log.WithError(err).Error("Could not read system info.")
return
Expand All @@ -51,6 +52,18 @@ func GenerateSurvey(v *Visor, log *logging.Logger, routine bool) {
survey.ServicesURLs.StunServers = v.conf.StunServers
survey.DmsgServers = v.dmsgC.ConnectedServersPK()

//use the existing dmsg client of the visor to get ip from dmsg server
tries := 8
for tries > 0 {
ipAddr, err := v.dmsgC.LookupIP(context.Background(), nil)
if err != nil {
tries--
continue
}
survey.IPAddr = ipAddr.String()
break
}

log.Info("Generating system survey")
v.surveyLock.Lock()
v.survey = survey
Expand All @@ -71,7 +84,7 @@ func GenerateSurvey(v *Visor, log *logging.Logger, routine bool) {
v.surveyLock.Lock()
v.survey = visconf.Survey{}
v.surveyLock.Unlock()
log.Debug("Removed hadware survey for visor not seeking rewards")
log.Debug("Removed survey for visor not seeking rewards")
}
// break loop for generate each 24hours if just reward address chenged
if !routine {
Expand Down
46 changes: 0 additions & 46 deletions pkg/visor/visorconfig/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
package visorconfig

import (
"context"
"fmt"
"net/http"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"

"github.com/bitfield/script"
"github.com/skycoin/dmsg/pkg/disc"
"github.com/skycoin/dmsg/pkg/dmsg"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/cmdutil"
"github.com/skycoin/skywire-utilities/pkg/logging"
"github.com/skycoin/skywire/pkg/skyenv"
)

Expand Down Expand Up @@ -232,47 +227,6 @@ func IsRoot() bool {
return userLvl.Username == "root"
}

// FetchIP fetches the ip address by dmsg servers
func FetchIP(dmsgDisc string) (string, error) {
log := logging.MustGetLogger("ip_skycoin_fetch_dmsg")
ctx, cancel := cmdutil.SignalContext(context.Background(), nil)
defer cancel()

pk, sk := cipher.GenerateKeyPair()

dmsgC, closeDmsg, err := startDmsg(ctx, log, pk, sk, dmsgDisc)
if err != nil {
return "", fmt.Errorf("failed to start dmsg")
}
defer closeDmsg()

ip, err := dmsgC.LookupIP(ctx, nil)
return ip.String(), err
}

func startDmsg(ctx context.Context, log *logging.Logger, pk cipher.PubKey, sk cipher.SecKey, dmsgDisc string) (dmsgC *dmsg.Client, stop func(), err error) {
dmsgC = dmsg.NewClient(pk, sk, disc.NewHTTP(dmsgDisc, &http.Client{}, log), &dmsg.Config{MinSessions: dmsg.DefaultMinSessions})
go dmsgC.Serve(context.Background())

stop = func() {
err := dmsgC.Close()
log.WithError(err).Debug("Disconnected from dmsg network.")
fmt.Printf("\n")
}
log.WithField("public_key", pk.String()).WithField("dmsg_disc", dmsgDisc).
Debug("Connecting to dmsg network...")

select {
case <-ctx.Done():
stop()
return nil, nil, ctx.Err()

case <-dmsgC.Ready():
log.Debug("Dmsg network ready.")
return dmsgC, stop, nil
}
}

var (
// VisorConfigFile will contain the path to the visor's config or `stdin` to denote that the config was read from STDIN
VisorConfigFile string
Expand Down
18 changes: 9 additions & 9 deletions pkg/visor/visorconfig/values_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ type Survey struct {
}

// SystemSurvey returns system survey
func SystemSurvey(dmsgDisc string) (Survey, error) {
func SystemSurvey() (Survey, error) {
disks, err := ghw.Block(ghw.WithDisableWarnings())
if err != nil {
return Survey{}, err
}
var ipAddr string
for {
ipAddr, err = FetchIP(dmsgDisc)
if err == nil {
break
}
}
// var ipAddr string
// for {
// ipAddr, err = FetchIP(dmsgDisc)
// if err == nil {
// break
// }
// }
s := Survey{
IPAddr: ipAddr,
// IPAddr: ipAddr,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
UUID: uuid.New(),
Expand Down
20 changes: 10 additions & 10 deletions pkg/visor/visorconfig/values_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Survey struct {
}

// SystemSurvey returns system survey
func SystemSurvey(dmsgDisc string) (Survey, error) {
func SystemSurvey() (Survey, error) {
var si sysinfo.SysInfo
si.GetSysInfo()
disks, err := ghw.Block(ghw.WithDisableWarnings())
Expand All @@ -63,16 +63,16 @@ func SystemSurvey(dmsgDisc string) (Survey, error) {
if err != nil && !strings.Contains(err.Error(), "Could not determine total usable bytes of memory") {
return Survey{}, err
}
var ipAddr string
for {
ipAddr, err = FetchIP(dmsgDisc)
if err == nil {
break
}
}
// var ipAddr string
// for {
// ipAddr, err = FetchIP(dmsgDisc)
// if err == nil {
// break
// }
// }
s := Survey{
Timestamp: time.Now(),
IPAddr: ipAddr,
Timestamp: time.Now(),
// IPAddr: ipAddr,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
SYSINFO: si,
Expand Down
18 changes: 9 additions & 9 deletions pkg/visor/visorconfig/values_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Survey struct {
}

// SystemSurvey returns system survey
func SystemSurvey(dmsgDisc string) (Survey, error) {
func SystemSurvey() (Survey, error) {
disks, err := ghw.Block(ghw.WithDisableWarnings())
if err != nil {
return Survey{}, err
Expand All @@ -56,16 +56,16 @@ func SystemSurvey(dmsgDisc string) (Survey, error) {
if err != nil {
return Survey{}, err
}
var ipAddr string
for {
ipAddr, err = FetchIP(dmsgDisc)
if err == nil {
break
}
}
// var ipAddr string
// for {
// ipAddr, err = FetchIP(dmsgDisc)
// if err == nil {
// break
// }
// }

s := Survey{
IPAddr: ipAddr,
// IPAddr: ipAddr,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
UUID: uuid.New(),
Expand Down

0 comments on commit b5414f6

Please sign in to comment.