diff --git a/src/claimtrie/CMakeLists.txt b/src/claimtrie/CMakeLists.txt index a6d9c8f0e..74858d2b4 100644 --- a/src/claimtrie/CMakeLists.txt +++ b/src/claimtrie/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.10) -project(claimtrie) +project(libclaimtrie) set(CMAKE_CXX_STANDARD 14) set(CMAKE_POSITION_INDEPENDENT_CODE ON) @@ -22,27 +22,55 @@ set(CLAIMTRIE_SRC 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) + elseif(${BIND} STREQUAL "go") + find_program(GOLANG NAMES go) + if(NOT GOLANG) + message(FATAL_ERROR "Golang was not found in system path") + endif() + execute_process(COMMAND ${GOLANG} "version" OUTPUT_VARIABLE GOLANG_VERSION) + STRING(REGEX MATCH "[0-9]+.[0-9]+.[0-9]" GOLANG_VERSION "${GOLANG_VERSION}") + if(GOLANG_VERSION VERSION_LESS 1.5) + message(FATAL_ERROR "Update Golang to at least 1.5, now " ${GOLANG_VERSION}) + endif() + set(SWIG_OPTIONS -go;-cgo;-intgosize;32) + set(POST_BUILD_COMMAND go) + set(POST_BUILD_ARGS install;-x;${CMAKE_PROJECT_NAME}.go) + # CGO_LDFLAGS is buggy so we should inject linker flags in generated go file + set(POST_BUILD_PATCH -i "\"s/import \\\"C\\\"/\\/\\/ #cgo LDFLAGS: -L\\\$$\\{SRCDIR\\} -lclaimtrie\\nimport \\\"C\\\"/\"" ${CMAKE_PROJECT_NAME}.go) else() message(FATAL_ERROR "Implement a handler for ${BIND}") endif() - add_custom_command(OUTPUT ${INTERFACE_NAME}_wrap.cxx + add_custom_command(OUTPUT ${CMAKE_PROJECT_NAME}_wrap.cxx COMMAND ${SWIG} - ARGS -c++ ${SWIG_OPTIONS} -outcurrentdir ${CMAKE_CURRENT_SOURCE_DIR}/${INTERFACE_NAME}.i + ARGS -c++ ${SWIG_OPTIONS} -outcurrentdir ${CMAKE_CURRENT_SOURCE_DIR}/${CMAKE_PROJECT_NAME}.i WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} ) set(CLAIMTRIE_SRC ${CLAIMTRIE_SRC} - ${INTERFACE_NAME}_wrap.cxx + ${CMAKE_PROJECT_NAME}_wrap.cxx ) endif() add_library(claimtrie SHARED ${CLAIMTRIE_SRC}) +if (POST_BUILD_PATCH) + add_custom_command(TARGET claimtrie POST_BUILD + COMMAND sed + ARGS ${POST_BUILD_PATCH} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + ) +endif() +if(POST_BUILD_COMMAND) + add_custom_command(TARGET claimtrie POST_BUILD + COMMAND ${POST_BUILD_COMMAND} + ARGS ${POST_BUILD_ARGS} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + ) +endif() target_include_directories(claimtrie PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) @@ -76,7 +104,7 @@ target_link_libraries(claimtrie PRIVATE ssl) set(BOOST_LIBS filesystem,locale,system,chrono,thread,test) -set(BOOST_COMPONENTS filesystem;locale;system,chrono,thread,unit_test_framework) +set(BOOST_COMPONENTS filesystem;locale;system;chrono;thread;unit_test_framework) CPMAddPackage( NAME Boost diff --git a/src/claimtrie/libclaimtrie.i b/src/claimtrie/libclaimtrie.i index a02bc2c51..040114552 100644 --- a/src/claimtrie/libclaimtrie.i +++ b/src/claimtrie/libclaimtrie.i @@ -14,8 +14,7 @@ %include stl.i %include stdint.i -%include std_array.i -%include std_pair.i +%include typemaps.i %apply int& OUTPUT { int& nValidAtHeight }; @@ -29,6 +28,8 @@ %ignore uint160(uint160 &&); %ignore uint256(uint256 &&); +%template(vecUint8) std::vector; + %include "blob.h" %template(blob160) CBaseBlob<160>; diff --git a/src/claimtrie/libclaimtrie_test.go b/src/claimtrie/libclaimtrie_test.go new file mode 100644 index 000000000..2c2d92081 --- /dev/null +++ b/src/claimtrie/libclaimtrie_test.go @@ -0,0 +1,120 @@ + +package main + +import( + "testing" + ."libclaimtrie" +) + +func assertEqual(t *testing.T, a interface{}, b interface{}, msg string) { + if a != b { + t.Fatalf("%s != %s, %s", a, b, msg) + } +} + +func assertNotEqual(t *testing.T, a interface{}, b interface{}, msg string) { + if a == b { + t.Fatalf("%s == %s, %s", a, b, msg) + } +} + +func assertFalse(t *testing.T, a interface{}, msg string) { + if a != false { + t.Fatalf("%s != false, %s", a, msg) + } +} + +func assertTrue(t *testing.T, a interface{}, msg string) { + if a != true { + t.Fatalf("%s != true, %s", a, msg) + } +} + +func assertClaimEqual(t *testing.T, claim CClaimValue, txo COutPoint, cid Uint160, amount int64, effe int64, height int, validHeight int, msg string) { + assertEqual(t, claim.GetOutPoint(), txo, msg) + assertEqual(t, claim.GetClaimId(), cid, msg) + assertEqual(t, claim.GetNAmount(), amount, msg) + assertEqual(t, claim.GetNEffectiveAmount(), effe, msg) + assertEqual(t, claim.GetNHeight(), height, msg) + assertEqual(t, claim.GetNValidAtHeight(), validHeight, msg) +} + +func assertSupportEqual(t *testing.T, support CSupportValue, txo COutPoint, cid Uint160, amount int64, height int, validHeight int, msg string) { + assertEqual(t, support.GetOutPoint(), txo, msg) + assertEqual(t, support.GetSupportedClaimId(), cid, msg) + assertEqual(t, support.GetNAmount(), amount, msg) + assertEqual(t, support.GetNHeight(), height, msg) + assertEqual(t, support.GetNValidAtHeight(), validHeight, msg) +} + +var uint256s = "1234567890987654321012345678909876543210123456789098765432101234" +var uint160 = Uint160S("1234567890987654321012345678909876543210") +var uint256 = Uint256S(uint256s) +var txp = NewCOutPoint(uint256, uint(1)) + +func Test_uint256(t *testing.T) { + assertFalse(t, uint256.IsNull(), "incorrect uint256S or CBaseBlob::IsNull") + assertEqual(t, uint256.GetHex(), uint256s, "incorrect CBaseBlob::GetHex") + assertEqual(t, uint256.GetHex(), uint256.ToString(), "incorrect CBaseBlob::ToString") + assertEqual(t, uint256.Size(), 32, "incorrect CBaseBlob::size") + uint256c := NewUint256() + assertNotEqual(t, uint256c, uint256, "incorrect CBaseBlob::operator!=") + assertTrue(t, uint256c.IsNull(), "incorrect CBaseBlob::IsNull") + uint256c = NewUint256(uint256) + assertEqual(t, uint256c, uint256, "incorrect CBaseBlob::operator==") + uint256c.SetNull() + assertTrue(t, uint256c.IsNull(), "incorrect CBaseBlob::SetNull") +} + +func Test_txoupoint(t *testing.T) { + assertEqual(t, txp.GetHash(), uint256, "incorrect COutPoint::COutPoint") + assertEqual(t, txp.GetN(), 1, "incorrect COutPoint::COutPoint") + assertFalse(t, txp.IsNull(), "incorrect COutPoint::IsNull") + pcopy := NewCOutPoint() + assertTrue(t, pcopy.IsNull(), "incorrect COutPoint::IsNull") + assertEqual(t, pcopy.GetHash(), NewUint256(), "incorrect COutPoint::COutPoint") + assertNotEqual(t, pcopy, txp, "incorrect COutPoint::operator!=") +} + +func Test_claim(t *testing.T) { + assertEqual(t, uint160.Size(), 20, "incorrect CBaseBlob::size") + claim := NewCClaimValue(txp, uint160, 20, 1, 10) + assertClaimEqual(t, claim, txp, uint160, 20, 20, 1, 10, "incorrect CClaimValue::CClaimValue") +} + +func Test_support(t *testing.T) { + claim := NewCClaimValue(txp, uint160, 20, 1, 10) + support := NewCSupportValue(txp, uint160, 20, 1, 10) + assertSupportEqual(t, support, claim.GetOutPoint(), claim.GetClaimId(), claim.GetNAmount(), claim.GetNHeight(), claim.GetNValidAtHeight(), "incorrect CSupportValue::CSupportValue") +} + +func Test_claimtrie(t *testing.T) { + claim := NewCClaimValue(txp, uint160, 20, 0, 0) + wipe := true; height := 1; data_dir := "." + trie := NewCClaimTrie(10*1024*1024, wipe, height, data_dir) + cache := NewCClaimTrieCache(trie) + assertTrue(t, trie.Empty(), "incorrect CClaimtrieCache::empty") + assertTrue(t, cache.AddClaim("test", txp, uint160, 20, 0, 0), "incorrect CClaimtrieCache::addClaim") + assertTrue(t, cache.HaveClaim("test", txp), "incorrect CClaimtrieCache::haveClaim") + assertEqual(t, cache.GetTotalNamesInTrie(), 1, "incorrect CClaimtrieCache::getTotalNamesInTrie") + assertEqual(t, cache.GetTotalClaimsInTrie(), 1, "incorrect CClaimtrieCache::getTotalClaimsInTrie") + var nValidAtHeight []int + // add second claim + txp.SetN(2) + uint1601 := Uint160S("1234567890987654321012345678909876543211") + assertTrue(t, cache.AddClaim("test", txp, uint1601, 20, 1, 1), "incorrect CClaimtrieCache::addClaim") + result := cache.HaveClaimInQueue("test", txp, nValidAtHeight) + assertTrue(t, result, "incorrect CClaimTrieCache::haveClaimInQueue") + assertEqual(t, nValidAtHeight[0], 1, "incorrect CClaimTrieCache::haveClaimInQueue, nValidAtHeight") + claim1 := NewCClaimValue() + assertTrue(t, cache.GetInfoForName("test", claim1), "incorrect CClaimTrieCache::getInfoForName") + assertEqual(t, claim, claim1, "incorrect CClaimtrieCache::getInfoForName") + proof := NewCClaimTrieProof() + assertTrue(t, cache.GetProofForName("test", uint160, proof), "incorrect CacheProofCallback") + assertTrue(t, proof.GetHasValue(), "incorrect CClaimTrieCache::getProofForName") + claimsToName := cache.GetClaimsForName("test") + claims := claimsToName.GetClaimsNsupports() + assertEqual(t, claims.Size(), 2, "incorrect CClaimTrieCache::getClaimsForName") + assertFalse(t, claims.Get(0).IsNull(), "incorrect CClaimNsupports::IsNull") + assertFalse(t, claims.Get(1).IsNull(), "incorrect CClaimNsupports::IsNull") +}