BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
db_with.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 
27 
28 /*
29  * This file provides with() functions which modify the database
30  * temporarily, then restore it. These functions are mostly internal
31  * implementation detail of the database.
32  *
33  * Essentially, we want to be able to use "finally" to restore the
34  * database regardless of whether an exception is thrown or not, but there
35  * is no "finally" in C++. Instead, C++ requires us to create a struct
36  * and put the finally block in a destructor. Aagh!
37  */
38 
39 namespace graphene { namespace chain { namespace detail {
48 {
49  skip_flags_restorer( node_property_object& npo, uint32_t old_skip_flags )
50  : _npo( npo ), _old_skip_flags( old_skip_flags )
51  {}
52 
54  {
56  }
57 
59  uint32_t _old_skip_flags; // initialized in ctor
60 };
61 
70 {
71  pending_transactions_restorer( database& db, std::vector<processed_transaction>&& pending_transactions )
72  : _db(db), _pending_transactions( std::move(pending_transactions) )
73  {
75  }
76 
78  {
79  for( const auto& tx : _db._popped_tx )
80  {
81  try {
82  if( !_db.is_known_transaction( tx.id() ) ) {
83  _db._push_transaction( tx );
84  }
85  } catch ( const fc::exception& ) { // ignore invalid transactions
86  }
87  }
88  _db._popped_tx.clear();
90  {
91  try
92  {
93  if( !_db.is_known_transaction( tx.id() ) ) {
94  _db._push_transaction( tx );
95  }
96  }
97  catch( const fc::exception& )
98  { // ignore invalid transactions
99  }
100  }
101  }
102 
104  std::vector< processed_transaction > _pending_transactions;
105 };
106 
112 template< typename Lambda >
114  database& db,
115  uint32_t skip_flags,
116  Lambda callback )
117 {
119  skip_flags_restorer restorer( npo, npo.skip_flags );
120  npo.skip_flags = skip_flags;
121  callback();
122  return;
123 }
124 
131 template< typename Lambda >
133  database& db,
134  std::vector<processed_transaction>&& pending_transactions,
135  Lambda callback )
136 {
137  pending_transactions_restorer restorer( db, std::move(pending_transactions) );
138  callback();
139  return;
140 }
141 
142 } } } // graphene::chain::detail
graphene::chain::database::is_known_transaction
bool is_known_transaction(const transaction_id_type &id) const
Definition: db_block.cpp:57
graphene::chain::database
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
graphene::chain::detail::skip_flags_restorer
Definition: db_with.hpp:47
fc::exception
Used to generate a useful error report when an exception is thrown.
Definition: exception.hpp:56
graphene::chain::detail::pending_transactions_restorer::_pending_transactions
std::vector< processed_transaction > _pending_transactions
Definition: db_with.hpp:104
database.hpp
graphene::chain::database::clear_pending
void clear_pending()
Definition: db_block.cpp:541
graphene::chain::detail::with_skip_flags
void with_skip_flags(database &db, uint32_t skip_flags, Lambda callback)
Definition: db_with.hpp:113
graphene::chain::detail::without_pending_transactions
void without_pending_transactions(database &db, std::vector< processed_transaction > &&pending_transactions, Lambda callback)
Definition: db_with.hpp:132
graphene::chain::detail::pending_transactions_restorer::pending_transactions_restorer
pending_transactions_restorer(database &db, std::vector< processed_transaction > &&pending_transactions)
Definition: db_with.hpp:71
graphene::chain::database::_push_transaction
processed_transaction _push_transaction(const precomputable_transaction &trx)
Definition: db_block.cpp:282
graphene::chain::node_property_object
Contains per-node database configuration.
Definition: node_property_object.hpp:39
graphene::chain::detail::pending_transactions_restorer
Definition: db_with.hpp:69
graphene::chain::detail::skip_flags_restorer::~skip_flags_restorer
~skip_flags_restorer()
Definition: db_with.hpp:53
graphene::chain::database::_popped_tx
std::deque< precomputable_transaction > _popped_tx
Definition: database.hpp:660
graphene::protocol::processed_transaction
captures the result of evaluating the operations contained in the transaction
Definition: transaction.hpp:292
std
Definition: zeroed_array.hpp:76
graphene::chain::database::node_properties
node_property_object & node_properties()
Definition: db_getter.cpp:97
graphene::chain::detail::pending_transactions_restorer::~pending_transactions_restorer
~pending_transactions_restorer()
Definition: db_with.hpp:77
graphene::chain::detail::pending_transactions_restorer::_db
database & _db
Definition: db_with.hpp:103
graphene::chain::node_property_object::skip_flags
uint32_t skip_flags
Definition: node_property_object.hpp:45
graphene::chain::detail::skip_flags_restorer::_npo
node_property_object & _npo
Definition: db_with.hpp:58
graphene::chain::detail::skip_flags_restorer::_old_skip_flags
uint32_t _old_skip_flags
Definition: db_with.hpp:59
graphene
Definition: api.cpp:48
graphene::chain::detail::skip_flags_restorer::skip_flags_restorer
skip_flags_restorer(node_property_object &npo, uint32_t old_skip_flags)
Definition: db_with.hpp:49