Skip to content

Commit 44ddcd8

Browse files
committed
Merge #19706: refactor: make EncodeBase58{Check} consume Spans
356988e util: make EncodeBase58Check consume Spans (Sebastian Falbesoner) f0fce06 util: make EncodeBase58 consume Spans (Sebastian Falbesoner) Pull request description: This PR improves the interfaces for the functions `EncodeBase58{Check}` by using Spans, in a similar fashion to e.g. PRs #19660, #19687. Note that on the master branch there are currently two versions of `EncodeBase58`: one that takes two pointers (marking begin and end) and another one that takes a `std::vector<unsigned char>` const-ref. The PR branch only leaves one generic Span-interface, both simplifying the interface and allowing more generic containers to be passed. The same is done for `EncodeBase58Check`, where only one interface existed but it's more generic now (e.g. a std::array can be directly passed, as done in the benchmarks). ACKs for top commit: laanwj: Code review ACK 356988e Tree-SHA512: 47cfccdd7f3a2d4694bb8785e6e5fd756daee04ce1652ee59a7822e7e833b4a441ae9362b9bd67ea020d2b5b7d927629c9addb6abaa9881d8564fd3b1257f512
2 parents c6532fa + 356988e commit 44ddcd8

File tree

5 files changed

+18
-30
lines changed

5 files changed

+18
-30
lines changed

src/base58.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,21 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch, int max_ret_
8484
return true;
8585
}
8686

87-
std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
87+
std::string EncodeBase58(Span<const unsigned char> input)
8888
{
8989
// Skip & count leading zeroes.
9090
int zeroes = 0;
9191
int length = 0;
92-
while (pbegin != pend && *pbegin == 0) {
93-
pbegin++;
92+
while (input.size() > 0 && input[0] == 0) {
93+
input = input.subspan(1);
9494
zeroes++;
9595
}
9696
// Allocate enough space in big-endian base58 representation.
97-
int size = (pend - pbegin) * 138 / 100 + 1; // log(256) / log(58), rounded up.
97+
int size = input.size() * 138 / 100 + 1; // log(256) / log(58), rounded up.
9898
std::vector<unsigned char> b58(size);
9999
// Process the bytes.
100-
while (pbegin != pend) {
101-
int carry = *pbegin;
100+
while (input.size() > 0) {
101+
int carry = input[0];
102102
int i = 0;
103103
// Apply "b58 = b58 * 256 + ch".
104104
for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
@@ -109,7 +109,7 @@ std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
109109

110110
assert(carry == 0);
111111
length = i;
112-
pbegin++;
112+
input = input.subspan(1);
113113
}
114114
// Skip leading zeroes in base58 result.
115115
std::vector<unsigned char>::iterator it = b58.begin() + (size - length);
@@ -124,11 +124,6 @@ std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
124124
return str;
125125
}
126126

127-
std::string EncodeBase58(const std::vector<unsigned char>& vch)
128-
{
129-
return EncodeBase58(vch.data(), vch.data() + vch.size());
130-
}
131-
132127
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len)
133128
{
134129
if (!ValidAsCString(str)) {
@@ -137,10 +132,10 @@ bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, in
137132
return DecodeBase58(str.c_str(), vchRet, max_ret_len);
138133
}
139134

140-
std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
135+
std::string EncodeBase58Check(Span<const unsigned char> input)
141136
{
142137
// add 4-byte hash check to the end
143-
std::vector<unsigned char> vch(vchIn);
138+
std::vector<unsigned char> vch(input.begin(), input.end());
144139
uint256 hash = Hash(vch);
145140
vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
146141
return EncodeBase58(vch);

src/base58.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,15 @@
1515
#define BITCOIN_BASE58_H
1616

1717
#include <attributes.h>
18+
#include <span.h>
1819

1920
#include <string>
2021
#include <vector>
2122

2223
/**
23-
* Encode a byte sequence as a base58-encoded string.
24-
* pbegin and pend cannot be nullptr, unless both are.
24+
* Encode a byte span as a base58-encoded string
2525
*/
26-
std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
27-
28-
/**
29-
* Encode a byte vector as a base58-encoded string
30-
*/
31-
std::string EncodeBase58(const std::vector<unsigned char>& vch);
26+
std::string EncodeBase58(Span<const unsigned char> input);
3227

3328
/**
3429
* Decode a base58-encoded string (psz) into a byte vector (vchRet).
@@ -44,9 +39,9 @@ NODISCARD bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet,
4439
NODISCARD bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len);
4540

4641
/**
47-
* Encode a byte vector into a base58-encoded string, including checksum
42+
* Encode a byte span into a base58-encoded string, including checksum
4843
*/
49-
std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
44+
std::string EncodeBase58Check(Span<const unsigned char> input);
5045

5146
/**
5247
* Decode a base58-encoded string (psz) that includes a checksum into a byte

src/bench/base58.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static void Base58Encode(benchmark::Bench& bench)
2020
}
2121
};
2222
bench.batch(buff.size()).unit("byte").run([&] {
23-
EncodeBase58(buff.data(), buff.data() + buff.size());
23+
EncodeBase58(buff);
2424
});
2525
}
2626

@@ -34,10 +34,8 @@ static void Base58CheckEncode(benchmark::Bench& bench)
3434
200, 24
3535
}
3636
};
37-
std::vector<unsigned char> vch;
38-
vch.assign(buff.begin(), buff.end());
3937
bench.batch(buff.size()).unit("byte").run([&] {
40-
EncodeBase58Check(vch);
38+
EncodeBase58Check(buff);
4139
});
4240
}
4341

src/qt/guiutil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static std::string DummyAddress(const CChainParams &params)
9494
std::vector<unsigned char> sourcedata = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
9595
sourcedata.insert(sourcedata.end(), dummydata, dummydata + sizeof(dummydata));
9696
for(int i=0; i<256; ++i) { // Try every trailing byte
97-
std::string s = EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size());
97+
std::string s = EncodeBase58(sourcedata);
9898
if (!IsValidDestinationString(s)) {
9999
return s;
100100
}

src/test/base58_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
3333
std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
3434
std::string base58string = test[1].get_str();
3535
BOOST_CHECK_MESSAGE(
36-
EncodeBase58(sourcedata.data(), sourcedata.data() + sourcedata.size()) == base58string,
36+
EncodeBase58(sourcedata) == base58string,
3737
strTest);
3838
}
3939
}

0 commit comments

Comments
 (0)