Skip to content

Commit cfd9dd0

Browse files
app_test: Rewrite broken two_node_network test
1 parent da254cd commit cfd9dd0

File tree

1 file changed

+67
-22
lines changed

1 file changed

+67
-22
lines changed

tests/app/main.cpp

+67-22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <graphene/app/application.hpp>
1919
#include <graphene/app/plugin.hpp>
2020

21+
#include <graphene/chain/balance_object.hpp>
22+
2123
#include <graphene/time/time.hpp>
2224

2325
#include <graphene/utilities/tempdir.hpp>
@@ -38,19 +40,22 @@ BOOST_AUTO_TEST_CASE( two_node_network )
3840
using namespace graphene::chain;
3941
using namespace graphene::app;
4042
try {
43+
BOOST_TEST_MESSAGE( "Creating temporary files" );
44+
4145
fc::temp_directory app_dir( graphene::utilities::temp_directory_path() );
4246
fc::temp_directory app2_dir( graphene::utilities::temp_directory_path() );
4347
fc::temp_file genesis_json;
4448

45-
// TODO: Time should be read from the blockchain
46-
fc::time_point_sec now( 1431700000 );
49+
BOOST_TEST_MESSAGE( "Creating and initializing app1" );
4750

4851
graphene::app::application app1;
4952
app1.register_plugin<graphene::account_history::account_history_plugin>();
5053
boost::program_options::variables_map cfg;
5154
cfg.emplace("p2p-endpoint", boost::program_options::variable_value(string("127.0.0.1:3939"), false));
5255
app1.initialize(app_dir.path(), cfg);
5356

57+
BOOST_TEST_MESSAGE( "Creating and initializing app2" );
58+
5459
graphene::app::application app2;
5560
app2.register_plugin<account_history::account_history_plugin>();
5661
auto cfg2 = cfg;
@@ -59,42 +64,82 @@ BOOST_AUTO_TEST_CASE( two_node_network )
5964
cfg2.emplace("seed-node", boost::program_options::variable_value(vector<string>{"127.0.0.1:3939"}, false));
6065
app2.initialize(app2_dir.path(), cfg2);
6166

62-
app1.startup();
67+
BOOST_TEST_MESSAGE( "Starting app1 and waiting 500 ms" );
68+
app1.startup();
69+
fc::usleep(fc::milliseconds(500));
70+
BOOST_TEST_MESSAGE( "Starting app2 and waiting 500 ms" );
6371
app2.startup();
6472
fc::usleep(fc::milliseconds(500));
6573

6674
BOOST_REQUIRE_EQUAL(app1.p2p_node()->get_connection_count(), 1);
6775
BOOST_CHECK_EQUAL(std::string(app1.p2p_node()->get_connected_peers().front().host.get_address()), "127.0.0.1");
68-
ilog("Connected!");
76+
BOOST_TEST_MESSAGE( "app1 and app2 successfully connected" );
6977

70-
fc::ecc::private_key nathan_key = fc::ecc::private_key::generate();
71-
fc::ecc::private_key committee_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan")));
72-
graphene::chain::signed_transaction trx;
73-
trx.set_expiration(now + fc::seconds(30));
78+
std::shared_ptr<chain::database> db1 = app1.chain_database();
7479
std::shared_ptr<chain::database> db2 = app2.chain_database();
7580

76-
assert_operation op;
77-
op.fee_paying_account = GRAPHENE_TEMP_ACCOUNT;
78-
op.predicates.push_back( graphene::chain::asset_symbol_eq_lit_predicate{ asset_id_type(), "CORE" } );
79-
80-
trx.operations.push_back( std::move( op ) );
81+
BOOST_CHECK_EQUAL( db1->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 0 );
82+
BOOST_CHECK_EQUAL( db2->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 0 );
8183

82-
trx.validate();
83-
processed_transaction ptrx = app1.chain_database()->push_transaction(trx);
84+
BOOST_TEST_MESSAGE( "Creating transfer tx" );
85+
graphene::chain::signed_transaction trx;
86+
{
87+
account_id_type nathan_id = db2->get_index_type<account_index>().indices().get<by_name>().find( "nathan" )->id;
88+
fc::ecc::private_key nathan_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan")));
89+
90+
balance_claim_operation claim_op;
91+
balance_id_type bid = balance_id_type();
92+
claim_op.deposit_to_account = nathan_id;
93+
claim_op.balance_to_claim = bid;
94+
claim_op.balance_owner_key = nathan_key.get_public_key();
95+
claim_op.total_claimed = bid(*db1).balance;
96+
trx.operations.push_back( claim_op );
97+
db1->current_fee_schedule().set_fee( trx.operations.back() );
98+
99+
transfer_operation xfer_op;
100+
xfer_op.from = nathan_id;
101+
xfer_op.to = GRAPHENE_NULL_ACCOUNT;
102+
xfer_op.amount = asset( 1000000 );
103+
trx.operations.push_back( xfer_op );
104+
db1->current_fee_schedule().set_fee( trx.operations.back() );
105+
106+
trx.set_expiration( db1->get_slot_time( 10 ) );
107+
trx.sign( nathan_key );
108+
trx.validate();
109+
}
110+
111+
BOOST_TEST_MESSAGE( "Pushing tx locally on db1" );
112+
processed_transaction ptrx = db1->push_transaction(trx);
113+
114+
BOOST_CHECK_EQUAL( db1->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 1000000 );
115+
BOOST_CHECK_EQUAL( db2->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 0 );
116+
117+
BOOST_TEST_MESSAGE( "Broadcasting tx" );
84118
app1.p2p_node()->broadcast(graphene::net::trx_message(trx));
85119

86-
fc::usleep(fc::milliseconds(250));
87-
ilog("Pushed transaction");
120+
fc::usleep(fc::milliseconds(500));
121+
122+
BOOST_CHECK_EQUAL( db1->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 1000000 );
123+
BOOST_CHECK_EQUAL( db2->get_balance( GRAPHENE_NULL_ACCOUNT, asset_id_type() ).amount.value, 1000000 );
88124

89-
now += GRAPHENE_DEFAULT_BLOCK_INTERVAL;
90-
app2.p2p_node()->broadcast(graphene::net::block_message(db2->generate_block(now,
91-
db2->get_scheduled_witness(1).first,
92-
committee_key,
93-
database::skip_nothing)));
125+
BOOST_TEST_MESSAGE( "Generating block on db2" );
126+
fc::ecc::private_key committee_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan")));
127+
128+
auto block_1 = db2->generate_block(
129+
db2->get_slot_time(1),
130+
db2->get_scheduled_witness(1).first,
131+
committee_key,
132+
database::skip_nothing);
133+
134+
BOOST_TEST_MESSAGE( "Broadcasting block" );
135+
app2.p2p_node()->broadcast(graphene::net::block_message( block_1 ));
94136

95137
fc::usleep(fc::milliseconds(500));
138+
BOOST_TEST_MESSAGE( "Verifying nodes are still connected" );
96139
BOOST_CHECK_EQUAL(app1.p2p_node()->get_connection_count(), 1);
97140
BOOST_CHECK_EQUAL(app1.chain_database()->head_block_num(), 1);
141+
142+
BOOST_TEST_MESSAGE( "Checking GRAPHENE_NULL_ACCOUNT has balance" );
98143
} catch( fc::exception& e ) {
99144
edump((e.to_detail_string()));
100145
throw;

0 commit comments

Comments
 (0)