@@ -2394,16 +2394,45 @@ func GetValidatorsWithdrawalsByEpoch(validator []uint64, startEpoch uint64, endE
2394
2394
func GetAddressWithdrawalsTotal (address []byte ) (uint64 , error ) {
2395
2395
var total uint64
2396
2396
2397
- err := ReaderDb .Get (& total , `
2398
- /*+
2399
- BitmapScan(w)
2400
- NestLoop(b w)
2401
- */
2402
- SELECT
2403
- COALESCE(sum(w.amount), 0) as total
2404
- FROM blocks_withdrawals w
2405
- INNER JOIN blocks b ON b.blockroot = w.block_root AND b.status = '1'
2406
- WHERE w.address = $1` , address )
2397
+ tx , err := ReaderDb .Beginx ()
2398
+ if err != nil {
2399
+ return total , err
2400
+ }
2401
+ defer tx .Rollback ()
2402
+
2403
+ maxDay := uint64 (0 )
2404
+ err = tx .Get (& maxDay , `SELECT COALESCE(MAX(day), 0) FROM validator_stats` )
2405
+ if err != nil {
2406
+ return total , err
2407
+ }
2408
+
2409
+ if len (address ) != 20 {
2410
+ return 0 , fmt .Errorf ("invalid address length (!=20): %d" , len (address ))
2411
+ }
2412
+
2413
+ withdrawalCredentials := make ([]byte , 32 )
2414
+ withdrawalCredentials [0 ] = 0x01
2415
+ copy (withdrawalCredentials [12 :], address )
2416
+
2417
+ slotThreshold := (maxDay + 1 ) * utils .Config .Chain .ClConfig .SlotsPerEpoch * utils .EpochsPerDay ()
2418
+
2419
+ err = tx .Get (& total , `
2420
+ WITH
2421
+ stats AS (
2422
+ SELECT coalesce(sum(vs.withdrawals_amount_total),0) AS total
2423
+ FROM vars, validators v
2424
+ INNER JOIN validator_stats vs ON v.validatorindex = vs.validatorindex
2425
+ WHERE v.withdrawalcredentials = $1 AND day = $2
2426
+ ),
2427
+ today as (
2428
+ SELECT COALESCE(sum(w.amount), 0) AS total
2429
+ FROM vars, blocks_withdrawals w
2430
+ INNER JOIN blocks b ON b.slot > $4 AND b.blockroot = w.block_root AND b.status = '1'
2431
+ WHERE w.address = $3
2432
+ )
2433
+ SELECT stats.total + today.total
2434
+ FROM stats, today` ,
2435
+ withdrawalCredentials , maxDay , address , slotThreshold )
2407
2436
if err != nil {
2408
2437
if err == sql .ErrNoRows {
2409
2438
return 0 , nil
0 commit comments