-
Notifications
You must be signed in to change notification settings - Fork 15
/
eosio_contract.cpp
675 lines (551 loc) · 34.6 KB
/
eosio_contract.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/**
* @file
* @copyright defined in fio/LICENSE
*/
#include <eosio/chain/eosio_contract.hpp>
#include <eosio/chain/contract_table_objects.hpp>
#include <eosio/chain/controller.hpp>
#include <eosio/chain/transaction_context.hpp>
#include <eosio/chain/apply_context.hpp>
#include <eosio/chain/transaction.hpp>
#include <eosio/chain/exceptions.hpp>
#include <eosio/chain/account_object.hpp>
#include <eosio/chain/fioaction_object.hpp>
#include <eosio/chain/code_object.hpp>
#include <eosio/chain/permission_object.hpp>
#include <eosio/chain/permission_link_object.hpp>
#include <eosio/chain/global_property_object.hpp>
#include <eosio/chain/contract_types.hpp>
#include <eosio/chain/wasm_interface.hpp>
#include <eosio/chain/abi_serializer.hpp>
#include <eosio/chain/authorization_manager.hpp>
#include <eosio/chain/resource_limits.hpp>
namespace eosio {
namespace chain {
uint128_t transaction_id_to_sender_id(const transaction_id_type &tid) {
fc::uint128_t _id(tid._hash[3], tid._hash[2]);
return (unsigned __int128) _id;
}
void validate_authority_precondition(const apply_context &context, const authority &auth) {
for (const auto &a : auth.accounts) {
auto *acct = context.db.find<account_object, by_name>(a.permission.actor);
EOS_ASSERT(acct != nullptr, action_validate_exception,
"account '${account}' does not exist",
("account", a.permission.actor)
);
if (a.permission.permission == config::owner_name || a.permission.permission == config::active_name)
continue; // account was already checked to exist, so its owner and active permissions should exist
if (a.permission.permission ==
config::eosio_code_name) // virtual eosio.code permission does not really exist but is allowed
continue;
try {
context.control.get_authorization_manager().get_permission(
{a.permission.actor, a.permission.permission});
} catch (const permission_query_exception &) {
EOS_THROW(action_validate_exception,
"permission '${perm}' does not exist",
("perm", a.permission)
);
}
}
if (context.trx_context.enforce_whiteblacklist && context.control.is_producing_block()) {
for (const auto &p : auth.keys) {
context.control.check_key_list(p.key);
}
}
}
/**
* This method is called assuming precondition_system_newaccount succeeds a
*/
void apply_eosio_addaction(apply_context &context) {
auto create = context.get_action().data_as<addaction>();
try {
context.require_authorization(create.actor);
auto &db = context.db;
// for main net playback, if its after release 2.0.0 in block time use the new logic.
//NOTE -- on private test nets, it will always be after this main net block time, because
// genesis of the dev net is after this time, so we need to
//add logic to ensure that if the item isnt found, we fall back to the old logic.
//this allows dev nets to sync correctly.
//we need to have the block time for the main net playback, we need the extra checks
//for when we initialize a dev net..
if ((context.control.head_block_time().sec_since_epoch() > POST_RELEASE_200_BLOCK_TIME)){
//read the actions table get distinct contract names
//returns end iterator if index not found during playback.
const auto &idx = db.get_index<fioaction_index, by_actionname>();
vector<name> sysaccts;
// iterate through contract names in actions table
//if its before the actions table in playback, we wont execute this loop. idx will be end iterator.
for ( auto itr = idx.begin(); itr != idx.end(); itr++ ) {
name nm = name(itr->contractname);
if (std::find(sysaccts.begin(), sysaccts.end(), nm) == sysaccts.end()) {
sysaccts.insert(sysaccts.begin(),nm);
}
}
//EXTRA logic to ensure dev net initialized networks sync correctly
if (std::find(sysaccts.begin(), sysaccts.end(), create.actor) == sysaccts.end()){
EOS_ASSERT(create.actor == SYSTEMACCOUNT ||
create.actor == MSIGACCOUNT ||
create.actor == WRAPACCOUNT ||
create.actor == ASSERTACCOUNT ||
create.actor == REQOBTACCOUNT ||
create.actor == FeeContract ||
create.actor == AddressContract ||
create.actor == TPIDContract ||
create.actor == TokenContract ||
create.actor == TREASURYACCOUNT ||
create.actor == FIOSYSTEMACCOUNT ||
create.actor == FIOACCOUNT
,fio_invalid_account_or_action,"Invalid signature.");
}else {
EOS_ASSERT(std::find(sysaccts.begin(), sysaccts.end(), create.actor) != sysaccts.end(),
fio_invalid_account_or_action,
" signing account not in actions table, set code not permitted.");
}
}else{ //use the old logic, the list of well known fio accounts before 2.0.0
EOS_ASSERT(create.actor == SYSTEMACCOUNT ||
create.actor == MSIGACCOUNT ||
create.actor == WRAPACCOUNT ||
create.actor == ASSERTACCOUNT ||
create.actor == REQOBTACCOUNT ||
create.actor == FeeContract ||
create.actor == AddressContract ||
create.actor == TPIDContract ||
create.actor == TokenContract ||
create.actor == TREASURYACCOUNT ||
create.actor == FIOSYSTEMACCOUNT ||
create.actor == FIOACCOUNT
,fio_invalid_account_or_action,"Invalid signature.");
}
auto name_str = name(create.action).to_string();
auto contract_str = name(create.contract).to_string();
EOS_ASSERT(!create.action.empty(), action_validate_exception, "Action invalid or not found");
EOS_ASSERT(name_str.size() <= 12, action_validate_exception, "Action invalid or not found");
EOS_ASSERT(!create.contract.empty(), action_validate_exception, "Contract name invalid or not found");
EOS_ASSERT(contract_str.size() <= 12, action_validate_exception, "Contract name invalid or not found");
EOS_ASSERT(!create.actor.empty(), action_validate_exception, "Invalid Signature");
EOS_ASSERT(!create.contract.empty(), action_validate_exception, "Invalid Contract");
auto fioaction_name = db.find<fioaction_object, by_actionname>(create.action);
EOS_ASSERT(fioaction_name == nullptr, account_name_exists_exception,
"Action invalid or not found");
const auto &new_fioaction = db.create<fioaction_object>([&](auto &a) {
a.actionname = create.action;
a.contractname = create.contract;
a.blocktimestamp = context.control.pending_block_time().time_since_epoch().count();
});
} FC_CAPTURE_AND_RETHROW((create))
}
/**
* This method is called assuming precondition_system_newaccount succeeds a
*/
void apply_eosio_remaction(apply_context &context) {
auto rem = context.get_action().data_as<remaction>();
try {
context.require_authorization(rem.actor);
auto &db = context.db;
// for main net playback, if its after release 2.0.0 in block time use the new logic.
//NOTE -- on private test nets, it will always be after this main net block time, because
// genesis of the dev net is after this time, so we need to
//add logic to ensure that if the item isnt found, we fall back to the old logic.
//this allows dev nets to sync correctly.
//we need to have the block time for the main net playback, we need the extra checks
//for when we initialize a dev net..
if ((context.control.head_block_time().sec_since_epoch() > POST_RELEASE_200_BLOCK_TIME)){
//read the actions table get distinct contract names
//returns end iterator if index not found during playback.
const auto &idx = db.get_index<fioaction_index, by_actionname>();
vector<name> sysaccts;
// iterate through contract names in actions table
//if its before the actions table in playback, we wont execute this loop. idx will be end iterator.
for ( auto itr = idx.begin(); itr != idx.end(); itr++ ) {
name nm = name(itr->contractname);
if (std::find(sysaccts.begin(), sysaccts.end(), nm) == sysaccts.end()) {
sysaccts.insert(sysaccts.begin(),nm);
}
}
//EXTRA checks to ensure dev net initialized networks sync properly
if (std::find(sysaccts.begin(), sysaccts.end(), rem.actor) == sysaccts.end()){
EOS_ASSERT(rem.actor == SYSTEMACCOUNT ||
rem.actor == MSIGACCOUNT ||
rem.actor == WRAPACCOUNT ||
rem.actor == ASSERTACCOUNT ||
rem.actor == REQOBTACCOUNT ||
rem.actor == FeeContract ||
rem.actor == AddressContract ||
rem.actor == TPIDContract ||
rem.actor == TokenContract ||
rem.actor == TREASURYACCOUNT ||
rem.actor == FIOSYSTEMACCOUNT ||
rem.actor == FIOACCOUNT
,fio_invalid_account_or_action,"Invalid signature.");
}else {
EOS_ASSERT(std::find(sysaccts.begin(), sysaccts.end(), rem.actor) != sysaccts.end(),
fio_invalid_account_or_action,
" signing account not in actions table, remove action not permitted.");
}
}else{ //use the old logic the list of well known fio accounts before 2.0.0
EOS_ASSERT(rem.actor == SYSTEMACCOUNT ||
rem.actor == MSIGACCOUNT ||
rem.actor == WRAPACCOUNT ||
rem.actor == ASSERTACCOUNT ||
rem.actor == REQOBTACCOUNT ||
rem.actor == FeeContract ||
rem.actor == AddressContract ||
rem.actor == TPIDContract ||
rem.actor == TokenContract ||
rem.actor == TREASURYACCOUNT ||
rem.actor == FIOSYSTEMACCOUNT ||
rem.actor == FIOACCOUNT
,fio_invalid_account_or_action,"Invalid Signature" );
}
auto name_str = name(rem.action).to_string();
EOS_ASSERT(!rem.action.empty(), action_validate_exception, "Action invalid or not found");
EOS_ASSERT(name_str.size() <= 12, action_validate_exception, "Action invalid or not found");
EOS_ASSERT(!rem.actor.empty(), action_validate_exception, "Invalid Signature");
auto fioaction_name = db.find<fioaction_object, by_actionname>(rem.action);
EOS_ASSERT(fioaction_name != nullptr, account_name_exists_exception,
"Action invalid or not found");
db.remove(*fioaction_name);
} FC_CAPTURE_AND_RETHROW((rem))
}
/**
* This method is called assuming precondition_system_newaccount succeeds a
*/
void apply_eosio_newaccount(apply_context &context) {
auto create = context.get_action().data_as<newaccount>();
try {
context.require_authorization(create.creator);
// context.require_write_lock( config::eosio_auth_scope );
auto &authorization = context.control.get_mutable_authorization_manager();
EOS_ASSERT(validate(create.owner), action_validate_exception, "Invalid owner authority");
EOS_ASSERT(validate(create.active), action_validate_exception, "Invalid active authority");
auto &db = context.db;
auto name_str = name(create.name).to_string();
EOS_ASSERT(!create.name.empty(), action_validate_exception, "account name cannot be empty");
EOS_ASSERT(name_str.size() <= 12, action_validate_exception, "account names can only be 12 chars long");
// Check if the creator is privileged
const auto &creator = db.get<account_metadata_object, by_name>(create.creator);
if (!creator.is_privileged()) {
EOS_ASSERT(name_str.find("eosio.") != 0, action_validate_exception,
"only privileged accounts can have names that start with 'eosio.'");
}
auto existing_account = db.find<account_object, by_name>(create.name);
EOS_ASSERT(existing_account == nullptr, account_name_exists_exception,
"Cannot create account named ${name}, as that name is already taken",
("name", create.name));
const auto &new_account = db.create<account_object>([&](auto &a) {
a.name = create.name;
a.creation_date = context.control.pending_block_time();
});
db.create<account_metadata_object>([&](auto &a) {
a.name = create.name;
});
for (const auto &auth : {create.owner, create.active}) {
validate_authority_precondition(context, auth);
}
const auto &owner_permission = authorization.create_permission(create.name, config::owner_name, 0,
std::move(create.owner));
const auto &active_permission = authorization.create_permission(create.name, config::active_name,
owner_permission.id,
std::move(create.active));
context.control.get_mutable_resource_limits_manager().initialize_account(create.name);
int64_t ram_delta = config::overhead_per_account_ram_bytes;
ram_delta += 2 * config::billable_size_v<permission_object>;
ram_delta += owner_permission.auth.get_billable_size();
ram_delta += active_permission.auth.get_billable_size();
context.add_ram_usage(create.name, ram_delta);
} FC_CAPTURE_AND_RETHROW((create))
}
void apply_eosio_setcode(apply_context &context) {
const auto &cfg = context.control.get_global_properties().configuration;
auto &db = context.db;
auto act = context.get_action().data_as<setcode>();
context.require_authorization(act.account);
// for main net playback, if its after release 2.0.0 in block time use the new logic.
//NOTE -- on private test nets, it will always be after this main net block time, because
// genesis of the dev net is after this time, so we need to
//add logic to ensure that if the item isnt found, we fall back to the old logic.
//this allows dev nets to sync correctly.
//we need to have the block time for the main net playback, we need the extra checks
//for when we initialize a dev net..
if ((context.control.head_block_time().sec_since_epoch() > POST_RELEASE_200_BLOCK_TIME)){
//read the actions table get distinct contract names
//returns end iterator if index not found during playback.
const auto &idx = db.get_index<fioaction_index, by_actionname>();
vector<name> sysaccts;
// iterate through contract names in actions table
//if its before the actions table in playback, we wont execute this loop. idx will be end iterator.
for ( auto itr = idx.begin(); itr != idx.end(); itr++ ) {
name nm = name(itr->contractname);
if (std::find(sysaccts.begin(), sysaccts.end(), nm) == sysaccts.end()) {
sysaccts.insert(sysaccts.begin(),nm);
}
}
//EXTRA checks for dev net initialized networks, to ensure sync works correctly!
if (std::find(sysaccts.begin(), sysaccts.end(), act.account) == sysaccts.end()){
EOS_ASSERT(act.account == SYSTEMACCOUNT ||
act.account == MSIGACCOUNT ||
act.account == WRAPACCOUNT ||
act.account == ASSERTACCOUNT ||
act.account == REQOBTACCOUNT ||
act.account == FeeContract ||
act.account == AddressContract ||
act.account == TPIDContract ||
act.account == TokenContract ||
act.account == TREASURYACCOUNT ||
act.account == FIOSYSTEMACCOUNT ||
act.account == FIOACCOUNT
,fio_invalid_account_or_action,"Invalid signature.");
}else {
EOS_ASSERT(std::find(sysaccts.begin(), sysaccts.end(), act.account) != sysaccts.end(),
fio_invalid_account_or_action,
" signing account not in actions table, set code not permitted.");
}
}else{ //use the old logic the list of well known system accounts before release 2.0.0
EOS_ASSERT(act.account == SYSTEMACCOUNT ||
act.account == MSIGACCOUNT ||
act.account == WRAPACCOUNT ||
act.account == ASSERTACCOUNT ||
act.account == REQOBTACCOUNT ||
act.account == FeeContract ||
act.account == AddressContract ||
act.account == TPIDContract ||
act.account == TokenContract ||
act.account == TREASURYACCOUNT ||
act.account == FIOSYSTEMACCOUNT ||
act.account == FIOACCOUNT
,fio_invalid_account_or_action,"set code not permitted." );
}
EOS_ASSERT(act.vmtype == 0, invalid_contract_vm_type, "code should be 0");
EOS_ASSERT(act.vmversion == 0, invalid_contract_vm_version, "version should be 0");
fc::sha256 code_hash; /// default is the all zeros hash
int64_t code_size = (int64_t) act.code.size();
if (code_size > 0) {
code_hash = fc::sha256::hash(act.code.data(), (uint32_t) act.code.size());
wasm_interface::validate(context.control, act.code);
}
const auto &account = db.get<account_metadata_object, by_name>(act.account);
bool existing_code = (account.code_hash != digest_type());
EOS_ASSERT(code_size > 0 || existing_code, set_exact_code, "contract is already cleared");
int64_t old_size = 0;
int64_t new_size = code_size * config::setcode_ram_bytes_multiplier;
if (existing_code) {
const code_object &old_code_entry = db.get<code_object, by_code_hash>(
boost::make_tuple(account.code_hash, account.vm_type, account.vm_version));
EOS_ASSERT(old_code_entry.code_hash != code_hash, set_exact_code,
"contract is already running this version of code");
old_size = (int64_t) old_code_entry.code.size() * config::setcode_ram_bytes_multiplier;
if (old_code_entry.code_ref_count == 1) {
db.remove(old_code_entry);
context.control.get_wasm_interface().code_block_num_last_used(account.code_hash, account.vm_type,
account.vm_version,
context.control.head_block_num() + 1);
} else {
db.modify(old_code_entry, [](code_object &o) {
--o.code_ref_count;
});
}
}
if (code_size > 0) {
const code_object *new_code_entry = db.find<code_object, by_code_hash>(
boost::make_tuple(code_hash, act.vmtype, act.vmversion));
if (new_code_entry) {
db.modify(*new_code_entry, [&](code_object &o) {
++o.code_ref_count;
});
} else {
db.create<code_object>([&](code_object &o) {
o.code_hash = code_hash;
o.code.assign(act.code.data(), code_size);
o.code_ref_count = 1;
o.first_block_used = context.control.head_block_num() + 1;
o.vm_type = act.vmtype;
o.vm_version = act.vmversion;
});
}
}
db.modify(account, [&](auto &a) {
a.code_sequence += 1;
a.code_hash = code_hash;
a.vm_type = act.vmtype;
a.vm_version = act.vmversion;
a.last_code_update = context.control.pending_block_time();
});
if (new_size != old_size) {
context.add_ram_usage(act.account, new_size - old_size);
}
}
void apply_eosio_setabi(apply_context &context) {
auto &db = context.db;
auto act = context.get_action().data_as<setabi>();
context.require_authorization(act.account);
const auto &account = db.get<account_object, by_name>(act.account);
int64_t abi_size = act.abi.size();
int64_t old_size = (int64_t) account.abi.size();
int64_t new_size = abi_size;
db.modify(account, [&](auto &a) {
if (abi_size > 0) {
a.abi.assign(act.abi.data(), abi_size);
} else {
a.abi.resize(0);
}
});
const auto &account_metadata = db.get<account_metadata_object, by_name>(act.account);
db.modify(account_metadata, [&](auto &a) {
a.abi_sequence += 1;
});
if (new_size != old_size) {
context.add_ram_usage(act.account, new_size - old_size);
}
}
void apply_eosio_updateauth(apply_context &context) {
auto update = context.get_action().data_as<updateauth>();
context.require_authorization(
update.account); // only here to mark the single authority on this action as used
auto &authorization = context.control.get_mutable_authorization_manager();
auto &db = context.db;
EOS_ASSERT(!update.permission.empty(), action_validate_exception,
"Cannot create authority with empty name");
EOS_ASSERT(update.permission.to_string().find("eosio.") != 0, action_validate_exception,
"Permission names that start with 'eosio.' are reserved");
EOS_ASSERT(update.permission != update.parent, action_validate_exception,
"Cannot set an authority as its own parent");
db.get<account_object, by_name>(update.account);
EOS_ASSERT(validate(update.auth), action_validate_exception,
"Invalid authority: ${auth}", ("auth", update.auth));
if (update.permission == config::active_name)
EOS_ASSERT(update.parent == config::owner_name, action_validate_exception,
"Cannot change active authority's parent from owner", ("update.parent", update.parent));
if (update.permission == config::owner_name)
EOS_ASSERT(update.parent.empty(), action_validate_exception, "Cannot change owner authority's parent");
else
EOS_ASSERT(!update.parent.empty(), action_validate_exception,
"Only owner permission can have empty parent");
if (update.auth.waits.size() > 0) {
auto max_delay = context.control.get_global_properties().configuration.max_transaction_delay;
EOS_ASSERT(update.auth.waits.back().wait_sec <= max_delay, action_validate_exception,
"Cannot set delay longer than max_transacton_delay, which is ${max_delay} seconds",
("max_delay", max_delay));
}
validate_authority_precondition(context, update.auth);
auto permission = authorization.find_permission({update.account, update.permission});
// If a parent_id of 0 is going to be used to indicate the absence of a parent, then we need to make sure that the chain
// initializes permission_index with a dummy object that reserves the id of 0.
authorization_manager::permission_id_type parent_id = 0;
if (update.permission != config::owner_name) {
auto &parent = authorization.get_permission({update.account, update.parent});
parent_id = parent.id;
}
if (permission) {
EOS_ASSERT(parent_id == permission->parent, action_validate_exception,
"Changing parent authority is not currently supported");
int64_t old_size = (int64_t) (config::billable_size_v<permission_object> +
permission->auth.get_billable_size());
authorization.modify_permission(*permission, update.auth);
int64_t new_size = (int64_t) (config::billable_size_v<permission_object> +
permission->auth.get_billable_size());
context.add_ram_usage(permission->owner, new_size - old_size);
} else {
const auto &p = authorization.create_permission(update.account, update.permission, parent_id,
update.auth);
int64_t new_size = (int64_t) (config::billable_size_v<permission_object> + p.auth.get_billable_size());
context.add_ram_usage(update.account, new_size);
}
}
void apply_eosio_deleteauth(apply_context &context) {
// context.require_write_lock( config::eosio_auth_scope );
auto remove = context.get_action().data_as<deleteauth>();
context.require_authorization(
remove.account); // only here to mark the single authority on this action as used
EOS_ASSERT(remove.permission != config::active_name, action_validate_exception,
"Cannot delete active authority");
EOS_ASSERT(remove.permission != config::owner_name, action_validate_exception,
"Cannot delete owner authority");
auto &authorization = context.control.get_mutable_authorization_manager();
auto &db = context.db;
{ // Check for links to this permission
const auto &index = db.get_index<permission_link_index, by_permission_name>();
auto range = index.equal_range(boost::make_tuple(remove.account, remove.permission));
EOS_ASSERT(range.first == range.second, action_validate_exception,
"Cannot delete a linked authority. Unlink the authority first. This authority is linked to ${code}::${type}.",
("code", string(range.first->code))("type", string(range.first->message_type)));
}
const auto &permission = authorization.get_permission({remove.account, remove.permission});
int64_t old_size = config::billable_size_v<permission_object> + permission.auth.get_billable_size();
authorization.remove_permission(permission);
context.add_ram_usage(remove.account, -old_size);
}
void apply_eosio_linkauth(apply_context &context) {
// context.require_write_lock( config::eosio_auth_scope );
auto requirement = context.get_action().data_as<linkauth>();
try {
EOS_ASSERT(!requirement.requirement.empty(), action_validate_exception,
"Required permission cannot be empty");
context.require_authorization(
requirement.account); // only here to mark the single authority on this action as used
auto &db = context.db;
const auto *account = db.find<account_object, by_name>(requirement.account);
EOS_ASSERT(account != nullptr, account_query_exception,
"Failed to retrieve account: ${account}", ("account", requirement.account)); // Redundant?
const auto *code = db.find<account_object, by_name>(requirement.code);
EOS_ASSERT(code != nullptr, account_query_exception,
"Failed to retrieve code for account: ${account}", ("account", requirement.code));
if (requirement.requirement != config::eosio_any_name) {
const permission_object *permission = nullptr;
if (context.control.is_builtin_activated(
builtin_protocol_feature_t::only_link_to_existing_permission)) {
permission = db.find<permission_object, by_owner>(
boost::make_tuple(requirement.account, requirement.requirement)
);
} else {
permission = db.find<permission_object, by_name>(requirement.requirement);
}
EOS_ASSERT(permission != nullptr, permission_query_exception,
"Failed to retrieve permission: ${permission}", ("permission", requirement.requirement));
}
auto link_key = boost::make_tuple(requirement.account, requirement.code, requirement.type);
auto link = db.find<permission_link_object, by_action_name>(link_key);
if (link) {
EOS_ASSERT(link->required_permission != requirement.requirement, action_validate_exception,
"Attempting to update required authority, but new requirement is same as old");
db.modify(*link, [requirement = requirement.requirement](permission_link_object &link) {
link.required_permission = requirement;
});
} else {
const auto &l = db.create<permission_link_object>([&requirement](permission_link_object &link) {
link.account = requirement.account;
link.code = requirement.code;
link.message_type = requirement.type;
link.required_permission = requirement.requirement;
});
context.add_ram_usage(
l.account,
(int64_t) (config::billable_size_v<permission_link_object>)
);
}
} FC_CAPTURE_AND_RETHROW((requirement))
}
void apply_eosio_unlinkauth(apply_context &context) {
// context.require_write_lock( config::eosio_auth_scope );
auto &db = context.db;
auto unlink = context.get_action().data_as<unlinkauth>();
context.require_authorization(
unlink.account); // only here to mark the single authority on this action as used
auto link_key = boost::make_tuple(unlink.account, unlink.code, unlink.type);
auto link = db.find<permission_link_object, by_action_name>(link_key);
EOS_ASSERT(link != nullptr, action_validate_exception, "Attempting to unlink authority, but no link found");
context.add_ram_usage(
link->account,
-(int64_t) (config::billable_size_v<permission_link_object>)
);
db.remove(*link);
}
void apply_eosio_canceldelay(apply_context &context) {
auto cancel = context.get_action().data_as<canceldelay>();
context.require_authorization(
cancel.canceling_auth.actor); // only here to mark the single authority on this action as used
const auto &trx_id = cancel.trx_id;
context.cancel_deferred_transaction(transaction_id_to_sender_id(trx_id), account_name());
}
}
} // namespace eosio::chain