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

Add Storage for Pending Transactions #1598

Merged
merged 10 commits into from
Aug 28, 2018
Merged

Conversation

igor-egorov
Copy link
Contributor

@igor-egorov igor-egorov commented Jul 25, 2018

Signed-off-by: Igor Egorov igor@soramitsu.co.jp

Description of the Change

The storage listens to MST events and keeps track of semi signed transactions or batches of transactions.
The storage is a backend for "getPendingTransactions" query.
It returns all the transactions or batches of transactions created by query originator (single transactions or transactions from returned batches require more signatories to be processed).

Benefits

Storage for pending transactions is implemented and covered by tests.

Possible Drawbacks

This PR cannot be compiled until corresponding changes will be made in MST.

Usage Examples or Tests

cmake -H. -Bbuild
cmake --build build --target pending_txs_storage_test
cd build
ctest -R pending_txs_storage_test --output-on-failure

Alternate Designs

Storage is not divided to interface and implementation since having just the implementation is acceptable in that case.

Potentially, internal structures can be replaced with tbb::concurrent_{vector,hash_map}. We use two maps as underlying structures and concurrent tbb structures would not solve the problem of simultaneous lock of both maps.

@igor-egorov igor-egorov added needs-review pr awaits review from maintainers needs-correction pr/rfc is not completed and might be updated mst multisignature transactions labels Jul 25, 2018
## todo remove schema target, igor-egorov, IR-1512
## schema is included due to a dependency bug in shared_model_interfaces
schema
shared_model_interfaces
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two are going from pending_txs_storage target

const AccountIdType &accountId) const {
std::shared_lock<std::shared_timed_mutex> lock(mutex_);
auto creator_it = storage_.index.find(accountId);
if (storage_.index.end() != creator_it) {
Copy link
Contributor

@l4l l4l Jul 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you revert the condition, so the code is more linirized, e.g:

if (storage_.index.end() == creator_it) {
    return {};
}

...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer leaving it as is because of two reasons:

  1. Such change will introduce style inconsistency with the following code (line 49)
  2. For me, it is a matter of taste :)

I can apply the change if you insist on it.

@l4l l4l force-pushed the develop branch 8 times, most recently from 4750870 to b2a5906 Compare July 25, 2018 17:15
@igor-egorov igor-egorov changed the base branch from develop to feature/mst_batches August 7, 2018 06:08
@igor-egorov igor-egorov force-pushed the feature/pending-txs-storage branch 2 times, most recently from 3469dd9 to 0dd422b Compare August 14, 2018 07:10
#

add_library(pending_txs_storage
impl/pending_txs_storage_impl.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix indentation

BatchObservable preparedBatch,
BatchObservable expiredBatch) {
updated_batches_subscription_ =
updatedBatches.subscribe([this](const SharedState &updatedBatches) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confusing naming. updatedBatches variable already exists

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also use snake case for variables' name here and above


PendingTransactionStorageImpl::SharedTxsCollectionType
PendingTransactionStorageImpl::getPendingTransactions(
const AccountIdType &accountId) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use snake for variables name

void PendingTransactionStorageImpl::removeBatch(const SharedBatch &batch) {
auto creators = batchCreators(*batch);
auto hash = batch->reducedHash();
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the point of making additional scope by parentheses?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discovered that in this case an additional scope is not needed.

.Times(1);

validateAndExecute(query);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add empty line at the end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, fix this issue in your IDE for automatic endlines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to have git hook or any another automated way of checking the presence of trailing newlines rather than automatic "newline completion".

: log_(logger::log("PendingTxsStorageTest")){};

std::shared_ptr<PendingTxsStorageFixture::Batch>
PendingTxsStorageFixture::generateSharedBatch(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use batch helper's makeTestBatch method instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or put these functions to batch helper

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline. If the pr will be merged before the end of the week, then I will add a todo and fix that issue as a separate task.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UPD: now tests are using batch helpers


target_link_libraries(pending_txs_storage
mst_state
## todo remove schema target, igor-egorov, IR-1512
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

PendingTransactionStorageImpl::SharedTxsCollectionType
PendingTransactionStorageImpl::getPendingTransactions(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in the function looks a bit complicated. Maybe refactor it with functional primitives or separate with different functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are very welcome to propose the better solution.


std::set<PendingTransactionStorageImpl::AccountIdType>
PendingTransactionStorageImpl::batchCreators(
const TransactionBatch &batch) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this function static?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no external users. Let's do it static function.

return creators;
}

void PendingTransactionStorageImpl::updatedBatchesHandler(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function, also, seems a bit complicated. Maybe rework it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are very welcome to propose the better solution.

The code does nothing more except the required stuff.

});
prepared_batch_subscription_ =
preparedBatch.subscribe([this](const SharedBatch &preparedBatch) {
this->preparedBatchHandler(preparedBatch);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point to call wrapper instead of removeBatch by itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wrapper was removed.

Initially, it was not clear that it will contain the single function call.

});
expired_batch_subscription_ =
expiredBatch.subscribe([this](const SharedBatch &expiredBatch) {
this->expiredBatchHandler(expiredBatch);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as before

std::set<AccountIdType> batchCreators(const TransactionBatch &batch) const;

/**
* Subscriptions on MST events
Copy link
Contributor

@muratovv muratovv Aug 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment looks related only to one subscription. I think better to use /// instead.
Or use it with this manner:

// ---------| Subscriptions on MST events |---------

Of course, better to wrap it by 80 symbols for clean-up. Also, you may request me for the python generator of those comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will leave it as is. Subscriptions + comment are separated from the rest of code with empty lines. In my view, it is pretty clear that comment relates to all subscription objects. Even more, comment is written in plural form. Thus, everything here is ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed a bit with the comment, I imagine using // as in the example.

*/
mutable std::shared_timed_mutex mutex_;

struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This struct and purpose unobvious for me. Pls, add documentation for class(struct) and fields with explanation HOW it works.

.Times(1);

validateAndExecute(query);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, fix this issue in your IDE for automatic endlines

#include <rxcpp/rx.hpp>
#include "module/irohad/pending_txs_storage/pending_txs_storage_fixture.hpp"

TEST_F(PendingTxsStorageFixture, FixutureSelfCheck) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add given, when, then notation

@igor-egorov igor-egorov dismissed stale reviews from muratovv and kamilsa August 20, 2018 11:50

Requested changes were applied

@l4l l4l removed the needs-correction pr/rfc is not completed and might be updated label Aug 27, 2018
@l4l l4l removed the needs-review pr awaits review from maintainers label Aug 27, 2018
@muratovv muratovv closed this Aug 27, 2018
@muratovv muratovv changed the base branch from feature/mst_batches to dev August 28, 2018 06:20
@muratovv muratovv reopened this Aug 28, 2018
Igor Egorov added 9 commits August 28, 2018 09:38
Signed-off-by: Igor Egorov <igor@soramitsu.co.jp>
Signed-off-by: Igor Egorov <igor@soramitsu.co.jp>
Signed-off-by: Igor Egorov <igor@soramitsu.co.jp>
Signed-off-by: Igor Egorov <igor@soramitsu.co.jp>
Signed-off-by: Igor Egorov <igor@soramitsu.co.jp>
Signed-off-by: Igor Egorov <igor@soramitsu.co.jp>
Signed-off-by: Igor Egorov <igor@soramitsu.co.jp>
Signed-off-by: Igor Egorov <igor@soramitsu.co.jp>
Signed-off-by: Igor Egorov <igor@soramitsu.co.jp>
Signed-off-by: Fedor Muratov <muratovfyodor@yandex.ru>
@igor-egorov
Copy link
Contributor Author

There is only one failed test - module_ordering_service_test. This pr will be merged without fixing it - we have a separate task for this https://soramitsu.atlassian.net/browse/IR-1659 .

@igor-egorov igor-egorov merged commit a3dbd10 into dev Aug 28, 2018
@igor-egorov igor-egorov deleted the feature/pending-txs-storage branch August 28, 2018 10:44
nickaleks pushed a commit that referenced this pull request Sep 10, 2018
Signed-off-by: Igor Egorov <igor@soramitsu.co.jp>
bakhtin pushed a commit that referenced this pull request Nov 2, 2018
Signed-off-by: Igor Egorov <igor@soramitsu.co.jp>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
mst multisignature transactions
Development

Successfully merging this pull request may close these issues.

None yet

4 participants