BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
ticket_evaluator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Abit More, and 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  */
25 
27 
30 #include <graphene/chain/hardfork.hpp>
31 
33 
34 namespace graphene { namespace chain {
35 
37 { try {
38  const database& d = db();
39  const auto block_time = d.head_block_time();
40 
41  FC_ASSERT( HARDFORK_CORE_2103_PASSED(block_time), "Not allowed until hardfork 2103" );
42 
43  return void_result();
44 } FC_CAPTURE_AND_RETHROW( (op) ) }
45 
47 { try {
48  database& d = db();
49  const auto block_time = d.head_block_time();
50  const auto maint_time = d.get_dynamic_global_properties().next_maintenance_time;
51 
52  ticket_version version = ( HARDFORK_CORE_2262_PASSED(maint_time) ? ticket_v2 : ticket_v1 );
53 
54  d.adjust_balance( op.account, -op.amount );
55 
56  const auto& new_ticket_object = d.create<ticket_object>([&op,block_time,version](ticket_object& obj){
57  obj.init_new( block_time, op.account, op.target_type, op.amount, version );
58  });
59 
60  // Note: amount.asset_id is checked in validate(), so no check here
61  d.modify( d.get_account_stats_by_owner( op.account ), [&op,&new_ticket_object](account_statistics_object& aso) {
62  aso.total_core_pol += op.amount.amount;
63  aso.total_pol_value += new_ticket_object.value;
64  });
65 
66  return new_ticket_object.id;
67 } FC_CAPTURE_AND_RETHROW( (op) ) }
68 
70 { try {
71  database& d = db();
72 
73  _ticket = &op.ticket(d);
74 
75  FC_ASSERT( _ticket->account == op.account, "Ticket is not owned by the account" );
76 
77  FC_ASSERT( _ticket->current_type != lock_forever, "Can not to update a ticket that is locked forever" );
78 
79  FC_ASSERT( static_cast<uint64_t>(_ticket->target_type) != op.target_type, "Target type does not change" );
80 
81  if( op.amount_for_new_target.valid() )
82  {
83  // Note: amount.asset_id is checked in validate(), so no check here
84  FC_ASSERT( *op.amount_for_new_target <= _ticket->amount, "Insufficient amount in ticket to be updated" );
85  }
86 
87  return void_result();
88 } FC_CAPTURE_AND_RETHROW( (op) ) }
89 
91 { try {
92  database& d = db();
93  const auto block_time = d.head_block_time();
94  const auto maint_time = d.get_dynamic_global_properties().next_maintenance_time;
95 
96  ticket_version version = ( HARDFORK_CORE_2262_PASSED(maint_time) ? ticket_v2 : ticket_v1 );
97 
99 
100  share_type old_value = _ticket->value;
101  share_type delta_value;
102 
103  // To partially update the ticket, aka splitting
104  if ( op.amount_for_new_target.valid() && *op.amount_for_new_target < _ticket->amount )
105  {
106  const auto& new_ticket_object = d.create<ticket_object>([&op,this,block_time,version](ticket_object& obj){
107  obj.init_split( block_time, *_ticket, op.target_type, *op.amount_for_new_target, version );
108  });
109 
110  result.new_objects.insert( new_ticket_object.id );
111 
112  d.modify( *_ticket, [&op,version](ticket_object& obj){
113  obj.adjust_amount( -(*op.amount_for_new_target), version );
114  });
115  delta_value = new_ticket_object.value + _ticket->value - old_value;
116  }
117  else // To update the whole ticket
118  {
119  d.modify( *_ticket, [&op,block_time,version](ticket_object& obj){
120  obj.update_target_type( block_time, op.target_type, version );
121  });
122  delta_value = _ticket->value - old_value;
123  }
124  result.updated_objects.insert( _ticket->id );
125 
126  if( delta_value != 0 )
127  {
128  const auto& stat = d.get_account_stats_by_owner( op.account );
129  d.modify( stat, [delta_value](account_statistics_object& aso) {
130  aso.total_pol_value += delta_value;
131  });
132  }
133 
134  // Do auto-update now.
135  // Note: calling process_tickets() here won't affect other tickets,
136  // since head_block_time is not updated after last call,
137  // even when called via a proposal this time or last time
138  generic_operation_result process_result = d.process_tickets();
139  result.removed_objects.insert( process_result.removed_objects.begin(), process_result.removed_objects.end() );
140  result.updated_objects.insert( process_result.updated_objects.begin(), process_result.updated_objects.end() );
141  for( const auto& id : result.new_objects )
142  result.updated_objects.erase( id );
143  for( const auto& id : result.removed_objects )
144  result.updated_objects.erase( id );
145 
146  return result;
147 } FC_CAPTURE_AND_RETHROW( (op) ) }
148 
149 } } // graphene::chain
FC_CAPTURE_AND_RETHROW
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:479
graphene::chain::ticket_update_evaluator::do_apply
generic_operation_result do_apply(const ticket_update_operation &op)
Definition: ticket_evaluator.cpp:90
graphene::db::object::id
object_id_type id
Definition: object.hpp:69
graphene::chain::database
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
graphene::chain::database::head_block_time
time_point_sec head_block_time() const
Definition: db_getter.cpp:67
graphene::protocol::ticket_create_operation
Creates a new ticket.
Definition: ticket.hpp:47
graphene::chain::account_statistics_object::total_pol_value
share_type total_pol_value
Total value of tickets whose current type is not lock_forever.
Definition: account_object.hpp:83
graphene::protocol::ticket_update_operation::ticket
ticket_id_type ticket
The ticket to update.
Definition: ticket.hpp:71
database.hpp
graphene::chain::dynamic_global_property_object::next_maintenance_time
time_point_sec next_maintenance_time
Definition: global_property_object.hpp:70
graphene::chain::ticket_object
a ticket for governance voting
Definition: ticket_object.hpp:62
graphene::protocol::ticket_update_operation::amount_for_new_target
optional< asset > amount_for_new_target
The amount to be used for the new target.
Definition: ticket.hpp:74
graphene::chain::ticket_version
ticket_version
Version of a ticket.
Definition: ticket_object.hpp:50
graphene::db::object_database::create
const T & create(F &&constructor)
Definition: object_database.hpp:63
graphene::chain::database::adjust_balance
void adjust_balance(account_id_type account, asset delta)
Adjust a particular account's balance in a given asset by a delta.
Definition: db_balance.cpp:54
graphene::chain::ticket_object::value
share_type value
The current value of the ticket.
Definition: ticket_object.hpp:71
graphene::protocol::ticket_update_operation::account
account_id_type account
The account who owns the ticket.
Definition: ticket.hpp:72
graphene::chain::account_statistics_object
Definition: account_object.hpp:46
ticket_evaluator.hpp
graphene::chain::ticket_v1
@ ticket_v1
Definition: ticket_object.hpp:52
graphene::protocol::ticket_update_operation
Updates an existing ticket.
Definition: ticket.hpp:66
graphene::chain::ticket_v2
@ ticket_v2
Definition: ticket_object.hpp:53
graphene::chain::ticket_object::adjust_amount
void adjust_amount(const asset &delta_amount, ticket_version version)
Adjust amount and update member variables accordingly.
Definition: ticket_object.cpp:95
graphene::chain::ticket_object::current_type
ticket_type current_type
The current type of the ticket.
Definition: ticket_object.hpp:69
graphene::chain::ticket_object::target_type
ticket_type target_type
The target type of the ticket.
Definition: ticket_object.hpp:66
graphene::protocol::generic_operation_result::updated_objects
flat_set< object_id_type > updated_objects
Definition: base.hpp:91
graphene::chain::ticket_update_evaluator::_ticket
const ticket_object * _ticket
Definition: ticket_evaluator.hpp:50
graphene::protocol::ticket_update_operation::target_type
unsigned_int target_type
New target ticket type, see ticket_type.
Definition: ticket.hpp:73
graphene::chain::generic_evaluator::db
database & db() const
Definition: evaluator.cpp:39
graphene::chain::ticket_object::account
account_id_type account
The account who owns the ticket.
Definition: ticket_object.hpp:65
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
graphene::chain::ticket_create_evaluator::do_evaluate
void_result do_evaluate(const ticket_create_operation &op)
Definition: ticket_evaluator.cpp:36
graphene::db::object_id_type
Definition: object_id.hpp:30
graphene::protocol::asset::amount
share_type amount
Definition: asset.hpp:36
graphene::protocol::ticket_create_operation::account
account_id_type account
The account who creates the ticket.
Definition: ticket.hpp:52
graphene::protocol::generic_operation_result::new_objects
flat_set< object_id_type > new_objects
Definition: base.hpp:90
graphene::protocol::ticket_create_operation::amount
asset amount
The amount of the ticket.
Definition: ticket.hpp:54
graphene::chain::ticket_object::update_target_type
void update_target_type(time_point_sec now, ticket_type new_target_type, ticket_version version)
Set a new target type and update member variables accordingly.
Definition: ticket_object.cpp:61
graphene::chain::ticket_update_evaluator::do_evaluate
void_result do_evaluate(const ticket_update_operation &op)
Definition: ticket_evaluator.cpp:69
graphene::chain::ticket_object::amount
asset amount
The token type and amount in the ticket.
Definition: ticket_object.hpp:67
graphene::chain::ticket_create_evaluator::do_apply
object_id_type do_apply(const ticket_create_operation &op)
Definition: ticket_evaluator.cpp:46
graphene::chain::database::get_account_stats_by_owner
const account_statistics_object & get_account_stats_by_owner(account_id_type owner) const
Definition: db_getter.cpp:142
graphene::chain::database::process_tickets
generic_operation_result process_tickets()
Definition: db_update.cpp:652
ticket.hpp
graphene::protocol::void_result
Definition: base.hpp:86
graphene::chain::database::get_dynamic_global_properties
const dynamic_global_property_object & get_dynamic_global_properties() const
Definition: db_getter.cpp:57
fc::safe::value
T value
Definition: safe.hpp:28
graphene::protocol::lock_forever
@ lock_forever
Definition: ticket.hpp:39
graphene::protocol::generic_operation_result
Definition: base.hpp:88
graphene
Definition: api.cpp:48
ticket_object.hpp
exceptions.hpp
graphene::protocol::generic_operation_result::removed_objects
flat_set< object_id_type > removed_objects
Definition: base.hpp:92
graphene::db::object_database::modify
void modify(const T &obj, const Lambda &m)
Definition: object_database.hpp:99
graphene::chain::account_statistics_object::total_core_pol
share_type total_core_pol
Total amount of core token in other tickets.
Definition: account_object.hpp:77
fc::safe
Definition: safe.hpp:26