Skip to content

Commit 51030be

Browse files
committed
refactor: conditionally include libipfs and link libraries for non-Android platforms
1 parent 6092362 commit 51030be

File tree

6 files changed

+133
-113
lines changed

6 files changed

+133
-113
lines changed

src/blockchain_utilities/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ scala_add_executable(blockchain_import
140140

141141
target_link_libraries(blockchain_import
142142
PRIVATE
143-
libipfs
144143
cryptonote_core
145144
blockchain_db
146145
version
@@ -152,6 +151,10 @@ target_link_libraries(blockchain_import
152151
${EXTRA_LIBRARIES}
153152
$<TARGET_NAME_IF_EXISTS:blocks>)
154153

154+
if(NOT ANDROID)
155+
target_link_libraries(blockchain_import PRIVATE libipfs)
156+
endif()
157+
155158
if(ARCH_WIDTH)
156159
target_compile_definitions(blockchain_import
157160
PUBLIC -DARCH_WIDTH=${ARCH_WIDTH})

src/blockchain_utilities/blockchain_import.cpp

Lines changed: 96 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -707,105 +707,107 @@ int main(int argc, char* argv[])
707707

708708
import_file_path = fs_import_file_path.string();
709709

710+
#if !defined(__ANDROID__)
711+
if (command_line::has_arg(vm, arg_ipfs_import))
712+
{
713+
std::atomic<bool> downloadInProgress(true);
714+
715+
boost::filesystem::path export_path = boost::filesystem::path(m_config_folder) / "export";
716+
if (!boost::filesystem::exists(export_path))
717+
{
718+
boost::filesystem::create_directories(export_path);
719+
}
710720

711-
if (command_line::has_arg(vm, arg_ipfs_import))
712-
{
713-
std::atomic<bool> downloadInProgress(true);
714-
715-
boost::filesystem::path export_path = boost::filesystem::path(m_config_folder) / "export";
716-
if (!boost::filesystem::exists(export_path))
717-
{
718-
boost::filesystem::create_directories(export_path);
719-
}
720-
721-
opt_verify = false;
722-
boost::filesystem::path ipfs_path = boost::filesystem::path(m_config_folder) / "ipfs";
723-
std::string ipfs_path_str = ipfs_path.string();
724-
std::string startResult = Start(const_cast<char*>(ipfs_path_str.c_str()), ipfs_port);
725-
726-
rapidjson::Document doc;
727-
if (doc.Parse(startResult.c_str()).HasParseError()) {
728-
MGINFO("IPFS start response parse error");
729-
return 0;
730-
}
731-
732-
bool fail = false;
733-
734-
if (doc.HasMember("Status") && doc["Status"].IsString() &&
735-
std::string(doc["Status"].GetString()) == "success" &&
736-
doc.HasMember("Data") && doc["Data"].IsObject()) {
737-
const rapidjson::Value& dataVal = doc["Data"];
738-
if (dataVal.IsObject()) {
739-
const auto& data = dataVal.GetObject();
740-
if (data.HasMember("peerId") && data["peerId"].IsString()) {
741-
MGINFO("IPFS Started with Peer ID: " << data["peerId"].GetString());
742-
743-
if (boost::filesystem::exists(import_file_path)) {
744-
boost::filesystem::remove(import_file_path);
745-
}
746-
747-
std::thread progressThread([&]() {
748-
using boost::posix_time::ptime;
749-
using boost::posix_time::microsec_clock;
750-
using boost::posix_time::time_duration;
751-
using boost::posix_time::to_tm;
752-
753-
while (downloadInProgress) {
754-
if (boost::filesystem::exists(import_file_path)) {
755-
std::uintmax_t size = boost::filesystem::file_size(import_file_path);
756-
757-
ptime now = microsec_clock::local_time();
758-
time_duration td = now.time_of_day();
759-
struct tm tm_now = to_tm(now);
760-
761-
std::ostringstream time_ss;
762-
time_ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S")
763-
<< '.' << std::setw(3) << std::setfill('0') << td.total_milliseconds() % 1000;
764-
765-
std::ostringstream oss;
766-
oss << time_ss.str() << " I Downloaded: " << (size / (1024 * 1024)) << " MB";
767-
768-
std::cout << "\r" << oss.str() << std::flush;
769-
}
770-
771-
std::this_thread::sleep_for(std::chrono::seconds(1));
772-
}
773-
});
774-
775-
MGINFO("Downloading blockchain from IPFS, this might take a few minutes..");
776-
777-
const std::string getCid = "/ipns/blockchain.raw.scala.network";
778-
std::string downloadResult = Get((char*)getCid.c_str(), (char*)import_file_path.c_str(), true);
779-
downloadInProgress = false;
780-
progressThread.join();
721+
opt_verify = false;
722+
boost::filesystem::path ipfs_path = boost::filesystem::path(m_config_folder) / "ipfs";
723+
std::string ipfs_path_str = ipfs_path.string();
724+
std::string startResult = Start(const_cast<char*>(ipfs_path_str.c_str()), ipfs_port);
781725

782-
rapidjson::Document dlDoc;
783-
if (dlDoc.Parse(downloadResult.c_str()).HasParseError()) {
784-
MGINFO("Download result parse error");
785-
return 0;
786-
}
726+
rapidjson::Document doc;
727+
if (doc.Parse(startResult.c_str()).HasParseError()) {
728+
MGINFO("IPFS start response parse error");
729+
return 0;
730+
}
787731

788-
if (dlDoc.HasMember("Status") && dlDoc["Status"].IsString() &&
789-
std::string(dlDoc["Status"].GetString()) == "success") {
790-
MGINFO("Blockchain downloaded from IPFS");
791-
} else {
792-
MGINFO("Blockchain download failed");
793-
}
732+
bool fail = false;
794733

795-
} else {
796-
fail = true;
797-
}
798-
}
799-
} else {
800-
fail = true;
801-
}
802-
803-
if (fail) {
804-
MGINFO("IPFS failed to initialize, exiting!");
805-
return 0;
806-
}
807-
}
734+
if (doc.HasMember("Status") && doc["Status"].IsString() &&
735+
std::string(doc["Status"].GetString()) == "success" &&
736+
doc.HasMember("Data") && doc["Data"].IsObject()) {
737+
const rapidjson::Value& dataVal = doc["Data"];
738+
if (dataVal.IsObject()) {
739+
const auto& data = dataVal.GetObject();
740+
if (data.HasMember("peerId") && data["peerId"].IsString()) {
741+
MGINFO("IPFS Started with Peer ID: " << data["peerId"].GetString());
742+
743+
if (boost::filesystem::exists(import_file_path)) {
744+
boost::filesystem::remove(import_file_path);
745+
}
808746

747+
std::thread progressThread([&]() {
748+
using boost::posix_time::ptime;
749+
using boost::posix_time::microsec_clock;
750+
using boost::posix_time::time_duration;
751+
using boost::posix_time::to_tm;
752+
753+
while (downloadInProgress) {
754+
if (boost::filesystem::exists(import_file_path)) {
755+
std::uintmax_t size = boost::filesystem::file_size(import_file_path);
756+
757+
ptime now = microsec_clock::local_time();
758+
time_duration td = now.time_of_day();
759+
struct tm tm_now = to_tm(now);
760+
761+
std::ostringstream time_ss;
762+
time_ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S")
763+
<< '.' << std::setw(3) << std::setfill('0') << td.total_milliseconds() % 1000;
764+
765+
std::ostringstream oss;
766+
oss << time_ss.str() << " I Downloaded: " << (size / (1024 * 1024)) << " MB";
767+
768+
std::cout << "\r" << oss.str() << std::flush;
769+
}
770+
771+
std::this_thread::sleep_for(std::chrono::seconds(1));
772+
}
773+
});
774+
775+
MGINFO("Downloading blockchain from IPFS, this might take a few minutes..");
776+
777+
const std::string getCid = "/ipns/blockchain.raw.scala.network";
778+
std::string downloadResult = Get((char*)getCid.c_str(), (char*)import_file_path.c_str(), true);
779+
downloadInProgress = false;
780+
progressThread.join();
781+
782+
rapidjson::Document dlDoc;
783+
if (dlDoc.Parse(downloadResult.c_str()).HasParseError()) {
784+
MGINFO("Download result parse error");
785+
return 0;
786+
}
787+
788+
if (dlDoc.HasMember("Status") && dlDoc["Status"].IsString() &&
789+
std::string(dlDoc["Status"].GetString()) == "success") {
790+
MGINFO("Blockchain downloaded from IPFS");
791+
} else {
792+
MGINFO("Blockchain download failed");
793+
}
794+
795+
} else {
796+
fail = true;
797+
}
798+
}
799+
} else {
800+
fail = true;
801+
}
802+
803+
if (fail) {
804+
MGINFO("IPFS failed to initialize, exiting!");
805+
return 0;
806+
}
807+
}
808+
#else
809+
MGINFO("IPFS import is not supported on Android");
810+
#endif
809811

810812
if (command_line::has_arg(vm, arg_ipfs_import))
811813
{

src/blockchain_utilities/blockchain_import.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,9 @@
2929
#pragma once
3030

3131
#include "version.h"
32-
#include "libipfs.h"
32+
33+
#if !defined(__ANDROID__)
34+
#include "libipfs.h"
35+
#endif
36+
3337

src/daemon/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ scala_add_executable(daemon
4848
target_link_libraries(daemon
4949
PRIVATE
5050
rpc
51-
libipfs
5251
blockchain_db
5352
cryptonote_core
5453
cncrypto
@@ -70,6 +69,11 @@ target_link_libraries(daemon
7069
${GNU_READLINE_LIBRARY}
7170
${EXTRA_LIBRARIES}
7271
$<TARGET_NAME_IF_EXISTS:blocks>)
72+
73+
if(NOT ANDROID)
74+
target_link_libraries(blockchain_import PRIVATE libipfs)
75+
endif()
76+
7377
set_property(TARGET daemon
7478
PROPERTY
7579
OUTPUT_NAME "scalad")

src/daemon/daemon.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,25 @@ struct t_internals {
125125
}
126126
}
127127

128-
if (!command_line::get_arg(vm, daemon_args::arg_ipfs_enabled))
129-
{
130-
MGINFO("IPFS disabled");
131-
} else {
132-
boost::filesystem::path data_dir = boost::filesystem::absolute(
133-
command_line::get_arg(vm, cryptonote::arg_data_dir));
134-
boost::filesystem::path ipfs_dir = data_dir / "ipfs";
135-
std::string ipfs_path_str = ipfs_dir.string();
136-
const std::string ipfs_p2p_port_str = command_line::get_arg(vm, daemon_args::arg_ipfs_bind_port);
137-
int ipfs_p2p_port = std::stoi(ipfs_p2p_port_str);
138-
139-
Start(const_cast<char*>(ipfs_path_str.c_str()), ipfs_p2p_port);
140-
141-
MGINFO("IPFS initialized OK on port: " << ipfs_p2p_port);
142-
}
128+
#if !defined(__ANDROID__)
129+
if (!command_line::get_arg(vm, daemon_args::arg_ipfs_enabled))
130+
{
131+
MGINFO("IPFS disabled");
132+
} else {
133+
boost::filesystem::path data_dir = boost::filesystem::absolute(
134+
command_line::get_arg(vm, cryptonote::arg_data_dir));
135+
boost::filesystem::path ipfs_dir = data_dir / "ipfs";
136+
std::string ipfs_path_str = ipfs_dir.string();
137+
const std::string ipfs_p2p_port_str = command_line::get_arg(vm, daemon_args::arg_ipfs_bind_port);
138+
int ipfs_p2p_port = std::stoi(ipfs_p2p_port_str);
139+
140+
Start(const_cast<char*>(ipfs_path_str.c_str()), ipfs_p2p_port);
141+
142+
MGINFO("IPFS initialized OK on port: " << ipfs_p2p_port);
143+
}
144+
#elif
145+
MGINFO("IPFS disabled on Android");
146+
#endif
143147
}
144148
};
145149

src/daemon/daemon.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828

2929
#pragma once
3030
#include <boost/program_options.hpp>
31-
#include "libipfs.h"
31+
32+
#if !defined(__ANDROID__)
33+
#include "libipfs.h"
34+
#endif
3235

3336
#undef SCALA_DEFAULT_LOG_CATEGORY
3437
#define SCALA_DEFAULT_LOG_CATEGORY "daemon"

0 commit comments

Comments
 (0)