eosio.system.hpp 10.1 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
      int64_t              pervote_bucket = 0;
      int64_t              perblock_bucket = 0;
57
      uint32_t             total_unpaid_blocks = 0; /// all blocks which have been produced but not paid
K
Khaled Al-Hassanieh 已提交
58
      int64_t              total_activated_stake = 0;
59
      uint64_t             thresh_activated_stake_time = 0;
60
      checksum160          last_producer_schedule_id;
61
      uint16_t             last_producer_schedule_size = 0;
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)
67
                                (last_producer_schedule_update)(last_pervote_bucket_fill)
68
                                (pervote_bucket)(perblock_bucket)(total_unpaid_blocks)(total_activated_stake)(thresh_activated_stake_time)
69
                                (last_producer_schedule_id)(last_producer_schedule_size)(total_producer_vote_weight)(last_name_close) )
70 71 72
   };

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

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

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

D
Daniel Larimer 已提交
92 93 94 95
   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 已提交
96
      int64_t                     staked = 0;
D
Daniel Larimer 已提交
97 98 99 100 101 102 103

      /**
       *  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)
       */
104
      double                      last_vote_weight = 0; /// the vote weight cast the last time the vote was updated
D
Daniel Larimer 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

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


126
   typedef eosio::multi_index< N(producers), producer_info,
D
Daniel Larimer 已提交
127
                               indexed_by<N(prototalvote), const_mem_fun<producer_info, double, &producer_info::by_votes>  >
128 129
                               >  producers_table;

130
   typedef eosio::singleton<N(global), eosio_global_state> global_state_singleton;
131

132
   //   static constexpr uint32_t     max_inflation_rate = 5;  // 5% annual inflation
133
   static constexpr uint32_t     seconds_per_day = 24 * 3600;
134
   static constexpr uint64_t     system_token_symbol = CORE_SYMBOL;
135

D
Daniel Larimer 已提交
136
   class system_contract : public native {
D
Daniel Larimer 已提交
137 138 139 140 141 142
      private:
         voters_table           _voters;
         producers_table        _producers;
         global_state_singleton _global;

         eosio_global_state     _gstate;
D
Daniel Larimer 已提交
143
         rammarket              _rammarket;
D
Daniel Larimer 已提交
144

145
      public:
D
Daniel Larimer 已提交
146
         system_contract( account_name s );
147
         ~system_contract();
148

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

153
         // functions defined in delegate_bandwidth.cpp
154 155 156 157 158 159

         /**
          *  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 已提交
160
         void delegatebw( account_name from, account_name receiver,
161
                          asset stake_net_quantity, asset stake_cpu_quantity, bool transfer );
D
Daniel Larimer 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189


         /**
          *  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 );
190
         void buyrambytes( account_name buyer, account_name receiver, uint32_t bytes );
D
Daniel Larimer 已提交
191 192 193 194 195

         /**
          *  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 已提交
196
         void sellram( account_name receiver, int64_t bytes );
D
Daniel Larimer 已提交
197 198 199 200 201 202

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

204
         // functions defined in voting.cpp
205

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

208
         void unregprod( const account_name producer );
209

210
         void setram( uint64_t max_ram_size );
211

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

214
         void regproxy( const account_name proxy, bool isproxy );
215

216 217
         void setparams( const eosio_parameters& params );

218 219
         // functions defined in producer_pay.cpp
         void claimrewards( const account_name& owner );
220

221 222
         void setpriv( account_name account, uint8_t ispriv );

D
Daniel Larimer 已提交
223
         void bidname( account_name bidder, account_name newname, asset bid );
224
      private:
225
         void update_elected_producers( block_timestamp timestamp );
D
Daniel Larimer 已提交
226

227
         // Implementation details:
228

229 230 231 232
         //defind in delegate_bandwidth.cpp
         void changebw( account_name from, account_name receiver,
                        asset stake_net_quantity, asset stake_cpu_quantity, bool transfer );

233 234 235
         //defined in voting.hpp
         static eosio_global_state get_default_parameters();

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

238
         // defined in voting.cpp
239
         void propagate_weight_change( const voter_info& voter );
240
   };
241

242
} /// eosiosystem