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

@ -26,6 +26,8 @@ type apiCache struct {
Voting int64 Voting int64
Voted int64 Voted int64
Revoked int64 Revoked int64
VotingWalletsOnline int64
TotalVotingWallets int64
BlockHeight uint32 BlockHeight uint32
NetworkProportion float32 NetworkProportion float32
RevokedProportion float32 RevokedProportion float32
@ -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

@ -15,6 +15,8 @@ type vspInfoResponse struct {
VspdVersion string `json:"vspdversion"` VspdVersion string `json:"vspdversion"`
Voting int64 `json:"voting"` Voting int64 `json:"voting"`
Voted int64 `json:"voted"` Voted int64 `json:"voted"`
TotalVotingWallets int64 `json:"totalvotingwallets"`
VotingWalletsOnline int64 `json:"votingwalletsonline"`
Revoked int64 `json:"revoked"` Revoked int64 `json:"revoked"`
BlockHeight uint32 `json:"blockheight"` BlockHeight uint32 `json:"blockheight"`
NetworkProportion float32 `json:"estimatednetworkproportion"` NetworkProportion float32 `json:"estimatednetworkproportion"`

View File

@ -25,6 +25,8 @@ func vspInfo(c *gin.Context) {
VspdVersion: version.String(), VspdVersion: version.String(),
Voting: cachedStats.Voting, Voting: cachedStats.Voting,
Voted: cachedStats.Voted, Voted: cachedStats.Voted,
TotalVotingWallets: cachedStats.TotalVotingWallets,
VotingWalletsOnline: cachedStats.VotingWalletsOnline,
Revoked: cachedStats.Revoked, Revoked: cachedStats.Revoked,
BlockHeight: cachedStats.BlockHeight, BlockHeight: cachedStats.BlockHeight,
NetworkProportion: cachedStats.NetworkProportion, NetworkProportion: cachedStats.NetworkProportion,

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)
} }