-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmempool.cpp
584 lines (458 loc) · 19.4 KB
/
mempool.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
#include <koinos/mempool/mempool.hpp>
#include <koinos/mempool/state.hpp>
#include <chrono>
#include <functional>
#include <tuple>
#include <boost/multiprecision/cpp_int.hpp>
#include <koinos/chain/value.pb.h>
#include <koinos/protocol/protocol.pb.h>
#include <koinos/mempool/mempool.pb.h>
#include <koinos/util/base58.hpp>
#include <koinos/util/conversion.hpp>
#include <koinos/util/hex.hpp>
namespace koinos::mempool {
namespace detail {
using int128_t = boost::multiprecision::int128_t;
class mempool_impl final
{
private:
state_db::database _db;
crypto::multihash tmp_id( crypto::multihash id ) const;
state_db::state_node_ptr relevant_node( std::optional< crypto::multihash > id, state_db::shared_lock_ptr lock ) const;
state_db::state_node_ptr relevant_node( std::optional< crypto::multihash > id, state_db::unique_lock_ptr lock ) const;
void cleanup_account_resources_on_node( state_db::state_node_ptr node, const pending_transaction_record& pending_trx );
uint64_t remove_pending_transactions_on_node( state_db::state_node_ptr node, const std::vector< transaction_id_type >& ids );
bool check_pending_account_resources_on_node(
state_db::abstract_state_node_ptr node,
const account_type& payer,
uint64_t max_payer_resources,
uint64_t trx_resource_limit ) const;
uint64_t add_pending_transaction_to_node(
state_db::anonymous_state_node_ptr node,
const protocol::transaction& transaction,
std::chrono::system_clock::time_point time,
uint64_t max_payer_rc,
uint64_t disk_storaged_used,
uint64_t network_bandwidth_used,
uint64_t compute_bandwidth_used );
public:
mempool_impl( state_db::fork_resolution_algorithm algo );
virtual ~mempool_impl();
bool has_pending_transaction( const transaction_id_type& id, std::optional< crypto::multihash > block_id ) const;
std::vector< rpc::mempool::pending_transaction > get_pending_transactions( uint64_t limit, std::optional< crypto::multihash > block_id );
bool check_pending_account_resources(
const account_type& payer,
uint64_t max_payer_resources,
uint64_t trx_resource_limit,
std::optional< crypto::multihash > block_id = {} ) const;
uint64_t add_pending_transaction(
const protocol::transaction& transaction,
std::chrono::system_clock::time_point time,
uint64_t max_payer_rc,
uint64_t disk_storaged_used,
uint64_t network_bandwidth_used,
uint64_t compute_bandwidth_used );
uint64_t remove_pending_transactions( const std::vector< transaction_id_type >& ids );
uint64_t prune( std::chrono::seconds expiration, std::chrono::system_clock::time_point now );
void handle_block( const koinos::broadcast::block_accepted& bam );
void handle_irreversibility( const koinos::broadcast::block_irreversible& bi );
};
mempool_impl::mempool_impl( state_db::fork_resolution_algorithm algo )
{
auto lock = _db.get_unique_lock();
_db.open( {}, []( state_db::state_node_ptr ){}, algo, lock );
auto node_id = _db.get_head( lock )->id();
[[maybe_unused]] auto node = _db.create_writable_node( node_id, tmp_id( node_id ), protocol::block_header(), lock );
assert( node );
}
mempool_impl::~mempool_impl() = default;
crypto::multihash mempool_impl::tmp_id( crypto::multihash id ) const
{
return crypto::hash( crypto::multicodec::sha2_256, id );
}
state_db::state_node_ptr mempool_impl::relevant_node( std::optional< crypto::multihash > id, state_db::shared_lock_ptr lock ) const
{
state_db::state_node_ptr node;
if ( id )
{
if ( node = _db.get_node( *id, lock ); node )
node = _db.get_node( tmp_id( node->id() ), lock );
}
else
{
if ( node = _db.get_head( lock ); node )
node = _db.get_node( tmp_id( node->id() ), lock );
}
return node;
}
state_db::state_node_ptr mempool_impl::relevant_node( std::optional< crypto::multihash > id, state_db::unique_lock_ptr lock ) const
{
state_db::state_node_ptr node;
if ( id )
{
if ( node = _db.get_node( *id, lock ); node )
node = _db.get_node( tmp_id( node->id() ), lock );
}
else
{
if ( node = _db.get_head( lock ); node )
node = _db.get_node( tmp_id( node->id() ), lock );
}
return node;
}
void mempool_impl::handle_block( const koinos::broadcast::block_accepted& bam )
{
auto block_id = util::converter::to< crypto::multihash >( bam.block().id() );
auto previous_id = util::converter::to< crypto::multihash >( bam.block().header().previous() );
auto lock = _db.get_unique_lock();
LOG(debug) << "Handling block - Height: " << bam.block().header().height() << ", ID: " << block_id;
try
{
auto root_id = _db.get_root( lock )->id();
// We are handling the genesis case
crypto::multihash node_id;
if ( root_id != _db.get_head( lock )->id() )
node_id = tmp_id( previous_id );
else
node_id = tmp_id( root_id );
auto node = _db.get_node( node_id, lock );
KOINOS_ASSERT(
node,
pending_transaction_unlinkable_block,
"encountered an unlinkable block - Height: ${h}, ID: ${i}", ("h", bam.block().header().height())("i", util::to_hex( bam.block().id() ))
);
node = _db.clone_node( node_id, block_id, bam.block().header(), lock );
std::vector< transaction_id_type > ids;
for ( int i = 0; i < bam.block().transactions_size(); ++i )
ids.emplace_back( bam.block().transactions( i ).id() );
const auto removed_count = remove_pending_transactions_on_node( node, ids );
if ( bam.live() && removed_count )
LOG(info) << "Removed " << removed_count << " included transaction(s) via block - Height: "
<< bam.block().header().height() << ", ID: " << util::to_hex( bam.block().id() );
}
catch ( ... )
{
// It is possible for this to occur, especially when cycling the mempool or spinning up a new one
// in a running cluster. This will result in a warning being emitted.
_db.discard_node( block_id, lock );
throw;
}
_db.finalize_node( block_id, lock );
[[maybe_unused]] auto node = _db.create_writable_node( block_id, tmp_id( block_id ), protocol::block_header(), lock );
assert( node );
}
void mempool_impl::handle_irreversibility( const koinos::broadcast::block_irreversible& bi )
{
auto block_id = util::converter::to< crypto::multihash >( bi.topology().id() );
auto lock = _db.get_unique_lock();
if ( auto lib = _db.get_node( block_id, lock ); lib )
_db.commit_node( block_id, lock );
}
bool mempool_impl::has_pending_transaction( const transaction_id_type& id, std::optional< crypto::multihash > block_id ) const
{
auto lock = _db.get_shared_lock();
if ( auto node = relevant_node( block_id, lock ); node )
return node->get_object( space::transaction_index(), id ) != nullptr;
return false;
}
std::vector< rpc::mempool::pending_transaction > mempool_impl::get_pending_transactions( uint64_t limit, std::optional< crypto::multihash > block_id )
{
KOINOS_ASSERT(
limit <= constants::max_request_limit,
pending_transaction_request_overflow,
"requested too many pending transactions. max: ${max}", ("max", constants::max_request_limit)
);
auto lock = _db.get_shared_lock();
auto node = relevant_node( block_id, lock );
KOINOS_ASSERT(
node,
pending_transaction_unknown_block,
"cannot retrieve pending transactions from an unknown block"
);
std::vector< rpc::mempool::pending_transaction > pending_transactions;
pending_transactions.reserve( limit );
state_db::object_key next = state_db::object_key();
while ( pending_transactions.size() < limit )
{
auto [ value, key ] = node->get_next_object( space::pending_transaction(), next );
if ( !value )
break;
next = key;
auto pending_tx = util::converter::to< pending_transaction_record >( *value );
uint64_t seq_no = util::converter::to< uint64_t >( key );
rpc::mempool::pending_transaction ptx;
*ptx.mutable_transaction() = pending_tx.transaction();
ptx.set_disk_storage_used( pending_tx.disk_storage_used() );
ptx.set_network_bandwidth_used( pending_tx.network_bandwidth_used() );
ptx.set_compute_bandwidth_used( pending_tx.compute_bandwidth_used() );
pending_transactions.push_back( ptx );
}
return pending_transactions;
}
bool mempool_impl::check_pending_account_resources(
const account_type& payer,
uint64_t max_payer_resources,
uint64_t trx_resource_limit,
std::optional< crypto::multihash > block_id ) const
{
auto lock = _db.get_shared_lock();
auto node = relevant_node( block_id, lock );
KOINOS_ASSERT(
node,
pending_transaction_unknown_block,
"cannot check pending account resources from an unknown block"
);
return check_pending_account_resources_on_node( node, payer, max_payer_resources, trx_resource_limit );
}
bool mempool_impl::check_pending_account_resources_on_node(
state_db::abstract_state_node_ptr node,
const account_type& payer,
uint64_t max_payer_resources,
uint64_t trx_resource_limit ) const
{
if ( auto obj = node->get_object( space::address_resources(), payer ); obj )
{
auto arr = util::converter::to< address_resource_record >( *obj );
int128_t max_resource_delta = int128_t( max_payer_resources ) - int128_t( arr.max_rc() );
int128_t new_resources = int128_t( arr.current_rc() ) + max_resource_delta - int128_t( trx_resource_limit );
return new_resources >= 0;
}
return trx_resource_limit <= max_payer_resources;
}
uint64_t mempool_impl::add_pending_transaction_to_node(
state_db::anonymous_state_node_ptr node,
const protocol::transaction& transaction,
std::chrono::system_clock::time_point time,
uint64_t max_payer_rc,
uint64_t disk_storaged_used,
uint64_t network_bandwidth_used,
uint64_t compute_bandwidth_used )
{
uint64_t rc_used = 0;
KOINOS_ASSERT(
check_pending_account_resources_on_node( node, transaction.header().payer(), max_payer_rc, transaction.header().rc_limit() ),
pending_transaction_exceeds_resources,
"transaction would exceed maximum resources for account: ${a}",
("a", util::encode_base58( util::converter::as< std::vector< std::byte > >( transaction.header().payer() )) )
);
KOINOS_ASSERT(
node->get_object( space::transaction_index(), transaction.id() ) == nullptr,
pending_transaction_insertion_failure,
"cannot insert duplicate transaction"
);
// Grab the latest metadata object if it exists
mempool_metadata metadata;
if ( auto obj = node->get_object( space::mempool_metadata(), std::string{} ); obj )
metadata = util::converter::to< mempool_metadata >( *obj );
else
metadata.set_seq_num( 1 );
uint64_t tx_id = metadata.seq_num();
metadata.set_seq_num( metadata.seq_num() + 1 );
// Update metadata
auto obj = util::converter::as< std::string >( metadata );
assert( !obj.empty() );
node->put_object( space::mempool_metadata(), std::string{}, &obj );
pending_transaction_record pending_tx;
*pending_tx.mutable_transaction() = transaction;
pending_tx.set_timestamp( std::chrono::duration_cast< std::chrono::milliseconds >( time.time_since_epoch() ).count() );
pending_tx.set_disk_storage_used( disk_storaged_used );
pending_tx.set_network_bandwidth_used( network_bandwidth_used );
pending_tx.set_compute_bandwidth_used( compute_bandwidth_used );
std::string transaction_index_bytes = util::converter::as< std::string >( tx_id );
auto pending_transaction_bytes = util::converter::as< std::string >( pending_tx );
node->put_object( space::pending_transaction(), transaction_index_bytes, &pending_transaction_bytes );
node->put_object( space::transaction_index(), transaction.id(), &transaction_index_bytes );
address_resource_record arr;
if ( auto obj = node->get_object( space::address_resources(), transaction.header().payer() ); obj )
{
arr = util::converter::to< address_resource_record >( *obj );
int128_t max_rc_delta = int128_t( max_payer_rc ) - int128_t( arr.max_rc() );
int128_t new_rc = int128_t( arr.current_rc() ) + max_rc_delta - int128_t( transaction.header().rc_limit() );
arr.set_max_rc( max_payer_rc );
arr.set_current_rc( new_rc.convert_to< uint64_t >() );
rc_used = max_payer_rc - arr.current_rc();
}
else
{
arr.set_max_rc( max_payer_rc );
arr.set_current_rc( max_payer_rc - transaction.header().rc_limit() );
rc_used = transaction.header().rc_limit();
}
auto address_resource_bytes = util::converter::as< std::string >( arr );
node->put_object( space::address_resources(), transaction.header().payer(), &address_resource_bytes );
return rc_used;
}
uint64_t mempool_impl::add_pending_transaction(
const protocol::transaction& transaction,
std::chrono::system_clock::time_point time,
uint64_t max_payer_rc,
uint64_t disk_storaged_used,
uint64_t network_bandwidth_used,
uint64_t compute_bandwidth_used )
{
uint64_t rc_used = 0;
auto lock = _db.get_unique_lock();
auto nodes = _db.get_all_nodes( lock );
try
{
std::vector< state_db::anonymous_state_node_ptr > anonymous_state_nodes;
auto tmp_head = relevant_node( _db.get_head( lock )->id(), lock );
for ( auto state_node : nodes )
{
if ( state_node->is_finalized() )
continue;
auto node = state_node->create_anonymous_node();
anonymous_state_nodes.push_back( node );
auto rc = add_pending_transaction_to_node(
node,
transaction,
time,
max_payer_rc,
disk_storaged_used,
network_bandwidth_used,
compute_bandwidth_used
);
// We're only returning the RC used as it pertains to head
if ( state_node->id() == tmp_head->id() )
rc_used = rc;
}
for ( auto anonymous_state_node : anonymous_state_nodes )
anonymous_state_node->commit();
}
catch ( const std::exception& e )
{
LOG(debug) << "Failed to apply pending transaction " << util::to_hex( transaction.id() ) << " with: " << e.what();
throw;
}
catch ( ... )
{
LOG(debug) << "Failed to apply pending transaction " << util::to_hex( transaction.id() );
throw;
}
LOG(debug) << "Transaction added to mempool: " << util::to_hex( transaction.id() );
return rc_used;
}
uint64_t mempool_impl::remove_pending_transactions( const std::vector< transaction_id_type >& ids )
{
uint64_t count = 0;
auto lock = _db.get_unique_lock();
auto nodes = _db.get_fork_heads( lock );
auto head = _db.get_head( lock );
for ( auto block_node : nodes )
{
auto node = relevant_node( block_node->id(), lock );
auto num_removed = remove_pending_transactions_on_node( node, ids );
// We're only returning the number of transactions removed as it pertains to head
if ( block_node->id() == head->id() )
count = num_removed;
}
return count;
}
uint64_t mempool_impl::remove_pending_transactions_on_node( state_db::state_node_ptr node, const std::vector< transaction_id_type >& ids )
{
uint64_t count = 0;
for ( const auto& id : ids )
{
auto seq_obj = node->get_object( space::transaction_index(), id );
if ( !seq_obj )
continue;
auto ptx_obj = node->get_object( space::pending_transaction(), *seq_obj );
assert( ptx_obj );
auto pending_tx = util::converter::to< pending_transaction_record >( *ptx_obj );
cleanup_account_resources_on_node( node, pending_tx );
node->remove_object( space::pending_transaction(), *seq_obj );
node->remove_object( space::transaction_index(), id );
count++;
}
return count;
}
uint64_t mempool_impl::prune( std::chrono::seconds expiration, std::chrono::system_clock::time_point now )
{
uint64_t count = 0;
auto lock = _db.get_unique_lock();
auto nodes = _db.get_fork_heads( lock );
auto head = _db.get_head( lock );
for ( auto block_node : nodes )
{
auto node = relevant_node( block_node->id(), lock );
for (;;)
{
auto [ ptx_obj, key ] = node->get_next_object( space::pending_transaction(), std::string{} );
if ( !ptx_obj )
break;
auto pending_tx = util::converter::to< pending_transaction_record >( *ptx_obj );
std::chrono::system_clock::time_point time { std::chrono::milliseconds { pending_tx.timestamp() } };
if ( time + expiration > now )
break;
cleanup_account_resources_on_node( node, pending_tx );
node->remove_object( space::pending_transaction(), key );
node->remove_object( space::transaction_index(), pending_tx.transaction().id() );
// Only consider pruned transactions on the fork considered head
if ( block_node->id() == head->id() )
count++;
}
}
return count;
}
void mempool_impl::cleanup_account_resources_on_node( state_db::state_node_ptr node, const pending_transaction_record& pending_trx )
{
auto obj = node->get_object( space::address_resources(), pending_trx.transaction().header().payer() );
assert( obj );
auto arr = util::converter::to< address_resource_record >( *obj );
if ( arr.current_rc() + pending_trx.transaction().header().rc_limit() >= arr.max_rc() )
{
node->remove_object( space::address_resources(), pending_trx.transaction().header().payer() );
}
else
{
arr.set_current_rc( arr.current_rc() + pending_trx.transaction().header().rc_limit() );
auto arr_obj = util::converter::as< std::string >( arr );
node->put_object( space::address_resources(), pending_trx.transaction().header().payer(), &arr_obj );
}
}
} // detail
mempool::mempool( state_db::fork_resolution_algorithm algo ) : _my( std::make_unique< detail::mempool_impl >( algo ) ) {}
mempool::~mempool() = default;
bool mempool::has_pending_transaction( const transaction_id_type& id, std::optional< crypto::multihash > block_id ) const
{
return _my->has_pending_transaction( id, block_id );
}
std::vector< rpc::mempool::pending_transaction > mempool::get_pending_transactions( uint64_t limit, std::optional< crypto::multihash > block_id )
{
return _my->get_pending_transactions( limit, block_id );
}
bool mempool::check_pending_account_resources(
const account_type& payer,
uint64_t max_payer_resources,
uint64_t trx_resource_limit,
std::optional< crypto::multihash > block_id ) const
{
return _my->check_pending_account_resources( payer, max_payer_resources, trx_resource_limit, block_id );
}
uint64_t mempool::add_pending_transaction(
const protocol::transaction& transaction,
std::chrono::system_clock::time_point time,
uint64_t max_payer_rc,
uint64_t disk_storaged_used,
uint64_t network_bandwidth_used,
uint64_t compute_bandwidth_used )
{
return _my->add_pending_transaction( transaction, time, max_payer_rc, disk_storaged_used, network_bandwidth_used, compute_bandwidth_used );
}
uint64_t mempool::remove_pending_transactions( const std::vector< transaction_id_type >& ids )
{
return _my->remove_pending_transactions( ids );
}
uint64_t mempool::prune( std::chrono::seconds expiration, std::chrono::system_clock::time_point now )
{
return _my->prune( expiration, now );
}
void mempool::handle_block( const koinos::broadcast::block_accepted& bam )
{
_my->handle_block( bam );
}
void mempool::handle_irreversibility( const koinos::broadcast::block_irreversible& bi )
{
_my->handle_irreversibility( bi );
}
} // koinos::mempool