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

4 5 6
#include "delegate_bandwidth.cpp"
#include "producer_pay.cpp"
#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),
D
Daniel Larimer 已提交
16 17
    _global(_self,_self),
    _rammarket(_self,_self)
18
   {
19
      //print( "construct system\n" );
20
      _gstate = _global.exists() ? _global.get() : get_default_parameters();
D
Daniel Larimer 已提交
21

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

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

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


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

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

57
      eosio_assert( _gstate.max_ram_size < max_ram_size, "ram may only be increased" ); /// decreasing ram might result market maker issues
58 59 60 61
      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);
62
      auto itr = _rammarket.find(S(4,RAMCORE));
63 64 65 66 67 68 69 70 71 72 73 74 75

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

76 77 78 79 80
   void system_contract::setpriv( account_name account, uint8_t ispriv ) {
      require_auth( _self );
      set_privileged( account, ispriv );
   }

D
Daniel Larimer 已提交
81 82 83 84 85 86 87 88
   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" );
      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)},
89
                                                    { bidder, N(eosio.names), bid, std::string("bid name ")+(name{newname}).to_string()  } );
D
Daniel Larimer 已提交
90 91

      name_bid_table bids(_self,_self);
92
      print( name{bidder}, " bid ", bid, " on ", name{newname}, "\n" );
D
Daniel Larimer 已提交
93
      auto current = bids.find( newname );
94
      if( current == bids.end() ) {
D
Daniel Larimer 已提交
95 96 97 98 99 100 101 102 103 104 105
         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%" );
         eosio_assert( current->high_bidder != bidder, "account is already high bidder" );

106 107
         INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {N(eosio.names),N(active)},
                                                       { N(eosio.names), current->high_bidder, asset(current->high_bid), 
D
Daniel Larimer 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                                                       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.
    *
    *  1. accounts cannot contain '.' symbols which forces all acccounts to be 12
    *  characters long without '.' until a future account auction process is implemented
    *  which prevents name squatting.
    *
    *  2. new accounts must stake a minimal number of tokens (as set in system parameters)
    *     therefore, this method will execute an inline buyram from receiver for newacnt in
    *     an amount equal to the current new account creation fee. 
    */
   void native::newaccount( account_name     creator,
                            account_name     newact
                            /*  no need to parse authorites
                            const authority& owner,
                            const authority& active*/ ) {

      if( creator != _self ) {
137
         auto tmp = newact >> 4;
D
Daniel Larimer 已提交
138
         bool has_dot = false;
139 140 141 142

         for( uint32_t i = 0; i < 12; ++i ) {
           has_dot |= !(tmp & 0x1f);
           tmp >>= 5;
D
Daniel Larimer 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
         }
         auto suffix = eosio::name_suffix(newact);
         if( has_dot ) {
            if( suffix == newact ) {
               name_bid_table bids(_self,_self);
               auto current = bids.find( newact );
               eosio_assert( current != bids.end(), "no active bid for name" ); 
               eosio_assert( current->high_bidder == creator, "only high bidder can claim" );
               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 );
   }

168 169 170 171 172 173 174
   void system_contract::setparams( const eosio_parameters& params ) {
      require_auth( N(eosio) );
      (eosiosystem::eosio_parameters&)(_gstate) = params;
      eosio_assert( 3 <= _gstate.max_authority_depth, "max_authority_depth should be at least 3" );
      set_blockchain_parameters( params );
   }

175 176
} /// eosio.system
 
D
Daniel Larimer 已提交
177

178
EOSIO_ABI( eosiosystem::system_contract,
179
     (setram)
D
Daniel Larimer 已提交
180 181
     // delegate_bandwith.cpp
     (delegatebw)(undelegatebw)(refund)
182
     (buyram)(buyrambytes)(sellram)
D
Daniel Larimer 已提交
183 184
     // voting.cpp
     // producer_pay.cpp
185
     (regproxy)(regproducer)(unregprod)(voteproducer)
D
Daniel Larimer 已提交
186 187 188 189
     (claimrewards)
     // native.hpp
     (onblock)
     (newaccount)(updateauth)(deleteauth)(linkauth)(unlinkauth)(postrecovery)(passrecovery)(vetorecovery)(onerror)(canceldelay)
190
     //this file
D
Daniel Larimer 已提交
191
     (bidname)
192
     (setpriv)
193
     (setparams)
194
)