BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface 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  {
74  _db.clear_pending();
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();
89  for( const processed_transaction& tx : _pending_transactions )
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
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
Definition: api.cpp:56
Used to generate a useful error report when an exception is thrown.At each level in the stack where t...
Definition: exception.hpp:56
void with_skip_flags(database &db, uint32_t skip_flags, Lambda callback)
Definition: db_with.hpp:113
void without_pending_transactions(database &db, std::vector< processed_transaction > &&pending_transactions, Lambda callback)
Definition: db_with.hpp:132
Contains per-node database configuration.
std::vector< processed_transaction > _pending_transactions
Definition: db_with.hpp:104
skip_flags_restorer(node_property_object &npo, uint32_t old_skip_flags)
Definition: db_with.hpp:49
node_property_object & node_properties()
Definition: db_getter.cpp:94
captures the result of evaluating the operations contained in the transaction
pending_transactions_restorer(database &db, std::vector< processed_transaction > &&pending_transactions)
Definition: db_with.hpp:71