Skip to content

Commit

Permalink
- Add missed tests for commands;
Browse files Browse the repository at this point in the history
- Fix bug in pb converter (missed filling field)
- Add constructors for Account
- Fix implicit constructor in block serializer
  • Loading branch information
muratovv authored and lebdron committed Jul 19, 2017
1 parent 3da4fda commit 846a26e
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 30 deletions.
2 changes: 1 addition & 1 deletion irohad/ametsuchi/impl/block_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ namespace iroha {
BlockSerializer::deserialize_add_asset_quantity(
GenericValue<rapidjson::UTF8<char>>::Object& json_command) {
// TODO: make this function return nullopt when some field is missed
model::AddAssetQuantity add_asset_quantity{};
model::AddAssetQuantity add_asset_quantity;

// account_id
add_asset_quantity.account_id = json_command["account_id"].GetString();
Expand Down
17 changes: 15 additions & 2 deletions irohad/model/account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ namespace iroha {
*/
struct Account {
struct Permissions {
Permissions() {
issue_assets = false;
create_assets = false;
create_accounts = false;
create_domains = false;
read_all_accounts = false;
add_signatory = false;
remove_signatory = false;
set_permissions = false;
set_quorum = false;
can_transfer = false;
}

/**
* Can account add assets to own account;
* Dangerous operation - require high number of quorum;
Expand Down Expand Up @@ -83,8 +96,8 @@ namespace iroha {
*/
bool can_transfer;

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

/**
Expand Down
7 changes: 5 additions & 2 deletions irohad/model/converters/impl/pb_command_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,15 @@ namespace iroha {
const protocol::RemoveSignatory &pb_remove_signatory) {
model::RemoveSignatory remove_signatory;
remove_signatory.account_id = pb_remove_signatory.account_id();
std::copy(pb_remove_signatory.public_key().begin(),
pb_remove_signatory.public_key().end(),
remove_signatory.pubkey.begin());
return remove_signatory;
}

// set account permissions
protocol::SetAccountPermissions PbCommandFactory::serializeSetAccountPermissions(
const model::SetAccountPermissions &set_account_permissions) {
const model::SetAccountPermissions &set_account_permissions) {
protocol::SetAccountPermissions pb_set_account_permissions;
pb_set_account_permissions.set_account_id(set_account_permissions.account_id);
auto permissions = pb_set_account_permissions.mutable_permissions();
Expand All @@ -196,7 +199,7 @@ namespace iroha {

model::SetAccountPermissions
PbCommandFactory::deserializeSetAccountPermissions(
const protocol::SetAccountPermissions &pb_set_account_permissions) {
const protocol::SetAccountPermissions &pb_set_account_permissions) {
model::SetAccountPermissions set_account_permissions;
set_account_permissions.account_id =
pb_set_account_permissions.account_id();
Expand Down
24 changes: 17 additions & 7 deletions libs/common/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ namespace iroha {
uint8_t front, back;
auto ptr = this->data();
for (uint32_t i = 0, k = 0; i < size_; i++) {
front = (uint8_t)(ptr[i] & 0xF0) >> 4;
back = (uint8_t)(ptr[i] & 0xF);
front = (uint8_t) (ptr[i] & 0xF0) >> 4;
back = (uint8_t) (ptr[i] & 0xF);
res[k++] = code[front];
res[k++] = code[back];
}
return res;
}
};

template <size_t size>
template<size_t size>
using hash_t = blob_t<size>;

// fixed-size hashes
Expand Down Expand Up @@ -113,6 +113,16 @@ namespace iroha {
uint64_t int_part;
uint64_t frac_part;

Amount(uint64_t integer_part, uint64_t fractional_part) {
int_part = integer_part;
frac_part = fractional_part;
}

Amount() {
int_part = 0;
frac_part = 0;
}

uint32_t get_frac_number() { return std::to_string(frac_part).length(); }

uint64_t get_joint_amount(uint32_t precision) {
Expand All @@ -138,13 +148,13 @@ namespace iroha {
};

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

template <typename Base, typename T>
inline bool instanceof (const T &ptr) {
template<typename Base, typename T>
inline bool instanceof(const T &ptr) {
return typeid(Base) == typeid(ptr);
}

Expand Down
118 changes: 100 additions & 18 deletions test/module/irohad/model/converters/pb_commands_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,33 @@

#include "model/converters/pb_command_factory.hpp"

#include <algorithm>

void command_converter_test(iroha::model::Command &abstract_command) {
auto factory = iroha::model::converters::PbCommandFactory();
auto pb_repr = factory.serializeAbstractCommand(abstract_command);
auto model_repr = factory.deserializeAbstractCommand(pb_repr);
ASSERT_EQ(abstract_command, *model_repr);
}

TEST(CommandTest, add_asset_quantity) {
auto orig_command = iroha::model::AddAssetQuantity();
orig_command.account_id = "23";
iroha::Amount amount;
amount.frac_part = 50;
amount.int_part = 1;

orig_command.amount = amount;
orig_command.asset_id = "23";

auto factory = iroha::model::converters::PbCommandFactory();
auto proto_command = factory.serializeAddAssetQuantity(orig_command);
auto serial_command = factory.deserializeAddAssetQuantity(proto_command);

ASSERT_EQ(orig_command, serial_command);
command_converter_test(orig_command);
}

TEST(CommandTest, add_peer) {
auto orig_addPeer = iroha::model::AddPeer();
orig_addPeer.address = "10.90.129.23";
Expand All @@ -54,7 +74,6 @@ TEST(CommandTest, add_peer) {
ASSERT_NE(serial_addPeer, orig_addPeer);
}


TEST(CommandTest, add_signatory) {
auto orig_command = iroha::model::AddSignatory();
orig_command.account_id = "23";
Expand All @@ -77,33 +96,30 @@ TEST(CommandTest, add_signatory_abstract_factory) {
command_converter_test(orig_command);
}


TEST(CommandTest, add_asset_quantity) {
auto orig_command = iroha::model::AddAssetQuantity();
TEST(CommandTest, assign_master_key) {
auto orig_command = iroha::model::AssignMasterKey();
orig_command.account_id = "23";
iroha::Amount amount;
amount.frac_part = 50;
amount.int_part = 1;

orig_command.amount = amount;
orig_command.asset_id = "23";

auto factory = iroha::model::converters::PbCommandFactory();
auto proto_command = factory.serializeAddAssetQuantity(orig_command);
auto serial_command = factory.deserializeAddAssetQuantity(proto_command);
auto proto_command = factory.serializeAssignMasterKey(orig_command);
auto serial_command = factory.deserializeAssignMasterKey(proto_command);

ASSERT_EQ(orig_command, serial_command);
command_converter_test(orig_command);
}

TEST(CommandTest, assign_master_key) {
auto orig_command = iroha::model::AssignMasterKey();
orig_command.account_id = "23";
TEST(CommandTest, create_asset) {
auto factory = iroha::model::converters::PbCommandFactory();
auto proto_command = factory.serializeAssignMasterKey(orig_command);
auto serial_command = factory.deserializeAssignMasterKey(proto_command);

auto orig_command = iroha::model::CreateAsset();
orig_command.domain_id = "kek_cheburek";
orig_command.precision = 1;
orig_command.asset_name = "test_asset";

auto proto_command = factory.serializeCreateAsset(orig_command);
auto serial_command = factory.deserializeCreateAsset(proto_command);

ASSERT_EQ(orig_command, serial_command);

command_converter_test(orig_command);
}

Expand All @@ -118,3 +134,69 @@ TEST(CommandTest, create_account) {
command_converter_test(orig_command);
}

TEST(CommandTest, remove_signatory) {
auto factory = iroha::model::converters::PbCommandFactory();

auto orig_command = iroha::model::RemoveSignatory();
orig_command.account_id = "Vasya";
std::fill(orig_command.pubkey.begin(), orig_command.pubkey.end(), 0xF);

auto proto_command = factory.serializeRemoveSignatory(orig_command);
auto serial_command = factory.deserializeRemoveSignatory(proto_command);

ASSERT_EQ(orig_command, serial_command);

command_converter_test(orig_command);
}

TEST(CommandTest, set_acount_permissions) {
auto factory = iroha::model::converters::PbCommandFactory();

auto orig_command = iroha::model::SetAccountPermissions();
orig_command.account_id = "Vasya";
iroha::model::Account::Permissions perm;
perm.can_transfer = true;
perm.add_signatory = true;
perm.issue_assets = true;
orig_command.new_permissions = perm;

auto proto_command = factory.serializeSetAccountPermissions(orig_command);
auto serial_command = factory.deserializeSetAccountPermissions(proto_command);

ASSERT_EQ(orig_command, serial_command);

command_converter_test(orig_command);
}

TEST(CommandTest, set_account_quorum) {
auto factory = iroha::model::converters::PbCommandFactory();

auto orig_command = iroha::model::SetQuorum();
orig_command.new_quorum = 11;
orig_command.account_id = "Vasya";

auto proto_command = factory.serializeSetQuorum(orig_command);
auto serial_command = factory.deserializeSetQuorum(proto_command);

ASSERT_EQ(orig_command, serial_command);

command_converter_test(orig_command);
}

TEST(CommandTest, set_transfer_asset) {
auto factory = iroha::model::converters::PbCommandFactory();

auto orig_command = iroha::model::TransferAsset();
orig_command.amount = {1, 20};
orig_command.asset_id = "tugrik";
orig_command.src_account_id = "Vasya";
orig_command.dest_account_id = "Petya";

auto proto_command = factory.serializeTransferAsset(orig_command);
auto serial_command = factory.deserializeTransferAsset(proto_command);

ASSERT_EQ(orig_command, serial_command);

command_converter_test(orig_command);
}

0 comments on commit 846a26e

Please sign in to comment.