Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Refactor tx presence methods in block query
Browse files Browse the repository at this point in the history
Signed-off-by: kamilsa <[email protected]>
  • Loading branch information
kamilsa committed Nov 16, 2018
1 parent 060fb90 commit 39875f1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 43 deletions.
17 changes: 5 additions & 12 deletions irohad/ametsuchi/block_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <cmath>
#include <rxcpp/rx.hpp>

#include "ametsuchi/tx_cache_response.hpp"
#include "common/result.hpp"
#include "interfaces/iroha_internal/block.hpp"
#include "interfaces/transaction.hpp"
Expand Down Expand Up @@ -106,21 +107,13 @@ namespace iroha {
const shared_model::crypto::Hash &hash) = 0;

/**
* Synchronously checks whether transaction
* with given hash is present in any block
* @param hash - transaction hash
* @return true if transaction exists, false otherwise
*/
virtual bool hasCommittedTxWithHash(
const shared_model::crypto::Hash &hash) = 0;

/**
* Synchronously checks whether rejected transaction's hash is present in
* Synchronously checks whether transaction with given hash is present in
* any block
* @param hash - rejected transaction's hash
* @return true if rejected transaction's hash exists, false otherwise
* @return TxCacheStatusType which returns status of transaction:
* Committed, Rejected or Missing
*/
virtual bool hasRejectedTxWithHash(
virtual TxCacheStatusType checkTxPresence(
const shared_model::crypto::Hash &hash) = 0;

/**
Expand Down
11 changes: 11 additions & 0 deletions irohad/ametsuchi/impl/postgres_block_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,17 @@ namespace iroha {
};
}

TxCacheStatusType PostgresBlockQuery::checkTxPresence(
const shared_model::crypto::Hash &hash) {
if (hasCommittedTxWithHash(hash)) {
return tx_cache_status_responses::Committed();
}
if (hasRejectedTxWithHash(hash)) {
return tx_cache_status_responses::Rejected();
}
return tx_cache_status_responses::Missing();
}

bool PostgresBlockQuery::hasCommittedTxWithHash(
const shared_model::crypto::Hash &hash) {
return getBlockId(hash) != boost::none;
Expand Down
10 changes: 5 additions & 5 deletions irohad/ametsuchi/impl/postgres_block_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ namespace iroha {

uint32_t getTopBlockHeight() override;

bool hasCommittedTxWithHash(
const shared_model::crypto::Hash &hash) override;

bool hasRejectedTxWithHash(
TxCacheStatusType checkTxPresence(
const shared_model::crypto::Hash &hash) override;

expected::Result<wBlock, std::string> getTopBlock() override;
Expand All @@ -77,7 +74,6 @@ namespace iroha {
*/
std::vector<shared_model::interface::types::HeightType> getBlockIds(
const shared_model::interface::types::AccountIdType &account_id);

/**
* Returns block id which contains transaction with a given hash
* @param hash - hash of transaction
Expand Down Expand Up @@ -105,6 +101,10 @@ namespace iroha {
std::string>
getBlock(shared_model::interface::types::HeightType id) const;

bool hasCommittedTxWithHash(const shared_model::crypto::Hash &hash);

bool hasRejectedTxWithHash(const shared_model::crypto::Hash &hash);

std::unique_ptr<soci::session> psql_;
soci::session &sql_;

Expand Down
6 changes: 2 additions & 4 deletions test/module/irohad/ametsuchi/ametsuchi_mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,8 @@ namespace iroha {
shared_model::interface::types::HeightType));
MOCK_METHOD1(getTopBlocks, std::vector<BlockQuery::wBlock>(uint32_t));
MOCK_METHOD0(getTopBlock, expected::Result<wBlock, std::string>(void));
MOCK_METHOD1(hasCommittedTxWithHash,
bool(const shared_model::crypto::Hash &hash));
MOCK_METHOD1(hasRejectedTxWithHash,
bool(const shared_model::crypto::Hash &hash));
MOCK_METHOD1(checkTxPresence,
TxCacheStatusType(const shared_model::crypto::Hash &));
MOCK_METHOD0(getTopBlockHeight, uint32_t(void));
};

Expand Down
40 changes: 18 additions & 22 deletions test/module/irohad/ametsuchi/block_query_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
#include <framework/specified_visitor.hpp>

#include "ametsuchi/impl/postgres_block_index.hpp"
#include "ametsuchi/impl/postgres_block_query.hpp"
Expand Down Expand Up @@ -350,45 +351,40 @@ TEST_F(BlockQueryTest, GetTop2Blocks) {

/**
* @given block store with preinserted blocks
* @when hasTxWithHash is invoked on existing transaction hash
* @then True is returned
* @when checkTxPresence is invoked on existing transaction hash
* @then Committed status is returned
*/
TEST_F(BlockQueryTest, HasTxWithExistingHash) {
for (const auto &hash : tx_hashes) {
EXPECT_TRUE(blocks->hasCommittedTxWithHash(hash));
ASSERT_NO_THROW(boost::apply_visitor(
framework::SpecifiedVisitor<tx_cache_status_responses::Committed>(),
blocks->checkTxPresence(hash)));
}
}

/**
* @given block store with preinserted blocks
* user1@test AND 1 tx created by user2@test
* @when hasTxWithHash is invoked on non-existing hash
* @then False is returned
* @when checkTxPresence is invoked on non-existing hash
* @then Missing status is returned
*/
TEST_F(BlockQueryTest, HasTxWithInvalidHash) {
shared_model::crypto::Hash invalid_tx_hash(zero_string);
EXPECT_FALSE(blocks->hasCommittedTxWithHash(invalid_tx_hash));
TEST_F(BlockQueryTest, HasTxWithMissingHash) {
shared_model::crypto::Hash missing_tx_hash(zero_string);
ASSERT_NO_THROW(boost::apply_visitor(
framework::SpecifiedVisitor<tx_cache_status_responses::Missing>(),
blocks->checkTxPresence(missing_tx_hash)));
}

/**
* @given block store with preinserted blocks containing rejected_hash1 in one
* of the block
* @when hasRejectedTxWithHash is invoked on existing rejected hash
* @then True is returned
* @when checkTxPresence is invoked on existing rejected hash
* @then Rejected is returned
*/
TEST_F(BlockQueryTest, HasTxWithRejectedHash) {
EXPECT_TRUE(blocks->hasRejectedTxWithHash(rejected_hash));
}

/**
* @given block store with preinserted blocks containing rejected_hash1 in one
* of the block
* @when hasRejectedTxWithHash is invoked on non-existing rejected hash
* @then False is returned
*/
TEST_F(BlockQueryTest, HasTxWithNonExistingRejectedHash) {
shared_model::crypto::Hash invalid_tx_hash(zero_string);
EXPECT_FALSE(blocks->hasRejectedTxWithHash(invalid_tx_hash));
ASSERT_NO_THROW(boost::apply_visitor(
framework::SpecifiedVisitor<tx_cache_status_responses::Rejected>(),
blocks->checkTxPresence(rejected_hash)));
}

/**
Expand Down

0 comments on commit 39875f1

Please sign in to comment.