Don't use unsigned ints

This commit is contained in:
tobtoht
2020-12-25 15:38:44 +01:00
parent 136f877778
commit 14e3422423
18 changed files with 68 additions and 69 deletions
+25 -25
View File
@@ -19,44 +19,44 @@
struct RestoreHeightLookup {
NetworkType::Type type;
QMap<unsigned int, unsigned int> data;
QMap<int, int> data;
explicit RestoreHeightLookup(NetworkType::Type type) : type(type) {}
unsigned int dateToRestoreHeight(unsigned int date) {
int dateToRestoreHeight(int date) {
// restore height based on a given timestamp using a lookup
// table. If it cannot find the date in the lookup table, it
// will calculate the blockheight based off the last known
// date: ((now - lastKnownDate) / blockTime) - clearance
if(this->type == NetworkType::TESTNET) return 1;
unsigned int blockTime = 120;
unsigned int blocksPerDay = 86400 / blockTime;
unsigned int blockCalcClearance = blocksPerDay * 5;
QList<unsigned int> values = this->data.keys();
int blockTime = 120;
int blocksPerDay = 86400 / blockTime;
int blockCalcClearance = blocksPerDay * 5;
QList<int> values = this->data.keys();
if(date <= values.at(0))
return this->data[values.at(0)];
for(unsigned int i = 0; i != values.count(); i++) {
for(int i = 0; i != values.count(); i++) {
if(values[i] > date) {
return i - 1 < 0 ? this->data[values[i]] : this->data[values[i-1]] - blockCalcClearance;
}
}
// lookup failed, calculate blockheight from last known checkpoint
unsigned int lastBlockHeightTime = values.at(values.count() - 1);
unsigned int lastBlockHeight = this->data[lastBlockHeightTime];
unsigned int deltaTime = date - lastBlockHeightTime;
unsigned int deltaBlocks = deltaTime / blockTime;
unsigned int blockHeight = (lastBlockHeight + deltaBlocks) - blockCalcClearance;
int lastBlockHeightTime = values.at(values.count() - 1);
int lastBlockHeight = this->data[lastBlockHeightTime];
int deltaTime = date - lastBlockHeightTime;
int deltaBlocks = deltaTime / blockTime;
int blockHeight = (lastBlockHeight + deltaBlocks) - blockCalcClearance;
qDebug() << "Calculated blockheight: " << blockHeight << " from epoch " << date;
return blockHeight;
}
unsigned int restoreHeightToDate(unsigned int height) {
int restoreHeightToDate(int height) {
// @TODO: most likely inefficient, refactor
QMap<unsigned int, unsigned int>::iterator i;
unsigned int timestamp = 0;
QMap<int, int>::iterator i;
int timestamp = 0;
for (i = this->data.begin(); i != this->data.end(); ++i) {
unsigned int ts = i.key();
int ts = i.key();
if (i.value() > height)
return timestamp;
timestamp = ts;
@@ -68,7 +68,7 @@ struct RestoreHeightLookup {
// initialize this class using a lookup table, e.g `:/assets/restore_heights_monero_mainnet.txt`/
auto rtn = new RestoreHeightLookup(type);
auto data = Utils::barrayToString(Utils::fileOpen(fn));
QMap<unsigned int, unsigned int> _data;
QMap<int, int> _data;
for(const auto &line: data.split('\n')) {
if(line.trimmed().isEmpty()) continue;
auto spl = line.trimmed().split(':');
@@ -82,7 +82,7 @@ struct FeatherSeed {
QString mnemonicSeed;
QString spendKey;
time_t time = 0;
unsigned int restoreHeight = 0;
int restoreHeight = 0;
RestoreHeightLookup *lookup = nullptr;
QString language;
std::string coinName;
@@ -124,7 +124,7 @@ struct FeatherSeed {
return rtn;
}
Wallet *writeWallet(WalletManager *manager, NetworkType::Type type, const QString &path, const QString &password, unsigned int kdfRounds) {
Wallet *writeWallet(WalletManager *manager, NetworkType::Type type, const QString &path, const QString &password, quint64 kdfRounds) {
// writes both 14/25 word mnemonic seeds.
Wallet *wallet = nullptr;
if(this->lookup == nullptr) return wallet;
@@ -136,25 +136,25 @@ struct FeatherSeed {
this->restoreHeight = _seed.restoreHeight;
this->spendKey = _seed.spendKey;
}
wallet = manager->createDeterministicWalletFromSpendKey(path, password, this->language, type, this->spendKey, this->restoreHeight, (quint64)kdfRounds);
wallet = manager->createDeterministicWalletFromSpendKey(path, password, this->language, type, this->spendKey, this->restoreHeight, kdfRounds);
wallet->setCacheAttribute("feather.seed", this->mnemonicSeed);
} else {
wallet = manager->recoveryWallet(path, password, this->mnemonicSeed, "", type, this->restoreHeight, (quint64) kdfRounds);
wallet = manager->recoveryWallet(path, password, this->mnemonicSeed, "", type, this->restoreHeight, kdfRounds);
}
wallet->setPassword(password);
return wallet;
}
unsigned int setRestoreHeight() {
int setRestoreHeight() {
if(this->lookup == nullptr) return 1;
if(this->time == 0) return 1;
this->restoreHeight = this->lookup->dateToRestoreHeight((unsigned int)this->time);
this->restoreHeight = this->lookup->dateToRestoreHeight(this->time);
return this->restoreHeight;
}
unsigned int setRestoreHeight(unsigned int height) {
auto now = (unsigned int)std::time(nullptr);
int setRestoreHeight(int height) {
auto now = std::time(nullptr);
auto nowClearance = 3600 * 24;
auto currentBlockHeight = this->lookup->dateToRestoreHeight(now - nowClearance);
if(height >= currentBlockHeight + nowClearance) {
+4 -4
View File
@@ -7,7 +7,7 @@
#include "txfiathistory.h"
#include "utils/utils.h"
TxFiatHistory::TxFiatHistory(unsigned int genesis_timestamp, const QString &configDirectory, QObject *parent) :
TxFiatHistory::TxFiatHistory(int genesis_timestamp, const QString &configDirectory, QObject *parent) :
QObject(parent),
m_genesis_timestamp(genesis_timestamp),
m_configDirectory(configDirectory) {
@@ -15,7 +15,7 @@ TxFiatHistory::TxFiatHistory(unsigned int genesis_timestamp, const QString &conf
this->loadDatabase();
}
double TxFiatHistory::get(unsigned int timestamp) {
double TxFiatHistory::get(int timestamp) {
QDateTime ts;
ts.setTime_t(timestamp);
auto key = ts.toString("yyyyMMdd");
@@ -59,7 +59,7 @@ void TxFiatHistory::onUpdateDatabase() {
auto now = QDate::currentDate();
auto nowKey = now.toString("yyyyMMdd");
unsigned int year = genesis.toString("yyyy").toUInt();
int year = genesis.toString("yyyy").toInt();
auto yearCurrent = now.year();
// if current year is genesis year we'll refresh regardless.
@@ -71,7 +71,7 @@ void TxFiatHistory::onUpdateDatabase() {
// keep local fiatTxHistory database up to date, loop for missing dates
for(year; year != yearCurrent + 1; year += 1){
for(unsigned int month = 1; month != 13; month++) {
for(int month = 1; month != 13; month++) {
if(year == yearCurrent && month == now.month() && now.day() == 1) break;
QDateTime _now;
_now.setDate(QDate(year, month, 1));
+5 -5
View File
@@ -6,17 +6,17 @@ class TxFiatHistory : public QObject {
Q_OBJECT
public:
explicit TxFiatHistory(unsigned int genesis_timestamp, const QString &configDirectory, QObject *parent = nullptr);
explicit TxFiatHistory(int genesis_timestamp, const QString &configDirectory, QObject *parent = nullptr);
double get(const QString &date);
double get(unsigned int timestamp);
double get(int timestamp);
public slots:
void onUpdateDatabase();
void onWSData(const QJsonObject &data);
signals:
void requestYear(unsigned int year);
void requestYearMonth(unsigned int year, unsigned int month);
void requestYear(int year);
void requestYearMonth(int year, int month);
private:
void loadDatabase();
@@ -25,7 +25,7 @@ private:
QString m_configDirectory;
bool m_initialized = false;
QMap<QString, double> m_database;
unsigned int m_genesis_timestamp;
int m_genesis_timestamp;
};
#endif //FEATHER_TXFIATHISTORY_H
+1 -1
View File
@@ -33,7 +33,7 @@ void XmRig::stop() {
}
void XmRig::start(const QString &path,
unsigned int threads,
int threads,
const QString &address,
const QString &username,
const QString &password,
+1 -1
View File
@@ -23,7 +23,7 @@ public:
explicit XmRig(const QString &configDir, QObject *parent = nullptr);
void prepare();
void start(const QString &path, unsigned int threads, const QString &address, const QString &username, const QString &password, bool tor = false, bool tls = true);
void start(const QString &path, int threads, const QString &address, const QString &username, const QString &password, bool tor = false, bool tls = true);
void stop();
bool unpackBins();