-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: simplify data model, proofs_hashes table (#101)
- Loading branch information
1 parent
82f3158
commit da76b8e
Showing
6 changed files
with
269 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
api/migrations/scripts/000-rebuild-proofs-hashes/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"runtime" | ||
"runtime/debug" | ||
"sync" | ||
|
||
"github.com/contextwtf/lanyard/merkle" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/jackc/pgx/v4" | ||
"github.com/jackc/pgx/v4/pgxpool" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func check(err error) { | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "processor error: %s", err) | ||
debug.PrintStack() | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func hashProof(p [][]byte) []byte { | ||
return crypto.Keccak256(p...) | ||
} | ||
|
||
func migrateTree( | ||
ctx context.Context, | ||
tx pgx.Tx, | ||
leaves [][]byte, | ||
) error { | ||
tree := merkle.New(leaves) | ||
|
||
var ( | ||
proofHashes = [][]any{} | ||
eg errgroup.Group | ||
pm sync.Mutex | ||
) | ||
eg.SetLimit(runtime.NumCPU()) | ||
|
||
for _, l := range leaves { | ||
l := l //avoid capture | ||
eg.Go(func() error { | ||
pf := tree.Proof(l) | ||
if !merkle.Valid(tree.Root(), pf, l) { | ||
return errors.New("invalid proof for tree") | ||
} | ||
proofHash := hashProof(pf) | ||
pm.Lock() | ||
proofHashes = append(proofHashes, []any{tree.Root(), proofHash}) | ||
pm.Unlock() | ||
return nil | ||
}) | ||
} | ||
err := eg.Wait() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = tx.CopyFrom(ctx, pgx.Identifier{"proofs_hashes"}, | ||
[]string{"root", "hash"}, | ||
pgx.CopyFromRows(proofHashes), | ||
) | ||
|
||
return err | ||
} | ||
|
||
func main() { | ||
ctx := context.Background() | ||
const defaultPGURL = "postgres:///al" | ||
dburl := os.Getenv("DATABASE_URL") | ||
if dburl == "" { | ||
dburl = defaultPGURL | ||
} | ||
dbc, err := pgxpool.ParseConfig(dburl) | ||
check(err) | ||
|
||
db, err := pgxpool.ConnectConfig(ctx, dbc) | ||
check(err) | ||
|
||
log.Println("fetching roots from db") | ||
const q = ` | ||
SELECT unhashed_leaves | ||
FROM trees | ||
WHERE root not in (select root from proofs_hashes group by 1) | ||
` | ||
rows, err := db.Query(ctx, q) | ||
check(err) | ||
defer rows.Close() | ||
|
||
trees := [][][]byte{} | ||
|
||
for rows.Next() { | ||
var t [][]byte | ||
err := rows.Scan(&t) | ||
trees = append(trees, t) | ||
check(err) | ||
} | ||
|
||
if len(trees) == 0 { | ||
log.Println("no trees to process") | ||
return | ||
} | ||
|
||
log.Printf("migrating %d trees", len(trees)) | ||
|
||
tx, err := db.Begin(ctx) | ||
check(err) | ||
defer tx.Rollback(ctx) | ||
|
||
var count int | ||
|
||
for _, tree := range trees { | ||
err = migrateTree(ctx, tx, tree) | ||
check(err) | ||
count++ | ||
if count%1000 == 0 { | ||
log.Printf("migrated %d/%d trees", count, len(trees)) | ||
} | ||
} | ||
|
||
log.Printf("committing %d trees", len(trees)) | ||
err = tx.Commit(ctx) | ||
check(err) | ||
log.Printf("done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
da76b8e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
lanyard – ./
lanyard.mf.dev
lanyard-git-main.mf.dev
lanyard-production.mf.dev