diff --git a/go.mod b/go.mod index eb2e9e5..1345281 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,11 @@ module github.com/decred/dcrwebapi -go 1.18 +go 1.23.0 require ( github.com/decred/dcrd/dcrutil/v4 v4.0.2 github.com/decred/dcrdata/v6 v6.0.0 + github.com/decred/vspd/types/v3 v3.0.0 github.com/gorilla/handlers v1.5.2 ) diff --git a/go.sum b/go.sum index edf4b4f..3b7e88b 100644 --- a/go.sum +++ b/go.sum @@ -91,6 +91,8 @@ github.com/decred/go-socks v1.1.0/go.mod h1:sDhHqkZH0X4JjSa02oYOGhcGHYp12FsY1jQ/ github.com/decred/slog v1.1.0/go.mod h1:kVXlGnt6DHy2fV5OjSeuvCJ0OmlmTF6LFpEPMu/fOY0= github.com/decred/slog v1.2.0 h1:soHAxV52B54Di3WtKLfPum9OFfWqwtf/ygf9njdfnPM= github.com/decred/slog v1.2.0/go.mod h1:kVXlGnt6DHy2fV5OjSeuvCJ0OmlmTF6LFpEPMu/fOY0= +github.com/decred/vspd/types/v3 v3.0.0 h1:jHlQIpp6aCjIcFs8WE3AaVCJe1kgepNTq+nkBKAyQxk= +github.com/decred/vspd/types/v3 v3.0.0/go.mod h1:hwifRZu6tpkbhSg2jZCUwuPaO/oETgbSCWCYJd4XepY= github.com/dgraph-io/badger v1.6.2/go.mod h1:JW2yswe3V058sS0kZ2h/AXeDSqFjxnZcRrVH//y2UQE= github.com/dgraph-io/ristretto v0.0.2/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= diff --git a/service.go b/service.go index f921b5f..1f9b27e 100644 --- a/service.go +++ b/service.go @@ -16,6 +16,7 @@ import ( "github.com/decred/dcrd/dcrutil/v4" apitypes "github.com/decred/dcrdata/v6/api/types" "github.com/decred/dcrdata/v6/db/dbtypes" + "github.com/decred/vspd/types/v3" ) // Vsp contains information about a single Voting Service Provider. Includes @@ -36,8 +37,8 @@ type Vsp struct { Expired int64 `json:"expired"` Missed int64 `json:"missed"` VspdVersion string `json:"vspdversion"` - BlockHeight uint64 `json:"blockheight"` - EstimatedNetworkProportion float64 `json:"estimatednetworkproportion"` + BlockHeight uint32 `json:"blockheight"` + EstimatedNetworkProportion float32 `json:"estimatednetworkproportion"` } type vspSet map[string]Vsp @@ -170,15 +171,13 @@ func NewService() *Service { func (service *Service) getHTTP(url string) ([]byte, error) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { - return nil, fmt.Errorf("%v: failed to create request: %v", - url, err) + return nil, fmt.Errorf("%v: failed to create request: %w", url, err) } req.Header.Set("User-Agent", "decred/dcrweb bot") poolResp, err := service.HTTPClient.Do(req) if err != nil { - return nil, fmt.Errorf("%v: failed to send request: %v", - url, err) + return nil, fmt.Errorf("%v: failed to send request: %w", url, err) } defer poolResp.Body.Close() @@ -189,8 +188,7 @@ func (service *Service) getHTTP(url string) ([]byte, error) { respBody, err := io.ReadAll(poolResp.Body) if err != nil { - return nil, fmt.Errorf("%v: failed to read body: %v", - url, err) + return nil, fmt.Errorf("%v: failed to read body: %w", url, err) } return respBody, nil @@ -209,46 +207,22 @@ func vspStats(service *Service, url string) error { return err } - var info map[string]interface{} + var info types.VspInfoResponse err = json.Unmarshal(infoResp, &info) if err != nil { - return fmt.Errorf("%v: unmarshal failed: %v", - infoURL, err) + return fmt.Errorf("%v: unmarshal failed: %w", infoURL, err) } - apiversions, hasAPIVersions := info["apiversions"] - feepercentage, hasFeePercentage := info["feepercentage"] - vspclosed, hasClosed := info["vspclosed"] - voting, hasVoting := info["voting"] - voted, hasVoted := info["voted"] - expired, hasExpired := info["expired"] - missed, hasMissed := info["missed"] - version, hasVersion := info["vspdversion"] - blockheight, hasBlockHeight := info["blockheight"] - networkproportion, hasnetworkproportion := info["estimatednetworkproportion"] - - hasRequiredFields := hasAPIVersions && hasFeePercentage && - hasClosed && hasVoting && hasVoted && hasExpired && hasMissed && - hasVersion && hasBlockHeight && hasnetworkproportion - - if !hasRequiredFields { - return fmt.Errorf("%v: missing required fields: %+v", infoURL, info) - } - - vsp.APIVersions = make([]int64, 0) - for _, i := range apiversions.([]interface{}) { - vsp.APIVersions = append(vsp.APIVersions, int64(i.(float64))) - } - - vsp.FeePercentage = feepercentage.(float64) - vsp.Closed = vspclosed.(bool) - vsp.Voting = int64(voting.(float64)) - vsp.Voted = int64(voted.(float64)) - vsp.Expired = int64(expired.(float64)) - vsp.Missed = int64(missed.(float64)) - vsp.VspdVersion = version.(string) - vsp.BlockHeight = uint64(blockheight.(float64)) - vsp.EstimatedNetworkProportion = networkproportion.(float64) + vsp.APIVersions = info.APIVersions + vsp.FeePercentage = info.FeePercentage + vsp.Closed = info.VspClosed + vsp.Voting = info.Voting + vsp.Voted = info.Voted + vsp.Expired = info.Expired + vsp.Missed = info.Missed + vsp.VspdVersion = info.VspdVersion + vsp.BlockHeight = info.BlockHeight + vsp.EstimatedNetworkProportion = info.NetworkProportion vsp.LastUpdated = time.Now().Unix()