Skip to content

Commit

Permalink
Ordering bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPiechota committed Jul 12, 2024
1 parent 9e18942 commit 1a2d357
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
31 changes: 19 additions & 12 deletions apps/arweave/src/ar_info.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ get_recent() ->
#{
%% #{
%% "id": <indep_hash>,
%% "received": <received_timestamp>"
%% "received": <received_timestamp>",
%% "height": <height>
%% }
<<"blocks">> => get_recent_blocks(),
%% #{
Expand All @@ -56,19 +57,27 @@ get_recent() ->
}.

%% @doc Return the the most recent blocks in reverse chronological order.
%%
%% There are a few list reversals that happen here:
%% 1. get_block_anchors returns the blocks in reverse chronological order (latest block first)
%% 2. [Element | Acc] reverses the list into chronological order (latest block last)
%% 3. The final lists:reverse puts the list back into reverse chronological order
%% (latest block first)
get_recent_blocks() ->
Anchors = lists:sublist(ar_node:get_block_anchors(), ?CHECKPOINT_DEPTH),
CutOff = length(Anchors) - ?RECENT_BLOCKS_WITHOUT_TIMESTAMP,
lists:foldl(
Blocks = lists:foldl(
fun(H, Acc) ->
B = ar_block_cache:get(block_cache, H),
[#{
<<"id">> => ar_util:encode(H),
<<"received">> => get_block_timestamp(H, length(Acc), CutOff)
<<"received">> => get_block_timestamp(B, length(Acc)),
<<"height">> => B#block.height
} | Acc]
end,
[],
Anchors
).
),
lists:reverse(Blocks).

%% @doc Return the the most recent forks in reverse chronological order.
get_recent_forks() ->
Expand All @@ -93,12 +102,10 @@ get_recent_forks() ->
)
end.

get_block_timestamp(H, Depth, CutOff) when Depth >= CutOff ->
get_block_timestamp(B, Depth)
when Depth < ?RECENT_BLOCKS_WITHOUT_TIMESTAMP orelse
B#block.receive_timestamp =:= undefined ->
<<"pending">>;
get_block_timestamp(H, _Depth, _Cutoff) ->
B = ar_block_cache:get(block_cache, H),
case B#block.receive_timestamp of
undefined -> <<"pending">>;
Timestamp -> ar_util:timestamp_to_seconds(Timestamp)
end.
get_block_timestamp(B, _Depth) ->
ar_util:timestamp_to_seconds(B#block.receive_timestamp).

25 changes: 19 additions & 6 deletions apps/arweave/test/ar_info_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ test_recent_blocks(Type) ->
ar_test_node:start_peer(peer1, B0),
GenesisBlock = [#{
<<"id">> => ar_util:encode(B0#block.indep_hash),
<<"received">> => <<"pending">>
<<"received">> => <<"pending">>,
<<"height">> => 0
}],
?assertEqual(GenesisBlock, get_recent(ar_test_node:peer_ip(peer1), blocks)),

Expand Down Expand Up @@ -67,7 +68,9 @@ test_recent_blocks(Type) ->
ar_test_node:peer_ip(peer1), Announcement)
end
end,
lists:sublist(PeerBI, TargetHeight)
%% Reverse the list so that the peer receives the blocks in the same order they
%% were mined.
lists:reverse(lists:sublist(PeerBI, TargetHeight))
),

%% Peer1 recent should now have timestamps, but also black out the most recent
Expand All @@ -78,30 +81,40 @@ test_recent_blocks(Type) ->
expected_blocks(Node, BI) ->
expected_blocks(Node, BI, false).
expected_blocks(Node, BI, ForcePending) ->
lists:foldl(
%% There are a few list reversals that happen here:
%% 1. BI has the blocks in reverse chronological order (latest block first)
%% 2. [Element | Acc] reverses the list into chronological order (latest block last)
%% 3. The final lists:reverse puts the list back into reverse chronological order
%% (latest block first)
Blocks = lists:foldl(
fun({H, _WeaveSize, _TXRoot}, Acc) ->
B = ar_test_node:remote_call(Node, ar_block_cache, get, [block_cache, H]),
Timestamp = case ForcePending of
true -> <<"pending">>;
false ->
case length(Acc) > (?CHECKPOINT_DEPTH - ?RECENT_BLOCKS_WITHOUT_TIMESTAMP - 1) of
case length(Acc) < ?RECENT_BLOCKS_WITHOUT_TIMESTAMP of
true -> <<"pending">>;
false -> ar_util:timestamp_to_seconds(B#block.receive_timestamp)
end
end,
[#{
<<"id">> => ar_util:encode(H),
<<"received">> => Timestamp
<<"received">> => Timestamp,
<<"height">> => B#block.height
} | Acc]
end,
[],
lists:sublist(BI, ?CHECKPOINT_DEPTH)
).
),
lists:reverse(Blocks).

%% -------------------------------------------------------------------------------------------
%% Recent forks tests
%% -------------------------------------------------------------------------------------------
test_get_recent_forks() ->
[B0] = ar_weave:init([]),
ar_test_node:start(B0),

ForkRootB1 = #block{ height = 1 },
ForkRootB2= #block{ height = 2 },

Expand Down

0 comments on commit 1a2d357

Please sign in to comment.