Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block_storage load block and votes test #1011

Open
wants to merge 4 commits into
base: block-storage-tests-2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions deku-p/src/core/block_storage/tests/block_storage_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ open Deku_crypto
open Deku_consensus
open Deku_concepts
open Deku_gossip
open Deku_block_storage

exception Test_finished

Expand All @@ -27,17 +28,19 @@ 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

(* NOTE: These tests generate new databases in /tmp/ for every test, for every run. *)
(* TODO: change to an in-memory databse *)
let uri = Uri.of_string (Format.sprintf "sqlite3:/tmp/%s.db" file_hash)
let uri () =
let file_hash =
let randn = Stdlib.Random.int 230 in
Deku_crypto.BLAKE2b.hash (Int.to_string randn) |> BLAKE2b.to_hex
in
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
let storage = Block_storage.make ~worker ~uri:(uri ()) in
storage

let test_empty_block_load env () =
Expand Down Expand Up @@ -85,14 +88,12 @@ let test_empty_block_and_votes env () =
let (Deku_gossip.Message.Message { header = _; content = _; network }) =
Deku_gossip.Message.encode ~content
in
Deku_block_storage.Block_storage.save_block_and_votes ~level ~network
block_storage;
Block_storage.save_block_and_votes ~level ~network block_storage;

let retrieved_block_and_votes =
let default_return = (Genesis.block, []) in
match
Deku_block_storage.Block_storage.find_block_and_votes_by_level ~level
block_storage
Block_storage.find_block_and_votes_by_level ~level block_storage
with
| Some (Message.Network.Network_message { raw_header; raw_content }) -> (
let expected = Message.Header.decode ~raw_header in
Expand All @@ -111,6 +112,24 @@ let test_empty_block_and_votes env () =
Eio.Switch.fail sw Test_finished
with _ -> ()

let test_200k_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:200_000 in
Block_storage.save_block ~block block_storage;
let retrieved_block =
match Block_storage.find_block_by_hash ~block_hash:hash block_storage with
| Some block -> block
| None -> Genesis.block
in
Alcotest.(check' block_testable)
~msg:"hash loaded block is equal to saved block" ~expected:block
~actual:retrieved_block;

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

let run () =
Eio_main.run (fun env ->
let open Alcotest in
Expand All @@ -122,6 +141,8 @@ let run () =
(test_empty_block_load env);
test_case "empty block and one vote is returned" `Quick
(test_empty_block_and_votes env);
test_case "200k_block is returned" `Slow
(test_200k_block_load env);
] );
])

Expand Down