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 e8f9970 commit 60bf902
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions apps/arweave/test/ar_coordinated_mining_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -403,17 +403,30 @@ mine_in_parallel(Miners, ValidatorNode, CurrentHeight) ->
ar_util:pmap(fun(Node) -> ar_test_node:mine(Node) end, Miners),
?debugFmt("Waiting until the validator node (port ~B) advances to height ~B.",
[ar_test_node:peer_port(ValidatorNode), CurrentHeight + 1]),
[{Hash, _, _} | _] = ar_test_node:wait_until_height(ValidatorNode, CurrentHeight + 1),
BIValidator = ar_test_node:wait_until_height(ValidatorNode, CurrentHeight + 1),
%% Since multiple nodes are mining in parallel it's possible that multiple blocks
%% were mined. Get the Validator's current height in cas it's more than CurrentHeight+1.
NewHeight = ar_test_node:remote_call(ValidatorNode, ar_node, get_height, []),

Hashes = [Hash || {Hash, _, _} <- lists:sublist(BIValidator, NewHeight - CurrentHeight)],

lists:foreach(
fun(Node) ->
[{MinerHash, _, _} | _] = ar_test_node:wait_until_height(Node, CurrentHeight + 1),
?LOG_DEBUG([{test, ar_coordinated_mining_tests},
{waiting_for_height, NewHeight}, {node, Node}]),
%% Make sure the miner contains all of the new validator hashes, it's okay if
%% the miner contains *more* hashes since it's possible concurrent blocks were
%% mined between when the Validator checked and now.
BIMiner = ar_test_node:wait_until_height(Node, NewHeight),
MinerHashes = [Hash || {Hash, _, _} <- BIMiner],
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:all(fun(Hash) -> lists:member(Hash, MinerHashes) end, Hashes), Message)
end,
Miners
),
{ok, Block} = ar_test_node:http_get_block(Hash, ValidatorNode),
LatestHash = lists:last(Hashes),
{ok, Block} = ar_test_node:http_get_block(LatestHash, ValidatorNode),

case Block#block.recall_byte2 of
undefined ->
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 60bf902

Please sign in to comment.