Skip to content

Commit

Permalink
Work on comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles committed Oct 20, 2023
1 parent e8ce41f commit 1253d06
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 357 deletions.
2 changes: 1 addition & 1 deletion bookkeeper/storage/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type (
Ping(ctx context.Context) error
Insert(ctx context.Context, columns *Columns, input InsertMetadata, usrs []*model.User) error
SelectBalanceHistory(ctx context.Context, id int64, createdAts []stdlibtime.Time) ([]*BalanceHistory, error)
GetAdjustUserInformation(ctx context.Context, userIDs map[int64]struct{}) ([]*AdjustUserInfo, error)
GetAdjustUserInformation(ctx context.Context, userIDs map[int64]struct{}, limit, offset int64) ([]*AdjustUserInfo, error)
}
AdjustUserInfo struct {
MiningSessionSoloStartedAt *time.Time
Expand Down
9 changes: 7 additions & 2 deletions bookkeeper/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func (db *db) SelectBalanceHistory(ctx context.Context, id int64, createdAts []s
return res, nil
}

func (db *db) GetAdjustUserInformation(ctx context.Context, userIDs map[int64]struct{}) ([]*AdjustUserInfo, error) {
func (db *db) GetAdjustUserInformation(ctx context.Context, userIDs map[int64]struct{}, limit, offset int64) ([]*AdjustUserInfo, error) {
var (
id = make(proto.ColInt64, 0, len(userIDs))
miningSessionSoloStartedAt = proto.ColDateTime64{Data: make([]proto.DateTime64, 0, len(userIDs)), Location: stdlibtime.UTC}
Expand All @@ -463,6 +463,10 @@ func (db *db) GetAdjustUserInformation(ctx context.Context, userIDs map[int64]st
for key, _ := range userIDs {
userIDArray = append(userIDArray, fmt.Sprint(key))
}
var offsetStr string
if offset > 0 {
offsetStr = fmt.Sprintf(", %v", offset)
}
if err := db.pools[atomic.AddUint64(&db.currentIndex, 1)%uint64(len(db.pools))].Do(ctx, ch.Query{
Body: fmt.Sprintf(`SELECT id,
mining_session_solo_started_at,
Expand All @@ -479,7 +483,8 @@ func (db *db) GetAdjustUserInformation(ctx context.Context, userIDs map[int64]st
FROM %[1]v
WHERE id IN [%[2]v]
ORDER BY created_at ASC
`, tableName, strings.Join(userIDArray, ",")),
LIMIT %[3]v %[4]v
`, tableName, strings.Join(userIDArray, ","), limit, offsetStr),
Result: append(make(proto.Results, 0, 12),
proto.ResultColumn{Name: "id", Data: &id},
proto.ResultColumn{Name: "mining_session_solo_started_at", Data: &miningSessionSoloStartedAt},
Expand Down
32 changes: 0 additions & 32 deletions miner/DDL.sql

This file was deleted.

150 changes: 150 additions & 0 deletions miner/adoption_range_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// SPDX-License-Identifier: ice License 1.0

package miner

import (
"testing"
stdlibtime "time"

"github.com/ice-blockchain/freezer/tokenomics"
"github.com/ice-blockchain/wintr/time"
"github.com/stretchr/testify/assert"
)

func TestGetAdoptionsRange_1AdoptionPerRange(t *testing.T) {
var adoptions []*tokenomics.Adoption[float64]
adoptions = append(adoptions, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-58 * stdlibtime.Minute)),
BaseMiningRate: 16.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-55 * stdlibtime.Minute)),
BaseMiningRate: 8.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-30 * stdlibtime.Minute)),
BaseMiningRate: 4.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-10 * stdlibtime.Minute)),
BaseMiningRate: 2.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: nil,
BaseMiningRate: 1.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: nil,
BaseMiningRate: 0.5,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: nil,
BaseMiningRate: 0.25,
Milestone: 1,
TotalActiveUsers: 1,
})
startedAt := time.New(time.Now().Add(-1 * stdlibtime.Minute))
endedAt := time.Now()
ranges := splitByAdoptionTimeRanges(adoptions, startedAt, endedAt)
assert.Equal(t, 2., ranges[0].BaseMiningRate)
assert.Equal(t, 2., ranges[1].BaseMiningRate)
}

func TestGetAdoptionsRange_2AdoptionsPerRange(t *testing.T) {
var adoptions []*tokenomics.Adoption[float64]
adoptions = append(adoptions, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-58 * stdlibtime.Minute)),
BaseMiningRate: 16.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-55 * stdlibtime.Minute)),
BaseMiningRate: 8.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-30 * stdlibtime.Minute)),
BaseMiningRate: 4.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-10 * stdlibtime.Minute)),
BaseMiningRate: 2.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-30 * stdlibtime.Second)),
BaseMiningRate: 1.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: nil,
BaseMiningRate: 0.5,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: nil,
BaseMiningRate: 0.25,
Milestone: 1,
TotalActiveUsers: 1,
})
startedAt := time.New(time.Now().Add(-1 * stdlibtime.Minute))
endedAt := time.Now()
ranges := splitByAdoptionTimeRanges(adoptions, startedAt, endedAt)
assert.Equal(t, 2., ranges[0].BaseMiningRate)
assert.Equal(t, 1., ranges[1].BaseMiningRate)
assert.Equal(t, 1., ranges[2].BaseMiningRate)
}

func TestGetAdoptionsRange_AdoptionDemotionPerRange(t *testing.T) {
var adoptions []*tokenomics.Adoption[float64]
adoptions = append(adoptions, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-30 * stdlibtime.Second)),
BaseMiningRate: 16.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-55 * stdlibtime.Minute)),
BaseMiningRate: 8.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-30 * stdlibtime.Minute)),
BaseMiningRate: 4.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: time.New(time.Now().Add(-1 * stdlibtime.Minute)),
BaseMiningRate: 2.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: nil,
BaseMiningRate: 1.0,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: nil,
BaseMiningRate: 0.5,
Milestone: 1,
TotalActiveUsers: 1,
}, &tokenomics.Adoption[float64]{
AchievedAt: nil,
BaseMiningRate: 0.25,
Milestone: 1,
TotalActiveUsers: 1,
})
startedAt := time.New(time.Now().Add(-2 * stdlibtime.Minute))
endedAt := time.Now()
ranges := splitByAdoptionTimeRanges(adoptions, startedAt, endedAt)
assert.Equal(t, 4., ranges[0].BaseMiningRate)
assert.Equal(t, 2., ranges[1].BaseMiningRate)
assert.Equal(t, 16., ranges[2].BaseMiningRate)
assert.Equal(t, 16., ranges[3].BaseMiningRate)
}
9 changes: 1 addition & 8 deletions miner/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ const (

// .
var (
//go:embed DDL.sql
ddl string

//nolint:gochecknoglobals // Singleton & global config mounted only during bootstrap.
cfg config
)
Expand Down Expand Up @@ -116,11 +113,6 @@ type (
model.BalanceT2Field
}

recalculatedUser struct {
model.DeserializedRecalculatedUsersKey
model.RecalculatedTiersBalancesAtField
}

referral struct {
model.MiningSessionSoloStartedAtField
model.MiningSessionSoloEndedAtField
Expand All @@ -146,6 +138,7 @@ type (
wg *sync.WaitGroup
extraBonusStartDate *time.Time
extraBonusIndicesDistribution map[uint16]map[uint16]uint16
recalculationBalanceStartDate *time.Time
}
config struct {
disableAdvancedTeam *atomic.Pointer[[]string]
Expand Down
Loading

0 comments on commit 1253d06

Please sign in to comment.