Skip to content

Commit

Permalink
Ensure ar_block_cache is only update from within ar_node_worker
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPiechota committed Jul 10, 2024
1 parent 319984a commit e6b2dd1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
26 changes: 13 additions & 13 deletions apps/arweave/src/ar_block_cache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ get(Tab, H) ->
B
end.

%% @doc Get the block and its status from cache.
%% Returns not_found if the block is not in cache.
get_block_and_status(Tab, H) ->
case ets:lookup(Tab, {block, H}) of
[] ->
not_found;
[{_, {B, Status, Timestamp, _Children}}] ->
{B, {Status, Timestamp}}
end.

%% @doc Get a {block, previous blocks, status} tuple for the earliest block from
%% the longest chain, which has not been validated yet. The previous blocks are
%% sorted from newest to oldest. The last one is a block from the current fork.
Expand Down Expand Up @@ -297,16 +307,6 @@ tx_id(#tx{ id = ID }) ->
tx_id(TXID) ->
TXID.

%% @doc Get the block and its status from cache.
%% Returns not_found if the block is not in cache.
get_block_and_status(Tab, H) ->
case ets:lookup(Tab, {block, H}) of
[] ->
not_found;
[{_, {B, Status, Timestamp, _Children}}] ->
{B, {Status, Timestamp}}
end.

%% @doc Mark the given block as the tip block. Mark the previous blocks as on-chain.
%% Mark the on-chain blocks from other forks as validated. Raises invalid_tip if
%% one of the preceeding blocks is not validated. Raises not_found if the block
Expand Down Expand Up @@ -458,6 +458,8 @@ get_siblings(Tab, B) ->

update_timestamp(Tab, H, ReceiveTimestamp) ->
case ets:lookup(Tab, {block, H}) of
[] ->
not_found;
[{_, {B, Status, Timestamp, Children}}] ->
case B#block.receive_timestamp of
undefined ->
Expand All @@ -472,9 +474,7 @@ update_timestamp(Tab, H, ReceiveTimestamp) ->
}, false);
_ ->
ok
end;
[] ->
not_found
end
end.

%%%===================================================================
Expand Down
2 changes: 1 addition & 1 deletion apps/arweave/src/ar_events_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ init([]) ->
%% Events: new, ready_for_mining, orphaned, emitting_scheduled,
%% preparing_unblacklisting, ready_for_unblacklisting, registered_offset.
?CHILD(ar_events, tx, worker),
%% Events: discovered, rejected, new, double_signing.
%% Events: discovered, rejected, new, double_signing, mined_block_received.
?CHILD(ar_events, block, worker),
%% Events: unpacked, packed.
?CHILD(ar_events, chunk, worker),
Expand Down
19 changes: 17 additions & 2 deletions apps/arweave/src/ar_http_iface_middleware.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2321,7 +2321,7 @@ handle_block_announcement(#block_announcement{ indep_hash = H, previous_block =
solution_hash = SolutionH }, Req) ->
case ar_ignore_registry:member(H) of
true ->
ar_block_cache:update_timestamp(block_cache, H, erlang:timestamp()),
check_block_receive_timestamp(H),
{208, #{}, <<>>, Req};
false ->
case ar_node:get_block_shadow_from_cache(PrevH) of
Expand Down Expand Up @@ -2438,7 +2438,7 @@ post_block(check_block_hash_header, Peer, {Req, Pid, Encoding}, ReceiveTimestamp
{ok, BH} when byte_size(BH) =< 48 ->
case ar_ignore_registry:member(BH) of
true ->
ar_block_cache:update_timestamp(block_cache, BH, ReceiveTimestamp),
check_block_receive_timestamp(BH),
{208, #{}, <<"Block already processed.">>, Req};
false ->
post_block(read_body, Peer, {Req, Pid, Encoding},
Expand Down Expand Up @@ -2508,6 +2508,21 @@ post_block(enqueue_block, {B, Peer}, Req, ReceiveTimestamp) ->
end,
{200, #{}, <<"OK">>, Req}.

check_block_receive_timestamp(H) ->
case ar_block_cache:get(block_cache, H) of
not_found ->
not_found;
B ->
case B#block.receive_timestamp of
undefined ->
%% This node mined block H and this is the first time it's been
%% gossipped back to it. Update the node's receive_timestamp.
ar_events:send(block, {mined_block_received, H, erlang:timestamp()});
_ ->
ok
end
end.

handle_post_partial_solution(Req, Pid) ->
{ok, Config} = application:get_env(arweave, config),
CMExitNode = ar_coordination:is_exit_peer() andalso ar_pool:is_client(),
Expand Down
4 changes: 4 additions & 0 deletions apps/arweave/src/ar_node_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ handle_info({event, block, {new, B, _Source}}, State) ->
{noreply, State}
end;

handle_info({event, block, {mined_block_received, H, ReceiveTimestamp}}, State) ->
ar_block_cache:update_timestamp(block_cache, H, ReceiveTimestamp),
{noreply, State};

handle_info({event, block, _}, State) ->
{noreply, State};

Expand Down

0 comments on commit e6b2dd1

Please sign in to comment.