From f745a149ad2b663bb07763902dec8e8cc9af763d Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Tue, 1 May 2018 14:45:17 -0700 Subject: [PATCH] add verify_block_fit function to worker --- test/hbbft_SUITE.erl | 2 +- test/hbbft_worker.erl | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/test/hbbft_SUITE.erl b/test/hbbft_SUITE.erl index b658c3c..7796661 100644 --- a/test/hbbft_SUITE.erl +++ b/test/hbbft_SUITE.erl @@ -51,7 +51,7 @@ simple_test(_Config) -> [Chain] = sets:to_list(Chains), io:format("chain is of height ~p~n", [length(Chain)]), %% verify they are cryptographically linked - hbbft_worker:verify_chain(Chain, PubKey), + true = hbbft_worker:verify_chain(Chain, PubKey), %% check all the transactions are unique BlockTxns = lists:flatten([ hbbft_worker:block_transactions(B) || B <- Chain ]), true = length(BlockTxns) == sets:size(sets:from_list(BlockTxns)), diff --git a/test/hbbft_worker.erl b/test/hbbft_worker.erl index a730363..ab529f4 100644 --- a/test/hbbft_worker.erl +++ b/test/hbbft_worker.erl @@ -68,6 +68,25 @@ verify_chain([A, B|_]=Chain, PubKey) -> false end. +verify_block_fit([A, B | _], PubKey) -> + %% A should have the the prev_hash of B + case A#block.prev_hash == hash_block(B) of + true -> + %% A should have a valid signature + HM = tpke_pubkey:hash_message(PubKey, term_to_binary(A#block{signature= <<>>})), + Signature = tpke_pubkey:deserialize_element(PubKey, A#block.signature), + case tpke_pubkey:verify_signature(PubKey, Signature, HM) of + true -> + true; + false -> + io:format("bad signature~n"), + false + end; + false -> + io:format("parent hash mismatch ~p ~p~n", [A#block.prev_hash, hash_block(B)]), + false + end. + block_transactions(Block) -> Block#block.transactions.