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

7
#include "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
   using eosio::asset;
   using eosio::indexed_by;
   using eosio::const_mem_fun;

A
arhag 已提交
23
   struct block_header {
K
Khaled Al-Hassanieh 已提交
24 25 26 27 28 29 30 31
      checksum256                               previous;
      time                                      timestamp;
      checksum256                               transaction_mroot;
      checksum256                               action_mroot;
      checksum256                               block_mroot;
      account_name                              producer;
      uint32_t                                  schedule_version;
      eosio::optional<eosio::producer_schedule> new_producers;
A
arhag 已提交
32

A
Anton Perkov 已提交
33
      // explicit serialization macro is not necessary, used here only to improve compilation time
K
Khaled Al-Hassanieh 已提交
34 35
      EOSLIB_SERIALIZE(block_header, (previous)(timestamp)(transaction_mroot)(action_mroot)(block_mroot)
                                     (producer)(schedule_version)(new_producers))
K
Khaled Al-Hassanieh 已提交
36 37
   };

38 39 40 41 42
   struct eosio_parameters : eosio::blockchain_parameters {
      uint64_t          max_storage_size = 10 * 1024 * 1024;
      uint32_t          percent_of_max_inflation_rate = 0;
      uint32_t          storage_reserve_ratio = 1000;      // ratio * 1000

A
Anton Perkov 已提交
43
      // explicit serialization macro is not necessary, used here only to improve compilation time
44 45 46 47 48 49 50 51 52 53 54 55 56
      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 已提交
57
      // explicit serialization macro is not necessary, used here only to improve compilation time
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
      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 {
      account_name      owner;
      uint128_t         total_votes = 0;
      eosio_parameters  prefs;
      eosio::bytes      packed_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;       }
      uint128_t   by_votes()const    { return total_votes; }
75
      bool active() const { return 0 < packed_key.size(); }
76

A
Anton Perkov 已提交
77
      // explicit serialization macro is not necessary, used here only to improve compilation time
78 79 80 81 82 83 84 85 86
      EOSLIB_SERIALIZE( producer_info, (owner)(total_votes)(prefs)(packed_key)
                        (per_block_payments)(last_rewards_claim)
                        (time_became_active)(last_produced_block_time) )
   };

   typedef eosio::multi_index< N(producerinfo), producer_info,
                               indexed_by<N(prototalvote), const_mem_fun<producer_info, uint128_t, &producer_info::by_votes>  >
                               >  producers_table;

87
   typedef eosio::singleton<N(global), eosio_global_state> global_state_singleton;
88 89 90

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

93
   class system_contract : public native, private eosio::contract {
94
      public:
95

96
         using eosio::contract::contract;
97

98
         // Actions:
99

100 101 102 103
         // functions defined in delegate_bandwidth.cpp
         void delegatebw( const account_name from, const account_name receiver,
                          const asset stake_net_quantity, const asset stake_cpu_quantity,
                          const asset stake_storage_quantity );
104

105 106 107
         void undelegatebw( const account_name from, const account_name receiver,
                            const asset unstake_net_quantity, const asset unstake_cpu_quantity,
                            const uint64_t unstake_storage_bytes );
108

109
         void refund( const account_name owner );
110

111
         // functions defined in voting.cpp
112

113
         void regproducer( const account_name producer, const bytes& producer_key, const eosio_parameters& prefs );
114

115
         void unregprod( const account_name producer );
116

117
         eosio::asset payment_per_block(uint32_t percent_of_max_inflation_rate);
118

119
         void update_elected_producers(time cycle_time);
120

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

123
         void regproxy( const account_name proxy );
124

125
         void unregproxy( const account_name proxy );
126

127
         void nonce( const std::string& /*value*/ ) {}
128

129
         // functions defined in producer_pay.cpp
130

131
         void onblock( const block_header& header );
132

133
         void claimrewards( const account_name& owner );
134

135 136
      private:
         // Implementation details:
137

138 139 140 141 142 143 144 145 146 147
         //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 );
148

149
   };
150

151
} /// eosiosystem