|
| 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/chain/custom_authority_evaluator.hpp> |
| 26 | +#include <graphene/chain/custom_authority_object.hpp> |
| 27 | +#include <graphene/chain/account_object.hpp> |
| 28 | +#include <graphene/chain/database.hpp> |
| 29 | +#include <graphene/chain/exceptions.hpp> |
| 30 | +#include <graphene/chain/hardfork_visitor.hpp> |
| 31 | + |
| 32 | +namespace graphene { namespace chain { |
| 33 | + |
| 34 | +void_result custom_authority_create_evaluator::do_evaluate(const custom_authority_create_operation& op) |
| 35 | +{ try { |
| 36 | + const database& d = db(); |
| 37 | + auto now = d.head_block_time(); |
| 38 | + FC_ASSERT(HARDFORK_BSIP_40_PASSED(now), "Custom active authorities are not yet enabled"); |
| 39 | + |
| 40 | + op.account(d); |
| 41 | + |
| 42 | + const auto& config = d.get_global_properties().parameters.extensions.value.custom_authority_options; |
| 43 | + FC_ASSERT(config.valid(), "Cannot use custom authorities yet: global configuration not set"); |
| 44 | + FC_ASSERT(op.valid_to > now, "Custom authority expiration must be in the future"); |
| 45 | + FC_ASSERT((op.valid_to - now).to_seconds() <= config->max_custom_authority_lifetime_seconds, |
| 46 | + "Custom authority lifetime exceeds maximum limit"); |
| 47 | + |
| 48 | + bool operation_forked_in = hardfork_visitor(now).visit((operation::tag_type)op.operation_type.value); |
| 49 | + FC_ASSERT(operation_forked_in, "Cannot create custom authority for operation which is not valid yet"); |
| 50 | + |
| 51 | + auto restriction_count = restriction::restriction_count(op.restrictions); |
| 52 | + FC_ASSERT(restriction_count <= config->max_custom_authority_restrictions, |
| 53 | + "Custom authority has more than the maximum number of restrictions"); |
| 54 | + |
| 55 | + for (const auto& account_weight_pair : op.auth.account_auths) |
| 56 | + account_weight_pair.first(d); |
| 57 | + |
| 58 | + const auto& index = d.get_index_type<custom_authority_index>().indices().get<by_account_custom>(); |
| 59 | + auto range = index.equal_range(op.account); |
| 60 | + FC_ASSERT(std::distance(range.first, range.second) < config->max_custom_authorities_per_account, |
| 61 | + "Cannot create custom authority: account already has maximum number"); |
| 62 | + range = index.equal_range(boost::make_tuple(op.account, op.operation_type)); |
| 63 | + FC_ASSERT(std::distance(range.first, range.second) < config->max_custom_authorities_per_account_op, |
| 64 | + "Cannot create custom authority: account already has maximum number for this operation type"); |
| 65 | + |
| 66 | + return void_result(); |
| 67 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 68 | + |
| 69 | +object_id_type custom_authority_create_evaluator::do_apply(const custom_authority_create_operation& op) |
| 70 | +{ try { |
| 71 | + database& d = db(); |
| 72 | + |
| 73 | + return d.create<custom_authority_object>([&op] (custom_authority_object& obj) mutable { |
| 74 | + obj.account = op.account; |
| 75 | + obj.enabled = op.enabled; |
| 76 | + obj.valid_from = op.valid_from; |
| 77 | + obj.valid_to = op.valid_to; |
| 78 | + obj.operation_type = op.operation_type; |
| 79 | + obj.auth = op.auth; |
| 80 | + std::for_each(op.restrictions.begin(), op.restrictions.end(), [&obj](const restriction& r) mutable { |
| 81 | + obj.restrictions.insert(std::make_pair(obj.restriction_counter++, r)); |
| 82 | + }); |
| 83 | + }).id; |
| 84 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 85 | + |
| 86 | +void_result custom_authority_update_evaluator::do_evaluate(const custom_authority_update_operation& op) |
| 87 | +{ try { |
| 88 | + const database& d = db(); |
| 89 | + auto now = d.head_block_time(); |
| 90 | + old_object = &op.authority_to_update(d); |
| 91 | + FC_ASSERT(old_object->account == op.account, "Cannot update a different account's custom authority"); |
| 92 | + |
| 93 | + if (op.new_enabled) |
| 94 | + FC_ASSERT(*op.new_enabled != old_object->enabled, |
| 95 | + "Custom authority update specifies an enabled flag, but flag is not changed"); |
| 96 | + |
| 97 | + const auto& config = d.get_global_properties().parameters.extensions.value.custom_authority_options; |
| 98 | + auto valid_from = old_object->valid_from; |
| 99 | + auto valid_to = old_object->valid_to; |
| 100 | + if (op.new_valid_from) { |
| 101 | + FC_ASSERT(*op.new_valid_from != old_object->valid_from, |
| 102 | + "Custom authority update specifies a new valid from date, but date is not changed"); |
| 103 | + valid_from = *op.new_valid_from; |
| 104 | + } |
| 105 | + if (op.new_valid_to) { |
| 106 | + FC_ASSERT(*op.new_valid_to != old_object->valid_to, |
| 107 | + "Custom authority update specifies a new valid to date, but date is not changed"); |
| 108 | + FC_ASSERT(*op.new_valid_to > now, "Custom authority expiration must be in the future"); |
| 109 | + FC_ASSERT((*op.new_valid_to - now).to_seconds() <= config->max_custom_authority_lifetime_seconds, |
| 110 | + "Custom authority lifetime exceeds maximum limit"); |
| 111 | + valid_to = *op.new_valid_to; |
| 112 | + } |
| 113 | + FC_ASSERT(valid_from < valid_to, "Custom authority validity begin date must be before expiration date"); |
| 114 | + |
| 115 | + if (op.new_auth) { |
| 116 | + FC_ASSERT(*op.new_auth != old_object->auth, |
| 117 | + "Custom authority update specifies a new authentication authority, but authority is not changed"); |
| 118 | + for (const auto& account_weight_pair : op.new_auth->account_auths) |
| 119 | + account_weight_pair.first(d); |
| 120 | + } |
| 121 | + |
| 122 | + std::for_each(op.restrictions_to_remove.begin(), op.restrictions_to_remove.end(), [this](uint16_t id) { |
| 123 | + FC_ASSERT(old_object->restrictions.count(id) == 1, "Cannot remove restriction ID ${I}: ID not found", |
| 124 | + ("I", id)); |
| 125 | + }); |
| 126 | + if (!op.restrictions_to_add.empty()) { |
| 127 | + // Sanity check |
| 128 | + if (!old_object->restrictions.empty()) |
| 129 | + FC_ASSERT((--old_object->restrictions.end())->first < old_object->restriction_counter, |
| 130 | + "LOGIC ERROR: Restriction counter overlaps restrictions. Please report this error."); |
| 131 | + FC_ASSERT(old_object->restriction_counter + op.restrictions_to_add.size() > old_object->restriction_counter, |
| 132 | + "Unable to add restrictions: causes wraparound of restriction IDs"); |
| 133 | + } |
| 134 | + |
| 135 | + // Add up the restriction counts for all old restrictions not being removed, and all new ones |
| 136 | + size_t restriction_count = 0; |
| 137 | + for (const auto& restriction_pair : old_object->restrictions) |
| 138 | + if (op.restrictions_to_remove.count(restriction_pair.first) == 0) |
| 139 | + restriction_count += restriction_pair.second.restriction_count(); |
| 140 | + restriction_count += restriction::restriction_count(op.restrictions_to_add); |
| 141 | + // Check restriction count against limit |
| 142 | + FC_ASSERT(restriction_count <= config->max_custom_authority_restrictions, |
| 143 | + "Cannot update custom authority: updated authority would exceed the maximum number of restrictions"); |
| 144 | + |
| 145 | + get_restriction_predicate(op.restrictions_to_add, old_object->operation_type); |
| 146 | + return void_result(); |
| 147 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 148 | + |
| 149 | +void_result custom_authority_update_evaluator::do_apply(const custom_authority_update_operation& op) |
| 150 | +{ try { |
| 151 | + database& d = db(); |
| 152 | + |
| 153 | + d.modify(*old_object, [&op](custom_authority_object& obj) { |
| 154 | + if (op.new_enabled) obj.enabled = *op.new_enabled; |
| 155 | + if (op.new_valid_from) obj.valid_from = *op.new_valid_from; |
| 156 | + if (op.new_valid_to) obj.valid_to = *op.new_valid_to; |
| 157 | + if (op.new_auth) obj.auth = *op.new_auth; |
| 158 | + |
| 159 | + std::for_each(op.restrictions_to_remove.begin(), op.restrictions_to_remove.end(), [&obj](auto id) mutable { |
| 160 | + obj.restrictions.erase(id); |
| 161 | + }); |
| 162 | + std::for_each(op.restrictions_to_add.begin(), op.restrictions_to_add.end(), [&obj](const auto& r) mutable { |
| 163 | + obj.restrictions.insert(std::make_pair(obj.restriction_counter++, r)); |
| 164 | + }); |
| 165 | + |
| 166 | + // Clear the predicate cache |
| 167 | + obj.clear_predicate_cache(); |
| 168 | + }); |
| 169 | + |
| 170 | + return void_result(); |
| 171 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 172 | + |
| 173 | +void_result custom_authority_delete_evaluator::do_evaluate(const custom_authority_delete_operation& op) |
| 174 | +{ try { |
| 175 | + const database& d = db(); |
| 176 | + |
| 177 | + old_object = &op.authority_to_delete(d); |
| 178 | + FC_ASSERT(old_object->account == op.account, "Cannot delete a different account's custom authority"); |
| 179 | + |
| 180 | + return void_result(); |
| 181 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 182 | + |
| 183 | +void_result custom_authority_delete_evaluator::do_apply(const custom_authority_delete_operation& op) |
| 184 | +{ try { |
| 185 | + database& d = db(); |
| 186 | + |
| 187 | + d.remove(*old_object); |
| 188 | + |
| 189 | + return void_result(); |
| 190 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 191 | + |
| 192 | +} } // graphene::chain |
0 commit comments