diff --git a/libraries/chain/db_block.cpp b/libraries/chain/db_block.cpp index 5985e0fa..7bed189d 100644 --- a/libraries/chain/db_block.cpp +++ b/libraries/chain/db_block.cpp @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -85,9 +86,13 @@ bool database::push_block(const signed_block& new_block, uint32_t skip) { idump((new_block.block_num())(new_block.id())); bool result; - with_skip_flags(skip, [&]() + detail::with_skip_flags( *this, skip, [&]() { - result = _push_block(new_block); + detail::without_pending_transactions( *this, std::move(_pending_block.transactions), + [&]() + { + result = _push_block(new_block); + }); }); return result; } @@ -156,11 +161,6 @@ bool database::_push_block(const signed_block& new_block) } } - // If there is a pending block session, then the database state is dirty with pending transactions. - // Drop the pending session to reset the database to a clean head block state. - // TODO: Preserve pending transactions, and re-apply any which weren't included in the new block. - clear_pending(); - try { auto session = _undo_db.start_undo_session(); apply_block(new_block, skip); @@ -187,7 +187,7 @@ bool database::_push_block(const signed_block& new_block) processed_transaction database::push_transaction( const signed_transaction& trx, uint32_t skip ) { try { processed_transaction result; - with_skip_flags( skip, [&]() + detail::with_skip_flags( *this, skip, [&]() { result = _push_transaction( trx ); } ); @@ -249,7 +249,7 @@ signed_block database::generate_block( ) { signed_block result; - with_skip_flags( skip, [&]() + detail::with_skip_flags( *this, skip, [&]() { result = _generate_block( when, witness_id, block_signing_private_key, true ); } ); @@ -390,7 +390,7 @@ void database::apply_block( const signed_block& next_block, uint32_t skip ) skip = ~0;// WE CAN SKIP ALMOST EVERYTHING } - with_skip_flags( skip, [&]() + detail::with_skip_flags( *this, skip, [&]() { _apply_block( next_block ); } ); @@ -479,7 +479,7 @@ void database::notify_changed_objects() processed_transaction database::apply_transaction(const signed_transaction& trx, uint32_t skip) { processed_transaction result; - with_skip_flags(skip, [&]() + detail::with_skip_flags( *this, skip, [&]() { result = _apply_transaction(trx); }); diff --git a/libraries/chain/db_update.cpp b/libraries/chain/db_update.cpp index 11e5c0e0..cccc3bb2 100644 --- a/libraries/chain/db_update.cpp +++ b/libraries/chain/db_update.cpp @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -100,10 +101,6 @@ void database::update_pending_block(const signed_block& next_block, uint8_t curr { _pending_block.timestamp = next_block.timestamp + current_block_interval; _pending_block.previous = next_block.id(); - auto old_pending_trx = std::move(_pending_block.transactions); - _pending_block.transactions.clear(); - for( auto old_trx : old_pending_trx ) - push_transaction( old_trx ); } void database::clear_expired_transactions() @@ -142,7 +139,7 @@ void database::clear_expired_proposals() void database::clear_expired_orders() { - with_skip_flags( + detail::with_skip_flags( *this, get_node_properties().skip_flags | skip_authority_check, [&](){ transaction_evaluation_state cancel_context(this); @@ -158,7 +155,6 @@ void database::clear_expired_orders() } }); - //Process expired force settlement orders auto& settlement_index = get_index_type().indices().get(); if( !settlement_index.empty() ) diff --git a/libraries/chain/include/graphene/chain/database.hpp b/libraries/chain/include/graphene/chain/database.hpp index a601b49c..abfd1936 100644 --- a/libraries/chain/include/graphene/chain/database.hpp +++ b/libraries/chain/include/graphene/chain/database.hpp @@ -40,31 +40,6 @@ namespace graphene { namespace chain { using graphene::db::abstract_object; using graphene::db::object; - namespace detail - { - /** - * Class used to help the with_skip_flags implementation. - * It must be defined in this header because it must be - * available to the with_skip_flags implementation, - * which is a template and therefore must also be defined - * in this header. - */ - struct skip_flags_restorer - { - skip_flags_restorer( node_property_object& npo, uint32_t old_skip_flags ) - : _npo( npo ), _old_skip_flags( old_skip_flags ) - {} - - ~skip_flags_restorer() - { - _npo.skip_flags = _old_skip_flags; - } - - node_property_object& _npo; - uint32_t _old_skip_flags; - }; - } - /** * @class database * @brief tracks the blockchain state in an extensible manner @@ -270,23 +245,6 @@ namespace graphene { namespace chain { node_property_object& node_properties(); - /** - * Set the skip_flags to the given value, call callback, - * then reset skip_flags to their previous value after - * callback is done. - */ - template< typename Lambda > - void with_skip_flags( - uint32_t skip_flags, - Lambda callback ) - { - node_property_object& npo = node_properties(); - detail::skip_flags_restorer restorer( npo, npo.skip_flags ); - npo.skip_flags = skip_flags; - callback(); - return; - } - //////////////////// db_init.cpp //////////////////// void initialize_evaluators(); diff --git a/libraries/chain/include/graphene/chain/db_with.hpp b/libraries/chain/include/graphene/chain/db_with.hpp new file mode 100644 index 00000000..09781f0f --- /dev/null +++ b/libraries/chain/include/graphene/chain/db_with.hpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2015, Cryptonomex, Inc. + * All rights reserved. + * + * This source code is provided for evaluation in private test networks only, until September 8, 2015. After this date, this license expires and + * the code may not be used, modified or distributed for any purpose. Redistribution and use in source and binary forms, with or without modification, + * are permitted until September 8, 2015, provided that the following conditions are met: + * + * 1. The code and/or derivative works are used only for private test networks consisting of no more than 10 P2P nodes. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#pragma once + +#include + +/* + * This file provides with() functions which modify the database + * temporarily, then restore it. These functions are mostly internal + * implementation detail of the database. + * + * Essentially, we want to be able to use "finally" to restore the + * database regardless of whether an exception is thrown or not, but there + * is no "finally" in C++. Instead, C++ requires us to create a struct + * and put the finally block in a destructor. Aagh! + */ + +namespace graphene { namespace chain { namespace detail { +/** + * Class used to help the with_skip_flags implementation. + * It must be defined in this header because it must be + * available to the with_skip_flags implementation, + * which is a template and therefore must also be defined + * in this header. + */ +struct skip_flags_restorer +{ + skip_flags_restorer( node_property_object& npo, uint32_t old_skip_flags ) + : _npo( npo ), _old_skip_flags( old_skip_flags ) + {} + + ~skip_flags_restorer() + { + _npo.skip_flags = _old_skip_flags; + } + + node_property_object& _npo; + uint32_t _old_skip_flags; +}; + +/** + * Class used to help the without_pending_transactions + * implementation. + */ +struct pending_transactions_restorer +{ + pending_transactions_restorer( database& db, std::vector&& pending_transactions ) + : _db(db), _pending_transactions( std::move(pending_transactions) ) + { + _db.clear_pending(); + } + + ~pending_transactions_restorer() + { + for( const processed_transaction& tx : _pending_transactions ) + { + try + { + // since push_transaction() takes a signed_transaction, + // the operation_results field will be ignored. + _db.push_transaction( tx ); + } + catch( const fc::exception& e ) + { + wlog( "Pending transaction became invalid after switching to block ${b}", ("b", _db.head_block_id()) ); + wlog( "The invalid pending transaction is ${t}", ("t", tx) ); + wlog( "The invalid pending transaction caused exception ${e}", ("e", e) ); + } + } + } + + database& _db; + std::vector< processed_transaction > _pending_transactions; +}; + +/** + * Set the skip_flags to the given value, call callback, + * then reset skip_flags to their previous value after + * callback is done. + */ +template< typename Lambda > +void with_skip_flags( + database& db, + uint32_t skip_flags, + Lambda callback ) +{ + node_property_object& npo = db.node_properties(); + skip_flags_restorer restorer( npo, npo.skip_flags ); + npo.skip_flags = skip_flags; + callback(); + return; +} + +/** + * Empty pending_transactions, call callback, + * then reset pending_transactions after callback is done. + * + * Pending transactions which no longer validate will be culled. + */ +template< typename Lambda > +void without_pending_transactions( + database& db, + std::vector&& pending_transactions, + Lambda callback ) +{ + pending_transactions_restorer restorer( db, std::move(pending_transactions) ); + callback(); + return; +} + +} } } // graphene::chain::detail diff --git a/libraries/net/node.cpp b/libraries/net/node.cpp index 124e0e13..6a8ef753 100644 --- a/libraries/net/node.cpp +++ b/libraries/net/node.cpp @@ -1199,9 +1199,9 @@ namespace graphene { namespace net { namespace detail { wdump((inventory_to_advertise)); for (const item_id& item_to_advertise : inventory_to_advertise) { - if (peer->inventory_advertised_to_peer.find(item_to_advertise) == peer->inventory_advertised_to_peer.end() ) + if (peer->inventory_advertised_to_peer.find(item_to_advertise) != peer->inventory_advertised_to_peer.end() ) wdump((*peer->inventory_advertised_to_peer.find(item_to_advertise))); - if (peer->inventory_peer_advertised_to_us.find(item_to_advertise) == peer->inventory_peer_advertised_to_us.end() ) + if (peer->inventory_peer_advertised_to_us.find(item_to_advertise) != peer->inventory_peer_advertised_to_us.end() ) wdump((*peer->inventory_peer_advertised_to_us.find(item_to_advertise))); if (peer->inventory_advertised_to_peer.find(item_to_advertise) == peer->inventory_advertised_to_peer.end() && diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 09d7ee29..575db736 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -648,6 +648,17 @@ public: FC_THROW( "Wallet chain ID does not match", ("wallet.chain_id", _wallet.chain_id) ("chain_id", _chain_id) ); + + vector< account_id_type > my_account_ids; + my_account_ids.reserve( _wallet.my_accounts.size() ); + for( const account_object& acct : _wallet.my_accounts ) + my_account_ids.push_back( acct.id ); + // TODO: Batch requests using _remote_db->get_accounts() + // to reduce number of round-trips. Remember get_accounts() has + // a limit of 100 results per call! + for( const account_id_type& acct_id : my_account_ids ) + _wallet.update_account( get_account( acct_id ) ); + return true; } void save_wallet_file(string wallet_filename = "") diff --git a/tests/tests/block_tests.cpp b/tests/tests/block_tests.cpp index 2504e7ce..4a904390 100644 --- a/tests/tests/block_tests.cpp +++ b/tests/tests/block_tests.cpp @@ -127,12 +127,14 @@ BOOST_AUTO_TEST_CASE( generate_empty_blocks ) // TODO: Don't generate this here auto init_account_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")) ); + signed_block b200; { database db; db.open(data_dir.path(), make_genesis ); b = db.generate_block(db.get_slot_time(1), db.get_scheduled_witness(1), init_account_priv_key, database::skip_nothing); - for( uint32_t i = 1; i < 200; ++i ) + // n.b. we generate GRAPHENE_MIN_UNDO_HISTORY+1 extra blocks which will be discarded on save + for( uint32_t i = 1; i < 200+GRAPHENE_MIN_UNDO_HISTORY+1; ++i ) { BOOST_CHECK( db.head_block_id() == b.id() ); witness_id_type prev_witness = b.witness; @@ -140,6 +142,8 @@ BOOST_AUTO_TEST_CASE( generate_empty_blocks ) BOOST_CHECK( cur_witness != prev_witness ); b = db.generate_block(db.get_slot_time(1), cur_witness, init_account_priv_key, database::skip_nothing); BOOST_CHECK( b.witness == cur_witness ); + if( i == 199 ) + b200 = b; } db.close(); } @@ -147,6 +151,7 @@ BOOST_AUTO_TEST_CASE( generate_empty_blocks ) database db; db.open(data_dir.path(), []{return genesis_state_type();}); BOOST_CHECK_EQUAL( db.head_block_num(), 200 ); + b = b200; for( uint32_t i = 0; i < 200; ++i ) { BOOST_CHECK( db.head_block_id() == b.id() ); @@ -1018,4 +1023,157 @@ BOOST_FIXTURE_TEST_CASE( rsf_missed_blocks, database_fixture ) FC_LOG_AND_RETHROW() } +BOOST_FIXTURE_TEST_CASE( transaction_invalidated_in_cache, database_fixture ) +{ + try + { + ACTORS( (alice)(bob) ); + + auto generate_block = [&]( database& d ) -> signed_block + { + return d.generate_block(d.get_slot_time(1), d.get_scheduled_witness(1), init_account_priv_key, database::skip_nothing); + }; + + wdump( (db.fetch_block_by_number(1)) ); + wdump( (db.fetch_block_by_id( db.head_block_id() ) ) ); + + signed_block b1 = generate_block(db); + wdump( (db.fetch_block_by_number(1)) ); + wdump( (db.fetch_block_by_id( db.head_block_id() ) ) ); + + fc::temp_directory data_dir2( graphene::utilities::temp_directory_path() ); + + database db2; + db2.open(data_dir2.path(), make_genesis); + BOOST_CHECK( db.get_chain_id() == db2.get_chain_id() ); + + while( db2.head_block_num() < db.head_block_num() ) + { + wdump( (db.head_block_num()) (db2.head_block_num()) ); + optional< signed_block > b = db.fetch_block_by_number( db2.head_block_num()+1 ); + db2.push_block(*b, database::skip_witness_signature); + } + wlog("caught up db2 to db"); + BOOST_CHECK( db2.get( alice_id ).name == "alice" ); + BOOST_CHECK( db2.get( bob_id ).name == "bob" ); + + db2.push_block(generate_block(db)); + transfer( account_id_type(), alice_id, asset( 1000 ) ); + transfer( account_id_type(), bob_id, asset( 1000 ) ); + db2.push_block(generate_block(db)); + + BOOST_CHECK_EQUAL(db.get_balance(alice_id, asset_id_type()).amount.value, 1000); + BOOST_CHECK_EQUAL(db.get_balance( bob_id, asset_id_type()).amount.value, 1000); + BOOST_CHECK_EQUAL(db2.get_balance(alice_id, asset_id_type()).amount.value, 1000); + BOOST_CHECK_EQUAL(db2.get_balance( bob_id, asset_id_type()).amount.value, 1000); + + auto generate_and_send = [&]( int n ) + { + for( int i=0; i signed_transaction + { + signed_transaction tx; + transfer_operation xfer_op; + xfer_op.from = from; + xfer_op.to = to; + xfer_op.amount = asset( amount, asset_id_type() ); + xfer_op.fee = asset( 0, asset_id_type() ); + tx.operations.push_back( xfer_op ); + tx.set_expiration( db.head_block_time() + blocks_to_expire * db.get_global_properties().parameters.block_interval ); + if( from == alice_id ) + sign( tx, alice_private_key ); + else + sign( tx, bob_private_key ); + return tx; + }; + + signed_transaction tx = generate_xfer_tx( alice_id, bob_id, 1000, 2 ); + tx.set_expiration( db.head_block_time() + 2 * db.get_global_properties().parameters.block_interval ); + tx.signatures.clear(); + sign( tx, alice_private_key ); + // put the tx in db tx cache + PUSH_TX( db, tx ); + + BOOST_CHECK_EQUAL(db.get_balance(alice_id, asset_id_type()).amount.value, 0); + BOOST_CHECK_EQUAL(db.get_balance( bob_id, asset_id_type()).amount.value, 2000); + + // generate some blocks with db2, make tx expire in db's cache + generate_and_send(3); + + BOOST_CHECK_EQUAL(db.get_balance(alice_id, asset_id_type()).amount.value, 1000); + BOOST_CHECK_EQUAL(db.get_balance( bob_id, asset_id_type()).amount.value, 1000); + + // generate a block with db and ensure we don't somehow apply it + PUSH_BLOCK(db2, generate_block(db)); + BOOST_CHECK_EQUAL(db.get_balance(alice_id, asset_id_type()).amount.value, 1000); + BOOST_CHECK_EQUAL(db.get_balance( bob_id, asset_id_type()).amount.value, 1000); + + // now the tricky part... + // (A) Bob sends 1000 to Alice + // (B) Alice sends 2000 to Bob + // (C) Alice sends 500 to Bob + // + // We push AB, then receive a block containing C. + // we need to apply the block, then invalidate B in the cache. + // AB results in Alice having 0, Bob having 2000. + // C results in Alice having 500, Bob having 1500. + // + // This needs to occur while switching to a fork. + // + + signed_transaction tx_a = generate_xfer_tx( bob_id, alice_id, 1000, 3 ); + signed_transaction tx_b = generate_xfer_tx( alice_id, bob_id, 2000 ); + signed_transaction tx_c = generate_xfer_tx( alice_id, bob_id, 500 ); + + generate_block( db ); + + PUSH_TX( db, tx_a ); + BOOST_CHECK_EQUAL(db.get_balance(alice_id, asset_id_type()).amount.value, 2000); + BOOST_CHECK_EQUAL(db.get_balance( bob_id, asset_id_type()).amount.value, 0); + + PUSH_TX( db, tx_b ); + PUSH_TX( db2, tx_c ); + + BOOST_CHECK_EQUAL(db.get_balance(alice_id, asset_id_type()).amount.value, 0); + BOOST_CHECK_EQUAL(db.get_balance( bob_id, asset_id_type()).amount.value, 2000); + + BOOST_CHECK_EQUAL(db2.get_balance(alice_id, asset_id_type()).amount.value, 500); + BOOST_CHECK_EQUAL(db2.get_balance( bob_id, asset_id_type()).amount.value, 1500); + + // generate enough blocks on db2 to cause db to switch forks + generate_and_send(2); + + // db should invalidate B, but still be applying A, so the states don't agree + + BOOST_CHECK_EQUAL(db.get_balance(alice_id, asset_id_type()).amount.value, 1500); + BOOST_CHECK_EQUAL(db.get_balance( bob_id, asset_id_type()).amount.value, 500); + + BOOST_CHECK_EQUAL(db2.get_balance(alice_id, asset_id_type()).amount.value, 500); + BOOST_CHECK_EQUAL(db2.get_balance( bob_id, asset_id_type()).amount.value, 1500); + + // This will cause A to expire in db + generate_and_send(1); + + BOOST_CHECK_EQUAL(db.get_balance(alice_id, asset_id_type()).amount.value, 500); + BOOST_CHECK_EQUAL(db.get_balance( bob_id, asset_id_type()).amount.value, 1500); + + BOOST_CHECK_EQUAL(db2.get_balance(alice_id, asset_id_type()).amount.value, 500); + BOOST_CHECK_EQUAL(db2.get_balance( bob_id, asset_id_type()).amount.value, 1500); + + // Make sure we can generate and accept a plain old empty block on top of all this! + generate_and_send(1); + } + catch (fc::exception& e) + { + edump((e.to_detail_string())); + throw; + } +} + BOOST_AUTO_TEST_SUITE_END()