Skip to content

Commit

Permalink
Port over staker operator table generation
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Dec 2, 2024
1 parent e30e126 commit 8cbaf97
Show file tree
Hide file tree
Showing 22 changed files with 1,166 additions and 54 deletions.
5 changes: 4 additions & 1 deletion cmd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/Layr-Labs/sidecar/pkg/postgres"
"github.com/Layr-Labs/sidecar/pkg/postgres/migrations"
"github.com/Layr-Labs/sidecar/pkg/rewards"
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
pgStorage "github.com/Layr-Labs/sidecar/pkg/storage/postgres"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -88,7 +89,9 @@ var runDatabaseCmd = &cobra.Command{

idxr := indexer.NewIndexer(mds, contractStore, cm, client, fetchr, cc, grm, l, cfg)

rc, err := rewards.NewRewardsCalculator(cfg, grm, mds, l)
sog := stakerOperators.NewStakerOperatorGenerator(grm, l, cfg)

rc, err := rewards.NewRewardsCalculator(cfg, grm, mds, sog, l)
if err != nil {
l.Sugar().Fatalw("Failed to create rewards calculator", zap.Error(err))
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/Layr-Labs/sidecar/pkg/pipeline"
"github.com/Layr-Labs/sidecar/pkg/postgres"
"github.com/Layr-Labs/sidecar/pkg/rewards"
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
"github.com/Layr-Labs/sidecar/pkg/shutdown"
"github.com/Layr-Labs/sidecar/pkg/sidecar"
pgStorage "github.com/Layr-Labs/sidecar/pkg/storage/postgres"
Expand Down Expand Up @@ -94,7 +95,9 @@ var runCmd = &cobra.Command{

idxr := indexer.NewIndexer(mds, contractStore, cm, client, fetchr, cc, grm, l, cfg)

rc, err := rewards.NewRewardsCalculator(cfg, grm, mds, l)
sog := stakerOperators.NewStakerOperatorGenerator(grm, l, cfg)

rc, err := rewards.NewRewardsCalculator(cfg, grm, mds, sog, l)
if err != nil {
l.Sugar().Fatalw("Failed to create rewards calculator", zap.Error(err))
}
Expand Down
60 changes: 60 additions & 0 deletions pkg/postgres/migrations/202412021311_stakerOperatorTables/up.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package _202412021311_stakerOperatorTables

import (
"database/sql"
"gorm.io/gorm"
)

type Migration struct {
}

func (m *Migration) Up(db *sql.DB, grm *gorm.DB) error {
queries := []string{
`CREATE TABLE IF NOT EXISTS sot_staker_strategy_payouts (
reward_hash varchar NOT NULL,
snapshot TIMESTAMP NOT NULL,
token varchar NOT NULL,
tokens_per_day double precision NOT NULL,
avs varchar NOT NULL,
strategy varchar NOT NULL,
multiplier numeric NOT NULL,
reward_type varchar NOT NULL,
operator varchar NOT NULL,
staker varchar NOT NULL,
shares numeric NOT NULL,
staker_tokens numeric NOT NULL,
staker_strategy_weight numeric NOT NULL,
staker_total_strategy_weight numeric NOT NULL,
staker_strategy_proportion numeric NOT NULL,
staker_strategy_tokens numeric NOT NULL
);`,
`CREATE TABLE IF NOT EXISTS sot_operator_strategy_rewards (
reward_hash varchar NOT NULL,
snapshot TIMESTAMP NOT NULL,
token varchar NOT NULL,
tokens_per_day double precision NOT NULL,
avs varchar NOT NULL,
strategy varchar NOT NULL,
multiplier numeric NOT NULL,
reward_type varchar NOT NULL,
operator varchar NOT NULL,
shares numeric NOT NULL,
operator_tokens numeric NOT NULL,
operator_strategy_weight numeric NOT NULL,
operator_total_strategy_weight numeric NOT NULL,
operator_strategy_proportion numeric NOT NULL,
operator_strategy_tokens numeric NOT NULL
)`,
}

for _, query := range queries {
if err := grm.Exec(query).Error; err != nil {
return err
}
}
return nil
}

func (m *Migration) GetName() string {
return "202412021311_stakerOperatorTables"
}
2 changes: 2 additions & 0 deletions pkg/postgres/migrations/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
_202411130953_addHashColumns "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202411130953_addHashColumns"
_202411131200_eigenStateModelConstraints "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202411131200_eigenStateModelConstraints"
_202411191947_cleanupUnusedTables "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202411191947_cleanupUnusedTables"
_202412021311_stakerOperatorTables "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202412021311_stakerOperatorTables"
"go.uber.org/zap"
"gorm.io/gorm"
"time"
Expand Down Expand Up @@ -104,6 +105,7 @@ func (m *Migrator) MigrateAll() error {
&_202411130953_addHashColumns.Migration{},
&_202411131200_eigenStateModelConstraints.Migration{},
&_202411191947_cleanupUnusedTables.Migration{},
&_202412021311_stakerOperatorTables.Migration{},
}

for _, migration := range migrations {
Expand Down
4 changes: 3 additions & 1 deletion pkg/rewards/combinedRewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/Layr-Labs/sidecar/pkg/postgres"
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"gorm.io/gorm"
Expand Down Expand Up @@ -109,7 +110,8 @@ func Test_CombinedRewards(t *testing.T) {
}
})
t.Run("Should generate the proper combinedRewards", func(t *testing.T) {
rewards, _ := NewRewardsCalculator(cfg, grm, nil, l)
sog := stakerOperators.NewStakerOperatorGenerator(grm, l, cfg)
rewards, _ := NewRewardsCalculator(cfg, grm, nil, sog, l)

err = rewards.GenerateAndInsertCombinedRewards(snapshotDate)
assert.Nil(t, err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/rewards/operatorAvsRegistrationSnaphots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/Layr-Labs/sidecar/pkg/postgres"
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"gorm.io/gorm"
Expand Down Expand Up @@ -124,7 +125,8 @@ func Test_OperatorAvsRegistrationSnapshots(t *testing.T) {
}
})
t.Run("Should generate the proper operatorAvsRegistrationWindows", func(t *testing.T) {
rewards, _ := NewRewardsCalculator(cfg, grm, nil, l)
sog := stakerOperators.NewStakerOperatorGenerator(grm, l, cfg)
rewards, _ := NewRewardsCalculator(cfg, grm, nil, sog, l)

err := rewards.GenerateAndInsertOperatorAvsRegistrationSnapshots(snapshotDate)
assert.Nil(t, err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/rewards/operatorAvsStrategySnapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/Layr-Labs/sidecar/pkg/postgres"
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"gorm.io/gorm"
Expand Down Expand Up @@ -118,7 +119,8 @@ func Test_OperatorAvsStrategySnapshots(t *testing.T) {
})

t.Run("Should calculate correct operatorAvsStrategy windows", func(t *testing.T) {
rewards, _ := NewRewardsCalculator(cfg, grm, nil, l)
sog := stakerOperators.NewStakerOperatorGenerator(grm, l, cfg)
rewards, _ := NewRewardsCalculator(cfg, grm, nil, sog, l)

t.Log("Generating snapshots")
err := rewards.GenerateAndInsertOperatorAvsStrategySnapshots(snapshotDate)
Expand Down
4 changes: 3 additions & 1 deletion pkg/rewards/operatorShareSnapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/Layr-Labs/sidecar/pkg/postgres"
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"gorm.io/gorm"
Expand Down Expand Up @@ -99,7 +100,8 @@ func Test_OperatorShareSnapshots(t *testing.T) {
}
})
t.Run("Should generate operator share snapshots", func(t *testing.T) {
rewards, _ := NewRewardsCalculator(cfg, grm, nil, l)
sog := stakerOperators.NewStakerOperatorGenerator(grm, l, cfg)
rewards, _ := NewRewardsCalculator(cfg, grm, nil, sog, l)

t.Log("Generating operator share snapshots")
err := rewards.GenerateAndInsertOperatorShareSnapshots(snapshotDate)
Expand Down
4 changes: 3 additions & 1 deletion pkg/rewards/operatorShares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/Layr-Labs/sidecar/pkg/postgres"
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"gorm.io/gorm"
Expand Down Expand Up @@ -84,7 +85,8 @@ func Test_OperatorShares(t *testing.T) {
}
})
t.Run("Should generate staker shares", func(t *testing.T) {
rewards, _ := NewRewardsCalculator(cfg, grm, nil, l)
sog := stakerOperators.NewStakerOperatorGenerator(grm, l, cfg)
rewards, _ := NewRewardsCalculator(cfg, grm, nil, sog, l)

t.Log("Generating staker shares")
err := rewards.GenerateAndInsertOperatorShares(snapshotDate)
Expand Down
62 changes: 19 additions & 43 deletions pkg/rewards/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"errors"
"fmt"
"github.com/Layr-Labs/eigenlayer-rewards-proofs/pkg/distribution"
"github.com/Layr-Labs/sidecar/pkg/postgres/helpers"
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
"github.com/Layr-Labs/sidecar/pkg/rewardsUtils"
"github.com/Layr-Labs/sidecar/pkg/storage"
"github.com/Layr-Labs/sidecar/pkg/utils"
gethcommon "github.com/ethereum/go-ethereum/common"
Expand All @@ -23,19 +24,22 @@ type RewardsCalculator struct {
logger *zap.Logger
grm *gorm.DB
blockStore storage.BlockStore
sog *stakerOperators.StakerOperatorsGenerator
globalConfig *config.Config
}

func NewRewardsCalculator(
cfg *config.Config,
grm *gorm.DB,
bs storage.BlockStore,
sog *stakerOperators.StakerOperatorsGenerator,
l *zap.Logger,
) (*RewardsCalculator, error) {
rc := &RewardsCalculator{
logger: l,
grm: grm,
blockStore: bs,
sog: sog,
globalConfig: cfg,
}

Expand Down Expand Up @@ -231,6 +235,12 @@ func (rc *RewardsCalculator) calculateRewards(snapshotDate string) error {
return err
}

if err = rc.sog.GenerateStakerOperatorsTable(snapshotDate); err != nil {
_ = rc.UpdateRewardSnapshotStatus(snapshotDate, storage.RewardSnapshotStatusFailed)
rc.logger.Sugar().Errorw("Failed to generate staker operators table", "error", err)
return err
}

if err = rc.UpdateRewardSnapshotStatus(snapshotDate, storage.RewardSnapshotStatusCompleted); err != nil {
rc.logger.Sugar().Errorw("Failed to update reward snapshot status", "error", err)
return err
Expand Down Expand Up @@ -366,32 +376,13 @@ func (rc *RewardsCalculator) generateAndInsertFromQuery(
query string,
variables map[string]interface{},
) error {
tmpTableName := fmt.Sprintf("%s_tmp", tableName)

queryWithInsert := fmt.Sprintf("CREATE TABLE %s AS %s", tmpTableName, query)

_, err := helpers.WrapTxAndCommit(func(tx *gorm.DB) (interface{}, error) {
queries := []string{
queryWithInsert,
fmt.Sprintf(`drop table if exists %s`, tableName),
fmt.Sprintf(`alter table %s rename to %s`, tmpTableName, tableName),
}
for i, query := range queries {
var res *gorm.DB
if i == 0 && variables != nil {
res = tx.Exec(query, variables)
} else {
res = tx.Exec(query)
}
if res.Error != nil {
rc.logger.Sugar().Errorw("Failed to execute query", "query", query, "error", res.Error)
return nil, res.Error
}
}
return nil, nil
}, rc.grm, nil)

return err
return rewardsUtils.GenerateAndInsertFromQuery(
rc.grm,
tableName,
query,
variables,
rc.logger,
)
}

var (
Expand All @@ -405,23 +396,8 @@ var (
Table_8_GoldTable = "gold_table"
)

var goldTableBaseNames = map[string]string{
Table_1_ActiveRewards: "gold_1_active_rewards",
Table_2_StakerRewardAmounts: "gold_2_staker_reward_amounts",
Table_3_OperatorRewardAmounts: "gold_3_operator_reward_amounts",
Table_4_RewardsForAll: "gold_4_rewards_for_all",
Table_5_RfaeStakers: "gold_5_rfae_stakers",
Table_6_RfaeOperators: "gold_6_rfae_operators",
Table_7_GoldStaging: "gold_7_staging",
Table_8_GoldTable: "gold_table",
}

func getGoldTableNames(snapshotDate string) map[string]string {
tableNames := make(map[string]string)
for key, baseName := range goldTableBaseNames {
tableNames[key] = formatTableName(baseName, snapshotDate)
}
return tableNames
return rewardsUtils.GetGoldTableNames(snapshotDate)
}

func renderQueryTemplate(query string, variables map[string]string) (string, error) {
Expand Down
11 changes: 10 additions & 1 deletion pkg/rewards/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/Layr-Labs/sidecar/pkg/postgres"
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
"github.com/Layr-Labs/sidecar/pkg/utils"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
Expand Down Expand Up @@ -94,6 +95,8 @@ func setupRewards() (
error,
) {
cfg := tests.GetConfig()
cfg.Rewards.GenerateStakerOperatorsTable = true
cfg.Rewards.ValidateRewardsRoot = true
cfg.Chain = config.Chain_Mainnet

cfg.DatabaseConfig = *tests.GetDbConfigFromEnv()
Expand Down Expand Up @@ -128,8 +131,10 @@ func Test_Rewards(t *testing.T) {
// t.Fatal(err)
// }

sog := stakerOperators.NewStakerOperatorGenerator(grm, l, cfg)

t.Run("Should initialize the rewards calculator", func(t *testing.T) {
rc, err := NewRewardsCalculator(cfg, grm, nil, l)
rc, err := NewRewardsCalculator(cfg, grm, nil, sog, l)
assert.Nil(t, err)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -312,6 +317,10 @@ func Test_Rewards(t *testing.T) {
t.Fatalf("Invalid amounts: %d", invalidAmounts)
}

t.Logf("Generating staker operators table")
err = rc.sog.GenerateStakerOperatorsTable(snapshotDate)
assert.Nil(t, err)

accountTree, _, err := rc.MerkelizeRewardsForSnapshot(snapshotDate)
assert.Nil(t, err)

Expand Down
4 changes: 3 additions & 1 deletion pkg/rewards/stakerDelegationSnapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Layr-Labs/sidecar/internal/logger"
"github.com/Layr-Labs/sidecar/internal/tests"
"github.com/Layr-Labs/sidecar/pkg/postgres"
"github.com/Layr-Labs/sidecar/pkg/rewards/stakerOperators"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"gorm.io/gorm"
Expand Down Expand Up @@ -100,7 +101,8 @@ func Test_StakerDelegationSnapshots(t *testing.T) {
}
})
t.Run("Should generate staker share snapshots", func(t *testing.T) {
rewards, _ := NewRewardsCalculator(cfg, grm, nil, l)
sog := stakerOperators.NewStakerOperatorGenerator(grm, l, cfg)
rewards, _ := NewRewardsCalculator(cfg, grm, nil, sog, l)

t.Log("Generating staker delegation snapshots")
err = rewards.GenerateAndInsertStakerDelegationSnapshots(snapshotDate)
Expand Down
Loading

0 comments on commit 8cbaf97

Please sign in to comment.