├── .cirrus.yml ├── .github └── workflows │ ├── codeql-analysis.yml │ └── codeql.yml ├── .gitignore ├── Dockerfile ├── LICENSE ├── Makefile.am ├── README.md ├── ansi-colors.h ├── arith_uint256.cpp ├── arith_uint256.h ├── attributes.h ├── autogen.sh ├── base58.cpp ├── base58.h ├── bech32.cpp ├── bech32.h ├── btcc.cpp ├── btcdeb.cpp ├── build-aux ├── m4 │ ├── ax_check_compile_flag.m4 │ ├── ax_check_link_flag.m4 │ ├── ax_check_preproc_flag.m4 │ ├── ax_cxx_compile_stdcxx.m4 │ ├── ax_gcc_func_attribute.m4 │ ├── ax_lib_readline.m4 │ ├── bitcoin_subdir_to_include.m4 │ ├── ltoptions.m4 │ ├── ltsugar.m4 │ ├── ltversion.m4 │ └── lt~obsolete.m4 └── test-driver ├── ci ├── retry │ ├── README.md │ └── retry ├── test │ ├── 00_setup_env.sh │ ├── 00_setup_env_arm.sh │ ├── 00_setup_env_i686_centos.sh │ ├── 00_setup_env_mac.sh │ ├── 00_setup_env_mac_host.sh │ ├── 00_setup_env_native_asan.sh │ ├── 00_setup_env_native_msan.sh │ ├── 00_setup_env_native_tsan.sh │ ├── 00_setup_env_native_valgrind.sh │ ├── 00_setup_env_s390x.sh │ ├── 00_setup_env_win64.sh │ ├── 04_install.sh │ ├── 05_before_script.sh │ ├── 06_script_a.sh │ ├── 06_script_b.sh │ ├── wrap-qemu.sh │ ├── wrap-valgrind.sh │ └── wrap-wine.sh └── test_run_all.sh ├── cliargs.h ├── compat.h ├── compat ├── byteswap.h ├── cpuid.h └── endian.h ├── config └── EMPTY ├── configure.ac ├── consensus ├── amount.h ├── merkle.cpp └── merkle.h ├── crypto ├── common.h ├── hmac_sha512.cpp ├── hmac_sha512.h ├── ripemd160.cpp ├── ripemd160.h ├── sha1.cpp ├── sha1.h ├── sha256.cpp ├── sha256.h ├── sha512.cpp └── sha512.h ├── datasets.h ├── debugger ├── hash.cpp ├── interpreter.cpp ├── interpreter.h ├── script.cpp ├── script.h ├── see.h └── version.h ├── doc ├── 0001-btcdeb-on-Bitcoin-Core-0.20.99-current-master.patch ├── README.md ├── btcdeb.md ├── example-multisig-invalid-order.txt ├── mock-values.md ├── opcodes.md ├── patch-bitcoin-core-0.19.diff ├── tapscript-example-with-tap.md ├── tapscript-example.md └── txs │ ├── USAGE.md │ ├── p2pkh-in │ ├── p2pkh-tx │ ├── p2sh-multisig-2-of-2-in │ ├── p2sh-multisig-2-of-2-tx │ ├── p2sh-multisig-invalid-order-in │ ├── p2sh-multisig-invalid-order-tx │ ├── p2sh-p2wpkh-in │ ├── p2sh-p2wpkh-tx │ ├── p2tr-in │ ├── p2tr-tx │ ├── p2ts-in │ └── p2ts-tx ├── extras └── reset-to-core.sh ├── functions.cpp ├── functions.h ├── hash.cpp ├── hash.h ├── instance.cpp ├── instance.h ├── kerl ├── kerl.c └── kerl.h ├── mastify.cpp ├── merklebranch.cpp ├── policy └── policy.h ├── prevector.h ├── primitives ├── transaction.cpp └── transaction.h ├── pubkey.cpp ├── pubkey.h ├── script ├── interpreter.cpp ├── interpreter.h ├── script.cpp ├── script.h ├── script_error.cpp └── script_error.h ├── secp256k1 ├── .cirrus.yml ├── .gitignore ├── COPYING ├── Makefile.am ├── README.md ├── SECURITY.md ├── autogen.sh ├── build-aux │ └── m4 │ │ └── bitcoin_secp.m4 ├── ci │ ├── cirrus.sh │ └── linux-debian.Dockerfile ├── configure.ac ├── contrib │ ├── lax_der_parsing.c │ ├── lax_der_parsing.h │ ├── lax_der_privatekey_parsing.c │ └── lax_der_privatekey_parsing.h ├── include │ ├── secp256k1.h │ ├── secp256k1_ecdh.h │ ├── secp256k1_extrakeys.h │ ├── secp256k1_preallocated.h │ ├── secp256k1_recovery.h │ └── secp256k1_schnorrsig.h ├── libsecp256k1.pc.in ├── sage │ ├── gen_exhaustive_groups.sage │ ├── gen_split_lambda_constants.sage │ ├── group_prover.sage │ ├── prove_group_implementations.sage │ ├── secp256k1_params.sage │ └── weierstrass_prover.sage └── src │ ├── asm │ └── field_10x26_arm.s │ ├── assumptions.h │ ├── basic-config.h │ ├── bench.c │ ├── bench.h │ ├── bench_ecmult.c │ ├── bench_internal.c │ ├── ecdsa.h │ ├── ecdsa_impl.h │ ├── eckey.h │ ├── eckey_impl.h │ ├── ecmult.h │ ├── ecmult_compute_table.h │ ├── ecmult_compute_table_impl.h │ ├── ecmult_const.h │ ├── ecmult_const_impl.h │ ├── ecmult_gen.h │ ├── ecmult_gen_compute_table.h │ ├── ecmult_gen_compute_table_impl.h │ ├── ecmult_gen_impl.h │ ├── ecmult_impl.h │ ├── field.h │ ├── field_10x26.h │ ├── field_10x26_impl.h │ ├── field_5x52.h │ ├── field_5x52_asm_impl.h │ ├── field_5x52_impl.h │ ├── field_5x52_int128_impl.h │ ├── field_impl.h │ ├── group.h │ ├── group_impl.h │ ├── hash.h │ ├── hash_impl.h │ ├── modinv32.h │ ├── modinv32_impl.h │ ├── modinv64.h │ ├── modinv64_impl.h │ ├── modules │ ├── ecdh │ │ ├── Makefile.am.include │ │ ├── bench_impl.h │ │ ├── main_impl.h │ │ └── tests_impl.h │ ├── extrakeys │ │ ├── Makefile.am.include │ │ ├── main_impl.h │ │ ├── tests_exhaustive_impl.h │ │ └── tests_impl.h │ ├── recovery │ │ ├── Makefile.am.include │ │ ├── bench_impl.h │ │ ├── main_impl.h │ │ ├── tests_exhaustive_impl.h │ │ └── tests_impl.h │ └── schnorrsig │ │ ├── Makefile.am.include │ │ ├── bench_impl.h │ │ ├── main_impl.h │ │ ├── tests_exhaustive_impl.h │ │ └── tests_impl.h │ ├── precompute_ecmult.c │ ├── precompute_ecmult_gen.c │ ├── precomputed_ecmult.c │ ├── precomputed_ecmult.h │ ├── precomputed_ecmult_gen.c │ ├── precomputed_ecmult_gen.h │ ├── scalar.h │ ├── scalar_4x64.h │ ├── scalar_4x64_impl.h │ ├── scalar_8x32.h │ ├── scalar_8x32_impl.h │ ├── scalar_impl.h │ ├── scalar_low.h │ ├── scalar_low_impl.h │ ├── scratch.h │ ├── scratch_impl.h │ ├── secp256k1.c │ ├── selftest.h │ ├── testrand.h │ ├── testrand_impl.h │ ├── tests.c │ ├── tests_exhaustive.c │ ├── util.h │ └── valgrind_ctime_test.c ├── serialize.h ├── span.h ├── streams.h ├── support ├── allocators │ ├── secure.h │ └── zeroafterfree.h ├── cleanse.cpp ├── cleanse.h ├── lockedpool.cpp └── lockedpool.h ├── tap.cpp ├── test ├── catch.cpp ├── catch.hpp ├── signing.cpp ├── test-btcdeb.cpp ├── utils.cpp └── value.cpp ├── tinyformat.h ├── uint256.cpp ├── uint256.h ├── util ├── overflow.h ├── spanparsing.cpp ├── spanparsing.h ├── strencodings.cpp ├── strencodings.h ├── string.cpp ├── string.h └── vector.h ├── value.cpp └── value.h /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ "master" ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ "master" ] 20 | schedule: 21 | - cron: '16 5 * * 6' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | permissions: 28 | actions: read 29 | contents: read 30 | security-events: write 31 | 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | language: [ 'cpp' ] 36 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] 37 | # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support 38 | 39 | steps: 40 | - name: Checkout repository 41 | uses: actions/checkout@v3 42 | 43 | # Initializes the CodeQL tools for scanning. 44 | - name: Initialize CodeQL 45 | uses: github/codeql-action/init@v2 46 | with: 47 | languages: ${{ matrix.language }} 48 | # If you wish to specify custom queries, you can do so here or in a config file. 49 | # By default, queries listed here will override any specified in a config file. 50 | # Prefix the list here with "+" to use these queries and those in the config file. 51 | 52 | # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs 53 | # queries: security-extended,security-and-quality 54 | 55 | 56 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 57 | # If this step fails, then you should remove it and run the build manually (see below) 58 | - name: Autobuild 59 | uses: github/codeql-action/autobuild@v2 60 | 61 | # ℹ️ Command-line programs to run using the OS shell. 62 | # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun 63 | 64 | # If the Autobuild fails above, remove it and uncomment the following three lines. 65 | # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. 66 | 67 | # - run: | 68 | # echo "Run, Build Application using script" 69 | # ./location_of_script_within_repo/buildscript.sh 70 | 71 | - name: Perform CodeQL Analysis 72 | uses: github/codeql-action/analyze@v2 73 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ "master" ] 17 | pull_request: 18 | branches: [ "master" ] 19 | schedule: 20 | - cron: '34 20 * * 3' 21 | 22 | jobs: 23 | analyze: 24 | name: Analyze 25 | # Runner size impacts CodeQL analysis time. To learn more, please see: 26 | # - https://gh.io/recommended-hardware-resources-for-running-codeql 27 | # - https://gh.io/supported-runners-and-hardware-resources 28 | # - https://gh.io/using-larger-runners 29 | # Consider using larger runners for possible analysis time improvements. 30 | runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} 31 | timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }} 32 | permissions: 33 | # required for all workflows 34 | security-events: write 35 | 36 | # only required for workflows in private repositories 37 | actions: read 38 | contents: read 39 | 40 | strategy: 41 | fail-fast: false 42 | matrix: 43 | language: [ 'c-cpp' ] 44 | # CodeQL supports [ 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' ] 45 | # Use only 'java-kotlin' to analyze code written in Java, Kotlin or both 46 | # Use only 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both 47 | # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support 48 | 49 | steps: 50 | - name: Checkout repository 51 | uses: actions/checkout@v4 52 | 53 | # Initializes the CodeQL tools for scanning. 54 | - name: Initialize CodeQL 55 | uses: github/codeql-action/init@v3 56 | with: 57 | languages: ${{ matrix.language }} 58 | # If you wish to specify custom queries, you can do so here or in a config file. 59 | # By default, queries listed here will override any specified in a config file. 60 | # Prefix the list here with "+" to use these queries and those in the config file. 61 | 62 | # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs 63 | # queries: security-extended,security-and-quality 64 | 65 | 66 | # Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift). 67 | # If this step fails, then you should remove it and run the build manually (see below) 68 | - name: Autobuild 69 | uses: github/codeql-action/autobuild@v3 70 | 71 | # ℹ️ Command-line programs to run using the OS shell. 72 | # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun 73 | 74 | # If the Autobuild fails above, remove it and uncomment the following three lines. 75 | # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. 76 | 77 | # - run: | 78 | # echo "Run, Build Application using script" 79 | # ./location_of_script_within_repo/buildscript.sh 80 | 81 | - name: Perform CodeQL Analysis 82 | uses: github/codeql-action/analyze@v3 83 | with: 84 | category: "/language:${{matrix.language}}" 85 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | .deps 54 | .tags* 55 | Makefile 56 | Makefile.in 57 | aclocal.m4 58 | autom4te.cache 59 | btcc 60 | build-aux/depcomp 61 | config.log 62 | config.status 63 | config/bitcoin-config.h 64 | config/bitcoin-config.h.in 65 | *~ 66 | config/stamp-h1 67 | configure 68 | .dirstamp 69 | libtool 70 | .btcdeb_history 71 | btcdeb 72 | merklebranch 73 | mastify 74 | 75 | # autoreconf 76 | Makefile.in 77 | aclocal.m4 78 | autom4te.cache/ 79 | build-aux/config.guess 80 | build-aux/config.sub 81 | build-aux/depcomp 82 | build-aux/install-sh 83 | build-aux/ltmain.sh 84 | build-aux/m4/libtool.m4 85 | build-aux/m4/lt~obsolete.m4 86 | build-aux/m4/ltoptions.m4 87 | build-aux/m4/ltsugar.m4 88 | build-aux/m4/ltversion.m4 89 | build-aux/missing 90 | build-aux/compile 91 | build-aux/test-driver 92 | config.log 93 | config.status 94 | configure 95 | libtool 96 | src/config/bitcoin-config.h 97 | src/config/bitcoin-config.h.in 98 | src/config/stamp-h1 99 | share/setup.nsi 100 | share/qt/Info.plist 101 | test-btcdeb 102 | tap 103 | approach 104 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine 2 | 3 | RUN apk update && apk upgrade 4 | 5 | ENV BUILD_DEPS autoconf file gcc libc-dev make g++ pkgconf re2c git libtool automake build-base gcc 6 | 7 | RUN apk add --update --no-cache --virtual .build-deps $BUILD_DEPS 8 | 9 | WORKDIR /tmp/btcdeb 10 | 11 | COPY . . 12 | 13 | RUN ./autogen.sh 14 | 15 | RUN ./configure 16 | 17 | RUN make clean 18 | 19 | RUN make 20 | 21 | RUN make install 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-2020 Karl-Johan Alm 4 | Copyright (c) 2020 The Bitcoin Core developers 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # btcdeb 2 | 3 | Bitcoin Script debugging utilities. 4 | 5 | This is a set of tools used to debug or construct scripts for use in Bitcoin. 6 | 7 | [![Build Status](https://travis-ci.org/bitcoin-core/btcdeb.svg?branch=master)](https://travis-ci.org/bitcoin-core/btcdeb) 8 | 9 | ## Preparation 10 | 11 | Mac users need the macOS command line tools: 12 | 13 | ``` 14 | xcode-select --install 15 | ``` 16 | 17 | And [Homebrew](https://brew.sh/). 18 | 19 | ## Dependencies 20 | 21 | btcdeb depends on the following: 22 | 23 | - libtool 24 | - libssl 25 | - automake/autoconf 26 | - pkg-config 27 | 28 | Ubuntu/debian users can do: `apt-get install libtool libssl-dev autoconf pkg-config` (with `sudo` prepended if necessary) 29 | 30 | Mac users can do: `brew install libtool automake pkg-config` 31 | 32 | ## Installation 33 | 34 | On linux or mac, grab the source code and do: 35 | 36 | ```Bash 37 | $ ./autogen.sh 38 | $ ./configure 39 | $ make 40 | $ sudo make install 41 | ``` 42 | 43 | Or use **docker**: 44 | 45 | ```Bash 46 | docker build -t btcdeb . 47 | docker run -d -t --name btc-vm btcdeb btcdeb 48 | # and for run it in the shell 49 | docker exec -it btc-vm /bin/sh 50 | ``` 51 | 52 | If any of those give an error, please file an issue and I'll take a look. It could 53 | be a dependency that I forgot about. 54 | 55 | ## Emscripten 56 | 57 | You can compile btcdeb tools into JavaScript using [emscripten](http://kripken.github.io/emscripten-site/). 58 | 59 | After installing the SDK, compile btcdeb tools with the following commands: 60 | 61 | ```Bash 62 | $ make clean 63 | $ emconfigure ./configure 64 | $ emmake make 65 | $ for i in btcdeb btcc tap; do mv $i $i.bc && emcc -O2 $i.bc libbitcoin.a -o $i.js; done 66 | ``` 67 | 68 | and then instead of doing `./btcdeb` you do `node btcdeb.js` (or `mastify.js`, etc). 69 | 70 | The last part is done because emscripten's `emcc` expects the input bytecode file to have the `.bc` extension, whereas the makefile generates files with no extension. 71 | 72 | Note: most things work, but the console in btcdeb does not. You can work around this by doing `echo -n -e "step\n\n\n"` (with sufficient `\n`s). 73 | 74 | ## Script debugger 75 | 76 | The `btcdeb` command can step through a Bitcoin Script and show stack content and operations on a per op level. See [doc/btcdeb.md](doc/btcdeb.md) for details on usage. 77 | 78 | ## Script compiler 79 | 80 | The `btcc` command can interpret a script in its human readable form and will 81 | return a corresponding Bitcoin Script. 82 | 83 | ```Bash 84 | $ btcc OP_DUP OP_HASH160 897c81ac37ae36f7bc5b91356cfb0138bfacb3c1 OP_EQUALVERIFY OP_CHECKSIG 85 | 76a914897c81ac37ae36f7bc5b91356cfb0138bfacb3c188ac 86 | ``` 87 | 88 | The above is the script pub key for a transaction in Bitcoin in human readable format turned into its hexadecimal representation. 89 | -------------------------------------------------------------------------------- /ansi-colors.h: -------------------------------------------------------------------------------- 1 | #ifndef included_ansicolors_h_ 2 | #define included_ansicolors_h_ 3 | 4 | // Source: https://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal 5 | namespace ansi { 6 | 7 | const std::string reset("\033[0m"); // everything back to normal 8 | 9 | const std::string bold ("\033[1m"); // often a brighter shade of the same color 10 | const std::string uline ("\033[4m"); 11 | const std::string inverse("\033[7m"); // swap fg/bg 12 | 13 | const std::string bold_off ("\033[21m"); 14 | const std::string uline_off ("\033[24m"); 15 | const std::string inverse_off("\033[27m"); 16 | 17 | const std::string fg_black ("\033[0;30m"); 18 | const std::string fg_red ("\033[0;31m"); 19 | const std::string fg_green ("\033[0;32m"); 20 | const std::string fg_yellow ("\033[0;33m"); 21 | const std::string fg_blue ("\033[0;34m"); 22 | const std::string fg_magenta("\033[0;35m"); 23 | const std::string fg_cyan ("\033[0;36m"); 24 | const std::string fg_white ("\033[0;37m"); 25 | 26 | const std::string bg_black ("\033[0;40m"); 27 | const std::string bg_red ("\033[0;41m"); 28 | const std::string bg_green ("\033[0;42m"); 29 | const std::string bg_yellow ("\033[0;43m"); 30 | const std::string bg_blue ("\033[0;44m"); 31 | const std::string bg_magenta("\033[0;45m"); 32 | const std::string bg_cyan ("\033[0;46m"); 33 | const std::string bg_white ("\033[0;47m"); 34 | 35 | inline size_t length(const std::string& s) { 36 | size_t l = 0; 37 | for (size_t i = 0; s[i]; ++i) { 38 | char ch = s[i]; 39 | if (ch == 033) { 40 | for (++i; s[i] != 'm'; ++i); 41 | continue; 42 | } 43 | ++l; 44 | } 45 | return l; 46 | } 47 | 48 | inline std::string substring(const std::string& s, size_t start, size_t length) { 49 | char buf[1024]; 50 | size_t p = 0; 51 | size_t l = 0; 52 | size_t end = start + length; 53 | for (size_t i = 0; l < end && s[i]; ++i) { 54 | bool xfer = l >= start; 55 | if (s[i] == 033) { 56 | if (xfer) buf[p++] = s[i]; 57 | for (++i; s[i] != 'm'; ++i) if (xfer) buf[p++] = s[i]; 58 | if (xfer) buf[p++] = s[i]; 59 | continue; 60 | } 61 | if (xfer) buf[p++] = s[i]; 62 | ++l; 63 | } 64 | buf[p] = 0; 65 | return std::string(buf) + reset; 66 | } 67 | 68 | inline std::string trimmed_right(const std::string& s) { 69 | size_t len = s.length(); 70 | if (len == 0) return s; 71 | size_t j; 72 | for (size_t i = len - 1; i > 1; --i) { 73 | switch (s[i]) { 74 | case ' ': 75 | case '\t': 76 | case '\n': 77 | continue; 78 | case 'm': 79 | // ansi sequence? 80 | j = i - 1; 81 | while (j > 0 && s[j] >= '0' && s[j] <= '9') --j; 82 | if (j > 0 && s[j] == ';') { 83 | --j; 84 | while (j > 0 && s[j] >= '0' && s[j] <= '9') --j; 85 | if (j > 0 && s[j] == ']') { 86 | if (s[j] == 033) { 87 | i = j; 88 | continue; 89 | } 90 | } 91 | } 92 | // nope, not an ansi sequence; we're done! fall-through 93 | default: 94 | return s.substr(0, i + 1); 95 | } 96 | } 97 | // the entire string was whitespace and/or ansi codes 98 | return ""; 99 | } 100 | 101 | } // namespace ansi 102 | 103 | #endif // included_ansicolors_h_ 104 | -------------------------------------------------------------------------------- /attributes.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2020 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | #ifndef BITCOIN_ATTRIBUTES_H 7 | #define BITCOIN_ATTRIBUTES_H 8 | 9 | #if defined(__clang__) 10 | # if __has_attribute(lifetimebound) 11 | # define LIFETIMEBOUND [[clang::lifetimebound]] 12 | # else 13 | # define LIFETIMEBOUND 14 | # endif 15 | #else 16 | # define LIFETIMEBOUND 17 | #endif 18 | 19 | #endif // BITCOIN_ATTRIBUTES_H 20 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Copyright (c) 2013-2016 The Bitcoin Core developers 3 | # Distributed under the MIT software license, see the accompanying 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | set -e 7 | srcdir="$(dirname $0)" 8 | cd "$srcdir" 9 | if [ -z ${LIBTOOLIZE} ] && GLIBTOOLIZE="`which glibtoolize 2>/dev/null`"; then 10 | LIBTOOLIZE="${GLIBTOOLIZE}" 11 | export LIBTOOLIZE 12 | fi 13 | which autoreconf >/dev/null || \ 14 | (echo "configuration failed, please install autoconf first" && exit 1) 15 | autoreconf --install --force --warnings=all 16 | -------------------------------------------------------------------------------- /base58.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2020 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | /** 7 | * Why base-58 instead of standard base-64 encoding? 8 | * - Don't want 0OIl characters that look the same in some fonts and 9 | * could be used to create visually identical looking data. 10 | * - A string with non-alphanumeric characters is not as easily accepted as input. 11 | * - E-mail usually won't line-break if there's no punctuation to break at. 12 | * - Double-clicking selects the whole string as one word if it's all alphanumeric. 13 | */ 14 | #ifndef BITCOIN_BASE58_H 15 | #define BITCOIN_BASE58_H 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | /** 23 | * Encode a byte span as a base58-encoded string 24 | */ 25 | std::string EncodeBase58(Span input); 26 | 27 | /** 28 | * Decode a base58-encoded string (str) into a byte vector (vchRet). 29 | * return true if decoding is successful. 30 | */ 31 | [[nodiscard]] bool DecodeBase58(const std::string& str, std::vector& vchRet, int max_ret_len); 32 | 33 | /** 34 | * Encode a byte span into a base58-encoded string, including checksum 35 | */ 36 | std::string EncodeBase58Check(Span input); 37 | 38 | /** 39 | * Decode a base58-encoded string (str) that includes a checksum into a byte 40 | * vector (vchRet), return true if decoding is successful 41 | */ 42 | [[nodiscard]] bool DecodeBase58Check(const std::string& str, std::vector& vchRet, int max_ret_len); 43 | 44 | #endif // BITCOIN_BASE58_H 45 | -------------------------------------------------------------------------------- /bech32.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, 2021 Pieter Wuille 2 | // Copyright (c) 2021 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | // Bech32 and Bech32m are string encoding formats used in newer 7 | // address types. The outputs consist of a human-readable part 8 | // (alphanumeric), a separator character (1), and a base32 data 9 | // section, the last 6 characters of which are a checksum. The 10 | // module is namespaced under bech32 for historical reasons. 11 | // 12 | // For more information, see BIP 173 and BIP 350. 13 | 14 | #ifndef BITCOIN_BECH32_H 15 | #define BITCOIN_BECH32_H 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | namespace bech32 22 | { 23 | 24 | enum class Encoding { 25 | INVALID, //!< Failed decoding 26 | 27 | BECH32, //!< Bech32 encoding as defined in BIP173 28 | BECH32M, //!< Bech32m encoding as defined in BIP350 29 | }; 30 | 31 | /** Encode a Bech32 or Bech32m string. If hrp contains uppercase characters, this will cause an 32 | * assertion error. Encoding must be one of BECH32 or BECH32M. */ 33 | std::string Encode(Encoding encoding, const std::string& hrp, const std::vector& values); 34 | 35 | struct DecodeResult 36 | { 37 | Encoding encoding; //!< What encoding was detected in the result; Encoding::INVALID if failed. 38 | std::string hrp; //!< The human readable part 39 | std::vector data; //!< The payload (excluding checksum) 40 | 41 | DecodeResult() : encoding(Encoding::INVALID) {} 42 | DecodeResult(Encoding enc, std::string&& h, std::vector&& d) : encoding(enc), hrp(std::move(h)), data(std::move(d)) {} 43 | }; 44 | 45 | /** Decode a Bech32 or Bech32m string. */ 46 | DecodeResult Decode(const std::string& str); 47 | 48 | /** Return the positions of errors in a Bech32 string. */ 49 | std::pair> LocateErrors(const std::string& str); 50 | 51 | } // namespace bech32 52 | 53 | #endif // BITCOIN_BECH32_H 54 | -------------------------------------------------------------------------------- /btcc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | typedef std::vector valtype; 6 | 7 | int main(int argc, const char** argv) 8 | { 9 | if (argc < 2) { 10 | printf("syntax: %s \n", argv[0]); 11 | printf("e.g. %s OP_DUP OP_HASH160 '[62e907b15cbf27d5425399ebf6f0fb50ebb88f18]' OP_EQUALVERIFY OP_CHECKSIG\n", argv[0]); 12 | return 1; 13 | } 14 | std::vector result = Value::parse_args(argc, argv, 1); 15 | fprintf(stdout, "%s\n", Value::serialize(result).c_str()); 16 | } 17 | -------------------------------------------------------------------------------- /build-aux/m4/ax_check_compile_flag.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check whether the given FLAG works with the current language's compiler 12 | # or gives an error. (Warnings, however, are ignored) 13 | # 14 | # ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on 15 | # success/failure. 16 | # 17 | # If EXTRA-FLAGS is defined, it is added to the current language's default 18 | # flags (e.g. CFLAGS) when the check is done. The check is thus made with 19 | # the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to 20 | # force the compiler to issue an error when a bad flag is given. 21 | # 22 | # INPUT gives an alternative input source to AC_COMPILE_IFELSE. 23 | # 24 | # NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this 25 | # macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG. 26 | # 27 | # LICENSE 28 | # 29 | # Copyright (c) 2008 Guido U. Draheim 30 | # Copyright (c) 2011 Maarten Bosmans 31 | # 32 | # This program is free software: you can redistribute it and/or modify it 33 | # under the terms of the GNU General Public License as published by the 34 | # Free Software Foundation, either version 3 of the License, or (at your 35 | # option) any later version. 36 | # 37 | # This program is distributed in the hope that it will be useful, but 38 | # WITHOUT ANY WARRANTY; without even the implied warranty of 39 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 40 | # Public License for more details. 41 | # 42 | # You should have received a copy of the GNU General Public License along 43 | # with this program. If not, see . 44 | # 45 | # As a special exception, the respective Autoconf Macro's copyright owner 46 | # gives unlimited permission to copy, distribute and modify the configure 47 | # scripts that are the output of Autoconf when processing the Macro. You 48 | # need not follow the terms of the GNU General Public License when using 49 | # or distributing such scripts, even though portions of the text of the 50 | # Macro appear in them. The GNU General Public License (GPL) does govern 51 | # all other use of the material that constitutes the Autoconf Macro. 52 | # 53 | # This special exception to the GPL applies to versions of the Autoconf 54 | # Macro released by the Autoconf Archive. When you make and distribute a 55 | # modified version of the Autoconf Macro, you may extend this special 56 | # exception to the GPL to apply to your modified version as well. 57 | 58 | #serial 4 59 | 60 | AC_DEFUN([AX_CHECK_COMPILE_FLAG], 61 | [AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF 62 | AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl 63 | AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [ 64 | ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS 65 | _AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1" 66 | AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])], 67 | [AS_VAR_SET(CACHEVAR,[yes])], 68 | [AS_VAR_SET(CACHEVAR,[no])]) 69 | _AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags]) 70 | AS_VAR_IF(CACHEVAR,yes, 71 | [m4_default([$2], :)], 72 | [m4_default([$3], :)]) 73 | AS_VAR_POPDEF([CACHEVAR])dnl 74 | ])dnl AX_CHECK_COMPILE_FLAGS 75 | -------------------------------------------------------------------------------- /build-aux/m4/ax_check_link_flag.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_check_link_flag.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CHECK_LINK_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check whether the given FLAG works with the linker or gives an error. 12 | # (Warnings, however, are ignored) 13 | # 14 | # ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on 15 | # success/failure. 16 | # 17 | # If EXTRA-FLAGS is defined, it is added to the linker's default flags 18 | # when the check is done. The check is thus made with the flags: "LDFLAGS 19 | # EXTRA-FLAGS FLAG". This can for example be used to force the linker to 20 | # issue an error when a bad flag is given. 21 | # 22 | # INPUT gives an alternative input source to AC_LINK_IFELSE. 23 | # 24 | # NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this 25 | # macro in sync with AX_CHECK_{PREPROC,COMPILE}_FLAG. 26 | # 27 | # LICENSE 28 | # 29 | # Copyright (c) 2008 Guido U. Draheim 30 | # Copyright (c) 2011 Maarten Bosmans 31 | # 32 | # This program is free software: you can redistribute it and/or modify it 33 | # under the terms of the GNU General Public License as published by the 34 | # Free Software Foundation, either version 3 of the License, or (at your 35 | # option) any later version. 36 | # 37 | # This program is distributed in the hope that it will be useful, but 38 | # WITHOUT ANY WARRANTY; without even the implied warranty of 39 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 40 | # Public License for more details. 41 | # 42 | # You should have received a copy of the GNU General Public License along 43 | # with this program. If not, see . 44 | # 45 | # As a special exception, the respective Autoconf Macro's copyright owner 46 | # gives unlimited permission to copy, distribute and modify the configure 47 | # scripts that are the output of Autoconf when processing the Macro. You 48 | # need not follow the terms of the GNU General Public License when using 49 | # or distributing such scripts, even though portions of the text of the 50 | # Macro appear in them. The GNU General Public License (GPL) does govern 51 | # all other use of the material that constitutes the Autoconf Macro. 52 | # 53 | # This special exception to the GPL applies to versions of the Autoconf 54 | # Macro released by the Autoconf Archive. When you make and distribute a 55 | # modified version of the Autoconf Macro, you may extend this special 56 | # exception to the GPL to apply to your modified version as well. 57 | 58 | #serial 4 59 | 60 | AC_DEFUN([AX_CHECK_LINK_FLAG], 61 | [AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF 62 | AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_ldflags_$4_$1])dnl 63 | AC_CACHE_CHECK([whether the linker accepts $1], CACHEVAR, [ 64 | ax_check_save_flags=$LDFLAGS 65 | LDFLAGS="$LDFLAGS $4 $1" 66 | AC_LINK_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])], 67 | [AS_VAR_SET(CACHEVAR,[yes])], 68 | [AS_VAR_SET(CACHEVAR,[no])]) 69 | LDFLAGS=$ax_check_save_flags]) 70 | AS_VAR_IF(CACHEVAR,yes, 71 | [m4_default([$2], :)], 72 | [m4_default([$3], :)]) 73 | AS_VAR_POPDEF([CACHEVAR])dnl 74 | ])dnl AX_CHECK_LINK_FLAGS 75 | -------------------------------------------------------------------------------- /build-aux/m4/ax_check_preproc_flag.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_check_preproc_flag.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CHECK_PREPROC_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check whether the given FLAG works with the current language's 12 | # preprocessor or gives an error. (Warnings, however, are ignored) 13 | # 14 | # ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on 15 | # success/failure. 16 | # 17 | # If EXTRA-FLAGS is defined, it is added to the preprocessor's default 18 | # flags when the check is done. The check is thus made with the flags: 19 | # "CPPFLAGS EXTRA-FLAGS FLAG". This can for example be used to force the 20 | # preprocessor to issue an error when a bad flag is given. 21 | # 22 | # INPUT gives an alternative input source to AC_PREPROC_IFELSE. 23 | # 24 | # NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this 25 | # macro in sync with AX_CHECK_{COMPILE,LINK}_FLAG. 26 | # 27 | # LICENSE 28 | # 29 | # Copyright (c) 2008 Guido U. Draheim 30 | # Copyright (c) 2011 Maarten Bosmans 31 | # 32 | # This program is free software: you can redistribute it and/or modify it 33 | # under the terms of the GNU General Public License as published by the 34 | # Free Software Foundation, either version 3 of the License, or (at your 35 | # option) any later version. 36 | # 37 | # This program is distributed in the hope that it will be useful, but 38 | # WITHOUT ANY WARRANTY; without even the implied warranty of 39 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 40 | # Public License for more details. 41 | # 42 | # You should have received a copy of the GNU General Public License along 43 | # with this program. If not, see . 44 | # 45 | # As a special exception, the respective Autoconf Macro's copyright owner 46 | # gives unlimited permission to copy, distribute and modify the configure 47 | # scripts that are the output of Autoconf when processing the Macro. You 48 | # need not follow the terms of the GNU General Public License when using 49 | # or distributing such scripts, even though portions of the text of the 50 | # Macro appear in them. The GNU General Public License (GPL) does govern 51 | # all other use of the material that constitutes the Autoconf Macro. 52 | # 53 | # This special exception to the GPL applies to versions of the Autoconf 54 | # Macro released by the Autoconf Archive. When you make and distribute a 55 | # modified version of the Autoconf Macro, you may extend this special 56 | # exception to the GPL to apply to your modified version as well. 57 | 58 | #serial 4 59 | 60 | AC_DEFUN([AX_CHECK_PREPROC_FLAG], 61 | [AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF 62 | AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]cppflags_$4_$1])dnl 63 | AC_CACHE_CHECK([whether _AC_LANG preprocessor accepts $1], CACHEVAR, [ 64 | ax_check_save_flags=$CPPFLAGS 65 | CPPFLAGS="$CPPFLAGS $4 $1" 66 | AC_PREPROC_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])], 67 | [AS_VAR_SET(CACHEVAR,[yes])], 68 | [AS_VAR_SET(CACHEVAR,[no])]) 69 | CPPFLAGS=$ax_check_save_flags]) 70 | AS_VAR_IF(CACHEVAR,yes, 71 | [m4_default([$2], :)], 72 | [m4_default([$3], :)]) 73 | AS_VAR_POPDEF([CACHEVAR])dnl 74 | ])dnl AX_CHECK_PREPROC_FLAGS 75 | -------------------------------------------------------------------------------- /build-aux/m4/bitcoin_subdir_to_include.m4: -------------------------------------------------------------------------------- 1 | dnl Copyright (c) 2013-2014 The Bitcoin Core developers 2 | dnl Distributed under the MIT software license, see the accompanying 3 | dnl file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | dnl BITCOIN_SUBDIR_TO_INCLUDE([CPPFLAGS-VARIABLE-NAME],[SUBDIRECTORY-NAME],[HEADER-FILE]) 6 | dnl SUBDIRECTORY-NAME must end with a path separator 7 | AC_DEFUN([BITCOIN_SUBDIR_TO_INCLUDE],[ 8 | if test "x$2" = "x"; then 9 | AC_MSG_RESULT([default]) 10 | 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`] 13 | AC_MSG_RESULT([${newinclpath}]) 14 | if test "x${newinclpath}" != "x"; then 15 | eval "$1=\"\$$1\"' -I${newinclpath}'" 16 | fi 17 | fi 18 | ]) 19 | -------------------------------------------------------------------------------- /build-aux/m4/ltversion.m4: -------------------------------------------------------------------------------- 1 | # ltversion.m4 -- version numbers -*- Autoconf -*- 2 | # 3 | # Copyright (C) 2004, 2011-2015 Free Software Foundation, Inc. 4 | # Written by Scott James Remnant, 2004 5 | # 6 | # This file is free software; the Free Software Foundation gives 7 | # unlimited permission to copy and/or distribute it, with or without 8 | # modifications, as long as this notice is preserved. 9 | 10 | # @configure_input@ 11 | 12 | # serial 4179 ltversion.m4 13 | # This file is part of GNU Libtool 14 | 15 | m4_define([LT_PACKAGE_VERSION], [2.4.6]) 16 | m4_define([LT_PACKAGE_REVISION], [2.4.6]) 17 | 18 | AC_DEFUN([LTVERSION_VERSION], 19 | [macro_version='2.4.6' 20 | macro_revision='2.4.6' 21 | _LT_DECL(, macro_version, 0, [Which release of libtool.m4 was used?]) 22 | _LT_DECL(, macro_revision, 0) 23 | ]) 24 | -------------------------------------------------------------------------------- /ci/retry/README.md: -------------------------------------------------------------------------------- 1 | retry - The command line retry tool 2 | ------------------------------------------ 3 | 4 | Retry any shell command with exponential backoff or constant delay. 5 | 6 | ### Instructions 7 | 8 | Install: 9 | 10 | retry is a shell script, so drop it somewhere and make sure it's added to your $PATH. Or you can use the following one-liner: 11 | 12 | ```sh 13 | sudo sh -c "curl https://raw.githubusercontent.com/kadwanev/retry/master/retry -o /usr/local/bin/retry && chmod +x /usr/local/bin/retry" 14 | ``` 15 | 16 | If you're on OS X, retry is also on Homebrew: 17 | 18 | ``` 19 | brew pull 27283 20 | brew install retry 21 | ``` 22 | Not popular enough for homebrew-core. Please star this project to help. 23 | 24 | ### Usage 25 | 26 | Help: 27 | 28 | `retry -?` 29 | 30 | Usage: retry [options] -- execute command 31 | -h, -?, --help 32 | -v, --verbose Verbose output 33 | -t, --tries=# Set max retries: Default 10 34 | -s, --sleep=secs Constant sleep amount (seconds) 35 | -m, --min=secs Exponential Backoff: minimum sleep amount (seconds): Default 0.3 36 | -x, --max=secs Exponential Backoff: maximum sleep amount (seconds): Default 60 37 | -f, --fail="script +cmds" Fail Script: run in case of final failure 38 | 39 | ### Examples 40 | 41 | No problem: 42 | 43 | `retry echo u work good` 44 | 45 | u work good 46 | 47 | Test functionality: 48 | 49 | `retry 'echo "y u no work"; false'` 50 | 51 | y u no work 52 | Before retry #1: sleeping 0.3 seconds 53 | y u no work 54 | Before retry #2: sleeping 0.6 seconds 55 | y u no work 56 | Before retry #3: sleeping 1.2 seconds 57 | y u no work 58 | Before retry #4: sleeping 2.4 seconds 59 | y u no work 60 | Before retry #5: sleeping 4.8 seconds 61 | y u no work 62 | Before retry #6: sleeping 9.6 seconds 63 | y u no work 64 | Before retry #7: sleeping 19.2 seconds 65 | y u no work 66 | Before retry #8: sleeping 38.4 seconds 67 | y u no work 68 | Before retry #9: sleeping 60.0 seconds 69 | y u no work 70 | Before retry #10: sleeping 60.0 seconds 71 | y u no work 72 | etc.. 73 | 74 | Limit retries: 75 | 76 | `retry -t 4 'echo "y u no work"; false'` 77 | 78 | y u no work 79 | Before retry #1: sleeping 0.3 seconds 80 | y u no work 81 | Before retry #2: sleeping 0.6 seconds 82 | y u no work 83 | Before retry #3: sleeping 1.2 seconds 84 | y u no work 85 | Before retry #4: sleeping 2.4 seconds 86 | y u no work 87 | Retries exhausted 88 | 89 | Bad command: 90 | 91 | `retry poop` 92 | 93 | bash: poop: command not found 94 | 95 | Fail command: 96 | 97 | `retry -t 3 -f 'echo "oh poopsickles"' 'echo "y u no work"; false'` 98 | 99 | y u no work 100 | Before retry #1: sleeping 0.3 seconds 101 | y u no work 102 | Before retry #2: sleeping 0.6 seconds 103 | y u no work 104 | Before retry #3: sleeping 1.2 seconds 105 | y u no work 106 | Retries exhausted, running fail script 107 | oh poopsickles 108 | 109 | Last attempt passed: 110 | 111 | `retry -t 3 -- 'if [ $RETRY_ATTEMPT -eq 3 ]; then echo Passed at attempt $RETRY_ATTEMPT; true; else echo Failed at attempt $RETRY_ATTEMPT; false; fi;'` 112 | 113 | Failed at attempt 0 114 | Before retry #1: sleeping 0.3 seconds 115 | Failed at attempt 1 116 | Before retry #2: sleeping 0.6 seconds 117 | Failed at attempt 2 118 | Before retry #3: sleeping 1.2 seconds 119 | Passed at attempt 3 120 | 121 | ### License 122 | 123 | Apache 2.0 - go nuts 124 | -------------------------------------------------------------------------------- /ci/test/00_setup_env.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2019-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | # The root dir. 10 | # The ci system copies this folder. 11 | # This is where the depends build is done. 12 | BASE_ROOT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )"/../../ >/dev/null 2>&1 && pwd ) 13 | export BASE_ROOT_DIR 14 | 15 | echo "Setting specific values in env" 16 | if [ -n "${FILE_ENV}" ]; then 17 | set -o errexit; 18 | # shellcheck disable=SC1090 19 | source "${FILE_ENV}" 20 | fi 21 | 22 | if [ "$ENABLE_DANGEROUS" = "1" ]; then 23 | echo "Enabling dangerous features" 24 | export BITCOIN_CONFIG="--enable-dangerous $BITCOIN_CONFIG" 25 | fi 26 | 27 | echo "Fallback to default values in env (if not yet set)" 28 | # The number of parallel jobs to pass down to make and test_runner.py 29 | export MAKEJOBS=${MAKEJOBS:--j4} 30 | # A folder for the ci system to put temporary files (ccache, datadirs for tests, ...) 31 | # This folder only exists on the ci host. 32 | export BASE_SCRATCH_DIR=${BASE_SCRATCH_DIR:-$BASE_ROOT_DIR/ci/scratch} 33 | # What host to compile for. See also ./depends/README.md 34 | # Tests that need cross-compilation export the appropriate HOST. 35 | # Tests that run natively guess the host 36 | export HOST=${HOST:-$("$BASE_ROOT_DIR/depends/config.guess")} 37 | # Whether to prefer BusyBox over GNU utilities 38 | export USE_BUSY_BOX=${USE_BUSY_BOX:-false} 39 | 40 | export RUN_UNIT_TESTS=${RUN_UNIT_TESTS:-true} 41 | export RUN_FUNCTIONAL_TESTS=${RUN_FUNCTIONAL_TESTS:-true} 42 | export RUN_SECURITY_TESTS=${RUN_SECURITY_TESTS:-false} 43 | # By how much to scale the test_runner timeouts (option --timeout-factor). 44 | # This is needed because some ci machines have slow CPU or disk, so sanitizers 45 | # might be slow or a reindex might be waiting on disk IO. 46 | export TEST_RUNNER_TIMEOUT_FACTOR=${TEST_RUNNER_TIMEOUT_FACTOR:-40} 47 | export TEST_RUNNER_ENV=${TEST_RUNNER_ENV:-} 48 | export RUN_FUZZ_TESTS=${RUN_FUZZ_TESTS:-false} 49 | export EXPECTED_TESTS_DURATION_IN_SECONDS=${EXPECTED_TESTS_DURATION_IN_SECONDS:-1000} 50 | 51 | export CONTAINER_NAME=${CONTAINER_NAME:-ci_unnamed} 52 | export DOCKER_NAME_TAG=${DOCKER_NAME_TAG:-ubuntu:18.04} 53 | # Randomize test order. 54 | # See https://www.boost.org/doc/libs/1_71_0/libs/test/doc/html/boost_test/utf_reference/rt_param_reference/random.html 55 | export BOOST_TEST_RANDOM=${BOOST_TEST_RANDOM:-1} 56 | # See man 7 debconf 57 | export DEBIAN_FRONTEND=noninteractive 58 | export CCACHE_SIZE=${CCACHE_SIZE:-100M} 59 | export CCACHE_TEMPDIR=${CCACHE_TEMPDIR:-/tmp/.ccache-temp} 60 | export CCACHE_COMPRESS=${CCACHE_COMPRESS:-1} 61 | # The cache dir. 62 | # This folder exists on the ci host and ci guest. Changes are propagated back and forth. 63 | export CCACHE_DIR=${CCACHE_DIR:-$BASE_SCRATCH_DIR/.ccache} 64 | # The depends dir. 65 | # This folder exists on the ci host and ci guest. Changes are propagated back and forth. 66 | export DEPENDS_DIR=${DEPENDS_DIR:-$BASE_ROOT_DIR/depends} 67 | # Folder where the build result is put (bin and lib). 68 | export BASE_OUTDIR=${BASE_OUTDIR:-$BASE_SCRATCH_DIR/out/$HOST} 69 | # Folder where the build is done (dist and out-of-tree build). 70 | export BASE_BUILD_DIR=${BASE_BUILD_DIR:-$BASE_SCRATCH_DIR/build} 71 | export PREVIOUS_RELEASES_DIR=${PREVIOUS_RELEASES_DIR:-$BASE_ROOT_DIR/releases/$HOST} 72 | export SDK_URL=${SDK_URL:-https://bitcoincore.org/depends-sources/sdks} 73 | export DOCKER_PACKAGES=${DOCKER_PACKAGES:-build-essential libtool autotools-dev automake pkg-config bsdmainutils curl ca-certificates ccache python3 rsync git procps} 74 | export GOAL=${GOAL:-install} 75 | export PATH=${BASE_ROOT_DIR}/ci/retry:$PATH 76 | export CI_RETRY_EXE=${CI_RETRY_EXE:-"retry --"} 77 | -------------------------------------------------------------------------------- /ci/test/00_setup_env_arm.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2019-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | export HOST=arm-linux-gnueabihf 10 | # The host arch is unknown, so we run the tests through qemu. 11 | # If the host is arm and wants to run the tests natively, it can set QEMU_USER_CMD to the empty string. 12 | if [ -z ${QEMU_USER_CMD+x} ]; then export QEMU_USER_CMD="${QEMU_USER_CMD:-"qemu-arm -L /usr/arm-linux-gnueabihf/"}"; fi 13 | export DPKG_ADD_ARCH="armhf" 14 | export PACKAGES="python3-zmq g++-arm-linux-gnueabihf busybox libc6:armhf libstdc++6:armhf libfontconfig1:armhf libxcb1:armhf" 15 | if [ -n "$QEMU_USER_CMD" ]; then 16 | # Likely cross-compiling, so install the needed gcc and qemu-user 17 | export PACKAGES="$PACKAGES qemu-user" 18 | fi 19 | export CONTAINER_NAME=ci_arm_linux 20 | # Use debian to avoid 404 apt errors when cross compiling 21 | export DOCKER_NAME_TAG="debian:buster" 22 | export USE_BUSY_BOX=true 23 | export RUN_UNIT_TESTS=true 24 | export RUN_FUNCTIONAL_TESTS=false 25 | export GOAL="install" 26 | # -Wno-psabi is to disable ABI warnings: "note: parameter passing for argument of type ... changed in GCC 7.1" 27 | # This could be removed once the ABI change warning does not show up by default 28 | export BITCOIN_CONFIG="--enable-glibc-back-compat --enable-reduce-exports CXXFLAGS=-Wno-psabi --with-boost-process" 29 | -------------------------------------------------------------------------------- /ci/test/00_setup_env_i686_centos.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | export HOST=i686-pc-linux-gnu 10 | export CONTAINER_NAME=ci_i686_centos_8 11 | export DOCKER_NAME_TAG=centos:8 12 | export DOCKER_PACKAGES="gcc-c++ glibc-devel.x86_64 libstdc++-devel.x86_64 glibc-devel.i686 libstdc++-devel.i686 ccache libtool make git python3 python3-zmq which patch lbzip2 dash rsync coreutils" 13 | export GOAL="install" 14 | export BITCOIN_CONFIG="--enable-zmq --with-gui=qt5 --enable-reduce-exports --with-boost-process" 15 | export CONFIG_SHELL="/bin/dash" 16 | export TEST_RUNNER_ENV="LC_ALL=en_US.UTF-8" 17 | -------------------------------------------------------------------------------- /ci/test/00_setup_env_mac.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2019-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | export CONTAINER_NAME=ci_macos_cross 10 | export DOCKER_NAME_TAG=ubuntu:18.04 # Check that bionic can cross-compile to macos (bionic is used in the gitian build as well) 11 | export HOST=x86_64-apple-darwin18 12 | export PACKAGES="cmake imagemagick libcap-dev librsvg2-bin libz-dev libbz2-dev libtiff-tools python3-dev python3-setuptools xorriso" 13 | export XCODE_VERSION=11.3.1 14 | export XCODE_BUILD_ID=11C505 15 | export RUN_UNIT_TESTS=false 16 | export RUN_FUNCTIONAL_TESTS=false 17 | export GOAL="deploy" 18 | export BITCOIN_CONFIG="--with-gui --enable-reduce-exports --with-boost-process" 19 | -------------------------------------------------------------------------------- /ci/test/00_setup_env_mac_host.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2019-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | export HOST=x86_64-apple-darwin18 10 | export PIP_PACKAGES="zmq" 11 | export GOAL="install" 12 | export BITCOIN_CONFIG="--with-gui --enable-reduce-exports --with-boost-process" 13 | export CI_OS_NAME="macos" 14 | export OSX_SDK="" 15 | export CCACHE_SIZE=300M 16 | 17 | export RUN_SECURITY_TESTS="true" 18 | -------------------------------------------------------------------------------- /ci/test/00_setup_env_native_asan.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2019-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | export CONTAINER_NAME=ci_native_asan 10 | export PACKAGES="clang llvm python3-zmq qtbase5-dev qttools5-dev-tools libevent-dev bsdmainutils libboost-system-dev libboost-filesystem-dev libboost-test-dev libboost-thread-dev libdb5.3++-dev libminiupnpc-dev libnatpmp-dev libzmq3-dev libqrencode-dev libsqlite3-dev" 11 | export DOCKER_NAME_TAG=ubuntu:20.04 12 | export GOAL="install" 13 | export BITCOIN_CONFIG="--enable-zmq --with-incompatible-bdb --with-gui=qt5 CPPFLAGS='-DARENA_DEBUG -DDEBUG_LOCKORDER' --with-sanitizers=address,integer,undefined CC=clang CXX=clang++ --with-boost-process" 14 | -------------------------------------------------------------------------------- /ci/test/00_setup_env_native_msan.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | export DOCKER_NAME_TAG="ubuntu:20.04" 10 | LIBCXX_DIR="${BASE_ROOT_DIR}/ci/scratch/msan/build/" 11 | export MSAN_FLAGS="-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O1 -fno-optimize-sibling-calls" 12 | LIBCXX_FLAGS="-nostdinc++ -stdlib=libc++ -L${LIBCXX_DIR}lib -lc++abi -I${LIBCXX_DIR}include -I${LIBCXX_DIR}include/c++/v1 -lpthread -Wl,-rpath,${LIBCXX_DIR}lib -Wno-unused-command-line-argument" 13 | export MSAN_AND_LIBCXX_FLAGS="${MSAN_FLAGS} ${LIBCXX_FLAGS}" 14 | export BDB_PREFIX="${BASE_ROOT_DIR}/db4" 15 | 16 | export CONTAINER_NAME="ci_native_msan" 17 | export PACKAGES="clang-9 llvm-9 cmake" 18 | export DEP_OPTS="NO_BDB=1 NO_QT=1 CC='clang' CXX='clang++' CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}' boost_cxxflags='-std=c++17 -fvisibility=hidden -fPIC ${MSAN_AND_LIBCXX_FLAGS}' zeromq_cxxflags='-std=c++17 ${MSAN_AND_LIBCXX_FLAGS}'" 19 | export GOAL="install" 20 | export BITCOIN_CONFIG="--enable-wallet --with-sanitizers=memory --with-asm=no --prefix=${BASE_ROOT_DIR}/depends/x86_64-pc-linux-gnu/ CC=clang CXX=clang++ CFLAGS='${MSAN_FLAGS}' CXXFLAGS='${MSAN_AND_LIBCXX_FLAGS}' BDB_LIBS='-L${BDB_PREFIX}/lib -ldb_cxx-4.8' BDB_CFLAGS='-I${BDB_PREFIX}/include'" 21 | export USE_MEMORY_SANITIZER="true" 22 | export RUN_FUNCTIONAL_TESTS="false" 23 | export CCACHE_SIZE=250M 24 | -------------------------------------------------------------------------------- /ci/test/00_setup_env_native_tsan.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2019-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | export CONTAINER_NAME=ci_native_tsan 10 | export DOCKER_NAME_TAG=ubuntu:20.04 11 | export PACKAGES="clang llvm libc++abi-dev libc++-dev python3-zmq" 12 | export DEP_OPTS="CC=clang CXX='clang++ -stdlib=libc++'" 13 | export GOAL="install" 14 | export BITCOIN_CONFIG="--enable-zmq --with-gui=no CPPFLAGS='-DARENA_DEBUG -DDEBUG_LOCKORDER' CXXFLAGS='-g' --with-sanitizers=thread CC=clang CXX='clang++ -stdlib=libc++' --with-boost-process" 15 | -------------------------------------------------------------------------------- /ci/test/00_setup_env_native_valgrind.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2019-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | export CONTAINER_NAME=ci_native_valgrind 10 | export PACKAGES="valgrind clang llvm python3-zmq libevent-dev bsdmainutils libboost-system-dev libboost-filesystem-dev libboost-test-dev libboost-thread-dev libdb5.3++-dev libminiupnpc-dev libnatpmp-dev libzmq3-dev libsqlite3-dev" 11 | export USE_VALGRIND=1 12 | export TEST_RUNNER_EXTRA="--exclude rpc_bind" # Excluded for now, see https://github.com/bitcoin/bitcoin/issues/17765#issuecomment-602068547 13 | export GOAL="install" 14 | export BITCOIN_CONFIG="--enable-zmq --with-incompatible-bdb --with-gui=no CC=clang CXX=clang++" # TODO enable GUI 15 | -------------------------------------------------------------------------------- /ci/test/00_setup_env_s390x.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2019-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | export HOST=s390x-linux-gnu 10 | # The host arch is unknown, so we run the tests through qemu. 11 | # If the host is s390x and wants to run the tests natively, it can set QEMU_USER_CMD to the empty string. 12 | if [ -z ${QEMU_USER_CMD+x} ]; then export QEMU_USER_CMD="${QEMU_USER_CMD:-"qemu-s390x"}"; fi 13 | export PACKAGES="python3-zmq" 14 | if [ -n "$QEMU_USER_CMD" ]; then 15 | # Likely cross-compiling, so install the needed gcc and qemu-user 16 | export DPKG_ADD_ARCH="s390x" 17 | export PACKAGES="$PACKAGES g++-s390x-linux-gnu qemu-user libc6:s390x libstdc++6:s390x libfontconfig1:s390x libxcb1:s390x" 18 | fi 19 | # Use debian to avoid 404 apt errors 20 | export CONTAINER_NAME=ci_s390x 21 | export DOCKER_NAME_TAG="debian:buster" 22 | export RUN_UNIT_TESTS=true 23 | export TEST_RUNNER_ENV="LC_ALL=C" 24 | export RUN_FUNCTIONAL_TESTS=true 25 | export GOAL="install" 26 | export BITCOIN_CONFIG="--enable-reduce-exports --with-incompatible-bdb --with-boost-process" 27 | -------------------------------------------------------------------------------- /ci/test/00_setup_env_win64.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2019-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | export CONTAINER_NAME=ci_win64 10 | export DOCKER_NAME_TAG=ubuntu:18.04 # Check that bionic can cross-compile to win64 (bionic is used in the gitian build as well) 11 | export HOST=x86_64-w64-mingw32 12 | export PACKAGES="python3 nsis g++-mingw-w64-x86-64 wine-binfmt wine64 file" 13 | export RUN_FUNCTIONAL_TESTS=false 14 | export RUN_SECURITY_TESTS="true" 15 | export GOAL="deploy" 16 | export BITCOIN_CONFIG="--enable-reduce-exports --disable-gui-tests --without-boost-process" 17 | 18 | # Compiler for MinGW-w64 causes false -Wreturn-type warning. 19 | # See https://sourceforge.net/p/mingw-w64/bugs/306/ 20 | export NO_WERROR=1 21 | -------------------------------------------------------------------------------- /ci/test/05_before_script.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2018-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | # Make sure default datadir does not exist and is never read by creating a dummy file 10 | if [ "$CI_OS_NAME" == "macos" ]; then 11 | echo > $HOME/Library/Application\ Support/Bitcoin 12 | else 13 | DOCKER_EXEC echo \> \$HOME/.bitcoin 14 | fi 15 | 16 | DOCKER_EXEC mkdir -p ${DEPENDS_DIR}/SDKs ${DEPENDS_DIR}/sdk-sources 17 | 18 | OSX_SDK_BASENAME="Xcode-${XCODE_VERSION}-${XCODE_BUILD_ID}-extracted-SDK-with-libcxx-headers.tar.gz" 19 | OSX_SDK_PATH="${DEPENDS_DIR}/sdk-sources/${OSX_SDK_BASENAME}" 20 | 21 | if [ -n "$XCODE_VERSION" ] && [ ! -f "$OSX_SDK_PATH" ]; then 22 | curl --location --fail "${SDK_URL}/${OSX_SDK_BASENAME}" -o "$OSX_SDK_PATH" 23 | fi 24 | 25 | if [ -n "$XCODE_VERSION" ] && [ -f "$OSX_SDK_PATH" ]; then 26 | DOCKER_EXEC tar -C "${DEPENDS_DIR}/SDKs" -xf "$OSX_SDK_PATH" 27 | fi 28 | if [[ $HOST = *-mingw32 ]]; then 29 | DOCKER_EXEC update-alternatives --set $HOST-g++ \$\(which $HOST-g++-posix\) 30 | fi 31 | if [ -n "$PREVIOUS_RELEASES_TO_DOWNLOAD" ]; then 32 | DOCKER_EXEC test/get_previous_releases.py -b -t "$PREVIOUS_RELEASES_DIR" "${PREVIOUS_RELEASES_TO_DOWNLOAD}" 33 | fi 34 | -------------------------------------------------------------------------------- /ci/test/06_script_a.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2018-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | BITCOIN_CONFIG_ALL="--enable-suppress-external-warnings --disable-dependency-tracking --prefix=$DEPENDS_DIR/$HOST --bindir=$BASE_OUTDIR/bin --libdir=$BASE_OUTDIR/lib" 10 | if [ -z "$NO_WERROR" ]; then 11 | BITCOIN_CONFIG_ALL="${BITCOIN_CONFIG_ALL} --enable-werror" 12 | fi 13 | DOCKER_EXEC "ccache --zero-stats --max-size=$CCACHE_SIZE" 14 | 15 | if [ -n "$CONFIG_SHELL" ]; then 16 | DOCKER_EXEC "$CONFIG_SHELL" -c "./autogen.sh" 17 | else 18 | DOCKER_EXEC ./autogen.sh 19 | fi 20 | 21 | DOCKER_EXEC mkdir -p "${BASE_BUILD_DIR}" 22 | export P_CI_DIR="${BASE_BUILD_DIR}" 23 | 24 | DOCKER_EXEC "${BASE_ROOT_DIR}/configure" --cache-file=config.cache $BITCOIN_CONFIG_ALL $BITCOIN_CONFIG || ( (DOCKER_EXEC cat config.log) && false) 25 | 26 | export P_CI_DIR="${BASE_BUILD_DIR}" 27 | 28 | # DOCKER_EXEC ./configure --cache-file=../config.cache $BITCOIN_CONFIG_ALL $BITCOIN_CONFIG || ( (DOCKER_EXEC cat config.log) && false) 29 | 30 | set -o errtrace 31 | trap 'DOCKER_EXEC "cat ${BASE_SCRATCH_DIR}/sanitizer-output/* 2> /dev/null"' ERR 32 | 33 | if [[ ${USE_MEMORY_SANITIZER} == "true" ]]; then 34 | # MemorySanitizer (MSAN) does not support tracking memory initialization done by 35 | # using the Linux getrandom syscall. Avoid using getrandom by undefining 36 | # HAVE_SYS_GETRANDOM. See https://github.com/google/sanitizers/issues/852 for 37 | # details. 38 | DOCKER_EXEC 'grep -v HAVE_SYS_GETRANDOM src/config/bitcoin-config.h > src/config/bitcoin-config.h.tmp && mv src/config/bitcoin-config.h.tmp src/config/bitcoin-config.h' 39 | fi 40 | 41 | DOCKER_EXEC make $MAKEJOBS $GOAL || ( echo "Build failure. Verbose build follows." && DOCKER_EXEC make $GOAL V=1 ; false ) 42 | 43 | DOCKER_EXEC "ccache --version | head -n 1 && ccache --show-stats" 44 | DOCKER_EXEC du -sh "${DEPENDS_DIR}"/*/ 45 | DOCKER_EXEC du -sh "${PREVIOUS_RELEASES_DIR}" 46 | -------------------------------------------------------------------------------- /ci/test/06_script_b.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2018-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | if [[ $HOST = *-mingw32 ]]; then 10 | # Generate all binaries, so that they can be wrapped 11 | DOCKER_EXEC make $MAKEJOBS -C secp256k1 VERBOSE=1 12 | DOCKER_EXEC "${BASE_ROOT_DIR}/ci/test/wrap-wine.sh" 13 | fi 14 | 15 | if [ -n "$QEMU_USER_CMD" ]; then 16 | # Generate all binaries, so that they can be wrapped 17 | DOCKER_EXEC make $MAKEJOBS -C secp256k1 VERBOSE=1 18 | DOCKER_EXEC "${BASE_ROOT_DIR}/ci/test/wrap-qemu.sh" 19 | fi 20 | 21 | if [ -n "$USE_VALGRIND" ]; then 22 | DOCKER_EXEC "${BASE_ROOT_DIR}/ci/test/wrap-valgrind.sh" 23 | fi 24 | 25 | if [ "$RUN_UNIT_TESTS" = "true" ]; then 26 | DOCKER_EXEC ${TEST_RUNNER_ENV} "cp -r ${BASE_ROOT_DIR}/doc ${BASE_OUTDIR}" 27 | DOCKER_EXEC ${TEST_RUNNER_ENV} "echo \"base outdir = ${BASE_OUTDIR}\"" 28 | DOCKER_EXEC ${TEST_RUNNER_ENV} "LD_LIBRARY_PATH=$DEPENDS_DIR/$HOST/lib cd ${BASE_OUTDIR} && pwd && ls -l && ls -l bin && bin/test-btcdeb" 29 | fi 30 | -------------------------------------------------------------------------------- /ci/test/wrap-qemu.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2018-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | for b_name in {"${BASE_OUTDIR}/bin"/*,secp256k1/*tests}; do 10 | # shellcheck disable=SC2044 11 | for b in $(find "${BASE_ROOT_DIR}" -executable -type f -name $(basename $b_name)); do 12 | echo "Wrap $b ..." 13 | mv "$b" "${b}_orig" 14 | echo '#!/usr/bin/env bash' > "$b" 15 | echo "$QEMU_USER_CMD \"${b}_orig\" \"\$@\"" >> "$b" 16 | chmod +x "$b" 17 | done 18 | done 19 | -------------------------------------------------------------------------------- /ci/test/wrap-valgrind.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2018-2019 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | for b_name in "${BASE_OUTDIR}/bin"/*; do 10 | # shellcheck disable=SC2044 11 | for b in $(find "${BASE_ROOT_DIR}" -executable -type f -name $(basename $b_name)); do 12 | echo "Wrap $b ..." 13 | mv "$b" "${b}_orig" 14 | echo '#!/usr/bin/env bash' > "$b" 15 | echo "valgrind --gen-suppressions=all --quiet --error-exitcode=1 --suppressions=${BASE_ROOT_DIR}/contrib/valgrind.supp \"${b}_orig\" \"\$@\"" >> "$b" 16 | chmod +x "$b" 17 | done 18 | done 19 | -------------------------------------------------------------------------------- /ci/test/wrap-wine.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | for b_name in {"${BASE_OUTDIR}/bin"/*,secp256k1/*tests}.exe; do 10 | # shellcheck disable=SC2044 11 | for b in $(find "${BASE_ROOT_DIR}" -executable -type f -name "$(basename $b_name)"); do 12 | if (file "$b" | grep "Windows"); then 13 | echo "Wrap $b ..." 14 | mv "$b" "${b}_orig" 15 | echo '#!/usr/bin/env bash' > "$b" 16 | echo "wine64 \"${b}_orig\" \"\$@\"" >> "$b" 17 | chmod +x "$b" 18 | fi 19 | done 20 | done 21 | -------------------------------------------------------------------------------- /ci/test_run_all.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright (c) 2019-2020 The Bitcoin Core developers 4 | # Distributed under the MIT software license, see the accompanying 5 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. 6 | 7 | export LC_ALL=C.UTF-8 8 | 9 | set -o errexit; source ./ci/test/00_setup_env.sh 10 | set -o errexit; source ./ci/test/04_install.sh 11 | set -o errexit; source ./ci/test/05_before_script.sh 12 | set -o errexit; source ./ci/test/06_script_a.sh 13 | set -o errexit; source ./ci/test/06_script_b.sh 14 | -------------------------------------------------------------------------------- /cliargs.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2020 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 | #ifndef BITCOIN_CLIARGS_H 6 | #define BITCOIN_CLIARGS_H 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | enum cliarg_type { 18 | no_arg = no_argument, 19 | req_arg = required_argument, 20 | opt_arg = optional_argument, 21 | }; 22 | 23 | struct cliopt { 24 | char* longname; 25 | char shortname; 26 | cliarg_type type; 27 | cliopt(const char* longname_in, const char shortname_in, cliarg_type type_in) 28 | : longname(strdup(longname_in)) 29 | , shortname(shortname_in) 30 | , type(type_in) 31 | {} 32 | ~cliopt() { free(longname); } 33 | struct option get_option(std::string& opt) { 34 | opt += strprintf("%c%s", shortname, type == no_arg ? "" : ":"); 35 | return {longname, type, nullptr, shortname}; 36 | } 37 | }; 38 | 39 | struct cliargs { 40 | std::map m; 41 | std::vector l; 42 | std::vector long_options; 43 | 44 | ~cliargs() { 45 | while (!long_options.empty()) { 46 | delete long_options.back(); 47 | long_options.pop_back(); 48 | } 49 | } 50 | void add_option(const char* longname, const char shortname, cliarg_type t) { 51 | long_options.push_back(new cliopt(longname, shortname, t)); 52 | } 53 | void parse(int argc, char* const* argv) { 54 | struct option long_opts[long_options.size() + 1]; 55 | std::string opt = ""; 56 | for (size_t i = 0; i < long_options.size(); i++) { 57 | long_opts[i] = long_options[i]->get_option(opt); 58 | } 59 | long_opts[long_options.size()] = {0,0,0,0}; 60 | int c; 61 | int option_index = 0; 62 | for (;;) { 63 | c = getopt_long(argc, argv, opt.c_str(), long_opts, &option_index); 64 | if (c == -1) { 65 | break; 66 | } 67 | if (optarg) { 68 | m[c] = optarg; 69 | } else { 70 | m[c] = "1"; 71 | } 72 | } 73 | while (optind < argc) { 74 | l.push_back(argv[optind++]); 75 | } 76 | } 77 | }; 78 | 79 | /** 80 | * Parse a comma and/or space separated list of inputs into an existing set. 81 | */ 82 | inline void delimiter_set(const std::string& input, std::set& output) 83 | { 84 | size_t len = input.size(); 85 | std::string s; 86 | for (size_t j = 0; j <= len; ++j) { 87 | if (j == len || input[j] == ',' || input[j] == ' ') { 88 | if (s.empty()) continue; 89 | output.insert(s); 90 | s.clear(); 91 | } else s += tolower(input[j]); 92 | } 93 | } 94 | 95 | #endif // BITCOIN_CLIARGS_H 96 | -------------------------------------------------------------------------------- /compat.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2020 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | #ifndef BITCOIN_COMPAT_H 7 | #define BITCOIN_COMPAT_H 8 | 9 | #if defined(HAVE_CONFIG_H) 10 | #include 11 | #endif 12 | 13 | #ifdef WIN32 14 | #ifndef NOMINMAX 15 | #define NOMINMAX 16 | #endif 17 | #ifdef FD_SETSIZE 18 | #undef FD_SETSIZE // prevent redefinition compiler warning 19 | #endif 20 | #define FD_SETSIZE 1024 // max number of fds in fd_set 21 | #include 22 | #include 23 | #include 24 | #else 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #endif 39 | 40 | #ifndef WIN32 41 | typedef unsigned int SOCKET; 42 | #include 43 | #define WSAGetLastError() errno 44 | #define WSAEINVAL EINVAL 45 | #define WSAEALREADY EALREADY 46 | #define WSAEWOULDBLOCK EWOULDBLOCK 47 | #define WSAEAGAIN EAGAIN 48 | #define WSAEMSGSIZE EMSGSIZE 49 | #define WSAEINTR EINTR 50 | #define WSAEINPROGRESS EINPROGRESS 51 | #define WSAEADDRINUSE EADDRINUSE 52 | #define WSAENOTSOCK EBADF 53 | #define INVALID_SOCKET (SOCKET)(~0) 54 | #define SOCKET_ERROR -1 55 | #else 56 | #ifndef WSAEAGAIN 57 | #ifdef EAGAIN 58 | #define WSAEAGAIN EAGAIN 59 | #else 60 | #define WSAEAGAIN WSAEWOULDBLOCK 61 | #endif 62 | #endif 63 | #endif 64 | 65 | #ifdef WIN32 66 | #ifndef S_IRUSR 67 | #define S_IRUSR 0400 68 | #define S_IWUSR 0200 69 | #endif 70 | #else 71 | #define MAX_PATH 1024 72 | #endif 73 | #ifdef _MSC_VER 74 | #if !defined(ssize_t) 75 | #ifdef _WIN64 76 | typedef int64_t ssize_t; 77 | #else 78 | typedef int32_t ssize_t; 79 | #endif 80 | #endif 81 | #endif 82 | 83 | #if HAVE_DECL_STRNLEN == 0 84 | size_t strnlen( const char *start, size_t max_len); 85 | #endif // HAVE_DECL_STRNLEN 86 | 87 | #ifndef WIN32 88 | typedef void* sockopt_arg_type; 89 | #else 90 | typedef char* sockopt_arg_type; 91 | #endif 92 | 93 | // Note these both should work with the current usage of poll, but best to be safe 94 | // WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/ 95 | // __APPLE__ poll is broke https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408 96 | #if defined(__linux__) 97 | #define USE_POLL 98 | #endif 99 | 100 | bool static inline IsSelectableSocket(const SOCKET& s) { 101 | #if defined(USE_POLL) || defined(WIN32) 102 | return true; 103 | #else 104 | return (s < FD_SETSIZE); 105 | #endif 106 | } 107 | 108 | // MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define it as 0 109 | #if !defined(MSG_NOSIGNAL) 110 | #define MSG_NOSIGNAL 0 111 | #endif 112 | 113 | // MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0 114 | #if !defined(MSG_DONTWAIT) 115 | #define MSG_DONTWAIT 0 116 | #endif 117 | 118 | #endif // BITCOIN_COMPAT_H 119 | -------------------------------------------------------------------------------- /compat/byteswap.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2019 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 | #ifndef BITCOIN_COMPAT_BYTESWAP_H 6 | #define BITCOIN_COMPAT_BYTESWAP_H 7 | 8 | #if defined(HAVE_CONFIG_H) 9 | #include 10 | #endif 11 | 12 | #include 13 | 14 | #if defined(HAVE_BYTESWAP_H) 15 | #include 16 | #endif 17 | 18 | #if defined(MAC_OSX) 19 | 20 | #include 21 | #define bswap_16(x) OSSwapInt16(x) 22 | #define bswap_32(x) OSSwapInt32(x) 23 | #define bswap_64(x) OSSwapInt64(x) 24 | 25 | #else 26 | // Non-MacOS / non-Darwin 27 | 28 | #if HAVE_DECL_BSWAP_16 == 0 29 | inline uint16_t bswap_16(uint16_t x) 30 | { 31 | return (x >> 8) | (x << 8); 32 | } 33 | #endif // HAVE_DECL_BSWAP16 == 0 34 | 35 | #if HAVE_DECL_BSWAP_32 == 0 36 | inline uint32_t bswap_32(uint32_t x) 37 | { 38 | return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) | 39 | ((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24)); 40 | } 41 | #endif // HAVE_DECL_BSWAP32 == 0 42 | 43 | #if HAVE_DECL_BSWAP_64 == 0 44 | inline uint64_t bswap_64(uint64_t x) 45 | { 46 | return (((x & 0xff00000000000000ull) >> 56) 47 | | ((x & 0x00ff000000000000ull) >> 40) 48 | | ((x & 0x0000ff0000000000ull) >> 24) 49 | | ((x & 0x000000ff00000000ull) >> 8) 50 | | ((x & 0x00000000ff000000ull) << 8) 51 | | ((x & 0x0000000000ff0000ull) << 24) 52 | | ((x & 0x000000000000ff00ull) << 40) 53 | | ((x & 0x00000000000000ffull) << 56)); 54 | } 55 | #endif // HAVE_DECL_BSWAP64 == 0 56 | 57 | #endif // defined(MAC_OSX) 58 | 59 | #endif // BITCOIN_COMPAT_BYTESWAP_H 60 | -------------------------------------------------------------------------------- /compat/cpuid.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017-2019 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 | #ifndef BITCOIN_COMPAT_CPUID_H 6 | #define BITCOIN_COMPAT_CPUID_H 7 | 8 | #if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) 9 | #define HAVE_GETCPUID 10 | 11 | #include 12 | 13 | #include 14 | 15 | // We can't use cpuid.h's __get_cpuid as it does not support subleafs. 16 | void static inline GetCPUID(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d) 17 | { 18 | #ifdef __GNUC__ 19 | __cpuid_count(leaf, subleaf, a, b, c, d); 20 | #else 21 | __asm__ ("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(leaf), "2"(subleaf)); 22 | #endif 23 | } 24 | 25 | #endif // defined(__x86_64__) || defined(__amd64__) || defined(__i386__) 26 | #endif // BITCOIN_COMPAT_CPUID_H 27 | -------------------------------------------------------------------------------- /config/EMPTY: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bitcoin-core/btcdeb/e2c2e7b9fe2ecc0884129b53813a733f93a6e2c7/config/EMPTY -------------------------------------------------------------------------------- /consensus/amount.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto 2 | // Copyright (c) 2009-2021 The Bitcoin Core developers 3 | // Distributed under the MIT software license, see the accompanying 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 | 6 | #ifndef BITCOIN_CONSENSUS_AMOUNT_H 7 | #define BITCOIN_CONSENSUS_AMOUNT_H 8 | 9 | #include 10 | 11 | /** Amount in satoshis (Can be negative) */ 12 | typedef int64_t CAmount; 13 | 14 | /** The amount of satoshis in one BTC. */ 15 | static constexpr CAmount COIN = 100000000; 16 | 17 | /** No amount larger than this (in satoshi) is valid. 18 | * 19 | * Note that this constant is *not* the total money supply, which in Bitcoin 20 | * currently happens to be less than 21,000,000 BTC for various reasons, but 21 | * rather a sanity check. As this sanity check is used by consensus-critical 22 | * validation code, the exact value of the MAX_MONEY constant is consensus 23 | * critical; in unusual circumstances like a(nother) overflow bug that allowed 24 | * for the creation of coins out of thin air modification could lead to a fork. 25 | * */ 26 | static constexpr CAmount MAX_MONEY = 21000000 * COIN; 27 | inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); } 28 | 29 | #endif // BITCOIN_CONSENSUS_AMOUNT_H 30 | -------------------------------------------------------------------------------- /consensus/merkle.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2020 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 6 | #include 7 | 8 | /* WARNING! If you're reading this because you're learning about crypto 9 | and/or designing a new system that will use merkle trees, keep in mind 10 | that the following merkle tree algorithm has a serious flaw related to 11 | duplicate txids, resulting in a vulnerability (CVE-2012-2459). 12 | 13 | The reason is that if the number of hashes in the list at a given level 14 | is odd, the last one is duplicated before computing the next level (which 15 | is unusual in Merkle trees). This results in certain sequences of 16 | transactions leading to the same merkle root. For example, these two 17 | trees: 18 | 19 | A A 20 | / \ / \ 21 | B C B C 22 | / \ | / \ / \ 23 | D E F D E F F 24 | / \ / \ / \ / \ / \ / \ / \ 25 | 1 2 3 4 5 6 1 2 3 4 5 6 5 6 26 | 27 | for transaction lists [1,2,3,4,5,6] and [1,2,3,4,5,6,5,6] (where 5 and 28 | 6 are repeated) result in the same root hash A (because the hash of both 29 | of (F) and (F,F) is C). 30 | 31 | The vulnerability results from being able to send a block with such a 32 | transaction list, with the same merkle root, and the same block hash as 33 | the original without duplication, resulting in failed validation. If the 34 | receiving node proceeds to mark that block as permanently invalid 35 | however, it will fail to accept further unmodified (and thus potentially 36 | valid) versions of the same block. We defend against this by detecting 37 | the case where we would hash two identical hashes at the end of the list 38 | together, and treating that identically to the block having an invalid 39 | merkle root. Assuming no double-SHA256 collisions, this will detect all 40 | known ways of changing the transactions without affecting the merkle 41 | root. 42 | */ 43 | 44 | 45 | uint256 ComputeMerkleRoot(std::vector hashes, bool* mutated) { 46 | bool mutation = false; 47 | while (hashes.size() > 1) { 48 | if (mutated) { 49 | for (size_t pos = 0; pos + 1 < hashes.size(); pos += 2) { 50 | if (hashes[pos] == hashes[pos + 1]) mutation = true; 51 | } 52 | } 53 | if (hashes.size() & 1) { 54 | hashes.push_back(hashes.back()); 55 | } 56 | SHA256D64(hashes[0].begin(), hashes[0].begin(), hashes.size() / 2); 57 | hashes.resize(hashes.size() / 2); 58 | } 59 | if (mutated) *mutated = mutation; 60 | if (hashes.size() == 0) return uint256(); 61 | return hashes[0]; 62 | } 63 | 64 | 65 | // uint256 BlockMerkleRoot(const CBlock& block, bool* mutated) 66 | // { 67 | // std::vector leaves; 68 | // leaves.resize(block.vtx.size()); 69 | // for (size_t s = 0; s < block.vtx.size(); s++) { 70 | // leaves[s] = block.vtx[s]->GetHash(); 71 | // } 72 | // return ComputeMerkleRoot(std::move(leaves), mutated); 73 | // } 74 | 75 | // uint256 BlockWitnessMerkleRoot(const CBlock& block, bool* mutated) 76 | // { 77 | // std::vector leaves; 78 | // leaves.resize(block.vtx.size()); 79 | // leaves[0].SetNull(); // The witness hash of the coinbase is 0. 80 | // for (size_t s = 1; s < block.vtx.size(); s++) { 81 | // leaves[s] = block.vtx[s]->GetWitnessHash(); 82 | // } 83 | // return ComputeMerkleRoot(std::move(leaves), mutated); 84 | // } 85 | 86 | -------------------------------------------------------------------------------- /consensus/merkle.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2019 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 | #ifndef BITCOIN_CONSENSUS_MERKLE_H 6 | #define BITCOIN_CONSENSUS_MERKLE_H 7 | 8 | #include 9 | 10 | // #include 11 | #include 12 | 13 | uint256 ComputeMerkleRoot(std::vector hashes, bool* mutated = nullptr); 14 | 15 | /* 16 | * Compute the Merkle root of the transactions in a block. 17 | * *mutated is set to true if a duplicated subtree was found. 18 | */ 19 | // uint256 BlockMerkleRoot(const CBlock& block, bool* mutated = nullptr); 20 | 21 | /* 22 | * Compute the Merkle root of the witness transactions in a block. 23 | * *mutated is set to true if a duplicated subtree was found. 24 | */ 25 | // uint256 BlockWitnessMerkleRoot(const CBlock& block, bool* mutated = nullptr); 26 | 27 | #endif // BITCOIN_CONSENSUS_MERKLE_H 28 | -------------------------------------------------------------------------------- /crypto/common.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2020 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 | #ifndef BITCOIN_CRYPTO_COMMON_H 6 | #define BITCOIN_CRYPTO_COMMON_H 7 | 8 | #if defined(HAVE_CONFIG_H) 9 | #include 10 | #endif 11 | 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | uint16_t static inline ReadLE16(const unsigned char* ptr) 18 | { 19 | uint16_t x; 20 | memcpy((char*)&x, ptr, 2); 21 | return le16toh(x); 22 | } 23 | 24 | uint32_t static inline ReadLE32(const unsigned char* ptr) 25 | { 26 | uint32_t x; 27 | memcpy((char*)&x, ptr, 4); 28 | return le32toh(x); 29 | } 30 | 31 | uint64_t static inline ReadLE64(const unsigned char* ptr) 32 | { 33 | uint64_t x; 34 | memcpy((char*)&x, ptr, 8); 35 | return le64toh(x); 36 | } 37 | 38 | void static inline WriteLE16(unsigned char* ptr, uint16_t x) 39 | { 40 | uint16_t v = htole16(x); 41 | memcpy(ptr, (char*)&v, 2); 42 | } 43 | 44 | void static inline WriteLE32(unsigned char* ptr, uint32_t x) 45 | { 46 | uint32_t v = htole32(x); 47 | memcpy(ptr, (char*)&v, 4); 48 | } 49 | 50 | void static inline WriteLE64(unsigned char* ptr, uint64_t x) 51 | { 52 | uint64_t v = htole64(x); 53 | memcpy(ptr, (char*)&v, 8); 54 | } 55 | 56 | uint16_t static inline ReadBE16(const unsigned char* ptr) 57 | { 58 | uint16_t x; 59 | memcpy((char*)&x, ptr, 2); 60 | return be16toh(x); 61 | } 62 | 63 | uint32_t static inline ReadBE32(const unsigned char* ptr) 64 | { 65 | uint32_t x; 66 | memcpy((char*)&x, ptr, 4); 67 | return be32toh(x); 68 | } 69 | 70 | uint64_t static inline ReadBE64(const unsigned char* ptr) 71 | { 72 | uint64_t x; 73 | memcpy((char*)&x, ptr, 8); 74 | return be64toh(x); 75 | } 76 | 77 | void static inline WriteBE32(unsigned char* ptr, uint32_t x) 78 | { 79 | uint32_t v = htobe32(x); 80 | memcpy(ptr, (char*)&v, 4); 81 | } 82 | 83 | void static inline WriteBE64(unsigned char* ptr, uint64_t x) 84 | { 85 | uint64_t v = htobe64(x); 86 | memcpy(ptr, (char*)&v, 8); 87 | } 88 | 89 | /** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */ 90 | uint64_t static inline CountBits(uint64_t x) 91 | { 92 | #if HAVE_BUILTIN_CLZL 93 | if (sizeof(unsigned long) >= sizeof(uint64_t)) { 94 | return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0; 95 | } 96 | #endif 97 | #if HAVE_BUILTIN_CLZLL 98 | if (sizeof(unsigned long long) >= sizeof(uint64_t)) { 99 | return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0; 100 | } 101 | #endif 102 | int ret = 0; 103 | while (x) { 104 | x >>= 1; 105 | ++ret; 106 | } 107 | return ret; 108 | } 109 | 110 | #endif // BITCOIN_CRYPTO_COMMON_H 111 | -------------------------------------------------------------------------------- /crypto/hmac_sha512.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2018 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 6 | 7 | #include 8 | 9 | CHMAC_SHA512::CHMAC_SHA512(const unsigned char* key, size_t keylen) 10 | { 11 | unsigned char rkey[128]; 12 | if (keylen <= 128) { 13 | memcpy(rkey, key, keylen); 14 | memset(rkey + keylen, 0, 128 - keylen); 15 | } else { 16 | CSHA512().Write(key, keylen).Finalize(rkey); 17 | memset(rkey + 64, 0, 64); 18 | } 19 | 20 | for (int n = 0; n < 128; n++) 21 | rkey[n] ^= 0x5c; 22 | outer.Write(rkey, 128); 23 | 24 | for (int n = 0; n < 128; n++) 25 | rkey[n] ^= 0x5c ^ 0x36; 26 | inner.Write(rkey, 128); 27 | } 28 | 29 | void CHMAC_SHA512::Finalize(unsigned char hash[OUTPUT_SIZE]) 30 | { 31 | unsigned char temp[64]; 32 | inner.Finalize(temp); 33 | outer.Write(temp, 64).Finalize(hash); 34 | } 35 | -------------------------------------------------------------------------------- /crypto/hmac_sha512.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2018 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 | #ifndef BITCOIN_CRYPTO_HMAC_SHA512_H 6 | #define BITCOIN_CRYPTO_HMAC_SHA512_H 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | /** A hasher class for HMAC-SHA-512. */ 14 | class CHMAC_SHA512 15 | { 16 | private: 17 | CSHA512 outer; 18 | CSHA512 inner; 19 | 20 | public: 21 | static const size_t OUTPUT_SIZE = 64; 22 | 23 | CHMAC_SHA512(const unsigned char* key, size_t keylen); 24 | CHMAC_SHA512& Write(const unsigned char* data, size_t len) 25 | { 26 | inner.Write(data, len); 27 | return *this; 28 | } 29 | void Finalize(unsigned char hash[OUTPUT_SIZE]); 30 | }; 31 | 32 | #endif // BITCOIN_CRYPTO_HMAC_SHA512_H 33 | -------------------------------------------------------------------------------- /crypto/ripemd160.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2016 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 | #ifndef BITCOIN_CRYPTO_RIPEMD160_H 6 | #define BITCOIN_CRYPTO_RIPEMD160_H 7 | 8 | #include 9 | #include 10 | 11 | /** A hasher class for RIPEMD-160. */ 12 | class CRIPEMD160 13 | { 14 | private: 15 | uint32_t s[5]; 16 | unsigned char buf[64]; 17 | uint64_t bytes; 18 | 19 | public: 20 | static const size_t OUTPUT_SIZE = 20; 21 | 22 | CRIPEMD160(); 23 | CRIPEMD160& Write(const unsigned char* data, size_t len); 24 | void Finalize(unsigned char hash[OUTPUT_SIZE]); 25 | CRIPEMD160& Reset(); 26 | }; 27 | 28 | #endif // BITCOIN_CRYPTO_RIPEMD160_H 29 | -------------------------------------------------------------------------------- /crypto/sha1.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2016 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 | #ifndef BITCOIN_CRYPTO_SHA1_H 6 | #define BITCOIN_CRYPTO_SHA1_H 7 | 8 | #include 9 | #include 10 | 11 | /** A hasher class for SHA1. */ 12 | class CSHA1 13 | { 14 | private: 15 | uint32_t s[5]; 16 | unsigned char buf[64]; 17 | uint64_t bytes; 18 | 19 | public: 20 | static const size_t OUTPUT_SIZE = 20; 21 | 22 | CSHA1(); 23 | CSHA1& Write(const unsigned char* data, size_t len); 24 | void Finalize(unsigned char hash[OUTPUT_SIZE]); 25 | CSHA1& Reset(); 26 | }; 27 | 28 | #endif // BITCOIN_CRYPTO_SHA1_H 29 | -------------------------------------------------------------------------------- /crypto/sha256.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2018 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 | #ifndef BITCOIN_CRYPTO_SHA256_H 6 | #define BITCOIN_CRYPTO_SHA256_H 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | /** A hasher class for SHA-256. */ 13 | class CSHA256 14 | { 15 | private: 16 | uint32_t s[8]; 17 | unsigned char buf[64]; 18 | uint64_t bytes; 19 | 20 | public: 21 | static const size_t OUTPUT_SIZE = 32; 22 | 23 | CSHA256(); 24 | CSHA256& Write(const unsigned char* data, size_t len); 25 | void Finalize(unsigned char hash[OUTPUT_SIZE]); 26 | CSHA256& Reset(); 27 | }; 28 | 29 | /** Autodetect the best available SHA256 implementation. 30 | * Returns the name of the implementation. 31 | */ 32 | std::string SHA256AutoDetect(); 33 | 34 | /** Compute multiple double-SHA256's of 64-byte blobs. 35 | * output: pointer to a blocks*32 byte output buffer 36 | * input: pointer to a blocks*64 byte input buffer 37 | * blocks: the number of hashes to compute. 38 | */ 39 | void SHA256D64(unsigned char* output, const unsigned char* input, size_t blocks); 40 | 41 | #endif // BITCOIN_CRYPTO_SHA256_H 42 | -------------------------------------------------------------------------------- /crypto/sha512.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2014-2019 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 | #ifndef BITCOIN_CRYPTO_SHA512_H 6 | #define BITCOIN_CRYPTO_SHA512_H 7 | 8 | #include 9 | #include 10 | 11 | /** A hasher class for SHA-512. */ 12 | class CSHA512 13 | { 14 | private: 15 | uint64_t s[8]; 16 | unsigned char buf[128]; 17 | uint64_t bytes; 18 | 19 | public: 20 | static constexpr size_t OUTPUT_SIZE = 64; 21 | 22 | CSHA512(); 23 | CSHA512& Write(const unsigned char* data, size_t len); 24 | void Finalize(unsigned char hash[OUTPUT_SIZE]); 25 | CSHA512& Reset(); 26 | uint64_t Size() const { return bytes; } 27 | }; 28 | 29 | #endif // BITCOIN_CRYPTO_SHA512_H 30 | -------------------------------------------------------------------------------- /datasets.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020 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 | #ifndef BITCOIN_DATASETS_H 6 | #define BITCOIN_DATASETS_H 7 | 8 | #include 9 | #include 10 | 11 | inline std::string string_from_file(const std::string& path) { 12 | FILE* fp = fopen(path.c_str(), "r"); 13 | if (!fp) throw std::runtime_error("unable to open path " + path); 14 | char* buf = (char*)malloc(128); 15 | size_t cap = 128; 16 | size_t idx = 0; 17 | long count; 18 | while (0 < (count = fread(&buf[idx], 1, cap - idx, fp))) { 19 | idx += count; 20 | if (idx < cap) break; 21 | cap <<= 1; 22 | buf = (char*)realloc(buf, cap); 23 | } 24 | buf[idx] = 0; 25 | std::string r = buf; 26 | free(buf); 27 | fclose(fp); 28 | return r; 29 | } 30 | 31 | inline void process_datasets(std::map& m, bool verbose) { 32 | std::string dataset = m['X']; 33 | if (dataset == "1") { 34 | printf("Available datasets:\n"); 35 | printf(" p2pkh A legacy pay-to-pubkey-hash spend from Oct 7, 2020\n"); 36 | if (verbose) printf( 37 | " funding txid = cdc44e86eececa6d726cc93cea4e176fe6191b695444467a3b2bcdfbe64aac02\n" 38 | " spending txid = ad7941ba6a7f8f395638233a3dd20a2779c66da516c5b9c9ff4f3d65f2057e3c\n\n" 39 | ); 40 | printf(" p2sh-p2wpkh A non-native Segwit pubkey-hash spend from Aug 24, 2017\n"); 41 | if (verbose) printf( 42 | " funding txid = 42f7d0545ef45bd3b9cfee6b170cf6314a3bd8b3f09b610eeb436d92993ad440\n" 43 | " spending txid = c586389e5e4b3acb9d6c8be1c19ae8ab2795397633176f5a6442a261bbdefc3a\n\n" 44 | ); 45 | printf(" p2sh-multisig-2-of-2 A 2-of-2 legacy multisig spend from Oct 6, 2020\n"); 46 | if (verbose) printf( 47 | " funding txid = c55c9e0afa43f06e6e9c9277c7fe9768acf3b7b85d61b35770fc38f5f612f76d\n" 48 | " spending txid = 245ddb8e1bf5784ceb9d981ffbaae02fb8a73c552dfe23bce50b613b3acbdd62\n\n" 49 | ); 50 | printf(" p2sh-multisig-invalid-order An invalid multisig, where the signatures are correct, but they've been inserted in the wrong order\n"); 51 | if (verbose) printf( 52 | " funding txid = 7e69687c94c57a878cf711a39870383c6fe93b420f26184a21d020d8ace2df83\n" 53 | " spending txid = 56ae0d780759b8126a3eb9be605b1a6e48acb326af527547205d6749afab1a61\n" 54 | ); 55 | printf(" p2tr A standard taproot spend transaction (see example in documents)\n"); 56 | printf(" p2ts A standard tapscript spend transaction (see example in documents)\n"); 57 | exit(0); 58 | } 59 | try { 60 | if (!m.count('x')) { 61 | // populate --tx from dataset 62 | std::string data = string_from_file(std::string("doc/txs/") + dataset + "-tx"); 63 | m['x'] = data; 64 | if (verbose) printf("loaded spending (output) transaction from dataset %s: %s\n", dataset.c_str(), data.c_str()); 65 | } 66 | if (!m.count('i')) { 67 | // populate --txin from dataset 68 | std::string data = string_from_file(std::string("doc/txs/") + dataset + "-in"); 69 | m['i'] = data; 70 | if (verbose) printf("loaded funding (input) transaction from dataset %s: %s\n", dataset.c_str(), data.c_str()); 71 | } 72 | } catch (const std::runtime_error& err) { 73 | fprintf(stderr, "error loading from dataset \"%s\": %s\n", dataset.c_str(), err.what()); 74 | exit(1); 75 | } 76 | } 77 | 78 | #endif // BITCOIN_DATASETS_H 79 | -------------------------------------------------------------------------------- /debugger/hash.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018 Karl-Johan Alm 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 6 | 7 | -------------------------------------------------------------------------------- /debugger/interpreter.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018 Karl-Johan Alm 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_BTCDEB_INTERPRETER_H 6 | #define BITCOIN_BTCDEB_INTERPRETER_H 7 | 8 | #include 9 | #include // HexStr 10 | #include // XOnlyPubKey 11 | #include 12 | #include 13 | 14 | template static inline void print_vec(const T& v, T2 fun) { 15 | for (unsigned char c : v) fun("%02x", c); 16 | } 17 | 18 | static inline std::string hashtype_str(int h) { 19 | char buf[100]; 20 | char* pbuf = buf; 21 | if ((h & 0x1f) == SIGHASH_ALL) pbuf += snprintf(pbuf, 100 + buf - pbuf, " SIGHASH_ALL"); 22 | if ((h & 0x1f) == SIGHASH_NONE) pbuf += snprintf(pbuf, 100 + buf - pbuf, " SIGHASH_NONE"); 23 | if ((h & 0x1f) == SIGHASH_SINGLE) pbuf += snprintf(pbuf, 100 + buf - pbuf, " SIGHASH_SINGLE"); 24 | if (h & SIGHASH_ANYONECANPAY) pbuf += snprintf(pbuf, 100 + buf - pbuf, " SIGHASH_ANYONECANPAY"); 25 | return &buf[1]; 26 | } 27 | 28 | typedef std::vector valtype; 29 | typedef std::vector stack_type; 30 | 31 | inline bool set_success(ScriptError* ret) 32 | { 33 | if (ret) 34 | *ret = SCRIPT_ERR_OK; 35 | return true; 36 | } 37 | 38 | inline bool set_error(ScriptError* ret, const ScriptError serror) 39 | { 40 | if (ret) 41 | *ret = serror; 42 | return false; 43 | } 44 | 45 | static inline void _popstack(std::vector& stack) 46 | { 47 | if (stack.empty()) 48 | throw std::runtime_error("popstack(): stack empty"); 49 | stack.pop_back(); 50 | } 51 | 52 | #define popstack(stack) do { btc_logf("\t\t<> POP " #stack "\n"); _popstack(stack); } while (0) 53 | #define pushstack(stack, v) do { stack.push_back(v); btc_logf("\t\t<> PUSH " #stack " %s\n", HexStr(stack.at(stack.size()-1)).c_str()); } while (0) 54 | 55 | struct TaprootCommitmentEnv { 56 | enum class State : uint8_t { 57 | Processing, 58 | Failed, 59 | Tweaked, 60 | Done, 61 | }; 62 | const std::vector m_control; 63 | const std::vector m_program; 64 | const CScript m_script; 65 | uint256* m_tapleaf_hash; 66 | int m_path_len; 67 | XOnlyPubKey m_p; 68 | XOnlyPubKey m_q; 69 | uint256 m_k; 70 | std::string m_k_desc; 71 | int m_i; 72 | TaprootCommitmentEnv(const std::vector& control, const std::vector& program, const CScript& script, uint256* tapleaf_hash); 73 | State Iterate(); 74 | std::vector Description(); 75 | bool m_applied_tweak; 76 | }; 77 | 78 | struct InterpreterEnv : public ScriptExecutionEnvironment { 79 | CScript::const_iterator pc; 80 | std::vector stack_history; 81 | std::vector altstack_history; 82 | std::vector pc_history; 83 | std::vector nOpCount_history; 84 | std::vector script_history; 85 | const CScript& scriptIn; 86 | int curr_op_seq; 87 | bool fRequireMinimal; 88 | bool operational; 89 | bool done; 90 | InterpreterEnv(stack_type& stack_in, const CScript& script_in, unsigned int flags_in, const BaseSignatureChecker& checker_in, SigVersion sigversion_in, ScriptError* error_in = nullptr); 91 | 92 | // P2SH support 93 | bool is_p2sh; 94 | stack_type p2shstack; 95 | 96 | // Executed sigScript support (archaeology) 97 | CScript successor_script; 98 | 99 | // Taproot/tapscript support 100 | TaprootCommitmentEnv* tce; 101 | }; 102 | 103 | bool StepScript(InterpreterEnv& env); 104 | bool ContinueScript(InterpreterEnv& env); 105 | bool RewindScript(InterpreterEnv& env); 106 | 107 | #endif // BITCOIN_BTCDEB_INTERPRETER_H 108 | -------------------------------------------------------------------------------- /debugger/script.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2018 Karl-Johan Alm 2 | // Distributed under the MIT software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #ifndef BITCOIN_BTCDEB_SCRIPT_H 6 | #define BITCOIN_BTCDEB_SCRIPT_H 7 | 8 | #include