BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
ticket_object.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  */
24 
26 
27 #include <fc/io/raw.hpp>
28 
29 using namespace graphene::chain;
30 
31 void ticket_object::init_new( time_point_sec now, account_id_type new_account,
32  ticket_type new_target_type, const asset& new_amount, ticket_version version )
33 {
34  account = new_account;
35  target_type = new_target_type;
36  amount = new_amount;
37 
39  status = charging;
42 
43  update_value( version );
44 }
45 
46 void ticket_object::init_split( time_point_sec now, const ticket_object& old_ticket,
47  ticket_type new_target_type, const asset& new_amount, ticket_version version )
48 {
49  account = old_ticket.account;
50  target_type = old_ticket.target_type;
51  amount = new_amount;
52 
53  current_type = old_ticket.current_type;
54  status = old_ticket.status;
57 
58  update_target_type( now, new_target_type, version );
59 }
60 
61 void ticket_object::update_target_type( time_point_sec now, ticket_type new_target_type, ticket_version version )
62 {
63  if( current_type < new_target_type )
64  {
65  if( status != charging )
66  {
67  status = charging;
69  }
70  // else do nothing here
71  }
72  else // current_type >= new_target_type
73  {
75  if( next_type_downgrade_time == time_point_sec::maximum() ) // no downgrade was started ago
76  {
77  if( current_type == new_target_type ) // was charging, to cancel
79  else // was stable or charging, to downgrade
80  {
81  current_type = static_cast<ticket_type>( static_cast<uint8_t>(current_type) - 1 );
83  }
84  }
85  // else a downgrade was started ago, keep the old value of `next_type_downgrade_time`
86  // Note: it's possible that `next_type_downgrade_time` is in the past,
87  // in this case, it will be processed in database::process_tickets().
89  }
90  target_type = new_target_type;
91 
92  update_value( version );
93 }
94 
95 void ticket_object::adjust_amount( const asset& delta_amount, ticket_version version )
96 {
97  amount += delta_amount;
98  update_value( version );
99 }
100 
102 {
103  if( status == charging )
104  {
105  current_type = static_cast<ticket_type>( static_cast<uint8_t>(current_type) + 1 );
107  if( current_type < target_type )
108  {
110  }
111  else // reached target, stop
112  {
113  if( current_type != lock_forever )
114  {
115  status = stable;
117  }
118  else // lock forever
119  {
123  }
124  }
125  }
126  else // status == withdrawing
127  {
128  // Note: current_type != liquid, guaranteed by the caller
129  if( current_type == lock_forever )
130  {
131  share_type delta_value = amount.amount * value_multiplier( current_type, version )
133  if( delta_value <= 0 )
134  delta_value = 1;
135  if( value <= delta_value )
136  {
137  value = 0;
138  status = stable;
140  }
141  else
142  {
143  value -= delta_value;
145  }
146  }
147  else if( current_type == target_type ) // finished downgrading
148  {
149  status = stable;
152  }
153  else // to downgrade
154  {
155  current_type = static_cast<ticket_type>( static_cast<uint8_t>(current_type) - 1 );
158  }
159  }
160 
161  update_value( version );
162 }
163 
164 void ticket_object::update_value( ticket_version version )
165 {
166  if( current_type != lock_forever )
167  {
169  }
170  // else lock forever and to be updated, do nothing here
171 }
172 
174  (account)
175  (target_type)
176  (amount)
177  (current_type)
178  (status)
179  (value)
180  (next_auto_update_time)
181  (next_type_downgrade_time)
182  )
183 
graphene::chain::ticket_object::lock_forever_update_steps
static constexpr uint32_t lock_forever_update_steps
Definition: ticket_object.hpp:78
graphene::chain::ticket_object::status
ticket_status status
The status of the ticket.
Definition: ticket_object.hpp:70
graphene::protocol::liquid
@ liquid
Definition: ticket.hpp:35
graphene::chain::ticket_object
a ticket for governance voting
Definition: ticket_object.hpp:62
graphene::chain::ticket_version
ticket_version
Version of a ticket.
Definition: ticket_object.hpp:50
graphene::protocol::ticket_type
ticket_type
Type of a ticket.
Definition: ticket.hpp:33
graphene::chain::withdrawing
@ withdrawing
Definition: ticket_object.hpp:45
fc::time_point_sec::maximum
static time_point_sec maximum()
Definition: time.hpp:86
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
#define GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:86
graphene::chain::ticket_object::value
share_type value
The current value of the ticket.
Definition: ticket_object.hpp:71
graphene::chain::ticket_object::auto_update
void auto_update(ticket_version version)
Update the ticket when it's time.
Definition: ticket_object.cpp:101
graphene::chain::ticket_object::next_type_downgrade_time
time_point_sec next_type_downgrade_time
When the account has ever started a downgrade or withdrawal, the scheduled auto-update time is stored...
Definition: ticket_object.hpp:75
graphene::chain::ticket_object::seconds_to_cancel_charging
static constexpr uint32_t seconds_to_cancel_charging
Definition: ticket_object.hpp:81
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::seconds_per_charging_step
static constexpr uint32_t seconds_per_charging_step
Definition: ticket_object.hpp:80
FC_REFLECT_DERIVED_NO_TYPENAME
#define FC_REFLECT_DERIVED_NO_TYPENAME(TYPE, INHERITS, MEMBERS)
Definition: reflect.hpp:357
graphene::chain::charging
@ charging
Definition: ticket_object.hpp:43
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::chain::ticket_object::account
account_id_type account
The account who owns the ticket.
Definition: ticket_object.hpp:65
graphene::protocol::asset::amount
share_type amount
Definition: asset.hpp:36
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_object::value_multiplier
static uint8_t value_multiplier(ticket_type i, ticket_version version)
Definition: ticket_object.hpp:86
graphene::chain::ticket_object::init_new
void init_new(time_point_sec now, account_id_type new_account, ticket_type new_target_type, const asset &new_amount, ticket_version version)
Initialize member variables for a ticket newly created from account balance.
Definition: ticket_object.cpp:31
graphene::chain::ticket_object::init_split
void init_split(time_point_sec now, const ticket_object &old_ticket, ticket_type new_target_type, const asset &new_amount, ticket_version version)
Initialize member variables for a ticket split from another ticket.
Definition: ticket_object.cpp:46
graphene::chain::stable
@ stable
Definition: ticket_object.hpp:44
graphene::chain::ticket_object::amount
asset amount
The token type and amount in the ticket.
Definition: ticket_object.hpp:67
graphene::chain::ticket_object::seconds_to_downgrade
static uint32_t seconds_to_downgrade(ticket_type i)
Definition: ticket_object.hpp:82
graphene::chain::ticket_object::next_auto_update_time
time_point_sec next_auto_update_time
The next time that the ticket will be automatically updated.
Definition: ticket_object.hpp:72
graphene::protocol::asset
Definition: asset.hpp:31
graphene::chain::ticket_object::seconds_per_lock_forever_update_step
static constexpr uint32_t seconds_per_lock_forever_update_step
Definition: ticket_object.hpp:79
graphene::protocol::lock_forever
@ lock_forever
Definition: ticket.hpp:39
graphene::chain
Definition: util.hpp:32
ticket_object.hpp
graphene::db::object
base for all database objects
Definition: object.hpp:61
raw.hpp
fc::safe
Definition: safe.hpp:26