Skip to content

Commit

Permalink
fixup! fix: fix a race condition in ar_coordinated_mining_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPiechota committed Dec 24, 2024
1 parent 814558a commit d71645e
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions apps/arweave/test/ar_coordinated_mining_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -403,24 +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) ->
?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),
{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])),
?assert(lists:any(fun({MinerHash, _, _}) ->
ar_util:encode(Hash) == ar_util:encode(MinerHash)
end, MinerHashes), 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

0 comments on commit d71645e

Please sign in to comment.