From 4e5b6c49ab9021743b5eb9d9b916acd793b84948 Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Thu, 12 May 2022 19:35:35 -0700 Subject: [PATCH 1/3] More verbose ledger_poc_v3:verify error --- src/ledger/v1/blockchain_ledger_poc_v3.erl | 20 +++++++++++++++++-- .../v2/blockchain_txn_poc_receipts_v2.erl | 10 +++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/ledger/v1/blockchain_ledger_poc_v3.erl b/src/ledger/v1/blockchain_ledger_poc_v3.erl index 2081731fac..526c0c5130 100644 --- a/src/ledger/v1/blockchain_ledger_poc_v3.erl +++ b/src/ledger/v1/blockchain_ledger_poc_v3.erl @@ -82,9 +82,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, any()}. 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..f9fe231e70 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 @@ -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 -> From 1cf7419fd91e6774b7dec588306595384cdd569a Mon Sep 17 00:00:00 2001 From: Rahul Garg <3199183+vihu@users.noreply.github.com> Date: Wed, 18 May 2022 09:55:03 -0700 Subject: [PATCH 2/3] Update src/ledger/v1/blockchain_ledger_poc_v3.erl Co-authored-by: Siraaj Khandkar --- src/ledger/v1/blockchain_ledger_poc_v3.erl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ledger/v1/blockchain_ledger_poc_v3.erl b/src/ledger/v1/blockchain_ledger_poc_v3.erl index 526c0c5130..d504690e38 100644 --- a/src/ledger/v1/blockchain_ledger_poc_v3.erl +++ b/src/ledger/v1/blockchain_ledger_poc_v3.erl @@ -82,7 +82,18 @@ start_height(PoC) -> start_height(Height, PoC) -> PoC#poc_v3{start_height=Height}. --spec verify(poc(), libp2p_crypto:pubkey_bin(), binary()) -> ok | {error, any()}. +-spec verify(poc(), libp2p_crypto:pubkey_bin(), binary()) -> ok | {error, Reason} + when + Reason :: + [ + {onion_key_hash , binary()}, + {txn_challenger , string()}, + {ledger_challenger, string()}, + {txn_blockhash , string()}, + {ledger_blockhash , string()}, + {equal_challenger , boolean()}, + {equal_blockhash , boolean()} + ]. verify(PoC, Challenger, BlockHash) -> POCChallenger = ?MODULE:challenger(PoC), POCBlockHash = ?MODULE:block_hash(PoC), From 12860ea434228e17e99255453013f3482adfbd82 Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Wed, 18 May 2022 10:13:01 -0700 Subject: [PATCH 3/3] Address review cmts --- src/ledger/v1/blockchain_ledger_poc_v3.erl | 27 +++++++++---------- .../v2/blockchain_txn_poc_receipts_v2.erl | 2 +- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/ledger/v1/blockchain_ledger_poc_v3.erl b/src/ledger/v1/blockchain_ledger_poc_v3.erl index d504690e38..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,18 +90,7 @@ start_height(PoC) -> start_height(Height, PoC) -> PoC#poc_v3{start_height=Height}. --spec verify(poc(), libp2p_crypto:pubkey_bin(), binary()) -> ok | {error, Reason} - when - Reason :: - [ - {onion_key_hash , binary()}, - {txn_challenger , string()}, - {ledger_challenger, string()}, - {txn_blockhash , string()}, - {ledger_blockhash , string()}, - {equal_challenger , boolean()}, - {equal_blockhash , boolean()} - ]. +-spec verify(poc(), libp2p_crypto:pubkey_bin(), binary()) -> ok | {error, poc_verification_error()}. verify(PoC, Challenger, BlockHash) -> POCChallenger = ?MODULE:challenger(PoC), POCBlockHash = ?MODULE:block_hash(PoC), @@ -102,13 +99,13 @@ verify(PoC, Challenger, BlockHash) -> case (C1 andalso C2) of true -> ok; false -> - ErrorRes = [{onion_key_hash, ?MODULE:onion_key_hash(PoC)}, + 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}], + {equal_blockhash, C2}}, {error, ErrorRes} end. diff --git a/src/transactions/v2/blockchain_txn_poc_receipts_v2.erl b/src/transactions/v2/blockchain_txn_poc_receipts_v2.erl index f9fe231e70..472fa0eba7 100644 --- a/src/transactions/v2/blockchain_txn_poc_receipts_v2.erl +++ b/src/transactions/v2/blockchain_txn_poc_receipts_v2.erl @@ -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