From 05a6d4c09e6c559090e295450e968005715d839f Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Wed, 11 Sep 2024 13:51:04 +0200 Subject: [PATCH] clear old stuff, update MessageId and add test for it --- .../gradido_blockchain/data/iota/MessageId.h | 13 ++-- src/blockchain/InMemory.cpp | 6 +- src/data/iota/MessageId.cpp | 27 +++++--- test/CMakeLists.txt | 9 +-- test/data/MessageIdTest.cpp | 59 ++++++++++++++++ test/model/VectorCacheAllocatorTest.cpp | 31 --------- test/model/protopuf.cpp | 67 ------------------- test/model/protopuf.h | 21 ------ .../model/protopuf/CommunityFriendsUpdate.cpp | 0 test/model/protopuf/CommunityRoot.cpp | 33 --------- test/model/protopuf/ConfirmedTransaction.cpp | 0 test/model/protopuf/DeferredTransfer.cpp | 0 test/model/protopuf/GradidoCreation.cpp | 0 test/model/protopuf/GradidoTransfer.cpp | 0 test/model/protopuf/RegisterAddress.cpp | 0 15 files changed, 93 insertions(+), 173 deletions(-) create mode 100644 test/data/MessageIdTest.cpp delete mode 100644 test/model/VectorCacheAllocatorTest.cpp delete mode 100644 test/model/protopuf.cpp delete mode 100644 test/model/protopuf.h delete mode 100644 test/model/protopuf/CommunityFriendsUpdate.cpp delete mode 100644 test/model/protopuf/CommunityRoot.cpp delete mode 100644 test/model/protopuf/ConfirmedTransaction.cpp delete mode 100644 test/model/protopuf/DeferredTransfer.cpp delete mode 100644 test/model/protopuf/GradidoCreation.cpp delete mode 100644 test/model/protopuf/GradidoTransfer.cpp delete mode 100644 test/model/protopuf/RegisterAddress.cpp diff --git a/include/gradido_blockchain/data/iota/MessageId.h b/include/gradido_blockchain/data/iota/MessageId.h index 6c4a0e05..cd9e080a 100644 --- a/include/gradido_blockchain/data/iota/MessageId.h +++ b/include/gradido_blockchain/data/iota/MessageId.h @@ -12,6 +12,12 @@ namespace iota { public: MessageId(); + //! \param messageId binary version of message id, 32 Bytes expected + MessageId(const memory::Block& messageId); + //! \param messageIdHex hex version of message id, 64 character expected + MessageId(const std::string& messageIdHex); + + operator std::string() const { return toHex(); } //! operator needed for MessageId as key in unordered map bool operator==(const MessageId& other) const { @@ -41,10 +47,9 @@ namespace iota mMessageId[2] == ob.mMessageId[2] && mMessageId[3] < ob.mMessageId[3] ); - } - - static MessageId fromMemoryBlock(const memory::Block& bin); - memory::Block toMemoryBin() const; + } + memory::Block toMemoryBlock() const; + std::string toHex() const; bool isEmpty() const; inline uint64_t getMessageIdByte(uint8_t index) const { assert(index < 4); return mMessageId[index]; }; diff --git a/src/blockchain/InMemory.cpp b/src/blockchain/InMemory.cpp index b78e58ab..be91896c 100644 --- a/src/blockchain/InMemory.cpp +++ b/src/blockchain/InMemory.cpp @@ -284,7 +284,7 @@ namespace gradido { ) const { std::lock_guard _lock(mWorkMutex); - auto it = mMessageIdTransactionNrs.find(iota::MessageId::fromMemoryBlock(*messageId)); + auto it = mMessageIdTransactionNrs.find(iota::MessageId(*messageId)); if (it != mMessageIdTransactionNrs.end()) { return getTransactionForId(it->second); } @@ -308,7 +308,7 @@ namespace gradido { mTransactionsByPubkey.insert({ involvedAddress, transactionEntry }); } mMessageIdTransactionNrs.insert({ - iota::MessageId::fromMemoryBlock(*confirmedTransaction->getMessageId()), + iota::MessageId(*confirmedTransaction->getMessageId()), confirmedTransaction->getId() }); mTransactionsByNr.insert({ @@ -386,7 +386,7 @@ namespace gradido { } } } - mMessageIdTransactionNrs.erase(iota::MessageId::fromMemoryBlock(*confirmedTransaction->getMessageId())); + mMessageIdTransactionNrs.erase(iota::MessageId(*confirmedTransaction->getMessageId())); mTransactionsByNr.erase(confirmedTransaction->getId()); auto body = confirmedTransaction->getGradidoTransaction()->getTransactionBody(); diff --git a/src/data/iota/MessageId.cpp b/src/data/iota/MessageId.cpp index 67bec960..9e1a88ce 100644 --- a/src/data/iota/MessageId.cpp +++ b/src/data/iota/MessageId.cpp @@ -4,25 +4,36 @@ #include namespace iota { - MessageId::MessageId() + MessageId::MessageId() { mMessageId.fill(0); } - MessageId MessageId::fromMemoryBlock(const memory::Block& bin) + MessageId::MessageId(const memory::Block& messageId) { - if (bin.size() != 4 * sizeof(uint64_t)) { - throw MessageIdFormatException("message id as bin has wrong size", bin); + if (messageId.size() != sizeof(uint64_t) * mMessageId.size()) { + throw InvalidSizeException("invalid input for message id", sizeof(uint64_t) * mMessageId.size(), messageId.size()); } - MessageId messageId; - memcpy(messageId.mMessageId.data(), bin, 4 * sizeof(uint64_t)); - return messageId; + memcpy(mMessageId.data(), messageId, 4 * sizeof(uint64_t)); } - memory::Block MessageId::toMemoryBin() const + MessageId::MessageId(const std::string& messageIdHex) + : MessageId(memory::Block::fromHex(messageIdHex)) + { + + } + + memory::Block MessageId::toMemoryBlock() const { return memory::Block(mMessageId.size() * sizeof(uint64_t), (unsigned char*)mMessageId.data()); } + std::string MessageId::toHex() const + { + const auto hexSize = 8 * sizeof(uint64_t) + 1; + char hexTmp[hexSize]; + sodium_bin2hex(hexTmp, hexSize, reinterpret_cast(mMessageId.data()), 4 * sizeof(uint64_t)); + return std::string(hexTmp, hexSize - 1); + } bool MessageId::isEmpty() const { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 660728ce..05257f98 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -27,8 +27,7 @@ FILE(GLOB TEST_CRYPTO "crypto/*.cpp" "crypto/*.h") FILE(GLOB TEST_INTERACTION "interaction/*.cpp" "interaction/*.h") FILE(GLOB TEST_LIB "lib/*.cpp" "lib/*.h") FILE(GLOB TEST_MAIN "*.cpp" "*.h") -FILE(GLOB TEST_MODEL "model/*.cpp" "model/*.h") -FILE(GLOB TEST_MODEL_PROTOPUF "model/protopuf/*.cpp" "model/protopuf/*.h") +FILE(GLOB TEST_DATA "data/*.cpp" "data/*.h") SET(TEST_SRC @@ -36,8 +35,7 @@ SET(TEST_SRC ${TEST_MAIN} ${TEST_LIB} ${TEST_CRYPTO} - #${TEST_MODEL} - #${TEST_MODEL_PROTOPUF} + ${TEST_DATA} ${TEST_INTERACTION} ) @@ -46,8 +44,7 @@ if(MSVC) source_group("lib" FILES ${TEST_LIB}) source_group("blockchain" FILES ${TEST_BLOCKCHAIN}) source_group("crypto" FILES ${TEST_CRYPTO}) - source_group("model\\protopuf" FILES ${TEST_MODEL_PROTOPUF}) - source_group("model" FILES ${TEST_MODEL}) + source_group("data" FILES ${TEST_DATA}) source_group("interaction" FILES ${TEST_INTERACTION}) source_group("seeding" FILES ${TEST_SEEDING}) endif() diff --git a/test/data/MessageIdTest.cpp b/test/data/MessageIdTest.cpp new file mode 100644 index 00000000..9a69c445 --- /dev/null +++ b/test/data/MessageIdTest.cpp @@ -0,0 +1,59 @@ +#include "gradido_blockchain/data/iota/MessageId.h" +#include "gtest/gtest.h" + +using namespace iota; +using namespace memory; + +const auto testHex1 = "4f1a5e6b7c9d3a2e0f8b7d4c1e9f5a6b3c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f"; +const auto testHex2 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; + +TEST(MessageIdTest, Constructor) +{ + Block b(Block::fromHex(testHex1)); + MessageId idFromHex(testHex1); + MessageId idFromBlock(b); + + EXPECT_EQ(idFromHex.getMessageIdByte(0), 0x2e3a9d7c6b5e1a4f); + EXPECT_EQ(idFromHex.getMessageIdByte(1), 0x6b5a9f1e4c7d8b0f); + EXPECT_EQ(idFromHex.getMessageIdByte(2), 0x3d2c1b0a9f8e7d3c); + EXPECT_EQ(idFromHex.getMessageIdByte(3), 0x1f0e9d8c7b6a5f4e); + + EXPECT_EQ(idFromBlock.getMessageIdByte(0), 0x2e3a9d7c6b5e1a4f); + EXPECT_EQ(idFromBlock.getMessageIdByte(1), 0x6b5a9f1e4c7d8b0f); + EXPECT_EQ(idFromBlock.getMessageIdByte(2), 0x3d2c1b0a9f8e7d3c); + EXPECT_EQ(idFromBlock.getMessageIdByte(3), 0x1f0e9d8c7b6a5f4e); +} + +TEST(MessageIdTest, Operators) +{ + MessageId id1(testHex1); + MessageId id2(testHex2); + // test operator == + EXPECT_NE(id1, id2); + EXPECT_EQ(id1, id1); + EXPECT_EQ(id2, id2); + + // test operator < + EXPECT_LT(id2, id1); + + // test operator std::string + EXPECT_EQ(id1, std::string(testHex1)); + EXPECT_EQ(id2, std::string(testHex2)); + EXPECT_NE(id1, std::string(testHex2)); + EXPECT_NE(id2, std::string(testHex1)); +} + +TEST(MessageIdTest, Empty) +{ + MessageId emptyId; + MessageId id(testHex1); + EXPECT_TRUE(emptyId.isEmpty()); + EXPECT_FALSE(id.isEmpty()); +} + +TEST(MessageIdTest, toMemoryBlock) +{ + MessageId id(testHex1); + auto block = id.toMemoryBlock(); + EXPECT_EQ(block.convertToHex(), testHex1); +} \ No newline at end of file diff --git a/test/model/VectorCacheAllocatorTest.cpp b/test/model/VectorCacheAllocatorTest.cpp deleted file mode 100644 index e9842821..00000000 --- a/test/model/VectorCacheAllocatorTest.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "gradido_blockchain/memory/VectorCacheAllocator.h" -#include "gradido_blockchain/lib/Profiler.h" -#include - -#include - -using namespace memory; - -// test that with using VectorCacheAllocator, vector is faster, after enough memory blocks are cached -TEST(VectorCacheAllocator, sizeAfterReservePublicKey) { - Profiler timeUsed; - { - std::vector> testVector; - for (int i = 0; i < 512; ++i) { - testVector.push_back(rand()); - } - } - auto timeAfterFirst = timeUsed.nanos(); - auto mapSizeAfterFirstRun = VectorCacheAllocator::getMapSize(); - timeUsed.reset(); - - { - std::vector> testVector; - for (int i = 0; i < 512; ++i) { - testVector.push_back(rand()); - } - } - EXPECT_LT(timeUsed.nanos(), timeAfterFirst); - auto mapSizeAfterSecondRun = VectorCacheAllocator::getMapSize(); - EXPECT_EQ(mapSizeAfterFirstRun, mapSizeAfterSecondRun); -} diff --git a/test/model/protopuf.cpp b/test/model/protopuf.cpp deleted file mode 100644 index 992fc969..00000000 --- a/test/model/protopuf.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "protopuf.h" - -//#include "gradido_blockchain/model/protopufTypes.h" -//#include "gradido_blockchain/model/TransactionFactory.h" -#include "gradido_blockchain/crypto/CryptoConfig.h" - -void protopuf::SetUp() -{ - CryptoConfig::loadMnemonicWordLists(); - for (auto i = 0; i < 6; i++) { - auto passphrase = Passphrase::generate(&CryptoConfig::g_Mnemonic_WordLists[2]); - mTestKeyPairs.push_back(KeyPairEd25519::create(passphrase)); - } - -} - -void protopuf::TearDown() -{ - -} - -TEST_F(protopuf, CommunityRoot) { - /* - auto mm = MemoryManager::getInstance(); - auto pubkey = mTestKeyPairs[0]->getPublicKeyCopy(); - auto gmwPubkey = mTestKeyPairs[1]->getPublicKeyCopy(); - auto aufPubkey = mTestKeyPairs[1]->getPublicKeyCopy(); - auto gradidoTransaction = TransactionFactory::createCommunityRoot(pubkey, gmwPubkey, aufPubkey); - auto bodyBytes = gradidoTransaction->getTransactionBody()->getBodyBytes(); - auto signature = mTestKeyPairs[0]->sign(*bodyBytes.get()); - gradidoTransaction->addSign(pubkey, signature); - auto serializedTransaction = gradidoTransaction->getSerialized(); - - std::span gradidoTransacionByteSpan(reinterpret_cast(serializedTransaction->data()), serializedTransaction->size()); - auto [protopufGradidoTransaction, bufferEnd2] = message_coder::decode(gradidoTransacionByteSpan); - model::protopuf::SignatureMap sigMap = protopufGradidoTransaction["sig_map"_f].value(); - auto firstSigPair = sigMap["sig_pair"_f][0]; - std::vector& firstPubkey = firstSigPair["pubkey"_f].value(); - std::vector& firstSignature = firstSigPair["signature"_f].value(); - EXPECT_EQ(firstPubkey.size(), pubkey->size()); - EXPECT_FALSE(memcmp(firstPubkey.data(), pubkey->data(), firstPubkey.size())); - EXPECT_EQ(firstSignature.size(), signature->size()); - EXPECT_FALSE(memcmp(firstSignature.data(), signature->data(), firstSignature.size())); - - std::span bodyBytesSpan(reinterpret_cast(bodyBytes->data()), bodyBytes->size()); - auto [protopufBodyBytes, bufferEnd3] = message_coder::decode(bodyBytesSpan); - EXPECT_TRUE(protopufBodyBytes["community_root"_f].has_value()); - model::protopuf::CommunityRoot communityRoot = protopufBodyBytes["community_root"_f].value(); - std::vector& crPubkey = communityRoot["pubkey"_f].value(); - EXPECT_EQ(crPubkey.size(), pubkey->size()); - EXPECT_FALSE(memcmp(crPubkey.data(), pubkey->data(), crPubkey.size())); - EXPECT_FALSE(protopufBodyBytes["transfer"_f].has_value()); - - - // std::string publicKey = protopufGradidoTransaction["sig_map"_f]["sig_pair"_f][0]["pubkey"_f]; - //printf("size: %d\n", (int)publicKey.size()); - //for (const auto& signaturePair : sigMap["sig_pair"_f]) { - // auto pubkey = signaturePair["pubkey"_f]; - // auto signature = signaturePair["signature"_f]; - //} - - mm->releaseMemory(signature); - mm->releaseMemory(pubkey); - mm->releaseMemory(gmwPubkey); - mm->releaseMemory(aufPubkey); - */ -} \ No newline at end of file diff --git a/test/model/protopuf.h b/test/model/protopuf.h deleted file mode 100644 index d6dcda00..00000000 --- a/test/model/protopuf.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef __GRADIDO_BLOCKCHAIN_TEST_MODEL_PROTOPUF_H -#define __GRADIDO_BLOCKCHAIN_TEST_MODEL_PROTOPUF_H - -#include "gradido_blockchain/crypto/KeyPairEd25519.h" - -#include "gtest/gtest.h" - -#include - -class protopuf : public ::testing::Test -{ - -protected: - void SetUp() override; - void TearDown() override; - - std::vector> mTestKeyPairs; -}; - - -#endif \ No newline at end of file diff --git a/test/model/protopuf/CommunityFriendsUpdate.cpp b/test/model/protopuf/CommunityFriendsUpdate.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/test/model/protopuf/CommunityRoot.cpp b/test/model/protopuf/CommunityRoot.cpp deleted file mode 100644 index 3142e044..00000000 --- a/test/model/protopuf/CommunityRoot.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* -#include "gtest/gtest.h" -#include "protopuf/message.h" -#include "gradido_blockchain/model/protopuf/CommunityRoot.h" -#include "gradido_blockchain/MemoryManager.h" -#include "gradido_blockchain/crypto/CryptoConfig.h" -#include "gradido_blockchain/crypto/Passphrase.h" -#include "gradido_blockchain/crypto/KeyPairEd25519.h" -#include "loguru/loguru.hpp" - -using namespace pp; -using namespace model::protopuf; - -TEST(modelProtopufCommunityRoot, protobufEncodeProtopufDecode) { - - CryptoConfig::loadMnemonicWordLists(); - std::vector> testKeyPairs; - for (auto i = 0; i < 6; i++) { - auto passphrase = Passphrase::generate(&CryptoConfig::g_Mnemonic_WordLists[2]); - testKeyPairs.push_back(KeyPairEd25519::create(passphrase)); - } - for (const auto& keyPair : testKeyPairs) { - LOG_F(INFO, "pubkey: %s", keyPair->getPublicKeyHex().data()); - } - CachedMemoryBlock buffer(20); - CommunityRoot communityRoot( - std::make_shared(testKeyPairs[0]->getPublicKeySize(), testKeyPairs[0]->getPublicKey()), - std::make_shared(testKeyPairs[1]->getPublicKeySize(), testKeyPairs[0]->getPublicKey()), - std::make_shared(testKeyPairs[2]->getPublicKeySize(), testKeyPairs[0]->getPublicKey()) - ); - message_coder::encode() -} -*/ \ No newline at end of file diff --git a/test/model/protopuf/ConfirmedTransaction.cpp b/test/model/protopuf/ConfirmedTransaction.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/test/model/protopuf/DeferredTransfer.cpp b/test/model/protopuf/DeferredTransfer.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/test/model/protopuf/GradidoCreation.cpp b/test/model/protopuf/GradidoCreation.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/test/model/protopuf/GradidoTransfer.cpp b/test/model/protopuf/GradidoTransfer.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/test/model/protopuf/RegisterAddress.cpp b/test/model/protopuf/RegisterAddress.cpp deleted file mode 100644 index e69de29b..00000000