Skip to content

Commit d41dc7e

Browse files
authored
fix: use v2-pectra-request-tables in statistics (#1588)
Co-authored-by: Patrick Pfeiffer <306324+guybrush@users.noreply.github.com>
1 parent bfe6b89 commit d41dc7e

File tree

1 file changed

+61
-38
lines changed

1 file changed

+61
-38
lines changed

backend/pkg/commons/db/statistics.go

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ func WriteValidatorStatisticsForDay(day uint64, client rpc.Client) error {
3535

3636
firstEpoch, lastEpoch := utils.GetFirstAndLastEpochForDay(day)
3737

38+
var err error
39+
40+
clEventsIndexerEpoch := int64(-1)
41+
err = WriterDb.Get(&clEventsIndexerEpoch, "SELECT COALESCE(MAX(epoch), -1) FROM consensus_layer_events_indexer_metadata")
42+
if err != nil {
43+
return err
44+
}
45+
if clEventsIndexerEpoch < int64(lastEpoch) {
46+
return fmt.Errorf("consensusLayerEventsIndexer lagging behind, day: %v, lastEpoch: %d, indexerEpoch: %d", day, lastEpoch, clEventsIndexerEpoch)
47+
}
48+
3849
log.Infof("exporting statistics for day %v (epoch %v to %v)", day, firstEpoch, lastEpoch)
3950

4051
if err := CheckIfDayIsFinalized(day); err != nil {
@@ -47,7 +58,7 @@ func WriteValidatorStatisticsForDay(day uint64, client rpc.Client) error {
4758
Status bool `db:"status"`
4859
}
4960
exported := Exported{}
50-
err := WriterDb.Get(&exported, `
61+
err = WriterDb.Get(&exported, `
5162
SELECT
5263
status
5364
FROM validator_stats_status
@@ -841,34 +852,42 @@ func gatherValidatorDepositWithdrawals(day uint64, data []*types.ValidatorStatsT
841852
depositsQry := `
842853
with
843854
first_valid_deposits as (
844-
select distinct on (publickey)
845-
publickey,
846-
block_slot,
847-
block_index
855+
select distinct on (blocks_deposits.publickey)
856+
blocks_deposits.publickey,
857+
blocks_deposits.block_slot,
858+
blocks_deposits.block_index
848859
from
849860
blocks_deposits
861+
inner join blocks on
862+
blocks_deposits.block_root = blocks.blockroot and (blocks.status = '1' or blocks.slot = 0)
850863
where
851-
valid_signature
852-
and publickey in (select bd.publickey from blocks_deposits bd where bd.valid_signature and bd.block_slot >= $1 and bd.block_slot <= $2)
864+
-- filter out relevant requests for performance-reasons, no need to check for validity
865+
blocks_deposits.publickey in (select bd.publickey from blocks_deposits bd where bd.block_slot >= $1 and bd.block_slot <= $2)
866+
and blocks_deposits.valid_signature
853867
order by
854-
publickey,
855-
block_slot,
856-
block_index
868+
blocks_deposits.publickey,
869+
blocks_deposits.block_slot,
870+
blocks_deposits.block_index
857871
),
858872
first_valid_deposit_requests as (
859873
select
860-
distinct on (pubkey)
861-
pubkey,
862-
block_slot,
863-
request_index
874+
distinct on (blocks_deposit_requests_v2.pubkey)
875+
blocks_deposit_requests_v2.pubkey,
876+
blocks_deposit_requests_v2.slot_processed as block_slot,
877+
blocks_deposit_requests_v2.index_processed as request_index
864878
from
865-
blocks_deposit_requests
879+
blocks_deposit_requests_v2
880+
inner join blocks on
881+
blocks_deposit_requests_v2.block_processed_root = blocks.blockroot and (blocks.status = '1' or blocks.slot = 0)
866882
where
867-
pubkey in (select bdr.pubkey from blocks_deposit_requests bdr where bdr.block_slot >= $1 and bdr.block_slot <= $2)
883+
-- filter out relevant requests for performance-reasons, no need to check for validity
884+
blocks_deposit_requests_v2.pubkey in (select bdr.pubkey from blocks_deposit_requests_v2 bdr where bdr.slot_processed >= $1 and bdr.slot_processed <= $2)
885+
and blocks_deposit_requests_v2.slot_processed is not null
886+
and blocks_deposit_requests_v2.status = 'completed'
868887
order by
869-
pubkey,
870-
block_slot,
871-
request_index
888+
blocks_deposit_requests_v2.pubkey,
889+
blocks_deposit_requests_v2.slot_processed,
890+
blocks_deposit_requests_v2.index_processed
872891
),
873892
deposits as (
874893
select
@@ -884,11 +903,10 @@ func gatherValidatorDepositWithdrawals(day uint64, data []*types.ValidatorStatsT
884903
inner join first_valid_deposits on
885904
blocks_deposits.publickey = first_valid_deposits.publickey
886905
where
887-
blocks_deposits.valid_signature
888-
and blocks.slot >= $1
906+
blocks.slot >= $1
889907
and blocks.slot <= $2
890-
and (blocks.status = '1'
891-
or blocks.slot = 0)
908+
and (blocks.status = '1' or blocks.slot = 0)
909+
and first_valid_deposits.block_slot is not null
892910
and (blocks_deposits.block_slot > first_valid_deposits.block_slot -- any slot after the first valid deposit
893911
or (blocks_deposits.block_slot = first_valid_deposits.block_slot -- or the same slot but a equal or higher block-index
894912
and blocks_deposits.block_index >= first_valid_deposits.block_index)
@@ -902,27 +920,27 @@ func gatherValidatorDepositWithdrawals(day uint64, data []*types.ValidatorStatsT
902920
count(*) as deposits,
903921
sum(amount) as deposits_amount
904922
from
905-
blocks_deposit_requests bdr
923+
blocks_deposit_requests_v2 bdr
906924
inner join validators on
907925
bdr.pubkey = validators.pubkey
908926
inner join blocks on
909-
bdr.block_root = blocks.blockroot
927+
bdr.block_processed_root = blocks.blockroot
910928
left join first_valid_deposits on
911929
bdr.pubkey = first_valid_deposits.publickey
912930
left join first_valid_deposit_requests on
913931
bdr.pubkey = first_valid_deposit_requests.pubkey
914932
where
915-
blocks.slot >= $1
933+
bdr.status = 'completed'
934+
and blocks.slot >= $1
916935
and blocks.slot <= $2
917-
and (blocks.status = '1'
918-
or blocks.slot = 0)
936+
and (blocks.status = '1' or blocks.slot = 0)
919937
and (
920938
(first_valid_deposits.block_slot is not null
921-
and bdr.block_slot > first_valid_deposits.block_slot) -- any slot after the first valid deposit
939+
and bdr.slot_processed > first_valid_deposits.block_slot) -- any slot after the first valid deposit
922940
or (first_valid_deposit_requests.block_slot is not null
923-
and (bdr.block_slot > first_valid_deposit_requests.block_slot -- or any slot after the first valid deposit request
924-
or (bdr.block_slot = first_valid_deposit_requests.block_slot -- or the same slot but a equal or higher index
925-
and bdr.request_index >= first_valid_deposit_requests.request_index)
941+
and (bdr.slot_processed > first_valid_deposit_requests.block_slot -- or any slot after the first valid deposit request
942+
or (bdr.slot_processed = first_valid_deposit_requests.block_slot -- or the same slot but a equal or higher index
943+
and bdr.index_processed >= first_valid_deposit_requests.request_index)
926944
)
927945
)
928946
)
@@ -1216,12 +1234,17 @@ func gatherValidatorConsolidations(day uint64, data []*types.ValidatorStatsTable
12161234
}
12171235
err := ReaderDb.Select(&consolidationData, `
12181236
SELECT
1219-
source_index,
1220-
target_index,
1221-
COALESCE(amount_consolidated, 0) AS amount_consolidated
1222-
FROM blocks_consolidation_requests
1223-
INNER JOIN blocks ON blocks_consolidation_requests.block_root = blocks.blockroot AND blocks.status = '1'
1224-
WHERE block_slot >= $1 AND block_slot <= $2
1237+
v_source.validatorindex AS source_index,
1238+
v_target.validatorindex AS target_index,
1239+
COALESCE(blocks_consolidation_requests_v2.amount_consolidated, 0) AS amount_consolidated
1240+
FROM blocks_consolidation_requests_v2
1241+
INNER JOIN blocks ON blocks_consolidation_requests_v2.block_processed_root = blocks.blockroot AND blocks.status = '1'
1242+
INNER JOIN validators v_source ON v_source.pubkey = blocks_consolidation_requests_v2.source_pubkey
1243+
INNER JOIN validators v_target ON v_target.pubkey = blocks_consolidation_requests_v2.target_pubkey
1244+
WHERE blocks_consolidation_requests_v2.slot_processed IS NOT NULL
1245+
AND blocks_consolidation_requests_v2.slot_processed >= $1
1246+
AND blocks_consolidation_requests_v2.slot_processed <= $2
1247+
AND blocks_consolidation_requests_v2.status = 'completed'
12251248
`, firstSlot, lastSlot)
12261249
if err != nil {
12271250
return fmt.Errorf("error retrieving consolidation data for day [%v], firstSlot [%v] and lastSlot [%v]: %w", day, firstSlot, lastSlot, err)

0 commit comments

Comments
 (0)