From 9501dc9dc0984262478da34919f8accd735143e9 Mon Sep 17 00:00:00 2001 From: Massaki Archambault Date: Fri, 24 Dec 2021 12:45:17 -0500 Subject: [PATCH] fix reward value being added to counter multiple times An update to the heliumapi made minTime fuzzy, causing an activity to be returned even if it occured before the specified minTime. --- exporter.go | 126 +++++++++++++++++++++++++++------------------ heliumapi/query.go | 3 +- 2 files changed, 77 insertions(+), 52 deletions(-) diff --git a/exporter.go b/exporter.go index 73bc84b..37dbfc5 100644 --- a/exporter.go +++ b/exporter.go @@ -529,8 +529,7 @@ func (e *Exporter) collectAccountTransactionsMetrics(wg *sync.WaitGroup, ch chan } } - // add 1 second to the last update time to avoid querying the same activity twice - minTime := account.LastUpdate.Add(time.Second * 1) + minTime := account.LastUpdate // Explicitly set max_time, to avoid issues with server-side caching maxTime := time.Now() activities, err := heliumapi.GetActivityForAccount(account.Address, activityTypes, &minTime, &maxTime) @@ -541,74 +540,96 @@ func (e *Exporter) collectAccountTransactionsMetrics(wg *sync.WaitGroup, ch chan // impl based on https://github.com/helium/hotspot-app/blob/918563fba84d1abf4554a43a4d42bb838d017bd3/src/features/wallet/root/useActivityItem.tsx#L336 for _, activity := range activities.AddGatewayV1 { - account.Tx.WithdrawalTotal += activity.StakingFee - updateLastActivityTime(activity.Time) + if activity.Time > lastActivityTime { + account.Tx.WithdrawalTotal += activity.StakingFee + updateLastActivityTime(activity.Time) + } } for _, activity := range activities.AssertLocationV1 { - account.Tx.WithdrawalTotal += activity.StakingFee - updateLastActivityTime(activity.Time) + if activity.Time > lastActivityTime { + account.Tx.WithdrawalTotal += activity.StakingFee + updateLastActivityTime(activity.Time) + } } for _, activity := range activities.AssertLocationV2 { - account.Tx.WithdrawalTotal += activity.StakingFee - updateLastActivityTime(activity.Time) + if activity.Time > lastActivityTime { + account.Tx.WithdrawalTotal += activity.StakingFee + updateLastActivityTime(activity.Time) + } } for _, activity := range activities.PaymentV1 { - if activity.Payer == account.Address { - account.Tx.WithdrawalTotal += activity.Amount - } else { - account.Tx.DepositTotal += activity.Amount + if activity.Time > lastActivityTime { + if activity.Payer == account.Address { + account.Tx.WithdrawalTotal += activity.Amount + } else { + account.Tx.DepositTotal += activity.Amount + } + updateLastActivityTime(activity.Time) } - updateLastActivityTime(activity.Time) } for _, activity := range activities.PaymentV2 { - if activity.Payer == account.Address { - paymentTotal := 0 - for _, payment := range activity.Payments { - paymentTotal += payment.Amount - } - account.Tx.WithdrawalTotal += paymentTotal - } else { - for _, payment := range activity.Payments { - if payment.Payee == account.Address { - account.Tx.DepositTotal += payment.Amount + if activity.Time > lastActivityTime { + if activity.Payer == account.Address { + paymentTotal := 0 + for _, payment := range activity.Payments { + paymentTotal += payment.Amount + } + account.Tx.WithdrawalTotal += paymentTotal + } else { + for _, payment := range activity.Payments { + if payment.Payee == account.Address { + account.Tx.DepositTotal += payment.Amount + } } } + updateLastActivityTime(activity.Time) } - updateLastActivityTime(activity.Time) } for _, activity := range activities.RewardsV1 { - for _, reward := range activity.Rewards { - account.RewardsTotal[reward.Type] += reward.Amount - account.Tx.DepositTotal += reward.Amount + if activity.Time > lastActivityTime { + for _, reward := range activity.Rewards { + account.RewardsTotal[reward.Type] += reward.Amount + account.Tx.DepositTotal += reward.Amount + } + updateLastActivityTime(activity.Time) } - updateLastActivityTime(activity.Time) } for _, activity := range activities.RewardsV2 { - for _, reward := range activity.Rewards { - account.RewardsTotal[reward.Type] += reward.Amount - account.Tx.DepositTotal += reward.Amount + if activity.Time > lastActivityTime { + for _, reward := range activity.Rewards { + account.RewardsTotal[reward.Type] += reward.Amount + account.Tx.DepositTotal += reward.Amount + } + updateLastActivityTime(activity.Time) } - updateLastActivityTime(activity.Time) } for _, activity := range activities.StakeValidatorV1 { - account.Tx.WithdrawalTotal += activity.Stake - updateLastActivityTime(activity.Time) + if activity.Time > lastActivityTime { + account.Tx.WithdrawalTotal += activity.Stake + updateLastActivityTime(activity.Time) + } } for _, activity := range activities.TokenBurnV1 { - account.Tx.WithdrawalTotal += activity.Amount - updateLastActivityTime(activity.Time) + if activity.Time > lastActivityTime { + account.Tx.WithdrawalTotal += activity.Amount + updateLastActivityTime(activity.Time) + } } for _, activity := range activities.TransferHotspotV1 { - if activity.Buyer == account.Address { - account.Tx.WithdrawalTotal += activity.AmountToSeller - } else { - account.Tx.DepositTotal += activity.AmountToSeller + if activity.Time > lastActivityTime { + if activity.Buyer == account.Address { + account.Tx.WithdrawalTotal += activity.AmountToSeller + } else { + account.Tx.DepositTotal += activity.AmountToSeller + } + updateLastActivityTime(activity.Time) } - updateLastActivityTime(activity.Time) } for _, activity := range activities.UnstakeValidatorV1 { - account.Tx.WithdrawalTotal += activity.StakeAmount - updateLastActivityTime(activity.Time) + if activity.Time > lastActivityTime { + account.Tx.WithdrawalTotal += activity.StakeAmount + updateLastActivityTime(activity.Time) + } } account.LastUpdate = time.Unix(lastActivityTime, 0) @@ -791,8 +812,7 @@ func (e *Exporter) collectHotspotRewardsMetrics(wg *sync.WaitGroup, ch chan<- pr } } - // add 1 second to the last update time to avoid querying the same activity twice - minTime := hotspot.LastUpdate.Add(time.Second * 1) + minTime := hotspot.LastUpdate // Explicitly set max_time, to avoid issues with server-side caching maxTime := time.Now() activities, err := heliumapi.GetHotspotActivity(hotspot.Address, activityTypes, &minTime, &maxTime) @@ -802,16 +822,20 @@ func (e *Exporter) collectHotspotRewardsMetrics(wg *sync.WaitGroup, ch chan<- pr } for _, activity := range activities.RewardsV1 { - for _, reward := range activity.Rewards { - hotspot.RewardsTotal[reward.Type] += reward.Amount + if activity.Time > lastActivityTime { + for _, reward := range activity.Rewards { + hotspot.RewardsTotal[reward.Type] += reward.Amount + } + updateLastActivityTime(activity.Time) } - updateLastActivityTime(activity.Time) } for _, activity := range activities.RewardsV2 { - for _, reward := range activity.Rewards { - hotspot.RewardsTotal[reward.Type] += reward.Amount + if activity.Time > lastActivityTime { + for _, reward := range activity.Rewards { + hotspot.RewardsTotal[reward.Type] += reward.Amount + } + updateLastActivityTime(activity.Time) } - updateLastActivityTime(activity.Time) } hotspot.LastUpdate = time.Unix(lastActivityTime, 0) diff --git a/heliumapi/query.go b/heliumapi/query.go index 42f6f08..8ac9a0e 100644 --- a/heliumapi/query.go +++ b/heliumapi/query.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "log" "net/http" "time" @@ -124,7 +125,7 @@ func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, e defer resp.Body.Close() // read the response body and add it to the result array - // log.Printf("querying %v", req.URL.String()) + log.Printf("querying %v", req.URL.String()) body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read response body of %v: %v", req.URL.String(), err)