From 4064c9af95da5fc98926636873e2552a7ab072f8 Mon Sep 17 00:00:00 2001 From: InfiniteSwerve Date: Mon, 12 Dec 2022 12:03:38 -0800 Subject: [PATCH 1/5] added switch.fail to ensure tests finish --- .../tests/block_storage_tests.ml | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 deku-p/src/core/block_storage/tests/block_storage_tests.ml diff --git a/deku-p/src/core/block_storage/tests/block_storage_tests.ml b/deku-p/src/core/block_storage/tests/block_storage_tests.ml new file mode 100644 index 000000000..4fe2a9599 --- /dev/null +++ b/deku-p/src/core/block_storage/tests/block_storage_tests.ml @@ -0,0 +1,68 @@ +open Deku_stdlib +open Deku_crypto +open Deku_consensus +open Deku_concepts + +exception Test_finished + +let block_testable = Alcotest.testable Block.pp Block.equal + +let identity = + let secret = Ed25519.Secret.generate () in + let secret = Secret.Ed25519 secret in + Identity.make secret + +let block ~default_block_size = + let above = Genesis.block in + let withdrawal_handles_hash = BLAKE2b.hash "potato" in + let producer = Producer.empty in + Producer.produce ~identity ~default_block_size ~above ~withdrawal_handles_hash + producer + +let file_hash = + let randn = Stdlib.Random.int 230 in + Deku_crypto.BLAKE2b.hash (Int.to_string randn) |> BLAKE2b.to_hex + +let uri = Uri.of_string (Format.sprintf "sqlite3:/tmp/%s.db" file_hash) + +let make_block_storage env sw = + let domains = Eio.Stdenv.domain_mgr env in + let worker = Parallel.Worker.make ~domains ~sw in + let storage = Deku_block_storage.Block_storage.make ~worker ~uri in + storage + +let test_empty_block_load env () = + try + 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 + 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 -> Block.t_of_yojson json + | None -> Genesis.block + in + Alcotest.(check' block_testable) + ~msg:"hash loaded block is not equal to saved block" ~expected:block + ~actual:retrieved_block; + Eio.Switch.fail sw Test_finished + with _ -> () + +let eio_test_case : (Eio.Stdenv.t -> unit -> unit) -> unit -> unit = + fun f () -> Eio_main.run (fun env -> f env ()) + +let run () = + let open Alcotest in + run "Block_storage" ~and_exit:false + [ + ( "simple", + [ + test_case "empty_block is returned" `Quick + (eio_test_case test_empty_block_load); + ] ); + ] + +let () = run () From f63f83631adf58b6d43db40c5ef5ad0bc13b683a Mon Sep 17 00:00:00 2001 From: InfiniteSwerve Date: Thu, 1 Dec 2022 13:54:20 -0800 Subject: [PATCH 2/5] change block storage to private type --- deku-p/src/core/block_storage/block_storage.mli | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/deku-p/src/core/block_storage/block_storage.mli b/deku-p/src/core/block_storage/block_storage.mli index 534889d05..18c869c5b 100644 --- a/deku-p/src/core/block_storage/block_storage.mli +++ b/deku-p/src/core/block_storage/block_storage.mli @@ -3,7 +3,9 @@ open Deku_concepts open Deku_consensus open Deku_gossip -type storage +type storage = private + | Storage of { worker : Parallel.Worker.t; pool : Query.pool } + type t = storage val make : worker:Parallel.Worker.t -> uri:Uri.t -> storage From b03e5f3e5c65306ad3e483a565604a5a274317ea Mon Sep 17 00:00:00 2001 From: InfiniteSwerve Date: Thu, 1 Dec 2022 13:56:30 -0800 Subject: [PATCH 3/5] add test for empty block saving and loading by hash changed decoding to use Data_encoding added parallelism environment --- .../tests/block_storage_tests.ml | 28 ++++++++----------- deku-p/src/core/block_storage/tests/dune | 16 +++++++++++ 2 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 deku-p/src/core/block_storage/tests/dune diff --git a/deku-p/src/core/block_storage/tests/block_storage_tests.ml b/deku-p/src/core/block_storage/tests/block_storage_tests.ml index 4fe2a9599..7f69aacae 100644 --- a/deku-p/src/core/block_storage/tests/block_storage_tests.ml +++ b/deku-p/src/core/block_storage/tests/block_storage_tests.ml @@ -19,11 +19,7 @@ let block ~default_block_size = Producer.produce ~identity ~default_block_size ~above ~withdrawal_handles_hash producer -let file_hash = - let randn = Stdlib.Random.int 230 in - Deku_crypto.BLAKE2b.hash (Int.to_string randn) |> BLAKE2b.to_hex - -let uri = Uri.of_string (Format.sprintf "sqlite3:/tmp/%s.db" file_hash) +let uri = Uri.of_string "sqlite3:/tmp/database.db" let make_block_storage env sw = let domains = Eio.Stdenv.domain_mgr env in @@ -42,7 +38,7 @@ let test_empty_block_load env () = Deku_block_storage.Block_storage.find_block_by_hash ~block_hash:hash block_storage with - | Some json -> Block.t_of_yojson json + | Some block -> block | None -> Genesis.block in Alcotest.(check' block_testable) @@ -51,18 +47,16 @@ let test_empty_block_load env () = Eio.Switch.fail sw Test_finished with _ -> () -let eio_test_case : (Eio.Stdenv.t -> unit -> unit) -> unit -> unit = - fun f () -> Eio_main.run (fun env -> f env ()) - let run () = - let open Alcotest in - run "Block_storage" ~and_exit:false - [ - ( "simple", + Eio_main.run (fun env -> + let open Alcotest in + run "Block_storage" ~and_exit:false [ - test_case "empty_block is returned" `Quick - (eio_test_case test_empty_block_load); - ] ); - ] + ( "simple", + [ + test_case "empty_block is returned" `Quick + (test_empty_block_load env); + ] ); + ]) let () = run () diff --git a/deku-p/src/core/block_storage/tests/dune b/deku-p/src/core/block_storage/tests/dune new file mode 100644 index 000000000..9072010c6 --- /dev/null +++ b/deku-p/src/core/block_storage/tests/dune @@ -0,0 +1,16 @@ +(executable + (name block_storage_tests) + (libraries + deku_block_storage + deku_crypto + deku_protocol + deku_stdlib + alcotest + deku_consensus)) + +(rule + (alias runtest) + (deps + (:exe ./block_storage_tests.exe)) + (action + (run %{exe}))) From e5268a5589593eec8646d23f7dda37ec8d46a870 Mon Sep 17 00:00:00 2001 From: InfiniteSwerve Date: Thu, 1 Dec 2022 13:57:33 -0800 Subject: [PATCH 4/5] add random database naming --- deku-p/src/core/block_storage/tests/block_storage_tests.ml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/deku-p/src/core/block_storage/tests/block_storage_tests.ml b/deku-p/src/core/block_storage/tests/block_storage_tests.ml index 7f69aacae..209a7673f 100644 --- a/deku-p/src/core/block_storage/tests/block_storage_tests.ml +++ b/deku-p/src/core/block_storage/tests/block_storage_tests.ml @@ -19,7 +19,11 @@ let block ~default_block_size = Producer.produce ~identity ~default_block_size ~above ~withdrawal_handles_hash producer -let uri = Uri.of_string "sqlite3:/tmp/database.db" +let file_hash = + let randn = Stdlib.Random.int 230 in + Deku_crypto.BLAKE2b.hash (Int.to_string randn) |> BLAKE2b.to_hex + +let uri = Uri.of_string (Format.sprintf "sqlite3:/tmp/%s.db" file_hash) let make_block_storage env sw = let domains = Eio.Stdenv.domain_mgr env in From 89158c4bb8bd9a88fdca4133716583e9f141d745 Mon Sep 17 00:00:00 2001 From: InfiniteSwerve Date: Fri, 2 Dec 2022 10:54:28 -0800 Subject: [PATCH 5/5] added level loaded empty block block_storage test --- .../tests/block_storage_tests.ml | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/deku-p/src/core/block_storage/tests/block_storage_tests.ml b/deku-p/src/core/block_storage/tests/block_storage_tests.ml index 209a7673f..babe2c3dc 100644 --- a/deku-p/src/core/block_storage/tests/block_storage_tests.ml +++ b/deku-p/src/core/block_storage/tests/block_storage_tests.ml @@ -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 = @@ -35,7 +36,7 @@ let test_empty_block_load env () = try 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 @@ -48,6 +49,19 @@ let test_empty_block_load env () = 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 block -> block + | 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 _ -> () @@ -64,3 +78,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 *)