add votingwalletsonline and totalvotingwallets to /vspinfo

This commit is contained in:
Ukane philemon 2022-03-17 11:40:47 +01:00 committed by GitHub
parent 9a4646f002
commit 3bb2f65151
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 38 deletions

View File

@ -54,6 +54,8 @@ when a VSP is closed will result in an error.
"vspdversion":"1.0.0-pre", "vspdversion":"1.0.0-pre",
"voting":10, "voting":10,
"voted":25, "voted":25,
"totalvotingwallets": 3,
"votingwalletsonline":3,
"revoked":3, "revoked":3,
"blockheight":623212, "blockheight":623212,
"estimatednetworkproportion":0.048478414 "estimatednetworkproportion":0.048478414

View File

@ -20,15 +20,17 @@ import (
// apiCache is used to cache values which are commonly used by the API, so // apiCache is used to cache values which are commonly used by the API, so
// repeated web requests don't repeatedly trigger DB or RPC calls. // repeated web requests don't repeatedly trigger DB or RPC calls.
type apiCache struct { type apiCache struct {
UpdateTime string UpdateTime string
PubKey string PubKey string
DatabaseSize string DatabaseSize string
Voting int64 Voting int64
Voted int64 Voted int64
Revoked int64 Revoked int64
BlockHeight uint32 VotingWalletsOnline int64
NetworkProportion float32 TotalVotingWallets int64
RevokedProportion float32 BlockHeight uint32
NetworkProportion float32
RevokedProportion float32
} }
var cacheMtx sync.RWMutex var cacheMtx sync.RWMutex
@ -55,7 +57,7 @@ func initCache() {
// updateCache updates the dynamic values in the cache (ticket counts and best // updateCache updates the dynamic values in the cache (ticket counts and best
// block height). // block height).
func updateCache(ctx context.Context, db *database.VspDatabase, func updateCache(ctx context.Context, db *database.VspDatabase,
dcrd rpc.DcrdConnect, netParams *chaincfg.Params) error { dcrd rpc.DcrdConnect, netParams *chaincfg.Params, wallets rpc.WalletConnect) error {
dbSize, err := db.Size() dbSize, err := db.Size()
if err != nil { if err != nil {
@ -83,6 +85,14 @@ func updateCache(ctx context.Context, db *database.VspDatabase,
return errors.New("dcr node reports a network ticket pool size of zero") return errors.New("dcr node reports a network ticket pool size of zero")
} }
clients, failedConnections := wallets.Clients(ctx, cfg.NetParams)
if len(clients) == 0 {
log.Error("Could not connect to any wallets")
} else if len(failedConnections) > 0 {
log.Errorf("Failed to connect to %d wallet(s), proceeding with only %d",
len(failedConnections), len(clients))
}
cacheMtx.Lock() cacheMtx.Lock()
defer cacheMtx.Unlock() defer cacheMtx.Unlock()
@ -90,6 +100,8 @@ func updateCache(ctx context.Context, db *database.VspDatabase,
cache.DatabaseSize = humanize.Bytes(dbSize) cache.DatabaseSize = humanize.Bytes(dbSize)
cache.Voting = voting cache.Voting = voting
cache.Voted = voted cache.Voted = voted
cache.TotalVotingWallets = int64(len(clients) + len(failedConnections))
cache.VotingWalletsOnline = int64(len(clients))
cache.Revoked = revoked cache.Revoked = revoked
cache.BlockHeight = bestBlock.Height cache.BlockHeight = bestBlock.Height
cache.NetworkProportion = float32(voting) / float32(bestBlock.PoolSize) cache.NetworkProportion = float32(voting) / float32(bestBlock.PoolSize)

View File

@ -5,19 +5,21 @@
package webapi package webapi
type vspInfoResponse struct { type vspInfoResponse struct {
APIVersions []int64 `json:"apiversions"` APIVersions []int64 `json:"apiversions"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
PubKey []byte `json:"pubkey"` PubKey []byte `json:"pubkey"`
FeePercentage float64 `json:"feepercentage"` FeePercentage float64 `json:"feepercentage"`
VspClosed bool `json:"vspclosed"` VspClosed bool `json:"vspclosed"`
VspClosedMsg string `json:"vspclosedmsg"` VspClosedMsg string `json:"vspclosedmsg"`
Network string `json:"network"` Network string `json:"network"`
VspdVersion string `json:"vspdversion"` VspdVersion string `json:"vspdversion"`
Voting int64 `json:"voting"` Voting int64 `json:"voting"`
Voted int64 `json:"voted"` Voted int64 `json:"voted"`
Revoked int64 `json:"revoked"` TotalVotingWallets int64 `json:"totalvotingwallets"`
BlockHeight uint32 `json:"blockheight"` VotingWalletsOnline int64 `json:"votingwalletsonline"`
NetworkProportion float32 `json:"estimatednetworkproportion"` Revoked int64 `json:"revoked"`
BlockHeight uint32 `json:"blockheight"`
NetworkProportion float32 `json:"estimatednetworkproportion"`
} }
type feeAddressRequest struct { type feeAddressRequest struct {

View File

@ -15,18 +15,20 @@ import (
func vspInfo(c *gin.Context) { func vspInfo(c *gin.Context) {
cachedStats := getCache() cachedStats := getCache()
sendJSONResponse(vspInfoResponse{ sendJSONResponse(vspInfoResponse{
APIVersions: []int64{3}, APIVersions: []int64{3},
Timestamp: time.Now().Unix(), Timestamp: time.Now().Unix(),
PubKey: signPubKey, PubKey: signPubKey,
FeePercentage: cfg.VSPFee, FeePercentage: cfg.VSPFee,
Network: cfg.NetParams.Name, Network: cfg.NetParams.Name,
VspClosed: cfg.VspClosed, VspClosed: cfg.VspClosed,
VspClosedMsg: cfg.VspClosedMsg, VspClosedMsg: cfg.VspClosedMsg,
VspdVersion: version.String(), VspdVersion: version.String(),
Voting: cachedStats.Voting, Voting: cachedStats.Voting,
Voted: cachedStats.Voted, Voted: cachedStats.Voted,
Revoked: cachedStats.Revoked, TotalVotingWallets: cachedStats.TotalVotingWallets,
BlockHeight: cachedStats.BlockHeight, VotingWalletsOnline: cachedStats.VotingWalletsOnline,
NetworkProportion: cachedStats.NetworkProportion, Revoked: cachedStats.Revoked,
BlockHeight: cachedStats.BlockHeight,
NetworkProportion: cachedStats.NetworkProportion,
}, c) }, c)
} }

View File

@ -85,7 +85,7 @@ func Start(ctx context.Context, requestShutdown func(), shutdownWg *sync.WaitGro
// Populate cached VSP stats before starting webserver. // Populate cached VSP stats before starting webserver.
initCache() initCache()
err = updateCache(ctx, vdb, dcrd, config.NetParams) err = updateCache(ctx, vdb, dcrd, config.NetParams, wallets)
if err != nil { if err != nil {
log.Errorf("Could not initialize VSP stats cache: %v", err) log.Errorf("Could not initialize VSP stats cache: %v", err)
} }
@ -172,7 +172,7 @@ func Start(ctx context.Context, requestShutdown func(), shutdownWg *sync.WaitGro
shutdownWg.Done() shutdownWg.Done()
return return
case <-ticker.C: case <-ticker.C:
err := updateCache(ctx, vdb, dcrd, config.NetParams) err := updateCache(ctx, vdb, dcrd, config.NetParams, wallets)
if err != nil { if err != nil {
log.Errorf("Failed to update cached VSP stats: %v", err) log.Errorf("Failed to update cached VSP stats: %v", err)
} }