Skip to content

Commit

Permalink
Update Reward Calculation Cli & UI (#1884)
Browse files Browse the repository at this point in the history
* update & embed mainnet rules ; render mainnet rules in markdown in the terminal ; embed architectures

* update architecture setting in reward calculation & validate skycoin address

* update reward system UI

* add subcommand to set up systemd services for the reward system UI & backend

* make format check

* fix systemd service generation

* fix systemd service generation

* fix typo

* change reward calculation ; define two pools explicitly

* fix output text

* updates to reward UI for second pool

* continue refactoring reward system UI

* clean up reward system UI code ; remove no longer used display from skywire cli log st

* add more survey statistics to reward system UI

* update all deps in go.mod with GO111MODULE=on go get -u ./...

* fix CI error ; fix dependency error

* remove unused

* improve sorting of statistics in reward system UI

* update skycoin-service-discovery dependency
  • Loading branch information
0pcom authored Oct 19, 2024
1 parent 59a24a4 commit 0daac6f
Show file tree
Hide file tree
Showing 2,225 changed files with 499,217 additions and 178,681 deletions.
1 change: 1 addition & 0 deletions arches.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["amd64","arm64","386","arm","ppc64","riscv64","wasm","loong64","mips","mips64","mips64le","mipsle","ppc64le","s390x"]
100 changes: 100 additions & 0 deletions cmd/gen/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Package main cmd/gen/gen.go
package main

import (
"encoding/json"
"log"
"regexp"
"runtime"

"github.com/bitfield/script"
cc "github.com/ivanpirog/coloredcobra"
"github.com/spf13/cobra"
)

var (
printJSON bool
writeFile string
)

func init() {
rootCmd.Flags().BoolVarP(&printJSON, "json", "j", false, "output json format")
rootCmd.Flags().StringVarP(&writeFile, "output", "o", "", "write to the specified file (i.e. arches.json)")
}

// there is no other better way to get a list of architectures via a library or any simpler method at runtime
// this file is executed by go generate from the root directory of the repository github.com/skycoin/skywire
// go run cmd/gen/gen.go -j -o arches.json

var rootCmd = &cobra.Command{
Use: "gen",
Short: "print architectures",
Long: "print architectures",
Run: func(_ *cobra.Command, _ []string) {
if writeFile == "" {
switch runtime.GOOS {
case "windows":
writeFile = `\\.\NUL`
default: // For macOS and Linux
writeFile = "/dev/null"
}
}
if !printJSON {
//equivalent bash one-liner:
//go tool dist list | awk -F '/' '{print $NF}' | awk '{$1=$1};1' | sort | uniq
_, err := script.Exec(`go tool dist list`).ReplaceRegexp(regexp.MustCompile(".*/"), "").Freq().ReplaceRegexp(regexp.MustCompile(`^\s*\d+\s+`), "").Tee().WriteFile(writeFile)
if err != nil {
log.Fatal(err)
}
} else {
rawOutput, err := script.Exec(`go tool dist list`).ReplaceRegexp(regexp.MustCompile(".*/"), "").Freq().ReplaceRegexp(regexp.MustCompile(`^\s*\d+\s+`), "").Slice()
if err != nil {
log.Fatal(err)
}

jsonData, err := json.Marshal(rawOutput)
if err != nil {
log.Fatal(err)
}
//equivalent bash one-liner:
//go tool dist list | awk -F '/' '{print $NF}' | awk '{$1=$1};1' | sort | uniq | jq -R -s -c 'split("\n") | map(select(length > 0))' | tee arches.json
_, err = script.Echo(string(jsonData) + "\n").Tee().WriteFile(writeFile)
if err != nil {
log.Fatal(err)
}
}
},
}

func init() {
var helpflag bool
rootCmd.SetUsageTemplate(help)
rootCmd.PersistentFlags().BoolVarP(&helpflag, "help", "h", false, "help menu")
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
rootCmd.PersistentFlags().MarkHidden("help") //nolint
}

func main() {
cc.Init(&cc.Config{
RootCmd: rootCmd,
Headings: cc.HiBlue + cc.Bold,
Commands: cc.HiBlue + cc.Bold,
CmdShortDescr: cc.HiBlue,
Example: cc.HiBlue + cc.Italic,
ExecName: cc.HiBlue + cc.Bold,
Flags: cc.HiBlue + cc.Bold,
FlagsDescr: cc.HiBlue,
NoExtraNewlines: true,
NoBottomNewline: true,
})
rootCmd.Execute() //nolint
}

const help = "{{if .HasAvailableSubCommands}}{{end}} {{if gt (len .Aliases) 0}}\r\n\r\n" +
"{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}" +
"Available Commands:{{range .Commands}} {{if and (ne .Name \"completion\") .IsAvailableCommand}}\r\n " +
"{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}\r\n\r\n" +
"Flags:\r\n" +
"{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}\r\n\r\n" +
"Global Flags:\r\n" +
"{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}\r\n\r\n"
2 changes: 0 additions & 2 deletions cmd/skywire-cli/commands/log/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ var (
fetchFrom string
writeDir string
lcDir string
tpsnDir string
hideErr bool
showUT bool
pubKeys []cipher.PubKey
proxyCSV string
)

// RootCmd is logCmd
Expand Down
44 changes: 5 additions & 39 deletions cmd/skywire-cli/commands/log/st.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ func init() {
RootCmd.AddCommand(stCmd)
stCmd.Flags().StringVarP(&pubKey, "pk", "p", "", "public key(s) to check ; comma separated")
stCmd.Flags().StringVarP(&lcDir, "lcdir", "d", "", "path to surveys & transport bandwidth logging ")
stCmd.Flags().StringVarP(&tpsnDir, "tpsndir", "e", "", "path to transport setup-node surveys")
stCmd.Flags().StringVarP(&proxyCSV, "proxycsv", "x", "", "path to proxy test csv")
stCmd.Flags().BoolVarP(&hideErr, "noerr", "r", false, "hide error logging from output")
stCmd.Flags().BoolVarP(&showUT, "ut", "u", false, "show uptime percentage for the past two days and current online status")
}
Expand Down Expand Up @@ -50,19 +48,14 @@ func makeTree() {
utData, utDataErr := script.File("/tmp/ut.json").String()
var tree pterm.TreeNode
rootDir := lcDir
otherDir := tpsnDir
dn, err := script.ListFiles(rootDir).String()
if err != nil && !hideErr {
errstring := "script.ListFiles(" + rootDir + ").String()"
log.Printf("%v error: %v\n", errstring, err)
}
dn1, err := script.ListFiles(otherDir).String()
dirNodes, err := script.Echo(dn).Basename().Freq().Column(2).Slice()
if err != nil && !hideErr {
log.Printf("script.ListFiles("+otherDir+").String() error: %v\n", err)
}
dirNodes, err := script.Echo(dn + dn1).Basename().Freq().Column(2).Slice()
if err != nil && !hideErr {
log.Printf("script.Echo(dn + dn1).Basename().Freq().Column(2).Slice() error: %v\n", err)
log.Printf("script.Echo(dn).Basename().Freq().Column(2).Slice() error: %v\n", err)
}
if len(pubKeys) > 0 {
var checkTheseKeys string
Expand All @@ -82,19 +75,11 @@ func makeTree() {
errstring := fmt.Sprintf("script.ListFiles(\"%v\"/\"%v\").String()", rootDir, dirNode)
log.Printf("%v error: %v\n", errstring, err)
}
children1, err := script.ListFiles(otherDir + "/" + dirNode).String()
if err != nil && !hideErr {
errstring := fmt.Sprintf("script.ListFiles(\"%v\"/\"%v\").String()", otherDir, dirNode)
log.Printf("%v error: %v\n", errstring, err)
}
var ks string
if showUT {
ks = fmt.Sprintf("uptime\n%s\n%s", children, children1)
ks = fmt.Sprintf("uptime\n%s", children)
} else {
ks = fmt.Sprintf("%s\n%s", children, children1)
}
if proxyCSV != "" {
ks = fmt.Sprintf("%s\nproxy", ks)
ks = children
}
kids, err := script.Echo(ks).Slice()
if err != nil && !hideErr {
Expand All @@ -103,17 +88,6 @@ func makeTree() {
nodes := []pterm.TreeNode{}
for _, kid := range kids {
var coloredFile string
if kid == "proxy" {
testtime, _ := script.File(proxyCSV).Match(dirNode).Replace(",", " ").Column(2).String() //nolint
testres, _ := script.File(proxyCSV).Match(dirNode).Replace(",", " ").Column(3).String() //nolint
if testtime != "" && testres != "" {
coloredFile = fmt.Sprintf("%s %s %s", pterm.Green("proxy"), strings.ReplaceAll(testtime, "\n", ""), strings.ReplaceAll(testres, "\n", ""))
} else {
coloredFile = fmt.Sprintf("%s No data", pterm.Red("proxy"))
}
nodes = append(nodes, pterm.TreeNode{Text: coloredFile})
continue
}
if kid == "uptime" {
if utFileInfoErr != nil || utDataErr != nil {
continue
Expand All @@ -134,7 +108,7 @@ func makeTree() {
nodes = append(nodes, pterm.TreeNode{Text: fmt.Sprintf("%s Age: %s %s", coloredFile, time.Since(utFileInfo.ModTime()).Truncate(time.Second).String(), strings.TrimSuffix(pkUt, "\n"))})
continue
}
if filepath.Base(kid) == "health.json" || filepath.Base(kid) == "tp.json" {
if filepath.Base(kid) == "health.json" {
fileContents, _ := script.File(kid).String() //nolint
fileInfo, _ := os.Stat(kid) //nolint
if time.Since(fileInfo.ModTime()) < time.Hour {
Expand All @@ -145,14 +119,6 @@ func makeTree() {
if filepath.Base(kid) == "health.json" {
nodes = append(nodes, pterm.TreeNode{Text: fmt.Sprintf("%s Age: %s %s", coloredFile, time.Since(fileInfo.ModTime()).Truncate(time.Second).String(), strings.TrimSuffix(string(fileContents), "\n"))})
}
if filepath.Base(kid) == "tp.json" {
_, err := script.Echo(strings.TrimSuffix(string(fileContents), "\n")).JQ(".[]").String() //nolint
if err != nil {
fileContents = pterm.Red(strings.TrimSuffix(string(fileContents), "\n"))
coloredFile = pterm.Red(filepath.Base(kid))
}
nodes = append(nodes, pterm.TreeNode{Text: fmt.Sprintf("%s Age: %s %s", coloredFile, time.Since(fileInfo.ModTime()).Truncate(time.Second).String(), strings.TrimSuffix(string(fileContents), "\n"))})
}
continue
}
if filepath.Base(kid) == "node-info.json" {
Expand Down
9 changes: 6 additions & 3 deletions cmd/skywire-cli/commands/reward/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ const longtext = `
reward address setting
Sets the skycoin reward address for the visor.
The config is written to the root of the default local directory
The address is written to the root of the default 'local' directory specified in the visor's config
this config is served via dmsghttp along with transport logs
and the system hardware survey for automating reward distribution`
This file is parsed by the visor at runtime, and the skycoin address is included in the survey which is served via dmsghttp along with transport logs
and the system hardware survey for automating reward distribution
By setting a skycoin reward address, you consent to collection of the survey from the visors where the reward address is set.
The survey is ONLY for verification of reward eligibility. We respect your privacy.`

func longText() string {
//show configured reward address if valid configuration exists
Expand Down
31 changes: 31 additions & 0 deletions cmd/skywire-cli/commands/reward/rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Package clireward cmd/skywire-cli/reward/rules.go
package clireward

import (
"fmt"
"os"

markdown "github.com/MichaelMure/go-term-markdown"
"github.com/spf13/cobra"
"golang.org/x/term"

"github.com/skycoin/skywire"
)

func init() {
rewardCmd.AddCommand(rulesCmd)
}

var rulesCmd = &cobra.Command{
Use: "rules",
Short: "display the mainnet rules",
Long: "display the mainnet rules",
Run: func(_ *cobra.Command, _ []string) {
terminalWidth, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
terminalWidth = 80
}
leftPad := 6
fmt.Printf("%s\n", markdown.Render(skywire.MainnetRules, terminalWidth, leftPad))
},
}
Loading

0 comments on commit 0daac6f

Please sign in to comment.