Skip to content

Commit

Permalink
(BIDS-2261) handle genesis deposits correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbitfly committed Sep 26, 2023
1 parent 3a7061e commit c307d41
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
59 changes: 42 additions & 17 deletions db/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func WriteValidatorStatisticsForDay(day uint64) error {
return fmt.Errorf("error retrieving exported state: %w", err)
}

if exported.Status {
logger.Infof("Skipping day %v as it is already exported", day)
return nil
}

previousDayExported := Exported{}
err = WriterDb.Get(&previousDayExported, `
SELECT
Expand Down Expand Up @@ -89,12 +94,6 @@ func WriteValidatorStatisticsForDay(day uint64) error {
})
}

// temporarily disabled for debugging
// if exported.Status {
// logger.Infof("Skipping day %v as it is already exported", day)
// return nil
// }

g := &errgroup.Group{}

g.Go(func() error {
Expand Down Expand Up @@ -137,7 +136,7 @@ func WriteValidatorStatisticsForDay(day uint64) error {
var previousDayStatisticsData []*types.ValidatorStatsTableDbRow
g.Go(func() error {
var err error
previousDayStatisticsData, err = gatherPreviousDayStatisticsData(int64(day)) // convert to int64 to avoid underflows
previousDayStatisticsData, err = gatherStatisticsForDay(int64(day) - 1) // convert to int64 to avoid underflows
if err != nil {
return fmt.Errorf("error in GatherPreviousDayStatisticsData: %w", err)
}
Expand All @@ -153,7 +152,7 @@ func WriteValidatorStatisticsForDay(day uint64) error {
for index, data := range validatorData {

previousDayData := &types.ValidatorStatsTableDbRow{
ValidatorIndex: uint64(index),
ValidatorIndex: uint64(data.ValidatorIndex),
EndBalance: data.StartBalance, // special case for handling day 0
}

Expand All @@ -162,7 +161,7 @@ func WriteValidatorStatisticsForDay(day uint64) error {
}

if data.ValidatorIndex != previousDayData.ValidatorIndex {
return fmt.Errorf("logic error when retrieving previous day data for validator %v (%v wanted, %v retrieved)", index, index, previousDayData.ValidatorIndex)
return fmt.Errorf("logic error when retrieving previous day data for validator %v (%v wanted, %v retrieved)", index, data.ValidatorIndex, previousDayData.ValidatorIndex)
}

// update attestation totals
Expand All @@ -177,6 +176,10 @@ func WriteValidatorStatisticsForDay(day uint64) error {
data.ClRewardsGWei = data.EndBalance - previousDayData.EndBalance + data.WithdrawalsAmount - data.DepositsAmount
data.ClRewardsGWeiTotal = previousDayData.ClRewardsGWeiTotal + data.ClRewardsGWei

if day == 0 { // special case for deposits included in the genesis block
data.DepositsAmount = data.DepositsAmount + data.GenesisDepositsAmount
}

// update el reward total
data.ElRewardsWeiTotal = previousDayData.ElRewardsWeiTotal.Add(data.ElRewardsWei)

Expand Down Expand Up @@ -483,7 +486,7 @@ func gatherValidatorBlockStats(day uint64, data []*types.ValidatorStatsTableDbRo
;`,
firstEpoch, lastEpoch, MaxSqlInteger)
if err != nil {
return fmt.Errorf("error inserting blocks into validator_stats for day [%v], firstEpoch [%v] and lastEpoch [%v]: %w", day, firstEpoch, lastEpoch, err)
return fmt.Errorf("error retrieving blocks for day [%v], firstEpoch [%v] and lastEpoch [%v]: %w", day, firstEpoch, lastEpoch, err)
}

mux.Lock()
Expand All @@ -510,7 +513,7 @@ func gatherValidatorBlockStats(day uint64, data []*types.ValidatorStatsTableDbRo
`,
firstEpoch, lastEpoch, MaxSqlInteger)
if err != nil {
return fmt.Errorf("error inserting slashings into validator_stats for day [%v], firstEpoch [%v] and lastEpoch [%v]: %w", day, firstEpoch, lastEpoch, err)
return fmt.Errorf("error retrieving slashings for day [%v], firstEpoch [%v] and lastEpoch [%v]: %w", day, firstEpoch, lastEpoch, err)
}

mux.Lock()
Expand Down Expand Up @@ -677,12 +680,12 @@ func gatherValidatorDepositWithdrawals(day uint64, data []*types.ValidatorStatsT
from blocks_deposits
inner join validators on blocks_deposits.publickey = validators.pubkey
inner join blocks on blocks_deposits.block_root = blocks.blockroot
where blocks.slot >= $1 and blocks.slot <= $2 and blocks.status = '1' and blocks_deposits.valid_signature
where blocks.slot > 0 and blocks.slot >= $1 and blocks.slot <= $2 and blocks.status = '1' and blocks_deposits.valid_signature
group by validators.validatorindex`

err := WriterDb.Select(&resDeposits, depositsQry, firstSlot, lastSlot)
if err != nil {
return fmt.Errorf("error inserting deposits into validator_stats for day [%v], firstSlot [%v] and lastSlot [%v]: %w", day, firstSlot, lastSlot, err)
return fmt.Errorf("error retrieving deposits for day [%v], firstSlot [%v] and lastSlot [%v]: %w", day, firstSlot, lastSlot, err)
}

mux.Lock()
Expand All @@ -692,6 +695,28 @@ func gatherValidatorDepositWithdrawals(day uint64, data []*types.ValidatorStatsT
}
mux.Unlock()

if day == 0 {
resGenesisDeposits := make([]*resRowDeposits, 0, 1024)
genesisDepositsQry := `
select validators.validatorindex, count(*) AS deposits, sum(amount) AS deposits_amount
from blocks_deposits
inner join validators on blocks_deposits.publickey = validators.pubkey
inner join blocks on blocks_deposits.block_root = blocks.blockroot
where blocks.slot = 0 and blocks_deposits.valid_signature
group by validators.validatorindex`

err := WriterDb.Select(&resGenesisDeposits, genesisDepositsQry)
if err != nil {
return fmt.Errorf("error retrieving genesis deposits for day [%v]: %w", day, err)
}

mux.Lock()
for _, r := range resGenesisDeposits {
data[r.ValidatorIndex].GenesisDepositsAmount = int64(r.DepositsAmount)
}
mux.Unlock()
}

type resRowWithdrawals struct {
ValidatorIndex uint64 `db:"validatorindex"`
Withdrawals uint64 `db:"withdrawals"`
Expand All @@ -706,7 +731,7 @@ func gatherValidatorDepositWithdrawals(day uint64, data []*types.ValidatorStatsT
group by validatorindex;`
err = WriterDb.Select(&resWithdrawals, withdrawalsQuery, firstSlot, lastSlot)
if err != nil {
return fmt.Errorf("error inserting withdrawals into validator_stats for day [%v], firstSlot [%v] and lastSlot [%v]: %w", day, firstSlot, lastSlot, err)
return fmt.Errorf("error retrieving withdrawals for day [%v], firstSlot [%v] and lastSlot [%v]: %w", day, firstSlot, lastSlot, err)
}

mux.Lock()
Expand Down Expand Up @@ -802,7 +827,7 @@ func gatherValidatorFailedAttestationsStatisticsForDay(validators []uint64, day
return nil
}

func gatherPreviousDayStatisticsData(day int64) ([]*types.ValidatorStatsTableDbRow, error) {
func gatherStatisticsForDay(day int64) ([]*types.ValidatorStatsTableDbRow, error) {
ret := make([]*types.ValidatorStatsTableDbRow, 0)

err := WriterDb.Select(&ret, `SELECT
Expand Down Expand Up @@ -841,10 +866,10 @@ func gatherPreviousDayStatisticsData(day int64) ([]*types.ValidatorStatsTableDbR
COALESCE(mev_rewards_wei, 0) AS mev_rewards_wei,
COALESCE(mev_rewards_wei_total, 0) AS mev_rewards_wei_total
from validator_stats WHERE day = $1 ORDER BY validatorindex
`, day-1)
`, day)

if err != nil {
return nil, fmt.Errorf("error retreiving previous day statistics data: %w", err)
return nil, fmt.Errorf("error statistics for day %v data: %w", day, err)
}

return ret, nil
Expand Down
5 changes: 3 additions & 2 deletions types/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,9 @@ type ValidatorStatsTableDbRow struct {
AttesterSlashings int64 `db:"attester_slashings"`
ProposerSlashing int64 `db:"proposer_slashings"`

Deposits int64 `db:"deposits"`
DepositsAmount int64 `db:"deposits_amount"`
Deposits int64 `db:"deposits"`
DepositsAmount int64 `db:"deposits_amount"`
GenesisDepositsAmount int64 `db:"-"`

Withdrawals int64 `db:"withdrawals"`
WithdrawalsAmount int64 `db:"withdrawals_amount"`
Expand Down

0 comments on commit c307d41

Please sign in to comment.