Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Fix poc_mgr record upgrades again #1827

Merged
merged 4 commits into from
Oct 6, 2022
Merged
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
42 changes: 21 additions & 21 deletions src/poc/miner_poc_mgr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
target :: libp2p_crypto:pubkey_bin(),
onion :: binary() | undefined,
secret :: binary() | undefined,
responses = #{},
responses = #{} :: #{binary() => [{binary(), blockchain_poc_witness_v1:witness()}] | {binary(), blockchain_poc_receipt_v1:poc_receipt()}},
challengees = [] :: [libp2p_crypto:pubkey_bin()],
packet_hashes = [] :: [{libp2p_crypto:pubkey_bin(), binary()}],
start_height :: non_neg_integer()
Expand Down Expand Up @@ -932,11 +932,14 @@ update_addr_hash(Bloom, Element) ->
end.

upgrade_responses(#local_poc{responses=Responses}=POC) ->
NewResponses = maps:map(fun(_Key, Value) when element(1, Value) == blockchain_poc_receipt_v1_pb ->
blockchain_poc_receipt_v1:maybe_upgrade(Value);
(_Key, Value) when element(1, Value) == blockchain_poc_witness_v1_pb ->
blockchain_poc_witness_v1:maybe_upgrade(Value)
end, Responses),
NewResponses = maps:map(fun(_Key, Value) when is_list(Value) ->
%% witnesses are stored in a key/value list
lists:map(fun({Key, Witness}) ->
{Key, blockchain_poc_witness_v1:maybe_upgrade(Witness)}
end, Value);
(_Key, {Peer, Value}) when element(1, Value) == blockchain_poc_receipt_v1_pb ->
{Peer, blockchain_poc_receipt_v1:maybe_upgrade(Value)}
end, Responses),
POC#local_poc{responses=NewResponses}.

%% ------------------------------------------------------------------
Expand Down Expand Up @@ -989,28 +992,25 @@ delete_local_poc_keys(OnionKeyHash, DB, CF) ->
-ifdef(TEST).

upgrade_responses_test() ->
Responses = #{<<"a">> => {blockchain_poc_receipt_v1_pb,<<"r">>,10,10,<<"data">>,p2p,<<>>,
1.2,915.2,2,<<"dr">>,<<>>,0},
<<"b">> => {blockchain_poc_witness_v1_pb,<<"w1">>,10,10,<<"ph">>,<<>>,1.2,
915.2,2,<<"dr">>},
<<"c">> => {blockchain_poc_witness_v1_pb,<<"w2">>,10,10,<<"ph">>,<<>>,1.2,
Responses = #{<<"challengee">> => {<<"peer">>, {blockchain_poc_receipt_v1_pb,<<"r">>,10,10,<<"data">>,p2p,<<>>,
1.2,915.2,2,<<"dr">>,<<>>,0}},
<<"packethash">> => [{<<"2">>, {blockchain_poc_witness_v1_pb,<<"w1">>,10,10,<<"ph">>,<<>>,1.2,
915.2,2,<<"dr">>}},
%% the following should not need upgrading
{<<"3">>, {blockchain_poc_witness_v1_pb,<<"w2">>,10,10,<<"ph">>,<<>>,1.2,
915.2,2,<<"dr">>, 1.0}}]},
Upgraded = upgrade_responses(#local_poc{responses=Responses}),
lists:all(fun(E) when element(1, E) == blockchain_poc_receipt_v1_pb ->
blockchain_poc_receipt_v1:print(E),
true;
(E) when element(1, E) == blockchain_poc_witness_v1_pb ->
lists:all(fun(E) when element(1, E) == blockchain_poc_witness_v1_pb ->
blockchain_poc_witness_v1:print(E),
true
end, maps:values(Upgraded#local_poc.responses)),
end, element(2, lists:unzip(lists:flatten(maps:get(<<"packethash">>, Upgraded#local_poc.responses))))),
blockchain_poc_receipt_v1:print(element(2, maps:get(<<"challengee">>, Upgraded#local_poc.responses))),

?assertError(function_clause, lists:all(fun(E) when element(1, E) == blockchain_poc_receipt_v1_pb ->
blockchain_poc_receipt_v1:print(E),
true;
(E) when element(1, E) == blockchain_poc_witness_v1_pb ->
?assertError(function_clause, lists:all(fun(E) when element(1, E) == blockchain_poc_witness_v1_pb ->
blockchain_poc_witness_v1:print(E),
true
end, maps:values(Responses))),
end, element(2, lists:unzip(lists:flatten(maps:get(<<"packethash">>, Responses)))))),
?assertError(function_clause, blockchain_poc_receipt_v1:print(element(2, maps:get(<<"challengee">>, Responses)))),
ok.

-endif.