Skip to content

Commit

Permalink
operator == and != for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsa authored and lebdron committed Jul 19, 2017
1 parent 3aa3498 commit 107c58b
Show file tree
Hide file tree
Showing 16 changed files with 230 additions and 0 deletions.
1 change: 1 addition & 0 deletions irohad/model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_library(model
model_hash_provider_impl.cpp
impl/stateful_command_validation.cpp
impl/command_execution.cpp
impl/model_operators.cpp
converters/impl/pb_block_factory.cpp
converters/impl/pb_transaction_factory.cpp
converters/impl/pb_command_factory.cpp
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ namespace iroha {
* User's account permission
*/
bool can_transfer;

bool operator==(const Permissions& rhs) const;
bool operator!=(const Permissions& rhs) const;
};

/**
Expand Down
2 changes: 2 additions & 0 deletions irohad/model/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace iroha {
const Account& creator) = 0;
virtual bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) = 0;
virtual bool operator==(const Command& rhs) const = 0;
virtual bool operator!=(const Command& rhs) const = 0;
};
}
}
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/commands/add_asset_quantity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ namespace iroha {
const Account& creator) override;
bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) override;

bool operator==(const Command& command) const override;
bool operator!=(const Command& command) const override;
};
} // namespace model
} // namespace iroha
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/commands/add_peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ namespace iroha {
const Account& creator) override;
bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) override;

bool operator==(const Command& command) const override;
bool operator!=(const Command& command) const override;
};
} // namespace model
} // namespace iroha
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/commands/add_signatory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ namespace iroha {
const Account& creator) override;
bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) override;

bool operator==(const Command& command) const override;
bool operator!=(const Command& command) const override;
};
} // namespace model
} // namespace iroha
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/commands/assign_master_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ namespace iroha {
const Account& creator) override;
bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) override;

bool operator==(const Command& command) const override;
bool operator!=(const Command& command) const override;
};
} // namespace model
} // namespace iroha
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/commands/create_account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ namespace iroha {
const Account& creator) override;
bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) override;

bool operator==(const Command& command) const override;
bool operator!=(const Command& command) const override;
};
} // namespace model
} // namespace iroha
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/commands/create_asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace iroha {
const Account& creator) override;
bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) override;

bool operator==(const Command& command) const override;
bool operator!=(const Command& command) const override;
};
} // namespace model
} // namespace iroha
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/commands/create_domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ namespace iroha {
const Account& creator) override;
bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) override;

bool operator==(const Command& command) const override;
bool operator!=(const Command& command) const override;
};
} // namespace model
}
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/commands/remove_signatory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ namespace iroha {
const Account& creator) override;
bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) override;

bool operator==(const Command& command) const override;
bool operator!=(const Command& command) const override;
};
} // namespace model
} // namespace iroha
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/commands/set_permissions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ namespace iroha {
const Account& creator) override;
bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) override;

bool operator==(const Command& command) const override;
bool operator!=(const Command& command) const override;
};
} // namespace model
} // namespace iroha
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/commands/set_quorum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ namespace iroha {
const Account& creator) override;
bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) override;

bool operator==(const Command& command) const override;
bool operator!=(const Command& command) const override;
};
} // namespace model
} // namespace iroha
Expand Down
3 changes: 3 additions & 0 deletions irohad/model/commands/transfer_asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ namespace iroha {
const Account& creator) override;
bool execute(ametsuchi::WsvQuery& queries,
ametsuchi::WsvCommand& commands) override;

bool operator==(const Command& command) const override;
bool operator!=(const Command& command) const override;
};
} // namespace model
} // namespace iroha
Expand Down
184 changes: 184 additions & 0 deletions irohad/model/impl/model_operators.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <model/commands/add_asset_quantity.hpp>
#include <model/commands/add_peer.hpp>
#include <model/commands/add_signatory.hpp>
#include <model/commands/assign_master_key.hpp>
#include <model/commands/create_account.hpp>
#include <model/commands/create_asset.hpp>
#include <model/commands/create_domain.hpp>
#include <model/commands/remove_signatory.hpp>
#include <model/commands/set_permissions.hpp>
#include <model/commands/set_quorum.hpp>
#include <model/commands/transfer_asset.hpp>
namespace iroha {
namespace model {

/* AddAssetQuantity */
bool AddAssetQuantity::operator==(const Command &command) const {
if (! instanceof <AddAssetQuantity>(&command)) return false;
auto add_asset_quantity = static_cast<const AddAssetQuantity &>(command);
return add_asset_quantity.account_id == account_id &&
add_asset_quantity.asset_id == asset_id &&
add_asset_quantity.amount == amount;
}

bool AddAssetQuantity::operator!=(const Command &command) const {
return !operator==(command);
}

/* AddPeer */
bool AddPeer::operator==(const Command &command) const {
if (! instanceof <AddPeer>(&command)) return false;
auto add_peer = static_cast<const AddPeer &>(command);
return add_peer.peer_key == peer_key && add_peer.address == address;
}

bool AddPeer::operator!=(const Command &command) const {
return !operator==(command);
}

/* AddSignatory */
bool AddSignatory::operator==(const Command &command) const {
if (! instanceof <AddSignatory>(&command)) return false;
auto add_signatory = static_cast<const AddSignatory &>(command);
return add_signatory.account_id == account_id &&
add_signatory.pubkey == pubkey;
}

bool AddSignatory::operator!=(const Command &command) const {
return !operator==(command);
}

/* Assign master key */
bool AssignMasterKey::operator==(const Command &command) const {
if (! instanceof <AssignMasterKey>(&command)) return false;
auto assign_master_key = static_cast<const AssignMasterKey &>(command);
return assign_master_key.account_id == account_id &&
assign_master_key.pubkey == pubkey;
}

bool AssignMasterKey::operator!=(const Command &command) const {
return !operator==(command);
}

/* CreateAccount */
bool CreateAccount::operator==(const Command &command) const {
if (! instanceof <CreateAccount>(&command)) return false;
auto create_account = static_cast<const CreateAccount &>(command);
return create_account.pubkey == pubkey &&
create_account.domain_id == domain_id &&
create_account.account_name == account_name;
}

bool CreateAccount::operator!=(const Command &command) const {
return !operator==(command);
}

/* CreateAsset */
bool CreateAsset::operator==(const Command &command) const {
if (! instanceof <CreateAsset>(&command)) return false;
auto create_asset = static_cast<const CreateAsset &>(command);
return create_asset.domain_id == domain_id &&
create_asset.precision == precision &&
create_asset.asset_name == asset_name;
}

bool CreateAsset::operator!=(const Command &command) const {
return !operator==(command);
}

/* Create domain */
bool CreateDomain::operator==(const Command &command) const {
if (! instanceof <CreateDomain>(&command)) return false;
auto create_domain = static_cast<const CreateDomain &>(command);
return create_domain.domain_name == domain_name;
}

bool CreateDomain::operator!=(const Command &command) const {
return !operator==(command);
}

/* Remove signatory */
bool RemoveSignatory::operator==(const Command &command) const {
if (! instanceof <RemoveSignatory>(&command)) return false;
auto remove_signatory = static_cast<const RemoveSignatory &>(command);
return remove_signatory.pubkey == pubkey &&
remove_signatory.account_id == account_id;
}

bool RemoveSignatory::operator!=(const Command &command) const {
return !operator==(command);
}

bool Account::Permissions::operator==(const Permissions &rhs) const {
return rhs.add_signatory == add_signatory &&
rhs.can_transfer == can_transfer &&
rhs.create_accounts == create_accounts &&
rhs.create_assets == create_assets &&
rhs.create_domains == create_domains &&
rhs.issue_assets == issue_assets &&
rhs.read_all_accounts == read_all_accounts &&
rhs.remove_signatory == remove_signatory &&
rhs.set_permissions == set_permissions &&
rhs.set_quorum == set_quorum;
}

bool Account::Permissions::operator!=(const Permissions &rhs) const {
return !rhs.operator==(rhs);
}

/* Set permissions */
bool SetAccountPermissions::operator==(const Command &command) const {
if (! instanceof <SetAccountPermissions>(&command)) return false;
auto set_account_permissions =
static_cast<const SetAccountPermissions &>(command);
return set_account_permissions.account_id == account_id &&
set_account_permissions.new_permissions == new_permissions;
}

bool SetAccountPermissions::operator!=(const Command &command) const {
return !operator==(command);
}

/* Set Quorum*/
bool SetQuorum::operator==(const Command &command) const {
if (! instanceof <SetQuorum>(&command)) return false;
auto set_quorum = static_cast<const SetQuorum &>(command);
return set_quorum.account_id == account_id &&
set_quorum.new_quorum == new_quorum;
}

bool SetQuorum::operator!=(const Command &command) const {
return !operator==(command);
}

/* Transfer Asset */
bool TransferAsset::operator==(const Command &command) const {
if (! instanceof <TransferAsset>(&command)) return false;
auto transfer_asset = static_cast<const TransferAsset &>(command);
return transfer_asset.asset_id == asset_id &&
transfer_asset.amount == amount &&
transfer_asset.src_account_id == src_account_id &&
transfer_asset.dest_account_id == dest_account_id;
}
bool TransferAsset::operator!=(const Command &command) const {
return !operator==(command);
}
}
}
7 changes: 7 additions & 0 deletions libs/common/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <array>
#include <crypto/base64.hpp>
#include <cstdio>
#include <typeinfo>

/**
* This file defines common types used in iroha.
Expand Down Expand Up @@ -107,5 +108,11 @@ namespace iroha {
using ts64_t = uint64_t;
using ts32_t = uint32_t;

// check the type of the derived class
template<typename Base, typename T>
inline bool instanceof(const T *ptr) {
return typeid(Base) == typeid(*ptr);
}

} // namespace iroha
#endif // IROHA_COMMON_HPP

0 comments on commit 107c58b

Please sign in to comment.