eosio.system.cpp 8.1 KB
Newer Older
1 2
#include "eosio.system.hpp"
#include <eosiolib/dispatcher.hpp>
3

4
#include "producer_pay.cpp"
5
#include "delegate_bandwidth.cpp"
6
#include "voting.cpp"
D
Daniel Larimer 已提交
7
#include "exchange_state.cpp"
8

D
Daniel Larimer 已提交
9

10
namespace eosiosystem {
D
Daniel Larimer 已提交
11

12 13 14 15
   system_contract::system_contract( account_name s )
   :native(s),
    _voters(_self,_self),
    _producers(_self,_self),
16
    _producers2(_self,_self),    
D
Daniel Larimer 已提交
17 18
    _global(_self,_self),
    _rammarket(_self,_self)
19
   {
20
      //print( "construct system\n" );
21
      _gstate = _global.exists() ? _global.get() : get_default_parameters();
D
Daniel Larimer 已提交
22

23
      auto itr = _rammarket.find(S(4,RAMCORE));
D
Daniel Larimer 已提交
24 25 26

      if( itr == _rammarket.end() ) {
         auto system_token_supply   = eosio::token(N(eosio.token)).get_supply(eosio::symbol_type(system_token_symbol).name()).amount;
27 28 29
         if( system_token_supply > 0 ) {
            itr = _rammarket.emplace( _self, [&]( auto& m ) {
               m.supply.amount = 100000000000000ll;
30
               m.supply.symbol = S(4,RAMCORE);
31 32 33
               m.base.balance.amount = int64_t(_gstate.free_ram());
               m.base.balance.symbol = S(0,RAM);
               m.quote.balance.amount = system_token_supply / 1000;
34
               m.quote.balance.symbol = CORE_SYMBOL;
35 36
            });
         }
D
Daniel Larimer 已提交
37
      } else {
38
         //print( "ram market already created" );
D
Daniel Larimer 已提交
39
      }
40 41 42 43 44 45 46 47 48 49
   }

   eosio_global_state system_contract::get_default_parameters() {
      eosio_global_state dp;
      get_blockchain_parameters(dp);
      return dp;
   }


   system_contract::~system_contract() {
50
      //print( "destruct system\n" );
51
      _global.set( _gstate, _self );
52
      //eosio_exit(0);
53 54
   }

55 56 57
   void system_contract::setram( uint64_t max_ram_size ) {
      require_auth( _self );

58
      eosio_assert( _gstate.max_ram_size < max_ram_size, "ram may only be increased" ); /// decreasing ram might result market maker issues
59 60 61 62
      eosio_assert( max_ram_size < 1024ll*1024*1024*1024*1024, "ram size is unrealistic" );
      eosio_assert( max_ram_size > _gstate.total_ram_bytes_reserved, "attempt to set max below reserved" );

      auto delta = int64_t(max_ram_size) - int64_t(_gstate.max_ram_size);
63
      auto itr = _rammarket.find(S(4,RAMCORE));
64 65 66 67 68 69 70 71 72 73 74 75 76

      /**
       *  Increase or decrease the amount of ram for sale based upon the change in max
       *  ram size.
       */
      _rammarket.modify( itr, 0, [&]( auto& m ) {
         m.base.balance.amount += delta;
      });

      _gstate.max_ram_size = max_ram_size;
      _global.set( _gstate, _self );
   }

A
arhag 已提交
77 78 79 80 81 82 83
   void system_contract::setparams( const eosio::blockchain_parameters& params ) {
      require_auth( N(eosio) );
      (eosio::blockchain_parameters&)(_gstate) = params;
      eosio_assert( 3 <= _gstate.max_authority_depth, "max_authority_depth should be at least 3" );
      set_blockchain_parameters( params );
   }

84 85 86 87 88
   void system_contract::setpriv( account_name account, uint8_t ispriv ) {
      require_auth( _self );
      set_privileged( account, ispriv );
   }

A
arhag 已提交
89 90 91 92 93 94 95
   void system_contract::rmvproducer( account_name producer ) {
      require_auth( _self );
      auto prod = _producers.find( producer );
      eosio_assert( prod != _producers.end(), "producer not found" );
      _producers.modify( prod, 0, [&](auto& p) {
            p.deactivate();
         });
W
Wang Zhi 已提交
96 97

      active_producer_seq(producer, public_key(), false);   
A
arhag 已提交
98 99
   }

D
Daniel Larimer 已提交
100 101 102
   void system_contract::bidname( account_name bidder, account_name newname, asset bid ) {
      require_auth( bidder );
      eosio_assert( eosio::name_suffix(newname) == newname, "you can only bid on top-level suffix" );
103 104 105
      eosio_assert( newname != 0, "the empty name is not a valid account name to bid on" );
      eosio_assert( (newname & 0xFull) == 0, "13 character names are not valid account names to bid on" );
      eosio_assert( (newname & 0x1F0ull) == 0, "accounts with 12 character names and no dots can be created without bidding required" );
D
Daniel Larimer 已提交
106 107 108 109 110
      eosio_assert( !is_account( newname ), "account already exists" );
      eosio_assert( bid.symbol == asset().symbol, "asset must be system token" );
      eosio_assert( bid.amount > 0, "insufficient bid" );

      INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {bidder,N(active)},
111
                                                    { bidder, N(eosio.names), bid, std::string("bid name ")+(name{newname}).to_string()  } );
D
Daniel Larimer 已提交
112 113

      name_bid_table bids(_self,_self);
114
      print( name{bidder}, " bid ", bid, " on ", name{newname}, "\n" );
D
Daniel Larimer 已提交
115
      auto current = bids.find( newname );
116
      if( current == bids.end() ) {
D
Daniel Larimer 已提交
117 118 119 120 121 122 123 124 125
         bids.emplace( bidder, [&]( auto& b ) {
            b.newname = newname;
            b.high_bidder = bidder;
            b.high_bid = bid.amount;
            b.last_bid_time = current_time();
         });
      } else {
         eosio_assert( current->high_bid > 0, "this auction has already closed" );
         eosio_assert( bid.amount - current->high_bid > (current->high_bid / 10), "must increase bid by 10%" );
126
         eosio_assert( current->high_bidder != bidder, "account is already highest bidder" );
D
Daniel Larimer 已提交
127

128
         INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {N(eosio.names),N(active)},
129
                                                       { N(eosio.names), current->high_bidder, asset(current->high_bid),
D
Daniel Larimer 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143
                                                       std::string("refund bid on name ")+(name{newname}).to_string()  } );

         bids.modify( current, bidder, [&]( auto& b ) {
            b.high_bidder = bidder;
            b.high_bid = bid.amount;
            b.last_bid_time = current_time();
         });
      }
   }

   /**
    *  Called after a new account is created. This code enforces resource-limits rules
    *  for new accounts as well as new account naming conventions.
    *
144 145 146
    *  Account names containing '.' symbols must have a suffix equal to the name of the creator.
    *  This allows users who buy a premium name (shorter than 12 characters with no dots) to be the only ones
    *  who can create accounts with the creator's name as a suffix.
D
Daniel Larimer 已提交
147 148 149 150
    *
    */
   void native::newaccount( account_name     creator,
                            account_name     newact
151
                            /*  no need to parse authorities
D
Daniel Larimer 已提交
152 153 154 155
                            const authority& owner,
                            const authority& active*/ ) {

      if( creator != _self ) {
156
         auto tmp = newact >> 4;
D
Daniel Larimer 已提交
157
         bool has_dot = false;
158 159 160 161

         for( uint32_t i = 0; i < 12; ++i ) {
           has_dot |= !(tmp & 0x1f);
           tmp >>= 5;
D
Daniel Larimer 已提交
162
         }
163 164
         if( has_dot ) { // or is less than 12 characters
            auto suffix = eosio::name_suffix(newact);
D
Daniel Larimer 已提交
165 166 167
            if( suffix == newact ) {
               name_bid_table bids(_self,_self);
               auto current = bids.find( newact );
168
               eosio_assert( current != bids.end(), "no active bid for name" );
169
               eosio_assert( current->high_bidder == creator, "only highest bidder can claim" );
D
Daniel Larimer 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
               eosio_assert( current->high_bid < 0, "auction for name is not closed yet" );
               bids.erase( current );
            } else {
               eosio_assert( creator == suffix, "only suffix may create this account" );
            }
         }
      }

      user_resources_table  userres( _self, newact);

      userres.emplace( newact, [&]( auto& res ) {
        res.owner = newact;
      });

      set_resource_limits( newact, 0, 0, 0 );
185 186 187 188 189 190 191 192 193 194 195

      //##YTA-Change  start:
      global_count_singleton global(_self, _self);
      eosio_global_count gstate;
      if(global.exists()) {
         gstate = global.get();
      }
      gstate.total_accounts += 1;
      global.set( gstate, _self );
         //##YTA-Change  end:

D
Daniel Larimer 已提交
196 197
   }

198
} /// eosio.system
199

D
Daniel Larimer 已提交
200

201
EOSIO_ABI( eosiosystem::system_contract,
A
arhag 已提交
202 203 204
     // native.hpp (newaccount definition is actually in eosio.system.cpp)
     (newaccount)(updateauth)(deleteauth)(linkauth)(unlinkauth)(canceldelay)(onerror)
     // eosio.system.cpp
A
arhag 已提交
205
     (setram)(setparams)(setpriv)(rmvproducer)(bidname)
A
arhag 已提交
206 207
     // delegate_bandwidth.cpp
     (buyrambytes)(buyram)(sellram)(delegatebw)(undelegatebw)(refund)
D
Daniel Larimer 已提交
208
     // voting.cpp
W
Wang Zhi 已提交
209
     (regproducer)(unregprod)(voteproducer)(regproxy)(clsprods2)(seqproducer)(testnewelec)
D
Daniel Larimer 已提交
210
     // producer_pay.cpp
A
arhag 已提交
211
     (onblock)(claimrewards)
212
)