Skip to content

Commit 26aab22

Browse files
Merge #7123: backport: Merge bitcoin#28903, 28924, 29056, 28455, 29037
dc72085 Merge bitcoin#29037: Add multiplication operator to CFeeRate (Ava Chow) 32f945e Merge bitcoin#28455: refactor: share and use `GenerateRandomKey` helper (Ava Chow) 7ab8da3 Merge bitcoin#29056: refactor: Print verbose serialize compiler error messages (Ava Chow) 57f8b72 Merge bitcoin#28924: refactor: Remove unused and fragile string interface from arith_uint256 (fanquake) 4afafe2 Merge bitcoin#28903: refactor: Make CTxMemPoolEntry only explicitly copyable (Andrew Chow) Pull request description: bitcoin backports ACKs for top commit: UdjinM6: utACK dc72085 knst: utACK dc72085 Tree-SHA512: ea6726887b269d09ed7b8d024374c3a17111ad5d17557d55ca1ef55d5d60e3ad7e931d965a6ebc742e74c55d6e2695e37a1c5933bdd284f57d529e2651a78c00
2 parents c706b36 + dc72085 commit 26aab22

27 files changed

Lines changed: 166 additions & 141 deletions

src/.clang-format

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,7 @@ SpacesInAngles: false
5252
SpacesInContainerLiterals: true
5353
SpacesInCStyleCastParentheses: false
5454
SpacesInParentheses: false
55+
BreakBeforeConceptDeclarations: Always
56+
RequiresExpressionIndentation: OuterScope
5557
Standard: c++20
5658
UseTab: Never

src/arith_uint256.cpp

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@
88
#include <uint256.h>
99
#include <crypto/common.h>
1010

11-
12-
template <unsigned int BITS>
13-
base_uint<BITS>::base_uint(const std::string& str)
14-
{
15-
static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
16-
17-
SetHex(str);
18-
}
11+
#include <cassert>
1912

2013
template <unsigned int BITS>
2114
base_uint<BITS>& base_uint<BITS>::operator<<=(unsigned int shift)
@@ -153,22 +146,6 @@ std::string base_uint<BITS>::GetHex() const
153146
return b.GetHex();
154147
}
155148

156-
template <unsigned int BITS>
157-
void base_uint<BITS>::SetHex(const char* psz)
158-
{
159-
base_blob<BITS> b;
160-
b.SetHex(psz);
161-
for (int x = 0; x < this->WIDTH; ++x) {
162-
this->pn[x] = ReadLE32(b.begin() + x*4);
163-
}
164-
}
165-
166-
template <unsigned int BITS>
167-
void base_uint<BITS>::SetHex(const std::string& str)
168-
{
169-
SetHex(str.c_str());
170-
}
171-
172149
template <unsigned int BITS>
173150
std::string base_uint<BITS>::ToString() const
174151
{

src/arith_uint256.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#ifndef BITCOIN_ARITH_UINT256_H
77
#define BITCOIN_ARITH_UINT256_H
88

9+
#include <cstdint>
910
#include <cstring>
1011
#include <limits>
1112
#include <stdexcept>
12-
#include <stdint.h>
1313
#include <string>
1414

1515
class uint256;
@@ -56,8 +56,6 @@ class base_uint
5656
pn[i] = 0;
5757
}
5858

59-
explicit base_uint(const std::string& str);
60-
6159
base_uint operator~() const
6260
{
6361
base_uint ret;
@@ -219,8 +217,6 @@ class base_uint
219217
friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
220218

221219
std::string GetHex() const;
222-
void SetHex(const char* psz);
223-
void SetHex(const std::string& str);
224220
std::string ToString() const;
225221

226222
unsigned int size() const
@@ -247,7 +243,6 @@ class arith_uint256 : public base_uint<256> {
247243
arith_uint256() {}
248244
arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
249245
arith_uint256(uint64_t b) : base_uint<256>(b) {}
250-
explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
251246

252247
/**
253248
* The "compact" format is a representation of a whole

src/bench/ellswift.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ static void EllSwiftCreate(benchmark::Bench& bench)
1111
{
1212
ECC_Start();
1313

14-
CKey key;
15-
key.MakeNewKey(true);
16-
14+
CKey key = GenerateRandomKey();
1715
uint256 entropy = GetRandHash();
1816

1917
bench.batch(1).unit("pubkey").run([&] {

src/key.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,13 @@ ECDHSecret CKey::ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift, c
340340
return output;
341341
}
342342

343+
CKey GenerateRandomKey(bool compressed) noexcept
344+
{
345+
CKey key;
346+
key.MakeNewKey(/*fCompressed=*/compressed);
347+
return key;
348+
}
349+
343350
bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
344351
if (nDepth == std::numeric_limits<unsigned char>::max()) return false;
345352
out.nDepth = nDepth + 1;
@@ -391,8 +398,7 @@ void CExtKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
391398
}
392399

393400
bool ECC_InitSanityCheck() {
394-
CKey key;
395-
key.MakeNewKey(true);
401+
CKey key = GenerateRandomKey();
396402
CPubKey pubkey = key.GetPubKey();
397403
return key.VerifyPubKey(pubkey);
398404
}

src/key.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class CKey
168168
bool initiating) const;
169169
};
170170

171+
CKey GenerateRandomKey(bool compressed = true) noexcept;
172+
171173
struct CExtKey {
172174
unsigned char nDepth;
173175
unsigned char vchFingerprint[4];

src/net.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <node/eviction.h>
2222
#include <fs.h>
2323
#include <i2p.h>
24+
#include <key.h>
2425
#include <memusage.h>
2526
#include <net_permissions.h>
2627
#include <netaddress.h>
@@ -1057,13 +1058,6 @@ class V2MessageMap
10571058

10581059
const V2MessageMap V2_MESSAGE_MAP;
10591060

1060-
CKey GenerateRandomKey() noexcept
1061-
{
1062-
CKey key;
1063-
key.MakeNewKey(/*fCompressed=*/true);
1064-
return key;
1065-
}
1066-
10671061
std::vector<uint8_t> GenerateRandomGarbage() noexcept
10681062
{
10691063
std::vector<uint8_t> ret;

src/policy/feerate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class CFeeRate
7070
friend bool operator!=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK != b.nSatoshisPerK; }
7171
CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; }
7272
std::string ToString(const FeeEstimateMode& fee_estimate_mode = FeeEstimateMode::DASH_KB) const;
73+
friend CFeeRate operator*(const CFeeRate& f, int a) { return CFeeRate(a * f.nSatoshisPerK); }
74+
friend CFeeRate operator*(int a, const CFeeRate& f) { return CFeeRate(a * f.nSatoshisPerK); }
7375

7476
SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.nSatoshisPerK); }
7577
};

src/qt/test/addressbooktests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
8181
}
8282

8383
auto build_address = [wallet]() {
84-
CKey key;
85-
key.MakeNewKey(true);
84+
CKey key = GenerateRandomKey();
8685
CTxDestination dest = PKHash(key.GetPubKey());
8786

8887
return std::make_pair(dest, QString::fromStdString(EncodeDestination(dest)));

src/serialize.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_wri
213213
template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
214214
template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
215215
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
216-
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(MakeByteSpan(a)); }
217-
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(MakeByteSpan(a)); }
218-
template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.write(AsBytes(span)); }
216+
template <typename Stream, BasicByte B, int N> void Serialize(Stream& s, const B (&a)[N]) { s.write(MakeByteSpan(a)); }
217+
template <typename Stream, BasicByte B, std::size_t N> void Serialize(Stream& s, const std::array<B, N>& a) { s.write(MakeByteSpan(a)); }
218+
template <typename Stream, BasicByte B> void Serialize(Stream& s, Span<B> span) { s.write(AsBytes(span)); }
219219

220220
template <typename Stream, CharNotInt8 V> void Unserialize(Stream&, V) = delete; // char serialization forbidden. Use uint8_t or int8_t
221221
template <typename Stream> void Unserialize(Stream& s, std::byte& a) { a = std::byte{ser_readdata8(s)}; }
@@ -227,9 +227,9 @@ template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a =
227227
template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
228228
template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
229229
template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
230-
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
231-
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
232-
template <typename Stream, typename B> void Unserialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.read(AsWritableBytes(span)); }
230+
template <typename Stream, BasicByte B, int N> void Unserialize(Stream& s, B (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
231+
template <typename Stream, BasicByte B, std::size_t N> void Unserialize(Stream& s, std::array<B, N>& a) { s.read(MakeWritableByteSpan(a)); }
232+
template <typename Stream, BasicByte B> void Unserialize(Stream& s, Span<B> span) { s.read(AsWritableBytes(span)); }
233233

234234
template <typename Stream> inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); }
235235
template <typename Stream> inline void Unserialize(Stream& s, bool& a) { uint8_t f = ser_readdata8(s); a = f; }
@@ -886,18 +886,23 @@ template<typename Stream, typename T> void Serialize(Stream& os, const std::atom
886886
template<typename Stream, typename T> void Unserialize(Stream& is, std::atomic<T>& a);
887887

888888

889-
890889
/**
891890
* If none of the specialized versions above matched and T is a class, default to calling member function.
892891
*/
893-
template<typename Stream, typename T, typename std::enable_if<std::is_class<T>::value>::type* = nullptr>
894-
inline void Serialize(Stream& os, const T& a)
892+
template <class T, class Stream, typename std::enable_if<std::is_class<T>::value>::type* = nullptr >
893+
concept Serializable = requires(T a, Stream s) { a.Serialize(s); };
894+
template <typename Stream, typename T>
895+
requires Serializable<T, Stream>
896+
void Serialize(Stream& os, const T& a)
895897
{
896898
a.Serialize(os);
897899
}
898900

899-
template<typename Stream, typename T, typename std::enable_if<std::is_class<std::remove_reference<T> >::value>::type* = nullptr>
900-
inline void Unserialize(Stream& is, T&& a)
901+
template <class T, class Stream, typename std::enable_if<std::is_class<std::remove_reference<T> >::value>::type* = nullptr>
902+
concept Unserializable = requires(T a, Stream s) { a.Unserialize(s); };
903+
template <typename Stream, typename T>
904+
requires Unserializable<T, Stream>
905+
void Unserialize(Stream& is, T&& a)
901906
{
902907
a.Unserialize(is);
903908
}

0 commit comments

Comments
 (0)