From 8f6d3dadd212bf00310378dcddb615839b9ad692 Mon Sep 17 00:00:00 2001 From: Bill Blanke Date: Sat, 27 Jul 2019 11:20:10 -0700 Subject: [PATCH] pybind and cmake support --- lib/bip158/CMakeLists.txt | 32 + lib/bip158/README.md | 1 + lib/bip158/main.cpp | 42 + lib/bip158/python-bindings/chiabip158.cpp | 29 + lib/bip158/python-bindings/test.py | 13 + lib/bip158/setup.py | 80 ++ lib/bip158/src/PyBIP158.cpp | 53 + lib/bip158/src/PyBIP158.h | 24 + lib/bip158/src/amount.h | 28 + lib/bip158/src/attributes.h | 22 + lib/bip158/src/blockfilter.cpp | 338 ++++++ lib/bip158/src/blockfilter.h | 170 +++ lib/bip158/src/coins.h | 343 ++++++ lib/bip158/src/compat/byteswap.h | 66 + lib/bip158/src/compat/endian.h | 241 ++++ lib/bip158/src/compressor.h | 111 ++ lib/bip158/src/consensus/consensus.h | 32 + lib/bip158/src/core_memusage.h | 71 ++ lib/bip158/src/crypto/chacha20.cpp | 310 +++++ lib/bip158/src/crypto/chacha20.h | 34 + lib/bip158/src/crypto/common.h | 103 ++ lib/bip158/src/crypto/ripemd160.h | 28 + lib/bip158/src/crypto/sha256.cpp | 730 +++++++++++ lib/bip158/src/crypto/sha256.h | 41 + lib/bip158/src/crypto/sha512.cpp | 207 ++++ lib/bip158/src/crypto/sha512.h | 28 + lib/bip158/src/crypto/siphash.cpp | 173 +++ lib/bip158/src/crypto/siphash.h | 47 + lib/bip158/src/fs.cpp | 223 ++++ lib/bip158/src/fs.h | 96 ++ lib/bip158/src/hash.h | 207 ++++ lib/bip158/src/indirectmap.h | 56 + lib/bip158/src/logging.cpp | 307 +++++ lib/bip158/src/logging.h | 166 +++ lib/bip158/src/memusage.h | 170 +++ lib/bip158/src/prevector.h | 528 ++++++++ lib/bip158/src/primitives/block.cpp | 31 + lib/bip158/src/primitives/block.h | 155 +++ lib/bip158/src/primitives/transaction.cpp | 115 ++ lib/bip158/src/primitives/transaction.h | 412 +++++++ lib/bip158/src/random.cpp | 790 ++++++++++++ lib/bip158/src/random.h | 242 ++++ lib/bip158/src/script/script.cpp | 328 +++++ lib/bip158/src/script/script.h | 586 +++++++++ lib/bip158/src/serialize.h | 1005 ++++++++++++++++ lib/bip158/src/span.h | 60 + lib/bip158/src/streams.h | 857 +++++++++++++ lib/bip158/src/support/allocators/secure.h | 62 + .../src/support/allocators/zeroafterfree.h | 48 + lib/bip158/src/support/cleanse.cpp | 35 + lib/bip158/src/support/cleanse.h | 15 + lib/bip158/src/support/lockedpool.cpp | 404 +++++++ lib/bip158/src/support/lockedpool.h | 240 ++++ lib/bip158/src/sync.h | 321 +++++ lib/bip158/src/threadsafety.h | 57 + lib/bip158/src/tinyformat.h | 1069 +++++++++++++++++ lib/bip158/src/uint256.cpp | 78 ++ lib/bip158/src/uint256.h | 148 +++ lib/bip158/src/undo.h | 114 ++ lib/bip158/src/util/bytevectorhash.cpp | 18 + lib/bip158/src/util/bytevectorhash.h | 26 + lib/bip158/src/util/strencodings.cpp | 559 +++++++++ lib/bip158/src/util/strencodings.h | 242 ++++ lib/bip158/src/util/threadnames.cpp | 62 + lib/bip158/src/util/threadnames.h | 21 + lib/bip158/src/util/time.cpp | 113 ++ lib/bip158/src/util/time.h | 44 + lib/bip158/src/version.h | 45 + 68 files changed, 13452 insertions(+) create mode 100644 lib/bip158/CMakeLists.txt create mode 100644 lib/bip158/README.md create mode 100644 lib/bip158/main.cpp create mode 100644 lib/bip158/python-bindings/chiabip158.cpp create mode 100755 lib/bip158/python-bindings/test.py create mode 100644 lib/bip158/setup.py create mode 100644 lib/bip158/src/PyBIP158.cpp create mode 100644 lib/bip158/src/PyBIP158.h create mode 100644 lib/bip158/src/amount.h create mode 100644 lib/bip158/src/attributes.h create mode 100644 lib/bip158/src/blockfilter.cpp create mode 100644 lib/bip158/src/blockfilter.h create mode 100644 lib/bip158/src/coins.h create mode 100644 lib/bip158/src/compat/byteswap.h create mode 100644 lib/bip158/src/compat/endian.h create mode 100644 lib/bip158/src/compressor.h create mode 100644 lib/bip158/src/consensus/consensus.h create mode 100644 lib/bip158/src/core_memusage.h create mode 100644 lib/bip158/src/crypto/chacha20.cpp create mode 100644 lib/bip158/src/crypto/chacha20.h create mode 100644 lib/bip158/src/crypto/common.h create mode 100644 lib/bip158/src/crypto/ripemd160.h create mode 100644 lib/bip158/src/crypto/sha256.cpp create mode 100644 lib/bip158/src/crypto/sha256.h create mode 100644 lib/bip158/src/crypto/sha512.cpp create mode 100644 lib/bip158/src/crypto/sha512.h create mode 100644 lib/bip158/src/crypto/siphash.cpp create mode 100644 lib/bip158/src/crypto/siphash.h create mode 100644 lib/bip158/src/fs.cpp create mode 100644 lib/bip158/src/fs.h create mode 100644 lib/bip158/src/hash.h create mode 100644 lib/bip158/src/indirectmap.h create mode 100644 lib/bip158/src/logging.cpp create mode 100644 lib/bip158/src/logging.h create mode 100644 lib/bip158/src/memusage.h create mode 100644 lib/bip158/src/prevector.h create mode 100644 lib/bip158/src/primitives/block.cpp create mode 100644 lib/bip158/src/primitives/block.h create mode 100644 lib/bip158/src/primitives/transaction.cpp create mode 100644 lib/bip158/src/primitives/transaction.h create mode 100644 lib/bip158/src/random.cpp create mode 100644 lib/bip158/src/random.h create mode 100644 lib/bip158/src/script/script.cpp create mode 100644 lib/bip158/src/script/script.h create mode 100644 lib/bip158/src/serialize.h create mode 100644 lib/bip158/src/span.h create mode 100644 lib/bip158/src/streams.h create mode 100644 lib/bip158/src/support/allocators/secure.h create mode 100644 lib/bip158/src/support/allocators/zeroafterfree.h create mode 100644 lib/bip158/src/support/cleanse.cpp create mode 100644 lib/bip158/src/support/cleanse.h create mode 100644 lib/bip158/src/support/lockedpool.cpp create mode 100644 lib/bip158/src/support/lockedpool.h create mode 100644 lib/bip158/src/sync.h create mode 100644 lib/bip158/src/threadsafety.h create mode 100644 lib/bip158/src/tinyformat.h create mode 100644 lib/bip158/src/uint256.cpp create mode 100644 lib/bip158/src/uint256.h create mode 100644 lib/bip158/src/undo.h create mode 100644 lib/bip158/src/util/bytevectorhash.cpp create mode 100644 lib/bip158/src/util/bytevectorhash.h create mode 100644 lib/bip158/src/util/strencodings.cpp create mode 100644 lib/bip158/src/util/strencodings.h create mode 100644 lib/bip158/src/util/threadnames.cpp create mode 100644 lib/bip158/src/util/threadnames.h create mode 100644 lib/bip158/src/util/time.cpp create mode 100644 lib/bip158/src/util/time.h create mode 100644 lib/bip158/src/version.h diff --git a/lib/bip158/CMakeLists.txt b/lib/bip158/CMakeLists.txt new file mode 100644 index 0000000000000..ec0904871ad02 --- /dev/null +++ b/lib/bip158/CMakeLists.txt @@ -0,0 +1,32 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.1.0 FATAL_ERROR) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +IF(NOT CMAKE_BUILD_TYPE) + SET(CMAKE_BUILD_TYPE "RELEASE") +ENDIF() + +project(chiabip158) + +include_directories( + ${INCLUDE_DIRECTORIES} + ${CMAKE_CURRENT_SOURCE_DIR}/src + /usr/local/opt/openssl/include + ) + +set (CMAKE_CXX_FLAGS "-DHAVE_WORKING_BOOST_SLEEP -g -O3 -Wall -msse2 -msse -march=native -std=c++14 -maes") + +FILE(GLOB_RECURSE MyCSources src/*.cpp) +ADD_LIBRARY(biplib ${MyCSources}) + +add_subdirectory(lib/pybind11) + +pybind11_add_module(chiabip158 ${CMAKE_CURRENT_SOURCE_DIR}/python-bindings/chiabip158.cpp) + +add_executable(bip158 + main.cpp +) + +target_link_libraries(bip158 biplib -lboost_system -lpthread -lboost_thread -lboost_filesystem -lssl -lcrypto) +target_link_libraries(chiabip158 PRIVATE biplib -lboost_system -lpthread -lboost_thread -lboost_filesystem -lssl -lcrypto) + diff --git a/lib/bip158/README.md b/lib/bip158/README.md new file mode 100644 index 0000000000000..1f4cad07b71b2 --- /dev/null +++ b/lib/bip158/README.md @@ -0,0 +1 @@ +This implements the compact block filter construction in BIP 158. The code is not used anywhere in the Bitcoin Core code base yet. The next step towards BIP 157 support would be to create an indexing module similar to TxIndex that constructs the basic and extended filters for each validated block. diff --git a/lib/bip158/main.cpp b/lib/bip158/main.cpp new file mode 100644 index 0000000000000..4ef65786baa5e --- /dev/null +++ b/lib/bip158/main.cpp @@ -0,0 +1,42 @@ +#include +#include "blockfilter.h" + +int main() +{ + srand(time(NULL)); + for(int loop=0;loop<10;loop++) + { + GCSFilter::ElementSet elements2; + for (int i = 0; i < 10000; ++i) + { + GCSFilter::Element element(32); + element[0] = static_cast(i); + element[1] = static_cast(i >> 8); + elements2.insert(std::move(element)); + } + GCSFilter filter({0, 0, 20, 1 << 20}, + elements2); + + GCSFilter::ElementSet elements3; + + for (int j=0;j<10;j++) + { + int i=rand()%50000; + + GCSFilter::Element element(32); + element[0] = static_cast(i); + element[1] = static_cast(i >> 8); + + if(filter.Match(element)) + std::cout << "Found " << i << std::endl; + else + std::cout << "Not Found " << i << std::endl; + elements3.insert(std::move(element)); + } + if(filter.MatchAny(elements3)) + std::cout << loop << " ***** MatchAny returned true" << std::endl; + else + std::cout << loop << " ***** MatchAny returned false" << std::endl; + } + return 0; +} diff --git a/lib/bip158/python-bindings/chiabip158.cpp b/lib/bip158/python-bindings/chiabip158.cpp new file mode 100644 index 0000000000000..70b67fc5a4ab3 --- /dev/null +++ b/lib/bip158/python-bindings/chiabip158.cpp @@ -0,0 +1,29 @@ +// Copyright 2018 Chia Network Inc + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include "PyBIP158.h" + +namespace py = pybind11; + +PYBIND11_MODULE(chiabip158, mod) { + py::class_> clsPyBIP158(mod, "PyBIP158"); + + clsPyBIP158.def(py::init >&>()); + clsPyBIP158.def("Match", (bool (PyBIP158::*)(std::vector< unsigned char >&)) &PyBIP158::Match); + clsPyBIP158.def("MatchAny", (bool (PyBIP158::*)(std::vector< std::vector< unsigned char > >&)) &PyBIP158::MatchAny); +} diff --git a/lib/bip158/python-bindings/test.py b/lib/bip158/python-bindings/test.py new file mode 100755 index 0000000000000..59b8efc0a49f2 --- /dev/null +++ b/lib/bip158/python-bindings/test.py @@ -0,0 +1,13 @@ +from chiabip158 import PyBIP158 +from array import * + +a = [1,2,3] +b = [4,5,6] +c = [a,b] +d = [7,8,9] + +pl = PyBIP158(c) +if pl.Match(a): + print("OK") +else: + print("NOT FOUND") diff --git a/lib/bip158/setup.py b/lib/bip158/setup.py new file mode 100644 index 0000000000000..9c73f6333ab6d --- /dev/null +++ b/lib/bip158/setup.py @@ -0,0 +1,80 @@ +#!/usr/bin/python3 +import os +import re +import sys +import platform +import subprocess + +from setuptools import setup, Extension +from setuptools.command.build_ext import build_ext +from distutils.version import LooseVersion + + +class CMakeExtension(Extension): + def __init__(self, name, sourcedir=''): + Extension.__init__(self, name, sources=['./']) + self.sourcedir = os.path.abspath(sourcedir) + + +class CMakeBuild(build_ext): + def run(self): + try: + out = subprocess.check_output(['cmake', '--version']) + except OSError: + raise RuntimeError("CMake must be installed to build" + + " the following extensions: " + + ", ".join(e.name for e in self.extensions)) + + if platform.system() == "Windows": + cmake_version = LooseVersion( + re.search(r'version\s*([\d.]+)', out.decode()).group(1)) + if cmake_version < '3.1.0': + raise RuntimeError("CMake >= 3.1.0 is required on Windows") + + for ext in self.extensions: + self.build_extension(ext) + + def build_extension(self, ext): + extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) + cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + str(extdir), + '-DPYTHON_EXECUTABLE=' + sys.executable] + + cfg = 'Debug' if self.debug else 'Release' + build_args = ['--config', cfg] + + if platform.system() == "Windows": + cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}' + .format(cfg.upper(), extdir)] + if sys.maxsize > 2**32: + cmake_args += ['-A', 'x64'] + build_args += ['--', '/m'] + else: + cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg] + build_args += ['--', '-j', '6'] + + env = os.environ.copy() + env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format( + env.get('CXXFLAGS', ''), + self.distribution.get_version()) + if not os.path.exists(self.build_temp): + os.makedirs(self.build_temp) + subprocess.check_call(['cmake', ext.sourcedir] + + cmake_args, cwd=self.build_temp, env=env) + subprocess.check_call(['cmake', '--build', '.'] + + build_args, cwd=self.build_temp) + + +setup( + name='chiabip158', + version='0.2.2', + author='Mariano Sorgente', + author_email='mariano@chia.net', + description='Chia BIP158 (wraps C++)', + license='Apache License', + python_requires='>=3.7', + install_requires=['pytest', 'cppimport', 'bitstring', 'flake8'], + long_description=open('README.md').read(), + ext_modules=[CMakeExtension('chiabip158', '.')], + cmdclass=dict(build_ext=CMakeBuild), + zip_safe=False, +) diff --git a/lib/bip158/src/PyBIP158.cpp b/lib/bip158/src/PyBIP158.cpp new file mode 100644 index 0000000000000..be4d4467caf08 --- /dev/null +++ b/lib/bip158/src/PyBIP158.cpp @@ -0,0 +1,53 @@ +// Copyright (c) 2018 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php./Users/bill/downloads/gene/chia-blockchain/lib/bip158 + +#include + +PyBIP158::PyBIP158(std::vector< std::vector< unsigned char > >& hashes) +{ + GCSFilter::ElementSet elements; + for (int i = 0; i < hashes.size(); ++i) + { + GCSFilter::Element element(hashes[i].size()); + for(int j=0;j& hash) +{ + GCSFilter::Element element(hash.size()); + for(int j=0;jMatch(element); +} + +bool PyBIP158::MatchAny(std::vector< std::vector< unsigned char > >& hashes) +{ + GCSFilter::ElementSet elements; + + for (int i = 0; i < hashes.size(); ++i) + { + GCSFilter::Element element(hashes[i].size()); + for(int j=0;jMatchAny(elements); +} diff --git a/lib/bip158/src/PyBIP158.h b/lib/bip158/src/PyBIP158.h new file mode 100644 index 0000000000000..55986ac2c7412 --- /dev/null +++ b/lib/bip158/src/PyBIP158.h @@ -0,0 +1,24 @@ +// Copyright (c) 2018 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 BIP158_H +#define BIP158_H + +#include + +class PyBIP158 +{ +public: + GCSFilter *filter; + +public: + + PyBIP158(std::vector< std::vector< unsigned char > >& hashes); + ~PyBIP158(); + + bool Match(std::vector< unsigned char >& hash); + bool MatchAny(std::vector< std::vector< unsigned char > >& hashes); +}; + +#endif // BIP158_H diff --git a/lib/bip158/src/amount.h b/lib/bip158/src/amount.h new file mode 100644 index 0000000000000..47968e80b1740 --- /dev/null +++ b/lib/bip158/src/amount.h @@ -0,0 +1,28 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2018 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_AMOUNT_H +#define BITCOIN_AMOUNT_H + +#include + +/** Amount in satoshis (Can be negative) */ +typedef int64_t CAmount; + +static const CAmount COIN = 100000000; + +/** No amount larger than this (in satoshi) is valid. + * + * Note that this constant is *not* the total money supply, which in Bitcoin + * currently happens to be less than 21,000,000 BTC for various reasons, but + * rather a sanity check. As this sanity check is used by consensus-critical + * validation code, the exact value of the MAX_MONEY constant is consensus + * critical; in unusual circumstances like a(nother) overflow bug that allowed + * for the creation of coins out of thin air modification could lead to a fork. + * */ +static const CAmount MAX_MONEY = 21000000 * COIN; +inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } + +#endif // BITCOIN_AMOUNT_H diff --git a/lib/bip158/src/attributes.h b/lib/bip158/src/attributes.h new file mode 100644 index 0000000000000..45099bd8b8802 --- /dev/null +++ b/lib/bip158/src/attributes.h @@ -0,0 +1,22 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2018 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_ATTRIBUTES_H +#define BITCOIN_ATTRIBUTES_H + +#if defined(__has_cpp_attribute) +# if __has_cpp_attribute(nodiscard) +# define NODISCARD [[nodiscard]] +# endif +#endif +#ifndef NODISCARD +# if defined(_MSC_VER) && _MSC_VER >= 1700 +# define NODISCARD _Check_return_ +# else +# define NODISCARD __attribute__((warn_unused_result)) +# endif +#endif + +#endif // BITCOIN_ATTRIBUTES_H diff --git a/lib/bip158/src/blockfilter.cpp b/lib/bip158/src/blockfilter.cpp new file mode 100644 index 0000000000000..787390be31d42 --- /dev/null +++ b/lib/bip158/src/blockfilter.cpp @@ -0,0 +1,338 @@ +// Copyright (c) 2018 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 +#include + +#include +#include +#include +#include +#include