Skip to content

Commit

Permalink
feat: slashing testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gpsanant committed Oct 29, 2024
1 parent 752d11a commit 1a73ef5
Show file tree
Hide file tree
Showing 2 changed files with 315 additions and 26 deletions.
73 changes: 48 additions & 25 deletions internal/eigenState/stakerShares/stakerShares.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,14 @@ func (ss *StakerSharesModel) handleSlashingQueuedWithdrawal(log *storage.Transac
}

type operatorSlashedOutputData struct {
Operator string `json:"operator"`
Operator string `json:"operator"`
// OperatorSet struct {
// Avs string `json:"avs"`
// OperatorSetId uint32 `json:"operatorSetId"`
// }
Strategies []string `json:"strategies"`
WadsSlashed []json.Number `json:"wadsSlashed"`
// Description string `json:"description"`
}

func parseLogOutputForOperatorSlashedEvent(outputDataStr string) (*operatorSlashedOutputData, error) {
Expand Down Expand Up @@ -615,9 +620,9 @@ func (ss *StakerSharesModel) prepareState(blockNumber uint64) ([]*ShareDiff, err
switch diff.Event.(type) {
case *ShareDiff:
shareDiff := diff.Event.(*ShareDiff)
slotId := NewShareDiffSlotID(shareDiff.Staker, shareDiff.Strategy)
slotID := NewShareDiffSlotID(shareDiff.Staker, shareDiff.Strategy)

currStakerShares, ok := updatedStakerShares[slotId]
currStakerShares, ok := updatedStakerShares[slotID]
if !ok {
stakerShares := &StakerShares{}
// get the record from the database with the given staker and strategy with the latest blockNumber
Expand Down Expand Up @@ -648,7 +653,10 @@ func (ss *StakerSharesModel) prepareState(blockNumber uint64) ([]*ShareDiff, err
currStakerShares.Shares = currStakerShares.Shares.Add(currStakerShares.Shares, shareDiff.Shares)

// update the local cache
updatedStakerShares[slotId] = currStakerShares
updatedStakerShares[slotID] = currStakerShares

ss.logger.Sugar().Infow("Updating staker shares with share diff", zap.String("staker", currStakerShares.Staker), zap.String("strategy", currStakerShares.Strategy), zap.String("shares", currStakerShares.Shares.String()), zap.Uint64("blockNumber", blockNumber))

case *SlashDiff:
slashDiff := diff.Event.(*SlashDiff)
query := `
Expand All @@ -660,72 +668,87 @@ func (ss *StakerSharesModel) prepareState(blockNumber uint64) ([]*ShareDiff, err
ROW_NUMBER() OVER (PARTITION BY staker ORDER BY block_number desc, log_index desc) as rn
from staker_delegation_changes
where
block_number <= @blockNumber
block_number <= @blockNumber and
log_index <= @logIndex
),
delegated_stakers as (
select
lsd.staker
from latest_staker_delegations as lsd
from ranked_staker_delegations as lsd
where
lsd.operator = @operator
and lsd.rn = 1
),
ranked_staker_shares as (
select
ss.staker,
ds.staker,
ss.strategy,
ss.shares,
ss.block_number,
ROW_NUMBER() OVER (PARTITION BY ss.staker, ss.strategy ORDER BY ss.block_number desc) as rn
from staker_shares as ss
join delegated_stakers as ds
on ss.staker = ds.staker
COALESCE(ss.shares, 0) as shares,
COALESCE(ss.block_number, 0) as block_number, -- Assign a default block number if needed
ROW_NUMBER() OVER (
PARTITION BY ds.staker, ss.strategy
ORDER BY ss.block_number desc
) AS rn
from
delegated_stakers as ds
left join
staker_shares as ss
on ss.staker = ds.staker
and ss.strategy = @strategy
),
latest_staker_shares as (
select
ls.staker,
ls.strategy,
ls.shares,
ls.block_number
from latest_staker_shares as ls
from ranked_staker_shares as ls
where
ls.rn = 1
)
select * from latest_staker_shares
`
stakerShares := make([]StakerShares, 0)

// get the staker shares for the stakers who were delegated to the operator
// and update the shares with the new max magnitude
res := ss.DB.Model(&StakerShares{}).
Raw(query,
sql.Named("blockNumber", slashDiff.BlockNumber),
sql.Named("logIndex", slashDiff.LogIndex),
sql.Named("operator", slashDiff.Operator),
).Scan(&updatedStakerShares)
if res.Error != nil {
res := ss.DB.Raw(query,
sql.Named("blockNumber", slashDiff.BlockNumber),
sql.Named("logIndex", slashDiff.LogIndex),
sql.Named("operator", slashDiff.Operator),
sql.Named("strategy", slashDiff.Strategy),
).Scan(&stakerShares)
if res.Error != nil && res.Error != gorm.ErrRecordNotFound {
ss.logger.Sugar().Errorw("Failed to fetch staker_shares", zap.Error(res.Error))
return nil, res.Error
}

wadsLeft := new(big.Int).Sub(WAD, slashDiff.WadsSlashed)

ss.logger.Info("Updating staker shares with slash diff", zap.String("len", fmt.Sprint(len(stakerShares))))

for _, stakerSharesRecord := range stakerShares {
slotID := NewShareDiffSlotID(stakerSharesRecord.Staker, stakerSharesRecord.Strategy)
slotID := NewShareDiffSlotID(stakerSharesRecord.Staker, slashDiff.Strategy)
var err error
currStakerShares, ok := updatedStakerShares[slotID]
if !ok {
// if we have not cached but the staker exists in the database for the strategy
if !ok && stakerSharesRecord.Strategy == slashDiff.Strategy {
currStakerShares, err = stakerSharesRecord.ToShareDiff()
if err != nil {
return nil, err
}
} else if !ok {
// otherwise the staker is for a different strategy, so we skip
continue
}

// update the shares with wadsLeft
currStakerShares.Shares = new(big.Int).Div(new(big.Int).Mul(currStakerShares.Shares, wadsLeft), WAD)
currStakerShares.BlockNumber = blockNumber

// update the local cache
updatedStakerShares[slotID] = currStakerShares

ss.logger.Sugar().Infow("Updating staker shares with slash diff", zap.String("staker", currStakerShares.Staker), zap.String("strategy", currStakerShares.Strategy), zap.String("shares", currStakerShares.Shares.String()), zap.Uint64("blockNumber", stakerSharesRecord.BlockNumber))
}
default:
}
Expand Down
Loading

0 comments on commit 1a73ef5

Please sign in to comment.