Skip to content

Commit

Permalink
added load block and votes test
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteSwerve committed Dec 21, 2022
1 parent 89158c4 commit b840df2
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions deku-p/src/core/block_storage/tests/block_storage_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ open Deku_stdlib
open Deku_crypto
open Deku_consensus
open Deku_concepts
open Deku_gossip

exception Test_finished

let block_testable = Alcotest.testable Block.pp Block.equal

let vote_testable =
Alcotest.testable Verified_signature.pp Verified_signature.equal

let identity =
let secret = Ed25519.Secret.generate () in
let secret = Secret.Ed25519 secret in
Identity.make secret

let make_vote ~hash identity =
let hash = Block_hash.to_blake2b hash in
Verified_signature.sign hash identity

let block ~default_block_size =
let above = Genesis.block in
let withdrawal_handles_hash = BLAKE2b.hash "potato" in
Expand Down Expand Up @@ -65,6 +73,44 @@ let test_empty_block_load env () =
Eio.Switch.fail sw Test_finished
with _ -> ()

let test_empty_block_and_votes env () =
try
Eio.Switch.run @@ fun sw ->
let block_storage = make_block_storage env sw in
let (Block { hash; level; _ } as block) = block ~default_block_size:0 in
let vote = make_vote ~hash identity in
let votes = Verified_signature.Set.add vote Verified_signature.Set.empty in
let votes = Verified_signature.Set.elements votes in
let content = Deku_gossip.Message.Content.accepted ~block ~votes in
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;

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
with
| Some (Message.Network.Network_message { raw_header; raw_content }) -> (
let expected = Message.Header.decode ~raw_header in
let (Message.Message { content; _ }) =
Message.decode ~expected ~raw_content
in
match content with
| Content_accepted { block; votes } -> (block, votes)
| _ -> default_return)
| None -> default_return
in
Alcotest.(check' (pair block_testable (list vote_testable)))
~msg:"retrieved empty block and one vote equal saved"
~expected:(block, votes) ~actual:retrieved_block_and_votes;

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

let run () =
Eio_main.run (fun env ->
let open Alcotest in
Expand All @@ -74,6 +120,8 @@ let run () =
[
test_case "empty_block is returned" `Quick
(test_empty_block_load env);
test_case "empty block and one vote is returned" `Quick
(test_empty_block_and_votes env);
] );
])

Expand Down

0 comments on commit b840df2

Please sign in to comment.