-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BSIP 40: Add evaluators, transaction eval code, testing
Implementation passes cursory tests.
- Loading branch information
1 parent
7c3bdb2
commit fc168ec
Showing
20 changed files
with
527 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* Copyright (c) 2019 Contributors. | ||
* | ||
* The MIT License | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include <graphene/chain/custom_authority_evaluator.hpp> | ||
#include <graphene/chain/custom_authority_object.hpp> | ||
#include <graphene/chain/account_object.hpp> | ||
#include <graphene/chain/database.hpp> | ||
#include <graphene/chain/exceptions.hpp> | ||
#include <graphene/chain/hardfork.hpp> | ||
|
||
namespace graphene { namespace chain { | ||
|
||
void_result custom_authority_create_evaluator::do_evaluate(const custom_authority_create_operation& op) | ||
{ try { | ||
const database& d = db(); | ||
auto now = d.head_block_time(); | ||
FC_ASSERT(HARDFORK_BSIP_40_PASSED(now), "Custom active authorities are not yet enabled"); | ||
|
||
op.account(d); | ||
|
||
const auto& config = global_property_id_type()(d).parameters.extensions.value.custom_authority_options; | ||
FC_ASSERT(config.valid(), "Cannot use custom authorities yet: global configuration not set"); | ||
FC_ASSERT(op.valid_to > now, "Custom authority expiration must be in the future"); | ||
FC_ASSERT((op.valid_to - now).to_seconds() <= config->max_custom_authority_lifetime_seconds, | ||
"Custom authority lifetime exceeds maximum limit"); | ||
|
||
FC_ASSERT(op.operation_type.value <= config->max_operation_tag, | ||
"Cannot create custom authority for operation type which is not yet active"); | ||
|
||
for (const auto& account_weight_pair : op.auth.account_auths) | ||
account_weight_pair.first(d); | ||
|
||
const auto& index = d.get_index_type<custom_authority_index>().indices().get<by_account_custom>(); | ||
auto range = index.equal_range(op.account); | ||
FC_ASSERT(std::distance(range.first, range.second) < config->max_custom_authorities_per_account, | ||
"Cannot create custom authority for account: account already has maximum number"); | ||
|
||
predicate = get_restriction_predicate(op.restrictions, op.operation_type); | ||
return void_result(); | ||
} FC_CAPTURE_AND_RETHROW((op)) } | ||
|
||
object_id_type custom_authority_create_evaluator::do_apply(const custom_authority_create_operation& op) | ||
{ try { | ||
database& d = db(); | ||
|
||
return d.create<custom_authority_object>([&op, p=std::move(predicate)] (custom_authority_object& obj) mutable { | ||
obj.account = op.account; | ||
obj.enabled = op.enabled; | ||
obj.valid_from = op.valid_from; | ||
obj.valid_to = op.valid_to; | ||
obj.operation_type = op.operation_type; | ||
obj.auth = op.auth; | ||
obj.restrictions = op.restrictions; | ||
|
||
obj.predicate_cache = std::move(p); | ||
}).id; | ||
} FC_CAPTURE_AND_RETHROW((op)) } | ||
|
||
void_result custom_authority_update_evaluator::do_evaluate(const custom_authority_update_operation& op) | ||
{ try { | ||
const database& d = db(); | ||
auto now = d.head_block_time(); | ||
FC_ASSERT(HARDFORK_BSIP_40_PASSED(now), "Custom active authorities are not yet enabled"); | ||
const auto& old_object = op.authority_to_update(d); | ||
|
||
op.account(d); | ||
if (op.new_enabled) | ||
FC_ASSERT(*op.new_enabled != old_object.enabled, | ||
"Custom authority update specifies an enabled flag, but flag is not changed"); | ||
|
||
const auto& config = global_property_id_type()(d).parameters.extensions.value.custom_authority_options; | ||
if (op.new_valid_from) | ||
FC_ASSERT(*op.new_valid_from != old_object.valid_from, | ||
"Custom authority update specifies a new valid from date, but date is not changed"); | ||
if (op.new_valid_to) { | ||
FC_ASSERT(*op.new_valid_to != old_object.valid_to, | ||
"Custom authority update specifies a new valid to date, but date is not changed"); | ||
FC_ASSERT(*op.new_valid_to > now, "Custom authority expiration must be in the future"); | ||
FC_ASSERT((*op.new_valid_to - now).to_seconds() <= config->max_custom_authority_lifetime_seconds, | ||
"Custom authority lifetime exceeds maximum limit"); | ||
} | ||
|
||
if (op.new_auth) { | ||
FC_ASSERT(*op.new_auth != old_object.auth, | ||
"Custom authority update specifies a new authentication authority, but authority is not changed"); | ||
for (const auto& account_weight_pair : op.new_auth->account_auths) | ||
account_weight_pair.first(d); | ||
} | ||
|
||
auto largest_index = *(--op.restrictions_to_remove.end()); | ||
FC_ASSERT(largest_index < old_object.restrictions.size(), | ||
"Index of custom authority restriction to remove is out of bounds"); | ||
|
||
predicate = get_restriction_predicate(op.restrictions_to_add, old_object.operation_type); | ||
return void_result(); | ||
} FC_CAPTURE_AND_RETHROW((op)) } | ||
|
||
void_result custom_authority_update_evaluator::do_apply(const custom_authority_update_operation& op) | ||
{ try { | ||
database& d = db(); | ||
|
||
d.modify(op.authority_to_update(d), [&op, p=std::move(predicate)](custom_authority_object& obj) { | ||
if (op.new_enabled) obj.enabled = *op.new_enabled; | ||
if (op.new_valid_from) obj.valid_from = *op.new_valid_from; | ||
if (op.new_valid_to) obj.valid_to = *op.new_valid_to; | ||
if (op.new_auth) obj.auth = *op.new_auth; | ||
|
||
// Move restrictions at indexes to be removed to the end, then truncate them. | ||
// Note: we could use partition instead of stable_partition, which would be slightly faster, but would also | ||
// reorder the restrictions. I opted to preserve order as a courtesy to the user, who obviously does care about | ||
// what items are at what indexes (removed items are specified by index) | ||
std::stable_partition(obj.restrictions.begin(), obj.restrictions.end(), [&op, index=0](const auto&) mutable { | ||
return op.restrictions_to_remove.count(index++) == 0; | ||
}); | ||
obj.restrictions.resize(obj.restrictions.size() - op.restrictions_to_remove.size()); | ||
|
||
obj.restrictions.insert(obj.restrictions.end(), op.restrictions_to_add.begin(), op.restrictions_to_add.end()); | ||
|
||
obj.predicate_cache = std::move(p); | ||
}); | ||
|
||
return void_result(); | ||
} FC_CAPTURE_AND_RETHROW((op)) } | ||
|
||
void_result custom_authority_delete_evaluator::do_evaluate(const custom_authority_delete_operation& op) | ||
{ try { | ||
const database& d = db(); | ||
FC_ASSERT(HARDFORK_BSIP_40_PASSED(d.head_block_time()), "Custom active authorities are not yet enabled"); | ||
|
||
op.account(d); | ||
op.authority_to_delete(d); | ||
|
||
return void_result(); | ||
} FC_CAPTURE_AND_RETHROW((op)) } | ||
|
||
void_result custom_authority_delete_evaluator::do_apply(const custom_authority_delete_operation& op) | ||
{ try { | ||
database& d = db(); | ||
|
||
d.remove(op.authority_to_delete(d)); | ||
|
||
return void_result(); | ||
} FC_CAPTURE_AND_RETHROW((op)) } | ||
|
||
} } // graphene::chain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// BSIP 40 (Custom Active Authorities) hardfork check | ||
#ifndef HARDFORK_BSIP_40_TIME | ||
// Jan 1 2030, midnight; this is a dummy date until a hardfork date is scheduled | ||
#define HARDFORK_BSIP_40_TIME (fc::time_point_sec( 1893456000 )) | ||
#define HARDFORK_BSIP_40_PASSED(now) (now >= HARDFORK_BSIP_40_TIME) | ||
#endif |
58 changes: 58 additions & 0 deletions
58
libraries/chain/include/graphene/chain/custom_authority_evaluator.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2019 Contributors. | ||
* | ||
* The MIT License | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
#pragma once | ||
|
||
#include <graphene/protocol/restriction_predicate.hpp> | ||
|
||
#include <graphene/chain/evaluator.hpp> | ||
|
||
namespace graphene { namespace chain { | ||
|
||
class custom_authority_create_evaluator : public evaluator<custom_authority_create_evaluator> { | ||
public: | ||
using operation_type = custom_authority_create_operation; | ||
restriction_predicate_function predicate; | ||
|
||
void_result do_evaluate(const operation_type& op); | ||
object_id_type do_apply(const operation_type& op); | ||
}; | ||
|
||
class custom_authority_update_evaluator : public evaluator<custom_authority_update_evaluator> { | ||
public: | ||
using operation_type = custom_authority_update_operation; | ||
restriction_predicate_function predicate; | ||
|
||
void_result do_evaluate(const operation_type& op); | ||
void_result do_apply(const operation_type& op); | ||
}; | ||
|
||
class custom_authority_delete_evaluator : public evaluator<custom_authority_delete_evaluator> { | ||
public: | ||
using operation_type = custom_authority_delete_operation; | ||
|
||
void_result do_evaluate(const operation_type& op); | ||
void_result do_apply(const operation_type& op); | ||
}; | ||
|
||
} } // graphene::chain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.