2015-06-08 15:50:35 +00:00
|
|
|
/*
|
2015-10-12 17:48:40 +00:00
|
|
|
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
|
|
|
|
|
*
|
2016-01-06 09:51:18 +00:00
|
|
|
* The MIT License
|
2015-10-12 17:48:40 +00:00
|
|
|
*
|
2016-01-06 09:51:18 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2015-10-12 17:48:40 +00:00
|
|
|
*
|
2016-01-06 09:51:18 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
2015-10-12 17:02:59 +00:00
|
|
|
*
|
2016-01-06 09:51:18 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
2015-06-08 15:50:35 +00:00
|
|
|
*/
|
|
|
|
|
#include <graphene/app/application.hpp>
|
|
|
|
|
#include <graphene/app/plugin.hpp>
|
|
|
|
|
|
2015-07-14 21:43:29 +00:00
|
|
|
#include <graphene/chain/balance_object.hpp>
|
|
|
|
|
|
2015-07-13 18:56:29 +00:00
|
|
|
#include <graphene/utilities/tempdir.hpp>
|
|
|
|
|
|
2020-03-06 19:15:51 +00:00
|
|
|
#include <graphene/witness/witness.hpp>
|
2015-06-08 15:50:35 +00:00
|
|
|
#include <graphene/account_history/account_history_plugin.hpp>
|
2020-03-06 19:15:51 +00:00
|
|
|
#include <graphene/bookie/bookie_plugin.hpp>
|
|
|
|
|
#include <graphene/accounts_list/accounts_list_plugin.hpp>
|
|
|
|
|
#include <graphene/affiliate_stats/affiliate_stats_plugin.hpp>
|
|
|
|
|
#include <graphene/market_history/market_history_plugin.hpp>
|
2015-06-08 15:50:35 +00:00
|
|
|
#include <fc/thread/thread.hpp>
|
|
|
|
|
|
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
|
|
2019-06-14 17:26:42 +00:00
|
|
|
#include "../common/genesis_file_util.hpp"
|
|
|
|
|
|
2015-06-08 15:50:35 +00:00
|
|
|
#define BOOST_TEST_MODULE Test Application
|
|
|
|
|
#include <boost/test/included/unit_test.hpp>
|
|
|
|
|
|
|
|
|
|
using namespace graphene;
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE( two_node_network )
|
|
|
|
|
{
|
|
|
|
|
using namespace graphene::chain;
|
|
|
|
|
using namespace graphene::app;
|
|
|
|
|
try {
|
2015-07-14 21:43:29 +00:00
|
|
|
BOOST_TEST_MESSAGE( "Creating temporary files" );
|
|
|
|
|
|
2015-07-13 18:56:29 +00:00
|
|
|
fc::temp_directory app_dir( graphene::utilities::temp_directory_path() );
|
|
|
|
|
fc::temp_directory app2_dir( graphene::utilities::temp_directory_path() );
|
2015-06-08 15:50:35 +00:00
|
|
|
fc::temp_file genesis_json;
|
|
|
|
|
|
2015-07-14 21:43:29 +00:00
|
|
|
BOOST_TEST_MESSAGE( "Creating and initializing app1" );
|
2015-06-08 15:50:35 +00:00
|
|
|
|
|
|
|
|
graphene::app::application app1;
|
2019-12-08 06:43:05 +00:00
|
|
|
app1.register_plugin<graphene::witness_plugin::witness_plugin>();
|
2015-06-08 15:50:35 +00:00
|
|
|
app1.register_plugin<graphene::account_history::account_history_plugin>();
|
2020-03-06 19:15:51 +00:00
|
|
|
app1.register_plugin<graphene::bookie::bookie_plugin>();
|
|
|
|
|
app1.register_plugin<graphene::accounts_list::accounts_list_plugin>();
|
|
|
|
|
app1.register_plugin<graphene::affiliate_stats::affiliate_stats_plugin>();
|
2019-12-08 06:43:05 +00:00
|
|
|
app1.register_plugin<graphene::market_history::market_history_plugin>();
|
2020-03-06 19:15:51 +00:00
|
|
|
|
2015-06-24 02:12:07 +00:00
|
|
|
boost::program_options::variables_map cfg;
|
2019-09-27 14:58:49 +00:00
|
|
|
cfg.emplace("p2p-endpoint", boost::program_options::variable_value(string("127.0.0.1:0"), false));
|
2020-03-27 17:46:30 +00:00
|
|
|
cfg.emplace("plugins", boost::program_options::variable_value(string(" "), false));
|
2015-06-08 15:50:35 +00:00
|
|
|
app1.initialize(app_dir.path(), cfg);
|
2019-09-27 14:58:49 +00:00
|
|
|
cfg.emplace("genesis-json", boost::program_options::variable_value(create_genesis_file(app_dir), false));
|
|
|
|
|
|
|
|
|
|
BOOST_TEST_MESSAGE( "Starting app1 and waiting 1500 ms" );
|
|
|
|
|
app1.startup();
|
|
|
|
|
fc::usleep(fc::milliseconds(500));
|
|
|
|
|
string endpoint1 = app1.p2p_node()->get_actual_listening_endpoint();
|
2015-06-08 15:50:35 +00:00
|
|
|
|
2015-07-14 21:43:29 +00:00
|
|
|
BOOST_TEST_MESSAGE( "Creating and initializing app2" );
|
2019-09-27 14:58:49 +00:00
|
|
|
auto cfg2 = cfg;
|
2015-07-14 21:43:29 +00:00
|
|
|
|
2015-06-08 15:50:35 +00:00
|
|
|
graphene::app::application app2;
|
2019-12-08 06:43:05 +00:00
|
|
|
app2.register_plugin<graphene::witness_plugin::witness_plugin>();
|
2020-03-06 19:15:51 +00:00
|
|
|
app2.register_plugin<graphene::account_history::account_history_plugin>();
|
|
|
|
|
app2.register_plugin<graphene::bookie::bookie_plugin>();
|
|
|
|
|
app2.register_plugin<graphene::accounts_list::accounts_list_plugin>();
|
|
|
|
|
app2.register_plugin<graphene::affiliate_stats::affiliate_stats_plugin>();
|
2019-12-08 06:43:05 +00:00
|
|
|
app2.register_plugin<graphene::market_history::market_history_plugin>();
|
2015-06-08 15:50:35 +00:00
|
|
|
cfg2.erase("p2p-endpoint");
|
2019-09-27 14:58:49 +00:00
|
|
|
cfg2.emplace("p2p-endpoint", boost::program_options::variable_value(string("127.0.0.1:0"), false));
|
|
|
|
|
cfg2.emplace("seed-node", boost::program_options::variable_value(vector<string>{endpoint1}, false));
|
2015-06-08 15:50:35 +00:00
|
|
|
app2.initialize(app2_dir.path(), cfg2);
|
2019-06-14 17:26:42 +00:00
|
|
|
cfg2.emplace("genesis-json", boost::program_options::variable_value(create_genesis_file(app2_dir), false));
|
|
|
|
|
|
2019-08-29 11:03:38 +00:00
|
|
|
BOOST_TEST_MESSAGE( "Starting app2 and waiting 1500 ms" );
|
2015-06-08 15:50:35 +00:00
|
|
|
app2.startup();
|
2019-09-27 14:58:49 +00:00
|
|
|
int counter = 0;
|
|
|
|
|
while(!app2.p2p_node()->is_connected())
|
|
|
|
|
{
|
|
|
|
|
fc::usleep(fc::milliseconds(500));
|
|
|
|
|
if(counter++ >= 100)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-06-08 15:50:35 +00:00
|
|
|
|
2015-06-19 14:55:07 +00:00
|
|
|
BOOST_REQUIRE_EQUAL(app1.p2p_node()->get_connection_count(), 1);
|
2015-06-08 15:50:35 +00:00
|
|
|
BOOST_CHECK_EQUAL(std::string(app1.p2p_node()->get_connected_peers().front().host.get_address()), "127.0.0.1");
|
2015-07-14 21:43:29 +00:00
|
|
|
BOOST_TEST_MESSAGE( "app1 and app2 successfully connected" );
|
2015-06-08 15:50:35 +00:00
|
|
|
|
2015-07-14 21:43:29 +00:00
|
|
|
std::shared_ptr<chain::database> db1 = app1.chain_database();
|
2015-06-08 15:50:35 +00:00
|
|
|
std::shared_ptr<chain::database> db2 = app2.chain_database();
|
|
|
|
|
|
2015-07-14 21:43:29 +00:00
|
|
|
BOOST_CHECK_EQUAL( db1->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 0 );
|
|
|
|
|
BOOST_CHECK_EQUAL( db2->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 0 );
|
2015-07-02 16:58:21 +00:00
|
|
|
|
2015-07-14 21:43:29 +00:00
|
|
|
BOOST_TEST_MESSAGE( "Creating transfer tx" );
|
|
|
|
|
graphene::chain::signed_transaction trx;
|
|
|
|
|
{
|
|
|
|
|
account_id_type nathan_id = db2->get_index_type<account_index>().indices().get<by_name>().find( "nathan" )->id;
|
|
|
|
|
fc::ecc::private_key nathan_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan")));
|
|
|
|
|
|
|
|
|
|
balance_claim_operation claim_op;
|
|
|
|
|
balance_id_type bid = balance_id_type();
|
|
|
|
|
claim_op.deposit_to_account = nathan_id;
|
|
|
|
|
claim_op.balance_to_claim = bid;
|
|
|
|
|
claim_op.balance_owner_key = nathan_key.get_public_key();
|
|
|
|
|
claim_op.total_claimed = bid(*db1).balance;
|
|
|
|
|
trx.operations.push_back( claim_op );
|
|
|
|
|
db1->current_fee_schedule().set_fee( trx.operations.back() );
|
|
|
|
|
|
|
|
|
|
transfer_operation xfer_op;
|
|
|
|
|
xfer_op.from = nathan_id;
|
|
|
|
|
xfer_op.to = GRAPHENE_NULL_ACCOUNT;
|
|
|
|
|
xfer_op.amount = asset( 1000000 );
|
|
|
|
|
trx.operations.push_back( xfer_op );
|
|
|
|
|
db1->current_fee_schedule().set_fee( trx.operations.back() );
|
|
|
|
|
|
|
|
|
|
trx.set_expiration( db1->get_slot_time( 10 ) );
|
2015-08-06 16:41:45 +00:00
|
|
|
trx.sign( nathan_key, db1->get_chain_id() );
|
2015-07-14 21:43:29 +00:00
|
|
|
trx.validate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_TEST_MESSAGE( "Pushing tx locally on db1" );
|
|
|
|
|
processed_transaction ptrx = db1->push_transaction(trx);
|
|
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL( db1->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 1000000 );
|
|
|
|
|
BOOST_CHECK_EQUAL( db2->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 0 );
|
|
|
|
|
|
|
|
|
|
BOOST_TEST_MESSAGE( "Broadcasting tx" );
|
2015-06-08 15:50:35 +00:00
|
|
|
app1.p2p_node()->broadcast(graphene::net::trx_message(trx));
|
|
|
|
|
|
2015-07-14 21:43:29 +00:00
|
|
|
fc::usleep(fc::milliseconds(500));
|
|
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL( db1->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 1000000 );
|
|
|
|
|
BOOST_CHECK_EQUAL( db2->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 1000000 );
|
2015-06-08 15:50:35 +00:00
|
|
|
|
2015-07-14 21:43:29 +00:00
|
|
|
BOOST_TEST_MESSAGE( "Generating block on db2" );
|
|
|
|
|
fc::ecc::private_key committee_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan")));
|
|
|
|
|
|
|
|
|
|
auto block_1 = db2->generate_block(
|
|
|
|
|
db2->get_slot_time(1),
|
2015-08-25 18:46:56 +00:00
|
|
|
db2->get_scheduled_witness(1),
|
2015-07-14 21:43:29 +00:00
|
|
|
committee_key,
|
|
|
|
|
database::skip_nothing);
|
|
|
|
|
|
|
|
|
|
BOOST_TEST_MESSAGE( "Broadcasting block" );
|
|
|
|
|
app2.p2p_node()->broadcast(graphene::net::block_message( block_1 ));
|
2015-06-08 15:50:35 +00:00
|
|
|
|
|
|
|
|
fc::usleep(fc::milliseconds(500));
|
2015-07-14 21:43:29 +00:00
|
|
|
BOOST_TEST_MESSAGE( "Verifying nodes are still connected" );
|
2015-06-08 15:50:35 +00:00
|
|
|
BOOST_CHECK_EQUAL(app1.p2p_node()->get_connection_count(), 1);
|
|
|
|
|
BOOST_CHECK_EQUAL(app1.chain_database()->head_block_num(), 1);
|
2015-07-14 21:43:29 +00:00
|
|
|
|
|
|
|
|
BOOST_TEST_MESSAGE( "Checking GRAPHENE_NULL_ACCOUNT has balance" );
|
2015-06-08 15:50:35 +00:00
|
|
|
} catch( fc::exception& e ) {
|
|
|
|
|
edump((e.to_detail_string()));
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|