-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add methods to sync whole da * Fix version fallback in da client * Replace fetching of hash chain with diff chain * Add ledger caching to the archive relay syncing * Remove dead code * Move db from da node to separate file * Add posibility of migrations in da node * Add version tag to diff * Add migration that adds version tag
- Loading branch information
1 parent
8a98430
commit f106367
Showing
12 changed files
with
415 additions
and
283 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
open Core_kernel | ||
open Mina_base | ||
|
||
(** Holds keys to all the diffes *) | ||
module Index = struct | ||
[%%versioned | ||
module Stable = struct | ||
module V1 = struct | ||
type t = Ledger_hash.Stable.V1.t list | ||
|
||
let to_latest = Fn.id | ||
end | ||
end] | ||
|
||
let to_bigstring = Binable.to_bigstring (module Stable.Latest) | ||
|
||
let of_bigstring = Binable.of_bigstring (module Stable.Latest) | ||
end | ||
|
||
module Key_value = struct | ||
type _ t = | ||
| Diff : (Ledger_hash.t * Diff.t) t | ||
| Diff_index : (unit * Index.t) t | ||
| Migration : (unit * int) t | ||
|
||
let serialize_key : type k v. (k * v) t -> k -> Bigstring.t = | ||
fun pair_type key -> | ||
match pair_type with | ||
| Diff -> | ||
Bigstring.concat | ||
[ Bigstring.of_string "diff" | ||
; Bigstring.of_string @@ Ledger_hash.to_decimal_string key | ||
] | ||
| Diff_index -> | ||
Bigstring.of_string "diff_index" | ||
| Migration -> | ||
Bigstring.of_string "migration" | ||
|
||
let serialize_value : type k v. (k * v) t -> v -> Bigstring.t = | ||
fun pair_type value -> | ||
match pair_type with | ||
| Diff -> | ||
Diff.to_bigstring value | ||
| Diff_index -> | ||
Index.to_bigstring value | ||
| Migration -> | ||
Bigstring.of_string @@ Int.to_string value | ||
|
||
let deserialize_value : type k v. (k * v) t -> Bigstring.t -> v = | ||
fun pair_type data -> | ||
match pair_type with | ||
| Diff -> | ||
Diff.of_bigstring data |> Or_error.ok_exn | ||
| Diff_index -> | ||
Index.of_bigstring data | ||
| Migration -> | ||
Int.of_string @@ Bigstring.to_string data | ||
end | ||
|
||
include Kvdb_base.Make (Key_value) | ||
|
||
let set_index t ~index = set t Diff_index ~key:() ~data:index | ||
|
||
let get_index t = get t Diff_index ~key:() |> Option.value ~default:[] | ||
|
||
let add_diff t ~ledger_hash ~diff = | ||
let index = get_index t in | ||
if List.mem index ledger_hash ~equal:Ledger_hash.equal then `Already_existed | ||
else ( | ||
set t Diff ~key:ledger_hash ~data:diff ; | ||
set_index t ~index:(ledger_hash :: index) ; | ||
`Added ) | ||
|
||
let get_diff t ~ledger_hash = get t Diff ~key:ledger_hash | ||
|
||
let get_migration t = get t Migration ~key:() |> Option.value ~default:0 | ||
|
||
let set_migration t ~migration = set t Migration ~key:() ~data:migration |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
(library | ||
(name da_layer) | ||
(inline_tests) | ||
(libraries | ||
kvdb_base | ||
zkapps_rollup | ||
|
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,41 @@ | ||
open Core_kernel | ||
|
||
type migration = Db.t -> unit | ||
|
||
let progress_bar ?(width = 30) progress = | ||
let filled_length = int_of_float (float_of_int width *. progress) in | ||
let bar = | ||
String.make filled_length '#' ^ String.make (width - filled_length) '-' | ||
in | ||
Printf.printf "\r[%s] %.0f%%%!" bar (progress *. 100.0) ; | ||
if Float.(progress >= 1.0) then Printf.printf "\n%!" | ||
|
||
let add_version_tag db = | ||
let try_read buff = | ||
try | ||
let pos_ref = ref 0 in | ||
Ok (Diff.Stable.V1.bin_read_t ~pos_ref buff) | ||
with _ -> Error "Failed to convert diff" | ||
in | ||
let all = Db.to_alist db in | ||
let l = List.length all in | ||
List.iteri all ~f:(fun i (key, value) -> | ||
progress_bar (Float.of_int i /. Float.of_int l) ; | ||
match try_read value with | ||
| Error _ -> | ||
( (* The pair is something different from diff *) ) | ||
| Ok diff -> | ||
let v2 = Diff.to_bigstring @@ Diff.Stable.V1.to_latest diff in | ||
Db.set_raw db ~key ~data:v2 ) | ||
|
||
let migrations : migration list = [ add_version_tag ] | ||
|
||
let latest_migration = List.length migrations | ||
|
||
let run_migrations ~logger db = | ||
let old_migration = Db.get_migration db in | ||
List.drop migrations old_migration | ||
|> List.iteri ~f:(fun i migration -> | ||
[%log info] "Running migration %d" (old_migration + i + 1) ; | ||
migration db ; | ||
Db.set_migration db ~migration:(old_migration + i + 1) ) |
Oops, something went wrong.