Skip to content

Commit

Permalink
Merge branch 'fix/fork-recovery-tx-anchor-blocks'
Browse files Browse the repository at this point in the history
  • Loading branch information
ldmberman committed Oct 14, 2019
2 parents e51ec0e + 08356bb commit 7e398b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/ar_fork_recovery.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-module(ar_fork_recovery).
-export([start/5]).
-export([start/6]).
-include("ar.hrl").
-include_lib("eunit/include/eunit.hrl").

Expand All @@ -25,15 +25,16 @@

%% @doc Start the fork recovery 'catch up' server.
%% TargetBShadow - a block shadow with the reconstructed hash list.
start(Peers, TargetBShadow, HashList, Parent, BlockTXPairs) ->
start(Peers, TrustedPeers, TargetBShadow, HashList, Parent, BlockTXPairs) ->
case ?IS_BLOCK(TargetBShadow) of
true ->
ar:info(
[
{started_fork_recovery_proc, Parent},
{block, ar_util:encode(TargetBShadow#block.indep_hash)},
{target_height, TargetBShadow#block.height},
{top_peers, lists:sublist(Peers, 5)}
{top_peers, lists:sublist(Peers, 5)},
{top_trusted_peers, lists:sublist(TrustedPeers, 5)}
]
),
% Ensures that the block is within the recovery range and is has
Expand All @@ -60,7 +61,7 @@ start(Peers, TargetBShadow, HashList, Parent, BlockTXPairs) ->
target_block = TargetB,
recovery_hash_list =
[TargetB#block.indep_hash|TargetB#block.hash_list],
block_txs_pairs = get_block_txs_pairs(BlockTXPairs, BlockList)
block_txs_pairs = get_block_txs_pairs(BlockTXPairs, BlockList, TrustedPeers)
}
)
end
Expand Down Expand Up @@ -92,7 +93,7 @@ start(Peers, TargetBShadow, HashList, Parent, BlockTXPairs) ->
drop_until_diverge([X | R1], [X | R2]) -> drop_until_diverge(R1, R2);
drop_until_diverge(R1, _) -> R1.

get_block_txs_pairs(BlockTXPairs, BHL) ->
get_block_txs_pairs(BlockTXPairs, BHL, TrustedPeers) ->
CutBlockTXPairs = cut_block_txs_pairs(BlockTXPairs, hd(BHL)),
case {length(BHL), length(CutBlockTXPairs)} of
{L, _} when L =< ?MAX_TX_ANCHOR_DEPTH ->
Expand All @@ -104,7 +105,7 @@ get_block_txs_pairs(BlockTXPairs, BHL) ->
lists:map(
fun(Depth) ->
BH = lists:nth(Depth, BHL),
B = ar_storage:read_block(BH, BHL),
B = read_or_fetch_block(BH, BHL, TrustedPeers),
{BH, B#block.txs}
end,
lists:seq(L + 1, ?MAX_TX_ANCHOR_DEPTH)
Expand All @@ -118,6 +119,14 @@ cut_block_txs_pairs([], _) ->
cut_block_txs_pairs([_ | Rest], BH) ->
cut_block_txs_pairs(Rest, BH).

read_or_fetch_block(BH, BHL, TrustedPeers) ->
case ar_storage:read_block(BH, BHL) of
unavailable ->
ar_node_utils:get_full_block(TrustedPeers, BH, BHL);
B ->
B
end.

%% @doc Subtract the second list from the first. If the first list
%% is not a superset of the second, return the empty list
setminus([X | R1], [X | R2]) -> setminus(R1, R2);
Expand Down
1 change: 1 addition & 0 deletions src/ar_node_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ fork_recover(#{ node := Node, hash_list := HashList, block_txs_pairs := BlockTXP
process,
PID = ar_fork_recovery:start(
PrioritisedPeers,
maps:get(trusted_peers, StateIn),
NewB,
HashList,
Node,
Expand Down

0 comments on commit 7e398b1

Please sign in to comment.