Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.
Open
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
30 changes: 27 additions & 3 deletions src/ledger/v1/blockchain_ledger_poc_v3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@
-type poc_result_types() :: [poc_result_type()].
-type poc() :: #poc_v3{}.
-type pocs() :: [poc()].
-export_type([poc/0, pocs/0, poc_result_type/0, poc_result_types/0]).
-type poc_verification_error() :: {{onion_key_hash , binary()},
{txn_challenger , string()},
{ledger_challenger, string()},
{txn_blockhash , string()},
{ledger_blockhash , string()},
{equal_challenger , boolean()},
{equal_blockhash , boolean()}}.

-export_type([poc/0, pocs/0, poc_result_type/0, poc_result_types/0, poc_verification_error/0]).

-spec new(binary(), libp2p_crypto:pubkey_bin(), binary(), pos_integer()) -> poc().
new(OnionKeyHash, Challenger, BlockHash, StartHeight) ->
Expand Down Expand Up @@ -82,9 +90,25 @@ start_height(PoC) ->
start_height(Height, PoC) ->
PoC#poc_v3{start_height=Height}.

-spec verify(poc(), libp2p_crypto:pubkey_bin(), binary()) -> boolean().
-spec verify(poc(), libp2p_crypto:pubkey_bin(), binary()) -> ok | {error, poc_verification_error()}.
verify(PoC, Challenger, BlockHash) ->
?MODULE:challenger(PoC) =:= Challenger andalso ?MODULE:block_hash(PoC) =:= BlockHash.
POCChallenger = ?MODULE:challenger(PoC),
POCBlockHash = ?MODULE:block_hash(PoC),
C1 = POCChallenger =:= Challenger,
C2 = POCBlockHash =:= BlockHash,
case (C1 andalso C2) of
true -> ok;
false ->
ErrorRes = {{onion_key_hash, ?MODULE:onion_key_hash(PoC)},
{txn_challenger, libp2p_crypto:bin_to_b58(Challenger)},
{ledger_challenger, libp2p_crypto:bin_to_b58(POCChallenger)},
{txn_blockhash, libp2p_crypto:bin_to_b58(BlockHash)},
{ledger_blockhash, libp2p_crypto:bin_to_b58(POCBlockHash)},
{equal_challenger, C1},
{equal_blockhash, C2}},

{error, ErrorRes}
end.

-spec serialize(poc()) -> binary().
serialize(PoC) ->
Expand Down
12 changes: 6 additions & 6 deletions src/transactions/v2/blockchain_txn_poc_receipts_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ absorb(_POCVersion, Txn, Chain) ->
throw(replay)
end,
case blockchain_ledger_poc_v3:verify(PoC, Challenger, BlockHash) of
false ->
{error, invalid_poc};
true ->
{error, R} ->
{error, {invalid_poc, R}};
ok ->
%% maybe update the last activity field for all challengees and GWs
%% participating in the POC
case blockchain:config(?poc_activity_filter_enabled, Ledger) of
Expand Down Expand Up @@ -1181,7 +1181,7 @@ poc_version(Ledger) ->
Txn :: txn_poc_receipts(),
PoC :: blockchain_ledger_poc_v3:poc(),
Keys :: map()
) -> ok | {error, atom()}.
) -> ok | {error, atom()} | {error, {mismatched_poc, blockchain_ledger_poc_v3:poc_verification_error()}}.
verify_poc_details(Txn, PoC, Keys) ->
%% verify the secret (pub and priv keys) submitted by the challenger
%% are a valid key pair
Expand All @@ -1196,8 +1196,8 @@ verify_poc_details(Txn, PoC, Keys) ->
SigFun = libp2p_crypto:mk_sig_fun(PrivKey),
SignedPayload = SigFun(OnionHash),
case blockchain_ledger_poc_v3:verify(PoC, Challenger, BlockHash) of
false -> {error, mismatched_poc};
true ->
{error, R} -> {error, {mismatched_poc, R}};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to update the spec of verify_poc_details/3 with this new error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 12860ea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did it slightly differently though and made the error return a tuple of tuples.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sensible. So close to a record!

ok ->
case POCOnionKeyHash == OnionHash of
false -> {error, mismatched_onion_key_hash};
true ->
Expand Down