1
0
Fork 0

fix reward value being added to counter multiple times
continuous-integration/drone/push Build was killed Details

An update to the heliumapi made minTime fuzzy, causing
an activity to be returned even if it occured before the
specified minTime.
This commit is contained in:
Massaki Archambault 2021-12-24 12:45:17 -05:00
parent dec12e1780
commit 9501dc9dc0
2 changed files with 77 additions and 52 deletions

View File

@ -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
minTime := account.LastUpdate.Add(time.Second * 1)
// Explicitly set max_time, to avoid issues with server-side caching // Explicitly set max_time, to avoid issues with server-side caching
maxTime := time.Now() maxTime := time.Now()
activities, err := heliumapi.GetActivityForAccount(account.Address, activityTypes, &minTime, &maxTime) activities, err := heliumapi.GetActivityForAccount(account.Address, activityTypes, &minTime, &maxTime)
@ -541,18 +540,25 @@ 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 // impl based on https://github.com/helium/hotspot-app/blob/918563fba84d1abf4554a43a4d42bb838d017bd3/src/features/wallet/root/useActivityItem.tsx#L336
for _, activity := range activities.AddGatewayV1 { for _, activity := range activities.AddGatewayV1 {
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.StakingFee account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
for _, activity := range activities.AssertLocationV1 { for _, activity := range activities.AssertLocationV1 {
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.StakingFee account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
for _, activity := range activities.AssertLocationV2 { for _, activity := range activities.AssertLocationV2 {
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.StakingFee account.Tx.WithdrawalTotal += activity.StakingFee
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
for _, activity := range activities.PaymentV1 { for _, activity := range activities.PaymentV1 {
if activity.Time > lastActivityTime {
if activity.Payer == account.Address { if activity.Payer == account.Address {
account.Tx.WithdrawalTotal += activity.Amount account.Tx.WithdrawalTotal += activity.Amount
} else { } else {
@ -560,7 +566,9 @@ func (e *Exporter) collectAccountTransactionsMetrics(wg *sync.WaitGroup, ch chan
} }
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
for _, activity := range activities.PaymentV2 { for _, activity := range activities.PaymentV2 {
if activity.Time > lastActivityTime {
if activity.Payer == account.Address { if activity.Payer == account.Address {
paymentTotal := 0 paymentTotal := 0
for _, payment := range activity.Payments { for _, payment := range activity.Payments {
@ -576,29 +584,39 @@ func (e *Exporter) collectAccountTransactionsMetrics(wg *sync.WaitGroup, ch chan
} }
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
for _, activity := range activities.RewardsV1 { for _, activity := range activities.RewardsV1 {
if activity.Time > lastActivityTime {
for _, reward := range activity.Rewards { for _, reward := range activity.Rewards {
account.RewardsTotal[reward.Type] += reward.Amount account.RewardsTotal[reward.Type] += reward.Amount
account.Tx.DepositTotal += reward.Amount account.Tx.DepositTotal += reward.Amount
} }
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
for _, activity := range activities.RewardsV2 { for _, activity := range activities.RewardsV2 {
if activity.Time > lastActivityTime {
for _, reward := range activity.Rewards { for _, reward := range activity.Rewards {
account.RewardsTotal[reward.Type] += reward.Amount account.RewardsTotal[reward.Type] += reward.Amount
account.Tx.DepositTotal += reward.Amount account.Tx.DepositTotal += reward.Amount
} }
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
for _, activity := range activities.StakeValidatorV1 { for _, activity := range activities.StakeValidatorV1 {
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.Stake account.Tx.WithdrawalTotal += activity.Stake
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
for _, activity := range activities.TokenBurnV1 { for _, activity := range activities.TokenBurnV1 {
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.Amount account.Tx.WithdrawalTotal += activity.Amount
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
for _, activity := range activities.TransferHotspotV1 { for _, activity := range activities.TransferHotspotV1 {
if activity.Time > lastActivityTime {
if activity.Buyer == account.Address { if activity.Buyer == account.Address {
account.Tx.WithdrawalTotal += activity.AmountToSeller account.Tx.WithdrawalTotal += activity.AmountToSeller
} else { } else {
@ -606,10 +624,13 @@ func (e *Exporter) collectAccountTransactionsMetrics(wg *sync.WaitGroup, ch chan
} }
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
for _, activity := range activities.UnstakeValidatorV1 { for _, activity := range activities.UnstakeValidatorV1 {
if activity.Time > lastActivityTime {
account.Tx.WithdrawalTotal += activity.StakeAmount account.Tx.WithdrawalTotal += activity.StakeAmount
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
account.LastUpdate = time.Unix(lastActivityTime, 0) account.LastUpdate = time.Unix(lastActivityTime, 0)
for rType, rTotal := range account.RewardsTotal { for rType, rTotal := range account.RewardsTotal {
@ -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
minTime := hotspot.LastUpdate.Add(time.Second * 1)
// Explicitly set max_time, to avoid issues with server-side caching // Explicitly set max_time, to avoid issues with server-side caching
maxTime := time.Now() maxTime := time.Now()
activities, err := heliumapi.GetHotspotActivity(hotspot.Address, activityTypes, &minTime, &maxTime) activities, err := heliumapi.GetHotspotActivity(hotspot.Address, activityTypes, &minTime, &maxTime)
@ -802,17 +822,21 @@ func (e *Exporter) collectHotspotRewardsMetrics(wg *sync.WaitGroup, ch chan<- pr
} }
for _, activity := range activities.RewardsV1 { for _, activity := range activities.RewardsV1 {
if activity.Time > lastActivityTime {
for _, reward := range activity.Rewards { for _, reward := range activity.Rewards {
hotspot.RewardsTotal[reward.Type] += reward.Amount hotspot.RewardsTotal[reward.Type] += reward.Amount
} }
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
for _, activity := range activities.RewardsV2 { for _, activity := range activities.RewardsV2 {
if activity.Time > lastActivityTime {
for _, reward := range activity.Rewards { for _, reward := range activity.Rewards {
hotspot.RewardsTotal[reward.Type] += reward.Amount hotspot.RewardsTotal[reward.Type] += reward.Amount
} }
updateLastActivityTime(activity.Time) updateLastActivityTime(activity.Time)
} }
}
hotspot.LastUpdate = time.Unix(lastActivityTime, 0) hotspot.LastUpdate = time.Unix(lastActivityTime, 0)
for rType, rTotal := range hotspot.RewardsTotal { for rType, rTotal := range hotspot.RewardsTotal {

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"time" "time"
@ -124,7 +125,7 @@ func getHeliumApiWithCursor(path string, params *map[string]string) ([][]byte, e
defer resp.Body.Close() defer resp.Body.Close()
// read the response body and add it to the result array // 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) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read response body of %v: %v", req.URL.String(), err) return nil, fmt.Errorf("failed to read response body of %v: %v", req.URL.String(), err)