From 9b7b2c86bc26c0af2afbb4d24c2a7105c4aaf41b Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Sun, 16 Jan 2022 19:48:31 -0500 Subject: [PATCH 1/2] equihdr tests --- src/Makefile.ktest.include | 1 + src/test-komodo/test_nspv.cpp | 155 ++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 src/test-komodo/test_nspv.cpp diff --git a/src/Makefile.ktest.include b/src/Makefile.ktest.include index 04852a8beb7..ecb828c7395 100644 --- a/src/Makefile.ktest.include +++ b/src/Makefile.ktest.include @@ -25,6 +25,7 @@ komodo_test_SOURCES = \ test-komodo/test_pow.cpp \ test-komodo/test_txid.cpp \ test-komodo/test_coins.cpp \ + test-komodo/test_nspv.cpp \ main.cpp komodo_test_CPPFLAGS = $(komodod_CPPFLAGS) diff --git a/src/test-komodo/test_nspv.cpp b/src/test-komodo/test_nspv.cpp new file mode 100644 index 00000000000..c9162b11c28 --- /dev/null +++ b/src/test-komodo/test_nspv.cpp @@ -0,0 +1,155 @@ +#include "nSPV/nspv_defs.h" +#include "streams.h" + +#include +#include +#include +#include +#include +#include + +namespace TestNspv +{ + +/** + * Convenience functions for testing + */ + +bool compare_vector(std::vector a, std::vector b) +{ + if (a.size() != b.size()) + { + std::cerr << "A size: " << a.size() << " but B size: " << b.size() << std::endl; + return false; + } + for(size_t i = 0; i < a.size(); ++i) + { + if (a[i] != b[i]) + { + std::cerr << "Error at position " << i << " A has value of " << (unsigned int)a[i] << " but B has value of " << (unsigned int)b[i] << std::endl; + return false; + } + } + return true; +} + +void print_vector(std::vector in, std::ostream& out) +{ + for( auto itr = in.begin(); itr != in.end(); ++itr) + { + char temp[512]; + sprintf(temp, "%02x", (unsigned int)(*itr)); + out << temp; + } +} + +/**** + * Convert string of 2 digit hex values into form usable in code + */ +void print_string(const std::string& in, std::ostream& out) +{ + bool first = true; + + for(size_t i = 0; i < in.length(); i += 2) + { + if (!first) + { + out << ", "; + first = false; + } + out << "0x" << in.substr(i,2); + } + return; +} + +std::vector hex_str_to_vec(const std::string& in) +{ + std::vector retVal; + + for(size_t i = 0; i < in.length(); i += 2) + { + retVal.push_back(strtol(in.substr(i,2).c_str(), nullptr, 16 )); + } + return retVal; +} + +std::string hex_str_to_text_str(const std::string& in) +{ + std::stringstream ss; + for( size_t i = 0; i < in.size(); ++i) + { + char temp[10]; + sprintf(temp, "%02x", in[i]); + ss << temp; + } + return ss.str(); +} + +TEST(TestNspv, EquiHdrV4) +{ + std::string expected_string = "0400000001010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202040404040404040404040404040404040404040404040404040404040404040401000000020000000303030303030303030303030303030303030303030303030303030303030303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + std::vector expected = hex_str_to_vec(expected_string); + NSPV_equihdr old(4); + old.nTime = 1; + old.nBits = 2; + old.nNonce.SetHex("0303030303030303030303030303030303030303030303030303030303030303"); + old.nVersion = 4; + old.hashPrevBlock.SetHex("0101010101010101010101010101010101010101010101010101010101010101"); + old.hashMerkleRoot.SetHex("020202020202020202020202020202020202020202020202020202020202020202"); + old.hashFinalSaplingRoot.SetHex("0404040404040404040404040404040404040404040404040404040404040404"); + old.nSolution = std::vector(1344); // in versions < 5, nSolution is always 1344 + CDataStream ds(0, 0); + old.Serialize(ds); // now it is in a memory buffer + std::string test = ds.str(); // binary string + std::vector result(test.length()); + std::copy(test.begin(), test.end(), result.begin()); + EXPECT_TRUE( compare_vector(expected, result) ); +} + +TEST(TestNspv, EquiHdrV5) +{ + { + std::string expected_string = "040000000101010101010101010101010101010101010101010101010101010101010101020202020202020202020202020202020202020202020202020202020202020204040404040404040404040404040404040404040404040404040404040404040100000002000000030303030303030303030303030303030303030303030303030303030303030300"; + std::vector expected = hex_str_to_vec(expected_string); + NSPV_equihdr old(5); + old.nTime = 1; + old.nBits = 2; + old.nNonce.SetHex("0303030303030303030303030303030303030303030303030303030303030303"); + old.nVersion = 4; + old.hashPrevBlock.SetHex("0101010101010101010101010101010101010101010101010101010101010101"); + old.hashMerkleRoot.SetHex("020202020202020202020202020202020202020202020202020202020202020202"); + old.hashFinalSaplingRoot.SetHex("0404040404040404040404040404040404040404040404040404040404040404"); + old.nSolution = std::vector(); // in versions >= 5, nSolution size is prepended + CDataStream ds(0, 0); + old.Serialize(ds); // now it is in a memory buffer + std::string test = ds.str(); // binary string + std::vector result(test.length()); + std::copy(test.begin(), test.end(), result.begin()); + EXPECT_TRUE( compare_vector(expected, result) ); + } + { + std::string expected_string = "04000000010101010101010101010101010101010101010101010101010101010101010102020202020202020202020202020202020202020202020202020202020202020404040404040404040404040404040404040404040404040404040404040404010000000200000003030303030303030303030303030303030303030303030303030303030303030a00010203040506070809"; + std::vector expected = hex_str_to_vec(expected_string); + NSPV_equihdr old(5); + old.nTime = 1; + old.nBits = 2; + old.nNonce.SetHex("0303030303030303030303030303030303030303030303030303030303030303"); + old.nVersion = 4; + old.hashPrevBlock.SetHex("0101010101010101010101010101010101010101010101010101010101010101"); + old.hashMerkleRoot.SetHex("020202020202020202020202020202020202020202020202020202020202020202"); + old.hashFinalSaplingRoot.SetHex("0404040404040404040404040404040404040404040404040404040404040404"); + old.nSolution = std::vector(); // in versions >= 5, nSolution size is prepended + for(size_t i = 0; i < 10; ++i) + { + old.nSolution.push_back(i); + } + CDataStream ds(0, 0); + old.Serialize(ds); // now it is in a memory buffer + std::string test = ds.str(); // binary string + std::vector result(test.length()); + std::copy(test.begin(), test.end(), result.begin()); + EXPECT_TRUE( compare_vector(expected, result) ); + } +} + +} // end namespace TestNspv \ No newline at end of file From 2add703410f1b547b6ff381b313d234933518944 Mon Sep 17 00:00:00 2001 From: jmjatlanta Date: Mon, 17 Jan 2022 12:32:48 -0500 Subject: [PATCH 2/2] build script fixes for debug mode --- depends/hosts/darwin.mk | 4 ++-- zcutil/build-mac.sh | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/depends/hosts/darwin.mk b/depends/hosts/darwin.mk index 7be744aebc0..9af5b86e5dd 100644 --- a/depends/hosts/darwin.mk +++ b/depends/hosts/darwin.mk @@ -8,10 +8,10 @@ darwin_CXX=g++-8 -target $(host) -mmacosx-version-min=$(OSX_MIN_VERSION) --sysro darwin_CFLAGS=-pipe darwin_CXXFLAGS=$(darwin_CFLAGS) -darwin_release_CFLAGS=-O1 +darwin_release_CFLAGS=-g -O3 darwin_release_CXXFLAGS=$(darwin_release_CFLAGS) -darwin_debug_CFLAGS=-O1 +darwin_debug_CFLAGS=-g -O0 darwin_debug_CXXFLAGS=$(darwin_debug_CFLAGS) darwin_native_toolchain=native_cctools diff --git a/zcutil/build-mac.sh b/zcutil/build-mac.sh index 7549414f7cf..a1e9e44d27e 100755 --- a/zcutil/build-mac.sh +++ b/zcutil/build-mac.sh @@ -37,6 +37,11 @@ then shift fi +# Allow users to set arbitrary compile flags. Most users will not need this. +if [[ -z "${CONFIGURE_FLAGS-}" ]]; then + CONFIGURE_FLAGS="" +fi + TRIPLET=`./depends/config.guess` PREFIX="$(pwd)/depends/$TRIPLET"