diff --git a/src/ledger/v1/blockchain_ledger_poc_v3.erl b/src/ledger/v1/blockchain_ledger_poc_v3.erl index 2081731fac..8f5b5b3f7e 100644 --- a/src/ledger/v1/blockchain_ledger_poc_v3.erl +++ b/src/ledger/v1/blockchain_ledger_poc_v3.erl @@ -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) -> @@ -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) -> diff --git a/src/transactions/v2/blockchain_txn_poc_receipts_v2.erl b/src/transactions/v2/blockchain_txn_poc_receipts_v2.erl index 420602a0ba..472fa0eba7 100644 --- a/src/transactions/v2/blockchain_txn_poc_receipts_v2.erl +++ b/src/transactions/v2/blockchain_txn_poc_receipts_v2.erl @@ -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 @@ -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 @@ -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}}; + ok -> case POCOnionKeyHash == OnionHash of false -> {error, mismatched_onion_key_hash}; true ->