Skip to content

Commit

Permalink
Balance recalculations based on balanceForXX balances without any cli…
Browse files Browse the repository at this point in the history
…ckhouse usage. (#54)
  • Loading branch information
ice-myles authored Dec 13, 2023
1 parent 7f0f54c commit d1757de
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 721 deletions.
22 changes: 0 additions & 22 deletions bookkeeper/storage/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,6 @@ type (
Insert(ctx context.Context, columns *Columns, input InsertMetadata, usrs []*model.User) error
SelectBalanceHistory(ctx context.Context, id int64, createdAts []stdlibtime.Time) ([]*BalanceHistory, error)
SelectTotalCoins(ctx context.Context, createdAts []stdlibtime.Time) ([]*TotalCoins, error)
GetAdjustUserInformation(ctx context.Context, userIDs []string, limit, offset int64) ([]*AdjustUserInfo, error)
}
AdjustUserInfo struct {
MiningSessionSoloStartedAt *time.Time
MiningSessionSoloEndedAt *time.Time
MiningSessionSoloLastStartedAt *time.Time
MiningSessionSoloPreviouslyEndedAt *time.Time
CreatedAt *time.Time
ResurrectSoloUsedAt *time.Time
UserID string
ID int64
SlashingRateSolo float64
SlashingRateT1 float64
SlashingRateT2 float64
BalanceSolo float64
BalanceT0 float64
BalanceT1Pending float64
BalanceT1PendingApplied float64
BalanceT2Pending float64
BalanceT2PendingApplied float64
PrestakingAllocation uint16
PrestakingBonus uint16
}
BalanceHistory struct {
CreatedAt *time.Time
Expand Down
130 changes: 0 additions & 130 deletions bookkeeper/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/ClickHouse/ch-go/chpool"
"github.com/ClickHouse/ch-go/proto"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/ice-blockchain/freezer/model"
Expand Down Expand Up @@ -441,135 +440,6 @@ func (db *db) SelectBalanceHistory(ctx context.Context, id int64, createdAts []s
return res, nil
}

func (db *db) GetAdjustUserInformation(ctx context.Context, userIDs []string, limit, offset int64) ([]*AdjustUserInfo, error) {
const (
maxIDCount = 25_000
)
var (
res = make([]*AdjustUserInfo, 0, len(userIDs))
counter = 0
)
var userIDArray []string
for _, id := range userIDs {
userIDArray = append(userIDArray, id)
counter++
if counter >= maxIDCount { // Hack not to have 'Max query size exceeded' error.
result, err := db.getAdjustUserInformation(ctx, userIDArray, limit, offset)
if err != nil {
return nil, errors.Wrapf(err, "can't get adjust user information for userIDs:%#v", userIDArray)
}
res = append(res, result...)
userIDArray = userIDArray[:0]
counter = 0

continue
}
}
if len(userIDArray) > 0 {
result, err := db.getAdjustUserInformation(ctx, userIDArray, limit, offset)
if err != nil {
return nil, errors.Wrapf(err, "can't get adjust user information for userIDs:%#v", userIDArray)
}
res = append(res, result...)
}

return res, nil
}

func (db *db) getAdjustUserInformation(ctx context.Context, userIDArray []string, limit, offset int64) ([]*AdjustUserInfo, error) {
var (
id = make(proto.ColInt64, 0, len(userIDArray))
userID = &proto.ColStr{Buf: make([]byte, 0, 40*len(userIDArray)), Pos: make([]proto.Position, 0, len(userIDArray))}
miningSessionSoloStartedAt = proto.ColDateTime64{Data: make([]proto.DateTime64, 0, len(userIDArray)), Location: stdlibtime.UTC}
miningSessionSoloEndedAt = proto.ColDateTime64{Data: make([]proto.DateTime64, 0, len(userIDArray)), Location: stdlibtime.UTC}
miningSessionSoloPreviouslyEndedAt = proto.ColDateTime64{Data: make([]proto.DateTime64, 0, len(userIDArray)), Location: stdlibtime.UTC}
slashingRateSolo = make(proto.ColFloat64, 0, len(userIDArray))
createdAt = proto.ColDateTime{Data: make([]proto.DateTime, 0), Location: stdlibtime.UTC}
resurrectSoloUsedAt = proto.ColDateTime64{Data: make([]proto.DateTime64, 0, len(userIDArray)), Location: stdlibtime.UTC}
balanceSolo = make(proto.ColFloat64, 0, len(userIDArray))
balanceT1Pending = make(proto.ColFloat64, 0, len(userIDArray))
balanceT1PendingApplied = make(proto.ColFloat64, 0, len(userIDArray))
balanceT2Pending = make(proto.ColFloat64, 0, len(userIDArray))
balanceT2PendingApplied = make(proto.ColFloat64, 0, len(userIDArray))
res = make([]*AdjustUserInfo, 0, len(userIDArray))
)
if err := db.pools[atomic.AddUint64(&db.currentIndex, 1)%uint64(len(db.pools))].Do(ctx, ch.Query{
Body: fmt.Sprintf(`SELECT id,
user_id,
mining_session_solo_started_at,
mining_session_solo_ended_at,
mining_session_solo_previously_ended_at,
slashing_rate_solo,
created_at,
resurrect_solo_used_at,
balance_solo,
balance_t1_pending,
balance_t1_pending_applied,
balance_t2_pending,
balance_t2_pending_applied
FROM %[1]v
WHERE id IN [%[2]v]
ORDER BY id ASC, created_at ASC
LIMIT %[3]v, %[4]v
`, tableName, strings.Join(userIDArray, ","), offset, limit),
Result: append(make(proto.Results, 0, 13),
proto.ResultColumn{Name: "id", Data: &id},
proto.ResultColumn{Name: "user_id", Data: userID},
proto.ResultColumn{Name: "mining_session_solo_started_at", Data: &miningSessionSoloStartedAt},
proto.ResultColumn{Name: "mining_session_solo_ended_at", Data: &miningSessionSoloEndedAt},
proto.ResultColumn{Name: "mining_session_solo_previously_ended_at", Data: &miningSessionSoloPreviouslyEndedAt},
proto.ResultColumn{Name: "slashing_rate_solo", Data: &slashingRateSolo},
proto.ResultColumn{Name: "created_at", Data: &createdAt},
proto.ResultColumn{Name: "resurrect_solo_used_at", Data: &resurrectSoloUsedAt},
proto.ResultColumn{Name: "balance_solo", Data: &balanceSolo},
proto.ResultColumn{Name: "balance_t1_pending", Data: &balanceT1Pending},
proto.ResultColumn{Name: "balance_t1_pending_applied", Data: &balanceT1PendingApplied},
proto.ResultColumn{Name: "balance_t2_pending", Data: &balanceT2Pending},
proto.ResultColumn{Name: "balance_t2_pending_applied", Data: &balanceT2PendingApplied},
),
OnResult: func(_ context.Context, block proto.Block) error {
for ix := 0; ix < block.Rows; ix++ {
res = append(res, &AdjustUserInfo{
ID: (&id).Row(ix),
UserID: userID.Row(ix),
MiningSessionSoloStartedAt: time.New((&miningSessionSoloStartedAt).Row(ix)),
MiningSessionSoloEndedAt: time.New((&miningSessionSoloEndedAt).Row(ix)),
MiningSessionSoloPreviouslyEndedAt: time.New((&miningSessionSoloPreviouslyEndedAt).Row(ix)),
SlashingRateSolo: (&slashingRateSolo).Row(ix),
CreatedAt: time.New((&createdAt).Row(ix)),
ResurrectSoloUsedAt: time.New((&resurrectSoloUsedAt).Row(ix)),
BalanceSolo: (&balanceSolo).Row(ix),
BalanceT1Pending: (&balanceT1Pending).Row(ix),
BalanceT1PendingApplied: (&balanceT1PendingApplied).Row(ix),
BalanceT2Pending: (&balanceT2Pending).Row(ix),
BalanceT2PendingApplied: (&balanceT2PendingApplied).Row(ix),
})
}
(&id).Reset()
userID.Reset()
(&miningSessionSoloStartedAt).Reset()
(&miningSessionSoloEndedAt).Reset()
(&miningSessionSoloPreviouslyEndedAt).Reset()
(&slashingRateSolo).Reset()
(&createdAt).Reset()
(&resurrectSoloUsedAt).Reset()
(&balanceSolo).Reset()
(&balanceT1Pending).Reset()
(&balanceT1PendingApplied).Reset()
(&balanceT2Pending).Reset()
(&balanceT2PendingApplied).Reset()

return nil
},
Secret: "",
InitialUser: "",
}); err != nil {
return nil, err
}

return res, nil
}

func (db *db) SelectTotalCoins(ctx context.Context, createdAts []stdlibtime.Time) ([]*TotalCoins, error) {
var (
createdAt = proto.ColDateTime{Data: make([]proto.DateTime, 0, len(createdAts)), Location: stdlibtime.UTC}
Expand Down
10 changes: 10 additions & 0 deletions miner/.testdata/DDL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ CREATE TABLE IF NOT EXISTS balance_recalculation_metrics (
worker BIGINT NOT NULL PRIMARY KEY
) WITH (fillfactor = 70);
--************************************************************************************************************************************
-- balance_recalculation_dry_run
DROP TABLE IF EXISTS balance_recalculation_dry_run;
CREATE TABLE IF NOT EXISTS balance_recalculation_dry_run (
diff_t1_balance DOUBLE PRECISION NOT NULL,
diff_t2_balance DOUBLE PRECISION NOT NULL,
diff_t1_active_counts DOUBLE PRECISION NOT NULL,
diff_t2_active_counts DOUBLE PRECISION NOT NULL,
user_id text PRIMARY KEY
) WITH (fillfactor = 70);
--************************************************************************************************************************************
151 changes: 0 additions & 151 deletions miner/adoption_range_test.go

This file was deleted.

11 changes: 8 additions & 3 deletions 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 Expand Up @@ -122,8 +122,6 @@ type (
model.SlashingRateT2Field
model.FirstRecalculatedBalanceT1Field
model.FirstRecalculatedBalanceT2Field
model.FirstRecalculatedSlashingRateT1Field
model.FirstRecalculatedSlashingRateT2Field
model.DeserializedBackupUsersKey
model.ActiveT1ReferralsField
model.ActiveT2ReferralsField
Expand All @@ -141,6 +139,13 @@ type (
model.DeserializedUsersKey
}

recalculateReferral struct {
model.BalanceForT0Field
model.BalanceForTMinus1Field
model.UserIDField
model.DeserializedUsersKey
}

referralCountGuardUpdatedUser struct {
model.ReferralsCountChangeGuardUpdatedAtField
model.DeserializedUsersKey
Expand Down
Loading

0 comments on commit d1757de

Please sign in to comment.