|
| 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.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 = global_property_id_type()(d).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 | + FC_ASSERT(op.operation_type.value <= config->max_operation_tag, |
| 49 | + "Cannot create custom authority for operation type which is not yet active"); |
| 50 | + |
| 51 | + for (const auto& account_weight_pair : op.auth.account_auths) |
| 52 | + account_weight_pair.first(d); |
| 53 | + |
| 54 | + const auto& index = d.get_index_type<custom_authority_index>().indices().get<by_account_custom>(); |
| 55 | + auto range = index.equal_range(op.account); |
| 56 | + FC_ASSERT(std::distance(range.first, range.second) < config->max_custom_authorities_per_account, |
| 57 | + "Cannot create custom authority for account: account already has maximum number"); |
| 58 | + |
| 59 | + predicate = get_restriction_predicate(op.restrictions, op.operation_type); |
| 60 | + return void_result(); |
| 61 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 62 | + |
| 63 | +object_id_type custom_authority_create_evaluator::do_apply(const custom_authority_create_operation& op) |
| 64 | +{ try { |
| 65 | + database& d = db(); |
| 66 | + |
| 67 | + return d.create<custom_authority_object>([&op, p=std::move(predicate)] (custom_authority_object& obj) mutable { |
| 68 | + obj.account = op.account; |
| 69 | + obj.enabled = op.enabled; |
| 70 | + obj.valid_from = op.valid_from; |
| 71 | + obj.valid_to = op.valid_to; |
| 72 | + obj.operation_type = op.operation_type; |
| 73 | + obj.auth = op.auth; |
| 74 | + obj.restrictions = op.restrictions; |
| 75 | + |
| 76 | + obj.predicate_cache = std::move(p); |
| 77 | + }).id; |
| 78 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 79 | + |
| 80 | +void_result custom_authority_update_evaluator::do_evaluate(const custom_authority_update_operation& op) |
| 81 | +{ try { |
| 82 | + const database& d = db(); |
| 83 | + auto now = d.head_block_time(); |
| 84 | + FC_ASSERT(HARDFORK_BSIP_40_PASSED(now), "Custom active authorities are not yet enabled"); |
| 85 | + const auto& old_object = op.authority_to_update(d); |
| 86 | + |
| 87 | + op.account(d); |
| 88 | + if (op.new_enabled) |
| 89 | + FC_ASSERT(*op.new_enabled != old_object.enabled, |
| 90 | + "Custom authority update specifies an enabled flag, but flag is not changed"); |
| 91 | + |
| 92 | + const auto& config = global_property_id_type()(d).parameters.extensions.value.custom_authority_options; |
| 93 | + if (op.new_valid_from) |
| 94 | + FC_ASSERT(*op.new_valid_from != old_object.valid_from, |
| 95 | + "Custom authority update specifies a new valid from date, but date is not changed"); |
| 96 | + if (op.new_valid_to) { |
| 97 | + FC_ASSERT(*op.new_valid_to != old_object.valid_to, |
| 98 | + "Custom authority update specifies a new valid to date, but date is not changed"); |
| 99 | + FC_ASSERT(*op.new_valid_to > now, "Custom authority expiration must be in the future"); |
| 100 | + FC_ASSERT((*op.new_valid_to - now).to_seconds() <= config->max_custom_authority_lifetime_seconds, |
| 101 | + "Custom authority lifetime exceeds maximum limit"); |
| 102 | + } |
| 103 | + |
| 104 | + if (op.new_auth) { |
| 105 | + FC_ASSERT(*op.new_auth != old_object.auth, |
| 106 | + "Custom authority update specifies a new authentication authority, but authority is not changed"); |
| 107 | + for (const auto& account_weight_pair : op.new_auth->account_auths) |
| 108 | + account_weight_pair.first(d); |
| 109 | + } |
| 110 | + |
| 111 | + auto largest_index = *(--op.restrictions_to_remove.end()); |
| 112 | + FC_ASSERT(largest_index < old_object.restrictions.size(), |
| 113 | + "Index of custom authority restriction to remove is out of bounds"); |
| 114 | + |
| 115 | + predicate = get_restriction_predicate(op.restrictions_to_add, old_object.operation_type); |
| 116 | + return void_result(); |
| 117 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 118 | + |
| 119 | +void_result custom_authority_update_evaluator::do_apply(const custom_authority_update_operation& op) |
| 120 | +{ try { |
| 121 | + database& d = db(); |
| 122 | + |
| 123 | + d.modify(op.authority_to_update(d), [&op, p=std::move(predicate)](custom_authority_object& obj) { |
| 124 | + if (op.new_enabled) obj.enabled = *op.new_enabled; |
| 125 | + if (op.new_valid_from) obj.valid_from = *op.new_valid_from; |
| 126 | + if (op.new_valid_to) obj.valid_to = *op.new_valid_to; |
| 127 | + if (op.new_auth) obj.auth = *op.new_auth; |
| 128 | + |
| 129 | + // Move restrictions at indexes to be removed to the end, then truncate them. |
| 130 | + // Note: we could use partition instead of stable_partition, which would be slightly faster, but would also |
| 131 | + // reorder the restrictions. I opted to preserve order as a courtesy to the user, who obviously does care about |
| 132 | + // what items are at what indexes (removed items are specified by index) |
| 133 | + std::stable_partition(obj.restrictions.begin(), obj.restrictions.end(), [&op, index=0](const auto&) mutable { |
| 134 | + return op.restrictions_to_remove.count(index++) == 0; |
| 135 | + }); |
| 136 | + obj.restrictions.resize(obj.restrictions.size() - op.restrictions_to_remove.size()); |
| 137 | + |
| 138 | + obj.restrictions.insert(obj.restrictions.end(), op.restrictions_to_add.begin(), op.restrictions_to_add.end()); |
| 139 | + |
| 140 | + obj.predicate_cache = std::move(p); |
| 141 | + }); |
| 142 | + |
| 143 | + return void_result(); |
| 144 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 145 | + |
| 146 | +void_result custom_authority_delete_evaluator::do_evaluate(const custom_authority_delete_operation& op) |
| 147 | +{ try { |
| 148 | + const database& d = db(); |
| 149 | + FC_ASSERT(HARDFORK_BSIP_40_PASSED(d.head_block_time()), "Custom active authorities are not yet enabled"); |
| 150 | + |
| 151 | + op.account(d); |
| 152 | + op.authority_to_delete(d); |
| 153 | + |
| 154 | + return void_result(); |
| 155 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 156 | + |
| 157 | +void_result custom_authority_delete_evaluator::do_apply(const custom_authority_delete_operation& op) |
| 158 | +{ try { |
| 159 | + database& d = db(); |
| 160 | + |
| 161 | + d.remove(op.authority_to_delete(d)); |
| 162 | + |
| 163 | + return void_result(); |
| 164 | +} FC_CAPTURE_AND_RETHROW((op)) } |
| 165 | + |
| 166 | +} } // graphene::chain |
0 commit comments