Skip to content

Commit

Permalink
Merge pull request #20 from gradido/messageId_easy_to_use
Browse files Browse the repository at this point in the history
clear old stuff, update MessageId and add test for it
  • Loading branch information
einhornimmond authored Sep 11, 2024
2 parents fc5c4a5 + 05a6d4c commit 36e0d27
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 173 deletions.
13 changes: 9 additions & 4 deletions include/gradido_blockchain/data/iota/MessageId.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]; };
Expand Down
6 changes: 3 additions & 3 deletions src/blockchain/InMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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({
Expand Down Expand Up @@ -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();
Expand Down
27 changes: 19 additions & 8 deletions src/data/iota/MessageId.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,36 @@
#include <cassert>

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<const unsigned char*>(mMessageId.data()), 4 * sizeof(uint64_t));
return std::string(hexTmp, hexSize - 1);
}

bool MessageId::isEmpty() const
{
Expand Down
9 changes: 3 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ 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
${TEST_BLOCKCHAIN}
${TEST_MAIN}
${TEST_LIB}
${TEST_CRYPTO}
#${TEST_MODEL}
#${TEST_MODEL_PROTOPUF}
${TEST_DATA}
${TEST_INTERACTION}
)

Expand All @@ -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()
Expand Down
59 changes: 59 additions & 0 deletions test/data/MessageIdTest.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
31 changes: 0 additions & 31 deletions test/model/VectorCacheAllocatorTest.cpp

This file was deleted.

67 changes: 0 additions & 67 deletions test/model/protopuf.cpp

This file was deleted.

21 changes: 0 additions & 21 deletions test/model/protopuf.h

This file was deleted.

Empty file.
33 changes: 0 additions & 33 deletions test/model/protopuf/CommunityRoot.cpp

This file was deleted.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 36e0d27

Please sign in to comment.