From e3f53114887f1aad644aaea6d2c94b1231ca6339 Mon Sep 17 00:00:00 2001 From: Moses Narrow Date: Sat, 13 Jul 2024 17:09:51 -0500 Subject: [PATCH] fix reward calculation --- cmd/skywire-cli/commands/rewards/calc.go | 56 +++++++++-- cmd/skywire-cli/commands/rewards/tgbot.go | 117 ---------------------- 2 files changed, 45 insertions(+), 128 deletions(-) delete mode 100644 cmd/skywire-cli/commands/rewards/tgbot.go diff --git a/cmd/skywire-cli/commands/rewards/calc.go b/cmd/skywire-cli/commands/rewards/calc.go index 32f211d14d..986017bb8d 100644 --- a/cmd/skywire-cli/commands/rewards/calc.go +++ b/cmd/skywire-cli/commands/rewards/calc.go @@ -3,7 +3,6 @@ package clirewards import ( "fmt" - "log" "os" "sort" "strconv" @@ -12,6 +11,9 @@ import ( "github.com/bitfield/script" "github.com/spf13/cobra" + + "github.com/skycoin/skywire-utilities/pkg/logging" + tgbot "github.com/skycoin/skywire/cmd/skywire-cli/commands/rewards/tgbot" ) const yearlyTotalRewards int = 408000 @@ -29,6 +31,8 @@ var ( h2 bool grr bool pubkey string + logLvl string + log *logging.Logger ) type nodeinfo struct { @@ -56,7 +60,11 @@ type rewardData struct { } func init() { + RootCmd.AddCommand( + tgbot.RootCmd, + ) RootCmd.Flags().SortFlags = false + RootCmd.Flags().StringVarP(&logLvl, "loglvl", "s", "info", "[ debug | warn | error | fatal | panic | trace ] \u001b[0m*") RootCmd.Flags().StringVarP(&wdate, "date", "d", wdate, "date for which to calculate reward") RootCmd.Flags().StringVarP(&pubkey, "pk", "k", pubkey, "check reward for pubkey") RootCmd.Flags().StringVarP(&disallowArchitectures, "noarch", "n", "amd64", "disallowed architectures, comma separated") @@ -79,6 +87,15 @@ Collect surveys: skywire-cli log Fetch uptimes: skywire-cli ut > ut.txt`, Run: func(cmd *cobra.Command, args []string) { var err error + if log == nil { + log = logging.MustGetLogger("rewards") + } + if logLvl != "" { + if lvl, err := logging.LevelFromString(logLvl); err == nil { + logging.SetLevel(lvl) + } + } + wDate, err = time.Parse("2006-01-02", wdate) if err != nil { log.Fatal("Error parsing date:", err) @@ -119,25 +136,42 @@ 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_address`).Replace(" ", "").Replace(`"`, "").String() //nolint + _, err = os.Stat(nodeInfo) + if os.IsNotExist(err) { + log.Debug(err.Error()) + continue + } + var ( + tpsn string + tpsnErr error + ip string + sky string + arch string + uu string + ifc string + ifc1 string + macs []string + macs1 []string + ) + 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 == "" { + if strings.Count(ip, ".") != 3 { 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, _ = 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 + arch, _ = script.File(nodeInfo).JQ(".go_arch").Replace(" ", "").Replace(`"`, "").String() //nolint arch = strings.TrimRight(arch, "\n") - uu, _ := script.File(nodeInfo).JQ(".uuid").Replace(" ", "").Replace(`"`, "").String() //nolint + uu, _ = script.File(nodeInfo).JQ(".uuid").Replace(" ", "").Replace(`"`, "").String() //nolint uu = strings.TrimRight(uu, "\n") - ifc, _ := script.File(nodeInfo).JQ(`[.ip_addr[]? | select(.ifname != "lo") | {address: .address, ifname: .ifname}]`).Replace(" ", "").Replace(`"`, "").String() //nolint + ifc, _ = script.File(nodeInfo).JQ(`[.ip_addr[]? | select(.ifname != "lo") | {address: .address, ifname: .ifname}]`).Replace(" ", "").Replace(`"`, "").String() //nolint ifc = strings.TrimRight(ifc, "\n") - ifc1, _ := script.File(nodeInfo).JQ(`[.zcalusic_sysinfo.network[] | {address: .macaddress, ifname: .name}]`).Replace(" ", "").Replace(`"`, "").String() //nolint + ifc1, _ = script.File(nodeInfo).JQ(`[.zcalusic_sysinfo.network[] | {address: .macaddress, ifname: .name}]`).Replace(" ", "").Replace(`"`, "").String() //nolint ifc1 = strings.TrimRight(ifc1, "\n") - macs, _ := script.File(nodeInfo).JQ(`.ip_addr[]? | select(.ifname != "lo") | .address`).Replace(" ", "").Replace(`"`, "").Slice() //nolint - macs1, _ := script.File(nodeInfo).JQ(`.zcalusic_sysinfo.network[] | .macaddress`).Replace(" ", "").Replace(`"`, "").Slice() //nolint + macs, _ = script.File(nodeInfo).JQ(`.ip_addr[]? | select(.ifname != "lo") | .address`).Replace(" ", "").Replace(`"`, "").Slice() //nolint + macs1, _ = script.File(nodeInfo).JQ(`.zcalusic_sysinfo.network[] | .macaddress`).Replace(" ", "").Replace(`"`, "").Slice() //nolint if ifc == "[]" && ifc1 != "[]" { ifc = ifc1 } diff --git a/cmd/skywire-cli/commands/rewards/tgbot.go b/cmd/skywire-cli/commands/rewards/tgbot.go deleted file mode 100644 index ea698665f9..0000000000 --- a/cmd/skywire-cli/commands/rewards/tgbot.go +++ /dev/null @@ -1,117 +0,0 @@ -// Package clirewards cmd/skywire-cli/commands/rewards/tgbot.go -package clirewards - -import ( - "fmt" - "log" - "os" - "strconv" - "strings" - "time" - - "github.com/bitfield/script" - "github.com/spf13/cobra" - tele "gopkg.in/telebot.v3" -) - -var filePath string - -func init() { - RootCmd.AddCommand( - tgbotCmd, - ) - tgbotCmd.Flags().StringVarP(&filePath, "watch", "w", "../reward/rewards/transactions0.txt", "File to watch - file where reward transaction IDs are recorded") - -} - -var tgbotCmd = &cobra.Command{ - Use: "bot", - Short: "reward notification telegram bot", - Long: "reward notification telegram bot", - Run: func(_ *cobra.Command, _ []string) { - chatIDStr := os.Getenv("TG_CHAT_ID") - chatID, err := strconv.ParseInt(chatIDStr, 10, 64) - if err != nil { - log.Fatalf("failed to parse chat ID: %v", err) - } - pref := tele.Settings{ - Token: os.Getenv("TG_BOT_TOKEN"), - Poller: &tele.LongPoller{Timeout: 10 * time.Second}, - } - - b, err := tele.NewBot(pref) - if err != nil { - log.Fatal(err) - return - } - - tgbotscript := `#!/bin/bash - _stats() { - # [[ $1 == "" ]] && return - cat ` + strings.TrimSuffix(filePath, "/transactions0.txt") + `/hist/$(find ` + strings.TrimSuffix(filePath, "/transactions0.txt") + `/hist/ -name "*.txt" -type f -exec grep -l "$1" {} + | xargs -I{} basename {} | tr -d ".txt")_stats.txt - } - ` - - // var lastModTime time.Time - lastModTime, err := os.Stat(filePath) - if err != nil { - log.Fatal(err) - return - } - // Use a goroutine to periodically check the file for changes - go func() { - for { - time.Sleep(2 * time.Second) - - fileInfo, err := os.Stat(filePath) - if err != nil { - log.Printf("Error checking file info: %s", err) - continue - } - - if fileInfo.ModTime().After(lastModTime.ModTime()) { - // The file has been modified since the last check, get the last line which is the most recent txid - lastLine, err := script.File(filePath).Last(1).String() - if err != nil { - log.Printf("Error getting last line of file: %v", err) - continue - } - if lastLine != "" { - tmpFile, err := os.CreateTemp(os.TempDir(), "*.sh") - if err != nil { - return - } - if err := tmpFile.Close(); err != nil { - return - } - _, _ = script.Exec(`chmod +x ` + tmpFile.Name()).String() //nolint - _, _ = script.Echo(tgbotscript).WriteFile(tmpFile.Name()) //nolint - stats, err := script.Exec(`bash -c 'source ` + tmpFile.Name() + ` ; _stats ` + lastLine + `'`).String() //nolint - if err != nil { - log.Printf("Error getting statistics: %v", err) - continue - } - os.Remove(tmpFile.Name()) //nolint - - dateforlink, err := script.Echo(stats).First(1).Replace("date: ", "").String() - if err != nil { - log.Printf("Error getting date for link: %v", err) - continue - } - msg := fmt.Sprintf("Rewards have been distributed!\n\nhttps://explorer.skycoin.com/app/transaction/%s\n\n%s\n\nhttps://fiber.skywire.dev/skycoin-rewards/hist/%s", lastLine, stats, dateforlink) - // Send the last line to the Telegram chat - _, err = b.Send(&tele.Chat{ID: chatID}, msg) - if err != nil { - log.Printf("Error sending message to Telegram chat: %s", err) - continue - } - } - - lastModTime = fileInfo - } - } - }() - - b.Start() - }, -}