Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ar_data_sync: Skip unpacking if the chunk was downloaded from the local peer #637

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/arweave/src/ar_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ register() ->
]),
prometheus_gauge:new([
{name, v2_index_data_size_by_packing},
{labels, [store_id, packing, partition_number, storage_module_size, storage_module_index]},
{labels, [store_id, packing, partition_number, storage_module_size, storage_module_index,
packing_difficulty]},
{help, "The size (in bytes) of the data stored and indexed. Grouped by the "
"store ID, packing, partition number, storage module size, "
"and storage module index."}
"storage module index, and packing difficulty."}
]),

%% Disk pool.
Expand Down
7 changes: 5 additions & 2 deletions apps/arweave/src/ar_mining_stats.erl
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ set_storage_module_data_size(
StoreID, Packing, PartitionNumber, StorageModuleSize, StorageModuleIndex, DataSize) ->
StoreLabel = ar_storage_module:label_by_id(StoreID),
PackingLabel = ar_storage_module:packing_label(Packing),
try
try
PackingDifficulty = get_packing_difficulty(Packing),
prometheus_gauge:set(v2_index_data_size_by_packing,
[StoreLabel, PackingLabel, PartitionNumber, StorageModuleSize, StorageModuleIndex],
[StoreLabel, PackingLabel, PartitionNumber,
StorageModuleSize, StorageModuleIndex,
PackingDifficulty],
DataSize),
ets:insert(?MODULE, {
{partition, PartitionNumber, storage_module, StoreID, packing, Packing}, DataSize})
Expand Down
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
Loading