eosio.system.hpp 8.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 9 10
#include <eosiolib/asset.hpp>
#include <eosiolib/privileged.hpp>
#include <eosiolib/singleton.hpp>
11

12
#include <string>
13

14
namespace eosiosystem {
K
Khaled Al-Hassanieh 已提交
15

16 17 18 19 20
   using eosio::asset;
   using eosio::indexed_by;
   using eosio::const_mem_fun;

   struct eosio_parameters : eosio::blockchain_parameters {
21
      uint64_t          max_storage_size = 64ll*1024 * 1024 * 1024;
22
      uint32_t          percent_of_max_inflation_rate = 0;
23
      uint32_t          storage_reserve_ratio = 100;      // ratio * 10000
24

A
Anton Perkov 已提交
25
      // explicit serialization macro is not necessary, used here only to improve compilation time
26 27 28 29
      EOSLIB_SERIALIZE_DERIVED( eosio_parameters, eosio::blockchain_parameters, (max_storage_size)(percent_of_max_inflation_rate)(storage_reserve_ratio) )
   };

   struct eosio_global_state : eosio_parameters {
D
Daniel Larimer 已提交
30 31
      uint64_t free_ram()const { return max_storage_size - total_storage_bytes_reserved; }

32 33 34 35
      uint64_t             total_storage_bytes_reserved = 0;
      eosio::asset         total_storage_stake;
      eosio::asset         payment_per_block;
      eosio::asset         payment_to_eos_bucket;
D
Daniel Larimer 已提交
36

37 38 39
      time                 first_block_time_in_cycle = 0;
      uint32_t             blocks_per_cycle = 0;
      time                 last_bucket_fill_time = 0;
D
Daniel Larimer 已提交
40

41 42
      eosio::asset         eos_bucket;

A
Anton Perkov 已提交
43
      // explicit serialization macro is not necessary, used here only to improve compilation time
44 45 46 47 48 49
      EOSLIB_SERIALIZE_DERIVED( eosio_global_state, eosio_parameters, (total_storage_bytes_reserved)(total_storage_stake)
                                (payment_per_block)(payment_to_eos_bucket)(first_block_time_in_cycle)(blocks_per_cycle)
                                (last_bucket_fill_time)(eos_bucket) )
   };

   struct producer_info {
D
Daniel Larimer 已提交
50 51 52 53 54 55 56 57 58 59 60
      account_name          owner;
      double                total_votes = 0;
      eosio::public_key     producer_key; /// a packed public key object
      eosio::asset          per_block_payments;
      time                  last_rewards_claim = 0;
      time                  time_became_active = 0;
      time                  last_produced_block_time = 0;

      uint64_t    primary_key()const { return owner;                        }
      double      by_votes()const    { return -total_votes;                 }
      bool        active() const     { return producer_key != public_key(); }
61

A
Anton Perkov 已提交
62
      // explicit serialization macro is not necessary, used here only to improve compilation time
63
      EOSLIB_SERIALIZE( producer_info, (owner)(total_votes)(producer_key)
64 65 66 67
                        (per_block_payments)(last_rewards_claim)
                        (time_became_active)(last_produced_block_time) )
   };

D
Daniel Larimer 已提交
68 69 70 71
   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
72
      uint64_t                    staked = 0;
D
Daniel Larimer 已提交
73 74 75 76 77 78 79

      /**
       *  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)
       */
80
      double                      last_vote_weight = 0; /// the vote weight cast the last time the vote was updated
D
Daniel Larimer 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

      /**
       * 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;


102
   typedef eosio::multi_index< N(producers), producer_info,
D
Daniel Larimer 已提交
103
                               indexed_by<N(prototalvote), const_mem_fun<producer_info, double, &producer_info::by_votes>  >
104 105
                               >  producers_table;

106
   typedef eosio::singleton<N(global), eosio_global_state> global_state_singleton;
107 108 109

   static constexpr uint32_t     max_inflation_rate = 5;  // 5% annual inflation
   static constexpr uint32_t     seconds_per_day = 24 * 3600;
110 111
   static constexpr uint64_t     system_token_symbol = S(4,EOS);

D
Daniel Larimer 已提交
112
   class system_contract : public native {
D
Daniel Larimer 已提交
113 114 115 116 117 118 119
      private:
         voters_table           _voters;
         producers_table        _producers;
         global_state_singleton _global;

         eosio_global_state     _gstate;

120
      public:
D
Daniel Larimer 已提交
121
         system_contract( account_name s );
122
         [[noreturn]] ~system_contract();
123

124
         // Actions:
125
         void onblock( uint32_t timestamp_slot, account_name producer );
D
Daniel Larimer 已提交
126
                      // const block_header& header ); /// only parse first 3 fields of block header
127

128
         // functions defined in delegate_bandwidth.cpp
D
Daniel Larimer 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
         void delegatebw( account_name from, account_name receiver,
                          asset stake_net_quantity, asset stake_cpu_quantity );


         /**
          *  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 );

         /**
          *  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 已提交
164
         void sellram( account_name receiver, uint64_t bytes );
D
Daniel Larimer 已提交
165 166 167 168 169 170

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

172
         // functions defined in voting.cpp
173

174
         void regproducer( const account_name producer, const public_key& producer_key, const std::string& url );
175

176
         void unregprod( const account_name producer );
177

178 179
         void setparams( uint64_t max_storage_size, uint32_t storage_reserve_ratio );

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

182
         void regproxy( const account_name proxy, bool isproxy );
183

184
         void nonce( const std::string& /*value*/ ) {}
185

186 187
         // functions defined in producer_pay.cpp
         void claimrewards( const account_name& owner );
188

189
      private:
D
Daniel Larimer 已提交
190 191 192 193
         eosio::asset payment_per_block(uint32_t percent_of_max_inflation_rate);

         void update_elected_producers(time cycle_time);

194
         // Implementation details:
195

196 197 198 199
         //defined in voting.hpp
         static eosio_global_state get_default_parameters();

         // defined in voting.cpp
D
Daniel Larimer 已提交
200
         void adjust_voting_power( account_name acnt, int64_t delta );
201
   };
202

203
} /// eosiosystem