From eb0ecfb6844e110dcc78e713eb2d946443281171 Mon Sep 17 00:00:00 2001 From: Patrick Pfeiffer <306324+guybrush@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:12:00 +0100 Subject: [PATCH] fix(v1): fix db.GetEth2Deposits when searching --- db/db.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/db/db.go b/db/db.go index e47800d0cf..d47c17e2c9 100644 --- a/db/db.go +++ b/db/db.go @@ -387,11 +387,9 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin var err error // Define the base queries - deposistsCountQuery := ` - select sum(depositscount) from blocks where status = '1' and depositscount > 0 - %s` + depositsCountQuery := `SELECT SUM(depositscount) FROM blocks WHERE status = '1' AND depositscount > 0` - deposistsQuery := ` + depositsQuery := ` SELECT blocks_deposits.block_slot, blocks_deposits.block_index, @@ -417,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 } @@ -433,14 +431,29 @@ 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` @@ -449,15 +462,12 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin 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 }