Skip to content

Commit

Permalink
fix(v1): fix db.GetEth2Deposits when searching
Browse files Browse the repository at this point in the history
  • Loading branch information
guybrush committed Dec 9, 2024
1 parent d4eefe0 commit eb23daa
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand All @@ -433,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

0 comments on commit eb23daa

Please sign in to comment.