Skip to content

Commit 079cf88

Browse files
committed
refactor: move Boost datetime usage to wallet
This means we don't need datetime in a --disable-wallet build, and it isn't included in the kernel.
1 parent f59e915 commit 079cf88

File tree

9 files changed

+51
-28
lines changed

9 files changed

+51
-28
lines changed

src/Makefile.test.include

+3-2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ BITCOIN_TESTS += \
174174
wallet/test/availablecoins_tests.cpp \
175175
wallet/test/init_tests.cpp \
176176
wallet/test/ismine_tests.cpp \
177+
wallet/test/rpc_util_tests.cpp \
177178
wallet/test/scriptpubkeyman_tests.cpp \
178179
wallet/test/walletload_tests.cpp
179180

@@ -186,7 +187,8 @@ BITCOIN_TESTS += wallet/test/db_tests.cpp
186187
endif
187188

188189
FUZZ_WALLET_SRC = \
189-
wallet/test/fuzz/coinselection.cpp
190+
wallet/test/fuzz/coinselection.cpp \
191+
wallet/test/fuzz/parse_iso8601.cpp
190192

191193
if USE_SQLITE
192194
FUZZ_WALLET_SRC += \
@@ -288,7 +290,6 @@ test_fuzz_fuzz_SOURCES = \
288290
test/fuzz/node_eviction.cpp \
289291
test/fuzz/p2p_transport_serialization.cpp \
290292
test/fuzz/parse_hd_keypath.cpp \
291-
test/fuzz/parse_iso8601.cpp \
292293
test/fuzz/parse_numbers.cpp \
293294
test/fuzz/parse_script.cpp \
294295
test/fuzz/parse_univalue.cpp \

src/test/fuzz/util.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ CAmount ConsumeMoney(FuzzedDataProvider& fuzzed_data_provider, const std::option
307307
int64_t ConsumeTime(FuzzedDataProvider& fuzzed_data_provider, const std::optional<int64_t>& min, const std::optional<int64_t>& max) noexcept
308308
{
309309
// Avoid t=0 (1970-01-01T00:00:00Z) since SetMockTime(0) disables mocktime.
310-
static const int64_t time_min{ParseISO8601DateTime("2000-01-01T00:00:01Z")};
311-
static const int64_t time_max{ParseISO8601DateTime("2100-12-31T23:59:59Z")};
310+
static const int64_t time_min{946684801}; // 2000-01-01T00:00:01Z
311+
static const int64_t time_max{4133980799}; // 2100-12-31T23:59:59Z
312312
return fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(min.value_or(time_min), max.value_or(time_max));
313313
}
314314

src/test/util_tests.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,10 @@ BOOST_AUTO_TEST_CASE(util_TrimString)
282282
BOOST_CHECK_EQUAL(TrimStringView(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01\x00", 6)), "");
283283
}
284284

285-
BOOST_AUTO_TEST_CASE(util_FormatParseISO8601DateTime)
285+
BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
286286
{
287287
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
288288
BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z");
289-
290-
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z"), 0);
291-
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0);
292-
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z"), 1317425777);
293289
}
294290

295291
BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)

src/util/time.cpp

-16
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include <util/time.h>
1313
#include <util/check.h>
1414

15-
#include <boost/date_time/posix_time/posix_time.hpp>
16-
1715
#include <atomic>
1816
#include <chrono>
1917
#include <ctime>
@@ -142,20 +140,6 @@ std::string FormatISO8601Date(int64_t nTime) {
142140
return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
143141
}
144142

145-
int64_t ParseISO8601DateTime(const std::string& str)
146-
{
147-
static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
148-
static const std::locale loc(std::locale::classic(),
149-
new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
150-
std::istringstream iss(str);
151-
iss.imbue(loc);
152-
boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
153-
iss >> ptime;
154-
if (ptime.is_not_a_date_time() || epoch > ptime)
155-
return 0;
156-
return (ptime - epoch).total_seconds();
157-
}
158-
159143
struct timeval MillisToTimeval(int64_t nTimeout)
160144
{
161145
struct timeval timeout;

src/util/time.h

-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ T GetTime()
109109
*/
110110
std::string FormatISO8601DateTime(int64_t nTime);
111111
std::string FormatISO8601Date(int64_t nTime);
112-
int64_t ParseISO8601DateTime(const std::string& str);
113112

114113
/**
115114
* Convert milliseconds to a struct timeval for e.g. select.

src/wallet/rpc/util.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,26 @@
1212

1313
#include <univalue.h>
1414

15+
#include <boost/date_time/posix_time/posix_time.hpp>
16+
1517
namespace wallet {
1618
static const std::string WALLET_ENDPOINT_BASE = "/wallet/";
1719
const std::string HELP_REQUIRING_PASSPHRASE{"\nRequires wallet passphrase to be set with walletpassphrase call if wallet is encrypted.\n"};
1820

21+
int64_t ParseISO8601DateTime(const std::string& str)
22+
{
23+
static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
24+
static const std::locale loc(std::locale::classic(),
25+
new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
26+
std::istringstream iss(str);
27+
iss.imbue(loc);
28+
boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
29+
iss >> ptime;
30+
if (ptime.is_not_a_date_time() || epoch > ptime)
31+
return 0;
32+
return (ptime - epoch).total_seconds();
33+
}
34+
1935
bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param) {
2036
bool can_avoid_reuse = wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
2137
bool avoid_reuse = param.isNull() ? can_avoid_reuse : param.get_bool();

src/wallet/rpc/util.h

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ std::string LabelFromValue(const UniValue& value);
4545
void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey, UniValue& entry);
4646

4747
void HandleWalletError(const std::shared_ptr<CWallet> wallet, DatabaseStatus& status, bilingual_str& error);
48+
49+
int64_t ParseISO8601DateTime(const std::string& str);
4850
} // namespace wallet
4951

5052
#endif // BITCOIN_WALLET_RPC_UTIL_H

src/test/fuzz/parse_iso8601.cpp src/wallet/test/fuzz/parse_iso8601.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <test/fuzz/FuzzedDataProvider.h>
66
#include <test/fuzz/fuzz.h>
77
#include <util/time.h>
8+
#include <wallet/rpc/util.h>
89

910
#include <cassert>
1011
#include <cstdint>
@@ -20,14 +21,14 @@ FUZZ_TARGET(parse_iso8601)
2021

2122
const std::string iso8601_datetime = FormatISO8601DateTime(random_time);
2223
(void)FormatISO8601Date(random_time);
23-
const int64_t parsed_time_1 = ParseISO8601DateTime(iso8601_datetime);
24+
const int64_t parsed_time_1 = wallet::ParseISO8601DateTime(iso8601_datetime);
2425
if (random_time >= 0) {
2526
assert(parsed_time_1 >= 0);
2627
if (iso8601_datetime.length() == 20) {
2728
assert(parsed_time_1 == random_time);
2829
}
2930
}
3031

31-
const int64_t parsed_time_2 = ParseISO8601DateTime(random_string);
32+
const int64_t parsed_time_2 = wallet::ParseISO8601DateTime(random_string);
3233
assert(parsed_time_2 >= 0);
3334
}

src/wallet/test/rpc_util_tests.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <wallet/rpc/util.h>
6+
7+
#include <boost/test/unit_test.hpp>
8+
9+
namespace wallet {
10+
11+
BOOST_AUTO_TEST_SUITE(wallet_util_tests)
12+
13+
BOOST_AUTO_TEST_CASE(util_ParseISO8601DateTime)
14+
{
15+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z"), 0);
16+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0);
17+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:01Z"), 946684801);
18+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z"), 1317425777);
19+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2100-12-31T23:59:59Z"), 4133980799);
20+
}
21+
22+
BOOST_AUTO_TEST_SUITE_END()
23+
24+
} // namespace wallet

0 commit comments

Comments
 (0)