-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransaction_context.cpp
More file actions
682 lines (570 loc) · 32 KB
/
transaction_context.cpp
File metadata and controls
682 lines (570 loc) · 32 KB
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
676
677
678
679
680
681
682
#include <eosio/chain/apply_context.hpp>
#include <eosio/chain/transaction_context.hpp>
#include <eosio/chain/authorization_manager.hpp>
#include <eosio/chain/exceptions.hpp>
#include <eosio/chain/resource_limits.hpp>
#include <eosio/chain/generated_transaction_object.hpp>
#include <eosio/chain/transaction_object.hpp>
#include <eosio/chain/to_string.hpp>
#include <chrono>
namespace eosio { namespace chain {
using namespace db_util;
transaction_checktime_timer::transaction_checktime_timer(platform_timer& timer)
: expired(timer.expired), _timer(timer) {
expired = 0;
}
void transaction_checktime_timer::start(fc::time_point tp) {
_timer.start(tp);
}
void transaction_checktime_timer::stop() {
_timer.stop();
}
void transaction_checktime_timer::set_expiration_callback(void(*func)(void*), void* user) {
_timer.set_expiration_callback(func, user);
}
transaction_checktime_timer::~transaction_checktime_timer() {
stop();
_timer.set_expiration_callback(nullptr, nullptr);
}
transaction_context::transaction_context( controller& c,
const packed_transaction& t,
transaction_checktime_timer&& tmr,
fc::time_point s )
:control(c)
,packed_trx(t)
,undo_session(!c.skip_db_sessions() ? maybe_session(c.mutable_db()) : maybe_session())
,trace(std::make_shared<transaction_trace>())
,start(s)
,transaction_timer(std::move(tmr))
,net_usage(trace->net_usage)
,pseudo_start(s)
{
trace->id = packed_trx.id();
trace->block_num = c.head_block_num() + 1;
trace->block_time = c.pending_block_time();
trace->producer_block_id = c.pending_producer_block_id();
}
void transaction_context::disallow_transaction_extensions( const char* error_msg )const {
if( control.is_producing_block() ) {
EOS_THROW( subjective_block_production_exception, error_msg );
} else {
EOS_THROW( disallowed_transaction_extensions_bad_block_exception, error_msg );
}
}
void transaction_context::init( uint64_t initial_net_usage )
{
EOS_ASSERT( !is_initialized, transaction_exception, "cannot initialize twice" );
// set maximum to a semi-valid deadline to allow for pause math and conversion to dates for logging
if( block_deadline == fc::time_point::maximum() ) block_deadline = start + fc::hours(24*7*52);
const auto& cfg = control.get_global_properties().configuration;
auto& rl = control.get_mutable_resource_limits_manager();
net_limit = rl.get_block_net_limit();
objective_duration_limit = fc::microseconds( rl.get_block_cpu_limit() );
_deadline = start + objective_duration_limit;
// Possibly lower net_limit to the maximum net usage a transaction is allowed to be billed
if( cfg.max_transaction_net_usage <= net_limit ) {
net_limit = cfg.max_transaction_net_usage;
net_limit_due_to_block = false;
}
// Possibly lower objective_duration_limit to the maximum cpu usage a transaction is allowed to be billed
if( cfg.max_transaction_cpu_usage <= objective_duration_limit.count() ) {
objective_duration_limit = fc::microseconds(cfg.max_transaction_cpu_usage);
billing_timer_exception_code = tx_cpu_usage_exceeded::code_value;
_deadline = start + objective_duration_limit;
}
const transaction& trx = packed_trx.get_transaction();
// Possibly lower net_limit to optional limit set in the transaction header
uint64_t trx_specified_net_usage_limit = static_cast<uint64_t>(trx.max_net_usage_words.value) * 8;
if( trx_specified_net_usage_limit > 0 && trx_specified_net_usage_limit <= net_limit ) {
net_limit = trx_specified_net_usage_limit;
net_limit_due_to_block = false;
}
// Possibly lower objective_duration_limit to optional limit set in transaction header
if( trx.max_cpu_usage_ms > 0 ) {
auto trx_specified_cpu_usage_limit = fc::milliseconds(trx.max_cpu_usage_ms);
if( trx_specified_cpu_usage_limit <= objective_duration_limit ) {
objective_duration_limit = trx_specified_cpu_usage_limit;
billing_timer_exception_code = tx_cpu_usage_exceeded::code_value;
_deadline = start + objective_duration_limit;
}
}
if( explicit_billed_cpu_time )
validate_cpu_usage_to_bill( billed_cpu_time_us, std::numeric_limits<int64_t>::max(), false ); // Fail early if the amount to be billed is too high
// Record accounts to be billed for network and CPU usage
if( control.is_builtin_activated(builtin_protocol_feature_t::only_bill_first_authorizer) ) {
bill_to_accounts.insert( trx.first_authorizer() );
} else {
for( const auto& act : trx.actions ) {
for( const auto& auth : act.authorization ) {
bill_to_accounts.insert( auth.actor );
}
}
}
validate_ram_usage.reserve( bill_to_accounts.size() );
// Update usage values of accounts to reflect new time
rl.update_account_usage( bill_to_accounts, block_timestamp_type(control.pending_block_time()).slot );
// Calculate the highest network usage and CPU time that all of the billed accounts can afford to be billed
int64_t account_net_limit = 0;
int64_t account_cpu_limit = 0;
bool greylisted_net = false, greylisted_cpu = false;
std::tie( account_net_limit, account_cpu_limit, greylisted_net, greylisted_cpu) = max_bandwidth_billed_accounts_can_pay();
net_limit_due_to_greylist |= greylisted_net;
cpu_limit_due_to_greylist |= greylisted_cpu;
eager_net_limit = net_limit;
// Possibly lower eager_net_limit to what the billed accounts can pay plus some (objective) leeway
auto new_eager_net_limit = std::min( eager_net_limit, static_cast<uint64_t>(account_net_limit + cfg.net_usage_leeway) );
if( new_eager_net_limit < eager_net_limit ) {
eager_net_limit = new_eager_net_limit;
net_limit_due_to_block = false;
}
// Possibly limit deadline if the duration accounts can be billed for (+ a subjective leeway) does not exceed current delta
if( (fc::microseconds(account_cpu_limit) + leeway) <= (_deadline - start) ) {
_deadline = start + fc::microseconds(account_cpu_limit) + leeway;
billing_timer_exception_code = leeway_deadline_exception::code_value;
}
// Possibly limit deadline to subjective max_transaction_time
if( control.get_override_chain_cpu_limits() ||
( max_transaction_time_subjective != fc::microseconds::maximum() &&
( (start + max_transaction_time_subjective) <= _deadline ) ) ) {
if( max_transaction_time_subjective == fc::microseconds::maximum() ) max_transaction_time_subjective = fc::hours(24*7*52);
_deadline = start + max_transaction_time_subjective;
billing_timer_exception_code = tx_cpu_usage_exceeded::code_value;
}
// Possibly limit deadline to caller provided wall clock block deadline
if( block_deadline < _deadline && !control.get_override_chain_cpu_limits() ) {
_deadline = block_deadline;
billing_timer_exception_code = deadline_exception::code_value;
}
// Explicit billed_cpu_time_us should be used, block_deadline will be maximum unless in test code
if( explicit_billed_cpu_time ) {
_deadline = block_deadline;
deadline_exception_code = deadline_exception::code_value;
} else {
deadline_exception_code = billing_timer_exception_code;
}
if( !explicit_billed_cpu_time ) {
// Fail early if amount of the previous speculative execution is within 10% of remaining account cpu available
int64_t validate_account_cpu_limit = account_cpu_limit - subjective_cpu_bill_us + leeway.count(); // Add leeway to allow powerup
if( validate_account_cpu_limit > 0 )
validate_account_cpu_limit -= EOS_PERCENT( validate_account_cpu_limit, 10 * config::percent_1 );
if( validate_account_cpu_limit < 0 ) validate_account_cpu_limit = 0;
validate_account_cpu_usage_estimate( billed_cpu_time_us, validate_account_cpu_limit );
}
eager_net_limit = (eager_net_limit/8)*8; // Round down to nearest multiple of word size (8 bytes) so check_net_usage can be efficient
if( initial_net_usage > 0 )
add_net_usage( initial_net_usage ); // Fail early if current net usage is already greater than the calculated limit
if(control.skip_trx_checks())
transaction_timer.start(fc::time_point::maximum());
else {
transaction_timer.start(_deadline);
checktime(); // Fail early if deadline has already been exceeded
}
is_initialized = true;
}
void transaction_context::init_for_implicit_trx( uint64_t initial_net_usage )
{
const transaction& trx = packed_trx.get_transaction();
if( trx.transaction_extensions.size() > 0 ) {
disallow_transaction_extensions( "no transaction extensions supported yet for implicit transactions" );
}
published = control.pending_block_time();
init( initial_net_usage );
}
void transaction_context::init_for_input_trx( uint64_t packed_trx_unprunable_size,
uint64_t packed_trx_prunable_size,
bool skip_recording )
{
const transaction& trx = packed_trx.get_transaction();
if( trx.transaction_extensions.size() > 0 ) {
disallow_transaction_extensions( "no transaction extensions supported yet for input transactions" );
}
const auto& cfg = control.get_global_properties().configuration;
uint64_t discounted_size_for_pruned_data = packed_trx_prunable_size;
if( cfg.context_free_discount_net_usage_den > 0
&& cfg.context_free_discount_net_usage_num < cfg.context_free_discount_net_usage_den )
{
discounted_size_for_pruned_data *= cfg.context_free_discount_net_usage_num;
discounted_size_for_pruned_data = ( discounted_size_for_pruned_data + cfg.context_free_discount_net_usage_den - 1)
/ cfg.context_free_discount_net_usage_den; // rounds up
}
uint64_t initial_net_usage = static_cast<uint64_t>(cfg.base_per_transaction_net_usage)
+ packed_trx_unprunable_size + discounted_size_for_pruned_data;
init_for_input_trx_common( initial_net_usage, skip_recording );
}
void transaction_context::init_for_input_trx_with_explicit_net( uint32_t explicit_net_usage_words,
bool skip_recording )
{
const transaction& trx = packed_trx.get_transaction();
if( trx.transaction_extensions.size() > 0 ) {
disallow_transaction_extensions( "no transaction extensions supported yet for input transactions" );
}
explicit_net_usage = true;
net_usage = (static_cast<uint64_t>(explicit_net_usage_words) * 8);
init_for_input_trx_common( 0, skip_recording );
}
void transaction_context::init_for_input_trx_common( uint64_t initial_net_usage, bool skip_recording )
{
published = control.pending_block_time();
is_input = true;
const transaction& trx = packed_trx.get_transaction();
if (!control.skip_trx_checks()) {
control.validate_expiration(trx);
control.validate_tapos(trx);
validate_referenced_accounts( trx, enforce_whiteblacklist && control.is_producing_block() );
}
init( initial_net_usage );
if (!skip_recording)
record_transaction( packed_trx.id(), trx.expiration ); /// checks for dupes
}
void transaction_context::exec() {
EOS_ASSERT( is_initialized, transaction_exception, "must first initialize" );
const transaction& trx = packed_trx.get_transaction();
if( apply_context_free ) {
for( const auto& act : trx.context_free_actions ) {
schedule_action( act, act.account, true, 0, 0 );
}
}
for( const auto& act : trx.actions ) {
schedule_action( act, act.account, false, 0, 0 );
}
auto& action_traces = trace->action_traces;
uint32_t num_original_actions_to_execute = action_traces.size();
for( uint32_t i = 1; i <= num_original_actions_to_execute; ++i ) {
execute_action( i, 0 );
}
}
void transaction_context::finalize() {
EOS_ASSERT( is_initialized, transaction_exception, "must first initialize" );
if( is_input ) {
const transaction& trx = packed_trx.get_transaction();
auto& am = control.get_mutable_authorization_manager();
for( const auto& act : trx.actions ) {
for( const auto& auth : act.authorization ) {
am.update_permission_usage( am.get_permission(auth) );
}
}
}
auto& rl = control.get_mutable_resource_limits_manager();
for( auto a : validate_ram_usage ) {
rl.verify_account_ram_usage( a );
}
// Calculate the new highest network usage and CPU time that all of the billed accounts can afford to be billed
int64_t account_net_limit = 0;
int64_t account_cpu_limit = 0;
bool greylisted_net = false, greylisted_cpu = false;
std::tie( account_net_limit, account_cpu_limit, greylisted_net, greylisted_cpu) = max_bandwidth_billed_accounts_can_pay();
net_limit_due_to_greylist |= greylisted_net;
cpu_limit_due_to_greylist |= greylisted_cpu;
// Possibly lower net_limit to what the billed accounts can pay
if( static_cast<uint64_t>(account_net_limit) <= net_limit ) {
// NOTE: net_limit may possibly not be objective anymore due to net greylisting, but it should still be no greater than the truly objective net_limit
net_limit = static_cast<uint64_t>(account_net_limit);
net_limit_due_to_block = false;
}
// Possibly lower objective_duration_limit to what the billed accounts can pay
if( account_cpu_limit <= objective_duration_limit.count() ) {
// NOTE: objective_duration_limit may possibly not be objective anymore due to cpu greylisting, but it should still be no greater than the truly objective objective_duration_limit
objective_duration_limit = fc::microseconds(account_cpu_limit);
billing_timer_exception_code = tx_cpu_usage_exceeded::code_value;
}
eager_net_limit = net_limit;
round_up_net_usage(); // Round up to nearest multiple of word size (8 bytes).
check_net_usage(); // Check that NET usage satisfies limits (even when explicit_net_usage is true).
auto now = fc::time_point::now();
trace->elapsed = now - start;
update_billed_cpu_time( now );
validate_cpu_usage_to_bill( billed_cpu_time_us, account_cpu_limit, true );
rl.add_transaction_usage( bill_to_accounts, static_cast<uint64_t>(billed_cpu_time_us), net_usage,
block_timestamp_type(control.pending_block_time()).slot,
control.get_override_chain_cpu_limits() );
}
void transaction_context::squash() {
undo_session.squash();
}
void transaction_context::undo() {
undo_session.undo();
}
void transaction_context::check_net_usage()const {
if( BOOST_UNLIKELY(net_usage > eager_net_limit) ) {
if ( net_limit_due_to_block ) {
EOS_THROW( block_net_usage_exceeded,
"not enough space left in block: {net_usage} > {net_limit}",
("net_usage", net_usage)("net_limit", eager_net_limit) );
} else if (net_limit_due_to_greylist) {
EOS_THROW( greylist_net_usage_exceeded,
"greylisted transaction net usage is too high: {net_usage} > {net_limit}",
("net_usage", net_usage)("net_limit", eager_net_limit) );
} else {
EOS_THROW( tx_net_usage_exceeded,
"transaction net usage is too high: {net_usage} > {net_limit}",
("net_usage", net_usage)("net_limit", eager_net_limit) );
}
}
}
void transaction_context::checktime()const {
if(BOOST_LIKELY(transaction_timer.expired == false))
return;
auto now = fc::time_point::now();
if( explicit_billed_cpu_time || deadline_exception_code == deadline_exception::code_value ) {
EOS_THROW( deadline_exception, "deadline exceeded {billing_timer}us",
("billing_timer", now - pseudo_start)("now", now)("deadline", _deadline)("start", start) );
} else if( deadline_exception_code == block_cpu_usage_exceeded::code_value ) {
EOS_THROW( block_cpu_usage_exceeded,
"not enough time left in block to complete executing transaction {billing_timer}us",
("now", now)("deadline", _deadline)("start", start)("billing_timer", now - pseudo_start) );
} else if( deadline_exception_code == tx_cpu_usage_exceeded::code_value ) {
if (cpu_limit_due_to_greylist) {
EOS_THROW( greylist_cpu_usage_exceeded,
"greylisted transaction was executing for too long {billing_timer}us",
("now", now)("deadline", _deadline)("start", start)("billing_timer", now - pseudo_start) );
} else {
EOS_THROW( tx_cpu_usage_exceeded,
"transaction was executing for too long {billing_timer}us",
("now", now)("deadline", _deadline)("start", start)("billing_timer", now - pseudo_start) );
}
} else if( deadline_exception_code == leeway_deadline_exception::code_value ) {
EOS_THROW( leeway_deadline_exception,
"the transaction was unable to complete by deadline, "
"but it is possible it could have succeeded if it were allowed to run to completion {billing_timer}",
("now", now)("deadline", _deadline)("start", start)("billing_timer", now - pseudo_start) );
}
EOS_ASSERT( false, transaction_exception, "unexpected deadline exception code {code}", ("code", deadline_exception_code) );
}
void transaction_context::pause_billing_timer() {
if( explicit_billed_cpu_time || pseudo_start == fc::time_point() ) return; // either irrelevant or already paused
paused_time = fc::time_point::now();
billed_time = paused_time - pseudo_start;
pseudo_start = fc::time_point();
transaction_timer.stop();
}
void transaction_context::resume_billing_timer() {
if( explicit_billed_cpu_time || pseudo_start != fc::time_point() ) return; // either irrelevant or already running
auto now = fc::time_point::now();
auto paused = now - paused_time;
pseudo_start = now - billed_time;
_deadline += paused;
// do not allow to go past block wall clock deadline
if( block_deadline < _deadline && !control.get_override_chain_cpu_limits() ) {
deadline_exception_code = deadline_exception::code_value;
_deadline = block_deadline;
}
transaction_timer.start(_deadline);
}
void transaction_context::validate_cpu_usage_to_bill( int64_t billed_us, int64_t account_cpu_limit, bool check_minimum )const {
if (!control.skip_trx_checks()) {
if( check_minimum ) {
const auto& cfg = control.get_global_properties().configuration;
EOS_ASSERT( billed_us >= cfg.min_transaction_cpu_usage, transaction_exception,
"cannot bill CPU time less than the minimum of {min_billable} us",
("min_billable", cfg.min_transaction_cpu_usage)("billed_cpu_time_us", billed_us)
);
}
validate_account_cpu_usage( billed_us, account_cpu_limit );
}
}
void transaction_context::validate_account_cpu_usage( int64_t billed_us, int64_t account_cpu_limit )const {
if( (billed_us > 0) && !control.skip_trx_checks() && !control.get_override_chain_cpu_limits() ) {
const bool cpu_limited_by_account = (account_cpu_limit <= objective_duration_limit.count());
if( !cpu_limited_by_account && (billing_timer_exception_code == block_cpu_usage_exceeded::code_value) ) {
EOS_ASSERT( billed_us <= objective_duration_limit.count(),
block_cpu_usage_exceeded,
"billed CPU time ({billed} us) is greater than the billable CPU time left in the block ({billable} us)",
("billed", billed_us)( "billable", objective_duration_limit.count() )
);
} else {
if( cpu_limit_due_to_greylist && cpu_limited_by_account ) {
EOS_ASSERT( billed_us <= account_cpu_limit,
greylist_cpu_usage_exceeded,
"billed CPU time ({billed} us) is greater than the maximum greylisted billable CPU time for the transaction ({billable} us)",
("billed", billed_us)( "billable", account_cpu_limit )
);
} else {
// exceeds trx.max_cpu_usage_ms or cfg.max_transaction_cpu_usage if objective_duration_limit is greater
const int64_t cpu_limit = (cpu_limited_by_account ? account_cpu_limit : objective_duration_limit.count());
EOS_ASSERT( billed_us <= cpu_limit,
tx_cpu_usage_exceeded,
"billed CPU time ({billed} us) is greater than the maximum billable CPU time for the transaction ({billable} us)",
("billed", billed_us)( "billable", cpu_limit )
);
}
}
}
}
void transaction_context::validate_account_cpu_usage_estimate( int64_t prev_billed_us, int64_t account_cpu_limit )const {
// prev_billed_us can be 0, but so can account_cpu_limit
if( (prev_billed_us >= 0) && !control.skip_trx_checks() ) {
const bool cpu_limited_by_account = (account_cpu_limit <= objective_duration_limit.count());
if( !cpu_limited_by_account && (billing_timer_exception_code == block_cpu_usage_exceeded::code_value) ) {
EOS_ASSERT( prev_billed_us < objective_duration_limit.count(),
block_cpu_usage_exceeded,
"estimated CPU time ({billed} us) is not less than the billable CPU time left in the block ({billable} us)",
("billed", prev_billed_us)( "billable", objective_duration_limit.count() )
);
} else {
if( cpu_limit_due_to_greylist && cpu_limited_by_account ) {
EOS_ASSERT( prev_billed_us < account_cpu_limit,
greylist_cpu_usage_exceeded,
"estimated CPU time ({billed} us) is not less than the maximum greylisted billable CPU time for the transaction ({billable} us)",
("billed", prev_billed_us)( "billable", account_cpu_limit )
);
} else {
// exceeds trx.max_cpu_usage_ms or cfg.max_transaction_cpu_usage if objective_duration_limit is greater
const int64_t cpu_limit = (cpu_limited_by_account ? account_cpu_limit : objective_duration_limit.count());
EOS_ASSERT( prev_billed_us < cpu_limit,
tx_cpu_usage_exceeded,
"estimated CPU time ({billed} us) is not less than the maximum billable CPU time for the transaction ({billable} us)",
("billed", prev_billed_us)( "billable", cpu_limit )
);
}
}
}
}
void transaction_context::add_ram_usage( account_name account, int64_t ram_delta ) {
auto& rl = control.get_mutable_resource_limits_manager();
rl.add_pending_ram_usage( account, ram_delta );
if( ram_delta > 0 ) {
validate_ram_usage.insert( account );
}
}
uint32_t transaction_context::update_billed_cpu_time( fc::time_point now ) {
if( explicit_billed_cpu_time ) return static_cast<uint32_t>(billed_cpu_time_us);
const auto& cfg = control.get_global_properties().configuration;
billed_cpu_time_us = std::max( (now - pseudo_start).count(), static_cast<int64_t>(cfg.min_transaction_cpu_usage) );
return static_cast<uint32_t>(billed_cpu_time_us);
}
std::tuple<int64_t, int64_t, bool, bool> transaction_context::max_bandwidth_billed_accounts_can_pay( bool force_elastic_limits ) const{
// Assumes rl.update_account_usage( bill_to_accounts, block_timestamp_type(control.pending_block_time()).slot ) was already called prior
// Calculate the new highest network usage and CPU time that all of the billed accounts can afford to be billed
auto& rl = control.get_mutable_resource_limits_manager();
const static int64_t large_number_no_overflow = std::numeric_limits<int64_t>::max()/2;
int64_t account_net_limit = large_number_no_overflow;
int64_t account_cpu_limit = large_number_no_overflow;
bool greylisted_net = false;
bool greylisted_cpu = false;
uint32_t specified_greylist_limit = control.get_greylist_limit();
for( const auto& a : bill_to_accounts ) {
uint32_t greylist_limit = config::maximum_elastic_resource_multiplier;
if( !force_elastic_limits && control.is_producing_block() ) {
if( control.is_resource_greylisted(a) ) {
greylist_limit = 1;
} else {
greylist_limit = specified_greylist_limit;
}
}
auto [net_limit, net_was_greylisted] = rl.get_account_net_limit(a, greylist_limit);
if( net_limit >= 0 ) {
account_net_limit = std::min( account_net_limit, net_limit );
greylisted_net |= net_was_greylisted;
}
auto [cpu_limit, cpu_was_greylisted] = rl.get_account_cpu_limit(a, greylist_limit);
if( cpu_limit >= 0 ) {
account_cpu_limit = std::min( account_cpu_limit, cpu_limit );
greylisted_cpu |= cpu_was_greylisted;
}
}
EOS_ASSERT( (!force_elastic_limits && control.is_producing_block()) || (!greylisted_cpu && !greylisted_net),
transaction_exception, "greylisted when not producing block" );
return std::make_tuple(account_net_limit, account_cpu_limit, greylisted_net, greylisted_cpu);
}
action_trace& transaction_context::get_action_trace( uint32_t action_ordinal ) {
EOS_ASSERT( 0 < action_ordinal && action_ordinal <= trace->action_traces.size() ,
transaction_exception,
"action_ordinal {ordinal} is outside allowed range [1,{max}]",
("ordinal", action_ordinal)("max", trace->action_traces.size())
);
return trace->action_traces[action_ordinal-1];
}
const action_trace& transaction_context::get_action_trace( uint32_t action_ordinal )const {
EOS_ASSERT( 0 < action_ordinal && action_ordinal <= trace->action_traces.size() ,
transaction_exception,
"action_ordinal {ordinal} is outside allowed range [1,{max}]",
("ordinal", action_ordinal)("max", trace->action_traces.size())
);
return trace->action_traces[action_ordinal-1];
}
uint32_t transaction_context::schedule_action( const action& act, account_name receiver, bool context_free,
uint32_t creator_action_ordinal,
uint32_t closest_unnotified_ancestor_action_ordinal )
{
uint32_t new_action_ordinal = trace->action_traces.size() + 1;
trace->action_traces.emplace_back( *trace, act, receiver, context_free,
new_action_ordinal, creator_action_ordinal,
closest_unnotified_ancestor_action_ordinal );
return new_action_ordinal;
}
uint32_t transaction_context::schedule_action( action&& act, account_name receiver, bool context_free,
uint32_t creator_action_ordinal,
uint32_t closest_unnotified_ancestor_action_ordinal )
{
uint32_t new_action_ordinal = trace->action_traces.size() + 1;
trace->action_traces.emplace_back( *trace, std::move(act), receiver, context_free,
new_action_ordinal, creator_action_ordinal,
closest_unnotified_ancestor_action_ordinal );
return new_action_ordinal;
}
uint32_t transaction_context::schedule_action( uint32_t action_ordinal, account_name receiver, bool context_free,
uint32_t creator_action_ordinal,
uint32_t closest_unnotified_ancestor_action_ordinal )
{
uint32_t new_action_ordinal = trace->action_traces.size() + 1;
trace->action_traces.reserve( new_action_ordinal );
const action& provided_action = get_action_trace( action_ordinal ).act;
// The reserve above is required so that the emplace_back below does not invalidate the provided_action reference.
trace->action_traces.emplace_back( *trace, provided_action, receiver, context_free,
new_action_ordinal, creator_action_ordinal,
closest_unnotified_ancestor_action_ordinal );
return new_action_ordinal;
}
void transaction_context::execute_action( uint32_t action_ordinal, uint32_t recurse_depth ) {
apply_context acontext( control, *this, action_ordinal, recurse_depth );
acontext.exec();
}
void transaction_context::record_transaction( const transaction_id_type& id, fc::time_point_sec expire ) {
try {
control.mutable_db().create<transaction_object>([&](transaction_object& transaction) {
transaction.trx_id = id;
transaction.expiration = expire;
});
} catch( const boost::interprocess::bad_alloc& ) {
throw;
} catch ( ... ) {
EOS_ASSERT( false, tx_duplicate,
"duplicate transaction {id}", ("id", id ) );
}
} /// record_transaction
void transaction_context::validate_referenced_accounts( const transaction& trx, bool enforce_actor_whitelist_blacklist )const {
const auto& db = control.db();
const auto& auth_manager = control.get_authorization_manager();
if( !trx.context_free_actions.empty() && !control.skip_trx_checks() ) {
for( const auto& a : trx.context_free_actions ) {
auto* code = db.find<account_object, by_name>( a.account );
EOS_ASSERT( code != nullptr, transaction_exception,
"action's code account '{account}' does not exist", ("account", a.account) );
EOS_ASSERT( a.authorization.size() == 0, transaction_exception,
"context-free actions cannot have authorizations" );
}
}
flat_set<account_name> actors;
bool one_auth = false;
for( const auto& a : trx.actions ) {
auto* code = db.find<account_object, by_name>(a.account);
EOS_ASSERT( code != nullptr, transaction_exception,
"action's code account '{account}' does not exist", ("account", a.account) );
for( const auto& auth : a.authorization ) {
one_auth = true;
auto* actor = db.find<account_object, by_name>(auth.actor);
EOS_ASSERT( actor != nullptr, transaction_exception,
"action's authorizing actor '{account}' does not exist", ("account", auth.actor) );
EOS_ASSERT( auth_manager.find_permission(auth) != nullptr, transaction_exception,
"action's authorizations include a non-existent permission: {permission}",
("permission", auth) );
if( enforce_actor_whitelist_blacklist )
actors.insert( auth.actor );
}
}
EOS_ASSERT( one_auth, tx_no_auths, "transaction must have at least one authorization" );
if( enforce_actor_whitelist_blacklist ) {
control.check_actor_list( actors );
}
}
} } /// eosio::chain