Working python bindings

Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
Anthony Fieroni 2019-10-31 18:19:00 +02:00 committed by Brannon King
parent e386039392
commit 6d0b8e8196
13 changed files with 208 additions and 127 deletions

View file

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.10)
project(claimtrie)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
include(../../contrib/cmake/cmake/CPM.cmake)
@ -12,90 +12,43 @@ set(CLAIMTRIE_SRC
forks.cpp
hashes.cpp
log.cpp
prefixtrie.cpp
trie.cpp
txoutpoint.cpp
uints.cpp
sqlite/sqlite3.c
)
if(BIND)
find_program(SWIG NAMES swig)
string(TOLOWER ${BIND} BIND)
set(INTERFACE_NAME libclaimtrie)
if(${BIND} STREQUAL "python")
find_package(PythonInterp 3.6 REQUIRED)
find_package(PythonLibs 3.6 REQUIRED)
set(BIND_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS})
set(SWIG_OPTIONS -python;-py3)
set(CMAKE_SHARED_LIBRARY_PREFIX _lib)
else()
message(FATAL_ERROR "Implement a handler for ${BIND}")
endif()
add_custom_command(OUTPUT ${INTERFACE_NAME}_wrap.cxx
COMMAND ${SWIG}
ARGS -c++ ${SWIG_OPTIONS} -outcurrentdir ${CMAKE_CURRENT_SOURCE_DIR}/${INTERFACE_NAME}.i
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
set(CLAIMTRIE_SRC ${CLAIMTRIE_SRC}
${INTERFACE_NAME}_wrap.cxx
)
endif()
add_library(claimtrie SHARED ${CLAIMTRIE_SRC})
target_include_directories(claimtrie PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
# TODO remove leveldb depends
set(LEVELDB_SRC
../leveldb/db/builder.cc
../leveldb/db/c.cc
../leveldb/db/dbformat.cc
../leveldb/db/db_impl.cc
../leveldb/db/db_iter.cc
../leveldb/db/dumpfile.cc
../leveldb/db/filename.cc
../leveldb/db/log_reader.cc
../leveldb/db/log_writer.cc
../leveldb/db/memtable.cc
../leveldb/db/repair.cc
../leveldb/db/table_cache.cc
../leveldb/db/version_edit.cc
../leveldb/db/version_set.cc
../leveldb/db/write_batch.cc
../leveldb/table/block_builder.cc
../leveldb/table/block.cc
../leveldb/table/filter_block.cc
../leveldb/table/format.cc
../leveldb/table/iterator.cc
../leveldb/table/merger.cc
../leveldb/table/table_builder.cc
../leveldb/table/table.cc
../leveldb/table/two_level_iterator.cc
../leveldb/util/arena.cc
../leveldb/util/bloom.cc
../leveldb/util/cache.cc
../leveldb/util/coding.cc
../leveldb/util/comparator.cc
../leveldb/util/crc32c.cc
../leveldb/util/env.cc
../leveldb/util/env_posix.cc
../leveldb/util/filter_policy.cc
../leveldb/util/hash.cc
../leveldb/util/histogram.cc
../leveldb/util/logging.cc
../leveldb/util/options.cc
../leveldb/util/status.cc
)
if(WIN32)
set(LEVELDB_SRC ${LEVELDB_SRC}
../leveldb/util/env_win.cc
../leveldb/port/port_win.cc
)
else()
set(LEVELDB_SRC ${LEVELDB_SRC}
../leveldb/port/port_posix.cc
)
if(BIND_INCLUDE_DIRS)
target_include_directories(claimtrie PRIVATE ${BIND_INCLUDE_DIRS})
endif()
add_library(leveldb STATIC ${LEVELDB_SRC})
target_include_directories(leveldb PRIVATE ../leveldb ../leveldb/include)
if(WIN32)
target_compile_definitions(leveldb PRIVATE -DLEVELDB_PLATFORM_WINDOWS -DWINVER=0x0500 -D__USE_MINGW_ANSI_STDIO=1)
else()
target_compile_definitions(leveldb PRIVATE -DLEVELDB_PLATFORM_POSIX)
endif()
target_compile_definitions(leveldb PRIVATE -DLEVELDB_ATOMIC_PRESENT -D__STDC_LIMIT_MACROS)
target_link_libraries(claimtrie PRIVATE leveldb)
# leveldb depends end here
# TODO remove BITCOIN depends
target_include_directories(claimtrie PRIVATE ../ ../leveldb/include)
# it's needed a configure call to create config files
target_compile_definitions(claimtrie PRIVATE -DHAVE_CONFIG_H)
# bitcoin depends end here
CPMAddPackage(
NAME OpenSSL
GITHUB_REPOSITORY openssl/openssl

View file

@ -77,8 +77,3 @@ CNameOutPointHeightType::CNameOutPointHeightType(std::string name, CTxOutPoint o
: name(std::move(name)), outPoint(std::move(outPoint)), nValidHeight(nValidHeight)
{
}
CClaimIndexElement::CClaimIndexElement(std::string name, CClaimValue claim)
: name(std::move(name)), claim(std::move(claim))
{
}

View file

@ -2,7 +2,7 @@
#ifndef CLAIMTRIE_DATA_H
#define CLAIMTRIE_DATA_H
#include <claimtrie/sqlite/sqlite3.h>
#include <sqlite/sqlite3.h>
#include <txoutpoint.h>
#include <uints.h>
@ -29,7 +29,7 @@ namespace sqlite
}
}
#include <claimtrie/sqlite/hdr/sqlite_modern_cpp.h>
#include <sqlite/hdr/sqlite_modern_cpp.h>
namespace sqlite
{
@ -119,13 +119,4 @@ struct CNameOutPointHeightType
CNameOutPointHeightType(std::string name, CTxOutPoint outPoint, int nValidHeight);
};
struct CClaimIndexElement
{
std::string name;
CClaimValue claim;
CClaimIndexElement() = default;
CClaimIndexElement(std::string name, CClaimValue claim);
};
#endif // CLAIMTRIE_DATA_H

View file

@ -0,0 +1,57 @@
%module libclaimtrie
%{
#include "uints.h"
#include "txoutpoint.h"
#include "data.h"
#include "trie.h"
#include "forks.h"
%}
%feature("directors", 1);
%feature("flatnested", 1);
%include stl.i
%include stdint.i
%include std_pair.i
%ignore CBaseBlob(CBaseBlob &&);
%ignore CClaimIndexElement(CClaimIndexElement &&);
%ignore CClaimNsupports(CClaimNsupports &&);
%ignore CClaimTrieProof(CClaimTrieProof &&);
%ignore CClaimTrieProofNode(CClaimTrieProofNode &&);
%ignore CClaimValue(CClaimValue &&);
%ignore CSupportValue(CSupportValue &&);
%ignore CTxOutPoint(CTxOutPoint &&);
#define SWIG_INTERFACE
%include "uints.h"
%include "txoutpoint.h"
%include "data.h"
%include "trie.h"
%include "forks.h"
%template(CUint160) CBaseBlob<160>;
%template(CUint256) CBaseBlob<256>;
%template(uint8vec) std::vector<uint8_t>;
%template(claimEntryType) std::vector<CClaimValue>;
%template(supportEntryType) std::vector<CSupportValue>;
%template(claimsNsupports) std::vector<CClaimNsupports>;
%template(proofPair) std::pair<bool, CUint256>;
%template(proofNodePair) std::pair<unsigned char, CUint256>;
%template(claimUndoPair) std::pair<std::string, CClaimValue>;
%template(supportUndoPair) std::pair<std::string, CSupportValue>;
%template(takeoverUndoPair) std::pair<std::string, <std::pair<int, CUint160>>;
%template(proofNodes) std::vector<CClaimTrieProofNode>;
%template(proofPairs) std::vector<std::pair<bool, CUint256>>;
%template(proofNodeChildren) std::vector<std::pair<unsigned char, CUint256>>;
%template(claimUndoType) std::vector<claimUndoPair>;
%template(supportUndoType) std::vector<supportUndoPair>;
%template(insertUndoType) std::vector<CNameOutPointHeightType>;
%template(takeoverUndoType) std::vector<takeoverUndoPair>;
%rename(CClaimTrieCache) CClaimTrieCacheHashFork;

View file

@ -0,0 +1,86 @@
from libclaimtrie import *
import unittest
class TestClaimTrieTypes(unittest.TestCase):
def setUp(self):
self.uint256s = "1234567890987654321012345678909876543210123456789098765432101234"
self.uint160 = CUint160S("1234567890987654321012345678909876543210")
self.uint = CUint256S(self.uint256s)
self.txp = CTxOutPoint(self.uint, 1)
def assertClaimEqual(self, claim, txo, cid, amount, effe, height, validHeight, msg):
self.assertEqual(claim.outPoint, txo, msg)
self.assertEqual(claim.claimId, cid, msg)
self.assertEqual(claim.nAmount, amount, msg)
self.assertEqual(claim.nEffectiveAmount, effe, msg)
self.assertEqual(claim.nHeight, height, msg)
self.assertEqual(claim.nValidAtHeight, validHeight, msg)
def assertSupportEqual(self, support, txo, cid, amount, height, validHeight, msg):
self.assertEqual(support.outPoint, txo, msg)
self.assertEqual(support.supportedClaimId, cid, msg)
self.assertEqual(support.nAmount, amount, msg)
self.assertEqual(support.nHeight, height, msg)
self.assertEqual(support.nValidAtHeight, validHeight, msg)
def test_uint256(self):
uint = self.uint
self.assertFalse(uint.IsNull(), "incorrect CUint256S or CBaseBlob::IsNull")
self.assertEqual(uint.GetHex(), self.uint256s, "incorrect CBaseBlob::GetHex")
self.assertEqual(uint.GetHex(), uint.ToString(), "incorrect CBaseBlob::ToString")
self.assertEqual(uint.size(), 32, "incorrect CBaseBlob::size")
copy = CUint256()
self.assertNotEqual(copy, uint, "incorrect CBaseBlob::operator!=")
self.assertTrue(copy.IsNull(), "incorrect CBaseBlob::IsNull")
copy = CUint256(uint)
self.assertEqual(copy, uint, "incorrect CBaseBlob::operator==")
copy.SetNull()
self.assertTrue(copy.IsNull()), "incorrect CBaseBlob::SetNull"
def test_txoupoint(self):
txp = self.txp
uint = self.uint
self.assertEqual(txp.hash, uint, "incorrect CTxOutPoint::CTxOutPoint")
self.assertEqual(txp.n, 1, "incorrect CTxOutPoint::CTxOutPoint")
self.assertFalse(txp.IsNull(), "incorrect CTxOutPoint::IsNull")
pcopy = CTxOutPoint()
self.assertTrue(pcopy.IsNull(), "incorrect CTxOutPoint::IsNull")
self.assertEqual(pcopy.hash, CUint256(), "incorrect CTxOutPoint::CTxOutPoint")
self.assertNotEqual(pcopy, txp, "incorrect CTxOutPoint::operator!=")
self.assertIn(uint.ToString()[:10], txp.ToString(), "incorrect CTxOutPoint::ToString")
def test_claim(self):
txp = self.txp
uint160 = self.uint160
self.assertEqual(uint160.size(), 20, "incorrect CBaseBlob::size")
claim = CClaimValue(txp, uint160, 20, 1, 10)
self.assertClaimEqual(claim, txp, uint160, 20, 20, 1, 10, "incorrect CClaimValue::CClaimValue")
def test_support(self):
txp = self.txp
uint160 = self.uint160
claim = CClaimValue(txp, uint160, 20, 1, 10)
support = CSupportValue(txp, uint160, 20, 1, 10)
self.assertSupportEqual(support, claim.outPoint, claim.claimId, claim.nAmount, claim.nHeight, claim.nValidAtHeight, "incorrect CSupportValue::CSupportValue")
def test_claimtrie(self):
txp = self.txp
uint160 = self.uint160
claim = CClaimValue(txp, uint160, 20, 1, 10)
data = CClaimTrieData()
data.insertClaim(claim)
wipe = True; height = 0; data_dir = "."
trie = CClaimTrie(wipe, height, data_dir)
cache = CClaimTrieCacheBase(trie)
self.assertTrue(cache.empty(), "incorrect CClaimtrieCache::empty")
self.assertFalse(cache.haveClaim("test", txp), "incorrect CClaimtrieCache::haveClaim")
self.assertTrue(cache.addClaim("tes", txp, uint160, 20, 0), "incorrect CClaimtrieCache::addClaim")
self.assertEqual(cache.getTotalNamesInTrie(), 1, "incorrect CClaimtrieCache::getTotalNamesInTrie")
self.assertEqual(cache.getTotalClaimsInTrie(), 1, "incorrect CClaimtrieCache::getTotalClaimsInTrie")
claimsToName = cache.getClaimsForName("test")
claims = claimsToName.claimsNsupports
self.assertEqual(claims.size(), 1, "incorrect CClaimTrieCache::getClaimsForName")
self.assertFalse(claims[0].IsNull(), "incorrect CClaimNsupports::IsNull")
unittest.main()

View file

@ -452,7 +452,7 @@ CUint256 CClaimTrieCacheBase::recursiveComputeMerkleHash(const std::string& name
std::vector<std::tuple<std::string, std::unique_ptr<CUint256>, int>> children;
childHashQuery << name >> [&children](std::string name, std::unique_ptr<CUint256> hash, int takeoverHeight) {
children.push_back(std::make_tuple(std::move(name), std::move(hash), takeoverHeight));
}
};
childHashQuery++;
for (auto& child: children) {
@ -460,7 +460,7 @@ CUint256 CClaimTrieCacheBase::recursiveComputeMerkleHash(const std::string& name
auto& hash = std::get<1>(child);
if (!hash) hash = std::make_unique<CUint256>();
if (hash->IsNull())
hash = recursiveComputeMerkleHash(name, std::get<2>(child), checkOnly);
*hash = recursiveComputeMerkleHash(name, std::get<2>(child), checkOnly);
completeHash(*hash, name, pos);
vchToHash.push_back(name[pos]);
vchToHash.insert(vchToHash.end(), hash->begin(), hash->end());

View file

@ -55,17 +55,18 @@ class CClaimTrie
friend class CClaimTrieCacheHashFork;
friend class CClaimTrieCacheExpirationFork;
friend class CClaimTrieCacheNormalizationFork;
public:
CClaimTrie() = default;
CClaimTrie(CClaimTrie&&) = delete;
CClaimTrie(const CClaimTrie&) = delete;
CClaimTrie(bool fWipe, int height,
const std::string& dataDir,
int nNormalizedNameForkHeight,
int64_t nOriginalClaimExpirationTime,
int64_t nExtendedClaimExpirationTime,
int64_t nExtendedClaimExpirationForkHeight,
int64_t nAllClaimsInMerkleForkHeight,
int nNormalizedNameForkHeight = -1,
int64_t nOriginalClaimExpirationTime = -1,
int64_t nExtendedClaimExpirationTime = -1,
int64_t nExtendedClaimExpirationForkHeight = -1,
int64_t nAllClaimsInMerkleForkHeight = -1,
int proportionalDelayFactor = 32);
CClaimTrie& operator=(CClaimTrie&&) = delete;
@ -77,7 +78,7 @@ public:
protected:
int nNextHeight = 0;
sqlite::database db;
const int nProportionalDelayFactor = 0;
const int nProportionalDelayFactor = 1;
const int nNormalizedNameForkHeight = -1;
const int64_t nOriginalClaimExpirationTime = -1;
@ -90,6 +91,7 @@ struct CClaimTrieProofNode
{
CClaimTrieProofNode(std::vector<std::pair<unsigned char, CUint256>> children, bool hasValue, CUint256 valHash);
CClaimTrieProofNode() = default;
CClaimTrieProofNode(CClaimTrieProofNode&&) = default;
CClaimTrieProofNode(const CClaimTrieProofNode&) = default;
CClaimTrieProofNode& operator=(CClaimTrieProofNode&&) = default;
@ -118,10 +120,20 @@ struct CClaimTrieProof
template <typename T>
using queueEntryType = std::pair<std::string, T>;
typedef std::vector<queueEntryType<CClaimValue>> claimUndoType;
typedef std::vector<queueEntryType<CSupportValue>> supportUndoType;
#ifdef SWIG_INTERFACE // swig has a problem with using in typedef
using claimUndoPair = std::pair<std::string, CClaimValue>;
using supportUndoPair = std::pair<std::string, CSupportValue>;
using takeoverUndoPair = std::pair<std::string, <std::pair<int, CUint160>>;
#else
using claimUndoPair = queueEntryType<CClaimValue>;
using supportUndoPair = queueEntryType<CSupportValue>;
using takeoverUndoPair = queueEntryType<std::pair<int, CUint160>>;
#endif
typedef std::vector<claimUndoPair> claimUndoType;
typedef std::vector<supportUndoPair> supportUndoType;
typedef std::vector<CNameOutPointHeightType> insertUndoType;
typedef std::vector<queueEntryType<std::pair<int, CUint160>>> takeoverUndoType;
typedef std::vector<takeoverUndoPair> takeoverUndoType;
class CClaimTrieCacheBase
{

View file

@ -5,6 +5,7 @@
#include <uints.h>
#include <algorithm>
#include <string>
#include <type_traits>
#include <vector>
#include <utility>
@ -34,4 +35,8 @@ public:
std::string ToString() const;
};
#ifndef SWIG_INTERFACE
#endif // SWIG_INTERFACE
#endif // CLAIMTRIE_TXOUTPUT_H

View file

@ -88,18 +88,4 @@ void Unserialize(Stream& s, CNameOutPointHeightType& u)
Unserialize(s, u.nValidHeight);
}
template<typename Stream>
void Serialize(Stream& s, const CClaimIndexElement& u)
{
Serialize(s, u.name);
Serialize(s, u.claim);
}
template<typename Stream>
void Unserialize(Stream& s, CClaimIndexElement& u)
{
Unserialize(s, u.name);
Unserialize(s, u.claim);
}
#endif // CLAIMTRIE_SERIAL_H

View file

@ -1540,7 +1540,7 @@ bool AppInitMain()
}
auto tip = chainActive.Tip();
assert(tip);
if (!CClaimTrieCache(pclaimTrie).validateDb(tip->hashClaimTrie))
if (!CClaimTrieCache(pclaimTrie).validateDb(tip->hashClaimTrie)) {
strLoadError = _("Error loading the claim trie from disk");
break;
}

View file

@ -440,15 +440,15 @@ BOOST_AUTO_TEST_CASE(claimtrie_update_takeover_test)
fixture.getLastTakeoverForName("test", cid2, takeover);
CClaimValue value;
BOOST_REQUIRE(fixture.getInfoForName("test", value) && value.nAmount == 3);
BOOST_CHECK_EQUAL(cid, cid2);
BOOST_CHECK_EQUAL(cid, uint160(cid2));
BOOST_CHECK_EQUAL(height, takeover);
fixture.DecrementBlocks(1);
fixture.getLastTakeoverForName("test", cid2, takeover);
BOOST_CHECK_EQUAL(cid, cid2);
BOOST_CHECK_EQUAL(cid, uint160(cid2));
BOOST_CHECK_EQUAL(height, takeover);
fixture.DecrementBlocks(1);
fixture.getLastTakeoverForName("test", cid2, takeover);
BOOST_CHECK_EQUAL(cid, cid2);
BOOST_CHECK_EQUAL(cid, uint160(cid2));
BOOST_CHECK_EQUAL(height, takeover);
}
@ -1924,7 +1924,7 @@ BOOST_AUTO_TEST_CASE(update_on_support2_test)
int lastTakeover;
BOOST_CHECK(fixture.getLastTakeoverForName(name, claimId, lastTakeover));
BOOST_CHECK_EQUAL(lastTakeover, height + 1);
BOOST_CHECK_EQUAL(ClaimIdHash(tx1.GetHash(), 0), claimId);
BOOST_CHECK_EQUAL(ClaimIdHash(tx1.GetHash(), 0), uint160(claimId));
fixture.Spend(s1);
fixture.Spend(s2);

View file

@ -276,9 +276,6 @@ BOOST_AUTO_TEST_CASE(hardfork_disk_test)
BOOST_CHECK_EQUAL(fixture.expirationTime(), 3);
fixture.IncrementBlocks(7, true);
BOOST_CHECK_EQUAL(fixture.expirationTime(), 6);
auto tip = chainActive.Tip();
pclaimTrie->ReadFromDisk(tip->nHeight, tip->hashClaimTrie);
BOOST_CHECK_EQUAL(fixture.expirationTime(), 6);
// Create a claim and support 1 block before the fork height that will expire after the fork height.
// Reset to disk, increment past the fork height and make sure we get
@ -288,8 +285,6 @@ BOOST_AUTO_TEST_CASE(hardfork_disk_test)
CMutableTransaction s1 = fixture.MakeSupport(fixture.GetCoinbase(), tx1, "test", 1);
fixture.IncrementBlocks(1);
tip = chainActive.Tip();
pclaimTrie->ReadFromDisk(tip->nHeight, tip->hashClaimTrie);
BOOST_CHECK_EQUAL(fixture.expirationTime(), 3);
fixture.IncrementBlocks(1);
BOOST_CHECK_EQUAL(fixture.expirationTime(), 6);
@ -310,8 +305,7 @@ BOOST_AUTO_TEST_CASE(hardfork_disk_test)
CMutableTransaction tx2 = fixture.MakeClaim(fixture.GetCoinbase(),"test2","one",1);
CMutableTransaction s2 = fixture.MakeSupport(fixture.GetCoinbase(),tx2,"test2",1);
fixture.IncrementBlocks(1);
tip = chainActive.Tip();
pclaimTrie->ReadFromDisk(tip->nHeight, tip->hashClaimTrie);
CMutableTransaction u2 = fixture.MakeUpdate(tx2, "test2", "two", ClaimIdHash(tx2.GetHash(), 0), 1);
// increment to fork
fixture.IncrementBlocks(2);

View file

@ -28,6 +28,8 @@
extern ::CChainState g_chainstate;
extern ::ArgsManager gArgs;
extern std::vector<std::string> random_strings(std::size_t count);
CMutableTransaction BuildTransaction(const uint256& prevhash);
CMutableTransaction BuildTransaction(const CTransaction& prev, uint32_t prevout=0, unsigned int numOutputs=1, int locktime=0);