aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/hash_type.h72
-rw-r--r--src/util/sock.cpp4
-rw-r--r--src/util/sock.h29
-rw-r--r--src/util/strencodings.cpp11
-rw-r--r--src/util/system.h23
-rw-r--r--src/util/thread.cpp27
-rw-r--r--src/util/thread.h18
7 files changed, 141 insertions, 43 deletions
diff --git a/src/util/hash_type.h b/src/util/hash_type.h
new file mode 100644
index 0000000000..13b831cf19
--- /dev/null
+++ b/src/util/hash_type.h
@@ -0,0 +1,72 @@
+// Copyright (c) 2020-2021 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_UTIL_HASH_TYPE_H
+#define BITCOIN_UTIL_HASH_TYPE_H
+
+template <typename HashType>
+class BaseHash
+{
+protected:
+ HashType m_hash;
+
+public:
+ BaseHash() : m_hash() {}
+ explicit BaseHash(const HashType& in) : m_hash(in) {}
+
+ unsigned char* begin()
+ {
+ return m_hash.begin();
+ }
+
+ const unsigned char* begin() const
+ {
+ return m_hash.begin();
+ }
+
+ unsigned char* end()
+ {
+ return m_hash.end();
+ }
+
+ const unsigned char* end() const
+ {
+ return m_hash.end();
+ }
+
+ operator std::vector<unsigned char>() const
+ {
+ return std::vector<unsigned char>{m_hash.begin(), m_hash.end()};
+ }
+
+ std::string ToString() const
+ {
+ return m_hash.ToString();
+ }
+
+ bool operator==(const BaseHash<HashType>& other) const noexcept
+ {
+ return m_hash == other.m_hash;
+ }
+
+ bool operator!=(const BaseHash<HashType>& other) const noexcept
+ {
+ return !(m_hash == other.m_hash);
+ }
+
+ bool operator<(const BaseHash<HashType>& other) const noexcept
+ {
+ return m_hash < other.m_hash;
+ }
+
+ size_t size() const
+ {
+ return m_hash.size();
+ }
+
+ unsigned char* data() { return m_hash.data(); }
+ const unsigned char* data() const { return m_hash.data(); }
+};
+
+#endif // BITCOIN_UTIL_HASH_TYPE_H
diff --git a/src/util/sock.cpp b/src/util/sock.cpp
index 0bc9795db3..b6c2a47434 100644
--- a/src/util/sock.cpp
+++ b/src/util/sock.cpp
@@ -179,7 +179,7 @@ void Sock::SendComplete(const std::string& data,
// Wait for a short while (or the socket to become ready for sending) before retrying
// if nothing was sent.
const auto wait_time = std::min(deadline - now, std::chrono::milliseconds{MAX_WAIT_FOR_IO});
- Wait(wait_time, SEND);
+ (void)Wait(wait_time, SEND);
}
}
@@ -262,7 +262,7 @@ std::string Sock::RecvUntilTerminator(uint8_t terminator,
// Wait for a short while (or the socket to become ready for reading) before retrying.
const auto wait_time = std::min(deadline - now, std::chrono::milliseconds{MAX_WAIT_FOR_IO});
- Wait(wait_time, RECV);
+ (void)Wait(wait_time, RECV);
}
}
diff --git a/src/util/sock.h b/src/util/sock.h
index a4df7cd21b..59cc8c0b1d 100644
--- a/src/util/sock.h
+++ b/src/util/sock.h
@@ -64,7 +64,7 @@ public:
* Get the value of the contained socket.
* @return socket or INVALID_SOCKET if empty
*/
- virtual SOCKET Get() const;
+ [[nodiscard]] virtual SOCKET Get() const;
/**
* Get the value of the contained socket and drop ownership. It will not be closed by the
@@ -82,26 +82,29 @@ public:
* send(2) wrapper. Equivalent to `send(this->Get(), data, len, flags);`. Code that uses this
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
*/
- virtual ssize_t Send(const void* data, size_t len, int flags) const;
+ [[nodiscard]] virtual ssize_t Send(const void* data, size_t len, int flags) const;
/**
* recv(2) wrapper. Equivalent to `recv(this->Get(), buf, len, flags);`. Code that uses this
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
*/
- virtual ssize_t Recv(void* buf, size_t len, int flags) const;
+ [[nodiscard]] virtual ssize_t Recv(void* buf, size_t len, int flags) const;
/**
* connect(2) wrapper. Equivalent to `connect(this->Get(), addr, addrlen)`. Code that uses this
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
*/
- virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
+ [[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
/**
* getsockopt(2) wrapper. Equivalent to
* `getsockopt(this->Get(), level, opt_name, opt_val, opt_len)`. Code that uses this
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
*/
- virtual int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const;
+ [[nodiscard]] virtual int GetSockOpt(int level,
+ int opt_name,
+ void* opt_val,
+ socklen_t* opt_len) const;
using Event = uint8_t;
@@ -124,9 +127,9 @@ public:
* value of `true` and `occurred` being set to 0.
* @return true on success and false otherwise
*/
- virtual bool Wait(std::chrono::milliseconds timeout,
- Event requested,
- Event* occurred = nullptr) const;
+ [[nodiscard]] virtual bool Wait(std::chrono::milliseconds timeout,
+ Event requested,
+ Event* occurred = nullptr) const;
/* Higher level, convenience, methods. These may throw. */
@@ -154,17 +157,17 @@ public:
* @throws std::runtime_error if the operation cannot be completed. In this case some bytes may
* have been consumed from the socket.
*/
- virtual std::string RecvUntilTerminator(uint8_t terminator,
- std::chrono::milliseconds timeout,
- CThreadInterrupt& interrupt,
- size_t max_data) const;
+ [[nodiscard]] virtual std::string RecvUntilTerminator(uint8_t terminator,
+ std::chrono::milliseconds timeout,
+ CThreadInterrupt& interrupt,
+ size_t max_data) const;
/**
* Check if still connected.
* @param[out] errmsg The error string, if the socket has been disconnected.
* @return true if connected
*/
- virtual bool IsConnected(std::string& errmsg) const;
+ [[nodiscard]] virtual bool IsConnected(std::string& errmsg) const;
protected:
/**
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp
index 4734de3e0b..f514613f0d 100644
--- a/src/util/strencodings.cpp
+++ b/src/util/strencodings.cpp
@@ -593,13 +593,14 @@ std::string Capitalize(std::string str)
std::string HexStr(const Span<const uint8_t> s)
{
- std::string rv;
+ std::string rv(s.size() * 2, '\0');
static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
- rv.reserve(s.size() * 2);
- for (uint8_t v: s) {
- rv.push_back(hexmap[v >> 4]);
- rv.push_back(hexmap[v & 15]);
+ auto it = rv.begin();
+ for (uint8_t v : s) {
+ *it++ = hexmap[v >> 4];
+ *it++ = hexmap[v & 15];
}
+ assert(it == rv.end());
return rv;
}
diff --git a/src/util/system.h b/src/util/system.h
index 61f862c93a..f68975ffa3 100644
--- a/src/util/system.h
+++ b/src/util/system.h
@@ -22,7 +22,6 @@
#include <sync.h>
#include <tinyformat.h>
#include <util/settings.h>
-#include <util/threadnames.h>
#include <util/time.h>
#include <any>
@@ -478,28 +477,6 @@ std::string HelpMessageOpt(const std::string& option, const std::string& message
*/
int GetNumCores();
-/**
- * .. and a wrapper that just calls func once
- */
-template <typename Callable> void TraceThread(const char* name, Callable func)
-{
- util::ThreadRename(name);
- try
- {
- LogPrintf("%s thread start\n", name);
- func();
- LogPrintf("%s thread exit\n", name);
- }
- catch (const std::exception& e) {
- PrintExceptionContinue(&e, name);
- throw;
- }
- catch (...) {
- PrintExceptionContinue(nullptr, name);
- throw;
- }
-}
-
std::string CopyrightHolders(const std::string& strPrefix);
/**
diff --git a/src/util/thread.cpp b/src/util/thread.cpp
new file mode 100644
index 0000000000..14be668685
--- /dev/null
+++ b/src/util/thread.cpp
@@ -0,0 +1,27 @@
+// Copyright (c) 2021 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include <util/thread.h>
+
+#include <logging.h>
+#include <util/system.h>
+#include <util/threadnames.h>
+
+#include <exception>
+
+void util::TraceThread(const char* thread_name, std::function<void()> thread_func)
+{
+ util::ThreadRename(thread_name);
+ try {
+ LogPrintf("%s thread start\n", thread_name);
+ thread_func();
+ LogPrintf("%s thread exit\n", thread_name);
+ } catch (const std::exception& e) {
+ PrintExceptionContinue(&e, thread_name);
+ throw;
+ } catch (...) {
+ PrintExceptionContinue(nullptr, thread_name);
+ throw;
+ }
+}
diff --git a/src/util/thread.h b/src/util/thread.h
new file mode 100644
index 0000000000..ca2eccc0c3
--- /dev/null
+++ b/src/util/thread.h
@@ -0,0 +1,18 @@
+// Copyright (c) 2021 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_UTIL_THREAD_H
+#define BITCOIN_UTIL_THREAD_H
+
+#include <functional>
+
+namespace util {
+/**
+ * A wrapper for do-something-once thread functions.
+ */
+void TraceThread(const char* thread_name, std::function<void()> thread_func);
+
+} // namespace util
+
+#endif // BITCOIN_UTIL_THREAD_H