Skip to content

Commit 8390f66

Browse files
committed
fix: improve deposit-count-query
1 parent 4314513 commit 8390f66

File tree

1 file changed

+27
-15
lines changed
  • backend/pkg/commons/db

1 file changed

+27
-15
lines changed

backend/pkg/commons/db/db.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,9 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin
452452
var err error
453453

454454
// Define the base queries
455-
deposistsCountQuery := `
456-
SELECT COUNT(*)
457-
FROM blocks_deposits
458-
INNER JOIN blocks ON blocks_deposits.block_root = blocks.blockroot AND blocks.status = '1'
459-
%s`
455+
depositsCountQuery := `SELECT SUM(depositscount) FROM blocks WHERE status = '1' AND depositscount > 0`
460456

461-
deposistsQuery := `
457+
depositsQuery := `
462458
SELECT
463459
blocks_deposits.block_slot,
464460
blocks_deposits.block_index,
@@ -484,12 +480,12 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin
484480
}
485481
}
486482
if trimmedQuery == "" {
487-
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, ""))
483+
err = ReaderDb.Get(&totalCount, depositsCountQuery)
488484
if err != nil {
489485
return nil, 0, err
490486
}
491487

492-
err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, "", orderBy, orderDir), length, start)
488+
err = ReaderDb.Select(&deposits, fmt.Sprintf(depositsQuery, "", orderBy, orderDir), length, start)
493489
if err != nil && err != sql.ErrNoRows {
494490
return nil, 0, err
495491
}
@@ -500,31 +496,47 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin
500496
if utils.IsHash(trimmedQuery) {
501497
param = hash
502498
searchQuery = `WHERE blocks_deposits.publickey = $3`
499+
depositsCountQuery = `
500+
SELECT SUM(depositscount)
501+
FROM blocks
502+
INNER JOIN blocks_deposits ON blocks.blockroot = blocks_deposits.block_root AND blocks_deposits.publickey = $1
503+
WHERE status = '1' AND depositscount > 0`
503504
} else if utils.IsValidWithdrawalCredentials(trimmedQuery) {
504505
param = hash
505506
searchQuery = `WHERE blocks_deposits.withdrawalcredentials = $3`
507+
depositsCountQuery = `
508+
SELECT SUM(depositscount)
509+
FROM blocks
510+
INNER JOIN blocks_deposits ON blocks.blockroot = blocks_deposits.block_root AND blocks_deposits.withdrawalcredentials = $1
511+
WHERE status = '1' AND depositscount > 0`
506512
} else if utils.IsEth1Address(trimmedQuery) {
507513
param = hash
508514
searchQuery = `
509-
LEFT JOIN eth1_deposits ON blocks_deposits.publickey = eth1_deposits.publickey
510-
WHERE eth1_deposits.from_address = $3`
515+
LEFT JOIN eth1_deposits ON blocks_deposits.publickey = eth1_deposits.publickey
516+
WHERE eth1_deposits.from_address = $3`
517+
depositsCountQuery = `
518+
SELECT SUM(depositscount)
519+
FROM blocks
520+
INNER JOIN blocks_deposits ON blocks.blockroot = blocks_deposits.block_root AND blocks_deposits.from_address = $1
521+
WHERE status = '1' AND depositscount > 0`
511522
} else if uiQuery, parseErr := strconv.ParseUint(query, 10, 31); parseErr == nil { // Limit to 31 bits to stay within math.MaxInt32
512523
param = uiQuery
513524
searchQuery = `WHERE blocks_deposits.block_slot = $3`
525+
depositsCountQuery = `
526+
SELECT SUM(depositscount)
527+
FROM blocks
528+
WHERE status = '1' AND depositscount > 0 AND slot = $1`
514529
} else {
515530
// The query does not fulfill any of the requirements for a search
516531
return deposits, totalCount, nil
517532
}
518533

519-
// The deposits count query only has one parameter for the search
520-
countSearchQuery := strings.ReplaceAll(searchQuery, "$3", "$1")
521-
522-
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, countSearchQuery), param)
534+
err = ReaderDb.Get(&totalCount, depositsCountQuery, param)
523535
if err != nil {
524536
return nil, 0, err
525537
}
526538

527-
err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start, param)
539+
err = ReaderDb.Select(&deposits, fmt.Sprintf(depositsQuery, searchQuery, orderBy, orderDir), length, start, param)
528540
if err != nil && err != sql.ErrNoRows {
529541
return nil, 0, err
530542
}

0 commit comments

Comments
 (0)