Skip to content

Commit

Permalink
Add a test with a corrupted private key
Browse files Browse the repository at this point in the history
  • Loading branch information
Vagabond committed Jan 16, 2019
1 parent 3391a5d commit 26e0624
Showing 1 changed file with 60 additions and 3 deletions.
63 changes: 60 additions & 3 deletions test/hbbft_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
two_actors_missing_test/1,
encrypt_decrypt_test/1,
start_on_demand_test/1,
one_actor_wrong_key_test/1
one_actor_wrong_key_test/1,
one_actor_corrupted_key_test/1
]).

all() ->
Expand All @@ -24,7 +25,8 @@ all() ->
two_actors_missing_test,
encrypt_decrypt_test,
start_on_demand_test,
one_actor_wrong_key_test
one_actor_wrong_key_test,
one_actor_corrupted_key_test
].

init_per_testcase(_, Config) ->
Expand Down Expand Up @@ -284,9 +286,11 @@ one_actor_wrong_key_test(Config) ->
PrivateKeys0 = proplists:get_value(privatekeys, Config),
{ok, Dealer} = dealer:new(N, F+1, 'SS512'),
{ok, {_PubKey, PrivateKeys1}} = dealer:deal(Dealer),
%% give actor 1 a completely unrelated key
%% this will prevent it from doing any valid threshold cryptography
%% and thus it will not be able to reach consensus
PrivateKeys = [hd(PrivateKeys1)|tl(PrivateKeys0)],

ct:pal("Private keys ~p", [PrivateKeys]),
Workers = [ element(2, hbbft_worker:start_link(N, F, I, tpke_privkey:serialize(SK), BatchSize, false)) || {I, SK} <- enumerate(PrivateKeys) ],
Msgs = [ crypto:strong_rand_bytes(128) || _ <- lists:seq(1, N*20)],
%% feed the badgers some msgs
Expand Down Expand Up @@ -327,6 +331,59 @@ one_actor_wrong_key_test(Config) ->
io:format("chain contains ~p distinct transactions~n", [length(BlockTxns)]),
ok.

one_actor_corrupted_key_test(Config) ->
N = proplists:get_value(n, Config),
F = proplists:get_value(f, Config),
BatchSize = proplists:get_value(batchsize, Config),
PubKey = proplists:get_value(pubkey, Config),
[PK1|PrivateKeys0] = proplists:get_value(privatekeys, Config),
PKE = element(3, PK1),
%% scramble the private element of the key
%% this will not prevent the actor for encrypting their bundle
%% merely prevent it producing valid decryption shares
%% thus all the actors will be able to converge
PK2 = setelement(3, PK1, erlang_pbc:element_random(PKE)),
PrivateKeys = [PK2|PrivateKeys0],

Workers = [ element(2, hbbft_worker:start_link(N, F, I, tpke_privkey:serialize(SK), BatchSize, false)) || {I, SK} <- enumerate(PrivateKeys) ],
Msgs = [ crypto:strong_rand_bytes(128) || _ <- lists:seq(1, N*20)],
%% feed the badgers some msgs
lists:foreach(fun(Msg) ->
Destinations = random_n(rand:uniform(N), Workers),
io:format("destinations ~p~n", [Destinations]),
[ok = hbbft_worker:submit_transaction(Msg, D) || D <- Destinations]
end, Msgs),

%% wait for all the worker's mailboxes to settle and
%% wait for the chains to converge
ok = hbbft_ct_utils:wait_until(fun() ->
Chains = sets:from_list(lists:map(fun(W) ->
{ok, Blocks} = hbbft_worker:get_blocks(W),
Blocks
end, (Workers))),

0 == lists:sum([element(2, erlang:process_info(W, message_queue_len)) || W <- Workers ]) andalso
1 == sets:size(Chains) andalso
0 /= length(hd(sets:to_list(Chains)))
end, 60*2, 500),


Chains = sets:from_list(lists:map(fun(W) ->
{ok, Blocks} = hbbft_worker:get_blocks(W),
Blocks
end, (Workers))),
1 = sets:size(Chains),
[Chain] = sets:to_list(Chains),
io:format("chain is of height ~p~n", [length(Chain)]),
%% verify they are cryptographically linked
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)),
%% check they're all members of the original message list
true = sets:is_subset(sets:from_list(BlockTxns), sets:from_list(Msgs)),
io:format("chain contains ~p distinct transactions~n", [length(BlockTxns)]),
ok.

%% helper functions

Expand Down

0 comments on commit 26e0624

Please sign in to comment.