Skip to content

Commit

Permalink
fix: fix a race condition in ar_coordinated_mining_tests
Browse files Browse the repository at this point in the history
Sometimes 2 or more blocks are mined concurrently which can
mess with the wait_until_height logic
  • Loading branch information
JamesPiechota committed Dec 24, 2024
1 parent adcb37e commit 814558a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions apps/arweave/test/ar_coordinated_mining_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,17 @@ mine_in_parallel(Miners, ValidatorNode, CurrentHeight) ->
[{Hash, _, _} | _] = ar_test_node:wait_until_height(ValidatorNode, CurrentHeight + 1),
lists:foreach(
fun(Node) ->
[{MinerHash, _, _} | _] = ar_test_node:wait_until_height(Node, CurrentHeight + 1),
?LOG_DEBUG([{test, ar_coordinated_mining_tests},
{waiting_for_height, CurrentHeight+1}, {node, Node}]),
%% Since multiple nodes are mining in parallel it's possible that 2 blocks
%% get mined one after each other very quickly. In that scenario MinerHashes
%% might include 2 (or more) new blocks so we need to check all of them.
MinerHashes = ar_test_node:wait_until_height(Node, CurrentHeight + 1),
Message = lists:flatten(io_lib:format(
"Node ~p did not mine the same block as the validator node", [Node])),
?assertEqual(ar_util:encode(Hash), ar_util:encode(MinerHash), Message)
?assert(lists:any(fun({MinerHash, _, _}) ->
ar_util:encode(Hash) == ar_util:encode(MinerHash)
end, MinerHashes), Message)
end,
Miners
),
Expand Down
2 changes: 1 addition & 1 deletion apps/arweave/test/ar_test_node.erl
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ wait_until_height(TargetHeight) ->
{ok, BI} = ar_util:do_until(
fun() ->
case ar_node:get_blocks() of
BI when length(BI) - 1 == TargetHeight ->
BI when length(BI) - 1 >= TargetHeight ->
{ok, BI};
_ ->
false
Expand Down

0 comments on commit 814558a

Please sign in to comment.