Skip to content

Commit

Permalink
added level loaded empty block block_storage test
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteSwerve committed Dec 12, 2022
1 parent f7430a0 commit 8f60d75
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions deku-p/src/core/block_storage/tests/block_storage_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let file_hash =
let randn = Stdlib.Random.int 230 in
Deku_crypto.BLAKE2b.hash (Int.to_string randn) |> BLAKE2b.to_hex

(* TODO: change to an in-memory databse *)
let uri = Uri.of_string (Format.sprintf "sqlite3:/tmp/%s.db" file_hash)

let make_block_storage env sw =
Expand All @@ -36,28 +37,41 @@ let test_empty_block_load env () =
Parallel.Pool.run ~env ~domains:4 @@ fun () ->
Eio.Switch.run @@ fun sw ->
let block_storage = make_block_storage env sw in
let (Block { hash; _ } as block) = block ~default_block_size:0 in
let (Block { hash; level; _ } as block) = block ~default_block_size:0 in
Deku_block_storage.Block_storage.save_block ~block block_storage;
let retrieved_block =
match
Deku_block_storage.Block_storage.find_block_by_hash ~block_hash:hash
block_storage
with
| Some json ->
Format.eprintf "done!\n%!";
let b =
Data_encoding.Json.destruct Deku_consensus.Block.encoding json
in
Format.eprintf "done!\n%!";
b
Data_encoding.Json.destruct Deku_consensus.Block.encoding json
| None -> Genesis.block
in
Alcotest.(check' block_testable)
~msg:"hash loaded block is not equal to saved block" ~expected:block
~actual:retrieved_block;
let retrieved_block =
match
Deku_block_storage.Block_storage.find_block_by_level ~level
block_storage
with
| Some json ->
Data_encoding.Json.destruct Deku_consensus.Block.encoding json
| None -> Genesis.block
in

Alcotest.(check' block_testable)
~msg:"level loaded block is equal to saved block" ~expected:block
~actual:retrieved_block;

Eio.Switch.fail sw Test_finished
with _ -> ()

(* TODO: Add tests with only one env threaded through all tests *)
let eio_test_case : (Eio.Stdenv.t -> unit -> unit) -> unit -> unit =
fun f () -> Eio_main.run (fun env -> f env ())

let run () =
Eio_main.run (fun env ->
let open Alcotest in
Expand All @@ -71,3 +85,8 @@ let run () =
])

let () = run ()

(* TODO: Tests
try all combinations of what's in the block_storage.mli. Use it with different block sizes, do it in parallel, try reading and writing at the same time, try reading a query that doesn't exist
try reading something right before you write it and vice versa,
try reading or writing two things at once *)

0 comments on commit 8f60d75

Please sign in to comment.