Rename c++ tests
This commit is contained in:
25
src/test-hush/main.cpp
Normal file
25
src/test-hush/main.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
#include "key.h"
|
||||
#include "base58.h"
|
||||
#include "chainparams.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "crypto/common.h"
|
||||
#include "testutils.h"
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
assert(init_and_check_sodium() != -1);
|
||||
ECC_Start();
|
||||
ECCVerifyHandle handle; // Inits secp256k1 verify context
|
||||
SelectParams(CBaseChainParams::REGTEST);
|
||||
|
||||
CBitcoinSecret vchSecret;
|
||||
// this returns false due to network prefix mismatch but works anyway
|
||||
vchSecret.SetString(notarySecret);
|
||||
notaryKey = vchSecret.GetKey();
|
||||
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
865
src/test-hush/test_addrman.cpp
Normal file
865
src/test-hush/test_addrman.cpp
Normal file
@@ -0,0 +1,865 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "addrman.h"
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "hash.h"
|
||||
#include "random.h"
|
||||
#include "util/asmap.h"
|
||||
|
||||
#include "netbase.h"
|
||||
#include "chainparams.h"
|
||||
#include "tinyformat.h"
|
||||
#include "utilstrencodings.h"
|
||||
|
||||
#define NODE_NONE 0
|
||||
|
||||
// https://stackoverflow.com/questions/16491675/how-to-send-custom-message-in-google-c-testing-framework/29155677
|
||||
#define GTEST_COUT_NOCOLOR std::cerr << "[ ] [ INFO ] "
|
||||
namespace testing
|
||||
{
|
||||
namespace internal
|
||||
{
|
||||
enum GTestColor {
|
||||
COLOR_DEFAULT,
|
||||
COLOR_RED,
|
||||
COLOR_GREEN,
|
||||
COLOR_YELLOW
|
||||
};
|
||||
|
||||
extern void ColoredPrintf(GTestColor color, const char* fmt, ...);
|
||||
}
|
||||
}
|
||||
#define PRINTF(...) do { testing::internal::ColoredPrintf(testing::internal::COLOR_GREEN, "[ ] "); testing::internal::ColoredPrintf(testing::internal::COLOR_YELLOW, __VA_ARGS__); } while(0)
|
||||
|
||||
// C++ stream interface
|
||||
class TestCout : public std::stringstream
|
||||
{
|
||||
public:
|
||||
~TestCout()
|
||||
{
|
||||
PRINTF("%s",str().c_str());
|
||||
}
|
||||
};
|
||||
|
||||
#define GTEST_COUT_COLOR TestCout()
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* xxd -i est-komodo/data/asmap.raw | sed 's/unsigned char/static unsigned const char/g' */
|
||||
static unsigned const char asmap_raw[] = {
|
||||
0xfb, 0x03, 0xec, 0x0f, 0xb0, 0x3f, 0xc0, 0xfe, 0x00, 0xfb, 0x03, 0xec,
|
||||
0x0f, 0xb0, 0x3f, 0xc0, 0xfe, 0x00, 0xfb, 0x03, 0xec, 0x0f, 0xb0, 0xff,
|
||||
0xff, 0xfe, 0xff, 0xed, 0xb0, 0xff, 0xd4, 0x86, 0xe6, 0x28, 0x29, 0x00,
|
||||
0x00, 0x40, 0x00, 0x00, 0x40, 0x00, 0x40, 0x99, 0x01, 0x00, 0x80, 0x01,
|
||||
0x80, 0x04, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, 0x1c, 0xf0, 0x39
|
||||
};
|
||||
unsigned int asmap_raw_len = 59;
|
||||
|
||||
class CAddrManTest : public CAddrMan
|
||||
{
|
||||
private:
|
||||
uint64_t state;
|
||||
bool deterministic;
|
||||
public:
|
||||
|
||||
explicit CAddrManTest(bool makeDeterministic = true,
|
||||
std::vector<bool> asmap = std::vector<bool>())
|
||||
{
|
||||
if (makeDeterministic) {
|
||||
// Set addrman addr placement to be deterministic.
|
||||
MakeDeterministic();
|
||||
}
|
||||
deterministic = makeDeterministic;
|
||||
m_asmap = asmap;
|
||||
state = 1;
|
||||
}
|
||||
|
||||
void PrintInternals()
|
||||
{
|
||||
GTEST_COUT_NOCOLOR << "mapInfo.size() = " << mapInfo.size() << std::endl;
|
||||
GTEST_COUT_NOCOLOR << "nNew = " << nNew << std::endl;
|
||||
}
|
||||
|
||||
//! Ensure that bucket placement is always the same for testing purposes.
|
||||
void MakeDeterministic()
|
||||
{
|
||||
nKey.SetNull();
|
||||
seed_insecure_rand(true);
|
||||
}
|
||||
|
||||
int RandomInt(int nMax)
|
||||
{
|
||||
state = (CHashWriter(SER_GETHASH, 0) << state).GetHash().GetCheapHash();
|
||||
return (unsigned int)(state % nMax);
|
||||
}
|
||||
|
||||
CAddrInfo* Find(const CNetAddr& addr, int* pnId = NULL)
|
||||
{
|
||||
return CAddrMan::Find(addr, pnId);
|
||||
}
|
||||
|
||||
CAddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = NULL)
|
||||
{
|
||||
return CAddrMan::Create(addr, addrSource, pnId);
|
||||
}
|
||||
|
||||
void Delete(int nId)
|
||||
{
|
||||
CAddrMan::Delete(nId);
|
||||
}
|
||||
|
||||
// Used to test deserialization
|
||||
std::pair<int, int> GetBucketAndEntry(const CAddress& addr)
|
||||
{
|
||||
// LOCK(cs);
|
||||
int nId = mapAddr[addr];
|
||||
for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; ++bucket) {
|
||||
for (int entry = 0; entry < ADDRMAN_BUCKET_SIZE; ++entry) {
|
||||
if (nId == vvNew[bucket][entry]) {
|
||||
return std::pair<int, int>(bucket, entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::pair<int, int>(-1, -1);
|
||||
}
|
||||
|
||||
void Clear()
|
||||
{
|
||||
CAddrMan::Clear();
|
||||
if (deterministic) {
|
||||
nKey.SetNull();
|
||||
seed_insecure_rand(true);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static CNetAddr ResolveIP(const std::string& ip)
|
||||
{
|
||||
vector<CNetAddr> vIPs;
|
||||
CNetAddr addr;
|
||||
if (LookupHost(ip.c_str(), vIPs)) {
|
||||
addr = vIPs[0];
|
||||
} else
|
||||
{
|
||||
// it was BOOST_CHECK_MESSAGE, but we can't use ASSERT or EXPECT outside a test
|
||||
GTEST_COUT_COLOR << strprintf("failed to resolve: %s", ip) << std::endl;
|
||||
}
|
||||
return addr;
|
||||
}
|
||||
|
||||
static CService ResolveService(const std::string& ip, const int port = 0)
|
||||
{
|
||||
CService serv;
|
||||
if (!Lookup(ip.c_str(), serv, port, false))
|
||||
GTEST_COUT_COLOR << strprintf("failed to resolve: %s:%i", ip, port) << std::endl;
|
||||
return serv;
|
||||
}
|
||||
|
||||
static std::vector<bool> FromBytes(const unsigned char* source, int vector_size) {
|
||||
std::vector<bool> result(vector_size);
|
||||
for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) {
|
||||
unsigned char cur_byte = source[byte_i];
|
||||
for (int bit_i = 0; bit_i < 8; ++bit_i) {
|
||||
result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
namespace TestAddrmanTests {
|
||||
|
||||
TEST(TestAddrmanTests, display_constants) {
|
||||
|
||||
// Not actually the test, just used to display constants
|
||||
GTEST_COUT_COLOR << "ADDRMAN_NEW_BUCKET_COUNT = " << ADDRMAN_NEW_BUCKET_COUNT << std::endl;
|
||||
GTEST_COUT_COLOR << "ADDRMAN_TRIED_BUCKET_COUNT = " << ADDRMAN_TRIED_BUCKET_COUNT << std::endl;
|
||||
GTEST_COUT_COLOR << "ADDRMAN_BUCKET_SIZE = " << ADDRMAN_BUCKET_SIZE << std::endl;
|
||||
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, addrman_simple) {
|
||||
|
||||
CAddrManTest addrman;
|
||||
|
||||
// Set addrman addr placement to be deterministic.
|
||||
addrman.MakeDeterministic();
|
||||
|
||||
CNetAddr source = CNetAddr("252.2.2.2");
|
||||
|
||||
// Test 1: Does Addrman respond correctly when empty.
|
||||
ASSERT_TRUE(addrman.size() == 0);
|
||||
CAddrInfo addr_null = addrman.Select();
|
||||
ASSERT_TRUE(addr_null.ToString() == "[::]:0");
|
||||
|
||||
// Test 2: Does Addrman::Add work as expected.
|
||||
CService addr1 = CService("250.1.1.1", 8333);
|
||||
addrman.Add(CAddress(addr1, NODE_NONE), source);
|
||||
ASSERT_TRUE(addrman.size() == 1);
|
||||
CAddrInfo addr_ret1 = addrman.Select();
|
||||
ASSERT_TRUE(addr_ret1.ToString() == "250.1.1.1:8333");
|
||||
|
||||
// Test 3: Does IP address deduplication work correctly.
|
||||
// Expected dup IP should not be added.
|
||||
CService addr1_dup = CService("250.1.1.1", 8333);
|
||||
addrman.Add(CAddress(addr1_dup, NODE_NONE), source);
|
||||
ASSERT_TRUE(addrman.size() == 1);
|
||||
|
||||
// Test 5: New table has one addr and we add a diff addr we should
|
||||
// have two addrs.
|
||||
CService addr2 = CService("250.1.1.2", 8333);
|
||||
addrman.Add(CAddress(addr2, NODE_NONE), source);
|
||||
ASSERT_TRUE(addrman.size() == 2);
|
||||
|
||||
// Test 6: AddrMan::Clear() should empty the new table.
|
||||
addrman.Clear();
|
||||
ASSERT_TRUE(addrman.size() == 0);
|
||||
CAddrInfo addr_null2 = addrman.Select();
|
||||
ASSERT_TRUE(addr_null2.ToString() == "[::]:0");
|
||||
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, addrman_ports) {
|
||||
CAddrManTest addrman;
|
||||
|
||||
// Set addrman addr placement to be deterministic.
|
||||
addrman.MakeDeterministic();
|
||||
|
||||
CNetAddr source = CNetAddr("252.2.2.2");
|
||||
|
||||
ASSERT_TRUE(addrman.size() == 0);
|
||||
|
||||
// Test 7; Addr with same IP but diff port does not replace existing addr.
|
||||
CService addr1 = CService("250.1.1.1", 8333);
|
||||
addrman.Add(CAddress(addr1, NODE_NONE), source);
|
||||
ASSERT_TRUE(addrman.size() == 1);
|
||||
|
||||
CService addr1_port = CService("250.1.1.1", 8334);
|
||||
addrman.Add(CAddress(addr1_port, NODE_NONE), source);
|
||||
ASSERT_TRUE(addrman.size() == 1);
|
||||
CAddrInfo addr_ret2 = addrman.Select();
|
||||
ASSERT_TRUE(addr_ret2.ToString() == "250.1.1.1:8333");
|
||||
|
||||
// Test 8: Add same IP but diff port to tried table, it doesn't get added.
|
||||
// Perhaps this is not ideal behavior but it is the current behavior.
|
||||
addrman.Good(CAddress(addr1_port, NODE_NONE));
|
||||
ASSERT_TRUE(addrman.size() == 1);
|
||||
bool newOnly = true;
|
||||
CAddrInfo addr_ret3 = addrman.Select(newOnly);
|
||||
ASSERT_TRUE(addr_ret3.ToString() == "250.1.1.1:8333");
|
||||
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, addrman_select) {
|
||||
CAddrManTest addrman;
|
||||
|
||||
// Set addrman addr placement to be deterministic.
|
||||
addrman.MakeDeterministic();
|
||||
|
||||
CNetAddr source = CNetAddr("252.2.2.2");
|
||||
|
||||
// Test 9: Select from new with 1 addr in new.
|
||||
CService addr1 = CService("250.1.1.1", 8333);
|
||||
addrman.Add(CAddress(addr1, NODE_NONE), source);
|
||||
ASSERT_TRUE(addrman.size() == 1);
|
||||
|
||||
bool newOnly = true;
|
||||
CAddrInfo addr_ret1 = addrman.Select(newOnly);
|
||||
ASSERT_TRUE(addr_ret1.ToString() == "250.1.1.1:8333");
|
||||
|
||||
// Test 10: move addr to tried, select from new expected nothing returned.
|
||||
addrman.Good(CAddress(addr1, NODE_NONE));
|
||||
ASSERT_TRUE(addrman.size() == 1);
|
||||
CAddrInfo addr_ret2 = addrman.Select(newOnly);
|
||||
ASSERT_TRUE(addr_ret2.ToString() == "[::]:0");
|
||||
|
||||
CAddrInfo addr_ret3 = addrman.Select();
|
||||
ASSERT_TRUE(addr_ret3.ToString() == "250.1.1.1:8333");
|
||||
|
||||
ASSERT_TRUE(addrman.size() == 1);
|
||||
|
||||
|
||||
// Add three addresses to new table.
|
||||
CService addr2 = CService("250.3.1.1", 8333);
|
||||
CService addr3 = CService("250.3.2.2", 9999);
|
||||
CService addr4 = CService("250.3.3.3", 9999);
|
||||
|
||||
addrman.Add(CAddress(addr2, NODE_NONE), CService("250.3.1.1", 8333));
|
||||
addrman.Add(CAddress(addr3, NODE_NONE), CService("250.3.1.1", 8333));
|
||||
addrman.Add(CAddress(addr4, NODE_NONE), CService("250.4.1.1", 8333));
|
||||
|
||||
// Add three addresses to tried table.
|
||||
CService addr5 = CService("250.4.4.4", 8333);
|
||||
CService addr6 = CService("250.4.5.5", 7777);
|
||||
CService addr7 = CService("250.4.6.6", 8333);
|
||||
|
||||
addrman.Add(CAddress(addr5, NODE_NONE), CService("250.3.1.1", 8333));
|
||||
addrman.Good(CAddress(addr5, NODE_NONE));
|
||||
addrman.Add(CAddress(addr6, NODE_NONE), CService("250.3.1.1", 8333));
|
||||
addrman.Good(CAddress(addr6, NODE_NONE));
|
||||
addrman.Add(CAddress(addr7, NODE_NONE), CService("250.1.1.3", 8333));
|
||||
addrman.Good(CAddress(addr7, NODE_NONE));
|
||||
|
||||
// Test 11: 6 addrs + 1 addr from last test = 7.
|
||||
ASSERT_TRUE(addrman.size() == 7);
|
||||
|
||||
// Test 12: Select pulls from new and tried regardless of port number.
|
||||
ASSERT_TRUE(addrman.Select().ToString() == "250.4.6.6:8333");
|
||||
ASSERT_TRUE(addrman.Select().ToString() == "250.3.2.2:9999");
|
||||
ASSERT_TRUE(addrman.Select().ToString() == "250.3.3.3:9999");
|
||||
ASSERT_TRUE(addrman.Select().ToString() == "250.4.4.4:8333");
|
||||
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, addrman_new_collisions)
|
||||
{
|
||||
CAddrManTest addrman;
|
||||
|
||||
// Set addrman addr placement to be deterministic.
|
||||
addrman.MakeDeterministic();
|
||||
|
||||
CNetAddr source = CNetAddr("252.2.2.2");
|
||||
|
||||
ASSERT_TRUE(addrman.size() == 0);
|
||||
|
||||
for (unsigned int i = 1; i < 18; i++) {
|
||||
CService addr = CService("250.1.1." + boost::to_string(i));
|
||||
addrman.Add(CAddress(addr, NODE_NONE), source);
|
||||
//Test 13: No collision in new table yet.
|
||||
ASSERT_TRUE(addrman.size() == i);
|
||||
}
|
||||
|
||||
//Test 14: new table collision!
|
||||
CService addr1 = CService("250.1.1.18");
|
||||
addrman.Add(CAddress(addr1, NODE_NONE), source);
|
||||
ASSERT_TRUE(addrman.size() == 17);
|
||||
|
||||
CService addr2 = CService("250.1.1.19");
|
||||
addrman.Add(CAddress(addr2, NODE_NONE), source);
|
||||
ASSERT_TRUE(addrman.size() == 18);
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, addrman_tried_collisions)
|
||||
{
|
||||
CAddrManTest addrman;
|
||||
|
||||
// Set addrman addr placement to be deterministic.
|
||||
addrman.MakeDeterministic();
|
||||
|
||||
CNetAddr source = CNetAddr("252.2.2.2");
|
||||
|
||||
ASSERT_TRUE(addrman.size() == 0);
|
||||
|
||||
for (unsigned int i = 1; i < 80; i++) {
|
||||
CService addr = CService("250.1.1." + boost::to_string(i));
|
||||
addrman.Add(CAddress(addr, NODE_NONE), source);
|
||||
addrman.Good(CAddress(addr, NODE_NONE));
|
||||
|
||||
//Test 15: No collision in tried table yet.
|
||||
// GTEST_COUT << addrman.size() << std::endl;
|
||||
ASSERT_TRUE(addrman.size() == i);
|
||||
}
|
||||
|
||||
//Test 16: tried table collision!
|
||||
CService addr1 = CService("250.1.1.80");
|
||||
addrman.Add(CAddress(addr1, NODE_NONE), source);
|
||||
ASSERT_TRUE(addrman.size() == 79);
|
||||
|
||||
CService addr2 = CService("250.1.1.81");
|
||||
addrman.Add(CAddress(addr2, NODE_NONE), source);
|
||||
ASSERT_TRUE(addrman.size() == 80);
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, addrman_find)
|
||||
{
|
||||
CAddrManTest addrman;
|
||||
|
||||
// Set addrman addr placement to be deterministic.
|
||||
addrman.MakeDeterministic();
|
||||
|
||||
ASSERT_TRUE(addrman.size() == 0);
|
||||
|
||||
CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE);
|
||||
CAddress addr2 = CAddress(CService("250.1.2.1", 9999), NODE_NONE);
|
||||
CAddress addr3 = CAddress(CService("251.255.2.1", 8333), NODE_NONE);
|
||||
|
||||
CNetAddr source1 = CNetAddr("250.1.2.1");
|
||||
CNetAddr source2 = CNetAddr("250.1.2.2");
|
||||
|
||||
addrman.Add(addr1, source1);
|
||||
addrman.Add(addr2, source2);
|
||||
addrman.Add(addr3, source1);
|
||||
|
||||
// Test 17: ensure Find returns an IP matching what we searched on.
|
||||
CAddrInfo* info1 = addrman.Find(addr1);
|
||||
ASSERT_TRUE(info1);
|
||||
if (info1)
|
||||
ASSERT_TRUE(info1->ToString() == "250.1.2.1:8333");
|
||||
|
||||
// Test 18; Find does not discriminate by port number.
|
||||
CAddrInfo* info2 = addrman.Find(addr2);
|
||||
ASSERT_TRUE(info2);
|
||||
if (info2)
|
||||
ASSERT_TRUE(info2->ToString() == info1->ToString());
|
||||
|
||||
// Test 19: Find returns another IP matching what we searched on.
|
||||
CAddrInfo* info3 = addrman.Find(addr3);
|
||||
ASSERT_TRUE(info3);
|
||||
if (info3)
|
||||
ASSERT_TRUE(info3->ToString() == "251.255.2.1:8333");
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, addrman_create)
|
||||
{
|
||||
CAddrManTest addrman;
|
||||
|
||||
// Set addrman addr placement to be deterministic.
|
||||
addrman.MakeDeterministic();
|
||||
|
||||
ASSERT_TRUE(addrman.size() == 0);
|
||||
|
||||
CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE);
|
||||
CNetAddr source1 = CNetAddr("250.1.2.1");
|
||||
|
||||
int nId;
|
||||
CAddrInfo* pinfo = addrman.Create(addr1, source1, &nId);
|
||||
|
||||
// Test 20: The result should be the same as the input addr.
|
||||
ASSERT_TRUE(pinfo->ToString() == "250.1.2.1:8333");
|
||||
|
||||
CAddrInfo* info2 = addrman.Find(addr1);
|
||||
ASSERT_TRUE(info2->ToString() == "250.1.2.1:8333");
|
||||
}
|
||||
|
||||
|
||||
TEST(TestAddrmanTests, addrman_delete)
|
||||
{
|
||||
CAddrManTest addrman;
|
||||
|
||||
// Set addrman addr placement to be deterministic.
|
||||
addrman.MakeDeterministic();
|
||||
|
||||
ASSERT_TRUE(addrman.size() == 0);
|
||||
|
||||
CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE);
|
||||
CNetAddr source1 = CNetAddr("250.1.2.1");
|
||||
|
||||
int nId;
|
||||
addrman.Create(addr1, source1, &nId);
|
||||
|
||||
// Test 21: Delete should actually delete the addr.
|
||||
ASSERT_TRUE(addrman.size() == 1);
|
||||
addrman.Delete(nId);
|
||||
ASSERT_TRUE(addrman.size() == 0);
|
||||
CAddrInfo* info2 = addrman.Find(addr1);
|
||||
ASSERT_TRUE(info2 == NULL);
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, addrman_getaddr)
|
||||
{
|
||||
CAddrManTest addrman;
|
||||
|
||||
// Set addrman addr placement to be deterministic.
|
||||
addrman.MakeDeterministic();
|
||||
|
||||
// Test 22: Sanity check, GetAddr should never return anything if addrman
|
||||
// is empty.
|
||||
ASSERT_TRUE(addrman.size() == 0);
|
||||
vector<CAddress> vAddr1 = addrman.GetAddr();
|
||||
ASSERT_TRUE(vAddr1.size() == 0);
|
||||
|
||||
CAddress addr1 = CAddress(CService("250.250.2.1", 8333), NODE_NONE);
|
||||
addr1.nTime = GetTime(); // Set time so isTerrible = false
|
||||
CAddress addr2 = CAddress(CService("250.251.2.2", 9999), NODE_NONE);
|
||||
addr2.nTime = GetTime();
|
||||
CAddress addr3 = CAddress(CService("251.252.2.3", 8333), NODE_NONE);
|
||||
addr3.nTime = GetTime();
|
||||
CAddress addr4 = CAddress(CService("252.253.3.4", 8333), NODE_NONE);
|
||||
addr4.nTime = GetTime();
|
||||
CAddress addr5 = CAddress(CService("252.254.4.5", 8333), NODE_NONE);
|
||||
addr5.nTime = GetTime();
|
||||
CNetAddr source1 = CNetAddr("250.1.2.1");
|
||||
CNetAddr source2 = CNetAddr("250.2.3.3");
|
||||
|
||||
// Test 23: Ensure GetAddr works with new addresses.
|
||||
addrman.Add(addr1, source1);
|
||||
addrman.Add(addr2, source2);
|
||||
addrman.Add(addr3, source1);
|
||||
addrman.Add(addr4, source2);
|
||||
addrman.Add(addr5, source1);
|
||||
|
||||
// GetAddr returns 23% of addresses, 23% of 5 is 1 rounded down.
|
||||
ASSERT_TRUE(addrman.GetAddr().size() == 1);
|
||||
|
||||
// Test 24: Ensure GetAddr works with new and tried addresses.
|
||||
addrman.Good(CAddress(addr1, NODE_NONE));
|
||||
addrman.Good(CAddress(addr2, NODE_NONE));
|
||||
ASSERT_TRUE(addrman.GetAddr().size() == 1);
|
||||
|
||||
// Test 25: Ensure GetAddr still returns 23% when addrman has many addrs.
|
||||
for (unsigned int i = 1; i < (8 * 256); i++) {
|
||||
int octet1 = i % 256;
|
||||
int octet2 = (i / 256) % 256;
|
||||
int octet3 = (i / (256 * 2)) % 256;
|
||||
string strAddr = boost::to_string(octet1) + "." + boost::to_string(octet2) + "." + boost::to_string(octet3) + ".23";
|
||||
CAddress addr = CAddress(CService(strAddr), NODE_NONE);
|
||||
|
||||
// Ensure that for all addrs in addrman, isTerrible == false.
|
||||
addr.nTime = GetTime();
|
||||
addrman.Add(addr, CNetAddr(strAddr));
|
||||
if (i % 8 == 0)
|
||||
addrman.Good(addr);
|
||||
}
|
||||
vector<CAddress> vAddr = addrman.GetAddr();
|
||||
|
||||
size_t percent23 = (addrman.size() * 23) / 100;
|
||||
ASSERT_TRUE(vAddr.size() == percent23);
|
||||
ASSERT_TRUE(vAddr.size() == 461);
|
||||
// (Addrman.size() < number of addresses added) due to address collisons.
|
||||
ASSERT_TRUE(addrman.size() == 2007);
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, caddrinfo_get_tried_bucket_legacy)
|
||||
{
|
||||
CAddrManTest addrman;
|
||||
|
||||
CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
|
||||
CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);
|
||||
|
||||
CNetAddr source1 = ResolveIP("250.1.1.1");
|
||||
|
||||
CAddrInfo info1 = CAddrInfo(addr1, source1);
|
||||
|
||||
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
|
||||
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
|
||||
|
||||
std::vector<bool> asmap; // use /16
|
||||
|
||||
ASSERT_EQ(info1.GetTriedBucket(nKey1, asmap), 40);
|
||||
|
||||
// Test: Make sure key actually randomizes bucket placement. A fail on
|
||||
// this test could be a security issue.
|
||||
ASSERT_TRUE(info1.GetTriedBucket(nKey1, asmap) != info1.GetTriedBucket(nKey2, asmap));
|
||||
|
||||
// Test: Two addresses with same IP but different ports can map to
|
||||
// different buckets because they have different keys.
|
||||
CAddrInfo info2 = CAddrInfo(addr2, source1);
|
||||
|
||||
ASSERT_TRUE(info1.GetKey() != info2.GetKey());
|
||||
ASSERT_TRUE(info1.GetTriedBucket(nKey1, asmap) != info2.GetTriedBucket(nKey1, asmap));
|
||||
|
||||
std::set<int> buckets;
|
||||
for (int i = 0; i < 255; i++) {
|
||||
CAddrInfo infoi = CAddrInfo(
|
||||
CAddress(ResolveService("250.1.1." + boost::to_string(i)), NODE_NONE),
|
||||
ResolveIP("250.1.1." + boost::to_string(i)));
|
||||
int bucket = infoi.GetTriedBucket(nKey1, asmap);
|
||||
buckets.insert(bucket);
|
||||
}
|
||||
// Test: IP addresses in the same /16 prefix should
|
||||
// never get more than 8 buckets with legacy grouping
|
||||
ASSERT_EQ(buckets.size(), 8U);
|
||||
|
||||
buckets.clear();
|
||||
for (int j = 0; j < 255; j++) {
|
||||
CAddrInfo infoj = CAddrInfo(
|
||||
CAddress(ResolveService("250." + boost::to_string(j) + ".1.1"), NODE_NONE),
|
||||
ResolveIP("250." + boost::to_string(j) + ".1.1"));
|
||||
int bucket = infoj.GetTriedBucket(nKey1, asmap);
|
||||
buckets.insert(bucket);
|
||||
}
|
||||
// Test: IP addresses in the different /16 prefix should map to more than
|
||||
// 8 buckets with legacy grouping
|
||||
ASSERT_EQ(buckets.size(), 160U);
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, caddrinfo_get_new_bucket_legacy)
|
||||
{
|
||||
CAddrManTest addrman;
|
||||
|
||||
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
|
||||
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
|
||||
|
||||
CNetAddr source1 = ResolveIP("250.1.2.1");
|
||||
|
||||
CAddrInfo info1 = CAddrInfo(addr1, source1);
|
||||
|
||||
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
|
||||
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
|
||||
|
||||
std::vector<bool> asmap; // use /16
|
||||
|
||||
// Test: Make sure the buckets are what we expect
|
||||
ASSERT_EQ(info1.GetNewBucket(nKey1, asmap), 786);
|
||||
ASSERT_EQ(info1.GetNewBucket(nKey1, source1, asmap), 786);
|
||||
|
||||
// Test: Make sure key actually randomizes bucket placement. A fail on
|
||||
// this test could be a security issue.
|
||||
ASSERT_TRUE(info1.GetNewBucket(nKey1, asmap) != info1.GetNewBucket(nKey2, asmap));
|
||||
|
||||
// Test: Ports should not affect bucket placement in the addr
|
||||
CAddrInfo info2 = CAddrInfo(addr2, source1);
|
||||
ASSERT_TRUE(info1.GetKey() != info2.GetKey());
|
||||
ASSERT_EQ(info1.GetNewBucket(nKey1, asmap), info2.GetNewBucket(nKey1, asmap));
|
||||
|
||||
std::set<int> buckets;
|
||||
for (int i = 0; i < 255; i++) {
|
||||
CAddrInfo infoi = CAddrInfo(
|
||||
CAddress(ResolveService("250.1.1." + boost::to_string(i)), NODE_NONE),
|
||||
ResolveIP("250.1.1." + boost::to_string(i)));
|
||||
int bucket = infoi.GetNewBucket(nKey1, asmap);
|
||||
buckets.insert(bucket);
|
||||
}
|
||||
// Test: IP addresses in the same group (\16 prefix for IPv4) should
|
||||
// always map to the same bucket.
|
||||
ASSERT_EQ(buckets.size(), 1U);
|
||||
|
||||
buckets.clear();
|
||||
for (int j = 0; j < 4 * 255; j++) {
|
||||
CAddrInfo infoj = CAddrInfo(CAddress(
|
||||
ResolveService(
|
||||
boost::to_string(250 + (j / 255)) + "." + boost::to_string(j % 256) + ".1.1"), NODE_NONE),
|
||||
ResolveIP("251.4.1.1"));
|
||||
int bucket = infoj.GetNewBucket(nKey1, asmap);
|
||||
buckets.insert(bucket);
|
||||
}
|
||||
// Test: IP addresses in the same source groups should map to NO MORE
|
||||
// than 64 buckets.
|
||||
ASSERT_TRUE(buckets.size() <= 64);
|
||||
|
||||
buckets.clear();
|
||||
for (int p = 0; p < 255; p++) {
|
||||
CAddrInfo infoj = CAddrInfo(
|
||||
CAddress(ResolveService("250.1.1.1"), NODE_NONE),
|
||||
ResolveIP("250." + boost::to_string(p) + ".1.1"));
|
||||
int bucket = infoj.GetNewBucket(nKey1, asmap);
|
||||
buckets.insert(bucket);
|
||||
}
|
||||
// Test: IP addresses in the different source groups should map to MORE
|
||||
// than 64 buckets.
|
||||
ASSERT_TRUE(buckets.size() > 64);
|
||||
|
||||
}
|
||||
|
||||
// The following three test cases use asmap_raw[] from asmap.raw file
|
||||
// We use an artificial minimal mock mapping
|
||||
// 250.0.0.0/8 AS1000
|
||||
// 101.1.0.0/16 AS1
|
||||
// 101.2.0.0/16 AS2
|
||||
// 101.3.0.0/16 AS3
|
||||
// 101.4.0.0/16 AS4
|
||||
// 101.5.0.0/16 AS5
|
||||
// 101.6.0.0/16 AS6
|
||||
// 101.7.0.0/16 AS7
|
||||
// 101.8.0.0/16 AS8
|
||||
|
||||
TEST(TestAddrmanTests, caddrinfo_get_tried_bucket)
|
||||
{
|
||||
CAddrManTest addrman;
|
||||
|
||||
CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
|
||||
CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);
|
||||
|
||||
CNetAddr source1 = ResolveIP("250.1.1.1");
|
||||
|
||||
|
||||
CAddrInfo info1 = CAddrInfo(addr1, source1);
|
||||
|
||||
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
|
||||
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
|
||||
|
||||
std::vector<bool> asmap = FromBytes(asmap_raw, sizeof(asmap_raw) * 8);
|
||||
|
||||
ASSERT_EQ(info1.GetTriedBucket(nKey1, asmap), 236);
|
||||
|
||||
// Test: Make sure key actually randomizes bucket placement. A fail on
|
||||
// this test could be a security issue.
|
||||
ASSERT_TRUE(info1.GetTriedBucket(nKey1, asmap) != info1.GetTriedBucket(nKey2, asmap));
|
||||
|
||||
// Test: Two addresses with same IP but different ports can map to
|
||||
// different buckets because they have different keys.
|
||||
CAddrInfo info2 = CAddrInfo(addr2, source1);
|
||||
|
||||
ASSERT_TRUE(info1.GetKey() != info2.GetKey());
|
||||
ASSERT_TRUE(info1.GetTriedBucket(nKey1, asmap) != info2.GetTriedBucket(nKey1, asmap));
|
||||
|
||||
std::set<int> buckets;
|
||||
for (int j = 0; j < 255; j++) {
|
||||
CAddrInfo infoj = CAddrInfo(
|
||||
CAddress(ResolveService("101." + boost::to_string(j) + ".1.1"), NODE_NONE),
|
||||
ResolveIP("101." + boost::to_string(j) + ".1.1"));
|
||||
int bucket = infoj.GetTriedBucket(nKey1, asmap);
|
||||
buckets.insert(bucket);
|
||||
}
|
||||
// Test: IP addresses in the different /16 prefix MAY map to more than
|
||||
// 8 buckets.
|
||||
ASSERT_TRUE(buckets.size() > 8);
|
||||
|
||||
buckets.clear();
|
||||
for (int j = 0; j < 255; j++) {
|
||||
CAddrInfo infoj = CAddrInfo(
|
||||
CAddress(ResolveService("250." + boost::to_string(j) + ".1.1"), NODE_NONE),
|
||||
ResolveIP("250." + boost::to_string(j) + ".1.1"));
|
||||
int bucket = infoj.GetTriedBucket(nKey1, asmap);
|
||||
buckets.insert(bucket);
|
||||
}
|
||||
// Test: IP addresses in the different /16 prefix MAY NOT map to more than
|
||||
// 8 buckets.
|
||||
ASSERT_TRUE(buckets.size() == 8);
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, caddrinfo_get_new_bucket)
|
||||
{
|
||||
CAddrManTest addrman;
|
||||
|
||||
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
|
||||
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
|
||||
|
||||
CNetAddr source1 = ResolveIP("250.1.2.1");
|
||||
|
||||
CAddrInfo info1 = CAddrInfo(addr1, source1);
|
||||
|
||||
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
|
||||
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
|
||||
|
||||
std::vector<bool> asmap = FromBytes(asmap_raw, sizeof(asmap_raw) * 8);
|
||||
|
||||
// Test: Make sure the buckets are what we expect
|
||||
ASSERT_EQ(info1.GetNewBucket(nKey1, asmap), 795);
|
||||
ASSERT_EQ(info1.GetNewBucket(nKey1, source1, asmap), 795);
|
||||
|
||||
// Test: Make sure key actually randomizes bucket placement. A fail on
|
||||
// this test could be a security issue.
|
||||
ASSERT_TRUE(info1.GetNewBucket(nKey1, asmap) != info1.GetNewBucket(nKey2, asmap));
|
||||
|
||||
// Test: Ports should not affect bucket placement in the addr
|
||||
CAddrInfo info2 = CAddrInfo(addr2, source1);
|
||||
ASSERT_TRUE(info1.GetKey() != info2.GetKey());
|
||||
ASSERT_EQ(info1.GetNewBucket(nKey1, asmap), info2.GetNewBucket(nKey1, asmap));
|
||||
|
||||
std::set<int> buckets;
|
||||
for (int i = 0; i < 255; i++) {
|
||||
CAddrInfo infoi = CAddrInfo(
|
||||
CAddress(ResolveService("250.1.1." + boost::to_string(i)), NODE_NONE),
|
||||
ResolveIP("250.1.1." + boost::to_string(i)));
|
||||
int bucket = infoi.GetNewBucket(nKey1, asmap);
|
||||
buckets.insert(bucket);
|
||||
}
|
||||
// Test: IP addresses in the same /16 prefix
|
||||
// usually map to the same bucket.
|
||||
ASSERT_EQ(buckets.size(), 1U);
|
||||
|
||||
buckets.clear();
|
||||
for (int j = 0; j < 4 * 255; j++) {
|
||||
CAddrInfo infoj = CAddrInfo(CAddress(
|
||||
ResolveService(
|
||||
boost::to_string(250 + (j / 255)) + "." + boost::to_string(j % 256) + ".1.1"), NODE_NONE),
|
||||
ResolveIP("251.4.1.1"));
|
||||
int bucket = infoj.GetNewBucket(nKey1, asmap);
|
||||
buckets.insert(bucket);
|
||||
}
|
||||
// Test: IP addresses in the same source /16 prefix should not map to more
|
||||
// than 64 buckets.
|
||||
ASSERT_TRUE(buckets.size() <= 64);
|
||||
|
||||
buckets.clear();
|
||||
for (int p = 0; p < 255; p++) {
|
||||
CAddrInfo infoj = CAddrInfo(
|
||||
CAddress(ResolveService("250.1.1.1"), NODE_NONE),
|
||||
ResolveIP("101." + boost::to_string(p) + ".1.1"));
|
||||
int bucket = infoj.GetNewBucket(nKey1, asmap);
|
||||
buckets.insert(bucket);
|
||||
}
|
||||
// Test: IP addresses in the different source /16 prefixes usually map to MORE
|
||||
// than 1 bucket.
|
||||
ASSERT_TRUE(buckets.size() > 1);
|
||||
|
||||
buckets.clear();
|
||||
for (int p = 0; p < 255; p++) {
|
||||
CAddrInfo infoj = CAddrInfo(
|
||||
CAddress(ResolveService("250.1.1.1"), NODE_NONE),
|
||||
ResolveIP("250." + boost::to_string(p) + ".1.1"));
|
||||
int bucket = infoj.GetNewBucket(nKey1, asmap);
|
||||
buckets.insert(bucket);
|
||||
}
|
||||
// Test: IP addresses in the different source /16 prefixes sometimes map to NO MORE
|
||||
// than 1 bucket.
|
||||
ASSERT_TRUE(buckets.size() == 1);
|
||||
}
|
||||
|
||||
TEST(TestAddrmanTests, addrman_serialization)
|
||||
{
|
||||
std::vector<bool> asmap1 = FromBytes(asmap_raw, sizeof(asmap_raw) * 8);
|
||||
|
||||
CAddrManTest addrman_asmap1(true, asmap1);
|
||||
CAddrManTest addrman_asmap1_dup(true, asmap1);
|
||||
CAddrManTest addrman_noasmap;
|
||||
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
||||
|
||||
CAddress addr = CAddress(ResolveService("250.1.1.1"), NODE_NONE);
|
||||
CNetAddr default_source;
|
||||
|
||||
addrman_asmap1.Add(addr, default_source);
|
||||
|
||||
stream << addrman_asmap1;
|
||||
// serizalizing/deserializing addrman with the same asmap
|
||||
stream >> addrman_asmap1_dup;
|
||||
|
||||
std::pair<int, int> bucketAndEntry_asmap1 = addrman_asmap1.GetBucketAndEntry(addr);
|
||||
std::pair<int, int> bucketAndEntry_asmap1_dup = addrman_asmap1_dup.GetBucketAndEntry(addr);
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1.second != -1);
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1_dup.second != -1);
|
||||
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1.first == bucketAndEntry_asmap1_dup.first);
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1.second == bucketAndEntry_asmap1_dup.second);
|
||||
|
||||
// deserializing asmaped peers.dat to non-asmaped addrman
|
||||
stream << addrman_asmap1;
|
||||
stream >> addrman_noasmap;
|
||||
std::pair<int, int> bucketAndEntry_noasmap = addrman_noasmap.GetBucketAndEntry(addr);
|
||||
ASSERT_TRUE(bucketAndEntry_noasmap.second != -1);
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1.first != bucketAndEntry_noasmap.first);
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1.second != bucketAndEntry_noasmap.second);
|
||||
|
||||
// deserializing non-asmaped peers.dat to asmaped addrman
|
||||
addrman_asmap1.Clear();
|
||||
addrman_noasmap.Clear();
|
||||
addrman_noasmap.Add(addr, default_source);
|
||||
// GTEST_COUT_COLOR << addr.ToString() << " - " << default_source.ToString() << " - " << addrman_noasmap.size() << std::endl;
|
||||
// addrman_noasmap.PrintInternals();
|
||||
stream << addrman_noasmap;
|
||||
// std::string strHex = HexStr(stream.begin(), stream.end());
|
||||
// GTEST_COUT_COLOR << strHex << std::endl;
|
||||
|
||||
stream >> addrman_asmap1;
|
||||
std::pair<int, int> bucketAndEntry_asmap1_deser = addrman_asmap1.GetBucketAndEntry(addr);
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1_deser.second != -1);
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1_deser.first != bucketAndEntry_noasmap.first);
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1_deser.first == bucketAndEntry_asmap1_dup.first);
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1_deser.second == bucketAndEntry_asmap1_dup.second);
|
||||
|
||||
// used to map to different buckets, now maps to the same bucket.
|
||||
addrman_asmap1.Clear();
|
||||
addrman_noasmap.Clear();
|
||||
CAddress addr1 = CAddress(ResolveService("250.1.1.1"), NODE_NONE);
|
||||
CAddress addr2 = CAddress(ResolveService("250.2.1.1"), NODE_NONE);
|
||||
addrman_noasmap.Add(addr, default_source);
|
||||
addrman_noasmap.Add(addr2, default_source);
|
||||
std::pair<int, int> bucketAndEntry_noasmap_addr1 = addrman_noasmap.GetBucketAndEntry(addr1);
|
||||
std::pair<int, int> bucketAndEntry_noasmap_addr2 = addrman_noasmap.GetBucketAndEntry(addr2);
|
||||
ASSERT_TRUE(bucketAndEntry_noasmap_addr1.first != bucketAndEntry_noasmap_addr2.first);
|
||||
ASSERT_TRUE(bucketAndEntry_noasmap_addr1.second != bucketAndEntry_noasmap_addr2.second);
|
||||
stream << addrman_noasmap;
|
||||
stream >> addrman_asmap1;
|
||||
std::pair<int, int> bucketAndEntry_asmap1_deser_addr1 = addrman_asmap1.GetBucketAndEntry(addr1);
|
||||
std::pair<int, int> bucketAndEntry_asmap1_deser_addr2 = addrman_asmap1.GetBucketAndEntry(addr2);
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1_deser_addr1.first == bucketAndEntry_asmap1_deser_addr2.first);
|
||||
ASSERT_TRUE(bucketAndEntry_asmap1_deser_addr1.second != bucketAndEntry_asmap1_deser_addr2.second);
|
||||
}
|
||||
|
||||
}
|
||||
260
src/test-hush/test_coinimport.cpp
Normal file
260
src/test-hush/test_coinimport.cpp
Normal file
@@ -0,0 +1,260 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
|
||||
#include <cryptoconditions.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "cc/eval.h"
|
||||
#include "importcoin.h"
|
||||
#include "base58.h"
|
||||
#include "core_io.h"
|
||||
#include "key.h"
|
||||
#include "main.h"
|
||||
#include "primitives/transaction.h"
|
||||
#include "script/cc.h"
|
||||
#include "script/interpreter.h"
|
||||
#include "script/serverchecker.h"
|
||||
#include "txmempool.h"
|
||||
|
||||
#include "testutils.h"
|
||||
|
||||
|
||||
extern Eval* EVAL_TEST;
|
||||
|
||||
namespace TestCoinImport {
|
||||
|
||||
|
||||
static uint8_t testNum = 0;
|
||||
|
||||
class TestCoinImport : public ::testing::Test, public Eval {
|
||||
public:
|
||||
CMutableTransaction burnTx; std::vector<uint8_t> rawproof;
|
||||
std::vector<CTxOut> payouts;
|
||||
TxProof proof;
|
||||
uint256 MoMoM;
|
||||
CMutableTransaction importTx;
|
||||
uint32_t testCcid = 2;
|
||||
std::string testSymbol = "PIZZA";
|
||||
CAmount amount = 100;
|
||||
|
||||
void SetImportTx() {
|
||||
burnTx.vout.resize(0);
|
||||
burnTx.vout.push_back(MakeBurnOutput(amount, testCcid, testSymbol, payouts,rawproof));
|
||||
importTx = CMutableTransaction(MakeImportCoinTransaction(proof, CTransaction(burnTx), payouts));
|
||||
MoMoM = burnTx.GetHash(); // TODO: an actual branch
|
||||
}
|
||||
|
||||
uint32_t GetAssetchainsCC() const { return testCcid; }
|
||||
std::string GetAssetchainsSymbol() const { return testSymbol; }
|
||||
|
||||
bool GetProofRoot(uint256 hash, uint256 &momom) const
|
||||
{
|
||||
if (MoMoM.IsNull()) return false;
|
||||
momom = MoMoM;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
static void SetUpTestCase() { setupChain(); }
|
||||
virtual void SetUp() {
|
||||
ASSETCHAINS_CC = 1;
|
||||
EVAL_TEST = this;
|
||||
|
||||
std::vector<uint8_t> fakepk;
|
||||
fakepk.resize(33);
|
||||
fakepk.begin()[0] = testNum++;
|
||||
payouts.push_back(CTxOut(amount, CScript() << fakepk << OP_CHECKSIG));
|
||||
SetImportTx();
|
||||
}
|
||||
|
||||
|
||||
void TestRunCCEval(CMutableTransaction mtx)
|
||||
{
|
||||
CTransaction importTx(mtx);
|
||||
PrecomputedTransactionData txdata(importTx);
|
||||
ServerTransactionSignatureChecker checker(&importTx, 0, 0, false, txdata);
|
||||
CValidationState verifystate;
|
||||
if (!VerifyCoinImport(importTx.vin[0].scriptSig, checker, verifystate))
|
||||
printf("TestRunCCEval: %s\n", verifystate.GetRejectReason().data());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testProcessImportThroughPipeline)
|
||||
{
|
||||
CValidationState mainstate;
|
||||
CTransaction tx(importTx);
|
||||
|
||||
// first should work
|
||||
acceptTxFail(tx);
|
||||
|
||||
// should fail in mempool
|
||||
ASSERT_FALSE(acceptTx(tx, mainstate));
|
||||
EXPECT_EQ("already in mempool", mainstate.GetRejectReason());
|
||||
|
||||
// should be in persisted UTXO set
|
||||
generateBlock();
|
||||
ASSERT_FALSE(acceptTx(tx, mainstate));
|
||||
EXPECT_EQ("already have coins", mainstate.GetRejectReason());
|
||||
ASSERT_TRUE(pcoinsTip->HaveCoins(tx.GetHash()));
|
||||
|
||||
// Now disconnect the block
|
||||
CValidationState invalstate;
|
||||
if (!InvalidateBlock(invalstate, chainActive.Tip())) {
|
||||
FAIL() << invalstate.GetRejectReason();
|
||||
}
|
||||
ASSERT_FALSE(pcoinsTip->HaveCoins(tx.GetHash()));
|
||||
|
||||
// should be back in mempool
|
||||
ASSERT_FALSE(acceptTx(tx, mainstate));
|
||||
EXPECT_EQ("already in mempool", mainstate.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testImportTombstone)
|
||||
{
|
||||
CValidationState mainstate;
|
||||
// By setting an unspendable output, there will be no addition to UTXO
|
||||
// Nonetheless, we dont want to be able to import twice
|
||||
payouts[0].scriptPubKey = CScript() << OP_RETURN;
|
||||
SetImportTx();
|
||||
MoMoM = burnTx.GetHash(); // TODO: an actual branch
|
||||
CTransaction tx(importTx);
|
||||
|
||||
// first should work
|
||||
acceptTxFail(tx);
|
||||
|
||||
// should be in persisted UTXO set
|
||||
generateBlock();
|
||||
ASSERT_FALSE(acceptTx(tx, mainstate));
|
||||
EXPECT_EQ("import tombstone exists", mainstate.GetRejectReason());
|
||||
ASSERT_TRUE(pcoinsTip->HaveCoins(burnTx.GetHash()));
|
||||
|
||||
// Now disconnect the block
|
||||
CValidationState invalstate;
|
||||
if (!InvalidateBlock(invalstate, chainActive.Tip())) {
|
||||
FAIL() << invalstate.GetRejectReason();
|
||||
}
|
||||
// Tombstone should be gone from utxo set
|
||||
ASSERT_FALSE(pcoinsTip->HaveCoins(burnTx.GetHash()));
|
||||
|
||||
// should be back in mempool
|
||||
ASSERT_FALSE(acceptTx(tx, mainstate));
|
||||
EXPECT_EQ("already in mempool", mainstate.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testNoVouts)
|
||||
{
|
||||
importTx.vout.resize(0);
|
||||
TestRunCCEval(importTx);
|
||||
EXPECT_EQ("too-few-vouts", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testInvalidParams)
|
||||
{
|
||||
std::vector<uint8_t> payload = E_MARSHAL(ss << EVAL_IMPORTCOIN; ss << 'a');
|
||||
importTx.vin[0].scriptSig = CScript() << payload;
|
||||
TestRunCCEval(importTx);
|
||||
EXPECT_EQ("invalid-params", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testNonCanonical)
|
||||
{
|
||||
importTx.nLockTime = 10;
|
||||
TestRunCCEval(importTx);
|
||||
EXPECT_EQ("non-canonical", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testInvalidBurnOutputs)
|
||||
{
|
||||
burnTx.vout.resize(0);
|
||||
MoMoM = burnTx.GetHash(); // TODO: an actual branch
|
||||
CTransaction tx = MakeImportCoinTransaction(proof, CTransaction(burnTx), payouts);
|
||||
TestRunCCEval(tx);
|
||||
EXPECT_EQ("invalid-burn-tx", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testInvalidBurnParams)
|
||||
{
|
||||
burnTx.vout.back().scriptPubKey = CScript() << OP_RETURN << E_MARSHAL(ss << VARINT(testCcid));
|
||||
MoMoM = burnTx.GetHash(); // TODO: an actual branch
|
||||
CTransaction tx = MakeImportCoinTransaction(proof, CTransaction(burnTx), payouts);
|
||||
TestRunCCEval(tx);
|
||||
EXPECT_EQ("invalid-burn-tx", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testWrongChainId)
|
||||
{
|
||||
testCcid = 0;
|
||||
TestRunCCEval(importTx);
|
||||
EXPECT_EQ("importcoin-wrong-chain", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testInvalidBurnAmount)
|
||||
{
|
||||
burnTx.vout.back().nValue = 0;
|
||||
MoMoM = burnTx.GetHash(); // TODO: an actual branch
|
||||
CTransaction tx = MakeImportCoinTransaction(proof, CTransaction(burnTx), payouts);
|
||||
TestRunCCEval(tx);
|
||||
EXPECT_EQ("invalid-burn-amount", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testPayoutTooHigh)
|
||||
{
|
||||
importTx.vout[1].nValue = 101;
|
||||
TestRunCCEval(importTx);
|
||||
EXPECT_EQ("payout-too-high", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testAmountInOpret)
|
||||
{
|
||||
importTx.vout[0].nValue = 1;
|
||||
TestRunCCEval(importTx);
|
||||
EXPECT_EQ("non-canonical", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testInvalidPayouts)
|
||||
{
|
||||
importTx.vout[1].nValue = 40;
|
||||
importTx.vout.push_back(importTx.vout[0]);
|
||||
TestRunCCEval(importTx);
|
||||
EXPECT_EQ("wrong-payouts", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testCouldntLoadMomom)
|
||||
{
|
||||
MoMoM.SetNull();
|
||||
TestRunCCEval(importTx);
|
||||
EXPECT_EQ("coudnt-load-momom", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testMomomCheckFail)
|
||||
{
|
||||
MoMoM.SetNull();
|
||||
MoMoM.begin()[0] = 1;
|
||||
TestRunCCEval(importTx);
|
||||
EXPECT_EQ("momom-check-fail", state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestCoinImport, testGetCoinImportValue)
|
||||
{
|
||||
ASSERT_EQ(100, GetCoinImportValue(importTx));
|
||||
}
|
||||
|
||||
} /* namespace TestCoinImport */
|
||||
203
src/test-hush/test_cryptoconditions.cpp
Normal file
203
src/test-hush/test_cryptoconditions.cpp
Normal file
@@ -0,0 +1,203 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
#include <cryptoconditions.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "base58.h"
|
||||
#include "key.h"
|
||||
#include "script/cc.h"
|
||||
#include "cc/eval.h"
|
||||
#include "primitives/transaction.h"
|
||||
#include "script/interpreter.h"
|
||||
#include "script/serverchecker.h"
|
||||
|
||||
#include "testutils.h"
|
||||
|
||||
|
||||
|
||||
class CCTest : public ::testing::Test {
|
||||
public:
|
||||
void CCSign(CMutableTransaction &tx, CC *cond) {
|
||||
tx.vin.resize(1);
|
||||
PrecomputedTransactionData txdata(tx);
|
||||
uint256 sighash = SignatureHash(CCPubKey(cond), tx, 0, SIGHASH_ALL, 0, 0, &txdata);
|
||||
|
||||
int out = cc_signTreeSecp256k1Msg32(cond, notaryKey.begin(), sighash.begin());
|
||||
tx.vin[0].scriptSig = CCSig(cond);
|
||||
}
|
||||
protected:
|
||||
virtual void SetUp() {
|
||||
// enable CC
|
||||
ASSETCHAINS_CC = 1;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
TEST_F(CCTest, testIsPayToCryptoCondition)
|
||||
{
|
||||
CScript s = CScript() << VCH("a", 1);
|
||||
ASSERT_FALSE(s.IsPayToCryptoCondition());
|
||||
|
||||
s = CScript() << VCH("a", 1) << OP_CHECKCRYPTOCONDITION;
|
||||
ASSERT_TRUE(s.IsPayToCryptoCondition());
|
||||
|
||||
s = CScript() << OP_CHECKCRYPTOCONDITION;
|
||||
ASSERT_FALSE(s.IsPayToCryptoCondition());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(CCTest, testMayAcceptCryptoCondition)
|
||||
{
|
||||
CC *cond;
|
||||
|
||||
// ok
|
||||
CCFromJson(cond, R"!!(
|
||||
{ "type": "threshold-sha-256",
|
||||
"threshold": 2,
|
||||
"subfulfillments": [
|
||||
{ "type": "secp256k1-sha-256", "publicKey": "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47" }
|
||||
]
|
||||
})!!");
|
||||
ASSERT_TRUE(CCPubKey(cond).MayAcceptCryptoCondition());
|
||||
|
||||
|
||||
// prefix not allowed
|
||||
CCFromJson(cond, R"!!(
|
||||
{ "type": "prefix-sha-256",
|
||||
"prefix": "abc",
|
||||
"maxMessageLength": 10,
|
||||
"subfulfillment":
|
||||
{ "type": "secp256k1-sha-256", "publicKey": "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47" }
|
||||
})!!");
|
||||
ASSERT_FALSE(CCPubKey(cond).MayAcceptCryptoCondition());
|
||||
|
||||
|
||||
// has no signature nodes
|
||||
CCFromJson(cond, R"!!(
|
||||
{ "type": "threshold-sha-256",
|
||||
"threshold": 1,
|
||||
"subfulfillments": [
|
||||
{ "type": "eval-sha-256", "code": "" },
|
||||
{ "type": "eval-sha-256", "code": "" }
|
||||
]
|
||||
})!!");
|
||||
ASSERT_FALSE(CCPubKey(cond).MayAcceptCryptoCondition());
|
||||
}
|
||||
|
||||
|
||||
static bool CCVerify(const CMutableTransaction &mtxTo, const CC *cond) {
|
||||
CAmount amount;
|
||||
ScriptError error;
|
||||
CTransaction txTo(mtxTo);
|
||||
PrecomputedTransactionData txdata(txTo);
|
||||
auto checker = ServerTransactionSignatureChecker(&txTo, 0, amount, false, txdata);
|
||||
return VerifyScript(CCSig(cond), CCPubKey(cond), 0, checker, 0, &error);
|
||||
};
|
||||
|
||||
|
||||
TEST_F(CCTest, testVerifyCryptoCondition)
|
||||
{
|
||||
CC *cond;
|
||||
CMutableTransaction mtxTo;
|
||||
|
||||
// ok
|
||||
cond = CCNewSecp256k1(notaryKey.GetPubKey());
|
||||
CCFromJson(cond, R"!!({
|
||||
"type": "secp256k1-sha-256",
|
||||
"publicKey": "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47"
|
||||
})!!");
|
||||
CCSign(mtxTo, cond);
|
||||
ASSERT_TRUE(CCVerify(mtxTo, cond));
|
||||
|
||||
|
||||
// has signature nodes
|
||||
CCFromJson(cond, R"!!({
|
||||
"type": "threshold-sha-256",
|
||||
"threshold": 1,
|
||||
"subfulfillments": [
|
||||
{ "type": "preimage-sha-256", "preimage": "" },
|
||||
{ "type": "secp256k1-sha-256", "publicKey": "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47" }
|
||||
]
|
||||
})!!");
|
||||
cond->threshold = 2;
|
||||
CCSign(mtxTo, cond);
|
||||
ASSERT_TRUE(CCVerify(mtxTo, cond));
|
||||
|
||||
// no signatures; the preimage will get encoded as a fulfillment because it's cheaper
|
||||
// and the secp256k1 node will get encoded as a condition
|
||||
cond->threshold = 1;
|
||||
ASSERT_FALSE(CCVerify(mtxTo, cond));
|
||||
|
||||
// here the signature is set wrong
|
||||
cond->threshold = 2;
|
||||
ASSERT_TRUE(CCVerify(mtxTo, cond));
|
||||
memset(cond->subconditions[1]->signature, 0, 32);
|
||||
ASSERT_FALSE(CCVerify(mtxTo, cond));
|
||||
}
|
||||
|
||||
extern Eval* EVAL_TEST;
|
||||
|
||||
TEST_F(CCTest, testVerifyEvalCondition)
|
||||
{
|
||||
|
||||
class EvalMock : public Eval
|
||||
{
|
||||
public:
|
||||
bool Dispatch(const CC *cond, const CTransaction &txTo, unsigned int nIn)
|
||||
{ return cond->code[0] ? Valid() : Invalid(""); }
|
||||
};
|
||||
|
||||
EvalMock eval;
|
||||
EVAL_TEST = &eval;
|
||||
|
||||
|
||||
CC *cond;
|
||||
CMutableTransaction mtxTo;
|
||||
|
||||
// ok
|
||||
cond = CCNewThreshold(2, { CCNewSecp256k1(notaryKey.GetPubKey()), CCNewEval({1}) });
|
||||
CCSign(mtxTo, cond);
|
||||
ASSERT_TRUE(CCVerify(mtxTo, cond));
|
||||
|
||||
cond->subconditions[1]->code[0] = 0;
|
||||
ASSERT_FALSE(CCVerify(mtxTo, cond));
|
||||
}
|
||||
|
||||
|
||||
TEST_F(CCTest, testCryptoConditionsDisabled)
|
||||
{
|
||||
CC *cond;
|
||||
ScriptError error;
|
||||
CMutableTransaction mtxTo;
|
||||
|
||||
// ok
|
||||
CCFromJson(cond, R"!!({
|
||||
"type": "secp256k1-sha-256",
|
||||
"publicKey": "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47"
|
||||
})!!");
|
||||
CCSign(mtxTo, cond);
|
||||
ASSERT_TRUE(CCVerify(mtxTo, cond));
|
||||
|
||||
ASSETCHAINS_CC = 0;
|
||||
ASSERT_FALSE(CCVerify(mtxTo, cond));
|
||||
}
|
||||
|
||||
|
||||
TEST_F(CCTest, testLargeCondition)
|
||||
{
|
||||
CC *cond;
|
||||
ScriptError error;
|
||||
CMutableTransaction mtxTo;
|
||||
|
||||
std::vector<CC*> ccs;
|
||||
for (int i=0; i<18; i++) {
|
||||
ccs.push_back(CCNewSecp256k1(notaryKey.GetPubKey()));
|
||||
}
|
||||
cond = CCNewThreshold(16, ccs);
|
||||
CCSign(mtxTo, cond);
|
||||
EXPECT_EQ("(16 of 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,A5,A5)",
|
||||
CCShowStructure(CCPrune(cond)));
|
||||
EXPECT_EQ(1744, CCSig(cond).size());
|
||||
ASSERT_TRUE(CCVerify(mtxTo, cond));
|
||||
}
|
||||
602
src/test-hush/test_eval_bet.cpp
Normal file
602
src/test-hush/test_eval_bet.cpp
Normal file
@@ -0,0 +1,602 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
#include <cryptoconditions.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "cc/betprotocol.h"
|
||||
#include "cc/eval.h"
|
||||
#include "base58.h"
|
||||
#include "key.h"
|
||||
#include "main.h"
|
||||
#include "script/cc.h"
|
||||
#include "primitives/transaction.h"
|
||||
#include "script/interpreter.h"
|
||||
#include "script/serverchecker.h"
|
||||
|
||||
#include "testutils.h"
|
||||
|
||||
|
||||
extern Eval* EVAL_TEST;
|
||||
|
||||
|
||||
namespace TestBet {
|
||||
|
||||
|
||||
static std::vector<CKey> playerSecrets;
|
||||
static std::vector<CPubKey> players;
|
||||
|
||||
static int Dealer = 0, Player1 = 1, Player2 = 2;
|
||||
|
||||
|
||||
int CCSign(CMutableTransaction &tx, unsigned int nIn, CC *cond, std::vector<int> keyIds) {
|
||||
PrecomputedTransactionData txdata(tx);
|
||||
uint256 sighash = SignatureHash(CCPubKey(cond), tx, nIn, SIGHASH_ALL, 0, 0, &txdata);
|
||||
int nSigned = 0;
|
||||
for (int i=0; i<keyIds.size(); i++)
|
||||
nSigned += cc_signTreeSecp256k1Msg32(cond, playerSecrets[keyIds[i]].begin(), sighash.begin());
|
||||
tx.vin[nIn].scriptSig = CCSig(cond);
|
||||
return nSigned;
|
||||
}
|
||||
|
||||
|
||||
int TestCC(CMutableTransaction &mtxTo, unsigned int nIn, CC *cond)
|
||||
{
|
||||
CAmount amount;
|
||||
ScriptError error;
|
||||
CTransaction txTo(mtxTo);
|
||||
PrecomputedTransactionData txdata(txTo);
|
||||
auto checker = ServerTransactionSignatureChecker(&txTo, nIn, amount, false, txdata);
|
||||
return VerifyScript(txTo.vin[nIn].scriptSig, CCPubKey(cond), 0, checker, 0, &error);
|
||||
}
|
||||
|
||||
|
||||
#define ASSERT_CC(tx, nIn, cond) if (!TestCC(tx, nIn, cond)) FAIL();
|
||||
|
||||
|
||||
class MockVM : public AppVM
|
||||
{
|
||||
public:
|
||||
std::pair<int,std::vector<CTxOut>> evaluate(
|
||||
std::vector<unsigned char> header, std::vector<unsigned char> body)
|
||||
{
|
||||
std::vector<CTxOut> outs;
|
||||
if (memcmp(header.data(), "BetHeader", 9)) {
|
||||
printf("Wrong VM header\n");
|
||||
return std::make_pair(0, outs);
|
||||
}
|
||||
outs.push_back(CTxOut(2, CScript() << OP_RETURN << body.size()));
|
||||
return std::make_pair(body.size(), outs);
|
||||
}
|
||||
};
|
||||
|
||||
const EvalCode EVAL_DISPUTEBET = 0xf2;
|
||||
|
||||
class EvalMock : public Eval
|
||||
{
|
||||
public:
|
||||
uint256 MoM;
|
||||
int currentHeight;
|
||||
std::map<uint256, CTransaction> txs;
|
||||
std::map<uint256, CBlockIndex> blocks;
|
||||
std::map<uint256, std::vector<CTransaction>> spends;
|
||||
|
||||
bool Dispatch(const CC *cond, const CTransaction &txTo, unsigned int nIn)
|
||||
{
|
||||
EvalCode ecode = cond->code[0];
|
||||
std::vector<uint8_t> vparams(cond->code+1, cond->code+cond->codeLength);
|
||||
|
||||
if (ecode == EVAL_DISPUTEBET) {
|
||||
MockVM vm;
|
||||
return DisputePayout(vm, vparams, txTo, nIn);
|
||||
}
|
||||
if (ecode == EVAL_IMPORTPAYOUT) {
|
||||
return ImportPayout(vparams, txTo, nIn);
|
||||
}
|
||||
return Invalid("invalid-code");
|
||||
}
|
||||
|
||||
bool GetSpendsConfirmed(uint256 hash, std::vector<CTransaction> &spendsOut) const
|
||||
{
|
||||
auto r = spends.find(hash);
|
||||
if (r != spends.end()) {
|
||||
spendsOut = r->second;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GetTxUnconfirmed(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock) const
|
||||
{
|
||||
auto r = txs.find(hash);
|
||||
if (r != txs.end()) {
|
||||
txOut = r->second;
|
||||
if (blocks.count(hash) > 0)
|
||||
hashBlock = hash;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int GetCurrentHeight() const { return currentHeight; }
|
||||
|
||||
bool GetBlock(uint256 hash, CBlockIndex& blockIdx) const
|
||||
{
|
||||
auto r = blocks.find(hash);
|
||||
if (r == blocks.end()) return false;
|
||||
blockIdx = r->second;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetNotarisationData(uint256 notarisationHash, NotarisationData &data) const
|
||||
{
|
||||
if (notarisationHash == NotarisationHash()) {
|
||||
data.MoM = MoM;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static uint256 NotarisationHash()
|
||||
{
|
||||
uint256 h;
|
||||
h.begin()[0] = 123;
|
||||
return h;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Generates example data that we will test with and shows how to call BetProtocol.
|
||||
*/
|
||||
class ExampleBet
|
||||
{
|
||||
public:
|
||||
BetProtocol bet;
|
||||
CAmount totalPayout;
|
||||
|
||||
ExampleBet() : bet(BetProtocol(EVAL_DISPUTEBET, players, 2, VCH("BetHeader", 9))), totalPayout(100) {}
|
||||
~ExampleBet() {};
|
||||
|
||||
CTransaction SessionTx()
|
||||
{
|
||||
return CTransaction(bet.MakeSessionTx(1));
|
||||
}
|
||||
|
||||
CC* DisputeCond()
|
||||
{
|
||||
return bet.MakeDisputeCond();
|
||||
}
|
||||
|
||||
CC* PayoutCond()
|
||||
{
|
||||
return bet.MakePayoutCond(SessionTx().GetHash());
|
||||
}
|
||||
|
||||
CTransaction StakeTx()
|
||||
{
|
||||
return CTransaction(bet.MakeStakeTx(totalPayout, SessionTx().GetHash()));
|
||||
}
|
||||
|
||||
std::vector<unsigned char> PlayerState(int playerIdx)
|
||||
{
|
||||
std::vector<unsigned char> state;
|
||||
for (int i=0; i<playerIdx+1; i++) state.push_back(1);
|
||||
return state;
|
||||
}
|
||||
|
||||
std::vector<CTxOut> Payouts(int playerIdx)
|
||||
{
|
||||
return MockVM().evaluate(bet.vmParams, PlayerState(playerIdx)).second;
|
||||
}
|
||||
|
||||
CMutableTransaction DisputeTx(int playerIdx)
|
||||
{
|
||||
return bet.MakeDisputeTx(SessionTx().GetHash(), SerializeHash(Payouts(playerIdx)));
|
||||
}
|
||||
|
||||
CMutableTransaction PostEvidenceTx(int playerIdx)
|
||||
{
|
||||
return bet.MakePostEvidenceTx(SessionTx().GetHash(), playerIdx, PlayerState(playerIdx));
|
||||
}
|
||||
|
||||
CMutableTransaction AgreePayoutTx()
|
||||
{
|
||||
std::vector<CTxOut> v;
|
||||
return bet.MakeAgreePayoutTx(v, uint256());
|
||||
}
|
||||
|
||||
MoMProof GetMoMProof()
|
||||
{
|
||||
int nIndex = 5;
|
||||
std::vector<uint256> vBranch;
|
||||
vBranch.resize(3);
|
||||
return {MerkleBranch(nIndex, vBranch), EvalMock::NotarisationHash()};
|
||||
}
|
||||
|
||||
CMutableTransaction ImportPayoutTx()
|
||||
{
|
||||
CMutableTransaction disputeTx = DisputeTx(Player2);
|
||||
return bet.MakeImportPayoutTx(Payouts(Player2), disputeTx, uint256(), GetMoMProof());
|
||||
}
|
||||
|
||||
EvalMock SetEvalMock(int currentHeight)
|
||||
{
|
||||
EvalMock eval;
|
||||
CTransaction sessionTx = SessionTx();
|
||||
|
||||
eval.txs[sessionTx.GetHash()] = sessionTx;
|
||||
|
||||
CBlockIndex sessionBlock;
|
||||
sessionBlock.SetHeight(10);
|
||||
eval.blocks[sessionTx.GetHash()] = sessionBlock;
|
||||
|
||||
std::vector<CTransaction> sessionSpends;
|
||||
sessionSpends.push_back(CTransaction(PostEvidenceTx(Dealer)));
|
||||
sessionSpends.push_back(CTransaction()); // Invalid, should be ignored
|
||||
sessionSpends.push_back(CTransaction(PostEvidenceTx(Player2)));
|
||||
eval.spends[sessionTx.GetHash()] = sessionSpends;
|
||||
|
||||
eval.currentHeight = currentHeight;
|
||||
|
||||
MoMProof proof = GetMoMProof();
|
||||
eval.MoM = proof.branch.Exec(DisputeTx(Player2).GetHash());
|
||||
|
||||
EVAL_TEST = &eval;
|
||||
return eval;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
ExampleBet ebet;
|
||||
|
||||
|
||||
class TestBet : public ::testing::Test {
|
||||
protected:
|
||||
static void SetUpTestCase() {
|
||||
// Make playerSecrets
|
||||
CBitcoinSecret vchSecret;
|
||||
auto addKey = [&] (std::string k) { vchSecret.SetString(k); playerSecrets.push_back(vchSecret.GetKey()); };
|
||||
addKey("UwFBKf4d6wC3yqdnk3LoGrFjy7gwxrWerBT8jTFamrBbem8wSw9L");
|
||||
addKey("Up6GpWwrmx2VpqF8rD3snJXToKT56Dzc8YSoL24osXnfNdCucaMR");
|
||||
addKey("UxEHwki3A95PSHHVRzE2N67eHTeoUcqLkovxp6yDPVViv54skF8c");
|
||||
// Make playerpubkeys
|
||||
for (int i=0; i<playerSecrets.size(); i++) players.push_back(playerSecrets[i].GetPubKey());
|
||||
// enable CC
|
||||
ASSETCHAINS_CC = 1;
|
||||
}
|
||||
virtual void SetUp() {
|
||||
EVAL_TEST = 0;
|
||||
ebet = ExampleBet();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
TEST_F(TestBet, testMakeSessionTx)
|
||||
{
|
||||
CTransaction sessionTx = ebet.SessionTx();
|
||||
EXPECT_EQ(0, sessionTx.vin.size());
|
||||
EXPECT_EQ(4, sessionTx.vout.size());
|
||||
EXPECT_EQ(CCPubKey(ebet.DisputeCond()), sessionTx.vout[0].scriptPubKey);
|
||||
for (int i=0; i<players.size(); i++)
|
||||
EXPECT_EQ(CCPubKey(CCNewSecp256k1(players[i])), sessionTx.vout[i+1].scriptPubKey);
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testMakeDisputeCond)
|
||||
{
|
||||
CC *disputeCond = ebet.DisputeCond();
|
||||
EXPECT_EQ("(2 of 15,(1 of 5,5,5))", CCShowStructure(disputeCond));
|
||||
|
||||
CC *evalCond = disputeCond->subconditions[0];
|
||||
uint8_t target[100];
|
||||
sprintf((char*)target, "%c\x02\tBetHeader", EVAL_DISPUTEBET);
|
||||
EXPECT_EQ(0, memcmp(target, evalCond->code, 12));
|
||||
|
||||
for (int i=0; i<players.size(); i++)
|
||||
EXPECT_EQ(CCPubKey(CCNewSecp256k1(players[i])),
|
||||
CCPubKey(disputeCond->subconditions[1]->subconditions[i]));
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testSignDisputeCond)
|
||||
{
|
||||
// Only one key needed to dispute
|
||||
CMutableTransaction disputeTx = ebet.DisputeTx(Player1);
|
||||
CC *disputeCond = ebet.DisputeCond();
|
||||
EXPECT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player1}));
|
||||
|
||||
EXPECT_EQ(1, cc_isFulfilled(disputeCond->subconditions[0]));
|
||||
EXPECT_EQ(1, cc_isFulfilled(disputeCond->subconditions[1]));
|
||||
EXPECT_EQ(0, cc_isFulfilled(disputeCond->subconditions[1]->subconditions[0]));
|
||||
EXPECT_EQ(1, cc_isFulfilled(disputeCond->subconditions[1]->subconditions[1]));
|
||||
EXPECT_EQ(0, cc_isFulfilled(disputeCond->subconditions[1]->subconditions[2]));
|
||||
EXPECT_EQ(1, cc_isFulfilled(disputeCond));
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testDispute)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(12);
|
||||
|
||||
// Only one key needed to dispute
|
||||
CMutableTransaction disputeTx = ebet.DisputeTx(Player2);
|
||||
CC *disputeCond = ebet.DisputeCond();
|
||||
EXPECT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player2}));
|
||||
|
||||
// Success
|
||||
EXPECT_TRUE(TestCC(disputeTx, 0, disputeCond));
|
||||
|
||||
// Set result hash to 0 and check false
|
||||
uint256 nonsense;
|
||||
disputeTx.vout[0].scriptPubKey = CScript() << OP_RETURN << E_MARSHAL(ss << nonsense);
|
||||
EXPECT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player2}));
|
||||
EXPECT_FALSE(TestCC(disputeTx, 0, disputeCond));
|
||||
EXPECT_EQ("wrong-payout", eval.state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testDisputeInvalidOutput)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(11);
|
||||
|
||||
// Only one key needed to dispute
|
||||
CMutableTransaction disputeTx = ebet.DisputeTx(Dealer);
|
||||
CC *disputeCond = ebet.DisputeCond();
|
||||
|
||||
// invalid payout hash
|
||||
std::vector<unsigned char> invalidHash = {0,1,2};
|
||||
disputeTx.vout[0].scriptPubKey = CScript() << OP_RETURN << invalidHash;
|
||||
ASSERT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player1}));
|
||||
EXPECT_FALSE(TestCC(disputeTx, 0, disputeCond));
|
||||
EXPECT_EQ("invalid-payout-hash", eval.state.GetRejectReason());
|
||||
|
||||
// no vout at all
|
||||
disputeTx.vout.resize(0);
|
||||
ASSERT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player1}));
|
||||
EXPECT_FALSE(TestCC(disputeTx, 0, disputeCond));
|
||||
EXPECT_EQ("no-vouts", eval.state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testDisputeEarly)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(11);
|
||||
|
||||
// Only one key needed to dispute
|
||||
CMutableTransaction disputeTx = ebet.DisputeTx(Dealer);
|
||||
CC *disputeCond = ebet.DisputeCond();
|
||||
EXPECT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player1}));
|
||||
|
||||
EXPECT_FALSE(TestCC(disputeTx, 0, disputeCond));
|
||||
EXPECT_EQ("dispute-too-soon", eval.state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testDisputeInvalidParams)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(12);
|
||||
|
||||
CMutableTransaction disputeTx = ebet.DisputeTx(Player2);
|
||||
CC *disputeCond = ebet.DisputeCond();
|
||||
CC *evalCond = disputeCond->subconditions[0];
|
||||
|
||||
// too long
|
||||
evalCond->code = (unsigned char*) realloc(evalCond->code, ++evalCond->codeLength);
|
||||
ASSERT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player2}));
|
||||
EXPECT_FALSE(TestCC(disputeTx, 0, disputeCond));
|
||||
EXPECT_EQ("malformed-params", eval.state.GetRejectReason());
|
||||
|
||||
// too short
|
||||
eval.state = CValidationState();
|
||||
evalCond->codeLength = 1;
|
||||
ASSERT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player2}));
|
||||
EXPECT_FALSE(TestCC(disputeTx, 0, disputeCond));
|
||||
EXPECT_EQ("malformed-params", eval.state.GetRejectReason());
|
||||
|
||||
// is fine
|
||||
eval.state = CValidationState();
|
||||
evalCond->codeLength = 12;
|
||||
ASSERT_EQ(1, CCSign(disputeTx, 0, disputeCond, {Player2}));
|
||||
EXPECT_TRUE(TestCC(disputeTx, 0, disputeCond));
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testDisputeInvalidEvidence)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(12);
|
||||
|
||||
CMutableTransaction disputeTx = ebet.DisputeTx(Player2);
|
||||
CC *disputeCond = ebet.DisputeCond();
|
||||
CCSign(disputeTx, 0, disputeCond, {Player2});
|
||||
|
||||
CMutableTransaction mtx;
|
||||
|
||||
mtx.vout.resize(1);
|
||||
mtx.vout[0].scriptPubKey = CScript();
|
||||
eval.spends[ebet.SessionTx().GetHash()][1] = CTransaction(mtx);
|
||||
ASSERT_TRUE(TestCC(disputeTx, 0, disputeCond));
|
||||
|
||||
mtx.vout[0].scriptPubKey << OP_RETURN;
|
||||
eval.spends[ebet.SessionTx().GetHash()][1] = CTransaction(mtx);
|
||||
ASSERT_TRUE(TestCC(disputeTx, 0, disputeCond));
|
||||
|
||||
mtx.vout[0].scriptPubKey = CScript() << 0;
|
||||
eval.spends[ebet.SessionTx().GetHash()][1] = CTransaction(mtx);
|
||||
ASSERT_TRUE(TestCC(disputeTx, 0, disputeCond));
|
||||
|
||||
eval.spends[ebet.SessionTx().GetHash()].resize(1);
|
||||
eval.spends[ebet.SessionTx().GetHash()][0] = CTransaction();
|
||||
ASSERT_FALSE(TestCC(disputeTx, 0, disputeCond));
|
||||
EXPECT_EQ("no-evidence", eval.state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testMakeStakeTx)
|
||||
{
|
||||
CTransaction stakeTx = ebet.StakeTx();
|
||||
EXPECT_EQ(0, stakeTx.vin.size());
|
||||
EXPECT_EQ(1, stakeTx.vout.size());
|
||||
EXPECT_EQ(ebet.totalPayout, stakeTx.vout[0].nValue);
|
||||
EXPECT_EQ(CCPubKey(ebet.PayoutCond()), stakeTx.vout[0].scriptPubKey);
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testMakePayoutCond)
|
||||
{
|
||||
CC *payoutCond = ebet.PayoutCond();
|
||||
EXPECT_EQ("(1 of (3 of 5,5,5),(2 of (1 of 5,5,5),15))", CCShowStructure(payoutCond));
|
||||
EXPECT_EQ(0, memcmp(payoutCond->subconditions[1]->subconditions[1]->code+1,
|
||||
ebet.SessionTx().GetHash().begin(), 32));
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testSignPayout)
|
||||
{
|
||||
|
||||
CMutableTransaction payoutTx = ebet.AgreePayoutTx();
|
||||
CC *payoutCond = ebet.PayoutCond();
|
||||
|
||||
EXPECT_EQ(0, cc_isFulfilled(payoutCond->subconditions[0]));
|
||||
EXPECT_EQ(0, cc_isFulfilled(payoutCond->subconditions[1]));
|
||||
EXPECT_EQ(0, cc_isFulfilled(payoutCond));
|
||||
|
||||
EXPECT_EQ(2, CCSign(payoutTx, 0, payoutCond, {Player1}));
|
||||
EXPECT_EQ(0, cc_isFulfilled(payoutCond->subconditions[0]));
|
||||
EXPECT_EQ(1, cc_isFulfilled(payoutCond->subconditions[1]));
|
||||
EXPECT_EQ(1, cc_isFulfilled(payoutCond));
|
||||
|
||||
EXPECT_EQ(2, CCSign(payoutTx, 0, payoutCond, {Player2}));
|
||||
EXPECT_EQ(0, cc_isFulfilled(payoutCond->subconditions[0]));
|
||||
|
||||
EXPECT_EQ(2, CCSign(payoutTx, 0, payoutCond, {Dealer}));
|
||||
EXPECT_EQ(1, cc_isFulfilled(payoutCond->subconditions[0]));
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testAgreePayout)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(12);
|
||||
|
||||
CMutableTransaction payoutTx = ebet.AgreePayoutTx();
|
||||
CC *payoutCond = ebet.PayoutCond();
|
||||
|
||||
EXPECT_EQ(2, CCSign(payoutTx, 0, payoutCond, {Dealer}));
|
||||
EXPECT_FALSE(TestCC(payoutTx, 0, payoutCond));
|
||||
EXPECT_EQ("(1 of (2 of (1 of 5,A5,A5),15),A2)",
|
||||
CCShowStructure(CCPrune(payoutCond)));
|
||||
|
||||
EXPECT_EQ(2, CCSign(payoutTx, 0, payoutCond, {Player1}));
|
||||
EXPECT_FALSE(TestCC(payoutTx, 0, payoutCond));
|
||||
EXPECT_EQ("(1 of (2 of (1 of 5,A5,A5),15),A2)",
|
||||
CCShowStructure(CCPrune(payoutCond)));
|
||||
|
||||
EXPECT_EQ(2, CCSign(payoutTx, 0, payoutCond, {Player2}));
|
||||
EXPECT_TRUE( TestCC(payoutTx, 0, payoutCond));
|
||||
EXPECT_EQ("(1 of (3 of 5,5,5),A2)",
|
||||
CCShowStructure(CCPrune(payoutCond)));
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testImportPayout)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(12);
|
||||
|
||||
CMutableTransaction importTx = ebet.ImportPayoutTx();
|
||||
CC *payoutCond = ebet.PayoutCond();
|
||||
EXPECT_EQ(2, CCSign(importTx, 0, payoutCond, {Player2}));
|
||||
EXPECT_TRUE(TestCC(importTx, 0, payoutCond));
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testImportPayoutFewVouts)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(12);
|
||||
|
||||
CMutableTransaction importTx = ebet.ImportPayoutTx();
|
||||
importTx.vout.resize(0);
|
||||
CC *payoutCond = ebet.PayoutCond();
|
||||
EXPECT_EQ(2, CCSign(importTx, 0, payoutCond, {Player2}));
|
||||
EXPECT_FALSE(TestCC(importTx, 0, payoutCond));
|
||||
EXPECT_EQ("no-vouts", eval.state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testImportPayoutInvalidPayload)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(12);
|
||||
|
||||
CMutableTransaction importTx = ebet.ImportPayoutTx();
|
||||
importTx.vout[0].scriptPubKey.pop_back();
|
||||
CC *payoutCond = ebet.PayoutCond();
|
||||
EXPECT_EQ(2, CCSign(importTx, 0, payoutCond, {Player2}));
|
||||
EXPECT_FALSE(TestCC(importTx, 0, payoutCond));
|
||||
EXPECT_EQ("invalid-payload", eval.state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testImportPayoutWrongPayouts)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(12);
|
||||
|
||||
CMutableTransaction importTx = ebet.ImportPayoutTx();
|
||||
importTx.vout[1].nValue = 7;
|
||||
CC *payoutCond = ebet.PayoutCond();
|
||||
EXPECT_EQ(2, CCSign(importTx, 0, payoutCond, {Player2}));
|
||||
ASSERT_FALSE(TestCC(importTx, 0, payoutCond));
|
||||
EXPECT_EQ("wrong-payouts", eval.state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testImportPayoutMangleSessionId)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(12);
|
||||
|
||||
CMutableTransaction importTx = ebet.ImportPayoutTx();
|
||||
CC *payoutCond = ebet.PayoutCond();
|
||||
payoutCond->subconditions[1]->subconditions[1]->codeLength = 31;
|
||||
EXPECT_EQ(2, CCSign(importTx, 0, payoutCond, {Player2}));
|
||||
ASSERT_FALSE(TestCC(importTx, 0, payoutCond));
|
||||
EXPECT_EQ("malformed-params", eval.state.GetRejectReason());
|
||||
|
||||
payoutCond = ebet.PayoutCond();
|
||||
memset(payoutCond->subconditions[1]->subconditions[1]->code+1, 1, 32);
|
||||
EXPECT_EQ(2, CCSign(importTx, 0, payoutCond, {Player2}));
|
||||
ASSERT_FALSE(TestCC(importTx, 0, payoutCond));
|
||||
EXPECT_EQ("wrong-session", eval.state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testImportPayoutInvalidNotarisationHash)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(12);
|
||||
|
||||
MoMProof proof = ebet.GetMoMProof();
|
||||
proof.notarisationHash = uint256();
|
||||
CMutableTransaction importTx = ebet.bet.MakeImportPayoutTx(
|
||||
ebet.Payouts(Player2), ebet.DisputeTx(Player2), uint256(), proof);
|
||||
|
||||
CC *payoutCond = ebet.PayoutCond();
|
||||
EXPECT_EQ(2, CCSign(importTx, 0, payoutCond, {Player2}));
|
||||
EXPECT_FALSE(TestCC(importTx, 0, payoutCond));
|
||||
EXPECT_EQ("coudnt-load-mom", eval.state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(TestBet, testImportPayoutMomFail)
|
||||
{
|
||||
EvalMock eval = ebet.SetEvalMock(12);
|
||||
|
||||
MoMProof proof = ebet.GetMoMProof();
|
||||
proof.branch.nIndex ^= 1;
|
||||
CMutableTransaction importTx = ebet.bet.MakeImportPayoutTx(
|
||||
ebet.Payouts(Player2), ebet.DisputeTx(Player2), uint256(), proof);
|
||||
|
||||
CC *payoutCond = ebet.PayoutCond();
|
||||
EXPECT_EQ(2, CCSign(importTx, 0, payoutCond, {Player2}));
|
||||
EXPECT_FALSE(TestCC(importTx, 0, payoutCond));
|
||||
EXPECT_EQ("mom-check-fail", eval.state.GetRejectReason());
|
||||
}
|
||||
|
||||
|
||||
} /* namespace TestBet */
|
||||
206
src/test-hush/test_eval_notarisation.cpp
Normal file
206
src/test-hush/test_eval_notarisation.cpp
Normal file
@@ -0,0 +1,206 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
#include <cryptoconditions.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "cc/betprotocol.h"
|
||||
#include "cc/eval.h"
|
||||
#include "base58.h"
|
||||
#include "core_io.h"
|
||||
#include "key.h"
|
||||
#include "main.h"
|
||||
#include "script/cc.h"
|
||||
#include "primitives/transaction.h"
|
||||
#include "script/interpreter.h"
|
||||
#include "script/serverchecker.h"
|
||||
|
||||
#include "testutils.h"
|
||||
|
||||
|
||||
extern int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height,uint32_t timestamp);
|
||||
|
||||
|
||||
namespace TestEvalNotarisation {
|
||||
|
||||
|
||||
class EvalMock : public Eval
|
||||
{
|
||||
public:
|
||||
uint32_t nNotaries;
|
||||
uint8_t notaries[64][33];
|
||||
std::map<uint256, CTransaction> txs;
|
||||
std::map<uint256, CBlockIndex> blocks;
|
||||
|
||||
int32_t GetNotaries(uint8_t pubkeys[64][33], int32_t height, uint32_t timestamp) const
|
||||
{
|
||||
memcpy(pubkeys, notaries, sizeof(notaries));
|
||||
return nNotaries;
|
||||
}
|
||||
|
||||
bool GetTxUnconfirmed(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock) const
|
||||
{
|
||||
auto r = txs.find(hash);
|
||||
if (r != txs.end()) {
|
||||
txOut = r->second;
|
||||
if (blocks.count(hash) > 0)
|
||||
hashBlock = hash;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GetBlock(uint256 hash, CBlockIndex& blockIdx) const
|
||||
{
|
||||
auto r = blocks.find(hash);
|
||||
if (r == blocks.end()) return false;
|
||||
blockIdx = r->second;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
//static auto noop = [&](CMutableTransaction &mtx){};
|
||||
static auto noop = [](CMutableTransaction &mtx){};
|
||||
|
||||
|
||||
template<typename Modifier>
|
||||
void SetupEval(EvalMock &eval, CMutableTransaction ¬ary, Modifier modify)
|
||||
{
|
||||
eval.nNotaries = komodo_notaries(eval.notaries, 780060, 1522946781);
|
||||
|
||||
// make fake notary inputs
|
||||
notary.vin.resize(11);
|
||||
for (int i=0; i<notary.vin.size(); i++) {
|
||||
CMutableTransaction txIn;
|
||||
txIn.vout.resize(1);
|
||||
txIn.vout[0].scriptPubKey << VCH(eval.notaries[i*2], 33) << OP_CHECKSIG;
|
||||
notary.vin[i].prevout = COutPoint(txIn.GetHash(), 0);
|
||||
eval.txs[txIn.GetHash()] = CTransaction(txIn);
|
||||
}
|
||||
|
||||
modify(notary);
|
||||
|
||||
eval.txs[notary.GetHash()] = CTransaction(notary);
|
||||
eval.blocks[notary.GetHash()].SetHeight(780060);
|
||||
eval.blocks[notary.GetHash()].nTime = 1522946781;
|
||||
}
|
||||
|
||||
|
||||
// https://kmd.explorer.supernet.org/tx/5b8055d37cff745a404d1ae45e21ffdba62da7b28ed6533c67468d7379b20bae
|
||||
// inputs have been dropped
|
||||
static auto rawNotaryTx = "01000000000290460100000000002321020e46e79a2a8d12b9b5d12c7a91adb4e454edfae43c0a0cb805427d2ac7613fd9ac0000000000000000506a4c4dae8e0f3e6e5de498a072f5967f3c418c4faba5d56ac8ce17f472d029ef3000008f2e0100424f545300050ba773f0bc31da5839fc7cb9bd7b87f3b765ca608e5cf66785a466659b28880500000000000000";
|
||||
CTransaction notaryTx;
|
||||
static bool init = DecodeHexTx(notaryTx, rawNotaryTx);
|
||||
|
||||
static uint256 proofTxHash = uint256S("37f76551a16093fbb0a92ee635bbd45b3460da8fd00cf7d5a6b20d93e727fe4c");
|
||||
static auto vMomProof = ParseHex("0303faecbdd4b3da128c2cd2701bb143820a967069375b2ec5b612f39bbfe78a8611978871c193457ab1e21b9520f4139f113b8d75892eb93ee247c18bccfd067efed7eacbfcdc8946cf22de45ad536ec0719034fb9bc825048fe6ab61fee5bd6e9aae0bb279738d46673c53d68eb2a72da6dbff215ee41a4d405a74ff7cd355805b"); // $ fiat/bots txMoMproof $proofTxHash
|
||||
|
||||
/*
|
||||
TEST(TestEvalNotarisation, testGetNotarisation)
|
||||
{
|
||||
EvalMock eval;
|
||||
CMutableTransaction notary(notaryTx);
|
||||
SetupEval(eval, notary, noop);
|
||||
|
||||
NotarisationData data;
|
||||
ASSERT_TRUE(eval.GetNotarisationData(notary.GetHash(), data));
|
||||
EXPECT_EQ(data.height, 77455);
|
||||
EXPECT_EQ(data.blockHash.GetHex(), "000030ef29d072f417cec86ad5a5ab4f8c413c7f96f572a098e45d6e3e0f8eae");
|
||||
EXPECT_STREQ(data.symbol, "BOTS");
|
||||
EXPECT_EQ(data.MoMDepth, 5);
|
||||
EXPECT_EQ(data.MoM.GetHex(), "88289b6566a48567f65c8e60ca65b7f3877bbdb97cfc3958da31bcf073a70b05");
|
||||
|
||||
MoMProof proof;
|
||||
E_UNMARSHAL(vMomProof, ss >> proof);
|
||||
EXPECT_EQ(data.MoM, proof.branch.Exec(proofTxHash));
|
||||
}
|
||||
|
||||
|
||||
TEST(TestEvalNotarisation, testInvalidNotaryPubkey)
|
||||
{
|
||||
EvalMock eval;
|
||||
CMutableTransaction notary(notaryTx);
|
||||
SetupEval(eval, notary, noop);
|
||||
|
||||
memset(eval.notaries[10], 0, 33);
|
||||
|
||||
NotarisationData data;
|
||||
ASSERT_FALSE(eval.GetNotarisationData(notary.GetHash(), data));
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
TEST(TestEvalNotarisation, testInvalidNotarisationBadOpReturn)
|
||||
{
|
||||
EvalMock eval;
|
||||
CMutableTransaction notary(notaryTx);
|
||||
|
||||
notary.vout[1].scriptPubKey = CScript() << OP_RETURN << 0;
|
||||
SetupEval(eval, notary, noop);
|
||||
|
||||
NotarisationData data(0);
|
||||
ASSERT_FALSE(eval.GetNotarisationData(notary.GetHash(), data));
|
||||
}
|
||||
|
||||
|
||||
TEST(TestEvalNotarisation, testInvalidNotarisationTxNotEnoughSigs)
|
||||
{
|
||||
EvalMock eval;
|
||||
CMutableTransaction notary(notaryTx);
|
||||
|
||||
SetupEval(eval, notary, [](CMutableTransaction &tx) {
|
||||
tx.vin.resize(10);
|
||||
});
|
||||
|
||||
NotarisationData data(0);
|
||||
ASSERT_FALSE(eval.GetNotarisationData(notary.GetHash(), data));
|
||||
}
|
||||
|
||||
|
||||
TEST(TestEvalNotarisation, testInvalidNotarisationTxDoesntExist)
|
||||
{
|
||||
EvalMock eval;
|
||||
CMutableTransaction notary(notaryTx);
|
||||
|
||||
SetupEval(eval, notary, noop);
|
||||
|
||||
NotarisationData data(0);
|
||||
ASSERT_FALSE(eval.GetNotarisationData(uint256(), data));
|
||||
}
|
||||
|
||||
|
||||
TEST(TestEvalNotarisation, testInvalidNotarisationDupeNotary)
|
||||
{
|
||||
EvalMock eval;
|
||||
CMutableTransaction notary(notaryTx);
|
||||
|
||||
SetupEval(eval, notary, [](CMutableTransaction &tx) {
|
||||
tx.vin[1] = tx.vin[3];
|
||||
});
|
||||
|
||||
NotarisationData data(0);
|
||||
ASSERT_FALSE(eval.GetNotarisationData(notary.GetHash(), data));
|
||||
}
|
||||
|
||||
|
||||
TEST(TestEvalNotarisation, testInvalidNotarisationInputNotCheckSig)
|
||||
{
|
||||
EvalMock eval;
|
||||
CMutableTransaction notary(notaryTx);
|
||||
|
||||
SetupEval(eval, notary, [&](CMutableTransaction &tx) {
|
||||
int i = 1;
|
||||
CMutableTransaction txIn;
|
||||
txIn.vout.resize(1);
|
||||
txIn.vout[0].scriptPubKey << VCH(eval.notaries[i*2], 33) << OP_RETURN;
|
||||
notary.vin[i].prevout = COutPoint(txIn.GetHash(), 0);
|
||||
eval.txs[txIn.GetHash()] = CTransaction(txIn);
|
||||
});
|
||||
|
||||
NotarisationData data(0);
|
||||
ASSERT_FALSE(eval.GetNotarisationData(notary.GetHash(), data));
|
||||
}
|
||||
|
||||
|
||||
|
||||
} /* namespace TestEvalNotarisation */
|
||||
81
src/test-hush/test_netbase_tests.cpp
Normal file
81
src/test-hush/test_netbase_tests.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
#include "addrman.h"
|
||||
#include <string>
|
||||
#include "netbase.h"
|
||||
|
||||
#define GTEST_COUT_NOCOLOR std::cerr << "[ ] [ INFO ] "
|
||||
namespace testing
|
||||
{
|
||||
namespace internal
|
||||
{
|
||||
enum GTestColor {
|
||||
COLOR_DEFAULT,
|
||||
COLOR_RED,
|
||||
COLOR_GREEN,
|
||||
COLOR_YELLOW
|
||||
};
|
||||
|
||||
extern void ColoredPrintf(GTestColor color, const char* fmt, ...);
|
||||
}
|
||||
}
|
||||
#define PRINTF(...) do { testing::internal::ColoredPrintf(testing::internal::COLOR_GREEN, "[ ] "); testing::internal::ColoredPrintf(testing::internal::COLOR_YELLOW, __VA_ARGS__); } while(0)
|
||||
|
||||
// C++ stream interface
|
||||
class TestCout : public std::stringstream
|
||||
{
|
||||
public:
|
||||
~TestCout()
|
||||
{
|
||||
PRINTF("%s",str().c_str());
|
||||
}
|
||||
};
|
||||
|
||||
#define GTEST_COUT_COLOR TestCout()
|
||||
|
||||
using namespace std;
|
||||
|
||||
static CNetAddr ResolveIP(const std::string& ip)
|
||||
{
|
||||
vector<CNetAddr> vIPs;
|
||||
CNetAddr addr;
|
||||
if (LookupHost(ip.c_str(), vIPs)) {
|
||||
addr = vIPs[0];
|
||||
} else
|
||||
{
|
||||
// it was BOOST_CHECK_MESSAGE, but we can't use ASSERT outside a test
|
||||
GTEST_COUT_COLOR << strprintf("failed to resolve: %s", ip) << std::endl;
|
||||
}
|
||||
return addr;
|
||||
}
|
||||
|
||||
namespace TestNetBaseTests {
|
||||
|
||||
TEST(TestAddrmanTests, netbase_getgroup) {
|
||||
|
||||
std::vector<bool> asmap; // use /16
|
||||
ASSERT_TRUE(ResolveIP("127.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // Local -> !Routable()
|
||||
ASSERT_TRUE(ResolveIP("257.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // !Valid -> !Routable()
|
||||
ASSERT_TRUE(ResolveIP("10.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()
|
||||
ASSERT_TRUE(ResolveIP("169.254.1.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // RFC3927 -> !Routable()
|
||||
ASSERT_TRUE(ResolveIP("1.2.3.4").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // IPv4
|
||||
|
||||
// std::vector<unsigned char> vch = ResolveIP("4.3.2.1").GetGroup(asmap);
|
||||
// GTEST_COUT_COLOR << boost::to_string((int)vch[0]) << boost::to_string((int)vch[1]) << boost::to_string((int)vch[2]) << std::endl;
|
||||
|
||||
ASSERT_TRUE(ResolveIP("::FFFF:0:102:304").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6145
|
||||
ASSERT_TRUE(ResolveIP("64:FF9B::102:304").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6052
|
||||
ASSERT_TRUE(ResolveIP("2002:102:304:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC3964
|
||||
ASSERT_TRUE(ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC4380
|
||||
ASSERT_TRUE(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_ONION, 239})); // Tor
|
||||
ASSERT_TRUE(ResolveIP("2001:470:abcd:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV6, 32, 1, 4, 112, 175})); //he.net
|
||||
ASSERT_TRUE(ResolveIP("2001:2001:9999:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV6, 32, 1, 32, 1})); //IPv6
|
||||
|
||||
}
|
||||
}
|
||||
57
src/test-hush/test_parse_notarisation.cpp
Normal file
57
src/test-hush/test_parse_notarisation.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "cc/eval.h"
|
||||
#include "core_io.h"
|
||||
#include "key.h"
|
||||
|
||||
#include "testutils.h"
|
||||
|
||||
|
||||
namespace TestParseNotarisation {
|
||||
|
||||
class TestParseNotarisation : public ::testing::Test, public Eval {};
|
||||
|
||||
|
||||
TEST(TestParseNotarisation, test_ee2fa)
|
||||
{
|
||||
// ee2fa47820a31a979f9f21cb3fedbc484bf9a8957cb6c9acd0af28ced29bdfe1
|
||||
std::vector<uint8_t> opret = ParseHex("c349ff90f3bce62c1b7b49d1da0423b1a3d9b733130cce825b95b9e047c729066e020d00743a06fdb95ad5775d032b30bbb3680dac2091a0f800cf54c79fd3461ce9b31d4b4d4400");
|
||||
NotarisationData nd;
|
||||
ASSERT_TRUE(E_UNMARSHAL(opret, ss >> nd));
|
||||
}
|
||||
|
||||
TEST(TestParseNotarisation, test__)
|
||||
{
|
||||
// 576e910a1f704207bcbcf724124ff9adc5237f45cb6919589cd0aa152caec424
|
||||
std::vector<uint8_t> opret = ParseHex("b3ed7fbbfbc027caeeeec81e65489ec5d9cd47cda675a5cbb75b4a845e67cf0ef6330300b5a6bd8385feb833f3be961c9d8a46fcecd36dcdfa42ad81a20a892433722f0b4b4d44004125a06024eae24c11f36ea110acd707b041d5355b6e1b42de5e2614357999c6aa02000d26ad0300000000404b4c000000000005130300500d000061f22ba7d19fe29ac3baebd839af8b7127d1f90755534400");
|
||||
NotarisationData nd;
|
||||
// We can't parse this one
|
||||
ASSERT_FALSE(E_UNMARSHAL(opret, ss >> nd));
|
||||
}
|
||||
|
||||
TEST(TestParseNotarisation, test__a)
|
||||
{
|
||||
// be55101e6c5a93fb3611a44bd66217ad8714d204275ea4e691cfff9d65dff85c TXSCL
|
||||
std::vector<uint8_t> opret = ParseHex("fb9ea2818eec8b07f8811bab49d64379db074db478997f8114666f239bd79803cc460000d0fac4e715b7e2b917a5d79f85ece0c423d27bd3648fd39ac1dc7db8e1bd4b16545853434c00a69eab9f23d7fb63c4624973e7a9079d6ada2f327040936356d7af5e849f6d670a0003001caf7b7b9e1c9bc59d0c7a619c9683ab1dd0794b6f3ea184a19f8fda031150e700000000");
|
||||
NotarisationData nd(1);
|
||||
bool res = E_UNMARSHAL(opret, ss >> nd);
|
||||
ASSERT_TRUE(res);
|
||||
}
|
||||
|
||||
TEST(TestParseNotarisation, test__b)
|
||||
{
|
||||
// 03085dafed656aaebfda25bf43ffe9d1fb72565bb1fc8b2a12a631659f28f877 TXSCL
|
||||
std::vector<uint8_t> opret = ParseHex("48c71a10aa060eab1a43f52acefac3b81fb2a2ce310186b06141884c0501d403c246000052e6d49afd82d9ab3d97c996dd9b6a78a554ffa1625e8dadf0494bd1f8442e3e545853434c007cc5c07e3b67520fd14e23cd5b49f2aa022f411500fd3326ff91e6dc0544a1c90c0003008b69117bb1376ac8df960f785d8c208c599d3a36248c98728256bb6d4737e59600000000");
|
||||
NotarisationData nd(1);
|
||||
bool res = E_UNMARSHAL(opret, ss >> nd);
|
||||
ASSERT_TRUE(res);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// for l in `g 'parse notarisation' ~/.komodo/debug.log | pyline 'l.split()[8]'`; do hoek decodeTx '{"hex":"'`src/komodo-cli getrawtransaction "$l"`'"}' | jq '.outputs[1].script.op_return' | pyline 'import base64; print base64.b64decode(l).encode("hex")'; done
|
||||
|
||||
}
|
||||
150
src/test-hush/testutils.cpp
Normal file
150
src/test-hush/testutils.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
#include <cryptoconditions.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include "core_io.h"
|
||||
#include "key.h"
|
||||
#include "main.h"
|
||||
#include "miner.h"
|
||||
#include "notarisationdb.h"
|
||||
#include "random.h"
|
||||
#include "rpc/server.h"
|
||||
#include "rpc/protocol.h"
|
||||
#include "txdb.h"
|
||||
#include "util.h"
|
||||
#include "utilstrencodings.h"
|
||||
#include "utiltime.h"
|
||||
#include "consensus/validation.h"
|
||||
#include "primitives/transaction.h"
|
||||
#include "script/cc.h"
|
||||
#include "script/interpreter.h"
|
||||
|
||||
#include "testutils.h"
|
||||
|
||||
|
||||
std::string notaryPubkey = "0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47";
|
||||
std::string notarySecret = "UxFWWxsf1d7w7K5TvAWSkeX4H95XQKwdwGv49DXwWUTzPTTjHBbU";
|
||||
CKey notaryKey;
|
||||
|
||||
|
||||
/*
|
||||
* We need to have control of clock,
|
||||
* otherwise block production can fail.
|
||||
*/
|
||||
int64_t nMockTime;
|
||||
|
||||
extern uint32_t USE_EXTERNAL_PUBKEY;
|
||||
extern std::string NOTARY_PUBKEY;
|
||||
|
||||
void setupChain()
|
||||
{
|
||||
SelectParams(CBaseChainParams::REGTEST);
|
||||
|
||||
// Settings to get block reward
|
||||
NOTARY_PUBKEY = notaryPubkey;
|
||||
USE_EXTERNAL_PUBKEY = 1;
|
||||
mapArgs["-mineraddress"] = "bogus";
|
||||
COINBASE_MATURITY = 1;
|
||||
// Global mock time
|
||||
nMockTime = GetTime();
|
||||
|
||||
// Unload
|
||||
UnloadBlockIndex();
|
||||
|
||||
// Init blockchain
|
||||
ClearDatadirCache();
|
||||
auto pathTemp = GetTempPath() / strprintf("test_komodo_%li_%i", GetTime(), GetRand(100000));
|
||||
if (ASSETCHAINS_SYMBOL[0])
|
||||
pathTemp = pathTemp / strprintf("_%s", ASSETCHAINS_SYMBOL);
|
||||
boost::filesystem::create_directories(pathTemp);
|
||||
mapArgs["-datadir"] = pathTemp.string();
|
||||
pblocktree = new CBlockTreeDB(1 << 20, true);
|
||||
CCoinsViewDB *pcoinsdbview = new CCoinsViewDB(1 << 23, true);
|
||||
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
|
||||
pnotarisations = new NotarisationDB(1 << 20, true);
|
||||
InitBlockIndex();
|
||||
}
|
||||
|
||||
|
||||
void generateBlock(CBlock *block)
|
||||
{
|
||||
UniValue params;
|
||||
params.setArray();
|
||||
params.push_back(1);
|
||||
uint256 blockId;
|
||||
|
||||
SetMockTime(nMockTime+=100); // CreateNewBlock can fail if not enough time passes
|
||||
|
||||
try {
|
||||
UniValue out = generate(params, false, CPubKey());
|
||||
blockId.SetHex(out[0].getValStr());
|
||||
if (block) ASSERT_TRUE(ReadBlockFromDisk(*block, mapBlockIndex[blockId], false));
|
||||
} catch (const UniValue& e) {
|
||||
FAIL() << "failed to create block: " << e.write().data();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void acceptTxFail(const CTransaction tx)
|
||||
{
|
||||
CValidationState state;
|
||||
if (!acceptTx(tx, state)) FAIL() << state.GetRejectReason();
|
||||
}
|
||||
|
||||
|
||||
bool acceptTx(const CTransaction tx, CValidationState &state)
|
||||
{
|
||||
LOCK(cs_main);
|
||||
return AcceptToMemoryPool(mempool, state, tx, false, NULL);
|
||||
}
|
||||
|
||||
|
||||
CMutableTransaction spendTx(const CTransaction &txIn, int nOut)
|
||||
{
|
||||
CMutableTransaction mtx;
|
||||
mtx.vin.resize(1);
|
||||
mtx.vin[0].prevout.hash = txIn.GetHash();
|
||||
mtx.vin[0].prevout.n = nOut;
|
||||
mtx.vout.resize(1);
|
||||
mtx.vout[0].nValue = txIn.vout[nOut].nValue - 1000;
|
||||
return mtx;
|
||||
}
|
||||
|
||||
|
||||
std::vector<uint8_t> getSig(const CMutableTransaction mtx, CScript inputPubKey, int nIn)
|
||||
{
|
||||
uint256 hash = SignatureHash(inputPubKey, mtx, nIn, SIGHASH_ALL, 0, 0);
|
||||
std::vector<uint8_t> vchSig;
|
||||
notaryKey.Sign(hash, vchSig);
|
||||
vchSig.push_back((unsigned char)SIGHASH_ALL);
|
||||
return vchSig;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* In order to do tests there needs to be inputs to spend.
|
||||
* This method creates a block and returns a transaction that spends the coinbase.
|
||||
*/
|
||||
void getInputTx(CScript scriptPubKey, CTransaction &txIn)
|
||||
{
|
||||
// Get coinbase
|
||||
CBlock block;
|
||||
generateBlock(&block);
|
||||
CTransaction coinbase = block.vtx[0];
|
||||
|
||||
// Create tx
|
||||
auto mtx = spendTx(coinbase);
|
||||
mtx.vout[0].scriptPubKey = scriptPubKey;
|
||||
uint256 hash = SignatureHash(coinbase.vout[0].scriptPubKey, mtx, 0, SIGHASH_ALL, 0, 0);
|
||||
std::vector<unsigned char> vchSig;
|
||||
notaryKey.Sign(hash, vchSig);
|
||||
vchSig.push_back((unsigned char)SIGHASH_ALL);
|
||||
mtx.vin[0].scriptSig << vchSig;
|
||||
|
||||
// Accept
|
||||
acceptTxFail(mtx);
|
||||
txIn = CTransaction(mtx);
|
||||
}
|
||||
32
src/test-hush/testutils.h
Normal file
32
src/test-hush/testutils.h
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) 2019-2020 The Hush developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
#ifndef TESTUTILS_H
|
||||
#define TESTUTILS_H
|
||||
|
||||
#include "main.h"
|
||||
|
||||
|
||||
#define VCH(a,b) std::vector<unsigned char>(a, a + b)
|
||||
|
||||
static char ccjsonerr[1000] = "\0";
|
||||
#define CCFromJson(o,s) \
|
||||
o = cc_conditionFromJSONString(s, ccjsonerr); \
|
||||
if (!o) FAIL() << "bad json: " << ccjsonerr;
|
||||
|
||||
|
||||
extern std::string notaryPubkey;
|
||||
extern std::string notarySecret;
|
||||
extern CKey notaryKey;
|
||||
|
||||
|
||||
void setupChain();
|
||||
void generateBlock(CBlock *block=NULL);
|
||||
bool acceptTx(const CTransaction tx, CValidationState &state);
|
||||
void acceptTxFail(const CTransaction tx);
|
||||
void getInputTx(CScript scriptPubKey, CTransaction &txIn);
|
||||
CMutableTransaction spendTx(const CTransaction &txIn, int nOut=0);
|
||||
std::vector<uint8_t> getSig(const CMutableTransaction mtx, CScript inputPubKey, int nIn=0);
|
||||
|
||||
|
||||
#endif /* TESTUTILS_H */
|
||||
Reference in New Issue
Block a user