eosio.system.hpp 10.3 KB
Newer Older
1 2 3 4
/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */
5
#pragma once
6

D
Daniel Larimer 已提交
7
#include <eosio.system/native.hpp>
8
#include <eosiolib/asset.hpp>
K
Khaled Al-Hassanieh 已提交
9
#include <eosiolib/time.hpp>
10 11
#include <eosiolib/privileged.hpp>
#include <eosiolib/singleton.hpp>
D
Daniel Larimer 已提交
12
#include <eosio.system/exchange_state.hpp>
13

14
#include <string>
15

16
namespace eosiosystem {
K
Khaled Al-Hassanieh 已提交
17

18 19 20
   using eosio::asset;
   using eosio::indexed_by;
   using eosio::const_mem_fun;
K
Khaled Al-Hassanieh 已提交
21
   using eosio::block_timestamp;
22 23

   struct eosio_parameters : eosio::blockchain_parameters {
24
      uint64_t          max_ram_size = 64ll*1024 * 1024 * 1024;
25

A
Anton Perkov 已提交
26
      // explicit serialization macro is not necessary, used here only to improve compilation time
K
Khaled Al-Hassanieh 已提交
27
      EOSLIB_SERIALIZE_DERIVED( eosio_parameters, eosio::blockchain_parameters, (max_ram_size) )
28 29
   };

D
Daniel Larimer 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
   struct name_bid {
     account_name            newname;
     account_name            high_bidder;
     int64_t                 high_bid = 0; ///< negative high_bid == closed auction waiting to be claimed
     uint64_t                last_bid_time = 0;

     auto     primary_key()const { return newname;   }
     uint64_t by_high_bid()const { return -high_bid; }
   };

   typedef eosio::multi_index< N(namebids), name_bid,
                               indexed_by<N(highbid), const_mem_fun<name_bid, uint64_t, &name_bid::by_high_bid>  >
                               >  name_bid_table;




47
   struct eosio_global_state : eosio_parameters {
48
      uint64_t free_ram()const { return max_ram_size - total_ram_bytes_reserved; }
D
Daniel Larimer 已提交
49

50
      uint64_t             total_ram_bytes_reserved = 0;
51
      int64_t              total_ram_stake = 0;
D
Daniel Larimer 已提交
52

K
Khaled Al-Hassanieh 已提交
53
      block_timestamp      last_producer_schedule_update;
54
      uint64_t             last_pervote_bucket_fill = 0;
55 56 57
      int64_t              pervote_bucket = 0;
      int64_t              perblock_bucket = 0;
      int64_t              savings = 0;
58
      uint32_t             total_unpaid_blocks = 0; /// all blocks which have been produced but not paid
K
Khaled Al-Hassanieh 已提交
59
      int64_t              total_activated_stake = 0;
60
      uint64_t             thresh_activated_stake_time = 0;
61
      checksum160          last_producer_schedule_id;
D
Daniel Larimer 已提交
62
      double               total_producer_vote_weight = 0; /// the sum of all producer votes
D
Daniel Larimer 已提交
63
      block_timestamp      last_name_close;
64

A
Anton Perkov 已提交
65
      // explicit serialization macro is not necessary, used here only to improve compilation time
66
      EOSLIB_SERIALIZE_DERIVED( eosio_global_state, eosio_parameters, (total_ram_bytes_reserved)(total_ram_stake)
D
Daniel Larimer 已提交
67 68
                                (last_producer_schedule_update)
                                (last_pervote_bucket_fill)
69 70
                                (pervote_bucket)(perblock_bucket)(savings)(total_unpaid_blocks)(total_activated_stake)(thresh_activated_stake_time)
                                (last_producer_schedule_id)(total_producer_vote_weight)(last_name_close) )
71 72 73
   };

   struct producer_info {
D
Daniel Larimer 已提交
74 75 76
      account_name          owner;
      double                total_votes = 0;
      eosio::public_key     producer_key; /// a packed public key object
77
      bool                  is_active = true;
A
Anton Perkov 已提交
78
      std::string           url;
79
      uint32_t              unpaid_blocks = 0;
80
      uint64_t              last_claim_time = 0;
D
Daniel Larimer 已提交
81
      uint16_t              location = 0;
K
Khaled Al-Hassanieh 已提交
82 83
      block_timestamp       time_became_active;
      block_timestamp       last_produced_block_time;
D
Daniel Larimer 已提交
84

85
      uint64_t primary_key()const { return owner;                                   }
K
Khaled Al-Hassanieh 已提交
86
      double   by_votes()const    { return is_active ? -total_votes : total_votes;  }
87 88
      bool     active()const      { return is_active;                               }
      void     deactivate()       { producer_key = public_key(); is_active = false; }
89

A
Anton Perkov 已提交
90
      // explicit serialization macro is not necessary, used here only to improve compilation time
91
      EOSLIB_SERIALIZE( producer_info, (owner)(total_votes)(producer_key)(is_active)(url)
92
                        (unpaid_blocks)(last_claim_time)(location)
93 94 95
                        (time_became_active)(last_produced_block_time) )
   };

D
Daniel Larimer 已提交
96 97 98 99
   struct voter_info {
      account_name                owner = 0; /// the voter
      account_name                proxy = 0; /// the proxy set by the voter, if any
      std::vector<account_name>   producers; /// the producers approved by this voter if no proxy set
D
Daniel Larimer 已提交
100
      int64_t                     staked = 0;
D
Daniel Larimer 已提交
101 102 103 104 105 106 107

      /**
       *  Every time a vote is cast we must first "undo" the last vote weight, before casting the
       *  new vote weight.  Vote weight is calculated as:
       *
       *  stated.amount * 2 ^ ( weeks_since_launch/weeks_per_year)
       */
108
      double                      last_vote_weight = 0; /// the vote weight cast the last time the vote was updated
D
Daniel Larimer 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

      /**
       * Total vote weight delegated to this voter.
       */
      double                      proxied_vote_weight= 0; /// the total vote weight delegated to this voter as a proxy
      bool                        is_proxy = 0; /// whether the voter is a proxy for others


      uint32_t                    deferred_trx_id = 0; /// the ID of the 3-day delay deferred transaction
      time                        last_unstake_time = 0; /// the time when the deferred_trx_id was sent
      eosio::asset                unstaking; /// the total unstaking (pending 3 day delay)

      uint64_t primary_key()const { return owner; }

      // explicit serialization macro is not necessary, used here only to improve compilation time
      EOSLIB_SERIALIZE( voter_info, (owner)(proxy)(producers)(staked)(last_vote_weight)(proxied_vote_weight)(is_proxy)(deferred_trx_id)(last_unstake_time)(unstaking) )
   };

   typedef eosio::multi_index< N(voters), voter_info>  voters_table;


130
   typedef eosio::multi_index< N(producers), producer_info,
D
Daniel Larimer 已提交
131
                               indexed_by<N(prototalvote), const_mem_fun<producer_info, double, &producer_info::by_votes>  >
132 133
                               >  producers_table;

134
   typedef eosio::singleton<N(global), eosio_global_state> global_state_singleton;
135

136
   //   static constexpr uint32_t     max_inflation_rate = 5;  // 5% annual inflation
137
   static constexpr uint32_t     seconds_per_day = 24 * 3600;
138
   static constexpr uint64_t     system_token_symbol = CORE_SYMBOL;
139

D
Daniel Larimer 已提交
140
   class system_contract : public native {
D
Daniel Larimer 已提交
141 142 143 144 145 146
      private:
         voters_table           _voters;
         producers_table        _producers;
         global_state_singleton _global;

         eosio_global_state     _gstate;
D
Daniel Larimer 已提交
147
         rammarket              _rammarket;
D
Daniel Larimer 已提交
148

149
      public:
D
Daniel Larimer 已提交
150
         system_contract( account_name s );
151
         ~system_contract();
152

153
         // Actions:
K
Khaled Al-Hassanieh 已提交
154
         void onblock( block_timestamp timestamp, account_name producer );
D
Daniel Larimer 已提交
155
                      // const block_header& header ); /// only parse first 3 fields of block header
156

157
         // functions defined in delegate_bandwidth.cpp
158 159 160 161 162 163

         /**
          *  Stakes SYS from the balance of 'from' for the benfit of 'receiver'. 
          *  If transfer == true, then 'receiver' can unstake to their account
          *  Else 'from' can unstake at any time.
          */
D
Daniel Larimer 已提交
164
         void delegatebw( account_name from, account_name receiver,
165
                          asset stake_net_quantity, asset stake_cpu_quantity, bool transfer );
D
Daniel Larimer 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193


         /**
          *  Decreases the total tokens delegated by from to receiver and/or
          *  frees the memory associated with the delegation if there is nothing
          *  left to delegate.
          *
          *  This will cause an immediate reduction in net/cpu bandwidth of the
          *  receiver. 
          *
          *  A transaction is scheduled to send the tokens back to 'from' after
          *  the staking period has passed. If existing transaction is scheduled, it
          *  will be canceled and a new transaction issued that has the combined
          *  undelegated amount.
          *
          *  The 'from' account loses voting power as a result of this call and
          *  all producer tallies are updated.
          */
         void undelegatebw( account_name from, account_name receiver,
                            asset unstake_net_quantity, asset unstake_cpu_quantity );


         /**
          * Increases receiver's ram quota based upon current price and quantity of
          * tokens provided. An inline transfer from receiver to system contract of
          * tokens will be executed.
          */
         void buyram( account_name buyer, account_name receiver, asset tokens );
194
         void buyrambytes( account_name buyer, account_name receiver, uint32_t bytes );
D
Daniel Larimer 已提交
195 196 197 198 199

         /**
          *  Reduces quota my bytes and then performs an inline transfer of tokens
          *  to receiver based upon the average purchase price of the original quota.
          */
D
Daniel Larimer 已提交
200
         void sellram( account_name receiver, int64_t bytes );
D
Daniel Larimer 已提交
201 202 203 204 205 206

         /**
          *  This action is called after the delegation-period to claim all pending
          *  unstaked tokens belonging to owner
          */
         void refund( account_name owner );
207

208
         // functions defined in voting.cpp
209

210
         void regproducer( const account_name producer, const public_key& producer_key, const std::string& url, uint16_t location );
211

212
         void unregprod( const account_name producer );
213

214
         void setram( uint64_t max_ram_size );
215

216
         void voteproducer( const account_name voter, const account_name proxy, const std::vector<account_name>& producers );
217

218
         void regproxy( const account_name proxy, bool isproxy );
219

220 221
         void setparams( const eosio_parameters& params );

222 223
         // functions defined in producer_pay.cpp
         void claimrewards( const account_name& owner );
224

225 226
         void setpriv( account_name account, uint8_t ispriv );

D
Daniel Larimer 已提交
227
         void bidname( account_name bidder, account_name newname, asset bid );
228
      private:
229
         void update_elected_producers( block_timestamp timestamp );
D
Daniel Larimer 已提交
230

231
         // Implementation details:
232

233 234 235 236
         //defind in delegate_bandwidth.cpp
         void changebw( account_name from, account_name receiver,
                        asset stake_net_quantity, asset stake_cpu_quantity, bool transfer );

237 238 239
         //defined in voting.hpp
         static eosio_global_state get_default_parameters();

240 241
         void update_votes( const account_name voter, const account_name proxy, const std::vector<account_name>& producers, bool voting );

242
         // defined in voting.cpp
243
         void propagate_weight_change( const voter_info& voter );
244
   };
245

246
} /// eosiosystem