-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbootstrap.cpp
121 lines (100 loc) · 3.61 KB
/
bootstrap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2024 The DECENOMY Core Developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "bootstrap.h"
#ifdef HAVE_CONFIG_H
#include "config/pivx-config.h"
#endif
#include "amount.h"
#include "chainparams.h"
#include "curl.h"
#include "guiinterface.h"
#include "util.h"
#include "zip.h"
#include <iostream>
namespace fs = boost::filesystem;
std::string extractFilenameFromURL(const std::string& url)
{
// Find the last '/' in the URL, which is expected to precede the filename
size_t last_slash_pos = url.find_last_of("/");
// Check if the last slash position is valid and there are characters after the '/'
if (last_slash_pos != std::string::npos && last_slash_pos + 1 < url.size()) {
return url.substr(last_slash_pos + 1);
}
// If no '/' found, or there is nothing after the last '/', return an empty string
return "";
}
int progressCallback(
void* clientp,
curl_off_t dltotal,
curl_off_t dlnow,
curl_off_t ultotal,
curl_off_t ulnow)
{
static int progress = 0;
const auto currentProgress = dltotal > 0 ? (int)((dlnow * 100) / dltotal) : 0;
if (currentProgress > progress) {
progress = currentProgress;
LogPrintf("CBootstrap::%s: Download: %d%%\n", __func__, progress);
uiInterface.ShowProgress(_("Download: "), progress);
}
return 0;
}
bool CBootstrap::DownloadAndApply()
{
const auto url =
std::string(BOOTSTRAP_URL) +
(Params().IsTestNet() ? "T" : "") + std::string(CURRENCY_UNIT) +
"/bootstrap.zip";
const auto datadir = GetDataDir();
const auto fileName = datadir / extractFilenameFromURL(url);
const auto blocks = datadir / "blocks";
const auto chainstate = datadir / "chainstate";
const auto sporks = datadir / "sporks";
const auto banlist = datadir / "banlist.dat";
try {
// Step 1: Download the bootstrap file
if (!CCurlWrapper::DownloadFile(url, fileName.string(), progressCallback)) {
LogPrintf(
"CBootstrap::%s: Failed to download the bootstrap file: %s\n",
__func__,
fileName.string());
return false;
}
// Step 2: Cleanup the existing folders and files
try {
fs::remove_all(blocks);
fs::remove_all(chainstate);
fs::remove_all(sporks);
fs::remove_all(banlist);
} catch (const boost::filesystem::filesystem_error& e) {
LogPrintf("CBootstrap::%s: Error removing: %s\n", __func__, e.what());
return false;
}
// Step 3: Unzip the downloaded file
if (!CZipWrapper::ExtractZip(fileName.string(), datadir.string())) {
LogPrintf(
"CBootstrap::%s: Failed to unzip the bootstrap file: %s into: %s\n",
__func__, fileName.string(), datadir.string());
fs::remove_all(fileName);
fs::remove_all(blocks);
fs::remove_all(chainstate);
fs::remove_all(sporks);
fs::remove_all(banlist);
return false;
}
// Step 4: Cleanup the bootstrap file
fs::remove_all(fileName);
} catch (const std::exception& e) {
LogPrintf(
"CBootstrap::%s: Error downloading bootstrap file: %s\n",
__func__, e.what());
fs::remove_all(fileName);
fs::remove_all(blocks);
fs::remove_all(chainstate);
fs::remove_all(sporks);
fs::remove_all(banlist);
return false;
}
return true;
}