Skip to content

Commit

Permalink
Merge pull request #648 from ArweaveTeam/fix/missing_poa_cache2
Browse files Browse the repository at this point in the history
Initialize poa caches for initial block cache blocks
  • Loading branch information
vird authored Nov 25, 2024
2 parents b6df9c2 + 84ba1ea commit 0ebfa93
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions apps/arweave/src/ar_node_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,12 @@ handle_info({event, node_state, _Event}, State) ->
handle_info({event, nonce_limiter, initialized}, State) ->
[{_, {Height, Blocks, BI}}] = ets:lookup(node_state, join_state),
ar_storage:store_block_index(BI),
B = hd(Blocks),
RecentBI = lists:sublist(BI, ?BLOCK_INDEX_HEAD_LEN),
Current = element(1, hd(RecentBI)),
RecentBlocks = lists:sublist(Blocks, ?STORE_BLOCKS_BEHIND_CURRENT),
RecentBlocks2 = set_poa_caches(RecentBlocks),
ar_block_cache:initialize_from_list(block_cache, RecentBlocks2),
B = hd(RecentBlocks2),
RewardHistory = [{H, {Addr, HashRate, Reward, Denomination}}
|| {{Addr, HashRate, Reward, Denomination}, {H, _, _}}
<- lists:zip(B#block.reward_history,
Expand All @@ -394,15 +399,11 @@ handle_info({event, nonce_limiter, initialized}, State) ->
<- lists:zip(B#block.block_time_history,
lists:sublist(BI, length(B#block.block_time_history)))],
ar_storage:store_block_time_history_part2(BlockTimeHistory),
RecentBI = lists:sublist(BI, ?BLOCK_INDEX_HEAD_LEN),
Height = B#block.height,
ar_disk_cache:write_block(B),
ar_data_sync:join(RecentBI),
ar_header_sync:join(Height, RecentBI, Blocks),
ar_tx_blacklist:start_taking_down(),
Current = element(1, hd(RecentBI)),
ar_block_cache:initialize_from_list(block_cache,
lists:sublist(Blocks, ?STORE_BLOCKS_BEHIND_CURRENT)),
BlockTXPairs = [block_txs_pair(Block) || Block <- Blocks],
{BlockAnchors, RecentTXMap} = get_block_anchors_and_recent_txs_map(BlockTXPairs),
{Rate, ScheduledRate} = {B#block.usd_to_ar_rate, B#block.scheduled_usd_to_ar_rate},
Expand Down Expand Up @@ -1755,6 +1756,39 @@ read_recent_blocks3([{BH, _, _} | BI], BlocksToRead, Blocks) ->
not_found
end.

set_poa_caches([]) ->
[];
set_poa_caches([B | Blocks]) ->
[set_poa_cache(B) | set_poa_caches(Blocks)].

set_poa_cache(B) ->
PoA1 = B#block.poa,
PoA2 = B#block.poa2,
MiningAddress = B#block.reward_addr,
PackingDifficulty = B#block.packing_difficulty,
Nonce = B#block.nonce,
RecallByte1 = B#block.recall_byte,
RecallByte2 = B#block.recall_byte2,
Packing = ar_block:get_packing(PackingDifficulty, MiningAddress),
PoACache = compute_poa_cache(B, PoA1, RecallByte1, Nonce, Packing),
B2 = B#block{ poa_cache = PoACache },
%% Compute PoA2 cache if PoA2 is present.
case RecallByte2 of
undefined ->
B2;
_ ->
PoA2Cache = compute_poa_cache(B, PoA2, RecallByte2, Nonce, Packing),
B2#block{ poa2_cache = PoA2Cache }
end.

compute_poa_cache(B, PoA, RecallByte, Nonce, Packing) ->
PackingDifficulty = B#block.packing_difficulty,
SubChunkIndex = ar_block:get_sub_chunk_index(PackingDifficulty, Nonce),
{BlockStart, BlockEnd, TXRoot} = ar_block_index:get_block_bounds(RecallByte),
BlockSize = BlockEnd - BlockStart,
ChunkID = ar_tx:generate_chunk_id(PoA#poa.chunk),
{{BlockStart, RecallByte, TXRoot, BlockSize, Packing, SubChunkIndex}, ChunkID}.

dump_mempool(TXs, MempoolSize) ->
SerializedTXs = maps:map(fun(_, {TX, St}) -> {ar_serialize:tx_to_binary(TX), St} end, TXs),
case ar_storage:write_term(mempool, {SerializedTXs, MempoolSize}) of
Expand Down

0 comments on commit 0ebfa93

Please sign in to comment.