Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
validate size and hash after download
Browse files Browse the repository at this point in the history
  • Loading branch information
joecaswell committed Mar 16, 2022
1 parent b706e45 commit 8b7aa76
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/blockchain_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,16 @@ set_filename(BaseFilename, true) ->
set_filename(BaseFilename, false) ->
BaseFilename.

attempt_fetch_snap_source_snapshot(BaseUrl, #snapshot_info{height = Height, file_hash = Hash,
attempt_fetch_snap_source_snapshot(BaseUrl, SnapInfo) ->
case validate_snapshot_file(SnapInfo) of
{ok, Filename} ->
{ok, Filename}
{invalid, Filename, Filepath ->
_ = do_snap_source_download(build_url(BaseUrl, Filename), Filepath),
validate_snapshot_file(SnapInfo)
end.

validate_snapshot_file(#snapshot_info{height = Height, file_hash = Hash,
file_size = Size, is_compressed = IsCompressed}) ->
%% httpc and ssl applications are started in the top level blockchain supervisor
Filename = set_filename(build_filename(Height), IsCompressed),
Expand Down Expand Up @@ -1240,17 +1249,15 @@ attempt_fetch_snap_source_snapshot(BaseUrl, #snapshot_info{height = Height, file
lager:info("Already have snapshot file for height ~p with hash ~p", [Height, Hash]),
ok = file:write_file(HashStateFile, Hash),
{ok, Filepath};
{smaller, _} ->
%% see if this is a a resumable download
_ = do_snap_source_download(build_url(BaseUrl, Filename), Filepath);
_ ->
%% file is bigger than it should be, or the hash is wrong, scrap it
%% file size or the hash is wrong, scrap it,
%% we'll try to resume from the scratch file, if there is one
safe_delete(Filename),
_ = do_snap_source_download(build_url(BaseUrl, Filename), Filepath)
{invalid, Filename, Filepath}
end
end;
false ->
_ = do_snap_source_download(build_url(BaseUrl, Filename), Filepath)
%% no file? same as failed hash
false -> {invalid, Filename, Filepath}
end.

same_stored_hash(File, Hash) ->
Expand Down

0 comments on commit 8b7aa76

Please sign in to comment.