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