Update p2pool-observer dependency, pool_info now using types instead of map[string]any

This commit is contained in:
DataHoarder 2023-07-30 21:02:05 +02:00
parent 951955ca6e
commit 4eaef54a9d
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
5 changed files with 17 additions and 51 deletions

42
api.go
View file

@ -4,42 +4,15 @@ import (
"encoding/json"
"fmt"
"git.gammaspectra.live/P2Pool/p2pool-observer/cmd/index"
"git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils"
"io"
"net/http"
"net/url"
"slices"
"strconv"
"strings"
"time"
)
func getFromAPI(host, method string) any {
uri, _ := url.Parse(host + method)
if response, err := http.DefaultClient.Do(&http.Request{
Method: "GET",
URL: uri,
}); err != nil {
return nil
} else {
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
if strings.Index(response.Header.Get("content-type"), "/json") != -1 {
var result any
decoder := json.NewDecoder(response.Body)
decoder.UseNumber()
err = decoder.Decode(&result)
return result
} else if data, err := io.ReadAll(response.Body); err != nil {
return nil
} else {
return data
}
} else {
return nil
}
}
}
func getTypeFromAPI[T any](host, method string) *T {
uri, _ := url.Parse(host + method)
if response, err := http.DefaultClient.Do(&http.Request{
@ -152,20 +125,15 @@ func toUint64(t any) uint64 {
return 0
}
func getPoolInfo(host string) map[string]any {
var basePoolInfo map[string]any
var ok bool
func getPoolInfo(host string) *utils.PoolInfoResult {
for {
d := getFromAPI(host, "/api/pool_info")
basePoolInfo, ok = d.(map[string]any)
if d == nil || !ok || basePoolInfo == nil || len(basePoolInfo) == 0 {
d := getTypeFromAPI[utils.PoolInfoResult](host, "/api/pool_info")
if d == nil || d.SideChain.LastBlock == nil {
time.Sleep(5)
continue
}
break
return d
}
return basePoolInfo
}
func getPreviousBlocks(host string) (result foundBlocks) {

View file

@ -86,9 +86,7 @@ var commands = []command{
effort = float64(lastFound.CumulativeDifficulty.Sub(previous.CumulativeDifficulty).Mul64(100).Lo) / float64(lastFound.MainBlock.Difficulty)
}
currentEffort := toFloat64(poolInfo["sidechain"].(map[string]any)["effort"].(map[string]any)["current"])
currentDifficulty, _ := types.DifficultyFromString(poolInfo["mainchain"].(map[string]any)["difficulty"].(string))
currentEffort := toFloat64(poolInfo.SideChain.Effort.Current)
bot.Msg(replyTo, fmt.Sprintf(
"Pool %s, last block found at height %s%d%s %s, %s UTC :: %s :: Effort %s%.02f%%%s :: %s%d miner outputs%s paid for %s%s%s XMR%s :: Current Effort %s%.02f%%%s :: Pool height %d :: Pool Hashrate %sH/s :: Global hashrate %sH/s",
@ -103,7 +101,7 @@ var commands = []command{
EffortColor(currentEffort), currentEffort, FormatReset,
e.Tip.SideHeight,
utils.SiUnits(float64(types.DifficultyFrom64(e.Tip.Difficulty).Div64(e.Consensus.TargetBlockTime).Lo), 2),
utils.SiUnits(float64(currentDifficulty.Div64(monero.BlockTime).Lo), 2),
utils.SiUnits(float64(poolInfo.MainChain.Difficulty.Div64(monero.BlockTime).Lo), 2),
))
}(e)
}

View file

@ -117,7 +117,7 @@ func (c *channelEntry) getConsensus() {
}
basePoolInfo := getPoolInfo(c.ApiEndpoint)
consensusData, _ := json.Marshal(basePoolInfo["sidechain"].(map[string]any)["consensus"].(map[string]any))
consensusData, _ := json.Marshal(basePoolInfo.SideChain.Consensus)
consensus, err := sidechain.NewConsensusFromJSON(consensusData)
if err != nil {
log.Panic(err)

6
go.mod
View file

@ -2,11 +2,11 @@ module git.gammaspectra.live/P2Pool/p2pool-observer-bot
go 1.21
replace git.gammaspectra.live/P2Pool/p2pool-observer v0.0.0 => git.gammaspectra.live/P2Pool/p2pool-observer v0.0.0-20230730122336-d91ef819f407
replace git.gammaspectra.live/P2Pool/p2pool-observer v0.0.0 => git.gammaspectra.live/P2Pool/p2pool-observer v0.0.0-20230730184502-ae74c56a08db
replace git.gammaspectra.live/P2Pool/p2pool-observer/cmd/index v0.0.0 => git.gammaspectra.live/P2Pool/p2pool-observer/cmd/index v0.0.0-20230730122336-d91ef819f407
replace git.gammaspectra.live/P2Pool/p2pool-observer/cmd/index v0.0.0 => git.gammaspectra.live/P2Pool/p2pool-observer/cmd/index v0.0.0-20230730184502-ae74c56a08db
replace git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils v0.0.0 => git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils v0.0.0-20230730122336-d91ef819f407
replace git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils v0.0.0 => git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils v0.0.0-20230730184502-ae74c56a08db
require (
git.gammaspectra.live/P2Pool/p2pool-observer v0.0.0

12
go.sum
View file

@ -6,12 +6,12 @@ git.gammaspectra.live/P2Pool/go-randomx v0.0.0-20221027085532-f46adfce03a7 h1:bz
git.gammaspectra.live/P2Pool/go-randomx v0.0.0-20221027085532-f46adfce03a7/go.mod h1:3kT0v4AMwT/OdorfH2gRWPwoOrUX/LV03HEeBsaXG1c=
git.gammaspectra.live/P2Pool/moneroutil v0.0.0-20230722215223-18ecc51ae61e h1:ropqS9niQR/ZKCUrlmWe+uDH0fLIyAnCIjkEjyTDgA8=
git.gammaspectra.live/P2Pool/moneroutil v0.0.0-20230722215223-18ecc51ae61e/go.mod h1:Wn5QI7XIMHMpEu10pPspW9h3eGmXQPJwh/4/+Gi3G1U=
git.gammaspectra.live/P2Pool/p2pool-observer v0.0.0-20230730122336-d91ef819f407 h1:avsKrFi2jTyna6rhpppomLkRL7VLUHyxL3bYpMTKr6w=
git.gammaspectra.live/P2Pool/p2pool-observer v0.0.0-20230730122336-d91ef819f407/go.mod h1:7oVpm68TYKnu1KMXGSet0Y+ovCBYcjhQipNQtt/8nuo=
git.gammaspectra.live/P2Pool/p2pool-observer/cmd/index v0.0.0-20230730122336-d91ef819f407 h1:SuHa0QgssCpwdEkpTjgI3gqhRG1q7u5ghH49PQiwFUs=
git.gammaspectra.live/P2Pool/p2pool-observer/cmd/index v0.0.0-20230730122336-d91ef819f407/go.mod h1:ZgU2h8ysqmyighVP3cEVWLNXPx0tFNkSSVXZ1nve970=
git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils v0.0.0-20230730122336-d91ef819f407 h1:Mo7F3gVgElxG5rd32FxujxPmj7e0CsD0JqUpOHIJ+Ss=
git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils v0.0.0-20230730122336-d91ef819f407/go.mod h1:H2nPProb7CWsok7cXGQJwtx7fHvEao58C6E4/jfYjYA=
git.gammaspectra.live/P2Pool/p2pool-observer v0.0.0-20230730184502-ae74c56a08db h1:33aPn5PQY55QjGAqZ1AbeSbvDaUtiyoTJEHbYYKqw9A=
git.gammaspectra.live/P2Pool/p2pool-observer v0.0.0-20230730184502-ae74c56a08db/go.mod h1:q12Xdw10RxlVi2/sAU7g/mrIxsUlRYLVO0q4ykA/S+c=
git.gammaspectra.live/P2Pool/p2pool-observer/cmd/index v0.0.0-20230730184502-ae74c56a08db h1:FwpZGbSlI2dgssp8BMrJ/u0QRnP8e+tn/nALEvOKOR8=
git.gammaspectra.live/P2Pool/p2pool-observer/cmd/index v0.0.0-20230730184502-ae74c56a08db/go.mod h1:rrml9cJtwBZ1zZhMG6i1kUXt3p6kMHcsEtoaqw8a92g=
git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils v0.0.0-20230730184502-ae74c56a08db h1:qzAXl5SrilKO0Uc6h6ewPuKrl/akCBi0wEKZuAj8Zrs=
git.gammaspectra.live/P2Pool/p2pool-observer/cmd/utils v0.0.0-20230730184502-ae74c56a08db/go.mod h1:G3Jps0P4Hno9hNS4FSdKbTn83OO+7MFLRsV3HcaPWGI=
git.gammaspectra.live/P2Pool/randomx-go-bindings v0.0.0-20230514082649-9c5f18cd5a71 h1:MgeHHcF+GnCJBWMSzq8XAbc8p/UhNwFruEKCPPJ74YQ=
git.gammaspectra.live/P2Pool/randomx-go-bindings v0.0.0-20230514082649-9c5f18cd5a71/go.mod h1:KQaYHIxGXNHNMQELC7xGLu8xouwvP/dN7iGk681BXmk=
git.gammaspectra.live/P2Pool/sha3 v0.0.0-20230604092430-04fe7dc6439a h1:c24MHv/z+aBYpYNsQHcJqmFuaYInGVixJZgDCXA/4bs=