Skip to content

Commit 6f33646

Browse files
authored
api: fix root query to be correctly specific (#75)
1 parent 210716b commit 6f33646

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

api/root.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,28 @@ func (s *Server) GetRoot(w http.ResponseWriter, r *http.Request) {
4545
const q = `
4646
SELECT root
4747
FROM trees
48-
WHERE proofs_array(proofs) @> proofs_array($1)
49-
LIMIT 1
48+
WHERE proofs_array(proofs) <@ proofs_array($1);
5049
`
51-
5250
rr := rootResp{}
53-
err := s.db.QueryRow(ctx, q, dbQuery).Scan(
54-
&rr.Root,
55-
)
56-
if errors.Is(err, pgx.ErrNoRows) {
57-
s.sendJSONError(r, w, nil, http.StatusNotFound, "root not found for proofs")
58-
return
59-
} else if err != nil {
51+
52+
// should only return one row, using QueryFunc to verify
53+
// that's the case and return an error if not (we've had
54+
// issues with this in the past)
55+
hasRow := false
56+
_, err := s.db.QueryFunc(ctx, q, []interface{}{dbQuery}, []interface{}{&rr.Root}, func(qfr pgx.QueryFuncRow) error {
57+
if hasRow {
58+
return errors.New("multiple rows returned")
59+
}
60+
hasRow = true
61+
return nil
62+
})
63+
64+
if err != nil {
6065
s.sendJSONError(r, w, err, http.StatusInternalServerError, "selecting root")
6166
return
67+
} else if len(rr.Root) == 0 { // db.QueryFunc doesn't return pgx.ErrNoRows
68+
s.sendJSONError(r, w, nil, http.StatusNotFound, "root not found for proofs")
69+
return
6270
}
6371

6472
w.Header().Set("Cache-Control", "public, max-age=3600")

0 commit comments

Comments
 (0)