Skip to content

Commit

Permalink
add verify_block_fit function to worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Garg committed May 1, 2018
1 parent c9c0fa7 commit f745a14
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test/hbbft_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
19 changes: 19 additions & 0 deletions test/hbbft_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit f745a14

Please sign in to comment.