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

Upgrade poc_mgr records before signing the receipts txn #1822

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{<<"base64url">>,{pkg,<<"base64url">>,<<"1.0.1">>},1},
{<<"blockchain">>,
{git,"https://github.com/helium/blockchain-core.git",
{ref,"dff66500c97bbd2ea9cb1ea181102f410769b7fd"}},
{ref,"83ed1aff3a7139870a1bde1896eec0d6bfb776eb"}},
0},
{<<"certifi">>,{pkg,<<"certifi">>,<<"2.9.0">>},2},
{<<"chatterbox">>,
Expand Down
41 changes: 39 additions & 2 deletions src/poc/miner_poc_mgr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
-define(LOCAL_POC_DB_CF, {?MODULE, local_poc_db_cf_handle}).
-define(LOCAL_POC_KEYS_DB_CF, {?MODULE, local_poc_keys_db_cf_handle}).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
%% lifespan of a POC, after which we will
%% submit the receipts txn and delete the local poc data
-define(POC_TIMEOUT, 4).
Expand Down Expand Up @@ -207,7 +208,7 @@ local_poc(OnionKeyHash) ->
case rocksdb:get(DB, CF, OnionKeyHash, []) of
{ok, Bin} ->
[POC] = erlang:binary_to_term(Bin),
{ok, POC};
{ok, upgrade_responses(POC)};
not_found ->
{error, not_found};
Error ->
Expand Down Expand Up @@ -930,6 +931,14 @@ update_addr_hash(Bloom, Element) ->
end
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),
POC#local_poc{responses=NewResponses}.

%% ------------------------------------------------------------------
%% DB functions
%% ------------------------------------------------------------------
Expand All @@ -941,7 +950,7 @@ local_pocs(Itr, {error, invalid_iterator}, Acc) ->
catch rocksdb:iterator_close(Itr),
Acc;
local_pocs(Itr, {ok, _, LocalPOCBin}, Acc) ->
local_pocs(Itr, rocksdb:iterator_move(Itr, next), [binary_to_term(LocalPOCBin)|Acc]).
local_pocs(Itr, rocksdb:iterator_move(Itr, next), [upgrade_responses(binary_to_term(LocalPOCBin))|Acc]).

local_poc_keys(#state{db=DB, local_poc_keys_cf=CF}) ->
{ok, Itr} = rocksdb:iterator(DB, CF, []),
Expand Down Expand Up @@ -977,3 +986,31 @@ write_local_poc_keys(LocalPOCKey = #poc_local_key_data{onion_key_hash = OnionKey
delete_local_poc_keys(OnionKeyHash, DB, CF) ->
rocksdb:delete(DB, CF, OnionKeyHash, []).

-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,
915.2,2,<<"dr">>}},
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 ->
blockchain_poc_witness_v1:print(E),
true
end, maps:values(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 ->
blockchain_poc_witness_v1:print(E),
true
end, maps:values(Responses))),
ok.

-endif.