Skip to content

Commit

Permalink
(BIDS-2548) Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Eisei24 committed Oct 18, 2023
1 parent 94f0698 commit 8e9f0da
Showing 1 changed file with 62 additions and 68 deletions.
130 changes: 62 additions & 68 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ func GetEth1DepositsJoinEth2Deposits(query string, length, start uint64, orderBy
}

var param interface{}
shouldSearch := true
var searchQuery string
var err error

Expand Down Expand Up @@ -216,30 +215,7 @@ func GetEth1DepositsJoinEth2Deposits(query string, length, start uint64, orderBy
return nil, 0, err
}
}
if trimmedQuery != "" {
param = hash
if utils.IsHash(trimmedQuery) {
searchQuery = `WHERE eth1.publickey = $3`
} else if utils.IsEth1Tx(trimmedQuery) {
// Withdrawal credentials have the same length as a tx hash
if utils.IsValidWithdrawalCredentials(trimmedQuery) {
searchQuery = `
WHERE
eth1.tx_hash = $3
OR eth1.withdrawal_credentials = $3`
} else {
searchQuery = `WHERE eth1.tx_hash = $3`
}
} else if utils.IsEth1Address(trimmedQuery) {
searchQuery = `WHERE eth1.from_address = $3`
} else if uiQuery, parseErr := strconv.ParseUint(query, 10, 31); parseErr == nil { // Limit to 31 bits to stay within math.MaxInt32
param = uiQuery
searchQuery = `WHERE eth1.block_number = $3`
} else {
// The query does not fulfill any of the requirements for a search
shouldSearch = false
}
} else {
if trimmedQuery == "" {
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, ""))
if err != nil {
return nil, 0, err
Expand All @@ -253,19 +229,40 @@ func GetEth1DepositsJoinEth2Deposits(query string, length, start uint64, orderBy
return deposits, totalCount, nil
}

if shouldSearch {
// 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)
if err != nil {
return nil, 0, err
param = hash
if utils.IsHash(trimmedQuery) {
searchQuery = `WHERE eth1.publickey = $3`
} else if utils.IsEth1Tx(trimmedQuery) {
// Withdrawal credentials have the same length as a tx hash
if utils.IsValidWithdrawalCredentials(trimmedQuery) {
searchQuery = `
WHERE
eth1.tx_hash = $3
OR eth1.withdrawal_credentials = $3`
} else {
searchQuery = `WHERE eth1.tx_hash = $3`
}
} else if utils.IsEth1Address(trimmedQuery) {
searchQuery = `WHERE eth1.from_address = $3`
} else if uiQuery, parseErr := strconv.ParseUint(query, 10, 31); parseErr == nil { // Limit to 31 bits to stay within math.MaxInt32
param = uiQuery
searchQuery = `WHERE eth1.block_number = $3`
} else {
// The query does not fulfill any of the requirements for a search
return deposits, totalCount, nil
}

err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start, param)
if err != nil && err != sql.ErrNoRows {
return nil, 0, err
}
// 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)
if err != nil {
return nil, 0, err
}

err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start, param)
if err != nil && err != sql.ErrNoRows {
return nil, 0, err
}

return deposits, totalCount, nil
Expand Down Expand Up @@ -354,7 +351,6 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin
}

var param interface{}
shouldSearch := true
var searchQuery string
var err error

Expand Down Expand Up @@ -390,26 +386,7 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin
return nil, 0, err
}
}
if trimmedQuery != "" {
if utils.IsHash(trimmedQuery) {
param = hash
searchQuery = `WHERE blocks_deposits.publickey = $3`
} else if utils.IsValidWithdrawalCredentials(trimmedQuery) {
param = hash
searchQuery = `WHERE blocks_deposits.withdrawalcredentials = $3`
} 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`
} 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`
} else {
// The query does not fulfill any of the requirements for a search
shouldSearch = false
}
} else {
if trimmedQuery == "" {
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, ""))
if err != nil {
return nil, 0, err
Expand All @@ -423,19 +400,36 @@ func GetEth2Deposits(query string, length, start uint64, orderBy, orderDir strin
return deposits, totalCount, nil
}

if shouldSearch {
// The deposits count query only has one parameter for the search
countSearchQuery := strings.ReplaceAll(searchQuery, "$3", "$1")
if utils.IsHash(trimmedQuery) {
param = hash
searchQuery = `WHERE blocks_deposits.publickey = $3`
} else if utils.IsValidWithdrawalCredentials(trimmedQuery) {
param = hash
searchQuery = `WHERE blocks_deposits.withdrawalcredentials = $3`
} 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`
} 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`
} else {
// The query does not fulfill any of the requirements for a search
return deposits, totalCount, nil
}

err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, countSearchQuery), param)
if err != nil {
return nil, 0, err
}
// The deposits count query only has one parameter for the search
countSearchQuery := strings.ReplaceAll(searchQuery, "$3", "$1")

err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start, param)
if err != nil && err != sql.ErrNoRows {
return nil, 0, err
}
err = ReaderDb.Get(&totalCount, fmt.Sprintf(deposistsCountQuery, countSearchQuery), param)
if err != nil {
return nil, 0, err
}

err = ReaderDb.Select(&deposits, fmt.Sprintf(deposistsQuery, searchQuery, orderBy, orderDir), length, start, param)
if err != nil && err != sql.ErrNoRows {
return nil, 0, err
}

return deposits, totalCount, nil
Expand Down

0 comments on commit 8e9f0da

Please sign in to comment.