Skip to content

Commit

Permalink
Enabled balance change. Fixed issues related to user information upda…
Browse files Browse the repository at this point in the history
…te due to clickhouse -> balanceForXX recalculations change. (#57)
  • Loading branch information
ice-myles authored Dec 15, 2023
1 parent 94e2402 commit 5dfad04
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 75 deletions.
2 changes: 1 addition & 1 deletion miner/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
applicationYamlKey = "miner"
parentApplicationYamlKey = "tokenomics"
requestDeadline = 30 * stdlibtime.Second
balanceBugFixEnabled = false
balanceBugFixEnabled = true
)

// .
Expand Down
74 changes: 25 additions & 49 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,6 @@ func (m *miner) mine(ctx context.Context, workerNumber int64) {

continue
}
_, err := m.db.Del(reqCtx, userBackupKeys...).Result()
if err != nil {
log.Error(errors.Wrap(err, fmt.Sprintf("can't remove backup keys:%#v", userBackupKeys)))
}
reqCancel()
if len(userKeys) > 0 {
go m.telemetry.collectElapsed(2, *before.Time)
Expand Down Expand Up @@ -394,7 +390,7 @@ func (m *miner) mine(ctx context.Context, workerNumber int64) {
backupedUsr, backupExists := backupedUsers[usr.ID]
if balanceBugFixEnabled {
if balanceBackupMode {
if backupExists {
if backupExists && backupedUsr.BalancesBackupUsedAt.IsNil() {
diffT1ActiveValue := backupedUsr.ActiveT1Referrals - usr.ActiveT1Referrals
diffT2ActiveValue := backupedUsr.ActiveT2Referrals - usr.ActiveT2Referrals
if diffT1ActiveValue < 0 && diffT1ActiveValue*-1 > usr.ActiveT1Referrals {
Expand All @@ -403,19 +399,19 @@ func (m *miner) mine(ctx context.Context, workerNumber int64) {
if diffT2ActiveValue < 0 && diffT2ActiveValue*-1 > usr.ActiveT2Referrals {
diffT2ActiveValue = -usr.ActiveT2Referrals
}
if false {
t1ReferralsToIncrementActiveValue[usr.ID] += diffT1ActiveValue
t2ReferralsToIncrementActiveValue[usr.ID] += diffT2ActiveValue

usr.BalanceT1 = backupedUsr.BalanceT1
usr.BalanceT2 = backupedUsr.BalanceT2
t1ReferralsToIncrementActiveValue[usr.ID] += diffT1ActiveValue
t2ReferralsToIncrementActiveValue[usr.ID] += diffT2ActiveValue

usr.SlashingRateT1 = backupedUsr.SlashingRateT1
usr.SlashingRateT2 = backupedUsr.SlashingRateT2
usr.BalanceT1 = backupedUsr.BalanceT1
usr.BalanceT2 = backupedUsr.BalanceT2
usr.SlashingRateT1 = backupedUsr.SlashingRateT1
usr.SlashingRateT2 = backupedUsr.SlashingRateT2
usr.ActiveT1Referrals = backupedUsr.ActiveT1Referrals
usr.ActiveT2Referrals = backupedUsr.ActiveT2Referrals

backupedUsr.BalancesBackupUsedAt = time.Now()
backupUsersUpdated = append(backupUsersUpdated, backupedUsr)
}
backupedUsr.BalancesBackupUsedAt = time.Now()
backupUsersUpdated = append(backupUsersUpdated, backupedUsr)
}
} else {
if !backupExists && recalculationInfo != nil {
Expand Down Expand Up @@ -452,19 +448,22 @@ func (m *miner) mine(ctx context.Context, workerNumber int64) {
if diffT2ActiveValue < 0 && diffT2ActiveValue*-1 > usr.ActiveT2Referrals {
diffT2ActiveValue = -usr.ActiveT2Referrals
}
if false {
t1ReferralsToIncrementActiveValue[usr.ID] += diffT1ActiveValue
t2ReferralsToIncrementActiveValue[usr.ID] += diffT2ActiveValue

usr.BalanceT1 = balanceT1
usr.BalanceT2 = balanceT2
}
t1ReferralsToIncrementActiveValue[usr.ID] += diffT1ActiveValue
t2ReferralsToIncrementActiveValue[usr.ID] += diffT2ActiveValue

metrics.AffectedUsers += 1
usr.BalanceT1 = balanceT1
usr.BalanceT2 = balanceT2
usr.SlashingRateT1 = 0
usr.SlashingRateT2 = 0
usr.ActiveT1Referrals = recalculationInfo.T1ActiveCounts[usr.UserID]
usr.ActiveT2Referrals = recalculationInfo.T2ActiveCounts[usr.UserID]

t1BalanceDiff := balanceT1 - oldBalanceT1
t2BalanceDiff := balanceT2 - oldBalanceT2

metrics.AffectedUsers += 1

if t1BalanceDiff >= 0 {
metrics.T1BalancePositive += t1BalanceDiff
} else {
Expand All @@ -485,29 +484,6 @@ func (m *miner) mine(ctx context.Context, workerNumber int64) {
} else {
metrics.T2ActiveCountsPositive += int64(diffT2ActiveValue)
}
if !backupExists {
metrics.AffectedUsers += 1
if t1BalanceDiff >= 0 {
metrics.T1BalancePositive += t1BalanceDiff
} else {
metrics.T1BalanceNegative += t1BalanceDiff
}
if t2BalanceDiff >= 0 {
metrics.T2BalancePositive += t2BalanceDiff
} else {
metrics.T2BalanceNegative += t2BalanceDiff
}
if diffT1ActiveValue < 0 {
metrics.T1ActiveCountsNegative += int64(diffT1ActiveValue)
} else {
metrics.T1ActiveCountsPositive += int64(diffT1ActiveValue)
}
if diffT2ActiveValue < 0 {
metrics.T2ActiveCountsNegative += int64(diffT2ActiveValue)
} else {
metrics.T2ActiveCountsPositive += int64(diffT2ActiveValue)
}
}

balanceRecalculationDryRunItems = append(balanceRecalculationDryRunItems, &balanceRecalculationDryRun{
T1BalanceDiff: t1BalanceDiff,
Expand Down Expand Up @@ -565,10 +541,10 @@ func (m *miner) mine(ctx context.Context, workerNumber int64) {
updatedUser.ExtraBonusDaysClaimNotAvailable = 0
updatedUser.ExtraBonusLastClaimAvailableAt = nil
}
if userStoppedMining := didUserStoppedMining(now, usr); userStoppedMining != nil {
referralsCountGuardOnlyUpdatedUsers = append(referralsCountGuardOnlyUpdatedUsers, userStoppedMining)
}
if true || balanceBackupMode || !backupExists {
if !balanceBugFixEnabled || balanceBackupMode {
if userStoppedMining := didUserStoppedMining(now, usr); userStoppedMining != nil {
referralsCountGuardOnlyUpdatedUsers = append(referralsCountGuardOnlyUpdatedUsers, userStoppedMining)
}
if userStoppedMining := didReferralJustStopMining(now, usr, t0Ref, tMinus1Ref); userStoppedMining != nil {
referralsThatStoppedMining = append(referralsThatStoppedMining, userStoppedMining)
}
Expand Down
25 changes: 0 additions & 25 deletions tokenomics/mining_sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,6 @@ func (s *miningSessionsTableSource) Process(ctx context.Context, msg *messagebro
).ErrorOrNil()
}

func mustGetBalancesBackupMode(ctx context.Context, db storage.DB) (result bool, err error) {
balancesBackupModeString, err := db.Get(ctx, "balances_backup_mode").Result()
if err != nil && errors.Is(err, redis.Nil) {
err = nil
}

return balancesBackupModeString == "true", err
}

//nolint:funlen,revive,gocognit // .
func (s *miningSessionsTableSource) incrementActiveReferralCountForT0AndTMinus1(ctx context.Context, ms *MiningSession) (err error) {
if ctx.Err() != nil || !ms.LastNaturalMiningStartedAt.Equal(*ms.StartedAt.Time) {
Expand Down Expand Up @@ -302,22 +293,6 @@ func (s *miningSessionsTableSource) incrementActiveReferralCountForT0AndTMinus1(
if err != nil {
return errors.Wrapf(err, "failed to getOrInitInternalID for userID:%v", *ms.UserID)
}
if false {
backupUsr, err := storage.Get[struct {
model.UserIDField
model.DeserializedBackupUsersKey
}](ctx, s.db, model.SerializedBackupUsersKey(id))
if err != nil {
return errors.Wrapf(err, "failed to get backupUser for id:%v, userID:%v", id, *ms.UserID)
}
balanceBackupMode, err := mustGetBalancesBackupMode(ctx, s.db)
if err != nil {
return errors.Wrapf(err, "failed to get backup flag for id:%v", id)
}
if !balanceBackupMode && backupUsr != nil {
return nil
}
}
referees, err := storage.Get[struct {
model.UserIDField
model.DeserializedUsersKey
Expand Down

0 comments on commit 5dfad04

Please sign in to comment.