Skip to content

Commit

Permalink
fix significant balance increase due to pending unapplied bonus (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-cronus authored Aug 2, 2024
1 parent b2368cd commit 1c28f39
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 14 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/ethereum/go-ethereum v1.14.7
github.com/goccy/go-json v0.10.3
github.com/hashicorp/go-multierror v1.1.1
github.com/ice-blockchain/eskimo v1.384.0
github.com/ice-blockchain/eskimo v1.388.0
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb
github.com/ice-blockchain/wintr v1.150.0
github.com/imroc/req/v3 v3.43.7
Expand Down Expand Up @@ -161,7 +161,7 @@ require (
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/quic-go v0.45.1 // indirect
github.com/quic-go/quic-go v0.45.2 // indirect
github.com/refraction-networking/utls v1.6.7 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
Expand Down Expand Up @@ -211,7 +211,7 @@ require (
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/api v0.189.0 // indirect
google.golang.org/api v0.190.0 // indirect
google.golang.org/appengine/v2 v2.0.6 // indirect
google.golang.org/genproto v0.0.0-20240730163845-b1a4ccb954bf // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240730163845-b1a4ccb954bf // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/huin/goupnp v1.3.0 h1:UvLUlWDNpoUdYzb2TCn+MuTWtcjXKSza2n6CBdQ0xXc=
github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ice-blockchain/eskimo v1.384.0 h1:iI4b6BQ2PJUSZ0GS+7RHTG2r0etdEOs+rLPxSflcffI=
github.com/ice-blockchain/eskimo v1.384.0/go.mod h1:1HmGav5jzNe3FVPxLMMjTXEwEWoURNsQ+FnMRHARbRg=
github.com/ice-blockchain/eskimo v1.388.0 h1:ao0agIR9TO+jtNvkufx55CDfivlEnoHBdtaKG75n96c=
github.com/ice-blockchain/eskimo v1.388.0/go.mod h1:xkpZN+kXoksxhLGhrkPwkWOROXj9qLKV/E0nLmBeQvI=
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb h1:8TnFP3mc7O+tc44kv2e0/TpZKnEVUaKH+UstwfBwRkk=
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb/go.mod h1:ZsQU7i3mxhgBBu43Oev7WPFbIjP4TniN/b1UPNGbrq8=
github.com/ice-blockchain/wintr v1.150.0 h1:ZzQrPKPVFYRpD3xgQ7/Cmrjz8eeu6UMQ9TvE+brVK0U=
Expand Down Expand Up @@ -439,8 +439,8 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A=
github.com/quic-go/quic-go v0.45.1 h1:tPfeYCk+uZHjmDRwHHQmvHRYL2t44ROTujLeFVBmjCA=
github.com/quic-go/quic-go v0.45.1/go.mod h1:1dLehS7TIR64+vxGR70GDcatWTOtMX2PUtnKsjbTurI=
github.com/quic-go/quic-go v0.45.2 h1:DfqBmqjb4ExSdxRIb/+qXhPC+7k6+DUNZha4oeiC9fY=
github.com/quic-go/quic-go v0.45.2/go.mod h1:1dLehS7TIR64+vxGR70GDcatWTOtMX2PUtnKsjbTurI=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
Expand Down Expand Up @@ -678,8 +678,8 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
google.golang.org/api v0.189.0 h1:equMo30LypAkdkLMBqfeIqtyAnlyig1JSZArl4XPwdI=
google.golang.org/api v0.189.0/go.mod h1:FLWGJKb0hb+pU2j+rJqwbnsF+ym+fQs73rbJ+KAUgy8=
google.golang.org/api v0.190.0 h1:ASM+IhLY1zljNdLu19W1jTmU6A+gMk6M46Wlur61s+Q=
google.golang.org/api v0.190.0/go.mod h1:QIr6I9iedBLnfqoD6L6Vze1UvS5Hzj5r2aUBOaZnLHo=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine/v2 v2.0.6 h1:LvPZLGuchSBslPBp+LAhihBeGSiRh1myRoYK4NtuBIw=
Expand Down
1 change: 1 addition & 0 deletions miner/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type (
model.VerifiedT1ReferralsField
model.ActiveT1ReferralsField
model.ActiveT2ReferralsField
model.TotalT1ReferralsField
}

UpdatedUser struct { // This is public only because we have to embed it, and it has to be if so.
Expand Down
7 changes: 3 additions & 4 deletions miner/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,13 @@ func mine(now *time.Time, usr *user, t0Ref, tMinus1Ref *referral) (updatedUser *
updatedUser.SlashingRateForTMinus1 = updatedUser.BalanceForTMinus1 / float64(cfg.SlashingDaysCount) / miningSessionRatio
}
}

if updatedUser.BalanceT1WelcomeBonusPendingApplied < float64(maxT1Referrals)*tokenomics.WelcomeBonusV2Amount {
if updatedUser.BalanceT1WelcomeBonusPendingApplied < float64(min(maxT1Referrals, uint64(usr.TotalT1Referrals)))*tokenomics.WelcomeBonusV2Amount {
if unAppliedT1WelcomeBonusPending := updatedUser.BalanceT1WelcomeBonusPending - updatedUser.BalanceT1WelcomeBonusPendingApplied; unAppliedT1WelcomeBonusPending == 0 {
updatedUser.BalanceT1WelcomeBonusPending = 0
updatedUser.BalanceT1WelcomeBonusPendingApplied = 0
} else {
unAppliedT1Pending += min(unAppliedT1WelcomeBonusPending, float64(maxT1Referrals)*tokenomics.WelcomeBonusV2Amount-updatedUser.BalanceT1WelcomeBonusPendingApplied)
updatedUser.BalanceT1WelcomeBonusPendingApplied = min(updatedUser.BalanceT1WelcomeBonusPending, float64(maxT1Referrals)*tokenomics.WelcomeBonusV2Amount)
unAppliedT1Pending += min(unAppliedT1WelcomeBonusPending, float64(min(maxT1Referrals, uint64(usr.TotalT1Referrals)))*tokenomics.WelcomeBonusV2Amount-updatedUser.BalanceT1WelcomeBonusPendingApplied)
updatedUser.BalanceT1WelcomeBonusPendingApplied = min(updatedUser.BalanceT1WelcomeBonusPending, float64(min(maxT1Referrals, uint64(usr.TotalT1Referrals)))*tokenomics.WelcomeBonusV2Amount)
}
} else {
updatedUser.BalanceT1WelcomeBonusPending = 0
Expand Down
6 changes: 6 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ type (
ActiveT2ReferralsField struct {
ActiveT2Referrals int32 `redis:"active_t2_referrals,omitempty"`
}
TotalT1ReferralsField struct {
TotalT1Referrals int32 `json:"t1ReferralCount,omitempty" redis:"total_t1_referrals,omitempty"`
}
TotalT2ReferralsField struct {
TotalT2Referrals int32 `json:"t2ReferralCount,omitempty" redis:"total_t2_referrals,omitempty"`
}
NewsSeenField struct {
NewsSeen uint16 `redis:"news_seen"`
}
Expand Down
8 changes: 7 additions & 1 deletion tokenomics/kyc.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ func (r *repository) overrideKYCStateWithEskimoKYCState(ctx context.Context, use
model.VerifiedT1ReferralsField
model.DeserializedUsersKey
model.HideRankingField
model.TotalT1ReferralsField
model.BalanceT1WelcomeBonusPendingField
KycFaceAvailable bool `json:"kycFaceAvailable" redis:"-"`
}
if err3 := json.Unmarshal(data, &usr); err3 != nil {
Expand All @@ -540,7 +542,11 @@ func (r *repository) overrideKYCStateWithEskimoKYCState(ctx context.Context, use
usr.HideRanking = buildHideRanking(usr.HiddenProfileElements)
usr.CreatedAt = time.New(usr.AccountCreatedAt)
usr.ProfilePictureName = r.pictureClient.StripDownloadURL(usr.ProfilePictureName)

// We cant reset it to proper value in miner cuz we have active users who has zero total refs in state
// until they start next mining, we'll lose pending value in such case.
if state.BalanceT1WelcomeBonusPending > float64(usr.TotalT1Referrals)*WelcomeBonusV2Amount {
usr.BalanceT1WelcomeBonusPending = float64(usr.TotalT1Referrals) * WelcomeBonusV2Amount
}
return usr.KycFaceAvailable, multierror.Append(
errors.Wrapf(r.updateUsernameKeywords(ctx, state.ID, state.Username, usr.Username), "failed to updateUsernameKeywords for oldUser:%#v, user:%#v", state, usr), //nolint:lll // .
errors.Wrapf(r.updateReferredBy(ctx, state.ID, &state.IDT0, &state.IDTMinus1, state.UserID, usr.ReferredBy, state.BalanceForTMinus1), "failed to updateReferredBy for user:%#v", usr), //nolint:lll // .
Expand Down
1 change: 1 addition & 0 deletions tokenomics/mining_sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type (
model.IDTMinus1Field
model.PreStakingAllocationField
model.PreStakingBonusField
model.BalanceT1WelcomeBonusPendingField
}
)

Expand Down
3 changes: 3 additions & 0 deletions tokenomics/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ func (r *repository) updateReferredBy(ctx context.Context, id int64, oldIDT0, ol
if innerErr := pipeliner.HIncrBy(ctx, model.SerializedUsersKey(localIDT0), "balance_t1_welcome_bonus_pending", WelcomeBonusV2Amount).Err(); innerErr != nil {
return innerErr
}
if innerErr := pipeliner.HIncrBy(ctx, model.SerializedUsersKey(localIDT0), "total_t1_referrals", 1).Err(); innerErr != nil {
return innerErr
}

return pipeliner.HSet(ctx, newPartialState.Key(), storage.SerializeValue(newPartialState)...).Err()
})
Expand Down

0 comments on commit 1c28f39

Please sign in to comment.