Skip to content

Commit

Permalink
Balances recalculation optimizing (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles authored Oct 30, 2023
1 parent 77b2f69 commit 3768872
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 211 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ require (
google.golang.org/api v0.148.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/appengine/v2 v2.0.5 // indirect
google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -810,12 +810,12 @@ google.golang.org/genproto v0.0.0-20201210142538-e3217bee35cc/go.mod h1:FWY/as6D
google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210108203827-ffc7fda8c3d7/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210226172003-ab064af71705/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b h1:+YaDE2r2OG8t/z5qmsh7Y+XXwCbvadxxZ0YY6mTdrVA=
google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b/go.mod h1:CgAqfJo+Xmu0GwA0411Ht3OU3OntXwsGmrmjI8ioGXI=
google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b h1:CIC2YMXmIhYw6evmhPxBKJ4fmLbOFtXQN/GV3XOZR8k=
google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b/go.mod h1:IBQ646DjkDkvUIsVq/cc03FUFQ9wbZu7yE396YcL870=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b h1:ZlWIi1wSK56/8hn4QcBp/j9M7Gt3U/3hZw3mC7vDICo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b/go.mod h1:swOH3j0KzcDDgGUWr+SNpyTen5YrXjS3eyPzFYKc6lc=
google.golang.org/genproto v0.0.0-20231030173426-d783a09b4405 h1:I6WNifs6pF9tNdSob2W24JtyxIYjzFB9qDlpUC76q+U=
google.golang.org/genproto v0.0.0-20231030173426-d783a09b4405/go.mod h1:3WDQMjmJk36UQhjQ89emUzb1mdaHcPeeAh4SCBKznB4=
google.golang.org/genproto/googleapis/api v0.0.0-20231030173426-d783a09b4405 h1:HJMDndgxest5n2y77fnErkM62iUsptE/H8p0dC2Huo4=
google.golang.org/genproto/googleapis/api v0.0.0-20231030173426-d783a09b4405/go.mod h1:oT32Z4o8Zv2xPQTg0pbVaPr0MPOH6f14RgXt7zfIpwg=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 h1:AB/lmRny7e2pLhFEYIbl5qkDAUt2h0ZRO4wGPhZf+ik=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405/go.mod h1:67X1fPuzjcrkymZzZV1vvkFeTn2Rvc6lYF9MYFGCcwE=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
Expand Down
24 changes: 18 additions & 6 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ func (m *miner) mine(ctx context.Context, workerNumber int64) {
recalculatedTiersBalancesUsers = make(map[int64]*user, batchSize)
historyColumns, historyInsertMetadata = dwh.InsertDDL(int(batchSize))
shouldSynchronizeBalanceFunc = func(batchNumberArg uint64) bool { return false }
recalculationHistory *historyData
allAdoptions []*tokenomics.Adoption[float64]
)
resetVars := func(success bool) {
if success && len(userKeys) == int(batchSize) && len(userResults) == 0 {
Expand Down Expand Up @@ -212,6 +214,7 @@ func (m *miner) mine(ctx context.Context, workerNumber int64) {
histories = histories[:0]
userGlobalRanks = userGlobalRanks[:0]
referralsThatStoppedMining = referralsThatStoppedMining[:0]
allAdoptions = allAdoptions[:0]
for k := range t0Referrals {
delete(t0Referrals, k)
}
Expand Down Expand Up @@ -270,9 +273,10 @@ func (m *miner) mine(ctx context.Context, workerNumber int64) {

continue
}
balanceBackupMode, err := mustGetBalancesBackupMode(ctx, m.db)
balanceBackupMode, err := mustGetBalancesBackupMode(reqCtx, m.db)
if err != nil {
log.Error(errors.Wrapf(err, "[miner] failed to get backup flag for batchNumber:%v,workerNumber:%v", batchNumber, workerNumber))
reqCancel()

continue
}
Expand Down Expand Up @@ -336,11 +340,18 @@ func (m *miner) mine(ctx context.Context, workerNumber int64) {
}
}
if !balanceBackupMode {
var err error
recalculatedTiersBalancesUsers, err = m.recalculateTiersBalances(ctx, userResults, tMinus1Referrals, t0Referrals)
reqCtx, reqCancel = context.WithTimeout(context.Background(), requestDeadline)
recalculationHistory, err = m.gatherHistoryAndReferralsInformation(ctx, userResults)
if err != nil {
log.Error(errors.New("tiers diff balances error"), err)
}
reqCancel()
reqCtx, reqCancel = context.WithTimeout(context.Background(), requestDeadline)
allAdoptions, err = tokenomics.GetAllAdoptions[float64](ctx, m.db)
if err != nil {
log.Error(errors.New("can't get all adoptions"), err)
}
reqCancel()
}
shouldSynchronizeBalance := shouldSynchronizeBalanceFunc(uint64(batchNumber))
for _, usr := range userResults {
Expand Down Expand Up @@ -373,9 +384,10 @@ func (m *miner) mine(ctx context.Context, workerNumber int64) {
})
}
} else {
if recalculatedUsr, ok := recalculatedTiersBalancesUsers[usr.ID]; ok {
diffT1ActiveValue := recalculatedUsr.ActiveT1Referrals - usr.ActiveT1Referrals
diffT2ActiveValue := recalculatedUsr.ActiveT2Referrals - usr.ActiveT2Referrals
if recalculatedUsr := m.recalculateUser(usr, allAdoptions, recalculationHistory); recalculatedUsr != nil {
diffT1ActiveValue := recalculationHistory.T1ActiveCounts[usr.ID] - usr.ActiveT1Referrals
diffT2ActiveValue := recalculationHistory.T2ActiveCounts[usr.ID] - usr.ActiveT2Referrals

if diffT1ActiveValue < 0 && diffT1ActiveValue*-1 > usr.ActiveT1Referrals {
diffT1ActiveValue = -usr.ActiveT1Referrals
}
Expand Down
Loading

0 comments on commit 3768872

Please sign in to comment.