Skip to content

Commit

Permalink
Add check to see if rewards-v2 is enabled
Browse files Browse the repository at this point in the history
update snapshot date in test
  • Loading branch information
seanmcgary committed Dec 12, 2024
1 parent 74941e1 commit 04e19a4
Show file tree
Hide file tree
Showing 35 changed files with 150 additions and 50 deletions.
40 changes: 38 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"
"strings"
"time"

"github.com/spf13/viper"
)
Expand Down Expand Up @@ -254,14 +255,14 @@ func (c *Config) GetForkDates() (ForkMap, error) {
Fork_Amazon: "1970-01-01", // Amazon hard fork was never on preprod as we backfilled
Fork_Nile: "2024-08-14", // Last calculation end timestamp was 8-13: https://holesky.etherscan.io/tx/0xb5a6855e88c79312b7c0e1c9f59ae9890b97f157ea27e69e4f0fadada4712b64#eventlog
Fork_Panama: "2024-10-01",
Fork_Arno: "2024-12-04",
Fork_Arno: "2024-12-12",
}, nil
case Chain_Holesky:
return ForkMap{
Fork_Amazon: "1970-01-01", // Amazon hard fork was never on testnet as we backfilled
Fork_Nile: "2024-08-13", // Last calculation end timestamp was 8-12: https://holesky.etherscan.io/tx/0x5fc81b5ed2a78b017ef313c181d8627737a97fef87eee85acedbe39fc8708c56#eventlog
Fork_Panama: "2024-10-01",
Fork_Arno: "2024-12-12",
Fork_Arno: "2024-12-13",
}, nil
case Chain_Mainnet:
return ForkMap{
Expand Down Expand Up @@ -296,6 +297,41 @@ func (c *Config) GetOperatorRestakedStrategiesStartBlock() uint64 {
return 0
}

func (c *Config) ShouldSkipRewardsGeneration(blockNumber uint64) bool {
switch c.Chain {
case Chain_Preprod:
// During this period we deployed the rewards-v2 contracts before updating the sidecar.
// This results in missed events which have to be filled by some means. To fill them in,
// we needed to manually delete delete blocks >= 2871534 and re-index. The trouble here
// is that re-indexing introduces new state which was not present at the original process time.
if blockNumber >= 2871534 && blockNumber <= 2909856 {
return true
}
case Chain_Holesky:
// Skip rewards generation for holesky
case Chain_Mainnet:
// Skip rewards generation for mainnet
}
return false
}

func (c *Config) IsRewardsV2EnabledForCutoffDate(cutoffDate string) (bool, error) {
forks, err := c.GetForkDates()
if err != nil {
return false, err
}
cutoffDateTime, err := time.Parse(time.DateOnly, cutoffDate)
if err != nil {
return false, errors.Join(fmt.Errorf("failed to parse cutoff date %s", cutoffDate), err)
}
arnoForkDateTime, err := time.Parse(time.DateOnly, forks[Fork_Arno])
if err != nil {
return false, errors.Join(fmt.Errorf("failed to parse Arno fork date %s", forks[Fork_Arno]), err)
}

return cutoffDateTime.Compare(arnoForkDateTime) >= 0, nil
}

// CanIgnoreIncorrectRewardsRoot returns true if the rewards root can be ignored for the given block number
//
// Due to inconsistencies in the rewards root calculation on testnet, we know that some roots
Expand Down
1 change: 1 addition & 0 deletions pkg/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func (p *Pipeline) RunForFetchedBlock(ctx context.Context, block *fetcher.Fetche
distributionRoots, err := p.stateManager.GetSubmittedDistributionRoots(blockNumber)
if err == nil && distributionRoots != nil {
for _, rs := range distributionRoots {

rewardStartTime := time.Now()

// first check to see if the root was disabled. If it was, it's possible we introduced changes that
Expand Down
12 changes: 11 additions & 1 deletion pkg/rewards/10_goldAvsODRewardAmounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ SELECT * FROM operator_token_sums
`

func (rc *RewardsCalculator) GenerateGold10AvsODRewardAmountsTable(snapshotDate string) error {
rewardsV2Enabled, err := rc.globalConfig.IsRewardsV2EnabledForCutoffDate(snapshotDate)
if err != nil {
rc.logger.Sugar().Errorw("Failed to check if rewards v2 is enabled", "error", err)
return err
}
if !rewardsV2Enabled {
rc.logger.Sugar().Infow("Rewards v2 is not enabled for this cutoff date, skipping GenerateGold10AvsODRewardAmountsTable")
return nil
}

allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)
destTableName := allTableNames[rewardsUtils.Table_10_AvsODRewardAmounts]

Expand All @@ -73,7 +83,7 @@ func (rc *RewardsCalculator) GenerateGold10AvsODRewardAmountsTable(snapshotDate
zap.String("destTableName", destTableName),
)

query, err := rewardsUtils.RenderQueryTemplate(_10_goldAvsODRewardAmountsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_10_goldAvsODRewardAmountsQuery, map[string]interface{}{
"destTableName": destTableName,
"activeODRewardsTable": allTableNames[rewardsUtils.Table_7_ActiveODRewards],
})
Expand Down
21 changes: 17 additions & 4 deletions pkg/rewards/11_goldStaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ rewards_for_all_earners_operators AS (
operator_tokens as amount
FROM {{.rfaeOperatorTable}}
),
{{ if .enableRewardsV2 }}
operator_od_rewards AS (
SELECT DISTINCT
-- We can select DISTINCT here because the operator's tokens are the same for each strategy in the reward hash
Expand Down Expand Up @@ -86,6 +87,7 @@ avs_od_rewards AS (
avs_tokens as amount
FROM {{.avsODRewardAmountsTable}}
),
{{ end }}
combined_rewards AS (
SELECT * FROM operator_rewards
UNION ALL
Expand All @@ -96,12 +98,14 @@ combined_rewards AS (
SELECT * FROM rewards_for_all_earners_stakers
UNION ALL
SELECT * FROM rewards_for_all_earners_operators
{{ if .enableRewardsV2 }}
UNION ALL
SELECT * FROM operator_od_rewards
UNION ALL
SELECT * FROM staker_od_rewards
UNION ALL
SELECT * FROM avs_od_rewards
{{ end }}
),
-- Dedupe earners, primarily operators who are also their own staker.
deduped_earners AS (
Expand Down Expand Up @@ -131,7 +135,14 @@ func (rc *RewardsCalculator) GenerateGold11StagingTable(snapshotDate string) err
zap.String("destTableName", destTableName),
)

query, err := rewardsUtils.RenderQueryTemplate(_11_goldStagingQuery, map[string]string{
isRewardsV2Enabled, err := rc.globalConfig.IsRewardsV2EnabledForCutoffDate(snapshotDate)
if err != nil {
rc.logger.Sugar().Errorw("Failed to check if rewards v2 is enabled", "error", err)
return err
}
rc.logger.Sugar().Infow("Is RewardsV2 enabled?", "enabled", isRewardsV2Enabled)

query, err := rewardsUtils.RenderQueryTemplate(_11_goldStagingQuery, map[string]interface{}{
"destTableName": destTableName,
"stakerRewardAmountsTable": allTableNames[rewardsUtils.Table_2_StakerRewardAmounts],
"operatorRewardAmountsTable": allTableNames[rewardsUtils.Table_3_OperatorRewardAmounts],
Expand All @@ -141,6 +152,7 @@ func (rc *RewardsCalculator) GenerateGold11StagingTable(snapshotDate string) err
"operatorODRewardAmountsTable": allTableNames[rewardsUtils.Table_8_OperatorODRewardAmounts],
"stakerODRewardAmountsTable": allTableNames[rewardsUtils.Table_9_StakerODRewardAmounts],
"avsODRewardAmountsTable": allTableNames[rewardsUtils.Table_10_AvsODRewardAmounts],
"enableRewardsV2": isRewardsV2Enabled,
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
Expand All @@ -167,21 +179,22 @@ func (rc *RewardsCalculator) ListGoldStagingRowsForSnapshot(snapshotDate string)
allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)

results := make([]*GoldStagingRow, 0)
query, err := rewardsUtils.RenderQueryTemplate(`
query := `
SELECT
earner,
snapshot::text as snapshot,
reward_hash,
token,
amount
FROM {{.goldStagingTable}} WHERE DATE(snapshot) < @cutoffDate`, map[string]string{
FROM {{.goldStagingTable}} WHERE DATE(snapshot) < @cutoffDate`
query, err := rewardsUtils.RenderQueryTemplate(query, map[string]interface{}{
"goldStagingTable": allTableNames[rewardsUtils.Table_11_GoldStaging],
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
return nil, err
}
res := rc.grm.Raw(query,
res := rc.grm.Debug().Raw(query,
sql.Named("cutoffDate", snapshotDate),
).Scan(&results)
if res.Error != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/12_goldFinal.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (rc *RewardsCalculator) GenerateGold12FinalTable(snapshotDate string) error
zap.String("cutoffDate", snapshotDate),
)

query, err := rewardsUtils.RenderQueryTemplate(_12_goldFinalQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_12_goldFinalQuery, map[string]interface{}{
"goldStagingTable": allTableNames[rewardsUtils.Table_11_GoldStaging],
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/1_goldActiveRewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (r *RewardsCalculator) Generate1ActiveRewards(snapshotDate string) error {
zap.String("destTableName", destTableName),
)

query, err := rewardsUtils.RenderQueryTemplate(_1_goldActiveRewardsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_1_goldActiveRewardsQuery, map[string]interface{}{
"destTableName": destTableName,
"rewardsStart": rewardsStart,
"cutoffDate": snapshotDate,
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/2_goldStakerRewardAmounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (rc *RewardsCalculator) GenerateGold2StakerRewardAmountsTable(snapshotDate
zap.String("arnoHardforkDate", forks[config.Fork_Arno]),
)

query, err := rewardsUtils.RenderQueryTemplate(_2_goldStakerRewardAmountsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_2_goldStakerRewardAmountsQuery, map[string]interface{}{
"destTableName": destTableName,
"activeRewardsTable": allTableNames[rewardsUtils.Table_1_ActiveRewards],
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/3_goldOperatorRewardAmounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (rc *RewardsCalculator) GenerateGold3OperatorRewardAmountsTable(snapshotDat
zap.String("destTableName", destTableName),
)

query, err := rewardsUtils.RenderQueryTemplate(_3_goldOperatorRewardAmountsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_3_goldOperatorRewardAmountsQuery, map[string]interface{}{
"destTableName": destTableName,
"stakerRewardAmountsTable": allTableNames[rewardsUtils.Table_2_StakerRewardAmounts],
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/4_goldRewardsForAll.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (rc *RewardsCalculator) GenerateGold4RewardsForAllTable(snapshotDate string
zap.String("destTableName", destTableName),
)

query, err := rewardsUtils.RenderQueryTemplate(_4_goldRewardsForAllQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_4_goldRewardsForAllQuery, map[string]interface{}{
"destTableName": destTableName,
"activeRewardsTable": allTableNames[rewardsUtils.Table_1_ActiveRewards],
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/5_goldRfaeStakers.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (rc *RewardsCalculator) GenerateGold5RfaeStakersTable(snapshotDate string,
zap.String("arnoHardforkDate", forks[config.Fork_Arno]),
)

query, err := rewardsUtils.RenderQueryTemplate(_5_goldRfaeStakersQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_5_goldRfaeStakersQuery, map[string]interface{}{
"destTableName": destTableName,
"activeRewardsTable": allTableNames[rewardsUtils.Table_1_ActiveRewards],
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/6_goldRfaeOperators.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (rc *RewardsCalculator) GenerateGold6RfaeOperatorsTable(snapshotDate string
zap.String("destTableName", destTableName),
)

query, err := rewardsUtils.RenderQueryTemplate(_6_goldRfaeOperatorsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_6_goldRfaeOperatorsQuery, map[string]interface{}{
"destTableName": destTableName,
"rfaeStakersTable": allTableNames[rewardsUtils.Table_5_RfaeStakers],
})
Expand Down
12 changes: 11 additions & 1 deletion pkg/rewards/7_goldActiveODRewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ SELECT * FROM active_rewards_final
// @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) Generate7ActiveODRewards(snapshotDate string) error {
rewardsV2Enabled, err := r.globalConfig.IsRewardsV2EnabledForCutoffDate(snapshotDate)
if err != nil {
r.logger.Sugar().Errorw("Failed to check if rewards v2 is enabled", "error", err)
return err
}
if !rewardsV2Enabled {
r.logger.Sugar().Infow("Rewards v2 is not enabled for this cutoff date, skipping Generate7ActiveODRewards")
return nil
}

allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)
destTableName := allTableNames[rewardsUtils.Table_7_ActiveODRewards]

Expand All @@ -138,7 +148,7 @@ func (r *RewardsCalculator) Generate7ActiveODRewards(snapshotDate string) error
zap.String("destTableName", destTableName),
)

query, err := rewardsUtils.RenderQueryTemplate(_7_goldActiveODRewardsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_7_goldActiveODRewardsQuery, map[string]interface{}{
"destTableName": destTableName,
"rewardsStart": rewardsStart,
"cutoffDate": snapshotDate,
Expand Down
11 changes: 10 additions & 1 deletion pkg/rewards/8_goldOperatorODRewardAmounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ SELECT * FROM operator_splits
`

func (rc *RewardsCalculator) GenerateGold8OperatorODRewardAmountsTable(snapshotDate string) error {
rewardsV2Enabled, err := rc.globalConfig.IsRewardsV2EnabledForCutoffDate(snapshotDate)
if err != nil {
rc.logger.Sugar().Errorw("Failed to check if rewards v2 is enabled", "error", err)
return err
}
if !rewardsV2Enabled {
rc.logger.Sugar().Infow("Rewards v2 is not enabled for this cutoff date, skipping GenerateGold8OperatorODRewardAmountsTable")
return nil
}
allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)
destTableName := allTableNames[rewardsUtils.Table_8_OperatorODRewardAmounts]

Expand All @@ -74,7 +83,7 @@ func (rc *RewardsCalculator) GenerateGold8OperatorODRewardAmountsTable(snapshotD
zap.String("destTableName", destTableName),
)

query, err := rewardsUtils.RenderQueryTemplate(_8_goldOperatorODRewardAmountsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_8_goldOperatorODRewardAmountsQuery, map[string]interface{}{
"destTableName": destTableName,
"activeODRewardsTable": allTableNames[rewardsUtils.Table_7_ActiveODRewards],
})
Expand Down
12 changes: 11 additions & 1 deletion pkg/rewards/9_goldStakerODRewardAmounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ SELECT * FROM staker_reward_amounts
`

func (rc *RewardsCalculator) GenerateGold9StakerODRewardAmountsTable(snapshotDate string) error {
rewardsV2Enabled, err := rc.globalConfig.IsRewardsV2EnabledForCutoffDate(snapshotDate)
if err != nil {
rc.logger.Sugar().Errorw("Failed to check if rewards v2 is enabled", "error", err)
return err
}
if !rewardsV2Enabled {
rc.logger.Sugar().Infow("Rewards v2 is not enabled for this cutoff date, skipping GenerateGold9StakerODRewardAmountsTable")
return nil
}

allTableNames := rewardsUtils.GetGoldTableNames(snapshotDate)
destTableName := allTableNames[rewardsUtils.Table_9_StakerODRewardAmounts]

Expand All @@ -123,7 +133,7 @@ func (rc *RewardsCalculator) GenerateGold9StakerODRewardAmountsTable(snapshotDat
zap.String("destTableName", destTableName),
)

query, err := rewardsUtils.RenderQueryTemplate(_9_goldStakerODRewardAmountsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(_9_goldStakerODRewardAmountsQuery, map[string]interface{}{
"destTableName": destTableName,
"activeODRewardsTable": allTableNames[rewardsUtils.Table_7_ActiveODRewards],
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/combinedRewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const rewardsCombinedQuery = `
func (r *RewardsCalculator) GenerateAndInsertCombinedRewards(snapshotDate string) error {
tableName := "combined_rewards"

query, err := rewardsUtils.RenderQueryTemplate(rewardsCombinedQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(rewardsCombinedQuery, map[string]interface{}{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/operatorAvsRegistrationSnaphots.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ CROSS JOIN generate_series(DATE(start_time), DATE(end_time) - interval '1' day,
func (r *RewardsCalculator) GenerateAndInsertOperatorAvsRegistrationSnapshots(snapshotDate string) error {
tableName := "operator_avs_registration_snapshots"

query, err := rewardsUtils.RenderQueryTemplate(operatorAvsRegistrationSnapshotsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(operatorAvsRegistrationSnapshotsQuery, map[string]interface{}{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/operatorAvsSplitSnapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ select * from final_results
func (r *RewardsCalculator) GenerateAndInsertOperatorAvsSplitSnapshots(snapshotDate string) error {
tableName := "operator_avs_split_snapshots"

query, err := rewardsUtils.RenderQueryTemplate(operatorAvsSplitSnapshotQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(operatorAvsSplitSnapshotQuery, map[string]interface{}{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/operatorAvsStrategySnapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (r *RewardsCalculator) GenerateAndInsertOperatorAvsStrategySnapshots(snapsh
tableName := "operator_avs_strategy_snapshots"
contractAddresses := r.globalConfig.GetContractsMapForChain()

query, err := rewardsUtils.RenderQueryTemplate(operatorAvsStrategyWindowsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(operatorAvsStrategyWindowsQuery, map[string]interface{}{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/operatorDirectedRewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const operatorDirectedRewardsQuery = `
func (r *RewardsCalculator) GenerateAndInsertOperatorDirectedRewards(snapshotDate string) error {
tableName := "operator_directed_rewards"

query, err := rewardsUtils.RenderQueryTemplate(operatorDirectedRewardsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(operatorDirectedRewardsQuery, map[string]interface{}{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/operatorPISplitSnapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ select * from final_results
func (r *RewardsCalculator) GenerateAndInsertOperatorPISplitSnapshots(snapshotDate string) error {
tableName := "operator_pi_split_snapshots"

query, err := rewardsUtils.RenderQueryTemplate(operatorPISplitSnapshotQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(operatorPISplitSnapshotQuery, map[string]interface{}{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/operatorShareSnapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ FROM
func (r *RewardsCalculator) GenerateAndInsertOperatorShareSnapshots(snapshotDate string) error {
tableName := "operator_share_snapshots"

query, err := rewardsUtils.RenderQueryTemplate(operatorShareSnapshotsQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(operatorShareSnapshotsQuery, map[string]interface{}{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rewards/operatorShares.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const operatorSharesQuery = `
func (r *RewardsCalculator) GenerateAndInsertOperatorShares(snapshotDate string) error {
tableName := "operator_shares"

query, err := rewardsUtils.RenderQueryTemplate(operatorSharesQuery, map[string]string{
query, err := rewardsUtils.RenderQueryTemplate(operatorSharesQuery, map[string]interface{}{
"cutoffDate": snapshotDate,
})
if err != nil {
Expand Down
Loading

0 comments on commit 04e19a4

Please sign in to comment.