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

Common objects factory #1556

Merged
merged 14 commits into from
Jul 17, 2018
Merged

Common objects factory #1556

merged 14 commits into from
Jul 17, 2018

Conversation

nickaleks
Copy link
Contributor

Description of the Change

This Pull Requests implements abstract factory for common objects in shared model.

There is an abstract factory as well as implementation in protobuf

Benefits

Can remove builders for common objects in the future

Possible Drawbacks

None

Alternate Designs [optional]

Builders are not needed since no fields are optional

Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/benchmark/CMakeLists.txt
#	test/benchmark/bm_proto_creation.cpp
This allows addition of validator in the future

Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/benchmark/CMakeLists.txt
#	test/benchmark/bm_proto_creation.cpp
This allows addition of validator in the future

Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/benchmark/CMakeLists.txt
#	test/benchmark/bm_proto_creation.cpp
Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/benchmark/CMakeLists.txt
#	test/benchmark/bm_proto_creation.cpp
Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/benchmark/CMakeLists.txt
#	test/benchmark/bm_proto_creation.cpp
Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/benchmark/CMakeLists.txt
#	test/benchmark/bm_proto_creation.cpp
Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/benchmark/CMakeLists.txt
#	test/benchmark/bm_proto_creation.cpp
Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/benchmark/CMakeLists.txt
#	test/benchmark/bm_proto_creation.cpp
Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/benchmark/CMakeLists.txt
#	test/benchmark/bm_proto_creation.cpp
@nickaleks nickaleks requested review from l4l and Akvinikym July 12, 2018 08:21
namespace shared_model {
namespace interface {
/**
* CommonObjectsFactory provides methods to construct simple objects
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 "simple object"? Can you, please, define it in more specific way?

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 guess "common_objects" is a better name here since they are in the folder with this name

/**
* Create amount instance from string
*
* @param value integer will be divided by 10 * precision,
Copy link
Contributor

Choose a reason for hiding this comment

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

Aren't this implementation issues, which should not be located in interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the description of the contract which factory provides.
It basically says that if you pass this precision, this is the final result you should get.

* For example: "1.23", "10" etc.
*/
virtual FactoryResult<std::unique_ptr<Amount>> createAmount(
std::string amount) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not const reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because we modify this parameter in the code below. This logic was not written by me, and will be thrown out in the future, so it is simpler to just pass by value for now.

} // namespace interface
} // namespace shared_model

#endif // IROHA_COMMONOBJECTSFACTORY_HPP
Copy link
Contributor

Choose a reason for hiding this comment

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

No empty line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is present

target_link_libraries(proto_common_objects_factory_test
shared_model_cryptography
shared_model_stateless_validation
schema)
Copy link
Contributor

Choose a reason for hiding this comment

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

Bracket on a new line?

}

Validator validator_;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

There is an empty space between curly braces in the beginning of the file, but not here

validator_.validatePeer(reasons, peer);
});

if (answer) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't look very readable. Maybe, you should rename the variable to errors? It represents something wrong happened only, so I think it's a reasonable name

Copy link
Contributor

Choose a reason for hiding this comment

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

If you choose to follow the advice, change names of all variables in below functions as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Answer can be empty, which indicates lack of errors. But i agree that errors is a better name here

// create uint256 value from obtained string
boost::multiprecision::uint256_t value = 0;
if (begin <= amount.size()) {
value = boost::multiprecision::uint256_t(amount.substr(begin));
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't overflow possible anywhere? Of course, I don't think someone's going to provide more than uint256, but still, for safety reasons, is it possible to handle such kind of error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code is taken from lib/amount. It has worked so far, and I do not want to touch it :). It will be changed in the future, then we can replace it here

@@ -0,0 +1,259 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

The whole file is implantation inside an .hpp, won't is cause troubles with recompiling etc? Isn't it better to split 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.

It will only be used in setup of storage, so its fine.

TEST_F(PeerTest, InvalidPeerInitialization) {
auto peer = factory.createPeer(invalid_address, valid_pubkey);

peer.match(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't it be substituted with just ASSERT_TRUE(err(peer));?

Copy link
Contributor

Choose a reason for hiding this comment

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

Again, if you decide to follow, change it everywhere downwards as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Issue with err() is that it copies return result, but we have unique_ptr here.

Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/benchmark/CMakeLists.txt
#	test/benchmark/bm_proto_creation.cpp
@nickaleks nickaleks force-pushed the feature/common_objects_factory branch from e21faae to e40cb10 Compare July 12, 2018 10:29

auto errors = validate(*proto_amount, [](const auto &, auto &) {
// no validation needed,
// since any amount is valid in general context
Copy link
Contributor

Choose a reason for hiding this comment

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

There's still validateAmount function, so please use it

*/

#include <gtest/gtest.h>
#include <ed25519/ed25519.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that the header should be here

#include <gtest/gtest.h>
#include <ed25519/ed25519.h>

#include "backend/protobuf/common_objects/amount.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

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

That header should probably be in common_objects/common_objects_factory.hpp instead

Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/benchmark/CMakeLists.txt
#	test/benchmark/bm_proto_creation.cpp
Copy link
Contributor

@l4l l4l left a comment

Choose a reason for hiding this comment

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

lgtm

…ects_factory

Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/module/shared_model/backend_proto/CMakeLists.txt
Signed-off-by: Nikita Alekseev <[email protected]>

# Conflicts:
#	test/module/shared_model/backend_proto/CMakeLists.txt
@nickaleks nickaleks force-pushed the feature/common_objects_factory branch from 55742bb to 653b982 Compare July 17, 2018 10:51
@nickaleks nickaleks merged commit 359c8a7 into develop Jul 17, 2018
@l4l l4l deleted the feature/common_objects_factory branch July 17, 2018 20:34
l4l pushed a commit that referenced this pull request Jul 25, 2018
Allows to polymorphically create common objects

Signed-off-by: Nikita Alekseev <[email protected]>
l4l pushed a commit that referenced this pull request Jul 25, 2018
Allows to polymorphically create common objects

Signed-off-by: Nikita Alekseev <[email protected]>
l4l pushed a commit that referenced this pull request Jul 25, 2018
Allows to polymorphically create common objects

Signed-off-by: Nikita Alekseev <[email protected]>
l4l pushed a commit that referenced this pull request Jul 25, 2018
Allows to polymorphically create common objects

Signed-off-by: Nikita Alekseev <[email protected]>
l4l pushed a commit that referenced this pull request Jul 25, 2018
Allows to polymorphically create common objects

Signed-off-by: Nikita Alekseev <[email protected]>
l4l pushed a commit that referenced this pull request Jul 25, 2018
Allows to polymorphically create common objects

Signed-off-by: Nikita Alekseev <[email protected]>
l4l pushed a commit that referenced this pull request Jul 25, 2018
Allows to polymorphically create common objects

Signed-off-by: Nikita Alekseev <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants