Skip to content

Commit

Permalink
debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Oct 28, 2024
1 parent 471e050 commit 7e49fc8
Show file tree
Hide file tree
Showing 20 changed files with 508 additions and 305 deletions.
4 changes: 2 additions & 2 deletions internal/eigenState/rewardSubmissions/rewardSubmissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type RewardSubmission struct {
Strategy string
StrategyIndex uint64
Multiplier string
StartTimestamp *time.Time `gorm:"type:DATETIME"`
EndTimestamp *time.Time `gorm:"type:DATETIME"`
StartTimestamp *time.Time
EndTimestamp *time.Time
Duration uint64
BlockNumber uint64
RewardType string // avs, all_stakers, all_earners
Expand Down
212 changes: 106 additions & 106 deletions internal/postgres/migrations/202410241539_goldTables/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,112 +76,112 @@ func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error {
deleted_at timestamp with time zone,
unique(address)
)`,
`create table if not exists gold_1_active_rewards (
avs varchar not null,
snapshot date not null,
token varchar not null,
tokens_per_day double precision not null,
tokens_per_day_decimal numeric not null,
multiplier numeric(78) not null,
strategy varchar not null,
reward_hash varchar not null,
reward_type varchar not null,
reward_submission_date varchar not null
)`,
`create table if not exists gold_2_staker_reward_amounts (
reward_hash varchar not null,
snapshot date NOT NULL,
token varchar not null,
tokens_per_day double precision not null,
tokens_per_day_decimal numeric not null,
avs varchar not null,
strategy varchar not null,
multiplier numeric(78) not null,
reward_type varchar not null,
reward_submission_date varchar NOT NULL,
operator varchar not null,
staker varchar not null,
shares numeric not null,
staker_weight numeric not null,
rn bigint not null,
total_weight numeric not null,
staker_proportion numeric not null,
total_staker_operator_payout numeric not null,
operator_tokens numeric not null,
staker_tokens numeric not null
)`,
`create table if not exists gold_3_operator_reward_amounts (
reward_hash varchar not null,
snapshot date NOT NULL,
token varchar not null,
tokens_per_day double precision not null,
avs varchar not null,
strategy varchar not null,
multiplier numeric(78) not null,
reward_type varchar not null,
operator varchar not null,
operator_tokens numeric not null,
rn bigint NOT NULL
)`,
`create table if not exists gold_4_rewards_for_all (
reward_hash varchar not null,
snapshot date NOT NULL,
token varchar not null,
tokens_per_day double precision not null,
avs varchar not null,
strategy varchar not null,
multiplier numeric(78) not null,
reward_type varchar not null,
staker varchar not null,
shares numeric not null,
staker_weight numeric not null,
rn bigint NOT NULL,
total_staker_weight numeric not null,
staker_proportion numeric not null,
staker_tokens numeric(38) not null
)`,
`create table if not exists gold_5_rfae_stakers (
reward_hash varchar not null,
snapshot date NOT NULL,
token varchar not null,
tokens_per_day_decimal numeric not null,
avs varchar not null,
strategy varchar not null,
multiplier numeric(78) not null,
reward_type varchar not null,
reward_submission_date varchar NOT NULL,
operator varchar not null,
staker varchar not null,
shares numeric not null,
excluded_address varchar,
staker_weight numeric not null,
rn bigint NOT NULL,
total_weight numeric not null,
staker_proportion numeric not null,
total_staker_operator_payout numeric not null,
operator_tokens numeric not null,
staker_tokens numeric not null
)`,
`create table if not exists gold_6_rfae_operators (
reward_hash varchar not null,
snapshot date NOT NULL,
token varchar not null,
tokens_per_day_decimal numeric not null,
avs varchar not null,
strategy varchar not null,
multiplier numeric(78) not null,
reward_type varchar not null,
operator varchar not null,
operator_tokens numeric not null,
rn bigint NOT NULL
)`,
`create table if not exists gold_7_staging (
earner varchar not null,
snapshot date NOT NULL,
reward_hash varchar not null,
token varchar not null,
amount numeric not null
)`,
/*`create table if not exists gold_1_active_rewards (
avs varchar not null,
snapshot date not null,
token varchar not null,
tokens_per_day double precision not null,
tokens_per_day_decimal numeric not null,
multiplier numeric(78) not null,
strategy varchar not null,
reward_hash varchar not null,
reward_type varchar not null,
reward_submission_date varchar not null
)`,
`create table if not exists gold_2_staker_reward_amounts (
reward_hash varchar not null,
snapshot date NOT NULL,
token varchar not null,
tokens_per_day double precision not null,
tokens_per_day_decimal numeric not null,
avs varchar not null,
strategy varchar not null,
multiplier numeric(78) not null,
reward_type varchar not null,
reward_submission_date varchar NOT NULL,
operator varchar not null,
staker varchar not null,
shares numeric not null,
staker_weight numeric not null,
rn bigint not null,
total_weight numeric not null,
staker_proportion numeric not null,
total_staker_operator_payout numeric not null,
operator_tokens numeric not null,
staker_tokens numeric not null
)`,
`create table if not exists gold_3_operator_reward_amounts (
reward_hash varchar not null,
snapshot date NOT NULL,
token varchar not null,
tokens_per_day double precision not null,
avs varchar not null,
strategy varchar not null,
multiplier numeric(78) not null,
reward_type varchar not null,
operator varchar not null,
operator_tokens numeric not null,
rn bigint NOT NULL
)`,
`create table if not exists gold_4_rewards_for_all (
reward_hash varchar not null,
snapshot date NOT NULL,
token varchar not null,
tokens_per_day double precision not null,
avs varchar not null,
strategy varchar not null,
multiplier numeric(78) not null,
reward_type varchar not null,
staker varchar not null,
shares numeric not null,
staker_weight numeric not null,
rn bigint NOT NULL,
total_staker_weight numeric not null,
staker_proportion numeric not null,
staker_tokens numeric(38) not null
)`,
`create table if not exists gold_5_rfae_stakers (
reward_hash varchar not null,
snapshot date NOT NULL,
token varchar not null,
tokens_per_day_decimal numeric not null,
avs varchar not null,
strategy varchar not null,
multiplier numeric(78) not null,
reward_type varchar not null,
reward_submission_date varchar NOT NULL,
operator varchar not null,
staker varchar not null,
shares numeric not null,
excluded_address varchar,
staker_weight numeric not null,
rn bigint NOT NULL,
total_weight numeric not null,
staker_proportion numeric not null,
total_staker_operator_payout numeric not null,
operator_tokens numeric not null,
staker_tokens numeric not null
)`,
`create table if not exists gold_6_rfae_operators (
reward_hash varchar not null,
snapshot date NOT NULL,
token varchar not null,
tokens_per_day_decimal numeric not null,
avs varchar not null,
strategy varchar not null,
multiplier numeric(78) not null,
reward_type varchar not null,
operator varchar not null,
operator_tokens numeric not null,
rn bigint NOT NULL
)`,
`create table if not exists gold_7_staging (
earner varchar not null,
snapshot date NOT NULL,
reward_hash varchar not null,
token varchar not null,
amount numeric not null
)`,*/
`create table if not exists gold_table (
earner varchar not null,
snapshot date NOT NULL,
Expand Down
2 changes: 0 additions & 2 deletions internal/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,9 @@ func WrapTxAndCommit[T any](fn func(*gorm.DB) (T, error), db *gorm.DB, tx *gorm.
res, err := fn(tx)

if err != nil && !exists {
fmt.Printf("Rollback transaction\n")
tx.Rollback()
}
if err == nil && !exists {
fmt.Printf("Commit transaction\n")
tx.Commit()
}
return res, err
Expand Down
6 changes: 6 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"encoding/hex"
"fmt"
"regexp"
"strings"
)

Expand All @@ -28,3 +29,8 @@ func StripLeading0x(s string) string {
}
return s
}

func SnakeCase(s string) string {
notSnake := regexp.MustCompile(`[_-]`)
return notSnake.ReplaceAllString(s, "_")
}
37 changes: 26 additions & 11 deletions pkg/rewards/1_goldActiveRewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package rewards

import (
"database/sql"
"go.uber.org/zap"
)

var _1_goldActiveRewardsQuery = `
insert into gold_1_active_rewards
create table {{.destTableName}} as
WITH active_rewards_modified as (
SELECT *,
amount/(duration/86400) as tokens_per_day,
cast(@cutoffDate AS TIMESTAMP(6)) as global_end_inclusive -- Inclusive means we DO USE this day as a snapshot
FROM combined_rewards
WHERE end_timestamp >= DATE(@rewardsStart) and start_timestamp <= DATE(@cutoffDate)
WHERE end_timestamp >= TIMESTAMP '{{.rewardsStart}}' and start_timestamp <= TIMESTAMP '{{.cutoffDate}}'
),
-- Cut each reward's start and end windows to handle the global range
active_rewards_updated_end_timestamps as (
Expand Down Expand Up @@ -50,7 +51,7 @@ active_rewards_updated_start_timestamps as (
ap.reward_hash,
ap.reward_type,
ap.global_end_inclusive,
ap.reward_submission_date
ap.reward_submission_date
FROM active_rewards_updated_end_timestamps ap
LEFT JOIN gold_table g ON g.reward_hash = ap.reward_hash
GROUP BY ap.avs, ap.reward_end_inclusive, ap.token, ap.tokens_per_day, ap.multiplier, ap.strategy, ap.reward_hash, ap.global_end_inclusive, ap.reward_start_exclusive, ap.reward_type, ap.reward_submission_date
Expand Down Expand Up @@ -87,21 +88,35 @@ active_rewards_final AS (
WHERE day != reward_start_exclusive
)
select * from active_rewards_final
where
DATE(snapshot) >= @rewardsStart
and DATE(snapshot) < @cutoffDate
`

// Generate1ActiveRewards generates active rewards for the gold_1_active_rewards table
//
// @param snapshotDate: The upper bound of when to calculate rewards to
// @param startDate: The lower bound of when to calculate rewards from. If we're running rewards for the first time,
// this will be "1970-01-01". If this is a subsequent run, this will be the last snapshot date.
func (r *RewardsCalculator) Generate1ActiveRewards(cutoffDate string, startDate string) error {
r.logger.Sugar().Infow("Generating active rewards", "cutoffDate", cutoffDate, "startDate", startDate)
res := r.grm.Exec(_1_goldActiveRewardsQuery,
sql.Named("cutoffDate", cutoffDate),
sql.Named("rewardsStart", startDate),
func (r *RewardsCalculator) Generate1ActiveRewards(startDate string, snapshotDate string) error {
allTableNames := getGoldTableNames(snapshotDate)
destTableName := allTableNames[Table_1_ActiveRewards]

r.logger.Sugar().Infow("Generating active rewards",
zap.String("startDate", startDate),
zap.String("cutoffDate", snapshotDate),
zap.String("destTableName", destTableName),
)

query, err := renderQueryTemplate(_1_goldActiveRewardsQuery, map[string]string{
"destTableName": destTableName,
"rewardsStart": startDate,
"cutoffDate": snapshotDate,
})
if err != nil {
r.logger.Sugar().Errorw("Failed to render query template", "error", err)
return err
}

res := r.grm.Exec(query,
sql.Named("cutoffDate", snapshotDate),
)
if res.Error != nil {
r.logger.Sugar().Errorw("Failed to generate active rewards", "error", res.Error)
Expand Down
32 changes: 24 additions & 8 deletions pkg/rewards/2_goldStakerRewardAmounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package rewards
import (
"database/sql"
"github.com/Layr-Labs/go-sidecar/internal/config"
"go.uber.org/zap"
)

const _2_goldStakerRewardAmountsQuery = `
insert into gold_2_staker_reward_amounts
create table {{.destTableName}} as
WITH reward_snapshot_operators as (
SELECT
ap.reward_hash,
Expand All @@ -20,7 +21,7 @@ WITH reward_snapshot_operators as (
ap.reward_type,
ap.reward_submission_date,
oar.operator
FROM gold_1_active_rewards ap
FROM {{.activeRewardsTable}} ap
JOIN operator_avs_registration_snapshots oar
ON ap.avs = oar.avs and ap.snapshot = oar.snapshot
WHERE ap.reward_type = 'avs'
Expand Down Expand Up @@ -128,16 +129,31 @@ token_breakdowns AS (
FROM staker_operator_total_tokens
)
SELECT * from token_breakdowns
where
DATE(snapshot) >= @startDate
and DATE(snapshot) < @cutoffDate
ORDER BY reward_hash, snapshot, staker, operator
`

func (rc *RewardsCalculator) GenerateGold2StakerRewardAmountsTable(startDate string, snapshotDate string, forks config.ForkMap) error {
res := rc.grm.Exec(_2_goldStakerRewardAmountsQuery,
sql.Named("startDate", startDate),
sql.Named("cutoffDate", snapshotDate),
allTableNames := getGoldTableNames(snapshotDate)
destTableName := allTableNames[Table_2_StakerRewardAmounts]

rc.logger.Sugar().Infow("Generating staker reward amounts",
zap.String("startDate", startDate),
zap.String("cutoffDate", snapshotDate),
zap.String("destTableName", destTableName),
zap.String("amazonHardforkDate", forks[config.Fork_Amazon]),
zap.String("nileHardforkDate", forks[config.Fork_Nile]),
)

query, err := renderQueryTemplate(_2_goldStakerRewardAmountsQuery, map[string]string{
"destTableName": destTableName,
"activeRewardsTable": allTableNames[Table_1_ActiveRewards],
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
return err
}

res := rc.grm.Debug().Exec(query,
sql.Named("amazonHardforkDate", forks[config.Fork_Amazon]),
sql.Named("nileHardforkDate", forks[config.Fork_Nile]),
)
Expand Down
Loading

0 comments on commit 7e49fc8

Please sign in to comment.