BitShares-Core  4.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 
51  d.adjust_balance( op.account, -op.amount );
52 
53  const auto& new_ticket_object = d.create<ticket_object>([&op,block_time](ticket_object& obj){
54  obj.init_new( block_time, op.account, op.target_type, op.amount );
55  });
56 
57  // Note: amount.asset_id is checked in validate(), so no check here
58  d.modify( d.get_account_stats_by_owner( op.account ), [&op,&new_ticket_object](account_statistics_object& aso) {
59  aso.total_core_pol += op.amount.amount;
60  aso.total_pol_value += new_ticket_object.value;
61  });
62 
63  return new_ticket_object.id;
64 } FC_CAPTURE_AND_RETHROW( (op) ) }
65 
67 { try {
68  database& d = db();
69 
70  _ticket = &op.ticket(d);
71 
72  FC_ASSERT( _ticket->account == op.account, "Ticket is not owned by the account" );
73 
74  FC_ASSERT( _ticket->current_type != lock_forever, "Can not to update a ticket that is locked forever" );
75 
76  FC_ASSERT( static_cast<uint64_t>(_ticket->target_type) != op.target_type, "Target type does not change" );
77 
78  if( op.amount_for_new_target.valid() )
79  {
80  // Note: amount.asset_id is checked in validate(), so no check here
81  FC_ASSERT( *op.amount_for_new_target <= _ticket->amount, "Insufficient amount in ticket to be updated" );
82  }
83 
84  return void_result();
85 } FC_CAPTURE_AND_RETHROW( (op) ) }
86 
88 { try {
89  database& d = db();
90  const auto block_time = d.head_block_time();
91 
93 
94  share_type old_value = _ticket->value;
95  share_type delta_value;
96 
97  // To partially update the ticket, aka splitting
98  if ( op.amount_for_new_target.valid() && *op.amount_for_new_target < _ticket->amount )
99  {
100  const auto& new_ticket_object = d.create<ticket_object>([&op,this,block_time](ticket_object& obj){
101  obj.init_split( block_time, *_ticket, op.target_type, *op.amount_for_new_target );
102  });
103 
104  result.new_objects.insert( new_ticket_object.id );
105 
106  d.modify( *_ticket, [&op](ticket_object& obj){
107  obj.adjust_amount( -(*op.amount_for_new_target) );
108  });
109  delta_value = new_ticket_object.value + _ticket->value - old_value;
110  }
111  else // To update the whole ticket
112  {
113  d.modify( *_ticket, [&op,block_time](ticket_object& obj){
114  obj.update_target_type( block_time, op.target_type );
115  });
116  delta_value = _ticket->value - old_value;
117  }
118  result.updated_objects.insert( _ticket->id );
119 
120  if( delta_value != 0 )
121  {
122  const auto& stat = d.get_account_stats_by_owner( op.account );
123  d.modify( stat, [delta_value](account_statistics_object& aso) {
124  aso.total_pol_value += delta_value;
125  });
126  }
127 
128  // Do auto-update now.
129  // Note: calling process_tickets() here won't affect other tickets,
130  // since head_block_time is not updated after last call,
131  // even when called via a proposal this time or last time
132  generic_operation_result process_result = d.process_tickets();
133  result.removed_objects.insert( process_result.removed_objects.begin(), process_result.removed_objects.end() );
134  result.updated_objects.insert( process_result.updated_objects.begin(), process_result.updated_objects.end() );
135  for( const auto id : result.new_objects )
136  result.updated_objects.erase( id );
137  for( const auto id : result.removed_objects )
138  result.updated_objects.erase( id );
139 
140  return result;
141 } FC_CAPTURE_AND_RETHROW( (op) ) }
142 
143 } } // 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_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)
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
void update_target_type(time_point_sec now, ticket_type new_target_type)
Set a new target type and update member variables accordingly.
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
account_id_type account
The account who creates the ticket.
Definition: ticket.hpp:52
void adjust_amount(const asset &delta_amount)
Adjust amount and update member variables accordingly.
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