Skip to content

Commit

Permalink
Upgrade to 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Spl3en committed Oct 6, 2019
1 parent e10ce09 commit d4eaba8
Show file tree
Hide file tree
Showing 11 changed files with 747 additions and 792 deletions.
207 changes: 106 additions & 101 deletions src/aegis/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,132 +7,137 @@ using ICONation::Aegis::Db::Db;

namespace ICONation::Aegis
{
Application::Application (int argc, char **argv)
: Common::Application::Application (argc, argv)
{
// Arg 1 (optional) : ICON endpoint URL
m_endpoint = (argc > 1) ? argv[1] : "https://ctz.solidwallet.io/api/v3";
// m_endpoint = (argc > 1) ? argv[1] : "https://bicon.net.solidwallet.io/api/v3";
// m_endpoint = (argc > 1) ? argv[1] : "http://iconation.team:9100/api/v3";
// Arg 2 (optional) : Number of threads
int threads = (argc > 2) ? atoi (argv[2]) : 5;
m_cacheSize = (argc > 3) ? atoi (argv[3]) : 50;

// Create client
m_client = std::make_unique<SDK::Client> (m_endpoint);

// Create block downloader
m_downloader = std::make_unique<BlockDownloader::Client> (threads, m_endpoint);

Common::Dbg::info ("Launching Aegis with the following parameters :");
Common::Dbg::info (" Endpoint : {}", m_endpoint);
Common::Dbg::info (" Threads : {}", threads);
Common::Dbg::info (" Cache Size : {}", m_cacheSize);
}

Application::~Application (void)
{
// Stop threading before delete
Common::Dbg::warn ("Stopping Application...");
if (m_downloader->running()) {
m_downloader->stop();
m_downloader->join();
}
}

void Application::print_usage (void)
Application::Application(int argc, char **argv)
: Common::Application::Application(argc, argv)
{
// Arg 1 (optional) : ICON endpoint URL
m_endpoint = (argc > 1) ? argv[1] : "https://ctz.solidwallet.io/api/v3";
// m_endpoint = (argc > 1) ? argv[1] : "https://bicon.net.solidwallet.io/api/v3";
// m_endpoint = (argc > 1) ? argv[1] : "http://iconation.team:9100/api/v3";
// Arg 2 (optional) : Number of threads
int threads = (argc > 2) ? atoi(argv[2]) : 5;
m_cacheSize = (argc > 3) ? atoi(argv[3]) : 50;

// Create client
m_client = std::make_unique<SDK::Client>(m_endpoint);

// Create block downloader
m_downloader = std::make_unique<BlockDownloader::Client>(threads, m_endpoint);

Common::Dbg::info("Launching Aegis with the following parameters :");
Common::Dbg::info(" Endpoint : {}", m_endpoint);
Common::Dbg::info(" Threads : {}", threads);
Common::Dbg::info(" Cache Size : {}", m_cacheSize);
}

Application::~Application(void)
{
// Stop threading before delete
Common::Dbg::warn("Stopping Application...");
if (m_downloader->running())
{
info ("==========================================");
info (" +++ Aegis v1.1 +++++++++++++");
info (" Usage : {} [endpoint] [number of threads] [cache size]", m_binary_name);
info (" Exemple : {} https://ctz.solidwallet.io/api/v3 5 50", m_binary_name);
info ("==========================================");
m_downloader->stop();
m_downloader->join();
}
}

void Application::bootstrap (void)
{
warn ("Bootstrapping...");
void Application::print_usage(void)
{
info("==========================================");
info(" +++ Aegis v%s +++++++++++++", APP_VERSION);
info(" Usage : {} [endpoint] [number of threads] [cache size]", m_binary_name);
info(" Exemple : {} https://ctz.solidwallet.io/api/v3 5 50", m_binary_name);
info("==========================================");
}

void Application::bootstrap(void)
{
warn("Bootstrapping...");

// Bootstrap db
m_db.bootstrap (m_client->icx());
// Bootstrap db
m_db.bootstrap(m_client->icx());

// Get & insert genesis block
GenesisBlock genesis = m_client->get_genesis_block ();
::Db::Id id = m_db.genesis_insert (genesis);
}
// Get & insert genesis block
GenesisBlock genesis = m_client->get_genesis_block();
::Db::Id id = m_db.genesis_insert(genesis);
}

void Application::check_bootstrap (void)
void Application::check_bootstrap(void)
{
if (m_db.need_bootstrap())
{
if (m_db.need_bootstrap()) {
// No block has been created yet
bootstrap();
}
// No block has been created yet
bootstrap();
}
}

void Application::insert_cache (std::list<Block> &cache)
void Application::insert_cache(std::list<Block> &cache)
{
Common::Dbg::info("Inserting block height {} to {} into database...",
cache.front().height(), cache.back().height());

// Insert everything
m_db.start_transaction();
for (auto &block : cache)
{
Common::Dbg::info ("Inserting block height {} to {} into database...",
cache.front().height(), cache.back().height());
m_db.block_insert(block);
}
m_db.commit();

// Insert everything
m_db.start_transaction();
for (auto &block : cache) {
m_db.block_insert (block);
}
m_db.commit();
cache.clear();
}

cache.clear();
}
int Application::main(void)
{
// Bootstrap
check_bootstrap();

int Application::main (void)
while (true)
{
// Bootstrap
check_bootstrap();
// Get latest block
Block lastRemoteBlock = m_client->get_last_block();
Block lastLocalBlock = m_db.block_get(m_db.last_block_id());

while (true)
if (lastRemoteBlock.height() == lastLocalBlock.height())
{
// Get latest block
Block lastRemoteBlock = m_client->get_last_block();
Block lastLocalBlock = m_db.block_get (m_db.last_block_id());

if (lastRemoteBlock.height() == lastLocalBlock.height()) {
break;
}
break;
}

Common::Dbg::info ("Current height : {} | Remote height : {}", lastLocalBlock.height(), lastRemoteBlock.height());
Common::Dbg::info("Current height : {} | Remote height : {}", lastLocalBlock.height(), lastRemoteBlock.height());

// Start downloading blocks
m_downloader->start_download (lastLocalBlock.height() + 1, lastRemoteBlock.height());
// Start downloading blocks
m_downloader->start_download(lastLocalBlock.height() + 1, lastRemoteBlock.height());

// Block cache for bulk insertion
std::list<Block> cache;
// Block cache for bulk insertion
std::list<Block> cache;

while (lastLocalBlock.height() < lastRemoteBlock.height())
{
// Get the next block height
Block::Height newBlockHeight = lastLocalBlock.height() + 1;

// Get the next block from cache
Block newBlock = m_downloader->get_block (newBlockHeight);
while (lastLocalBlock.height() < lastRemoteBlock.height())
{
// Get the next block height
Block::Height newBlockHeight = lastLocalBlock.height() + 1;

// Insert it in cache
cache.emplace_back(newBlock);
// Get the next block from cache
Block newBlock = m_downloader->get_block(newBlockHeight);

if (cache.size() > m_cacheSize) {
insert_cache (cache);
}
// Insert it in cache
cache.emplace_back(newBlock);

// Update to the next block
lastLocalBlock = newBlock;
if (cache.size() > m_cacheSize)
{
insert_cache(cache);
}

// Insert remaining items
insert_cache (cache);
// Update to the next block
lastLocalBlock = newBlock;
}

m_downloader->stop();
m_downloader->join();

return 0;
// Insert remaining items
insert_cache(cache);
}
}

m_downloader->stop();
m_downloader->join();

return 0;
}
} // namespace ICONation::Aegis
77 changes: 40 additions & 37 deletions src/aegis/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,45 @@
#include "sdk/sdk.h"
#include "blockdownloader.h"

#define APP_VERSION "1.2"

namespace ICONation::Aegis
{
class Application : public Common::Application::Application
{
// Allocators
public:
Application (int argc, char **argv);
~Application (void);

// States
private:
void check_bootstrap (void);
void bootstrap (void);

// Main loop
private:
int main (void) override;

// Block Downloader
private:
std::unique_ptr<BlockDownloader::Client> m_downloader;

// Usage
public:
void print_usage (void) override;

// SDK Client
private:
std::unique_ptr<SDK::Client> m_client;
std::string m_endpoint;

// Database
public:
void insert_cache (std::list<SDK::Blockchain::Block> &cache);
private:
int m_cacheSize;
Db::Db m_db;
};
}
class Application : public Common::Application::Application
{
// Allocators
public:
Application(int argc, char **argv);
~Application(void);

// States
private:
void check_bootstrap(void);
void bootstrap(void);

// Main loop
private:
int main(void) override;

// Block Downloader
private:
std::unique_ptr<BlockDownloader::Client> m_downloader;

// Usage
public:
void print_usage(void) override;

// SDK Client
private:
std::unique_ptr<SDK::Client> m_client;
std::string m_endpoint;

// Database
public:
void insert_cache(std::list<SDK::Blockchain::Block> &cache);

private:
int m_cacheSize;
Db::Db m_db;
};
} // namespace ICONation::Aegis
Loading

0 comments on commit d4eaba8

Please sign in to comment.