eosio.system.hpp 6.5 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/producer_schedule.hpp>
9 10
#include <eosiolib/asset.hpp>
#include <eosiolib/contract.hpp>
11
#include <eosiolib/optional.hpp>
12 13
#include <eosiolib/privileged.hpp>
#include <eosiolib/singleton.hpp>
14

15
#include <string>
16

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

19 20 21 22 23
   using eosio::asset;
   using eosio::indexed_by;
   using eosio::const_mem_fun;

   struct eosio_parameters : eosio::blockchain_parameters {
24
      uint64_t          max_storage_size = 1024 * 1024 * 1024;
25
      uint32_t          percent_of_max_inflation_rate = 0;
26
      uint32_t          storage_reserve_ratio = 2000;      // ratio * 1000
27

A
Anton Perkov 已提交
28
      // explicit serialization macro is not necessary, used here only to improve compilation time
29 30 31 32 33 34 35 36 37 38 39 40 41
      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 {
      uint64_t             total_storage_bytes_reserved = 0;
      eosio::asset         total_storage_stake;
      eosio::asset         payment_per_block;
      eosio::asset         payment_to_eos_bucket;
      time                 first_block_time_in_cycle = 0;
      uint32_t             blocks_per_cycle = 0;
      time                 last_bucket_fill_time = 0;
      eosio::asset         eos_bucket;

A
Anton Perkov 已提交
42
      // explicit serialization macro is not necessary, used here only to improve compilation time
43 44 45 46 47 48
      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 已提交
49 50 51 52 53 54 55 56 57 58 59
      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(); }
60

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

   typedef eosio::multi_index< N(producerinfo), producer_info,
D
Daniel Larimer 已提交
68
                               indexed_by<N(prototalvote), const_mem_fun<producer_info, double, &producer_info::by_votes>  >
69 70
                               >  producers_table;

71
   typedef eosio::singleton<N(global), eosio_global_state> global_state_singleton;
72 73 74

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

D
Daniel Larimer 已提交
77
   class system_contract : public native {
78
      public:
79
         using eosio::contract::contract;
80

81
         // Actions:
82

83
         // functions defined in delegate_bandwidth.cpp
D
Daniel Larimer 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
         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.
          */
         void sellram( account_name receiver, uint32_t bytes );

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

127
         // functions defined in voting.cpp
128

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

131
         void unregprod( const account_name producer );
132

133 134
         void setparams( uint64_t max_storage_size, uint32_t storage_reserve_ratio );

135

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

138
         void regproxy( const account_name proxy );
139

140
         void unregproxy( const account_name proxy );
141

142
         void nonce( const std::string& /*value*/ ) {}
143

144
         // functions defined in producer_pay.cpp
145

146
         void onblock( const block_header& header );
147

148
         void claimrewards( const account_name& owner );
149

150
      private:
D
Daniel Larimer 已提交
151 152 153 154
         eosio::asset payment_per_block(uint32_t percent_of_max_inflation_rate);

         void update_elected_producers(time cycle_time);

155
         // Implementation details:
156

157 158 159 160 161 162 163 164 165 166
         //defined in voting.hpp
         static eosio_global_state get_default_parameters();

         // defined in voting.cpp
         void increase_voting_power( account_name acnt, const eosio::asset& amount );

         void decrease_voting_power( account_name acnt, const eosio::asset& amount );

         // defined in producer_pay.cpp
         bool update_cycle( time block_time );
167

168
   };
169

170
} /// eosiosystem