Skip to content

Commit ccbaaa1

Browse files
committed
Fix queries
1 parent 36effad commit ccbaaa1

File tree

5 files changed

+70
-37
lines changed

5 files changed

+70
-37
lines changed

server/internal/infrastructure/db/sqlite/round_repo.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql"
66
"errors"
77
"fmt"
8+
89
"github.com/ark-network/ark/common/tree"
910
"github.com/ark-network/ark/server/internal/core/domain"
1011
"github.com/ark-network/ark/server/internal/infrastructure/db/sqlite/sqlc/queries"
@@ -305,8 +306,8 @@ func (r *roundRepository) GetRoundForfeitTxs(ctx context.Context, roundTxid stri
305306
forfeits := make([]domain.ForfeitTx, 0, len(rows))
306307
for _, row := range rows {
307308
forfeits = append(forfeits, domain.ForfeitTx{
308-
Txid: row.Txid,
309-
Tx: row.Tx,
309+
Txid: row.Txid.String,
310+
Tx: row.Tx.String,
310311
})
311312
}
312313

@@ -324,11 +325,11 @@ func (r *roundRepository) GetRoundConnectorTree(ctx context.Context, roundTxid s
324325
for _, tx := range rows {
325326
level := tx.TreeLevel
326327
vtxoTree = extendArray(vtxoTree, int(level.Int64))
327-
vtxoTree[int(level.Int64)] = extendArray(vtxoTree[int(level.Int64)], int(tx.Position))
328-
if vtxoTree[int(level.Int64)][tx.Position] == (tree.Node{}) {
329-
vtxoTree[int(level.Int64)][tx.Position] = tree.Node{
330-
Tx: tx.Tx,
331-
Txid: tx.Txid,
328+
vtxoTree[int(level.Int64)] = extendArray(vtxoTree[int(level.Int64)], int(tx.Position.Int64))
329+
if vtxoTree[int(level.Int64)][tx.Position.Int64] == (tree.Node{}) {
330+
vtxoTree[int(level.Int64)][tx.Position.Int64] = tree.Node{
331+
Tx: tx.Tx.String,
332+
Txid: tx.Txid.String,
332333
ParentTxid: tx.ParentTxid.String,
333334
Leaf: tx.IsLeaf.Bool,
334335
}

server/internal/infrastructure/db/sqlite/sqlc/queries/db.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/internal/infrastructure/db/sqlite/sqlc/queries/models.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/internal/infrastructure/db/sqlite/sqlc/queries/query.sql.go

Lines changed: 38 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/internal/infrastructure/db/sqlite/sqlc/query.sql

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ SELECT
9393
r.starting_timestamp,
9494
r.ending_timestamp,
9595
(
96-
SELECT COALESCE(SUM(v2.amount), 0)
97-
FROM vtxo v2
98-
JOIN tx_request req2 ON req2.id = v2.request_id
99-
WHERE req2.round_id = r.id
96+
SELECT COALESCE(SUM(amount), 0)
97+
FROM (
98+
SELECT DISTINCT v2.*
99+
FROM vtxo v2
100+
JOIN tx_request req2 ON req2.id = v2.request_id
101+
WHERE req2.round_id = r.id
102+
)
100103
) AS total_forfeit_amount,
101104
(
102105
SELECT COALESCE(COUNT(v3.txid), 0)
@@ -105,11 +108,14 @@ SELECT
105108
WHERE req3.round_id = r.id
106109
) AS total_input_vtxos,
107110
(
108-
SELECT COALESCE(SUM(rr.amount), 0)
109-
FROM receiver rr
110-
JOIN tx_request req4 ON req4.id = rr.request_id
111-
WHERE req4.round_id = r.id
112-
AND (rr.onchain_address = '' OR rr.onchain_address IS NULL)
111+
SELECT COALESCE(SUM(amount), 0)
112+
FROM (
113+
SELECT DISTINCT rr.*
114+
FROM receiver rr
115+
JOIN tx_request req4 ON req4.id = rr.request_id
116+
WHERE req4.round_id = r.id
117+
AND (rr.onchain_address = '' OR rr.onchain_address IS NULL)
118+
)
113119
) AS total_batch_amount,
114120
(
115121
SELECT COUNT(*)
@@ -127,12 +133,14 @@ FROM round r
127133
WHERE r.txid = ?;
128134

129135
-- name: GetRoundForfeitTxs :many
130-
SELECT tx.* FROM tx
131-
WHERE tx.round_id = ? AND tx.type = 'forfeit';
136+
SELECT tx.* FROM round
137+
LEFT OUTER JOIN tx ON round.id=tx.round_id
138+
WHERE round.txid = ? AND tx.type = 'forfeit';
132139

133140
-- name: GetRoundConnectorTreeTxs :many
134-
SELECT tx.* FROM tx
135-
WHERE tx.round_id = ? AND tx.type = 'tree';
141+
SELECT tx.* FROM round
142+
LEFT OUTER JOIN tx ON round.id=tx.round_id
143+
WHERE round.txid = ? AND tx.type = 'connector';
136144

137145
-- name: GetSpendableVtxosWithPubKey :many
138146
SELECT vtxo.* FROM vtxo
@@ -183,7 +191,7 @@ SELECT sqlc.embed(vtxo) FROM vtxo;
183191

184192
-- name: SelectVtxosByRoundTxid :many
185193
SELECT sqlc.embed(vtxo) FROM vtxo
186-
WHERE round_tx = ?;
194+
WHERE round_tx = ? AND (redeem_tx IS NULL or redeem_tx = '');
187195

188196
-- name: MarkVtxoAsRedeemed :exec
189197
UPDATE vtxo SET redeemed = true WHERE txid = ? AND vout = ?;

0 commit comments

Comments
 (0)