Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation for tx & prover search #19

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

type Event struct {
State string `json:"state"`
TxID string `json:"tx_id"`
ProverID string `json:"prover_id"`
TxID string `db:"tx_id",json:"tx_id"`

Check failure on line 14 in model/model.go

View workflow job for this annotation

GitHub Actions / build

structtag: struct field tag `db:"tx_id",json:"tx_id"` not compatible with reflect.StructTag.Get: key:"value" pairs not separated by spaces (govet)
ProverID string `db:"prover_id",json:"prover_id"`

Check failure on line 15 in model/model.go

View workflow job for this annotation

GitHub Actions / build

structtag: struct field tag `db:"prover_id",json:"prover_id"` not compatible with reflect.StructTag.Get: key:"value" pairs not separated by spaces (govet)
Tag string `json:"tag"`
Timestamp time.Time `json:"timestamp"`
}
51 changes: 48 additions & 3 deletions store/pg/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"log/slog"
"strings"
"time"

"github.com/gevulotnetwork/devnet-explorer/model"
Expand Down Expand Up @@ -96,12 +97,56 @@ func (s *Store) Stats() (model.Stats, error) {
}

func (s *Store) Search(filter string) ([]model.Event, error) {
// TODO: implement search query
//
filter = strings.TrimSpace(filter)

// filter string: free text search input straight from the user, handle as such.
// This query should return 50 most recent matching events sorted by timestamp in newest first order.
const query = ``
const query = `
WITH t2 AS ((
(SELECT created_at, t.hash, t.kind FROM transaction AS t WHERE t.hash = $1)
UNION ALL
(SELECT created_at, t.hash, t.kind FROM transaction AS t JOIN workflow_step AS ws ON ws.tx=t.hash WHERE ws.sequence=1 AND ws.program = $1)
UNION ALL
(SELECT created_at, t.hash, t.kind FROM transaction AS t JOIN proof AS p ON t.hash = p.tx WHERE p.prover = $1)
UNION ALL
(SELECT created_at, t.hash, t.kind FROM transaction AS t JOIN verification AS v ON t.hash = v.tx JOIN proof AS p ON v.parent = p.tx WHERE p.prover = $1)
) ORDER BY created_at DESC LIMIT 50)
SELECT
CASE WHEN t2.kind = 'run' THEN
CASE WHEN (SELECT COUNT(*) FROM proof WHERE parent = t2.hash) = 0 THEN 'Submitted'
WHEN ((SELECT COUNT(*) FROM proof WHERE parent = t2.hash) >= 1 AND (SELECT COUNT(*) FROM verification AS v JOIN proof AS p ON v.parent=p.tx WHERE p.parent = t2.hash) = 0) IS TRUE THEN 'Proving'
WHEN (SELECT COUNT(*) FROM verification AS v JOIN proof AS p ON v.parent=p.tx WHERE p.parent = t2.hash) = 1 THEN 'Verifying'
WHEN (SELECT COUNT(*) FROM verification AS v JOIN proof AS p ON v.parent=p.tx WHERE p.parent = t2.hash) = 2 THEN 'Verifying'
WHEN (SELECT COUNT(*) FROM verification AS v JOIN proof AS p ON v.parent=p.tx WHERE p.parent = t2.hash) > 2 THEN 'Complete'
END
WHEN t2.kind = 'proof' THEN
CASE WHEN (SELECT COUNT(*) FROM proof WHERE parent = (SELECT parent FROM proof WHERE tx = t2.hash)) = 1 THEN 'Proving'
WHEN (SELECT COUNT(*) FROM verification AS v JOIN proof AS p ON v.parent=p.tx WHERE p.parent = (SELECT parent FROM proof WHERE tx = t2.hash)) = 1 THEN 'Verifying'
WHEN (SELECT COUNT(*) FROM verification AS v JOIN proof AS p ON v.parent=p.tx WHERE p.parent = (SELECT parent FROM proof WHERE tx = t2.hash)) = 2 THEN 'Verifying'
WHEN (SELECT COUNT(*) FROM verification AS v JOIN proof AS p ON v.parent=p.tx WHERE p.parent = (SELECT parent FROM proof WHERE tx = t2.hash)) > 2 THEN 'Complete'
END
WHEN t2.kind = 'verification' THEN
CASE WHEN (SELECT COUNT(*) FROM verification AS v JOIN proof AS p ON v.parent=p.tx WHERE p.parent = (SELECT p2.parent FROM proof AS p2 JOIN verification AS v2 ON p2.tx=v2.parent WHERE v2.tx = t2.hash)) = 1 THEN 'Verifying'
WHEN (SELECT COUNT(*) FROM verification AS v JOIN proof AS p ON v.parent=p.tx WHERE p.parent = (SELECT p2.parent FROM proof AS p2 JOIN verification AS v2 ON p2.tx=v2.parent WHERE v2.tx = t2.hash)) = 2 THEN 'Verifying'
WHEN (SELECT COUNT(*) FROM verification AS v JOIN proof AS p ON v.parent=p.tx WHERE p.parent = (SELECT p2.parent FROM proof AS p2 JOIN verification AS v2 ON p2.tx=v2.parent WHERE v2.tx = t2.hash)) > 2 THEN 'Complete'
END
END AS state,
t.hash AS tx_id,
(
(SELECT name AS tag FROM program AS p JOIN workflow_step AS ws ON p.hash = ws.program JOIN t2 ON ws.tx = t2.hash WHERE ws.sequence = 1)
UNION
(SELECT name AS tag FROM program WHERE hash = $1)
),
(
(SELECT program AS prover_id FROM workflow_step AS ws JOIN t2 ON ws.tx = t2.hash WHERE ws.sequence = 1)
UNION
(SELECT hash AS prover_id FROM program WHERE hash = $1)
),
t.created_at AS timestamp FROM transaction AS t JOIN t2 ON t.hash = t2.hash
`
Comment on lines +104 to +146
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should be implemented as a separate function in Postgres as well. It's inhuman to compute this in head 🤯


var events []model.Event

if _, err := s.db.Select(&events, query, filter); err != nil {
return nil, err
}
Expand Down
Loading