Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions depends/hosts/darwin.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/Makefile.ktest.include
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
155 changes: 155 additions & 0 deletions src/test-komodo/test_nspv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#include "nSPV/nspv_defs.h"
#include "streams.h"

#include <gtest/gtest.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <sstream>

namespace TestNspv
{

/**
* Convenience functions for testing
*/

bool compare_vector(std::vector<uint8_t> a, std::vector<uint8_t> 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<uint8_t> 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<uint8_t> hex_str_to_vec(const std::string& in)
{
std::vector<uint8_t> 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<uint8_t> 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<uint8_t>(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<uint8_t> 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<uint8_t> 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<uint8_t>(); // 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<uint8_t> result(test.length());
std::copy(test.begin(), test.end(), result.begin());
EXPECT_TRUE( compare_vector(expected, result) );
}
{
std::string expected_string = "04000000010101010101010101010101010101010101010101010101010101010101010102020202020202020202020202020202020202020202020202020202020202020404040404040404040404040404040404040404040404040404040404040404010000000200000003030303030303030303030303030303030303030303030303030303030303030a00010203040506070809";
std::vector<uint8_t> 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<uint8_t>(); // 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<uint8_t> result(test.length());
std::copy(test.begin(), test.end(), result.begin());
EXPECT_TRUE( compare_vector(expected, result) );
}
}

} // end namespace TestNspv
5 changes: 5 additions & 0 deletions zcutil/build-mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down