Scorum
db_with.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
5 /*
6  * This file provides with() functions which modify the database
7  * temporarily, then restore it. These functions are mostly internal
8  * implementation detail of the database.
9  *
10  * Essentially, we want to be able to use "finally" to restore the
11  * database regardless of whether an exception is thrown or not, but there
12  * is no "finally" in C++. Instead, C++ requires us to create a struct
13  * and put the finally block in a destructor. Aagh!
14  */
15 
16 namespace scorum {
17 namespace chain {
18 namespace detail {
27 {
28  skip_flags_restorer(node_property_object& npo, uint32_t old_skip_flags)
29  : _npo(npo)
30  , _old_skip_flags(old_skip_flags)
31  {
32  }
33 
35  {
37  }
38 
40  uint32_t _old_skip_flags; // initialized in ctor
41 };
42 
51 {
52  pending_transactions_restorer(database& db, std::vector<signed_transaction>&& pending_transactions)
53  : _db(db)
54  , _pending_transactions(std::move(pending_transactions))
55  {
57  }
58 
60  {
61  for (const auto& tx : _db._popped_tx)
62  {
63  try
64  {
65  if (!_db.is_known_transaction(tx.id()))
66  {
67  // since push_transaction() takes a signed_transaction,
68  // the operation_results field will be ignored.
70  }
71  }
72  catch (const fc::exception&)
73  {
74  }
75  }
76  _db._popped_tx.clear();
78  {
79  try
80  {
81  if (!_db.is_known_transaction(tx.id()))
82  {
83  // since push_transaction() takes a signed_transaction,
84  // the operation_results field will be ignored.
86  }
87  }
88  catch (const transaction_exception& e)
89  {
90  dlog("Pending transaction became invalid after switching to block ${b} ${n} ${t}",
91  ("b", _db.head_block_id())("n", _db.head_block_num())("t", _db.head_block_time()));
92  dlog("The invalid transaction caused exception ${e}", ("e", e.to_detail_string()));
93  dlog("${t}", ("t", tx));
94  }
95  catch (const fc::exception& e)
96  {
97 
98  /*
99  dlog( "Pending transaction became invalid after switching to block ${b} ${n} ${t}",
100  ("b", _db.head_block_id())("n", _db.head_block_num())("t", _db.head_block_time()) );
101  dlog( "The invalid pending transaction caused exception ${e}", ("e", e.to_detail_string() ) );
102  dlog( "${t}", ("t", tx) );
103  */
104  }
105  }
106  }
107 
109  std::vector<signed_transaction> _pending_transactions;
110 };
111 
117 template <typename Lambda> void with_skip_flags(database& db, uint32_t skip_flags, Lambda callback)
118 {
120  skip_flags_restorer restorer(npo, npo.skip_flags);
121  npo.skip_flags = skip_flags;
122  callback();
123  return;
124 }
125 
132 template <typename Lambda>
133 void without_pending_transactions(database& db, std::vector<signed_transaction>&& pending_transactions, Lambda callback)
134 {
135  pending_transactions_restorer restorer(db, std::move(pending_transactions));
136  callback();
137  return;
138 }
139 }
140 }
141 } // scorum::chain::detail
tracks the blockchain state in an extensible manner
Definition: database.hpp:52
time_point_sec head_block_time() const
Definition: database.cpp:1222
void _push_transaction(const signed_transaction &trx)
Definition: database.cpp:774
block_id_type head_block_id() const
Definition: database.cpp:1232
bool is_known_transaction(const transaction_id_type &id) const
Definition: database.cpp:390
node_property_object & node_properties()
Definition: database.cpp:1252
std::deque< signed_transaction > _popped_tx
Definition: database.hpp:311
uint32_t head_block_num() const
Definition: database.cpp:1227
Contains per-node database configuration.
void without_pending_transactions(database &db, std::vector< signed_transaction > &&pending_transactions, Lambda callback)
Definition: db_with.hpp:133
void with_skip_flags(database &db, uint32_t skip_flags, Lambda callback)
Definition: db_with.hpp:117
Definition: asset.cpp:15
pending_transactions_restorer(database &db, std::vector< signed_transaction > &&pending_transactions)
Definition: db_with.hpp:52
std::vector< signed_transaction > _pending_transactions
Definition: db_with.hpp:109
skip_flags_restorer(node_property_object &npo, uint32_t old_skip_flags)
Definition: db_with.hpp:28