diff --git a/README.md b/README.md
index 28adafe37ba..3c722358f67 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Swap
-Copyright (c) 2014-2024 The Monero Project.
+Copyright (c) 2014-2026 The Monero Project.
Swap (previously known as Freehaven) is based on [Monero](README_original.md)

@@ -12,7 +12,7 @@ Active Branches:
- Unstable: swap-v3.3dev(latest)
- Testing: N/A
-To contribute to the Swap Project, please make all pull requests to the _swap-v3.2dev_ branch.
+To contribute to the Swap Project, please make all pull requests to the _swap-v3.3dev_ branch.
For production, please use the _latest or tagged release_ of the _master_ branch.
## Resources
diff --git a/contrib/epee/include/console_handler.h b/contrib/epee/include/console_handler.h
index 210696dac38..7115fce0cd9 100644
--- a/contrib/epee/include/console_handler.h
+++ b/contrib/epee/include/console_handler.h
@@ -543,6 +543,31 @@ namespace epee
return it->second.second;
}
+ std::vector get_command_list(const std::vector& keywords = std::vector())
+ {
+ std::vector list;
+ list.reserve(m_command_handlers.size());
+ for(auto const& x:m_command_handlers)
+ {
+ bool take = true;
+ for(auto const& y:keywords)
+ {
+ bool in_usage = x.second.second.first.find(y) != std::string::npos;
+ bool in_description = x.second.second.second.find(y) != std::string::npos;
+ if (!(in_usage || in_description))
+ {
+ take = false;
+ break;
+ }
+ }
+ if (take)
+ {
+ list.push_back(x.first);
+ }
+ }
+ return list;
+ }
+
void set_handler(const std::string& cmd, const callback& hndlr, const std::string& usage = "", const std::string& description = "")
{
lookup::mapped_type & vt = m_command_handlers[cmd];
diff --git a/contrib/epee/include/net/abstract_http_client.h b/contrib/epee/include/net/abstract_http_client.h
index 787ae26672a..1f8bbc60559 100644
--- a/contrib/epee/include/net/abstract_http_client.h
+++ b/contrib/epee/include/net/abstract_http_client.h
@@ -64,6 +64,7 @@ namespace http
abstract_http_client() {}
virtual ~abstract_http_client() {}
bool set_server(const std::string& address, boost::optional user, ssl_options_t ssl_options = ssl_support_t::e_ssl_support_autodetect);
+ virtual bool set_proxy(const std::string& address);
virtual void set_server(std::string host, std::string port, boost::optional user, ssl_options_t ssl_options = ssl_support_t::e_ssl_support_autodetect) = 0;
virtual void set_auto_connect(bool auto_connect) = 0;
virtual bool connect(std::chrono::milliseconds timeout) = 0;
diff --git a/contrib/epee/include/net/http_client.h b/contrib/epee/include/net/http_client.h
index 86df48f65de..9645e896bab 100644
--- a/contrib/epee/include/net/http_client.h
+++ b/contrib/epee/include/net/http_client.h
@@ -885,14 +885,6 @@ namespace net_utils
}
};
typedef http_simple_client_template http_simple_client;
-
- class http_simple_client_factory : public http_client_factory
- {
- public:
- std::unique_ptr create() override {
- return std::unique_ptr(new epee::net_utils::http::http_simple_client());
- }
- };
}
}
}
diff --git a/contrib/epee/src/abstract_http_client.cpp b/contrib/epee/src/abstract_http_client.cpp
index 98b5b67d97f..540917873d3 100644
--- a/contrib/epee/src/abstract_http_client.cpp
+++ b/contrib/epee/src/abstract_http_client.cpp
@@ -137,6 +137,11 @@ namespace http
set_server(std::move(parsed.host), std::to_string(parsed.port), std::move(user), std::move(ssl_options));
return true;
}
+
+ bool epee::net_utils::http::abstract_http_client::set_proxy(const std::string& address)
+ {
+ return false;
+ }
}
}
}
diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt
index 3e5e606dbea..b37cffb7aaf 100644
--- a/external/CMakeLists.txt
+++ b/external/CMakeLists.txt
@@ -67,4 +67,6 @@ else()
endif()
add_subdirectory(db_drivers)
-add_subdirectory(easylogging++)
\ No newline at end of file
+add_subdirectory(easylogging++)
+add_subdirectory(qrcodegen)
+
diff --git a/external/qrcodegen/CMakeLists.txt b/external/qrcodegen/CMakeLists.txt
new file mode 100644
index 00000000000..094b49760d7
--- /dev/null
+++ b/external/qrcodegen/CMakeLists.txt
@@ -0,0 +1,8 @@
+project(libqrcodegen)
+
+add_library(qrcodegen STATIC QrCode.cpp)
+set_target_properties(qrcodegen PROPERTIES POSITION_INDEPENDENT_CODE ON)
+set_target_properties(qrcodegen PROPERTIES CXX_STANDARD 11)
+
+target_include_directories(qrcodegen PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR})
diff --git a/external/qrcodegen/QrCode.cpp b/external/qrcodegen/QrCode.cpp
new file mode 100644
index 00000000000..b9de86215e2
--- /dev/null
+++ b/external/qrcodegen/QrCode.cpp
@@ -0,0 +1,862 @@
+/*
+ * QR Code generator library (C++)
+ *
+ * Copyright (c) Project Nayuki. (MIT License)
+ * https://www.nayuki.io/page/qr-code-generator-library
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ * - The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * - The Software is provided "as is", without warranty of any kind, express or
+ * implied, including but not limited to the warranties of merchantability,
+ * fitness for a particular purpose and noninfringement. In no event shall the
+ * authors or copyright holders be liable for any claim, damages or other
+ * liability, whether in an action of contract, tort or otherwise, arising from,
+ * out of or in connection with the Software or the use or other dealings in the
+ * Software.
+ */
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include "QrCode.hpp"
+
+using std::int8_t;
+using std::uint8_t;
+using std::size_t;
+using std::vector;
+
+
+namespace qrcodegen {
+
+QrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :
+ modeBits(mode) {
+ numBitsCharCount[0] = cc0;
+ numBitsCharCount[1] = cc1;
+ numBitsCharCount[2] = cc2;
+}
+
+
+int QrSegment::Mode::getModeBits() const {
+ return modeBits;
+}
+
+
+int QrSegment::Mode::numCharCountBits(int ver) const {
+ return numBitsCharCount[(ver + 7) / 17];
+}
+
+
+const QrSegment::Mode QrSegment::Mode::NUMERIC (0x1, 10, 12, 14);
+const QrSegment::Mode QrSegment::Mode::ALPHANUMERIC(0x2, 9, 11, 13);
+const QrSegment::Mode QrSegment::Mode::BYTE (0x4, 8, 16, 16);
+const QrSegment::Mode QrSegment::Mode::KANJI (0x8, 8, 10, 12);
+const QrSegment::Mode QrSegment::Mode::ECI (0x7, 0, 0, 0);
+
+
+QrSegment QrSegment::makeBytes(const vector &data) {
+ if (data.size() > static_cast(INT_MAX))
+ throw std::length_error("Data too long");
+ BitBuffer bb;
+ for (uint8_t b : data)
+ bb.appendBits(b, 8);
+ return QrSegment(Mode::BYTE, static_cast(data.size()), std::move(bb));
+}
+
+
+QrSegment QrSegment::makeNumeric(const char *digits) {
+ BitBuffer bb;
+ int accumData = 0;
+ int accumCount = 0;
+ int charCount = 0;
+ for (; *digits != '\0'; digits++, charCount++) {
+ char c = *digits;
+ if (c < '0' || c > '9')
+ throw std::domain_error("String contains non-numeric characters");
+ accumData = accumData * 10 + (c - '0');
+ accumCount++;
+ if (accumCount == 3) {
+ bb.appendBits(static_cast(accumData), 10);
+ accumData = 0;
+ accumCount = 0;
+ }
+ }
+ if (accumCount > 0) // 1 or 2 digits remaining
+ bb.appendBits(static_cast(accumData), accumCount * 3 + 1);
+ return QrSegment(Mode::NUMERIC, charCount, std::move(bb));
+}
+
+
+QrSegment QrSegment::makeAlphanumeric(const char *text) {
+ BitBuffer bb;
+ int accumData = 0;
+ int accumCount = 0;
+ int charCount = 0;
+ for (; *text != '\0'; text++, charCount++) {
+ const char *temp = std::strchr(ALPHANUMERIC_CHARSET, *text);
+ if (temp == nullptr)
+ throw std::domain_error("String contains unencodable characters in alphanumeric mode");
+ accumData = accumData * 45 + static_cast(temp - ALPHANUMERIC_CHARSET);
+ accumCount++;
+ if (accumCount == 2) {
+ bb.appendBits(static_cast(accumData), 11);
+ accumData = 0;
+ accumCount = 0;
+ }
+ }
+ if (accumCount > 0) // 1 character remaining
+ bb.appendBits(static_cast(accumData), 6);
+ return QrSegment(Mode::ALPHANUMERIC, charCount, std::move(bb));
+}
+
+
+vector QrSegment::makeSegments(const char *text) {
+ // Select the most efficient segment encoding automatically
+ vector result;
+ if (*text == '\0'); // Leave result empty
+ else if (isNumeric(text))
+ result.push_back(makeNumeric(text));
+ else if (isAlphanumeric(text))
+ result.push_back(makeAlphanumeric(text));
+ else {
+ vector bytes;
+ for (; *text != '\0'; text++)
+ bytes.push_back(static_cast(*text));
+ result.push_back(makeBytes(bytes));
+ }
+ return result;
+}
+
+
+QrSegment QrSegment::makeEci(long assignVal) {
+ BitBuffer bb;
+ if (assignVal < 0)
+ throw std::domain_error("ECI assignment value out of range");
+ else if (assignVal < (1 << 7))
+ bb.appendBits(static_cast(assignVal), 8);
+ else if (assignVal < (1 << 14)) {
+ bb.appendBits(2, 2);
+ bb.appendBits(static_cast(assignVal), 14);
+ } else if (assignVal < 1000000L) {
+ bb.appendBits(6, 3);
+ bb.appendBits(static_cast(assignVal), 21);
+ } else
+ throw std::domain_error("ECI assignment value out of range");
+ return QrSegment(Mode::ECI, 0, std::move(bb));
+}
+
+
+QrSegment::QrSegment(Mode md, int numCh, const std::vector &dt) :
+ mode(md),
+ numChars(numCh),
+ data(dt) {
+ if (numCh < 0)
+ throw std::domain_error("Invalid value");
+}
+
+
+QrSegment::QrSegment(Mode md, int numCh, std::vector &&dt) :
+ mode(md),
+ numChars(numCh),
+ data(std::move(dt)) {
+ if (numCh < 0)
+ throw std::domain_error("Invalid value");
+}
+
+
+int QrSegment::getTotalBits(const vector &segs, int version) {
+ int result = 0;
+ for (const QrSegment &seg : segs) {
+ int ccbits = seg.mode.numCharCountBits(version);
+ if (seg.numChars >= (1L << ccbits))
+ return -1; // The segment's length doesn't fit the field's bit width
+ if (4 + ccbits > INT_MAX - result)
+ return -1; // The sum will overflow an int type
+ result += 4 + ccbits;
+ if (seg.data.size() > static_cast(INT_MAX - result))
+ return -1; // The sum will overflow an int type
+ result += static_cast(seg.data.size());
+ }
+ return result;
+}
+
+
+bool QrSegment::isAlphanumeric(const char *text) {
+ for (; *text != '\0'; text++) {
+ if (std::strchr(ALPHANUMERIC_CHARSET, *text) == nullptr)
+ return false;
+ }
+ return true;
+}
+
+
+bool QrSegment::isNumeric(const char *text) {
+ for (; *text != '\0'; text++) {
+ char c = *text;
+ if (c < '0' || c > '9')
+ return false;
+ }
+ return true;
+}
+
+
+QrSegment::Mode QrSegment::getMode() const {
+ return mode;
+}
+
+
+int QrSegment::getNumChars() const {
+ return numChars;
+}
+
+
+const std::vector &QrSegment::getData() const {
+ return data;
+}
+
+
+const char *QrSegment::ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
+
+
+
+int QrCode::getFormatBits(Ecc ecl) {
+ switch (ecl) {
+ case Ecc::LOW : return 1;
+ case Ecc::MEDIUM : return 0;
+ case Ecc::QUARTILE: return 3;
+ case Ecc::HIGH : return 2;
+ default: throw std::logic_error("Assertion error");
+ }
+}
+
+
+QrCode QrCode::encodeText(const char *text, Ecc ecl) {
+ vector segs = QrSegment::makeSegments(text);
+ return encodeSegments(segs, ecl);
+}
+
+
+QrCode QrCode::encodeBinary(const vector &data, Ecc ecl) {
+ vector segs{QrSegment::makeBytes(data)};
+ return encodeSegments(segs, ecl);
+}
+
+
+QrCode QrCode::encodeSegments(const vector &segs, Ecc ecl,
+ int minVersion, int maxVersion, int mask, bool boostEcl) {
+ if (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7)
+ throw std::invalid_argument("Invalid value");
+
+ // Find the minimal version number to use
+ int version, dataUsedBits;
+ for (version = minVersion; ; version++) {
+ int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available
+ dataUsedBits = QrSegment::getTotalBits(segs, version);
+ if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)
+ break; // This version number is found to be suitable
+ if (version >= maxVersion) { // All versions in the range could not fit the given data
+ std::ostringstream sb;
+ if (dataUsedBits == -1)
+ sb << "Segment too long";
+ else {
+ sb << "Data length = " << dataUsedBits << " bits, ";
+ sb << "Max capacity = " << dataCapacityBits << " bits";
+ }
+ throw data_too_long(sb.str());
+ }
+ }
+ if (dataUsedBits == -1)
+ throw std::logic_error("Assertion error");
+
+ // Increase the error correction level while the data still fits in the current version number
+ for (Ecc newEcl : vector{Ecc::MEDIUM, Ecc::QUARTILE, Ecc::HIGH}) { // From low to high
+ if (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8)
+ ecl = newEcl;
+ }
+
+ // Concatenate all segments to create the data bit string
+ BitBuffer bb;
+ for (const QrSegment &seg : segs) {
+ bb.appendBits(static_cast(seg.getMode().getModeBits()), 4);
+ bb.appendBits(static_cast(seg.getNumChars()), seg.getMode().numCharCountBits(version));
+ bb.insert(bb.end(), seg.getData().begin(), seg.getData().end());
+ }
+ if (bb.size() != static_cast(dataUsedBits))
+ throw std::logic_error("Assertion error");
+
+ // Add terminator and pad up to a byte if applicable
+ size_t dataCapacityBits = static_cast(getNumDataCodewords(version, ecl)) * 8;
+ if (bb.size() > dataCapacityBits)
+ throw std::logic_error("Assertion error");
+ bb.appendBits(0, std::min(4, static_cast(dataCapacityBits - bb.size())));
+ bb.appendBits(0, (8 - static_cast(bb.size() % 8)) % 8);
+ if (bb.size() % 8 != 0)
+ throw std::logic_error("Assertion error");
+
+ // Pad with alternating bytes until data capacity is reached
+ for (uint8_t padByte = 0xEC; bb.size() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
+ bb.appendBits(padByte, 8);
+
+ // Pack bits into bytes in big endian
+ vector dataCodewords(bb.size() / 8);
+ for (size_t i = 0; i < bb.size(); i++)
+ dataCodewords[i >> 3] |= (bb.at(i) ? 1 : 0) << (7 - (i & 7));
+
+ // Create the QR Code object
+ return QrCode(version, ecl, dataCodewords, mask);
+}
+
+
+QrCode::QrCode(int ver, Ecc ecl, const vector &dataCodewords, int msk) :
+ // Initialize fields and check arguments
+ version(ver),
+ errorCorrectionLevel(ecl) {
+ if (ver < MIN_VERSION || ver > MAX_VERSION)
+ throw std::domain_error("Version value out of range");
+ if (msk < -1 || msk > 7)
+ throw std::domain_error("Mask value out of range");
+ size = ver * 4 + 17;
+ size_t sz = static_cast(size);
+ modules = vector >(sz, vector(sz)); // Initially all white
+ isFunction = vector >(sz, vector(sz));
+
+ // Compute ECC, draw modules
+ drawFunctionPatterns();
+ const vector allCodewords = addEccAndInterleave(dataCodewords);
+ drawCodewords(allCodewords);
+
+ // Do masking
+ if (msk == -1) { // Automatically choose best mask
+ long minPenalty = LONG_MAX;
+ for (int i = 0; i < 8; i++) {
+ applyMask(i);
+ drawFormatBits(i);
+ long penalty = getPenaltyScore();
+ if (penalty < minPenalty) {
+ msk = i;
+ minPenalty = penalty;
+ }
+ applyMask(i); // Undoes the mask due to XOR
+ }
+ }
+ if (msk < 0 || msk > 7)
+ throw std::logic_error("Assertion error");
+ this->mask = msk;
+ applyMask(msk); // Apply the final choice of mask
+ drawFormatBits(msk); // Overwrite old format bits
+
+ isFunction.clear();
+ isFunction.shrink_to_fit();
+}
+
+
+int QrCode::getVersion() const {
+ return version;
+}
+
+
+int QrCode::getSize() const {
+ return size;
+}
+
+
+QrCode::Ecc QrCode::getErrorCorrectionLevel() const {
+ return errorCorrectionLevel;
+}
+
+
+int QrCode::getMask() const {
+ return mask;
+}
+
+
+bool QrCode::getModule(int x, int y) const {
+ return 0 <= x && x < size && 0 <= y && y < size && module(x, y);
+}
+
+
+std::string QrCode::toSvgString(int border) const {
+ if (border < 0)
+ throw std::domain_error("Border must be non-negative");
+ if (border > INT_MAX / 2 || border * 2 > INT_MAX - size)
+ throw std::overflow_error("Border too large");
+
+ std::ostringstream sb;
+ sb << "\n";
+ sb << "\n";
+ sb << "\n";
+ return sb.str();
+}
+
+
+void QrCode::drawFunctionPatterns() {
+ // Draw horizontal and vertical timing patterns
+ for (int i = 0; i < size; i++) {
+ setFunctionModule(6, i, i % 2 == 0);
+ setFunctionModule(i, 6, i % 2 == 0);
+ }
+
+ // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
+ drawFinderPattern(3, 3);
+ drawFinderPattern(size - 4, 3);
+ drawFinderPattern(3, size - 4);
+
+ // Draw numerous alignment patterns
+ const vector alignPatPos = getAlignmentPatternPositions();
+ size_t numAlign = alignPatPos.size();
+ for (size_t i = 0; i < numAlign; i++) {
+ for (size_t j = 0; j < numAlign; j++) {
+ // Don't draw on the three finder corners
+ if (!((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0)))
+ drawAlignmentPattern(alignPatPos.at(i), alignPatPos.at(j));
+ }
+ }
+
+ // Draw configuration data
+ drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
+ drawVersion();
+}
+
+
+void QrCode::drawFormatBits(int msk) {
+ // Calculate error correction code and pack bits
+ int data = getFormatBits(errorCorrectionLevel) << 3 | msk; // errCorrLvl is uint2, msk is uint3
+ int rem = data;
+ for (int i = 0; i < 10; i++)
+ rem = (rem << 1) ^ ((rem >> 9) * 0x537);
+ int bits = (data << 10 | rem) ^ 0x5412; // uint15
+ if (bits >> 15 != 0)
+ throw std::logic_error("Assertion error");
+
+ // Draw first copy
+ for (int i = 0; i <= 5; i++)
+ setFunctionModule(8, i, getBit(bits, i));
+ setFunctionModule(8, 7, getBit(bits, 6));
+ setFunctionModule(8, 8, getBit(bits, 7));
+ setFunctionModule(7, 8, getBit(bits, 8));
+ for (int i = 9; i < 15; i++)
+ setFunctionModule(14 - i, 8, getBit(bits, i));
+
+ // Draw second copy
+ for (int i = 0; i < 8; i++)
+ setFunctionModule(size - 1 - i, 8, getBit(bits, i));
+ for (int i = 8; i < 15; i++)
+ setFunctionModule(8, size - 15 + i, getBit(bits, i));
+ setFunctionModule(8, size - 8, true); // Always black
+}
+
+
+void QrCode::drawVersion() {
+ if (version < 7)
+ return;
+
+ // Calculate error correction code and pack bits
+ int rem = version; // version is uint6, in the range [7, 40]
+ for (int i = 0; i < 12; i++)
+ rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
+ long bits = static_cast(version) << 12 | rem; // uint18
+ if (bits >> 18 != 0)
+ throw std::logic_error("Assertion error");
+
+ // Draw two copies
+ for (int i = 0; i < 18; i++) {
+ bool bit = getBit(bits, i);
+ int a = size - 11 + i % 3;
+ int b = i / 3;
+ setFunctionModule(a, b, bit);
+ setFunctionModule(b, a, bit);
+ }
+}
+
+
+void QrCode::drawFinderPattern(int x, int y) {
+ for (int dy = -4; dy <= 4; dy++) {
+ for (int dx = -4; dx <= 4; dx++) {
+ int dist = std::max(std::abs(dx), std::abs(dy)); // Chebyshev/infinity norm
+ int xx = x + dx, yy = y + dy;
+ if (0 <= xx && xx < size && 0 <= yy && yy < size)
+ setFunctionModule(xx, yy, dist != 2 && dist != 4);
+ }
+ }
+}
+
+
+void QrCode::drawAlignmentPattern(int x, int y) {
+ for (int dy = -2; dy <= 2; dy++) {
+ for (int dx = -2; dx <= 2; dx++)
+ setFunctionModule(x + dx, y + dy, std::max(std::abs(dx), std::abs(dy)) != 1);
+ }
+}
+
+
+void QrCode::setFunctionModule(int x, int y, bool isBlack) {
+ size_t ux = static_cast(x);
+ size_t uy = static_cast(y);
+ modules .at(uy).at(ux) = isBlack;
+ isFunction.at(uy).at(ux) = true;
+}
+
+
+bool QrCode::module(int x, int y) const {
+ return modules.at(static_cast(y)).at(static_cast(x));
+}
+
+
+vector QrCode::addEccAndInterleave(const vector &data) const {
+ if (data.size() != static_cast(getNumDataCodewords(version, errorCorrectionLevel)))
+ throw std::invalid_argument("Invalid argument");
+
+ // Calculate parameter numbers
+ int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[static_cast(errorCorrectionLevel)][version];
+ int blockEccLen = ECC_CODEWORDS_PER_BLOCK [static_cast(errorCorrectionLevel)][version];
+ int rawCodewords = getNumRawDataModules(version) / 8;
+ int numShortBlocks = numBlocks - rawCodewords % numBlocks;
+ int shortBlockLen = rawCodewords / numBlocks;
+
+ // Split data into blocks and append ECC to each block
+ vector > blocks;
+ const vector rsDiv = reedSolomonComputeDivisor(blockEccLen);
+ for (int i = 0, k = 0; i < numBlocks; i++) {
+ vector dat(data.cbegin() + k, data.cbegin() + (k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)));
+ k += static_cast(dat.size());
+ const vector ecc = reedSolomonComputeRemainder(dat, rsDiv);
+ if (i < numShortBlocks)
+ dat.push_back(0);
+ dat.insert(dat.end(), ecc.cbegin(), ecc.cend());
+ blocks.push_back(std::move(dat));
+ }
+
+ // Interleave (not concatenate) the bytes from every block into a single sequence
+ vector result;
+ for (size_t i = 0; i < blocks.at(0).size(); i++) {
+ for (size_t j = 0; j < blocks.size(); j++) {
+ // Skip the padding byte in short blocks
+ if (i != static_cast(shortBlockLen - blockEccLen) || j >= static_cast(numShortBlocks))
+ result.push_back(blocks.at(j).at(i));
+ }
+ }
+ if (result.size() != static_cast(rawCodewords))
+ throw std::logic_error("Assertion error");
+ return result;
+}
+
+
+void QrCode::drawCodewords(const vector &data) {
+ if (data.size() != static_cast(getNumRawDataModules(version) / 8))
+ throw std::invalid_argument("Invalid argument");
+
+ size_t i = 0; // Bit index into the data
+ // Do the funny zigzag scan
+ for (int right = size - 1; right >= 1; right -= 2) { // Index of right column in each column pair
+ if (right == 6)
+ right = 5;
+ for (int vert = 0; vert < size; vert++) { // Vertical counter
+ for (int j = 0; j < 2; j++) {
+ size_t x = static_cast(right - j); // Actual x coordinate
+ bool upward = ((right + 1) & 2) == 0;
+ size_t y = static_cast(upward ? size - 1 - vert : vert); // Actual y coordinate
+ if (!isFunction.at(y).at(x) && i < data.size() * 8) {
+ modules.at(y).at(x) = getBit(data.at(i >> 3), 7 - static_cast(i & 7));
+ i++;
+ }
+ // If this QR Code has any remainder bits (0 to 7), they were assigned as
+ // 0/false/white by the constructor and are left unchanged by this method
+ }
+ }
+ }
+ if (i != data.size() * 8)
+ throw std::logic_error("Assertion error");
+}
+
+
+void QrCode::applyMask(int msk) {
+ if (msk < 0 || msk > 7)
+ throw std::domain_error("Mask value out of range");
+ size_t sz = static_cast(size);
+ for (size_t y = 0; y < sz; y++) {
+ for (size_t x = 0; x < sz; x++) {
+ bool invert;
+ switch (msk) {
+ case 0: invert = (x + y) % 2 == 0; break;
+ case 1: invert = y % 2 == 0; break;
+ case 2: invert = x % 3 == 0; break;
+ case 3: invert = (x + y) % 3 == 0; break;
+ case 4: invert = (x / 3 + y / 2) % 2 == 0; break;
+ case 5: invert = x * y % 2 + x * y % 3 == 0; break;
+ case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;
+ case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
+ default: throw std::logic_error("Assertion error");
+ }
+ modules.at(y).at(x) = modules.at(y).at(x) ^ (invert & !isFunction.at(y).at(x));
+ }
+ }
+}
+
+
+long QrCode::getPenaltyScore() const {
+ long result = 0;
+
+ // Adjacent modules in row having same color, and finder-like patterns
+ for (int y = 0; y < size; y++) {
+ bool runColor = false;
+ int runX = 0;
+ std::array runHistory = {};
+ for (int x = 0; x < size; x++) {
+ if (module(x, y) == runColor) {
+ runX++;
+ if (runX == 5)
+ result += PENALTY_N1;
+ else if (runX > 5)
+ result++;
+ } else {
+ finderPenaltyAddHistory(runX, runHistory);
+ if (!runColor)
+ result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
+ runColor = module(x, y);
+ runX = 1;
+ }
+ }
+ result += finderPenaltyTerminateAndCount(runColor, runX, runHistory) * PENALTY_N3;
+ }
+ // Adjacent modules in column having same color, and finder-like patterns
+ for (int x = 0; x < size; x++) {
+ bool runColor = false;
+ int runY = 0;
+ std::array runHistory = {};
+ for (int y = 0; y < size; y++) {
+ if (module(x, y) == runColor) {
+ runY++;
+ if (runY == 5)
+ result += PENALTY_N1;
+ else if (runY > 5)
+ result++;
+ } else {
+ finderPenaltyAddHistory(runY, runHistory);
+ if (!runColor)
+ result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
+ runColor = module(x, y);
+ runY = 1;
+ }
+ }
+ result += finderPenaltyTerminateAndCount(runColor, runY, runHistory) * PENALTY_N3;
+ }
+
+ // 2*2 blocks of modules having same color
+ for (int y = 0; y < size - 1; y++) {
+ for (int x = 0; x < size - 1; x++) {
+ bool color = module(x, y);
+ if ( color == module(x + 1, y) &&
+ color == module(x, y + 1) &&
+ color == module(x + 1, y + 1))
+ result += PENALTY_N2;
+ }
+ }
+
+ // Balance of black and white modules
+ int black = 0;
+ for (const vector &row : modules) {
+ for (bool color : row) {
+ if (color)
+ black++;
+ }
+ }
+ int total = size * size; // Note that size is odd, so black/total != 1/2
+ // Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)%
+ int k = static_cast((std::abs(black * 20L - total * 10L) + total - 1) / total) - 1;
+ result += k * PENALTY_N4;
+ return result;
+}
+
+
+vector QrCode::getAlignmentPatternPositions() const {
+ if (version == 1)
+ return vector();
+ else {
+ int numAlign = version / 7 + 2;
+ int step = (version == 32) ? 26 :
+ (version*4 + numAlign*2 + 1) / (numAlign*2 - 2) * 2;
+ vector result;
+ for (int i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step)
+ result.insert(result.begin(), pos);
+ result.insert(result.begin(), 6);
+ return result;
+ }
+}
+
+
+int QrCode::getNumRawDataModules(int ver) {
+ if (ver < MIN_VERSION || ver > MAX_VERSION)
+ throw std::domain_error("Version number out of range");
+ int result = (16 * ver + 128) * ver + 64;
+ if (ver >= 2) {
+ int numAlign = ver / 7 + 2;
+ result -= (25 * numAlign - 10) * numAlign - 55;
+ if (ver >= 7)
+ result -= 36;
+ }
+ if (!(208 <= result && result <= 29648))
+ throw std::logic_error("Assertion error");
+ return result;
+}
+
+
+int QrCode::getNumDataCodewords(int ver, Ecc ecl) {
+ return getNumRawDataModules(ver) / 8
+ - ECC_CODEWORDS_PER_BLOCK [static_cast(ecl)][ver]
+ * NUM_ERROR_CORRECTION_BLOCKS[static_cast(ecl)][ver];
+}
+
+
+vector QrCode::reedSolomonComputeDivisor(int degree) {
+ if (degree < 1 || degree > 255)
+ throw std::domain_error("Degree out of range");
+ // Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
+ // For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.
+ vector result(static_cast(degree));
+ result.at(result.size() - 1) = 1; // Start off with the monomial x^0
+
+ // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
+ // and drop the highest monomial term which is always 1x^degree.
+ // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
+ uint8_t root = 1;
+ for (int i = 0; i < degree; i++) {
+ // Multiply the current product by (x - r^i)
+ for (size_t j = 0; j < result.size(); j++) {
+ result.at(j) = reedSolomonMultiply(result.at(j), root);
+ if (j + 1 < result.size())
+ result.at(j) ^= result.at(j + 1);
+ }
+ root = reedSolomonMultiply(root, 0x02);
+ }
+ return result;
+}
+
+
+vector QrCode::reedSolomonComputeRemainder(const vector &data, const vector &divisor) {
+ vector result(divisor.size());
+ for (uint8_t b : data) { // Polynomial division
+ uint8_t factor = b ^ result.at(0);
+ result.erase(result.begin());
+ result.push_back(0);
+ for (size_t i = 0; i < result.size(); i++)
+ result.at(i) ^= reedSolomonMultiply(divisor.at(i), factor);
+ }
+ return result;
+}
+
+
+uint8_t QrCode::reedSolomonMultiply(uint8_t x, uint8_t y) {
+ // Russian peasant multiplication
+ int z = 0;
+ for (int i = 7; i >= 0; i--) {
+ z = (z << 1) ^ ((z >> 7) * 0x11D);
+ z ^= ((y >> i) & 1) * x;
+ }
+ if (z >> 8 != 0)
+ throw std::logic_error("Assertion error");
+ return static_cast(z);
+}
+
+
+int QrCode::finderPenaltyCountPatterns(const std::array &runHistory) const {
+ int n = runHistory.at(1);
+ if (n > size * 3)
+ throw std::logic_error("Assertion error");
+ bool core = n > 0 && runHistory.at(2) == n && runHistory.at(3) == n * 3 && runHistory.at(4) == n && runHistory.at(5) == n;
+ return (core && runHistory.at(0) >= n * 4 && runHistory.at(6) >= n ? 1 : 0)
+ + (core && runHistory.at(6) >= n * 4 && runHistory.at(0) >= n ? 1 : 0);
+}
+
+
+int QrCode::finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, std::array &runHistory) const {
+ if (currentRunColor) { // Terminate black run
+ finderPenaltyAddHistory(currentRunLength, runHistory);
+ currentRunLength = 0;
+ }
+ currentRunLength += size; // Add white border to final run
+ finderPenaltyAddHistory(currentRunLength, runHistory);
+ return finderPenaltyCountPatterns(runHistory);
+}
+
+
+void QrCode::finderPenaltyAddHistory(int currentRunLength, std::array &runHistory) const {
+ if (runHistory.at(0) == 0)
+ currentRunLength += size; // Add white border to initial run
+ std::copy_backward(runHistory.cbegin(), runHistory.cend() - 1, runHistory.end());
+ runHistory.at(0) = currentRunLength;
+}
+
+
+bool QrCode::getBit(long x, int i) {
+ return ((x >> i) & 1) != 0;
+}
+
+
+/*---- Tables of constants ----*/
+
+const int QrCode::PENALTY_N1 = 3;
+const int QrCode::PENALTY_N2 = 3;
+const int QrCode::PENALTY_N3 = 40;
+const int QrCode::PENALTY_N4 = 10;
+
+
+const int8_t QrCode::ECC_CODEWORDS_PER_BLOCK[4][41] = {
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
+ //0, 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 Error correction level
+ {-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Low
+ {-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, // Medium
+ {-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Quartile
+ {-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // High
+};
+
+const int8_t QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
+ // Version: (note that index 0 is for padding, and is set to an illegal value)
+ //0, 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 Error correction level
+ {-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low
+ {-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium
+ {-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile
+ {-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High
+};
+
+
+data_too_long::data_too_long(const std::string &msg) :
+ std::length_error(msg) {}
+
+
+
+BitBuffer::BitBuffer()
+ : std::vector() {}
+
+
+void BitBuffer::appendBits(std::uint32_t val, int len) {
+ if (len < 0 || len > 31 || val >> len != 0)
+ throw std::domain_error("Value out of range");
+ for (int i = len - 1; i >= 0; i--) // Append bit by bit
+ this->push_back(((val >> i) & 1) != 0);
+}
+
+}
diff --git a/external/qrcodegen/QrCode.hpp b/external/qrcodegen/QrCode.hpp
new file mode 100644
index 00000000000..7341e410292
--- /dev/null
+++ b/external/qrcodegen/QrCode.hpp
@@ -0,0 +1,556 @@
+/*
+ * QR Code generator library (C++)
+ *
+ * Copyright (c) Project Nayuki. (MIT License)
+ * https://www.nayuki.io/page/qr-code-generator-library
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ * - The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * - The Software is provided "as is", without warranty of any kind, express or
+ * implied, including but not limited to the warranties of merchantability,
+ * fitness for a particular purpose and noninfringement. In no event shall the
+ * authors or copyright holders be liable for any claim, damages or other
+ * liability, whether in an action of contract, tort or otherwise, arising from,
+ * out of or in connection with the Software or the use or other dealings in the
+ * Software.
+ */
+
+#pragma once
+
+#include
+#include
+#include
+#include
+#include
+
+
+namespace qrcodegen {
+
+/*
+ * A segment of character/binary/control data in a QR Code symbol.
+ * Instances of this class are immutable.
+ * The mid-level way to create a segment is to take the payload data
+ * and call a static factory function such as QrSegment::makeNumeric().
+ * The low-level way to create a segment is to custom-make the bit buffer
+ * and call the QrSegment() constructor with appropriate values.
+ * This segment class imposes no length restrictions, but QR Codes have restrictions.
+ * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
+ * Any segment longer than this is meaningless for the purpose of generating QR Codes.
+ */
+class QrSegment final {
+
+ /*---- Public helper enumeration ----*/
+
+ /*
+ * Describes how a segment's data bits are interpreted. Immutable.
+ */
+ public: class Mode final {
+
+ /*-- Constants --*/
+
+ public: static const Mode NUMERIC;
+ public: static const Mode ALPHANUMERIC;
+ public: static const Mode BYTE;
+ public: static const Mode KANJI;
+ public: static const Mode ECI;
+
+
+ /*-- Fields --*/
+
+ // The mode indicator bits, which is a uint4 value (range 0 to 15).
+ private: int modeBits;
+
+ // Number of character count bits for three different version ranges.
+ private: int numBitsCharCount[3];
+
+
+ /*-- Constructor --*/
+
+ private: Mode(int mode, int cc0, int cc1, int cc2);
+
+
+ /*-- Methods --*/
+
+ /*
+ * (Package-private) Returns the mode indicator bits, which is an unsigned 4-bit value (range 0 to 15).
+ */
+ public: int getModeBits() const;
+
+ /*
+ * (Package-private) Returns the bit width of the character count field for a segment in
+ * this mode in a QR Code at the given version number. The result is in the range [0, 16].
+ */
+ public: int numCharCountBits(int ver) const;
+
+ };
+
+
+
+ /*---- Static factory functions (mid level) ----*/
+
+ /*
+ * Returns a segment representing the given binary data encoded in
+ * byte mode. All input byte vectors are acceptable. Any text string
+ * can be converted to UTF-8 bytes and encoded as a byte mode segment.
+ */
+ public: static QrSegment makeBytes(const std::vector &data);
+
+
+ /*
+ * Returns a segment representing the given string of decimal digits encoded in numeric mode.
+ */
+ public: static QrSegment makeNumeric(const char *digits);
+
+
+ /*
+ * Returns a segment representing the given text string encoded in alphanumeric mode.
+ * The characters allowed are: 0 to 9, A to Z (uppercase only), space,
+ * dollar, percent, asterisk, plus, hyphen, period, slash, colon.
+ */
+ public: static QrSegment makeAlphanumeric(const char *text);
+
+
+ /*
+ * Returns a list of zero or more segments to represent the given text string. The result
+ * may use various segment modes and switch modes to optimize the length of the bit stream.
+ */
+ public: static std::vector makeSegments(const char *text);
+
+
+ /*
+ * Returns a segment representing an Extended Channel Interpretation
+ * (ECI) designator with the given assignment value.
+ */
+ public: static QrSegment makeEci(long assignVal);
+
+
+ /*---- Public static helper functions ----*/
+
+ /*
+ * Tests whether the given string can be encoded as a segment in alphanumeric mode.
+ * A string is encodable iff each character is in the following set: 0 to 9, A to Z
+ * (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
+ */
+ public: static bool isAlphanumeric(const char *text);
+
+
+ /*
+ * Tests whether the given string can be encoded as a segment in numeric mode.
+ * A string is encodable iff each character is in the range 0 to 9.
+ */
+ public: static bool isNumeric(const char *text);
+
+
+
+ /*---- Instance fields ----*/
+
+ /* The mode indicator of this segment. Accessed through getMode(). */
+ private: Mode mode;
+
+ /* The length of this segment's unencoded data. Measured in characters for
+ * numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
+ * Always zero or positive. Not the same as the data's bit length.
+ * Accessed through getNumChars(). */
+ private: int numChars;
+
+ /* The data bits of this segment. Accessed through getData(). */
+ private: std::vector data;
+
+
+ /*---- Constructors (low level) ----*/
+
+ /*
+ * Creates a new QR Code segment with the given attributes and data.
+ * The character count (numCh) must agree with the mode and the bit buffer length,
+ * but the constraint isn't checked. The given bit buffer is copied and stored.
+ */
+ public: QrSegment(Mode md, int numCh, const std::vector &dt);
+
+
+ /*
+ * Creates a new QR Code segment with the given parameters and data.
+ * The character count (numCh) must agree with the mode and the bit buffer length,
+ * but the constraint isn't checked. The given bit buffer is moved and stored.
+ */
+ public: QrSegment(Mode md, int numCh, std::vector &&dt);
+
+
+ /*---- Methods ----*/
+
+ /*
+ * Returns the mode field of this segment.
+ */
+ public: Mode getMode() const;
+
+
+ /*
+ * Returns the character count field of this segment.
+ */
+ public: int getNumChars() const;
+
+
+ /*
+ * Returns the data bits of this segment.
+ */
+ public: const std::vector &getData() const;
+
+
+ // (Package-private) Calculates the number of bits needed to encode the given segments at
+ // the given version. Returns a non-negative number if successful. Otherwise returns -1 if a
+ // segment has too many characters to fit its length field, or the total bits exceeds INT_MAX.
+ public: static int getTotalBits(const std::vector &segs, int version);
+
+
+ /*---- Private constant ----*/
+
+ /* The set of all legal characters in alphanumeric mode, where
+ * each character value maps to the index in the string. */
+ private: static const char *ALPHANUMERIC_CHARSET;
+
+};
+
+
+
+/*
+ * A QR Code symbol, which is a type of two-dimension barcode.
+ * Invented by Denso Wave and described in the ISO/IEC 18004 standard.
+ * Instances of this class represent an immutable square grid of black and white cells.
+ * The class provides static factory functions to create a QR Code from text or binary data.
+ * The class covers the QR Code Model 2 specification, supporting all versions (sizes)
+ * from 1 to 40, all 4 error correction levels, and 4 character encoding modes.
+ *
+ * Ways to create a QR Code object:
+ * - High level: Take the payload data and call QrCode::encodeText() or QrCode::encodeBinary().
+ * - Mid level: Custom-make the list of segments and call QrCode::encodeSegments().
+ * - Low level: Custom-make the array of data codeword bytes (including
+ * segment headers and final padding, excluding error correction codewords),
+ * supply the appropriate version number, and call the QrCode() constructor.
+ * (Note that all ways require supplying the desired error correction level.)
+ */
+class QrCode final {
+
+ /*---- Public helper enumeration ----*/
+
+ /*
+ * The error correction level in a QR Code symbol.
+ */
+ public: enum class Ecc {
+ LOW = 0 , // The QR Code can tolerate about 7% erroneous codewords
+ MEDIUM , // The QR Code can tolerate about 15% erroneous codewords
+ QUARTILE, // The QR Code can tolerate about 25% erroneous codewords
+ HIGH , // The QR Code can tolerate about 30% erroneous codewords
+ };
+
+
+ // Returns a value in the range 0 to 3 (unsigned 2-bit integer).
+ private: static int getFormatBits(Ecc ecl);
+
+
+
+ /*---- Static factory functions (high level) ----*/
+
+ /*
+ * Returns a QR Code representing the given Unicode text string at the given error correction level.
+ * As a conservative upper bound, this function is guaranteed to succeed for strings that have 2953 or fewer
+ * UTF-8 code units (not Unicode code points) if the low error correction level is used. The smallest possible
+ * QR Code version is automatically chosen for the output. The ECC level of the result may be higher than
+ * the ecl argument if it can be done without increasing the version.
+ */
+ public: static QrCode encodeText(const char *text, Ecc ecl);
+
+
+ /*
+ * Returns a QR Code representing the given binary data at the given error correction level.
+ * This function always encodes using the binary segment mode, not any text mode. The maximum number of
+ * bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
+ * The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
+ */
+ public: static QrCode encodeBinary(const std::vector &data, Ecc ecl);
+
+
+ /*---- Static factory functions (mid level) ----*/
+
+ /*
+ * Returns a QR Code representing the given segments with the given encoding parameters.
+ * The smallest possible QR Code version within the given range is automatically
+ * chosen for the output. Iff boostEcl is true, then the ECC level of the result
+ * may be higher than the ecl argument if it can be done without increasing the
+ * version. The mask number is either between 0 to 7 (inclusive) to force that
+ * mask, or -1 to automatically choose an appropriate mask (which may be slow).
+ * This function allows the user to create a custom sequence of segments that switches
+ * between modes (such as alphanumeric and byte) to encode text in less space.
+ * This is a mid-level API; the high-level API is encodeText() and encodeBinary().
+ */
+ public: static QrCode encodeSegments(const std::vector &segs, Ecc ecl,
+ int minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true); // All optional parameters
+
+
+
+ /*---- Instance fields ----*/
+
+ // Immutable scalar parameters:
+
+ /* The version number of this QR Code, which is between 1 and 40 (inclusive).
+ * This determines the size of this barcode. */
+ private: int version;
+
+ /* The width and height of this QR Code, measured in modules, between
+ * 21 and 177 (inclusive). This is equal to version * 4 + 17. */
+ private: int size;
+
+ /* The error correction level used in this QR Code. */
+ private: Ecc errorCorrectionLevel;
+
+ /* The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
+ * Even if a QR Code is created with automatic masking requested (mask = -1),
+ * the resulting object still has a mask value between 0 and 7. */
+ private: int mask;
+
+ // Private grids of modules/pixels, with dimensions of size*size:
+
+ // The modules of this QR Code (false = white, true = black).
+ // Immutable after constructor finishes. Accessed through getModule().
+ private: std::vector > modules;
+
+ // Indicates function modules that are not subjected to masking. Discarded when constructor finishes.
+ private: std::vector > isFunction;
+
+
+
+ /*---- Constructor (low level) ----*/
+
+ /*
+ * Creates a new QR Code with the given version number,
+ * error correction level, data codeword bytes, and mask number.
+ * This is a low-level API that most users should not use directly.
+ * A mid-level API is the encodeSegments() function.
+ */
+ public: QrCode(int ver, Ecc ecl, const std::vector &dataCodewords, int msk);
+
+
+
+ /*---- Public instance methods ----*/
+
+ /*
+ * Returns this QR Code's version, in the range [1, 40].
+ */
+ public: int getVersion() const;
+
+
+ /*
+ * Returns this QR Code's size, in the range [21, 177].
+ */
+ public: int getSize() const;
+
+
+ /*
+ * Returns this QR Code's error correction level.
+ */
+ public: Ecc getErrorCorrectionLevel() const;
+
+
+ /*
+ * Returns this QR Code's mask, in the range [0, 7].
+ */
+ public: int getMask() const;
+
+
+ /*
+ * Returns the color of the module (pixel) at the given coordinates, which is false
+ * for white or true for black. The top left corner has the coordinates (x=0, y=0).
+ * If the given coordinates are out of bounds, then false (white) is returned.
+ */
+ public: bool getModule(int x, int y) const;
+
+
+ /*
+ * Returns a string of SVG code for an image depicting this QR Code, with the given number
+ * of border modules. The string always uses Unix newlines (\n), regardless of the platform.
+ */
+ public: std::string toSvgString(int border) const;
+
+
+
+ /*---- Private helper methods for constructor: Drawing function modules ----*/
+
+ // Reads this object's version field, and draws and marks all function modules.
+ private: void drawFunctionPatterns();
+
+
+ // Draws two copies of the format bits (with its own error correction code)
+ // based on the given mask and this object's error correction level field.
+ private: void drawFormatBits(int msk);
+
+
+ // Draws two copies of the version bits (with its own error correction code),
+ // based on this object's version field, iff 7 <= version <= 40.
+ private: void drawVersion();
+
+
+ // Draws a 9*9 finder pattern including the border separator,
+ // with the center module at (x, y). Modules can be out of bounds.
+ private: void drawFinderPattern(int x, int y);
+
+
+ // Draws a 5*5 alignment pattern, with the center module
+ // at (x, y). All modules must be in bounds.
+ private: void drawAlignmentPattern(int x, int y);
+
+
+ // Sets the color of a module and marks it as a function module.
+ // Only used by the constructor. Coordinates must be in bounds.
+ private: void setFunctionModule(int x, int y, bool isBlack);
+
+
+ // Returns the color of the module at the given coordinates, which must be in range.
+ private: bool module(int x, int y) const;
+
+
+ /*---- Private helper methods for constructor: Codewords and masking ----*/
+
+ // Returns a new byte string representing the given data with the appropriate error correction
+ // codewords appended to it, based on this object's version and error correction level.
+ private: std::vector addEccAndInterleave(const std::vector &data) const;
+
+
+ // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
+ // data area of this QR Code. Function modules need to be marked off before this is called.
+ private: void drawCodewords(const std::vector &data);
+
+
+ // XORs the codeword modules in this QR Code with the given mask pattern.
+ // The function modules must be marked and the codeword bits must be drawn
+ // before masking. Due to the arithmetic of XOR, calling applyMask() with
+ // the same mask value a second time will undo the mask. A final well-formed
+ // QR Code needs exactly one (not zero, two, etc.) mask applied.
+ private: void applyMask(int msk);
+
+
+ // Calculates and returns the penalty score based on state of this QR Code's current modules.
+ // This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
+ private: long getPenaltyScore() const;
+
+
+
+ /*---- Private helper functions ----*/
+
+ // Returns an ascending list of positions of alignment patterns for this version number.
+ // Each position is in the range [0,177), and are used on both the x and y axes.
+ // This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.
+ private: std::vector getAlignmentPatternPositions() const;
+
+
+ // Returns the number of data bits that can be stored in a QR Code of the given version number, after
+ // all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
+ // The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
+ private: static int getNumRawDataModules(int ver);
+
+
+ // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
+ // QR Code of the given version number and error correction level, with remainder bits discarded.
+ // This stateless pure function could be implemented as a (40*4)-cell lookup table.
+ private: static int getNumDataCodewords(int ver, Ecc ecl);
+
+
+ // Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
+ // implemented as a lookup table over all possible parameter values, instead of as an algorithm.
+ private: static std::vector reedSolomonComputeDivisor(int degree);
+
+
+ // Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
+ private: static std::vector reedSolomonComputeRemainder(const std::vector &data, const std::vector &divisor);
+
+
+ // Returns the product of the two given field elements modulo GF(2^8/0x11D).
+ // All inputs are valid. This could be implemented as a 256*256 lookup table.
+ private: static std::uint8_t reedSolomonMultiply(std::uint8_t x, std::uint8_t y);
+
+
+ // Can only be called immediately after a white run is added, and
+ // returns either 0, 1, or 2. A helper function for getPenaltyScore().
+ private: int finderPenaltyCountPatterns(const std::array &runHistory) const;
+
+
+ // Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
+ private: int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, std::array &runHistory) const;
+
+
+ // Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
+ private: void finderPenaltyAddHistory(int currentRunLength, std::array &runHistory) const;
+
+
+ // Returns true iff the i'th bit of x is set to 1.
+ private: static bool getBit(long x, int i);
+
+
+ /*---- Constants and tables ----*/
+
+ // The minimum version number supported in the QR Code Model 2 standard.
+ public: static constexpr int MIN_VERSION = 1;
+
+ // The maximum version number supported in the QR Code Model 2 standard.
+ public: static constexpr int MAX_VERSION = 40;
+
+
+ // For use in getPenaltyScore(), when evaluating which mask is best.
+ private: static const int PENALTY_N1;
+ private: static const int PENALTY_N2;
+ private: static const int PENALTY_N3;
+ private: static const int PENALTY_N4;
+
+
+ private: static const std::int8_t ECC_CODEWORDS_PER_BLOCK[4][41];
+ private: static const std::int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41];
+
+};
+
+
+
+/*---- Public exception class ----*/
+
+/*
+ * Thrown when the supplied data does not fit any QR Code version. Ways to handle this exception include:
+ * - Decrease the error correction level if it was greater than Ecc::LOW.
+ * - If the encodeSegments() function was called with a maxVersion argument, then increase
+ * it if it was less than QrCode::MAX_VERSION. (This advice does not apply to the other
+ * factory functions because they search all versions up to QrCode::MAX_VERSION.)
+ * - Split the text data into better or optimal segments in order to reduce the number of bits required.
+ * - Change the text or binary data to be shorter.
+ * - Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).
+ * - Propagate the error upward to the caller/user.
+ */
+class data_too_long : public std::length_error {
+
+ public: explicit data_too_long(const std::string &msg);
+
+};
+
+
+
+/*
+ * An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.
+ */
+class BitBuffer final : public std::vector {
+
+ /*---- Constructor ----*/
+
+ // Creates an empty bit buffer (length 0).
+ public: BitBuffer();
+
+
+
+ /*---- Method ----*/
+
+ // Appends the given number of low-order bits of the given value
+ // to this buffer. Requires 0 <= len <= 31 and val < 2^len.
+ public: void appendBits(std::uint32_t val, int len);
+
+};
+
+}
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index af26e2446d5..2e7c9e5aad4 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,4 +1,4 @@
-# Copyright (c) 2014-2024, The Monero Project
+# Copyright (c) 2014-2026, The Monero Project
#
# All rights reserved.
#
diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h
index d3a21836571..7a90ec8d8bc 100644
--- a/src/blockchain_db/blockchain_db.h
+++ b/src/blockchain_db/blockchain_db.h
@@ -1034,6 +1034,16 @@ class BlockchainDB
*/
virtual difficulty_type get_block_difficulty(const uint64_t& height) const = 0;
+ /**
+ * @brief correct blocks cumulative difficulties that were incorrectly calculated due to the 'difficulty drift' bug
+ *
+ * If the block does not exist, the subclass should throw BLOCK_DNE
+ *
+ * @param start_height the height where the drift starts
+ * @param new_cumulative_difficulties new cumulative difficulties to be stored
+ */
+ virtual void correct_block_cumulative_difficulties(const uint64_t& start_height, const std::vector& new_cumulative_difficulties) = 0;
+
/**
* @brief fetch a block's already generated coins
*
diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp
index b0aa03ce867..5b336db8faf 100644
--- a/src/blockchain_db/lmdb/db_lmdb.cpp
+++ b/src/blockchain_db/lmdb/db_lmdb.cpp
@@ -2779,6 +2779,44 @@ difficulty_type BlockchainLMDB::get_block_difficulty(const uint64_t& height) con
return diff1 - diff2;
}
+void BlockchainLMDB::correct_block_cumulative_difficulties(const uint64_t& start_height, const std::vector& new_cumulative_difficulties)
+{
+ LOG_PRINT_L3("BlockchainLMDB::" << __func__);
+ check_open();
+ mdb_txn_cursors *m_cursors = &m_wcursors;
+
+ int result = 0;
+ block_wtxn_start();
+ CURSOR(block_info)
+
+ const uint64_t bc_height = height();
+ if (start_height + new_cumulative_difficulties.size() != bc_height)
+ {
+ block_wtxn_abort();
+ throw0(DB_ERROR("Incorrect new_cumulative_difficulties size"));
+ }
+
+ for (uint64_t height = start_height; height < bc_height; ++height)
+ {
+ MDB_val_set(key, height);
+ result = mdb_cursor_get(m_cur_block_info, (MDB_val *)&zerokval, &key, MDB_GET_BOTH);
+ if (result)
+ throw1(BLOCK_DNE(lmdb_error("Failed to get block info: ", result).c_str()));
+
+ mdb_block_info bi = *(mdb_block_info*)key.mv_data;
+ const difficulty_type d = new_cumulative_difficulties[height - start_height];
+ bi.bi_diff_hi = ((d >> 64) & 0xffffffffffffffff).convert_to();
+ bi.bi_diff_lo = (d & 0xffffffffffffffff).convert_to();
+
+ MDB_val_set(key2, height);
+ MDB_val_set(val, bi);
+ result = mdb_cursor_put(m_cur_block_info, &key2, &val, MDB_CURRENT);
+ if (result)
+ throw0(DB_ERROR(lmdb_error("Failed to overwrite block info to db transaction: ", result).c_str()));
+ }
+ block_wtxn_stop();
+}
+
uint64_t BlockchainLMDB::get_block_already_generated_coins(const uint64_t& height) const
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h
index ba91bd4d510..c3f69995a48 100644
--- a/src/blockchain_db/lmdb/db_lmdb.h
+++ b/src/blockchain_db/lmdb/db_lmdb.h
@@ -229,6 +229,8 @@ class BlockchainLMDB : public BlockchainDB
virtual difficulty_type get_block_difficulty(const uint64_t& height) const;
+ virtual void correct_block_cumulative_difficulties(const uint64_t& start_height, const std::vector& new_cumulative_difficulties);
+
virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const;
virtual uint64_t get_block_long_term_weight(const uint64_t& height) const;
diff --git a/src/blockchain_db/testdb.h b/src/blockchain_db/testdb.h
index e8e994a0f3d..1e3a33cd8dc 100644
--- a/src/blockchain_db/testdb.h
+++ b/src/blockchain_db/testdb.h
@@ -82,6 +82,7 @@ class BaseTestDB: public cryptonote::BlockchainDB {
virtual std::vector get_block_weights(uint64_t start_height, size_t count) const override { return {}; }
virtual cryptonote::difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const override { return 10; }
virtual cryptonote::difficulty_type get_block_difficulty(const uint64_t& height) const override { return 0; }
+ virtual void correct_block_cumulative_difficulties(const uint64_t& start_height, const std::vector& new_cumulative_difficulties) override {}
virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const override { return 10000000000; }
virtual uint64_t get_block_long_term_weight(const uint64_t& height) const override { return 128; }
virtual std::vector get_long_term_block_weights(uint64_t start_height, size_t count) const override { return {}; }
diff --git a/src/blockchain_utilities/blockchain_blackball.cpp b/src/blockchain_utilities/blockchain_blackball.cpp
index abbbb7fd8d3..9b457f7f187 100644
--- a/src/blockchain_utilities/blockchain_blackball.cpp
+++ b/src/blockchain_utilities/blockchain_blackball.cpp
@@ -28,8 +28,6 @@
#include
#include
-#include
-#include
#include "common/unordered_containers_boost_serialization.h"
#include "common/command_line.h"
#include "common/varint.h"
diff --git a/src/blocks/checkpoints.dat b/src/blocks/checkpoints.dat
index e123a2199ca..6becb5fbc71 100644
Binary files a/src/blocks/checkpoints.dat and b/src/blocks/checkpoints.dat differ
diff --git a/src/checkpoints/checkpoints.cpp b/src/checkpoints/checkpoints.cpp
index 2ae08f21caa..d4a13ebd41e 100644
--- a/src/checkpoints/checkpoints.cpp
+++ b/src/checkpoints/checkpoints.cpp
@@ -72,7 +72,7 @@ namespace cryptonote
{
}
//---------------------------------------------------------------------------
- bool checkpoints::add_checkpoint(uint64_t height, const std::string& hash_str)
+ bool checkpoints::add_checkpoint(uint64_t height, const std::string& hash_str, const std::string& difficulty_str)
{
crypto::hash h = crypto::null_hash;
bool r = epee::string_tools::hex_to_pod(hash_str, h);
@@ -84,6 +84,23 @@ namespace cryptonote
CHECK_AND_ASSERT_MES(h == m_points[height], false, "Checkpoint at given height already exists, and hash for new checkpoint was different!");
}
m_points[height] = h;
+ if (!difficulty_str.empty())
+ {
+ try
+ {
+ difficulty_type difficulty(difficulty_str);
+ if (m_difficulty_points.count(height))
+ {
+ CHECK_AND_ASSERT_MES(difficulty == m_difficulty_points[height], false, "Difficulty checkpoint at given height already exists, and difficulty for new checkpoint was different!");
+ }
+ m_difficulty_points[height] = difficulty;
+ }
+ catch (...)
+ {
+ LOG_ERROR("Failed to parse difficulty checkpoint: " << difficulty_str);
+ return false;
+ }
+ }
return true;
}
//---------------------------------------------------------------------------
@@ -143,6 +160,11 @@ namespace cryptonote
{
return m_points;
}
+ //---------------------------------------------------------------------------
+ const std::map& checkpoints::get_difficulty_points() const
+ {
+ return m_difficulty_points;
+ }
bool checkpoints::check_for_conflicts(const checkpoints& other) const
{
@@ -166,35 +188,35 @@ namespace cryptonote
{
return true;
}
- ADD_CHECKPOINT(1, "f48e334a588b89260bd75a1925df0f145751cda3025b713c73aa78232da3e4d6");
- ADD_CHECKPOINT(100, "3ecd27aded3269d142128aab5af78fc91c7fa2c7adf63e58844208b46fa83b58");
- ADD_CHECKPOINT(10000, "ee54ea5924ffa8677290620a6ddc865224ea8a297e7c08a10dbb62d156e2a1ec");
- ADD_CHECKPOINT(50000, "cc3e8c228df95fe3abedeb0339ef8ae425b592b6ce61ee853a87b8aad20b69b7");
- ADD_CHECKPOINT(75000, "d9a01e54ebc9fb720e326fcc10c9a7a83e64c339a038fb33e420e4d913aa0dce");
- ADD_CHECKPOINT(100000, "1d69279e55a88120db679e7ecceb5415424623422609d86f6a2fbdb892a55b93");
- ADD_CHECKPOINT(110000, "019d822a299f2c5c7acfc19b32649c3add8e0697ea6e1592d3c46cd2036ca2f3");
- ADD_CHECKPOINT(150000, "b9cbaa0bccd30478f26fe4f6177b66c010cbf2b589c9c44c82a9794bce5ed418");
- ADD_CHECKPOINT(300000, "85fce3b66fd8113aace405957b3459c54dd39d9b5eb6976ecdfec9e750399562");
- ADD_CHECKPOINT(400000, "1ed1e3fd9174be998ac4f446dcfc91080477a4a790acf5d199fc633b9d76af01");
- ADD_CHECKPOINT(500000, "4a5e0b52efa1b922e793d1d5b5f77930c55c19770b5f5e680ee816f5bc2a7c97");
- ADD_CHECKPOINT(600000, "0a42fe6a40f5b084fb63c77c0d76e9e274a78687397af5cc6bbb27a9458d9afb");
- ADD_CHECKPOINT(700000, "a0a62377dc0ea9d869b0c381ffe19b18ea0434bb720390f4bd57b509f252289d");
- ADD_CHECKPOINT(800000, "62207aad524bdcc3572f1e1d778a64bd571c90cc4f0c20fa48a59b30eac42ed8");
- ADD_CHECKPOINT(1000000, "bd44362fdc5a0e3f83765fa26a85f0b395bb790e21ff25a9ed802cd8a94e91b4");
- ADD_CHECKPOINT(1250000, "7a53910047eb12b347375b75213ed7b28656fccc76281002f759e676a7931396");
- ADD_CHECKPOINT(1500000, "4c0a13efaa9f39e5e870621d39aaa7baf33c9c27a137a9a20d7ba57332a9f588");
- ADD_CHECKPOINT(1750000, "a06ad4b21cd3b9d5544f62f77c3dd163dc240913252582dfc75fab829ab0d149");
- ADD_CHECKPOINT(2000000, "2a5920d297d8e1e88bc0845d6b964c46009d78266258596c84dff446c9a1042c");
- ADD_CHECKPOINT(2250000, "e58da5b896cb9e923a8daf7d7f7d670636d2fb8e9b8b7ae723b1ddfd09068e93");
- ADD_CHECKPOINT(2500000, "70791ca10ff16168dca78076efa67f2153c4ff68fe5b92d100d16fca7966bfb3");
- ADD_CHECKPOINT(2750000, "7b7106b23809716b4a9e0e6ee881fa6272ac15f2f42522d0666d4a6aa31aad78");
- ADD_CHECKPOINT(3000000, "04f18897bb02bc6275069b6cb460d34ca7df011e6a913b5efec0557f8639086f");
- ADD_CHECKPOINT(3250000, "5535b32b30fe6896de4c966962006bdc4a9fc706f3f4d31ec35cfa9edacbc41e");
- ADD_CHECKPOINT(3500000, "551e50f2088bb66715a0ba3e8cc80af6d159d4f671426064382a74503eb0f4b3");
- ADD_CHECKPOINT(3616348, "e833befdc5ccb515d2fc4337c314bfcece32887cbc6ec619770599ae02c13122");
- ADD_CHECKPOINT(3619107, "b42aa409b05d7c0d399e67391b3f332f590dda2c92611bf82ea1850ccaabff2a");
- ADD_CHECKPOINT(8618276, "bc3e927cb1b24893264554a655671b31d5e938204c3c25b13eb0c18594d07e80");
- ADD_CHECKPOINT(13305079, "f8aa74925f361d4747d8d25a0cfd02960d15dc401bd7f4be621258b3119b6b9e")
+ ADD_CHECKPOINT2(1, "f48e334a588b89260bd75a1925df0f145751cda3025b713c73aa78232da3e4d6","0x2");
+ ADD_CHECKPOINT2(100, "3ecd27aded3269d142128aab5af78fc91c7fa2c7adf63e58844208b46fa83b58","0x3504B");
+ ADD_CHECKPOINT2(10000, "ee54ea5924ffa8677290620a6ddc865224ea8a297e7c08a10dbb62d156e2a1ec","0x2712D3E7B");
+ ADD_CHECKPOINT2(50000, "cc3e8c228df95fe3abedeb0339ef8ae425b592b6ce61ee853a87b8aad20b69b7","0x1F2F615B3EA");
+ ADD_CHECKPOINT2(75000, "d9a01e54ebc9fb720e326fcc10c9a7a83e64c339a038fb33e420e4d913aa0dce","0x43BD58C6FEC");
+ ADD_CHECKPOINT2(100000, "1d69279e55a88120db679e7ecceb5415424623422609d86f6a2fbdb892a55b93","0x6ACDF20304C");
+ ADD_CHECKPOINT2(110000, "019d822a299f2c5c7acfc19b32649c3add8e0697ea6e1592d3c46cd2036ca2f3","0x77748FED217");
+ ADD_CHECKPOINT2(150000, "b9cbaa0bccd30478f26fe4f6177b66c010cbf2b589c9c44c82a9794bce5ed418","0xA5AB15EEE01");
+ ADD_CHECKPOINT2(300000, "85fce3b66fd8113aace405957b3459c54dd39d9b5eb6976ecdfec9e750399562","0x1529FF8C8905");
+ ADD_CHECKPOINT2(400000, "1ed1e3fd9174be998ac4f446dcfc91080477a4a790acf5d199fc633b9d76af01","0x1AC1A7D1CCEB");
+ ADD_CHECKPOINT2(500000, "4a5e0b52efa1b922e793d1d5b5f77930c55c19770b5f5e680ee816f5bc2a7c97","0x2034D7D86291");
+ ADD_CHECKPOINT2(600000, "0a42fe6a40f5b084fb63c77c0d76e9e274a78687397af5cc6bbb27a9458d9afb","0x24F52E832797");
+ ADD_CHECKPOINT2(700000, "a0a62377dc0ea9d869b0c381ffe19b18ea0434bb720390f4bd57b509f252289d","0x24F551CF20A0");
+ ADD_CHECKPOINT2(800000, "62207aad524bdcc3572f1e1d778a64bd571c90cc4f0c20fa48a59b30eac42ed8","0x24F591211BE7");
+ ADD_CHECKPOINT2(1000000, "bd44362fdc5a0e3f83765fa26a85f0b395bb790e21ff25a9ed802cd8a94e91b4","0x24F601E4388B");
+ ADD_CHECKPOINT2(1250000, "7a53910047eb12b347375b75213ed7b28656fccc76281002f759e676a7931396","0x24F63CC490DE");
+ ADD_CHECKPOINT2(1500000, "4c0a13efaa9f39e5e870621d39aaa7baf33c9c27a137a9a20d7ba57332a9f588","0x24F6D4997FF4");
+ ADD_CHECKPOINT2(1750000, "a06ad4b21cd3b9d5544f62f77c3dd163dc240913252582dfc75fab829ab0d149","0x24F78B4023F1");
+ ADD_CHECKPOINT2(2000000, "2a5920d297d8e1e88bc0845d6b964c46009d78266258596c84dff446c9a1042c","0x24F7E904CEEC");
+ ADD_CHECKPOINT2(2250000, "e58da5b896cb9e923a8daf7d7f7d670636d2fb8e9b8b7ae723b1ddfd09068e93","0x24F837A95DA3");
+ ADD_CHECKPOINT2(2500000, "70791ca10ff16168dca78076efa67f2153c4ff68fe5b92d100d16fca7966bfb3","0x24F87E038F7C");
+ ADD_CHECKPOINT2(2750000, "7b7106b23809716b4a9e0e6ee881fa6272ac15f2f42522d0666d4a6aa31aad78","0x24F8AF51F7A2");
+ ADD_CHECKPOINT2(3000000, "04f18897bb02bc6275069b6cb460d34ca7df011e6a913b5efec0557f8639086f","0x24F8D34B21D0");
+ ADD_CHECKPOINT2(3250000, "5535b32b30fe6896de4c966962006bdc4a9fc706f3f4d31ec35cfa9edacbc41e","0x24F8EEC16082");
+ ADD_CHECKPOINT2(3500000, "551e50f2088bb66715a0ba3e8cc80af6d159d4f671426064382a74503eb0f4b3","0x24F909221282");
+ ADD_CHECKPOINT2(3616348, "e833befdc5ccb515d2fc4337c314bfcece32887cbc6ec619770599ae02c13122","0x24F911C83D1D");
+ ADD_CHECKPOINT2(3619107, "b42aa409b05d7c0d399e67391b3f332f590dda2c92611bf82ea1850ccaabff2a","0x24F911F345ED");
+ ADD_CHECKPOINT2(8618276, "bc3e927cb1b24893264554a655671b31d5e938204c3c25b13eb0c18594d07e80","0x24F97598B380");
+ ADD_CHECKPOINT2(13305079, "f8aa74925f361d4747d8d25a0cfd02960d15dc401bd7f4be621258b3119b6b9e","0x24F992DBF446")
return true;
}
diff --git a/src/checkpoints/checkpoints.h b/src/checkpoints/checkpoints.h
index a55b94bf089..72e4f875c03 100644
--- a/src/checkpoints/checkpoints.h
+++ b/src/checkpoints/checkpoints.h
@@ -33,8 +33,10 @@
#include "misc_log_ex.h"
#include "crypto/hash.h"
#include "cryptonote_config.h"
+#include "cryptonote_basic/difficulty.h"
#define ADD_CHECKPOINT(h, hash) CHECK_AND_ASSERT(add_checkpoint(h, hash), false);
+#define ADD_CHECKPOINT2(h, hash, difficulty) CHECK_AND_ASSERT(add_checkpoint(h, hash, difficulty), false);
#define JSON_HASH_FILE_NAME "checkpoints.json"
@@ -61,12 +63,13 @@ namespace cryptonote
*
* @param height the height of the block the checkpoint is for
* @param hash_str the hash of the block, as a string
+ * @param difficulty_str the cumulative difficulty of the block, as a string (optional)
*
* @return false if parsing the hash fails, or if the height is a duplicate
* AND the existing checkpoint hash does not match the new one,
* otherwise returns true
*/
- bool add_checkpoint(uint64_t height, const std::string& hash_str);
+ bool add_checkpoint(uint64_t height, const std::string& hash_str, const std::string& difficulty_str = "");
/**
* @brief checks if there is a checkpoint in the future
@@ -133,6 +136,13 @@ namespace cryptonote
*/
const std::map& get_points() const;
+ /**
+ * @brief gets the difficulty checkpoints container
+ *
+ * @return a const reference to the difficulty checkpoints container
+ */
+ const std::map& get_difficulty_points() const;
+
/**
* @brief checks if our checkpoints container conflicts with another
*
@@ -187,6 +197,7 @@ namespace cryptonote
private:
std::map m_points; //!< the checkpoints container
+ std::map m_difficulty_points; //!< the difficulty checkpoints container
};
}
diff --git a/src/cryptonote_basic/cryptonote_basic.h b/src/cryptonote_basic/cryptonote_basic.h
index a92327b0af3..0d39e1f4e18 100644
--- a/src/cryptonote_basic/cryptonote_basic.h
+++ b/src/cryptonote_basic/cryptonote_basic.h
@@ -37,7 +37,7 @@
#include
#include
#include "serialization/variant.h"
-#include "serialization/vector.h"
+#include "serialization/containers.h"
#include "serialization/binary_archive.h"
#include "serialization/json_archive.h"
#include "serialization/debug_archive.h"
diff --git a/src/cryptonote_basic/cryptonote_basic_impl.cpp b/src/cryptonote_basic/cryptonote_basic_impl.cpp
index bf67fa8ffef..d7582892d34 100644
--- a/src/cryptonote_basic/cryptonote_basic_impl.cpp
+++ b/src/cryptonote_basic/cryptonote_basic_impl.cpp
@@ -34,7 +34,6 @@ using namespace epee;
#include "cryptonote_basic_impl.h"
#include "string_tools.h"
#include "serialization/binary_utils.h"
-#include "serialization/container.h"
#include "cryptonote_format_utils.h"
#include "cryptonote_config.h"
#include "misc_language.h"
diff --git a/src/cryptonote_basic/cryptonote_boost_serialization.h b/src/cryptonote_basic/cryptonote_boost_serialization.h
index e31352702e3..df289f6de77 100644
--- a/src/cryptonote_basic/cryptonote_boost_serialization.h
+++ b/src/cryptonote_basic/cryptonote_boost_serialization.h
@@ -36,7 +36,6 @@
#include
#include
#include
-#include
#include
#include
#include "cryptonote_basic.h"
diff --git a/src/cryptonote_basic/difficulty.cpp b/src/cryptonote_basic/difficulty.cpp
index 8251614c559..fe38d699db4 100644
--- a/src/cryptonote_basic/difficulty.cpp
+++ b/src/cryptonote_basic/difficulty.cpp
@@ -174,7 +174,7 @@ namespace cryptonote {
// blockchain errors "difficulty overhead" if this function returns zero.
// TODO: consider throwing an exception instead
if (high != 0 || low + time_span - 1 < low) {
- return 0;
+ return 1;
}
return (low + time_span - 1) / time_span;
}
diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h
index 12818163993..e75516dd037 100644
--- a/src/cryptonote_config.h
+++ b/src/cryptonote_config.h
@@ -162,6 +162,7 @@
#define HF_VERSION_REJECT_SIGS_IN_COINBASE 12
#define HF_VERSION_ENFORCE_MIN_AGE 12
#define HF_VERSION_EFFECTIVE_SHORT_TERM_MEDIAN_IN_PENALTY 12
+#define HF_VERSION_EXACT_COINBASE 13
#define PER_KB_FEE_QUANTIZATION_DECIMALS 8
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp
index 484610bbc9e..bf950c382d8 100644
--- a/src/cryptonote_core/blockchain.cpp
+++ b/src/cryptonote_core/blockchain.cpp
@@ -32,6 +32,7 @@
#include
#include
#include
+#include
#include "include_base_utils.h"
#include "cryptonote_basic/cryptonote_basic_impl.h"
@@ -435,6 +436,15 @@ bool Blockchain::init(BlockchainDB* db, const network_type nettype, bool offline
m_long_term_block_weights_cache_rolling_median = epee::misc_utils::rolling_median_t(m_long_term_block_weights_window);
}
+ bool difficulty_ok;
+ uint64_t difficulty_recalc_height;
+ std::tie(difficulty_ok, difficulty_recalc_height) = check_difficulty_checkpoints();
+ if (!difficulty_ok)
+ {
+ MERROR("Difficulty drift detected!");
+ recalculate_difficulties(difficulty_recalc_height);
+ }
+
{
db_txn_guard txn_guard(m_db, m_db->is_read_only());
if (!update_next_cumulative_weight_limit())
@@ -957,6 +967,111 @@ difficulty_type Blockchain::get_difficulty_for_next_block()
return diff;
}
//------------------------------------------------------------------
+std::pair Blockchain::check_difficulty_checkpoints() const
+{
+ uint64_t res = 0;
+ for (const std::pair& i : m_checkpoints.get_difficulty_points())
+ {
+ if (i.first >= m_db->height())
+ break;
+ if (m_db->get_block_cumulative_difficulty(i.first) != i.second)
+ return {false, res};
+ res = i.first;
+ }
+ return {true, res};
+}
+//------------------------------------------------------------------
+size_t Blockchain::recalculate_difficulties(boost::optional start_height_opt)
+{
+ if (m_fixed_difficulty)
+ {
+ return 0;
+ }
+ LOG_PRINT_L3("Blockchain::" << __func__);
+ CRITICAL_REGION_LOCAL(m_blockchain_lock);
+
+ const uint64_t start_height = start_height_opt ? *start_height_opt : check_difficulty_checkpoints().second;
+ const uint64_t top_height = m_db->height() - 1;
+ MGINFO("Recalculating difficulties from height " << start_height << " to height " << top_height);
+
+ std::vector timestamps;
+ std::vector difficulties;
+ timestamps.reserve(DIFFICULTY_BLOCKS_COUNT + 1);
+ difficulties.reserve(DIFFICULTY_BLOCKS_COUNT + 1);
+ if (start_height > 1)
+ {
+ for (uint64_t i = 0; i < DIFFICULTY_BLOCKS_COUNT; ++i)
+ {
+ uint64_t height = start_height - 1 - i;
+ if (height == 0)
+ break;
+ timestamps.insert(timestamps.begin(), m_db->get_block_timestamp(height));
+ difficulties.insert(difficulties.begin(), m_db->get_block_cumulative_difficulty(height));
+ }
+ }
+ difficulty_type last_cum_diff = start_height <= 1 ? start_height : difficulties.back();
+ uint64_t drift_start_height = 0;
+ std::vector new_cumulative_difficulties;
+ for (uint64_t height = start_height; height <= top_height; ++height)
+ {
+ size_t target = DIFFICULTY_TARGET;
+ difficulty_type recalculated_diff = next_difficulty(timestamps, difficulties, target,height);
+
+ boost::multiprecision::uint256_t recalculated_cum_diff_256 = boost::multiprecision::uint256_t(recalculated_diff) + last_cum_diff;
+ CHECK_AND_ASSERT_THROW_MES(recalculated_cum_diff_256 <= std::numeric_limits::max(), "Difficulty overflow!");
+ difficulty_type recalculated_cum_diff = recalculated_cum_diff_256.convert_to();
+
+ if (drift_start_height == 0)
+ {
+ difficulty_type existing_cum_diff = m_db->get_block_cumulative_difficulty(height);
+ if (recalculated_cum_diff != existing_cum_diff)
+ {
+ drift_start_height = height;
+ new_cumulative_difficulties.reserve(top_height + 1 - height);
+ LOG_ERROR("Difficulty drift found at height:" << height << ", hash:" << m_db->get_block_hash_from_height(height) << ", existing:" << existing_cum_diff << ", recalculated:" << recalculated_cum_diff);
+ }
+ }
+ if (drift_start_height > 0)
+ {
+ new_cumulative_difficulties.push_back(recalculated_cum_diff);
+ if (height % 100000 == 0)
+ LOG_ERROR(boost::format("%llu / %llu (%.1f%%)") % height % top_height % (100 * (height - drift_start_height) / float(top_height - drift_start_height)));
+ }
+
+ if (height > 0)
+ {
+ timestamps.push_back(m_db->get_block_timestamp(height));
+ difficulties.push_back(recalculated_cum_diff);
+ }
+ if (timestamps.size() > DIFFICULTY_BLOCKS_COUNT)
+ {
+ CHECK_AND_ASSERT_THROW_MES(timestamps.size() == DIFFICULTY_BLOCKS_COUNT + 1, "Wrong timestamps size: " << timestamps.size());
+ timestamps.erase(timestamps.begin());
+ difficulties.erase(difficulties.begin());
+ }
+ last_cum_diff = recalculated_cum_diff;
+ }
+
+ if (drift_start_height > 0)
+ {
+ LOG_ERROR("Writing to the DB...");
+ try
+ {
+ m_db->correct_block_cumulative_difficulties(drift_start_height, new_cumulative_difficulties);
+ }
+ catch (const std::exception& e)
+ {
+ LOG_ERROR("Error correcting cumulative difficulties from height " << drift_start_height << ", what = " << e.what());
+ }
+ LOG_ERROR("Corrected difficulties for " << new_cumulative_difficulties.size() << " blocks");
+ // clear cache
+ m_difficulty_for_next_block_top_hash = crypto::null_hash;
+ m_timestamps_and_difficulties_height = 0;
+ }
+
+ return new_cumulative_difficulties.size();
+}
+//------------------------------------------------------------------
std::vector Blockchain::get_last_block_timestamps(unsigned int blocks) const
{
CRITICAL_REGION_LOCAL(m_blockchain_lock);
@@ -1268,10 +1383,24 @@ bool Blockchain::validate_miner_transaction(const block& b, size_t cumulative_bl
MERROR_VER("coinbase transaction spend too much money (" << print_money(money_in_use) << "). Block reward is " << print_money(base_reward + fee) << "(" << print_money(base_reward) << "+" << print_money(fee) << "), cumulative_block_weight " << cumulative_block_weight);
return false;
}
- if(base_reward + fee != money_in_use)
+ // From hard fork 2 till 12, we allow a miner to claim less block reward than is allowed, in case a miner wants less dust
+ if (version < 2 || version >= HF_VERSION_EXACT_COINBASE)
{
- MDEBUG("coinbase transaction doesn't use full amount of block reward: spent: " << money_in_use << ", block reward " << base_reward + fee << "(" << base_reward << "+" << fee << ")");
- return false;
+ if(base_reward + fee != money_in_use)
+ {
+ MDEBUG("coinbase transaction doesn't use full amount of block reward: spent: " << money_in_use << ", block reward " << base_reward + fee << "(" << base_reward << "+" << fee << ")");
+ return false;
+ }
+ }
+ else
+ {
+ // from hard fork 2, since a miner can claim less than the full block reward, we update the base_reward
+ // to show the amount of coins that were actually generated, the remainder will be pushed back for later
+ // emission. This modifies the emission curve very slightly.
+ CHECK_AND_ASSERT_MES(money_in_use - fee <= base_reward, false, "base reward calculation bug");
+ if(base_reward + fee != money_in_use)
+ partial_block_reward = true;
+ base_reward = money_in_use - fee;
}
return true;
}
@@ -5049,7 +5178,7 @@ void Blockchain::cancel()
}
#if defined(PER_BLOCK_CHECKPOINT)
-static const char expected_block_hashes_hash[] = "F8AA74925F361D4747D8D25A0CFD02960D15DC401BD7F4BE621258B3119B6B9E";
+static const char expected_block_hashes_hash[] = "701c1d2d019335284a04dd8fb9d95a70a8b7f388ac9f0e51f2cbcf3d90d7bb39";
void Blockchain::load_compiled_in_block_hashes(const GetCheckpointsCallback& get_checkpoints)
{
if (get_checkpoints == nullptr || !m_fast_sync)
diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h
index a6cca03571e..b39b6aba5f2 100644
--- a/src/cryptonote_core/blockchain.h
+++ b/src/cryptonote_core/blockchain.h
@@ -313,6 +313,22 @@ namespace cryptonote
*/
difficulty_type get_difficulty_for_next_block();
+ /**
+ * @brief check currently stored difficulties against difficulty checkpoints
+ *
+ * @return {flag, height} flag: true if all difficulty checkpoints pass, height: the last checkpoint height before the difficulty drift bug starts
+ */
+ std::pair check_difficulty_checkpoints() const;
+
+ /**
+ * @brief recalculate difficulties for blocks after the last difficulty checkpoints to circumvent the annoying 'difficulty drift' bug
+ *
+ * @param start_height: if omitted, starts recalculation from the last difficulty checkpoint
+ *
+ * @return number of blocks whose difficulties got corrected
+ */
+ size_t recalculate_difficulties(boost::optional start_height = boost::none);
+
/**
* @brief adds a block to the blockchain
*
diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp
index a45c43fb9de..f83830ca570 100644
--- a/src/cryptonote_core/cryptonote_core.cpp
+++ b/src/cryptonote_core/cryptonote_core.cpp
@@ -1686,6 +1686,7 @@ namespace cryptonote
m_check_disk_space_interval.do_call(boost::bind(&core::check_disk_space, this));
m_block_rate_interval.do_call(boost::bind(&core::check_block_rate, this));
m_blockchain_pruning_interval.do_call(boost::bind(&core::update_blockchain_pruning, this));
+ m_diff_recalc_interval.do_call(boost::bind(&core::recalculate_difficulties, this));
m_miner.on_idle();
m_mempool.on_idle();
return true;
@@ -1925,6 +1926,12 @@ namespace cryptonote
return true;
}
//-----------------------------------------------------------------------------------------------
+ bool core::recalculate_difficulties()
+ {
+ m_blockchain_storage.recalculate_difficulties();
+ return true;
+ }
+ //-----------------------------------------------------------------------------------------------
void core::flush_bad_txs_cache()
{
bad_semantics_txes_lock.lock();
diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h
index 71d72bb277f..0a8fde14065 100644
--- a/src/cryptonote_core/cryptonote_core.h
+++ b/src/cryptonote_core/cryptonote_core.h
@@ -1038,6 +1038,13 @@ namespace cryptonote
*/
bool check_block_rate();
+ /**
+ * @brief recalculate difficulties after the last difficulty checklpoint to circumvent the annoying 'difficulty drift' bug
+ *
+ * @return true
+ */
+ bool recalculate_difficulties();
+
bool m_test_drop_download = true; //!< whether or not to drop incoming blocks (for testing)
uint64_t m_test_drop_download_height = 0; //!< height under which to drop incoming blocks, if doing so
@@ -1063,6 +1070,7 @@ namespace cryptonote
epee::math_helper::once_a_time_seconds<60*10, true> m_check_disk_space_interval; //!< interval for checking for disk space
epee::math_helper::once_a_time_seconds<90, false> m_block_rate_interval; //!< interval for checking block rate
epee::math_helper::once_a_time_seconds<60*60*5, true> m_blockchain_pruning_interval; //!< interval for incremental blockchain pruning
+ epee::math_helper::once_a_time_seconds<60*60*24*7, false> m_diff_recalc_interval; //!< interval for recalculating difficulties
std::atomic m_starter_message_showed; //!< has the "daemon will sync now" message been shown?
diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp
index 6800dd627d0..d58e7170c03 100644
--- a/src/daemon/command_server.cpp
+++ b/src/daemon/command_server.cpp
@@ -57,6 +57,12 @@ t_command_server::t_command_server(
, "help []"
, "Show the help section or the documentation about a ."
);
+ m_command_lookup.set_handler(
+ "apropos"
+ , std::bind(&t_command_server::apropos, this, p::_1)
+ , "apropos [ ...]"
+ , "Search all command descriptions for keyword(s)."
+ );
m_command_lookup.set_handler(
"print_height"
, std::bind(&t_command_parser_executor::print_height, &m_parser, p::_1)
@@ -349,7 +355,7 @@ bool t_command_server::start_handling(std::function exit_handler)
{
if (m_is_rpc) return false;
- m_command_lookup.start_handling("", get_commands_str(), exit_handler);
+ m_command_lookup.start_handling("", "Use \"help\" to list all commands and their usage\n", exit_handler);
return true;
}
@@ -374,6 +380,33 @@ bool t_command_server::help(const std::vector& args)
return true;
}
+bool t_command_server::apropos(const std::vector& args)
+{
+ if (args.empty())
+ {
+ std::cout << "Missing keyword" << std::endl;
+ return true;
+ }
+ const std::vector& command_list = m_command_lookup.get_command_list(args);
+ if (command_list.empty())
+ {
+ std::cout << "Nothing found" << std::endl;
+ return true;
+ }
+
+ std::cout << std::endl;
+ for(auto const& command:command_list)
+ {
+ std::vector cmd;
+ cmd.push_back(command);
+ std::pair documentation = m_command_lookup.get_documentation(cmd);
+ std::cout << " " << documentation.first << std::endl;
+ }
+ std::cout << std::endl;
+
+ return true;
+}
+
std::string t_command_server::get_commands_str()
{
std::stringstream ss;
@@ -382,7 +415,7 @@ std::string t_command_server::get_commands_str()
std::string usage = m_command_lookup.get_usage();
boost::replace_all(usage, "\n", "\n ");
usage.insert(0, " ");
- ss << usage << std::endl;
+ ss << usage;
return ss.str();
}
diff --git a/src/daemon/command_server.h b/src/daemon/command_server.h
index da532223e56..5f8a79205d1 100644
--- a/src/daemon/command_server.h
+++ b/src/daemon/command_server.h
@@ -73,6 +73,7 @@ class t_command_server {
private:
bool help(const std::vector& args);
+ bool apropos(const std::vector& args);
std::string get_commands_str();
std::string get_command_usage(const std::vector &args);
diff --git a/src/net/CMakeLists.txt b/src/net/CMakeLists.txt
index 1ce7118adec..afcd42ef74f 100644
--- a/src/net/CMakeLists.txt
+++ b/src/net/CMakeLists.txt
@@ -26,9 +26,9 @@
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-set(net_sources dandelionpp.cpp error.cpp i2p_address.cpp parse.cpp socks.cpp
+set(net_sources dandelionpp.cpp error.cpp http.cpp i2p_address.cpp parse.cpp socks.cpp
socks_connect.cpp tor_address.cpp zmq.cpp)
-set(net_headers dandelionpp.h error.h i2p_address.h parse.h socks.h socks_connect.h
+set(net_headers dandelionpp.h error.h http.cpp i2p_address.h parse.h socks.h socks_connect.h
tor_address.h zmq.h)
monero_add_library(net ${net_sources} ${net_headers})
diff --git a/src/serialization/set.h b/src/net/http.cpp
similarity index 64%
rename from src/serialization/set.h
rename to src/net/http.cpp
index ae602a2f1a2..f54b65c2954 100644
--- a/src/serialization/set.h
+++ b/src/net/http.cpp
@@ -1,21 +1,21 @@
-// Copyright (c) 2014-2019, The Monero Project
-//
+// Copyright (c) 2020, The Monero Project
+//
// All rights reserved.
-//
+//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
-//
+//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
-//
+//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
-//
+//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
-//
+//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
@@ -25,34 +25,46 @@
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-//
-// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
-#pragma once
+#include "http.h"
-#include
+#include "parse.h"
+#include "socks_connect.h"
-template class Archive, class T>
-bool do_serialize(Archive &ar, std::set &v);
-template class Archive, class T>
-bool do_serialize(Archive &ar, std::set &v);
+namespace net
+{
+namespace http
+{
-namespace serialization
+bool client::set_proxy(const std::string &address)
{
- namespace detail
+ if (address.empty())
+ {
+ set_connector(epee::net_utils::direct_connect{});
+ }
+ else
{
- template
- void do_add(std::set &c, T &&e)
+ const auto endpoint = get_tcp_endpoint(address);
+ if (!endpoint)
+ {
+ auto always_fail = net::socks::connector{boost::asio::ip::tcp::endpoint()};
+ set_connector(always_fail);
+ }
+ else
{
- c.insert(std::move(e));
+ set_connector(net::socks::connector{*endpoint});
}
}
-}
-#include "serialization.h"
+ disconnect();
+
+ return true;
+}
-template class Archive, class T>
-bool do_serialize(Archive &ar, std::set &v) { return do_serialize_container(ar, v); }
-template class Archive, class T>
-bool do_serialize(Archive &ar, std::set &v) { return do_serialize_container(ar, v); }
+std::unique_ptr client_factory::create()
+{
+ return std::unique_ptr(new client());
+}
+} // namespace http
+} // namespace net
diff --git a/src/serialization/unordered_set.h b/src/net/http.h
similarity index 62%
rename from src/serialization/unordered_set.h
rename to src/net/http.h
index 6dd01fd9351..91a345851e4 100644
--- a/src/serialization/unordered_set.h
+++ b/src/net/http.h
@@ -1,21 +1,21 @@
-// Copyright (c) 2014-2019, The Monero Project
-//
+// Copyright (c) 2020, The Monero Project
+//
// All rights reserved.
-//
+//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
-//
+//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
-//
+//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
-//
+//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
-//
+//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
@@ -25,34 +25,27 @@
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-//
-// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#pragma once
-#include
+#include "net/http_client.h"
-template class Archive, class T>
-bool do_serialize(Archive &ar, std::unordered_set &v);
-template class Archive, class T>
-bool do_serialize(Archive &ar, std::unordered_set &v);
-
-namespace serialization
+namespace net
+{
+namespace http
{
- namespace detail
- {
- template
- void do_add(std::unordered_set &c, T &&e)
- {
- c.insert(std::move(e));
- }
- }
-}
-#include "serialization.h"
+class client : public epee::net_utils::http::http_simple_client
+{
+public:
+ bool set_proxy(const std::string &address) override;
+};
-template class Archive, class T>
-bool do_serialize(Archive &ar, std::unordered_set &v) { return do_serialize_container(ar, v); }
-template class Archive, class T>
-bool do_serialize(Archive &ar, std::unordered_set &v) { return do_serialize_container(ar, v); }
+class client_factory : public epee::net_utils::http::http_client_factory
+{
+public:
+ std::unique_ptr create() override;
+};
+} // namespace http
+} // namespace net
diff --git a/src/net/parse.cpp b/src/net/parse.cpp
index ce580afe7be..95b22cd4446 100644
--- a/src/net/parse.cpp
+++ b/src/net/parse.cpp
@@ -122,4 +122,39 @@ namespace net
return {epee::net_utils::ipv4_network_subnet{ip, (uint8_t)mask}};
}
+
+ expect get_tcp_endpoint(const boost::string_ref address)
+ {
+ uint16_t port = 0;
+ expect parsed = get_network_address(address, port);
+ if (!parsed)
+ {
+ return parsed.error();
+ }
+
+ boost::asio::ip::tcp::endpoint result;
+ switch (parsed->get_type_id())
+ {
+ case epee::net_utils::ipv4_network_address::get_type_id():
+ {
+ const auto &ipv4 = parsed->as();
+ result = boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4(ipv4.ip()), ipv4.port());
+ break;
+ }
+ case epee::net_utils::ipv6_network_address::get_type_id():
+ {
+ const auto &ipv6 = parsed->as();
+ result = boost::asio::ip::tcp::endpoint(ipv6.ip(), ipv6.port());
+ break;
+ }
+ default:
+ return make_error_code(net::error::unsupported_address);
+ }
+ if (result.port() == 0)
+ {
+ return make_error_code(net::error::invalid_port);
+ }
+
+ return result;
+ }
}
diff --git a/src/net/parse.h b/src/net/parse.h
index 0d8fda7113d..4d5efe16159 100644
--- a/src/net/parse.h
+++ b/src/net/parse.h
@@ -28,6 +28,7 @@
#pragma once
+#include
#include
#include
@@ -65,5 +66,7 @@ namespace net
*/
expect
get_ipv4_subnet_address(boost::string_ref address, bool allow_implicit_32 = false);
+
+ expect get_tcp_endpoint(const boost::string_ref address);
}
diff --git a/src/p2p/p2p_protocol_defs.h b/src/p2p/p2p_protocol_defs.h
index 609661871d7..c5045732278 100644
--- a/src/p2p/p2p_protocol_defs.h
+++ b/src/p2p/p2p_protocol_defs.h
@@ -39,6 +39,7 @@
#include "misc_language.h"
#include "string_tools.h"
#include "time_helper.h"
+#include "serialization/serialization.h"
#include "cryptonote_config.h"
namespace nodetool
@@ -84,6 +85,15 @@ namespace nodetool
KV_SERIALIZE_OPT(rpc_port, (uint16_t)0)
KV_SERIALIZE_OPT(rpc_credits_per_hash, (uint32_t)0)
END_KV_SERIALIZE_MAP()
+
+ BEGIN_SERIALIZE()
+ FIELD(adr)
+ FIELD(id)
+ VARINT_FIELD(last_seen)
+ VARINT_FIELD(pruning_seed)
+ VARINT_FIELD(rpc_port)
+ VARINT_FIELD(rpc_credits_per_hash)
+ END_SERIALIZE()
};
typedef peerlist_entry_base peerlist_entry;
@@ -99,6 +109,12 @@ namespace nodetool
KV_SERIALIZE(id)
KV_SERIALIZE(first_seen)
END_KV_SERIALIZE_MAP()
+
+ BEGIN_SERIALIZE()
+ FIELD(adr)
+ FIELD(id)
+ VARINT_FIELD(first_seen)
+ END_SERIALIZE()
};
typedef anchor_peerlist_entry_base anchor_peerlist_entry;
@@ -114,6 +130,12 @@ namespace nodetool
KV_SERIALIZE(id)
KV_SERIALIZE(is_income)
END_KV_SERIALIZE_MAP()
+
+ BEGIN_SERIALIZE()
+ FIELD(adr)
+ FIELD(id)
+ FIELD(is_income)
+ END_SERIALIZE()
};
typedef connection_entry_base connection_entry;
diff --git a/src/ringct/rctTypes.h b/src/ringct/rctTypes.h
index 9b7f26a0256..ce11981ad77 100644
--- a/src/ringct/rctTypes.h
+++ b/src/ringct/rctTypes.h
@@ -49,7 +49,7 @@ extern "C" {
#include "hex.h"
#include "span.h"
#include "memwipe.h"
-#include "serialization/vector.h"
+#include "serialization/containers.h"
#include "serialization/debug_archive.h"
#include "serialization/binary_archive.h"
#include "serialization/json_archive.h"
@@ -239,6 +239,12 @@ namespace rct {
struct RCTConfig {
RangeProofType range_proof_type;
int bp_version;
+
+ BEGIN_SERIALIZE_OBJECT()
+ VERSION_FIELD(0)
+ VARINT_FIELD(range_proof_type)
+ VARINT_FIELD(bp_version)
+ END_SERIALIZE()
};
struct rctSigBase {
uint8_t type;
@@ -317,6 +323,16 @@ namespace rct {
ar.end_array();
return ar.stream().good();
}
+
+ BEGIN_SERIALIZE_OBJECT()
+ FIELD(type)
+ FIELD(message)
+ FIELD(mixRing)
+ FIELD(pseudoOuts)
+ FIELD(ecdhInfo)
+ FIELD(outPk)
+ VARINT_FIELD(txnFee)
+ END_SERIALIZE()
};
struct rctSigPrunable {
std::vector rangeSigs;
@@ -436,6 +452,12 @@ namespace rct {
return ar.stream().good();
}
+ BEGIN_SERIALIZE_OBJECT()
+ FIELD(rangeSigs)
+ FIELD(bulletproofs)
+ FIELD(MGs)
+ FIELD(pseudoOuts)
+ END_SERIALIZE()
};
struct rctSig: public rctSigBase {
rctSigPrunable p;
@@ -449,6 +471,11 @@ namespace rct {
{
return type == RCTTypeBulletproof || type == RCTTypeBulletproof2 ? p.pseudoOuts : pseudoOuts;
}
+
+ BEGIN_SERIALIZE_OBJECT()
+ FIELDS((rctSigBase&)*this)
+ FIELD(p)
+ END_SERIALIZE()
};
//other basepoint H = toPoint(cn_fast_hash(G)), G the basepoint
diff --git a/src/rpc/rpc_payment.cpp b/src/rpc/rpc_payment.cpp
index b412073dc1a..903f09c15fe 100644
--- a/src/rpc/rpc_payment.cpp
+++ b/src/rpc/rpc_payment.cpp
@@ -27,14 +27,12 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include
-#include
#include "cryptonote_config.h"
#include "include_base_utils.h"
#include "string_tools.h"
#include "file_io_utils.h"
#include "int-util.h"
#include "common/util.h"
-#include "serialization/crypto.h"
#include "common/unordered_containers_boost_serialization.h"
#include "cryptonote_basic/cryptonote_boost_serialization.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
@@ -288,14 +286,28 @@ namespace cryptonote
data.open(state_file_path, std::ios_base::binary | std::ios_base::in);
if (!data.fail())
{
+ bool loaded = false;
try
{
- boost::archive::portable_binary_iarchive a(data);
- a >> *this;
+ binary_archive ar(data);
+ if (::serialization::serialize(ar, *this))
+ if (::serialization::check_stream_state(ar))
+ loaded = true;
}
- catch (const std::exception &e)
+ catch (...) {}
+ if (!loaded)
{
- MERROR("Failed to load RPC payments file: " << e.what());
+ try
+ {
+ boost::archive::portable_binary_iarchive a(data);
+ a >> *this;
+ loaded = true;
+ }
+ catch (...) {}
+ }
+ if (!loaded)
+ {
+ MERROR("Failed to load RPC payments file");
m_client_info.clear();
}
}
@@ -336,8 +348,9 @@ namespace cryptonote
MWARNING("Failed to save RPC payments to file " << state_file_path);
return false;
};
- boost::archive::portable_binary_oarchive a(data);
- a << *this;
+ binary_archive ar(data);
+ if (!::serialization::serialize(ar, *const_cast(this)))
+ return false;
return true;
CATCH_ENTRY_L0("rpc_payment::store", false);
}
diff --git a/src/rpc/rpc_payment.h b/src/rpc/rpc_payment.h
index 20117985f1f..08e953f15c5 100644
--- a/src/rpc/rpc_payment.h
+++ b/src/rpc/rpc_payment.h
@@ -31,10 +31,17 @@
#include
#include
#include
+#include