-
Notifications
You must be signed in to change notification settings - Fork 20
/
eosio.system.cpp
514 lines (427 loc) · 19.1 KB
/
eosio.system.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include <eosio.system/eosio.system.hpp>
#include <eosio.token/eosio.token.hpp>
#include <eosio/crypto.hpp>
#include <eosio/dispatcher.hpp>
#include <cmath>
namespace eosiosystem {
using eosio::current_time_point;
using eosio::token;
double get_continuous_rate(int64_t annual_rate) {
return std::log1p(double(annual_rate)/double(100*inflation_precision));
}
system_contract::system_contract( name s, name code, datastream<const char*> ds )
:native(s,code,ds),
_voters(get_self(), get_self().value),
_producers(get_self(), get_self().value),
_producers2(get_self(), get_self().value),
_global(get_self(), get_self().value),
_global2(get_self(), get_self().value),
_global3(get_self(), get_self().value),
_global4(get_self(), get_self().value),
_rammarket(get_self(), get_self().value),
_rexpool(get_self(), get_self().value),
_rexretpool(get_self(), get_self().value),
_rexretbuckets(get_self(), get_self().value),
_rexfunds(get_self(), get_self().value),
_rexbalance(get_self(), get_self().value),
_rexorders(get_self(), get_self().value)
{
_gstate = _global.exists() ? _global.get() : get_default_parameters();
_gstate2 = _global2.exists() ? _global2.get() : eosio_global_state2{};
_gstate3 = _global3.exists() ? _global3.get() : eosio_global_state3{};
_gstate4 = _global4.exists() ? _global4.get() : get_default_inflation_parameters();
}
eosio_global_state system_contract::get_default_parameters() {
eosio_global_state dp;
get_blockchain_parameters(dp);
return dp;
}
eosio_global_state4 system_contract::get_default_inflation_parameters() {
eosio_global_state4 gs4;
gs4.continuous_rate = get_continuous_rate(default_annual_rate);
gs4.inflation_pay_factor = default_inflation_pay_factor;
gs4.votepay_factor = default_votepay_factor;
return gs4;
}
symbol system_contract::core_symbol()const {
const static auto sym = get_core_symbol( _rammarket );
return sym;
}
system_contract::~system_contract() {
_global.set( _gstate, get_self() );
_global2.set( _gstate2, get_self() );
_global3.set( _gstate3, get_self() );
_global4.set( _gstate4, get_self() );
}
void system_contract::setram( uint64_t max_ram_size ) {
require_auth( get_self() );
check( _gstate.max_ram_size < max_ram_size, "ram may only be increased" ); /// decreasing ram might result market maker issues
check( max_ram_size < 1024ll*1024*1024*1024*1024, "ram size is unrealistic" );
check( 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);
auto itr = _rammarket.find(ramcore_symbol.raw());
/**
* Increase the amount of ram for sale based upon the change in max ram size.
*/
_rammarket.modify( itr, same_payer, [&]( auto& m ) {
m.base.balance.amount += delta;
});
_gstate.max_ram_size = max_ram_size;
}
void system_contract::update_ram_supply() {
auto cbt = eosio::current_block_time();
if( cbt <= _gstate2.last_ram_increase ) return;
auto itr = _rammarket.find(ramcore_symbol.raw());
auto new_ram = (cbt.slot - _gstate2.last_ram_increase.slot)*_gstate2.new_ram_per_block;
_gstate.max_ram_size += new_ram;
/**
* Increase the amount of ram for sale based upon the change in max ram size.
*/
_rammarket.modify( itr, same_payer, [&]( auto& m ) {
m.base.balance.amount += new_ram;
});
_gstate2.last_ram_increase = cbt;
}
void system_contract::setramrate( uint16_t bytes_per_block ) {
require_auth( get_self() );
update_ram_supply();
_gstate2.new_ram_per_block = bytes_per_block;
}
#ifdef SYSTEM_BLOCKCHAIN_PARAMETERS
extern "C" [[eosio::wasm_import]] void set_parameters_packed(const void*, size_t);
#endif
void system_contract::setparams( const blockchain_parameters_t& params ) {
require_auth( get_self() );
(eosio::blockchain_parameters&)(_gstate) = params;
check( 3 <= _gstate.max_authority_depth, "max_authority_depth should be at least 3" );
#ifndef SYSTEM_BLOCKCHAIN_PARAMETERS
set_blockchain_parameters( params );
#else
constexpr size_t param_count = 18;
// an upper bound on the serialized size
char buf[1 + sizeof(params) + param_count];
datastream<char*> stream(buf, sizeof(buf));
stream << uint8_t(17);
stream << uint8_t(0) << params.max_block_net_usage
<< uint8_t(1) << params.target_block_net_usage_pct
<< uint8_t(2) << params.max_transaction_net_usage
<< uint8_t(3) << params.base_per_transaction_net_usage
<< uint8_t(4) << params.net_usage_leeway
<< uint8_t(5) << params.context_free_discount_net_usage_num
<< uint8_t(6) << params.context_free_discount_net_usage_den
<< uint8_t(7) << params.max_block_cpu_usage
<< uint8_t(8) << params.target_block_cpu_usage_pct
<< uint8_t(9) << params.max_transaction_cpu_usage
<< uint8_t(10) << params.min_transaction_cpu_usage
<< uint8_t(11) << params.max_transaction_lifetime
<< uint8_t(12) << params.deferred_trx_expiration_window
<< uint8_t(13) << params.max_transaction_delay
<< uint8_t(14) << params.max_inline_action_size
<< uint8_t(15) << params.max_inline_action_depth
<< uint8_t(16) << params.max_authority_depth;
if(params.max_action_return_value_size)
{
stream << uint8_t(17) << params.max_action_return_value_size.value();
++buf[0];
}
set_parameters_packed(buf, stream.tellp());
#endif
}
#ifdef SYSTEM_CONFIGURABLE_WASM_LIMITS
// The limits on contract WebAssembly modules
struct wasm_parameters
{
uint32_t max_mutable_global_bytes;
uint32_t max_table_elements;
uint32_t max_section_elements;
uint32_t max_linear_memory_init;
uint32_t max_func_local_bytes;
uint32_t max_nested_structures;
uint32_t max_symbol_bytes;
uint32_t max_module_bytes;
uint32_t max_code_bytes;
uint32_t max_pages;
uint32_t max_call_depth;
};
static constexpr wasm_parameters default_limits = {
.max_mutable_global_bytes = 1024,
.max_table_elements = 1024,
.max_section_elements = 8192,
.max_linear_memory_init = 64*1024,
.max_func_local_bytes = 8192,
.max_nested_structures = 1024,
.max_symbol_bytes = 8192,
.max_module_bytes = 20*1024*1024,
.max_code_bytes = 20*1024*1024,
.max_pages = 528,
.max_call_depth = 251
};
static constexpr wasm_parameters high_limits = {
.max_mutable_global_bytes = 8192,
.max_table_elements = 8192,
.max_section_elements = 8192,
.max_linear_memory_init = 16*64*1024,
.max_func_local_bytes = 8192,
.max_nested_structures = 1024,
.max_symbol_bytes = 8192,
.max_module_bytes = 20*1024*1024,
.max_code_bytes = 20*1024*1024,
.max_pages = 528,
.max_call_depth = 1024
};
extern "C" [[eosio::wasm_import]] void set_wasm_parameters_packed( const void*, size_t );
void set_wasm_parameters( const wasm_parameters& params )
{
char buf[sizeof(uint32_t) + sizeof(params)] = {};
memcpy(buf + sizeof(uint32_t), ¶ms, sizeof(params));
set_wasm_parameters_packed( buf, sizeof(buf) );
}
void system_contract::wasmcfg( const name& settings )
{
require_auth( get_self() );
if( settings == "default"_n || settings == "low"_n )
{
set_wasm_parameters( default_limits );
}
else if( settings == "high"_n )
{
set_wasm_parameters( high_limits );
}
else
{
check(false, "Unknown configuration");
}
}
#endif
void system_contract::setpriv( const name& account, uint8_t ispriv ) {
require_auth( get_self() );
set_privileged( account, ispriv );
}
void system_contract::setalimits( const name& account, int64_t ram, int64_t net, int64_t cpu ) {
require_auth( get_self() );
user_resources_table userres( get_self(), account.value );
auto ritr = userres.find( account.value );
check( ritr == userres.end(), "only supports unlimited accounts" );
auto vitr = _voters.find( account.value );
if( vitr != _voters.end() ) {
bool ram_managed = has_field( vitr->flags1, voter_info::flags1_fields::ram_managed );
bool net_managed = has_field( vitr->flags1, voter_info::flags1_fields::net_managed );
bool cpu_managed = has_field( vitr->flags1, voter_info::flags1_fields::cpu_managed );
check( !(ram_managed || net_managed || cpu_managed), "cannot use setalimits on an account with managed resources" );
}
set_resource_limits( account, ram, net, cpu );
}
void system_contract::setacctram( const name& account, const std::optional<int64_t>& ram_bytes ) {
require_auth( get_self() );
int64_t current_ram, current_net, current_cpu;
get_resource_limits( account, current_ram, current_net, current_cpu );
int64_t ram = 0;
if( !ram_bytes ) {
auto vitr = _voters.find( account.value );
check( vitr != _voters.end() && has_field( vitr->flags1, voter_info::flags1_fields::ram_managed ),
"RAM of account is already unmanaged" );
user_resources_table userres( get_self(), account.value );
auto ritr = userres.find( account.value );
ram = ram_gift_bytes;
if( ritr != userres.end() ) {
ram += ritr->ram_bytes;
}
_voters.modify( vitr, same_payer, [&]( auto& v ) {
v.flags1 = set_field( v.flags1, voter_info::flags1_fields::ram_managed, false );
});
} else {
check( *ram_bytes >= 0, "not allowed to set RAM limit to unlimited" );
auto vitr = _voters.find( account.value );
if ( vitr != _voters.end() ) {
_voters.modify( vitr, same_payer, [&]( auto& v ) {
v.flags1 = set_field( v.flags1, voter_info::flags1_fields::ram_managed, true );
});
} else {
_voters.emplace( account, [&]( auto& v ) {
v.owner = account;
v.flags1 = set_field( v.flags1, voter_info::flags1_fields::ram_managed, true );
});
}
ram = *ram_bytes;
}
set_resource_limits( account, ram, current_net, current_cpu );
}
void system_contract::setacctnet( const name& account, const std::optional<int64_t>& net_weight ) {
require_auth( get_self() );
int64_t current_ram, current_net, current_cpu;
get_resource_limits( account, current_ram, current_net, current_cpu );
int64_t net = 0;
if( !net_weight ) {
auto vitr = _voters.find( account.value );
check( vitr != _voters.end() && has_field( vitr->flags1, voter_info::flags1_fields::net_managed ),
"Network bandwidth of account is already unmanaged" );
user_resources_table userres( get_self(), account.value );
auto ritr = userres.find( account.value );
if( ritr != userres.end() ) {
net = ritr->net_weight.amount;
}
_voters.modify( vitr, same_payer, [&]( auto& v ) {
v.flags1 = set_field( v.flags1, voter_info::flags1_fields::net_managed, false );
});
} else {
check( *net_weight >= -1, "invalid value for net_weight" );
auto vitr = _voters.find( account.value );
if ( vitr != _voters.end() ) {
_voters.modify( vitr, same_payer, [&]( auto& v ) {
v.flags1 = set_field( v.flags1, voter_info::flags1_fields::net_managed, true );
});
} else {
_voters.emplace( account, [&]( auto& v ) {
v.owner = account;
v.flags1 = set_field( v.flags1, voter_info::flags1_fields::net_managed, true );
});
}
net = *net_weight;
}
set_resource_limits( account, current_ram, net, current_cpu );
}
void system_contract::setacctcpu( const name& account, const std::optional<int64_t>& cpu_weight ) {
require_auth( get_self() );
int64_t current_ram, current_net, current_cpu;
get_resource_limits( account, current_ram, current_net, current_cpu );
int64_t cpu = 0;
if( !cpu_weight ) {
auto vitr = _voters.find( account.value );
check( vitr != _voters.end() && has_field( vitr->flags1, voter_info::flags1_fields::cpu_managed ),
"CPU bandwidth of account is already unmanaged" );
user_resources_table userres( get_self(), account.value );
auto ritr = userres.find( account.value );
if( ritr != userres.end() ) {
cpu = ritr->cpu_weight.amount;
}
_voters.modify( vitr, same_payer, [&]( auto& v ) {
v.flags1 = set_field( v.flags1, voter_info::flags1_fields::cpu_managed, false );
});
} else {
check( *cpu_weight >= -1, "invalid value for cpu_weight" );
auto vitr = _voters.find( account.value );
if ( vitr != _voters.end() ) {
_voters.modify( vitr, same_payer, [&]( auto& v ) {
v.flags1 = set_field( v.flags1, voter_info::flags1_fields::cpu_managed, true );
});
} else {
_voters.emplace( account, [&]( auto& v ) {
v.owner = account;
v.flags1 = set_field( v.flags1, voter_info::flags1_fields::cpu_managed, true );
});
}
cpu = *cpu_weight;
}
set_resource_limits( account, current_ram, current_net, cpu );
}
void system_contract::activate( const eosio::checksum256& feature_digest ) {
require_auth( get_self() );
preactivate_feature( feature_digest );
}
void system_contract::rmvproducer( const name& producer ) {
require_auth( get_self() );
auto prod = _producers.find( producer.value );
check( prod != _producers.end(), "producer not found" );
_producers.modify( prod, same_payer, [&](auto& p) {
p.deactivate();
});
}
void system_contract::updtrevision( uint8_t revision ) {
require_auth( get_self() );
check( _gstate2.revision < 255, "can not increment revision" ); // prevent wrap around
check( revision == _gstate2.revision + 1, "can only increment revision by one" );
check( revision <= 1, // set upper bound to greatest revision supported in the code
"specified revision is not yet supported by the code" );
_gstate2.revision = revision;
}
void system_contract::setinflation( int64_t annual_rate, int64_t inflation_pay_factor, int64_t votepay_factor ) {
require_auth(get_self());
check(annual_rate >= 0, "annual_rate can't be negative");
if ( inflation_pay_factor < pay_factor_precision ) {
check( false, "inflation_pay_factor must not be less than " + std::to_string(pay_factor_precision) );
}
if ( votepay_factor < pay_factor_precision ) {
check( false, "votepay_factor must not be less than " + std::to_string(pay_factor_precision) );
}
_gstate4.continuous_rate = get_continuous_rate(annual_rate);
_gstate4.inflation_pay_factor = inflation_pay_factor;
_gstate4.votepay_factor = votepay_factor;
_global4.set( _gstate4, get_self() );
}
/**
* Called after a new account is created. This code enforces resource-limits rules
* for new accounts as well as new account naming conventions.
*
* 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.
*
*/
void native::newaccount( const name& creator,
const name& new_account_name,
ignore<authority> owner,
ignore<authority> active ) {
if( creator != get_self() ) {
uint64_t tmp = new_account_name.value >> 4;
bool has_dot = false;
for( uint32_t i = 0; i < 12; ++i ) {
has_dot |= !(tmp & 0x1f);
tmp >>= 5;
}
if( has_dot ) { // or is less than 12 characters
auto suffix = new_account_name.suffix();
if( suffix == new_account_name ) {
name_bid_table bids(get_self(), get_self().value);
auto current = bids.find( new_account_name.value );
check( current != bids.end(), "no active bid for name" );
check( current->high_bidder == creator, "only highest bidder can claim" );
check( current->high_bid < 0, "auction for name is not closed yet" );
bids.erase( current );
} else {
check( creator == suffix, "only suffix may create this account" );
}
}
}
user_resources_table userres( get_self(), new_account_name.value );
userres.emplace( new_account_name, [&]( auto& res ) {
res.owner = new_account_name;
res.net_weight = asset( 0, system_contract::get_core_symbol() );
res.cpu_weight = asset( 0, system_contract::get_core_symbol() );
});
set_resource_limits( new_account_name, 0, 0, 0 );
}
void native::setabi( const name& acnt, const std::vector<char>& abi,
const binary_extension<std::string>& memo ) {
eosio::multi_index< "abihash"_n, abi_hash > table(get_self(), get_self().value);
auto itr = table.find( acnt.value );
if( itr == table.end() ) {
table.emplace( acnt, [&]( auto& row ) {
row.owner = acnt;
row.hash = eosio::sha256(const_cast<char*>(abi.data()), abi.size());
});
} else {
table.modify( itr, same_payer, [&]( auto& row ) {
row.hash = eosio::sha256(const_cast<char*>(abi.data()), abi.size());
});
}
}
void system_contract::init( unsigned_int version, const symbol& core ) {
require_auth( get_self() );
check( version.value == 0, "unsupported version for init action" );
auto itr = _rammarket.find(ramcore_symbol.raw());
check( itr == _rammarket.end(), "system contract has already been initialized" );
auto system_token_supply = eosio::token::get_supply(token_account, core.code() );
check( system_token_supply.symbol == core, "specified core symbol does not exist (precision mismatch)" );
check( system_token_supply.amount > 0, "system token supply must be greater than 0" );
_rammarket.emplace( get_self(), [&]( auto& m ) {
m.supply.amount = 100000000000000ll;
m.supply.symbol = ramcore_symbol;
m.base.balance.amount = int64_t(_gstate.free_ram());
m.base.balance.symbol = ram_symbol;
m.quote.balance.amount = system_token_supply.amount / 1000;
m.quote.balance.symbol = core;
});
token::open_action open_act{ token_account, { {get_self(), active_permission} } };
open_act.send( rex_account, core, get_self() );
}
} /// eosio.system