Skip to content

Commit 51da97d

Browse files
BSIP 40: Code review, round 2
1 parent e658bdd commit 51da97d

File tree

6 files changed

+66
-34
lines changed

6 files changed

+66
-34
lines changed

libraries/chain/custom_authority_evaluator.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void_result custom_authority_create_evaluator::do_evaluate(const custom_authorit
4848
bool operation_forked_in = hardfork_visitor(now).visit((operation::tag_type)op.operation_type.value);
4949
FC_ASSERT(operation_forked_in, "Cannot create custom authority for operation which is not valid yet");
5050

51-
auto restriction_count = std::for_each(op.restrictions.begin(), op.restrictions.end(), restriction::adder()).sum;
51+
auto restriction_count = restriction::restriction_count(op.restrictions);
5252
FC_ASSERT(restriction_count <= config->max_custom_authority_restrictions,
5353
"Custom authority has more than the maximum number of restrictions");
5454

@@ -137,8 +137,7 @@ void_result custom_authority_update_evaluator::do_evaluate(const custom_authorit
137137
for (const auto& restriction_pair : old_object->restrictions)
138138
if (op.restrictions_to_remove.count(restriction_pair.first) == 0)
139139
restriction_count += restriction_pair.second.restriction_count();
140-
restriction_count += std::for_each(op.restrictions_to_add.begin(), op.restrictions_to_add.end(),
141-
restriction::adder()).sum;
140+
restriction_count += restriction::restriction_count(op.restrictions_to_add);
142141
// Check restriction count against limit
143142
FC_ASSERT(restriction_count <= config->max_custom_authority_restrictions,
144143
"Cannot update custom authority: updated authority would exceed the maximum number of restrictions");

libraries/chain/proposal_evaluator.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ struct proposal_operation_hardfork_visitor
9595
}
9696
void operator()(const graphene::chain::committee_member_update_global_parameters_operation &op) const {
9797
if (block_time < HARDFORK_CORE_1468_TIME) {
98-
FC_ASSERT(!op.new_parameters.extensions.value.updatable_htlc_options.valid(),
99-
"Unable to set HTLC options before hardfork 1468");
98+
FC_ASSERT(!op.new_parameters.extensions.value.updatable_htlc_options.valid(), "Unable to set HTLC options before hardfork 1468");
10099
FC_ASSERT(!op.new_parameters.current_fees->exists<htlc_create_operation>());
101100
FC_ASSERT(!op.new_parameters.current_fees->exists<htlc_redeem_operation>());
102101
FC_ASSERT(!op.new_parameters.current_fees->exists<htlc_extend_operation>());

libraries/protocol/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ list(APPEND SOURCES account.cpp
1616
asset.cpp
1717
authority.cpp
1818
special_authority.cpp
19+
restriction.cpp
1920
custom_authority.cpp
2021
committee_member.cpp
2122
custom.cpp

libraries/protocol/include/graphene/protocol/restriction.hpp

+2-19
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,8 @@ struct restriction {
108108
restriction(const unsigned_int& member_index, function_type type, const argument_type& argument)
109109
: member_index(member_index), restriction_type(type), argument(argument) {}
110110

111-
struct adder {
112-
size_t sum = 0;
113-
void operator()(const restriction& r) { sum += r.restriction_count(); }
114-
void operator()(const vector<restriction>& r) { sum += std::for_each(r.begin(), r.end(), adder()).sum; }
115-
};
116-
117-
size_t restriction_count() const {
118-
if (argument.is_type<vector<restriction>>()) {
119-
const vector<restriction>& rs = argument.get<vector<restriction>>();
120-
return 1 + std::for_each(rs.begin(), rs.end(), adder()).sum;
121-
} else if (argument.is_type<vector<vector<restriction>>>()) {
122-
const vector<vector<restriction>>& rs = argument.get<vector<vector<restriction>>>();
123-
return 1 + std::for_each(rs.begin(), rs.end(), adder()).sum;
124-
} else if (argument.is_type<variant_assert_argument_type>()) {
125-
const variant_assert_argument_type& arg = argument.get<variant_assert_argument_type>();
126-
return 1 + std::for_each(arg.second.begin(), arg.second.end(), adder()).sum;
127-
}
128-
return 1;
129-
}
111+
static size_t restriction_count(const vector<restriction>& restrictions);
112+
size_t restriction_count() const;
130113
};
131114

132115
} } // graphene::protocol

libraries/protocol/restriction.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2019 Contributors.
3+
*
4+
* The MIT License
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#include <graphene/protocol/restriction.hpp>
26+
27+
namespace graphene { namespace protocol {
28+
29+
struct adder {
30+
size_t sum = 0;
31+
void operator()(const restriction& r) { sum += r.restriction_count(); }
32+
void operator()(const vector<restriction>& r) { sum += std::for_each(r.begin(), r.end(), adder()).sum; }
33+
};
34+
35+
size_t restriction::restriction_count(const vector<restriction>& restrictions) {
36+
return std::for_each(restrictions.begin(), restrictions.end(), adder()).sum;
37+
}
38+
39+
size_t restriction::restriction_count() const {
40+
if (argument.is_type<vector<restriction>>()) {
41+
const vector<restriction>& rs = argument.get<vector<restriction>>();
42+
return 1 + std::for_each(rs.begin(), rs.end(), adder()).sum;
43+
} else if (argument.is_type<vector<vector<restriction>>>()) {
44+
const vector<vector<restriction>>& rs = argument.get<vector<vector<restriction>>>();
45+
return 1 + std::for_each(rs.begin(), rs.end(), adder()).sum;
46+
} else if (argument.is_type<variant_assert_argument_type>()) {
47+
const variant_assert_argument_type& arg = argument.get<variant_assert_argument_type>();
48+
return 1 + std::for_each(arg.second.begin(), arg.second.end(), adder()).sum;
49+
}
50+
return 1;
51+
}
52+
53+
} } // namespace graphene::protocol

tests/tests/custom_authority_tests.cpp

+7-10
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ BOOST_FIXTURE_TEST_SUITE(custom_authority_tests, database_fixture)
5252

5353
#define FUNC(TYPE) BOOST_PP_CAT(restriction::func_, TYPE)
5454

55-
size_t restriction_count(const vector<restriction>& rs) {
56-
return std::for_each(rs.begin(), rs.end(), restriction::adder()).sum;
57-
}
5855
template<typename Object>
5956
unsigned_int member_index(string name) {
6057
unsigned_int index;
@@ -109,7 +106,7 @@ BOOST_AUTO_TEST_CASE(restriction_predicate_tests) { try {
109106
// "extensions": []
110107
// }
111108
//]
112-
BOOST_CHECK_EQUAL(restriction_count(restrictions), 1);
109+
BOOST_CHECK_EQUAL(restriction::restriction_count(restrictions), 1);
113110
BOOST_CHECK(get_restriction_predicate(restrictions, operation::tag<transfer_operation>::value)(transfer)
114111
.rejection_path.size() == 2);
115112
// Index 0 (the outer-most) rejection path refers to the first and only restriction
@@ -226,7 +223,7 @@ BOOST_AUTO_TEST_CASE(restriction_predicate_tests) { try {
226223
// }
227224
//]
228225

229-
BOOST_CHECK_EQUAL(restriction_count(restrictions), 2);
226+
BOOST_CHECK_EQUAL(restriction::restriction_count(restrictions), 2);
230227
//////
231228
// Check the transfer operation that pays the fee with Asset ID 0 against the restriction.
232229
// This should violate the restriction.
@@ -280,7 +277,7 @@ BOOST_AUTO_TEST_CASE(restriction_predicate_tests) { try {
280277
// "extensions": []
281278
// }
282279
//]
283-
BOOST_CHECK_EQUAL(restriction_count(restrictions), 3);
280+
BOOST_CHECK_EQUAL(restriction::restriction_count(restrictions), 3);
284281

285282
//////
286283
// Create a transfer operation that authorizes transfer to Account ID 12
@@ -338,7 +335,7 @@ BOOST_AUTO_TEST_CASE(restriction_predicate_tests) { try {
338335
// "extensions": []
339336
// }
340337
//]
341-
BOOST_CHECK_EQUAL(restriction_count(restrictions), 2);
338+
BOOST_CHECK_EQUAL(restriction::restriction_count(restrictions), 2);
342339
auto predicate = get_restriction_predicate(restrictions, operation::tag<account_update_operation>::value);
343340

344341
//////
@@ -453,7 +450,7 @@ BOOST_AUTO_TEST_CASE(restriction_predicate_tests) { try {
453450
// "extensions": []
454451
// }
455452
//]
456-
BOOST_CHECK_EQUAL(restriction_count(or_restrictions), 3);
453+
BOOST_CHECK_EQUAL(restriction::restriction_count(or_restrictions), 3);
457454
auto predicate = get_restriction_predicate(or_restrictions, operation::tag<transfer_operation>::value);
458455

459456
//////
@@ -570,7 +567,7 @@ BOOST_AUTO_TEST_CASE(custom_auths) { try {
570567
// "extensions": []
571568
// }
572569
//]
573-
BOOST_CHECK_EQUAL(restriction_count(op.restrictions), 3);
570+
BOOST_CHECK_EQUAL(restriction::restriction_count(op.restrictions), 3);
574571

575572

576573
//////
@@ -1643,7 +1640,7 @@ BOOST_AUTO_TEST_CASE(custom_auths) { try {
16431640
// "extensions": []
16441641
// }
16451642
//]
1646-
BOOST_CHECK_EQUAL(restriction_count(op.restrictions), 3);
1643+
BOOST_CHECK_EQUAL(restriction::restriction_count(op.restrictions), 3);
16471644

16481645
// Publish the new custom authority
16491646
trx.clear();

0 commit comments

Comments
 (0)