Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v1): improve deposit-count-qry #2984

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,9 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin
var err error

// Define the base queries
deposistsCountQuery := `
SELECT COUNT(*)
FROM blocks_deposits
INNER JOIN blocks ON blocks_deposits.block_root = blocks.blockroot AND blocks.status = '1'
%s`
depositsCountQuery := `SELECT SUM(depositscount) FROM blocks WHERE status = '1' AND depositscount > 0`

deposistsQuery := `
depositsQuery := `
SELECT
blocks_deposits.block_slot,
blocks_deposits.block_index,
Expand All @@ -419,12 +415,12 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin
}
}
if trimmedQuery == "" {
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, ""))
err = ReaderDb.Get(&totalCount, depositsCountQuery)
if err != nil {
return nil, 0, err
}

err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, "", orderBy, orderDir), length, start)
err = ReaderDb.Select(&deposits, fmt.Sprintf(depositsQuery, "", orderBy, orderDir), length, start)
if err != nil && err != sql.ErrNoRows {
return nil, 0, err
}
Expand All @@ -435,31 +431,47 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin
if utils.IsHash(trimmedQuery) {
param = hash
searchQuery = `WHERE blocks_deposits.publickey = $3`
depositsCountQuery = `
SELECT SUM(depositscount)
FROM blocks
INNER JOIN blocks_deposits ON blocks.blockroot = blocks_deposits.block_root AND blocks_deposits.publickey = $1
WHERE status = '1' AND depositscount > 0`
} else if utils.IsValidWithdrawalCredentials(trimmedQuery) {
param = hash
searchQuery = `WHERE blocks_deposits.withdrawalcredentials = $3`
depositsCountQuery = `
SELECT SUM(depositscount)
FROM blocks
INNER JOIN blocks_deposits ON blocks.blockroot = blocks_deposits.block_root AND blocks_deposits.withdrawalcredentials = $1
WHERE status = '1' AND depositscount > 0`
} else if utils.IsEth1Address(trimmedQuery) {
param = hash
searchQuery = `
LEFT JOIN eth1_deposits ON blocks_deposits.publickey = eth1_deposits.publickey
WHERE eth1_deposits.from_address = $3`
depositsCountQuery = `
SELECT SUM(depositscount)
FROM blocks
INNER JOIN blocks_deposits ON blocks.blockroot = blocks_deposits.block_root AND blocks_deposits.from_address = $1
WHERE status = '1' AND depositscount > 0`
} else if uiQuery, parseErr := strconv.ParseUint(query, 10, 31); parseErr == nil { // Limit to 31 bits to stay within math.MaxInt32
param = uiQuery
searchQuery = `WHERE blocks_deposits.block_slot = $3`
depositsCountQuery = `
SELECT SUM(depositscount)
FROM blocks
WHERE status = '1' AND depositscount > 0 AND slot = $1`
} else {
// The query does not fulfill any of the requirements for a search
return deposits, totalCount, nil
}

// The deposits count query only has one parameter for the search
countSearchQuery := strings.ReplaceAll(searchQuery, "$3", "$1")

err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, countSearchQuery), param)
err = ReaderDb.Get(&totalCount, depositsCountQuery, param)
if err != nil {
return nil, 0, err
}

err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start, param)
err = ReaderDb.Select(&deposits, fmt.Sprintf(depositsQuery, searchQuery, orderBy, orderDir), length, start, param)
if err != nil && err != sql.ErrNoRows {
return nil, 0, err
}
Expand Down Expand Up @@ -2249,10 +2261,9 @@ func GetTotalAmountWithdrawn() (sum uint64, count uint64, err error) {
func GetTotalAmountDeposited() (uint64, error) {
var total uint64
err := ReaderDb.Get(&total, `
SELECT
COALESCE(sum(d.amount), 0) as sum
FROM blocks_deposits d
INNER JOIN blocks b ON b.blockroot = d.block_root AND b.status = '1'`)
SELECT COALESCE(sum(d.amount), 0) as sum
FROM blocks_deposits d
INNER JOIN blocks b ON b.slot = d.block_slot AND b.blockroot = d.block_root WHERE b.status = '1' AND b.depositscount > 0;`)
return total, err
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- +goose Up
-- +goose StatementBegin
SELECT 'creating idx_blocks_status_depositscountgt0';
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_blocks_status_depositscountgt0 ON blocks (status, (depositscount > 0)) where depositscount > 0;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
SELECT 'dropping idx_blocks_status_depositscountgt0';
DROP INDEX CONCURRENTLY IF EXISTS idx_blocks_status_depositscountgt0;
-- +goose StatementEnd
39 changes: 19 additions & 20 deletions services/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,26 +249,25 @@ func startServicesMonitoringService() {
firstRun := true

servicesToCheck := map[string]time.Duration{
"eth1indexer": time.Minute * 15,
"slotVizUpdater": time.Minute * 15,
"slotUpdater": time.Minute * 15,
"latestProposedSlotUpdater": time.Minute * 15,
"epochUpdater": time.Minute * 15,
"rewardsExporter": time.Minute * 15,
"mempoolUpdater": time.Minute * 15,
"indexPageDataUpdater": time.Minute * 15,
"latestBlockUpdater": time.Minute * 15,
"headBlockRootHashUpdater": time.Minute * 15,
"notification-collector": time.Minute * 15,
"relaysUpdater": time.Minute * 15,
"ethstoreExporter": time.Minute * 60,
"statsUpdater": time.Minute * 30,
"poolsUpdater": time.Minute * 30,
"slotExporter": time.Minute * 15,
"statistics": time.Minute * 90,
"ethStoreStatistics": time.Minute * 15,
"lastExportedStatisticDay": time.Minute * 15,
"validatorStateCountsUpdater": time.Minute * 90,
"eth1indexer": time.Minute * 15,
"slotVizUpdater": time.Minute * 15,
"slotUpdater": time.Minute * 15,
"latestProposedSlotUpdater": time.Minute * 15,
"epochUpdater": time.Minute * 15,
"rewardsExporter": time.Minute * 15,
"mempoolUpdater": time.Minute * 15,
"indexPageDataUpdater": time.Minute * 15,
"latestBlockUpdater": time.Minute * 15,
"headBlockRootHashUpdater": time.Minute * 15,
"notification-collector": time.Minute * 15,
"relaysUpdater": time.Minute * 15,
"ethstoreExporter": time.Minute * 60,
"statsUpdater": time.Minute * 30,
"poolsUpdater": time.Minute * 30,
"slotExporter": time.Minute * 15,
"statistics": time.Minute * 90,
"ethStoreStatistics": time.Minute * 15,
"lastExportedStatisticDay": time.Minute * 15,
//"notification-sender", //exclude for now as the sender is only running on mainnet
}

Expand Down
Loading