Skip to content

Commit

Permalink
Possibility to disable api key
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Dec 25, 2024
1 parent 4b3d065 commit e18cb15
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 54 deletions.
3 changes: 2 additions & 1 deletion data/secret/secret_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
"joe": {
"key": "test0000/0000000000000000+00000000000000000000000+000000",
"private": "abc/defghijkl+mnopqrstuvwxyzABCDEFGHIJKLMNOPQ+RSTUVWXYZ012345678+9/abcdefghijklmnopqrs=="
"private": "abc/defghijkl+mnopqrstuvwxyzABCDEFGHIJKLMNOPQ+RSTUVWXYZ012345678+9/abcdefghijklmnopqrs==",
"enabled": false
}
},
"kucoin": {
Expand Down
10 changes: 7 additions & 3 deletions src/api-objects/src/apikeysprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ APIKeysProvider::APIKeysPerExchange APIKeysProvider::ParseAPIKeys(std::string_vi
APIKeysProvider::APIKeysPerExchange apiKeysPerExchange;

if (exchangeSecretsInfo.allExchangesWithoutSecrets()) {
log::info("Not loading private keys, using only public exchanges");
log::debug("Will not use private keys");
return apiKeysPerExchange;
}

std::string_view secretFileName = GetSecretFileName(runMode);
const auto throwOrNoThrow = settings::AreTestKeysRequested(runMode) ? File::IfError::kThrow : File::IfError::kNoThrow;
File secretsFile(dataDir, File::Type::kSecret, secretFileName, throwOrNoThrow);

schema::APIKeysPerExchangeMap apiKeysPerExchangeMap;
schema::APIKeysPerExchange apiKeysPerExchangeMap;

ReadExactJsonOrThrow(secretsFile.readAll(), apiKeysPerExchangeMap);

Expand All @@ -93,11 +93,15 @@ APIKeysProvider::APIKeysPerExchange APIKeysProvider::ParseAPIKeys(std::string_vi
if (std::ranges::any_of(exchangesWithoutSecrets, [exchangeNameEnum](const auto& exchangeName) {
return exchangeName.exchangeNameEnum() == exchangeNameEnum;
})) {
log::debug("Not loading {} private keys as requested", EnumToString(exchangeNameEnum));
log::debug("Do not use {} private keys", EnumToString(exchangeNameEnum));
continue;
}

for (auto& [keyName, apiKey] : apiKeys) {
if (!apiKey.enabled) {
log::debug("{} private key {} is disabled", EnumToString(exchangeNameEnum), keyName);
continue;
}
if (apiKey.key.empty() || apiKey.priv.empty()) {
log::error("Wrong format for secret.json file. It should contain at least fields 'key' and 'private'");
continue;
Expand Down
21 changes: 12 additions & 9 deletions src/schema/include/secret-schema.hpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
#pragma once

#include <unordered_map>
#include <utility>

#include "cct_const.hpp"
#include "cct_fixedcapacityvector.hpp"
#include "cct_json.hpp"
#include "cct_string.hpp"
#include "cct_vector.hpp"

namespace cct::schema {

struct AccountOwner {
using trivially_relocatable = is_trivially_relocatable<string>::type;

string enName;
string koName;

using trivially_relocatable = is_trivially_relocatable<string>::type;
};

struct APIKey {
using trivially_relocatable = is_trivially_relocatable<string>::type;

string key;
string priv; // private is a reserved keyword - we override the json field name below
string passphrase;
AccountOwner accountOwner;

using trivially_relocatable = is_trivially_relocatable<string>::type;
bool enabled = true;
};

using APIKeys = std::unordered_map<string, APIKey>;
using APIKeys = vector<std::pair<string, APIKey>>;

using APIKeysPerExchangeMap = std::unordered_map<ExchangeNameEnum, APIKeys>;
using APIKeysPerExchange = std::unordered_map<ExchangeNameEnum, APIKeys>;

} // namespace cct::schema

template <>
struct glz::meta<::cct::schema::APIKey> {
using V = ::cct::schema::APIKey;
static constexpr auto value =
object("key", &V::key, "private", &V::priv, "passphrase", &V::passphrase, "accountOwner", &V::accountOwner);
static constexpr auto value = object("key", &V::key, "private", &V::priv, "passphrase", &V::passphrase,
"accountOwner", &V::accountOwner, "enabled", &V::enabled);
};
1 change: 1 addition & 0 deletions src/tech/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ add_unit_test(

add_unit_test(
threadpool_test
src/threadpool.cpp
test/threadpool_test.cpp
DEFINITIONS
CCT_DISABLE_SPDLOG
Expand Down
44 changes: 3 additions & 41 deletions src/tech/include/threadpool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <type_traits>

#include "cct_exception.hpp"
#include "cct_invalid_argument_exception.hpp"
#include "cct_log.hpp"
#include "cct_vector.hpp"

Expand Down Expand Up @@ -73,8 +72,6 @@ class ThreadPool {
template <class Futures, class OutputIt>
OutputIt retrieveAllResults(Futures& futures, OutputIt out);

void stopRequested();

// the task queue
TasksQueue _tasks;

Expand All @@ -88,38 +85,8 @@ class ThreadPool {
vector<std::jthread> _workers;
};

inline ThreadPool::ThreadPool(int nbThreads) {
if (nbThreads < 1) {
throw invalid_argument("number of threads should be strictly positive");
}
_workers.reserve(static_cast<decltype(_workers)::size_type>(nbThreads));
for (decltype(nbThreads) threadPos = 0; threadPos < nbThreads; ++threadPos) {
_workers.emplace_back([this] {
while (true) {
TasksQueue::value_type task;

{
std::unique_lock<std::mutex> lock(this->_queueMutex);
this->_condition.wait(lock, [this] { return this->_stop || !this->_tasks.empty(); });
if (this->_stop && this->_tasks.empty()) {
break;
}
task = std::move(this->_tasks.front());
this->_tasks.pop();
}
task();
}
});
}
}

inline ThreadPool::~ThreadPool() {
stopRequested();
_condition.notify_all();
}

template <class Func, class... Args>
inline std::future<std::invoke_result_t<Func, Args...>> ThreadPool::enqueue(Func&& func, Args&&... args) {
std::future<std::invoke_result_t<Func, Args...>> ThreadPool::enqueue(Func&& func, Args&&... args) {
// std::bind copies the arguments. To avoid copies, you can use std::ref to copy reference instead.
using return_type = std::invoke_result_t<Func, Args...>;

Expand All @@ -145,7 +112,7 @@ template <std::ranges::input_range InputRange, std::weakly_incrementable OutputI
std::copy_constructible UnaryOperation>
requires std::indirectly_writable<OutputIt,
std::invoke_result_t<UnaryOperation, std::ranges::range_reference_t<InputRange>>>
inline OutputIt ThreadPool::parallelTransform(InputRange&& r, OutputIt result, UnaryOperation op) {
OutputIt ThreadPool::parallelTransform(InputRange&& r, OutputIt result, UnaryOperation op) {
using FutureT = std::future<std::invoke_result_t<UnaryOperation, std::ranges::range_reference_t<InputRange>>>;
vector<FutureT> futures;
if constexpr (std::ranges::sized_range<InputRange>) {
Expand All @@ -162,7 +129,7 @@ template <std::ranges::input_range InputRange1, std::ranges::input_range InputRa
requires std::indirectly_writable<OutputIt,
std::invoke_result_t<BinaryOperation, std::ranges::range_reference_t<InputRange1>,
std::ranges::range_reference_t<InputRange2>>>
inline OutputIt ThreadPool::parallelTransform(InputRange1&& r1, InputRange2&& r2, OutputIt result, BinaryOperation op) {
OutputIt ThreadPool::parallelTransform(InputRange1&& r1, InputRange2&& r2, OutputIt result, BinaryOperation op) {
using FutureT = std::future<std::invoke_result_t<BinaryOperation, std::ranges::range_reference_t<InputRange1>,
std::ranges::range_reference_t<InputRange2>>>;
vector<FutureT> futures;
Expand Down Expand Up @@ -203,9 +170,4 @@ inline OutputIt ThreadPool::retrieveAllResults(Futures& futures, OutputIt out) {
return out;
}

inline void ThreadPool::stopRequested() {
std::unique_lock<std::mutex> lock(_queueMutex);
_stop = true;
}

} // namespace cct
43 changes: 43 additions & 0 deletions src/tech/src/threadpool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "threadpool.hpp"

#include <mutex>
#include <utility>

#include "cct_invalid_argument_exception.hpp"

namespace cct {

ThreadPool::ThreadPool(int nbThreads) {
if (nbThreads < 1) {
throw invalid_argument("number of threads should be strictly positive");
}
_workers.reserve(static_cast<decltype(_workers)::size_type>(nbThreads));
for (decltype(nbThreads) threadPos = 0; threadPos < nbThreads; ++threadPos) {
_workers.emplace_back([this] {
while (true) {
TasksQueue::value_type task;

{
std::unique_lock<std::mutex> lock(this->_queueMutex);
this->_condition.wait(lock, [this] { return this->_stop || !this->_tasks.empty(); });
if (this->_stop && this->_tasks.empty()) {
break;
}
task = std::move(this->_tasks.front());
this->_tasks.pop();
}
task();
}
});
}
}

ThreadPool::~ThreadPool() {
{
std::unique_lock<std::mutex> lock(_queueMutex);
_stop = true;
}
_condition.notify_all();
}

} // namespace cct

0 comments on commit e18cb15

Please sign in to comment.