Skip to content

Commit

Permalink
initialize hash_ (bundle::getHash) whenever it is possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Cylix committed Mar 3, 2018
1 parent ac00ea0 commit 0236091
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
17 changes: 11 additions & 6 deletions source/models/bundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ namespace IOTA {
namespace Models {

Bundle::Bundle(const std::vector<Models::Transaction>& transactions) : transactions_(transactions) {
if (!empty()) {
hash_ = getTransactions()[0].getBundle();
}
}

const std::vector<Models::Transaction>&
Expand Down Expand Up @@ -68,6 +71,10 @@ Bundle::setHash(const Types::Trytes& hash) {

void
Bundle::addTransaction(const Transaction& transaction, int32_t signatureMessageLength) {
if (empty()) {
hash_ = transaction.getBundle();
}

transactions_.push_back(transaction);

if (signatureMessageLength > 1) {
Expand Down Expand Up @@ -112,9 +119,10 @@ Bundle::finalize(const std::shared_ptr<Crypto::ISponge>& customSponge) {
IOTA::Types::Trits hash(TritHashLength);
sponge->squeeze(hash);

Types::Trytes hashInTrytes = Types::tritsToTrytes(hash);
//! set bundle hash for each underlying transaction
hash_ = Types::tritsToTrytes(hash);
for (std::size_t i = 0; i < transactions_.size(); i++) {
transactions_[i].setBundle(hashInTrytes);
transactions_[i].setBundle(hash_);
}
}

Expand Down Expand Up @@ -194,10 +202,7 @@ Bundle::operator>(const Bundle& rhs) const {

bool
Bundle::operator==(const Bundle& rhs) const {
auto lhsHash = empty() ? "" : getTransactions()[0].getBundle();
auto rhsHash = rhs.empty() ? "" : rhs.getTransactions()[0].getBundle();

return lhsHash == rhsHash;
return getHash() == rhs.getHash();
}

bool
Expand Down
40 changes: 16 additions & 24 deletions test/source/models/bundle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,45 +135,37 @@ TEST(Bundle, GetLengthWithPush) {
}

TEST(Bundle, EqOperator) {
IOTA::Models::Bundle eq_lhs({ { ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 1, 2, 3, 4 },
{ ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 5, 6, 7, 8 } });
IOTA::Models::Bundle eq_rhs({ { ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 9, 2, 10, 11 },
{ ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 12, 13, 14, 16 } });
IOTA::Models::Bundle eq_lhs;
eq_lhs.setHash("ABC");

eq_lhs.getTransactions()[0].setBundle("eq");
eq_rhs.getTransactions()[0].setBundle("eq");
IOTA::Models::Bundle eq_rhs;
eq_rhs.setHash("ABC");

EXPECT_EQ(eq_lhs == eq_rhs, true);

IOTA::Models::Bundle neq_lhs({ { ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 1, 2, 3, 4 },
{ ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 5, 6, 7, 8 } });
IOTA::Models::Bundle neq_rhs({ { ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 9, 0, 10, 11 },
{ ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 12, 13, 14, 16 } });
IOTA::Models::Bundle neq_lhs;
neq_lhs.setHash("ABC");

neq_lhs.getTransactions()[0].setBundle("eq");
neq_rhs.getTransactions()[0].setBundle("neq");
IOTA::Models::Bundle neq_rhs;
neq_rhs.setHash("DEF");

EXPECT_EQ(neq_lhs == neq_rhs, false);
}

TEST(Bundle, NeqOperator) {
IOTA::Models::Bundle eq_lhs({ { ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 1, 2, 3, 4 },
{ ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 5, 6, 7, 8 } });
IOTA::Models::Bundle eq_rhs({ { ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 9, 2, 10, 11 },
{ ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 12, 13, 14, 16 } });
IOTA::Models::Bundle eq_lhs;
eq_lhs.setHash("ABC");

eq_lhs.getTransactions()[0].setBundle("eq");
eq_rhs.getTransactions()[0].setBundle("eq");
IOTA::Models::Bundle eq_rhs;
eq_rhs.setHash("ABC");

EXPECT_EQ(eq_lhs != eq_rhs, false);

IOTA::Models::Bundle neq_lhs({ { ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 1, 2, 3, 4 },
{ ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 5, 6, 7, 8 } });
IOTA::Models::Bundle neq_rhs({ { ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 9, 0, 10, 11 },
{ ACCOUNT_1_ADDRESS_1_HASH, 0, "TAG", 12, 13, 14, 16 } });
IOTA::Models::Bundle neq_lhs;
neq_lhs.setHash("ABC");

neq_lhs.getTransactions()[0].setBundle("eq");
neq_rhs.getTransactions()[0].setBundle("neq");
IOTA::Models::Bundle neq_rhs;
neq_rhs.setHash("DEF");

EXPECT_EQ(neq_lhs != neq_rhs, true);
}
Expand Down

0 comments on commit 0236091

Please sign in to comment.