From 8e9f0da2981f52d9205de9ab96a595bc131a748d Mon Sep 17 00:00:00 2001 From: spletka Date: Wed, 18 Oct 2023 12:19:55 +0200 Subject: [PATCH] (BIDS-2548) Refactor code --- db/db.go | 130 ++++++++++++++++++++++++++----------------------------- 1 file changed, 62 insertions(+), 68 deletions(-) diff --git a/db/db.go b/db/db.go index 4a4abbf436..702dbe0be3 100644 --- a/db/db.go +++ b/db/db.go @@ -169,7 +169,6 @@ func GetEth1DepositsJoinEth2Deposits(query string, length, start uint64, orderBy } var param interface{} - shouldSearch := true var searchQuery string var err error @@ -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 @@ -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 @@ -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 @@ -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 @@ -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