Skip to content

Commit 343e906

Browse files
committed
Add rebuild-mst subcommand
1 parent 5885442 commit 343e906

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

bin/main.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,19 @@ let migrate_blobs ?did () =
251251
print_endline "migrating all blobs to S3" ;
252252
S3.Blob_migration.migrate_all ()
253253

254+
let rebuild_mst ~did () =
255+
print_endline ("rebuilding MST for " ^ did) ;
256+
let%lwt repo = Repository.load did in
257+
match%lwt Repository.rebuild_mst repo with
258+
| Ok (commit_cid, commit) ->
259+
print_endline
260+
(Printf.sprintf "MST rebuilt successfully, new commit: %s (rev: %s)"
261+
(Cid.to_string commit_cid) commit.rev ) ;
262+
Lwt.return_unit
263+
| Error exn ->
264+
print_endline ("error rebuilding MST: " ^ Printexc.to_string exn) ;
265+
exit 1
266+
254267
let print_usage () =
255268
print_endline
256269
@@ String.trim
@@ -262,6 +275,7 @@ commands:
262275
create-invite [uses] create an invite code with an optional number of uses (default: 1)
263276
migrate-blobs migrate all local blobs to S3
264277
migrate-blobs <did> migrate blobs for a specific user to S3
278+
rebuild-mst <did> rebuild MST from records table (recovery tool)
265279

266280
see also: gen-keys
267281
|}
@@ -280,6 +294,8 @@ let () =
280294
Lwt_main.run (migrate_blobs ())
281295
| ["migrate-blobs"; did] ->
282296
Lwt_main.run (migrate_blobs ~did ())
297+
| ["rebuild-mst"; did] ->
298+
Lwt_main.run (rebuild_mst ~did ())
283299
| ["help"] | ["--help"] | ["-h"] ->
284300
print_usage ()
285301
| cmd :: _ ->

pegasus/lib/repository.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,3 +589,24 @@ let import_car t (stream : Car.stream) : (t, exn) Lwt_result.t =
589589
t.commit <- Some (root, commit) ;
590590
Lwt.return_ok t
591591
with exn -> Lwt.return_error exn
592+
593+
let rebuild_mst t : (Cid.t * signed_commit, exn) Lwt_result.t =
594+
try%lwt
595+
let%lwt record_cids = User_store.get_all_record_cids t.db in
596+
let record_count = List.length record_cids in
597+
Logs.info (fun m -> m "rebuilding MST from %d records" record_count) ;
598+
let%lwt () = User_store.clear_mst t.db in
599+
Logs.info (fun m -> m "cleared existing MST blocks") ;
600+
let%lwt mst_result = Mst.of_assoc t.db record_cids in
601+
Logs.info (fun m ->
602+
m "built new MST with root %s" (Cid.to_string mst_result.root) ) ;
603+
let%lwt prev_commit = User_store.get_commit t.db in
604+
let%lwt new_commit =
605+
put_commit t mst_result.root ~previous:(Option.map snd prev_commit)
606+
in
607+
let commit_cid, commit = new_commit in
608+
Logs.info (fun m ->
609+
m "inserted new commit %s with rev %s" (Cid.to_string commit_cid)
610+
commit.rev ) ;
611+
Lwt.return_ok new_commit
612+
with exn -> Lwt.return_error exn

0 commit comments

Comments
 (0)