BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface 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
flat_set< object_id_type > new_objects
Definition: base.hpp:90
share_type total_core_pol
Total amount of core token in other tickets.
void modify(const T &obj, const Lambda &m)
void adjust_amount(const asset &delta_amount, ticket_version version)
Adjust amount and update member variables accordingly.
void adjust_balance(account_id_type account, asset delta)
Adjust a particular account&#39;s balance in a given asset by a delta.
Definition: db_balance.cpp:54
asset amount
The amount of the ticket.
Definition: ticket.hpp:54
object_id_type do_apply(const ticket_create_operation &op)
ticket_type target_type
The target type of the ticket.
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
Definition: api.cpp:56
ticket_id_type ticket
The ticket to update.
Definition: ticket.hpp:71
void_result do_evaluate(const ticket_update_operation &op)
const dynamic_global_property_object & get_dynamic_global_properties() const
Definition: db_getter.cpp:54
share_type total_pol_value
Total value of tickets whose current type is not lock_forever.
Updates an existing ticket.
Definition: ticket.hpp:66
void_result do_evaluate(const ticket_create_operation &op)
asset amount
The token type and amount in the ticket.
generic_operation_result do_apply(const ticket_update_operation &op)
time_point_sec head_block_time() const
Definition: db_getter.cpp:64
account_id_type account
The account who owns the ticket.
Definition: ticket.hpp:72
optional< asset > amount_for_new_target
The amount to be used for the new target.
Definition: ticket.hpp:74
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:478
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
const account_statistics_object & get_account_stats_by_owner(account_id_type owner) const
Definition: db_getter.cpp:139
unsigned_int target_type
New target ticket type, see ticket_type.
Definition: ticket.hpp:73
flat_set< object_id_type > removed_objects
Definition: base.hpp:92
generic_operation_result process_tickets()
Definition: db_update.cpp:605
flat_set< object_id_type > updated_objects
Definition: base.hpp:91
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.
account_id_type account
The account who creates the ticket.
Definition: ticket.hpp:52
ticket_version
Version of a ticket.
account_id_type account
The account who owns the ticket.
a ticket for governance voting
const T & create(F &&constructor)
T value
Definition: safe.hpp:22