Skip to content

Commit

Permalink
Do not use formatted string in query for binary hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
boecklim committed Dec 14, 2023
1 parent e25644c commit e7ac1c2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 70 deletions.
66 changes: 34 additions & 32 deletions blocktx/store/sql/get_transaction_blocks.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package sql

import (
"database/sql"
"encoding/hex"
"fmt"
"strings"

"github.com/lib/pq"

"github.com/bitcoin-sv/arc/blocktx/blocktx_api"
"github.com/ordishs/gocore"

Expand All @@ -13,33 +16,24 @@ import (

const (
queryGetBlockHashHeightForTxHashesPostgres = `
SELECT
b.hash, b.height, t.hash
FROM blocks b
INNER JOIN block_transactions_map m ON m.blockid = b.id
INNER JOIN transactions t ON m.txid = t.id
WHERE t.hash in (%s)
AND b.orphanedyn = FALSE`
SELECT
b.hash, b.height, t.hash
FROM blocks b
INNER JOIN block_transactions_map m ON m.blockid = b.id
INNER JOIN transactions t ON m.txid = t.id
WHERE t.hash = ANY($1)
AND b.orphanedyn = FALSE`

queryGetBlockHashHeightForTxHashesSQLite = `
SELECT
b.hash, b.height, t.hash
FROM blocks b
INNER JOIN block_transactions_map m ON m.blockid = b.id
INNER JOIN transactions t ON m.txid = t.id
WHERE HEX(t.hash) in ('%s')
AND b.orphanedyn = FALSE`
SELECT
b.hash, b.height, t.hash
FROM blocks b
INNER JOIN block_transactions_map m ON m.blockid = b.id
INNER JOIN transactions t ON m.txid = t.id
WHERE HEX(t.hash) in ('%s')
AND b.orphanedyn = FALSE`
)

func getQueryPostgres(transactions *blocktx_api.Transactions) string {
var result []string
for _, v := range transactions.Transactions {
result = append(result, fmt.Sprintf("decode('%x', 'hex')", (v.Hash)))
}

return fmt.Sprintf(queryGetBlockHashHeightForTxHashesPostgres, strings.Join(result, ","))
}

func getQuerySQLite(transactions *blocktx_api.Transactions) string {
var result []string
for _, v := range transactions.Transactions {
Expand All @@ -58,24 +52,32 @@ func (s *SQL) GetTransactionBlocks(ctx context.Context, transactions *blocktx_ap
defer cancel()

results := &blocktx_api.TransactionBlocks{}
var query string
var rows *sql.Rows
var err error

switch s.engine {
case sqliteEngine:
query = getQuerySQLite(transactions)
fallthrough
case sqliteMemoryEngine:
query = getQuerySQLite(transactions)
rows, err = s.db.QueryContext(ctx, getQuerySQLite(transactions))
if err != nil {
return nil, err
}
case postgresEngine:
query = getQueryPostgres(transactions)
var hashSlice [][]byte
for _, tx := range transactions.Transactions {
hashSlice = append(hashSlice, tx.Hash)
}

rows, err = s.db.QueryContext(ctx, queryGetBlockHashHeightForTxHashesPostgres, pq.Array(hashSlice))
if err != nil {
return nil, err
}

default:
return nil, fmt.Errorf("engine not supported: %s", s.engine)
}

rows, err := s.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}

defer rows.Close()

for rows.Next() {
Expand Down
40 changes: 2 additions & 38 deletions blocktx/store/sql/get_transaction_blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestGetFullQuery(t *testing.T) {
t.Run("test", func(t *testing.T) {
hash1, err := chainhash.NewHashFromStr("181fcd0be5a1742aabd594a5bfd5a1e7863a4583290da72fb2a896dfa824645c")
require.NoError(t, err)
hash2, err := chainhash.NewHashFromStr("2e5c318f7f2e2e80e484ca1f00f1b7bee95a33a848de572a304b973ff2b0b35b")
require.NoError(t, err)
hash3, err := chainhash.NewHashFromStr("82b0a66c5dcbd0f6f6f99e2bf766e1d40b04c175c01ee87f1abc36136e511a7e")
require.NoError(t, err)

expectedQuery := `
SELECT
b.hash, b.height, t.hash
FROM blocks b
INNER JOIN block_transactions_map m ON m.blockid = b.id
INNER JOIN transactions t ON m.txid = t.id
WHERE t.hash in (decode('5c6424a8df96a8b22fa70d2983453a86e7a1d5bfa594d5ab2a74a1e50bcd1f18', 'hex'),decode('5bb3b0f23f974b302a57de48a8335ae9beb7f1001fca84e4802e2e7f8f315c2e', 'hex'),decode('7e1a516e1336bc1a7fe81ec075c1040bd4e166f72b9ef9f6f6d0cb5d6ca6b082', 'hex'))
AND b.orphanedyn = FALSE`
transactions := &blocktx_api.Transactions{
Transactions: []*blocktx_api.Transaction{
{
Hash: hash1.CloneBytes(),
},
{
Hash: hash2.CloneBytes(),
},
{
Hash: hash3.CloneBytes(),
},
},
}

q := getQueryPostgres(transactions)
require.Equal(t, expectedQuery, q)
})
}

func TestGetTransactionBlocks(t *testing.T) {
txHash1, err := chainhash.NewHashFromStr("181fcd0be5a1742aabd594a5bfd5a1e7863a4583290da72fb2a896dfa824645c")
require.NoError(t, err)
Expand Down Expand Up @@ -94,7 +58,7 @@ func TestGetTransactionBlocks(t *testing.T) {
FROM blocks b
INNER JOIN block_transactions_map m ON m.blockid = b.id
INNER JOIN transactions t ON m.txid = t.id
WHERE t.hash in (decode('5c6424a8df96a8b22fa70d2983453a86e7a1d5bfa594d5ab2a74a1e50bcd1f18', 'hex'),decode('5bb3b0f23f974b302a57de48a8335ae9beb7f1001fca84e4802e2e7f8f315c2e', 'hex'),decode('7e1a516e1336bc1a7fe81ec075c1040bd4e166f72b9ef9f6f6d0cb5d6ca6b082', 'hex'))
WHERE t.hash = ANY($1)
AND b.orphanedyn = FALSE`

mock.ExpectQuery(query).WillReturnError(errors.New("db connection error"))
Expand All @@ -110,7 +74,7 @@ func TestGetTransactionBlocks(t *testing.T) {
FROM blocks b
INNER JOIN block_transactions_map m ON m.blockid = b.id
INNER JOIN transactions t ON m.txid = t.id
WHERE t.hash in (decode('5c6424a8df96a8b22fa70d2983453a86e7a1d5bfa594d5ab2a74a1e50bcd1f18', 'hex'),decode('5bb3b0f23f974b302a57de48a8335ae9beb7f1001fca84e4802e2e7f8f315c2e', 'hex'),decode('7e1a516e1336bc1a7fe81ec075c1040bd4e166f72b9ef9f6f6d0cb5d6ca6b082', 'hex'))
WHERE t.hash = ANY($1)
AND b.orphanedyn = FALSE`

mock.ExpectQuery(query).WillReturnRows(
Expand Down

0 comments on commit e7ac1c2

Please sign in to comment.