Skip to content

Commit 99d981f

Browse files
committed
Merge branch 'sys_leveldb' into rm_minisketch-28+k
2 parents 1248d0d + 87e5c2d commit 99d981f

File tree

8 files changed

+154
-8
lines changed

8 files changed

+154
-8
lines changed

build-aux/m4/bitcoin_subdir_to_include.m4

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,48 @@ dnl file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
dnl BITCOIN_SUBDIR_TO_INCLUDE([CPPFLAGS-VARIABLE-NAME],[SUBDIRECTORY-NAME],[HEADER-FILE])
66
dnl SUBDIRECTORY-NAME must end with a path separator
77
AC_DEFUN([BITCOIN_SUBDIR_TO_INCLUDE],[
8-
if test "$2" = ""; then
8+
m4_pushdef([_result_var],[$1])
9+
m4_pushdef([_rel_path],[$2])
10+
m4_pushdef([_header_file],[$3.h])
11+
if test "[]_rel_path" = ""; then
912
AC_MSG_RESULT([default])
1013
else
11-
echo "#include <$2$3.h>" >conftest.cpp
12-
newinclpath=`${CXXCPP} ${CPPFLAGS} -M conftest.cpp 2>/dev/null | [ tr -d '\\n\\r\\\\' | sed -e 's/^.*[[:space:]:]\(\/[^[:space:]]*\)]$3[\.h[[:space:]].*$/\1/' -e t -e d`]
14+
echo '[#]include <'"_rel_path"'/_header_file>' >conftest.cpp
15+
newinclpath=$(
16+
${CXXCPP} ${CPPFLAGS} -M conftest.cpp 2>/dev/null |
17+
${SED} -E m4_bpatsubsts([[
18+
:build_line
19+
# If the line doesn't end with a backslash, it is complete; go on to process it
20+
/\\$/!b have_complete_line
21+
# Otherwise, read the next line, and concatenate it to the current one with a space
22+
N
23+
s/\\\n/ /
24+
# Then go back and check for a trailing backslash again.
25+
t build_line
26+
27+
# When we get here, we have the completed line, with all continuations collapsed.
28+
:have_complete_line
29+
s/^[^:]*:[[:space:]]*(([^[:space:]\]|\\.)*[[:space:]])*(([^[:space:]\]|\\.)*)(\\|\\\\|\/)?]]patsubst(]_header_file[,[\.],[\\.])[[([[:space:]].*)?$/\3/
30+
# ^^^^^^^ The Make line begins with a target (which we don't care about)
31+
# ^^^^^^^^^^^^ Ignore any spaces following it
32+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Match any number of other dependencies
33+
# ^^^^^^^^^^^^^^^^^^^^^^ Match any path components for our dependency; note this is reference 3, which we are replacing with
34+
# ^^^^^^^^^^^^^ Accept the path ending in a backslash, a double-backslash (ie escaped), or a forward slash
35+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The filename must match exactly (periods are escaped, since a normal period matches any character in regex)
36+
# ^^^^^^^^^^^^^^^^^ Filename must be followed by a space, but after that we don't care; we still need to match it all so it gets replaced, however
37+
# Delete the line, but only if we failed to find the directory (t jumps past the d if we matched)
38+
t
39+
d
40+
]],[
41+
\s*\(#.*\)?$],[],[
42+
\(.*\)],[ -e '\1'])
43+
dnl ^^^^^^^^^^^^^^^^^^^^^^^ Deletes comments and processes sed expressions into -e arguments
44+
)
45+
1346
AC_MSG_RESULT([${newinclpath}])
1447
if test "${newinclpath}" != ""; then
15-
eval "$1=\"\$$1\"' -I${newinclpath}'"
48+
eval "_result_var=\"\$_result_var\"' -I${newinclpath}'"
1649
fi
1750
fi
51+
m4_popdef([_result_var],[_rel_path],[_header_file])
1852
])

configure.ac

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,68 @@ if test "$have_any_system" != "no"; then
10751075
AC_DEFINE([HAVE_SYSTEM], [1], [Define to 1 if std::system or ::wsystem is available.])
10761076
fi
10771077

1078+
dnl Check for leveldb, only if explicitly requested
1079+
LEVELDB_CPPFLAGS=
1080+
LIBLEVELDB=
1081+
LIBMEMENV=
1082+
AC_ARG_WITH([system-leveldb],
1083+
[AS_HELP_STRING([--with-system-leveldb],
1084+
[Build with system LevelDB (default is no; DANGEROUS; NOT SUPPORTED)])],
1085+
[system_leveldb=$withval],
1086+
[system_leveldb=no]
1087+
)
1088+
if test x$system_leveldb != xno; then
1089+
LEVELDB_CPPFLAGS=
1090+
AC_CHECK_LIB([leveldb],[main],[
1091+
LIBLEVELDB=-lleveldb
1092+
],[
1093+
AC_MSG_ERROR([leveldb library not found; using --with-system-leveldb is not supported anyway])
1094+
])
1095+
AC_CHECK_HEADER([leveldb/filter_policy.h],[],[
1096+
AC_MSG_ERROR([LevelDB headers not found; using --with-system-leveldb is not supported anyway])
1097+
])
1098+
AC_CHECK_HEADER([leveldb/helpers/memenv.h],[
1099+
AC_MSG_CHECKING([for memenv.h path])
1100+
BITCOIN_SUBDIR_TO_INCLUDE([LEVELDB_CPPFLAGS],[leveldb/helpers/],[memenv])
1101+
],[
1102+
AC_CHECK_HEADER([memenv.h],[],[
1103+
AC_MSG_ERROR([LevelDB headers not found; using --with-system-leveldb is not supported anyway])
1104+
])
1105+
])
1106+
1107+
AC_MSG_CHECKING([library containing leveldb::NewMemEnv])
1108+
TEMP_LIBS="$LIBS"
1109+
TEMP_CPPFLAGS="$CPPFLAGS"
1110+
CPPFLAGS="$CPPFLAGS $LEVELDB_CPPFLAGS"
1111+
for searchlib in "" "-lmemenv" ERR; do
1112+
if test "x$searchlib" = "xERR"; then
1113+
AC_MSG_RESULT([no])
1114+
AC_MSG_ERROR([LevelDB's memenv helper not found; using --with-system-leveldb is not supported anyway])
1115+
fi
1116+
searchlib="$searchlib $LIBLEVELDB"
1117+
LIBS="$searchlib $TEMP_LIBS"
1118+
AC_LINK_IFELSE([AC_LANG_SOURCE([
1119+
#include <leveldb/env.h>
1120+
#include <memenv.h>
1121+
1122+
int main() {
1123+
leveldb::Env *myenv = leveldb::NewMemEnv(leveldb::Env::Default());
1124+
delete myenv;
1125+
}
1126+
])],[
1127+
AC_MSG_RESULT([$searchlib])
1128+
LIBMEMENV="$searchlib"
1129+
break
1130+
])
1131+
done
1132+
LIBS="$TEMP_LIBS"
1133+
CPPFLAGS="$TEMP_CPPFLAGS"
1134+
fi
1135+
AM_CONDITIONAL([EMBEDDED_LEVELDB],[test x$system_leveldb = xno])
1136+
AC_SUBST(LEVELDB_CPPFLAGS)
1137+
AC_SUBST(LIBLEVELDB)
1138+
AC_SUBST(LIBMEMENV)
1139+
10781140
dnl SUPPRESSED_CPPFLAGS=SUPPRESS_WARNINGS([$SOME_CPPFLAGS])
10791141
dnl Replace -I with -isystem in $SOME_CPPFLAGS to suppress warnings from
10801142
dnl headers from its include directories and return the result.

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,8 +1110,10 @@ endif
11101110

11111111
include Makefile.minisketch.include
11121112

1113+
if EMBEDDED_LEVELDB
11131114
include Makefile.crc32c.include
11141115
include Makefile.leveldb.include
1116+
endif
11151117

11161118
include Makefile.test_util.include
11171119
include Makefile.test_fuzz.include

src/Makefile.leveldb.include

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ LIBMEMENV_INT = leveldb/libmemenv.la
88
noinst_LTLIBRARIES += $(LIBLEVELDB_INT)
99
noinst_LTLIBRARIES += $(LIBMEMENV_INT)
1010

11-
LIBLEVELDB = $(LIBLEVELDB_INT) $(LIBCRC32C)
12-
LIBMEMENV = $(LIBMEMENV_INT)
11+
LIBLEVELDB += $(LIBLEVELDB_INT) $(LIBCRC32C)
12+
LIBMEMENV += $(LIBMEMENV_INT)
1313

14-
LEVELDB_CPPFLAGS =
1514
LEVELDB_CPPFLAGS += -I$(srcdir)/leveldb/include
15+
LEVELDB_CPPFLAGS += -I$(srcdir)/leveldb/helpers/memenv
1616

1717
LEVELDB_CPPFLAGS_INT =
1818
LEVELDB_CPPFLAGS_INT += -I$(srcdir)/leveldb

src/dbwrapper.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@
66

77
#include <logging.h>
88
#include <random.h>
9+
#include <node/interface_ui.h>
910
#include <serialize.h>
1011
#include <span.h>
1112
#include <streams.h>
1213
#include <util/fs.h>
1314
#include <util/fs_helpers.h>
1415
#include <util/strencodings.h>
16+
#include <util/translation.h>
1517

1618
#include <algorithm>
1719
#include <cassert>
1820
#include <cstdarg>
1921
#include <cstdint>
2022
#include <cstdio>
23+
#include <leveldb/c.h>
2124
#include <leveldb/cache.h>
2225
#include <leveldb/db.h>
2326
#include <leveldb/env.h>
2427
#include <leveldb/filter_policy.h>
25-
#include <leveldb/helpers/memenv/memenv.h>
28+
#include <memenv.h>
2629
#include <leveldb/iterator.h>
2730
#include <leveldb/options.h>
2831
#include <leveldb/slice.h>
@@ -51,6 +54,42 @@ static void HandleError(const leveldb::Status& status)
5154
throw dbwrapper_error(errmsg);
5255
}
5356

57+
bool dbwrapper_SanityCheck()
58+
{
59+
unsigned long header_version = (leveldb::kMajorVersion << 16) | leveldb::kMinorVersion;
60+
unsigned long library_version = (leveldb_major_version() << 16) | leveldb_minor_version();
61+
62+
if (header_version != library_version) {
63+
InitError(Untranslated(strprintf("Compiled with LevelDB %d.%d, but linked with LevelDB %d.%d (incompatible).",
64+
leveldb::kMajorVersion, leveldb::kMinorVersion,
65+
leveldb_major_version(), leveldb_minor_version()
66+
)));
67+
return false;
68+
}
69+
70+
return true;
71+
}
72+
73+
#ifndef WIN32
74+
namespace leveldb {
75+
class EnvPosixTestHelper {
76+
static void SetReadOnlyMMapLimit(int limit);
77+
public:
78+
static inline void SetReadOnlyMMapLimitForBitcoin(int limit) { SetReadOnlyMMapLimit(limit); }
79+
};
80+
}
81+
82+
class BitcoinLevelDBInit {
83+
public:
84+
BitcoinLevelDBInit() {
85+
if (sizeof(void*) >= 8) {
86+
leveldb::EnvPosixTestHelper::SetReadOnlyMMapLimitForBitcoin(4096);
87+
}
88+
}
89+
};
90+
static BitcoinLevelDBInit g_bitcoin_leveldb_init;
91+
#endif
92+
5493
class CBitcoinLevelDBLogger : public leveldb::Logger {
5594
public:
5695
// This code is adapted from posix_logger.h, which is why it is using vsprintf.

src/dbwrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <string>
2121
#include <vector>
2222

23+
bool dbwrapper_SanityCheck();
24+
2325
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
2426
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
2527

src/kernel/checks.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <kernel/checks.h>
66

7+
#include <dbwrapper.h>
78
#include <random.h>
89
#include <util/result.h>
910
#include <util/translation.h>
@@ -14,6 +15,10 @@ namespace kernel {
1415

1516
util::Result<void> SanityChecks(const Context&)
1617
{
18+
if (!dbwrapper_SanityCheck()) {
19+
return util::Error{Untranslated("Database sanity check failure. Aborting.")};
20+
}
21+
1722
if (!Random_SanityCheck()) {
1823
return util::Error{Untranslated("OS cryptographic RNG sanity check failure. Aborting.")};
1924
}

src/test/sanity_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <dbwrapper.h>
56
#include <key.h>
67
#include <test/util/setup_common.h>
78

@@ -11,6 +12,7 @@ BOOST_FIXTURE_TEST_SUITE(sanity_tests, BasicTestingSetup)
1112

1213
BOOST_AUTO_TEST_CASE(basic_sanity)
1314
{
15+
BOOST_CHECK_MESSAGE(dbwrapper_SanityCheck() == true, "dbwrapper sanity test");
1416
BOOST_CHECK_MESSAGE(ECC_InitSanityCheck() == true, "secp256k1 sanity test");
1517
}
1618

0 commit comments

Comments
 (0)