From 823adbbed588a47d1da2401bf980674f2e211902 Mon Sep 17 00:00:00 2001 From: theoreticalbts Date: Thu, 27 Aug 2015 15:46:43 -0400 Subject: [PATCH] generate_empty_blocks: Create binary to generate many blocks for testing --- tests/CMakeLists.txt | 2 + tests/generate_empty_blocks/CMakeLists.txt | 15 ++ tests/generate_empty_blocks/main.cpp | 169 +++++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 tests/generate_empty_blocks/CMakeLists.txt create mode 100644 tests/generate_empty_blocks/main.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b30e4357..b03d58a8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -28,3 +28,5 @@ target_link_libraries( app_test graphene_app graphene_account_history graphene_n file(GLOB INTENSE_SOURCES "intense/*.cpp") add_executable( intense_test ${INTENSE_SOURCES} ${COMMON_SOURCES} ) target_link_libraries( intense_test graphene_chain graphene_app graphene_account_history graphene_egenesis_none fc ${PLATFORM_SPECIFIC_LIBS} ) + +add_subdirectory( generate_empty_blocks ) diff --git a/tests/generate_empty_blocks/CMakeLists.txt b/tests/generate_empty_blocks/CMakeLists.txt new file mode 100644 index 00000000..af53ee91 --- /dev/null +++ b/tests/generate_empty_blocks/CMakeLists.txt @@ -0,0 +1,15 @@ +add_executable( generate_empty_blocks main.cpp ) +if( UNIX AND NOT APPLE ) + set(rt_library rt ) +endif() + +target_link_libraries( generate_empty_blocks + PRIVATE graphene_app graphene_chain graphene_egenesis_none fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} ) + +install( TARGETS + generate_empty_blocks + + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib +) diff --git a/tests/generate_empty_blocks/main.cpp b/tests/generate_empty_blocks/main.cpp new file mode 100644 index 00000000..d022cb94 --- /dev/null +++ b/tests/generate_empty_blocks/main.cpp @@ -0,0 +1,169 @@ +/* + * 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. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#ifndef WIN32 +#include +#endif + +using namespace graphene::app; +using namespace graphene::chain; +using namespace graphene::utilities; +using namespace std; +namespace bpo = boost::program_options; + +// hack: import create_example_genesis() even though it's a way, way +// specific internal detail +namespace graphene { namespace app { namespace detail { +genesis_state_type create_example_genesis(); +} } } // graphene::app::detail + +int main( int argc, char** argv ) +{ + try + { + bpo::options_description cli_options("Graphene empty blocks"); + cli_options.add_options() + ("help,h", "Print this help message and exit.") + ("data-dir", bpo::value()->default_value("empty_blocks_data_dir"), "Directory containing generator database") + ("genesis-json,g", bpo::value(), "File to read genesis state from") + ("genesis-time,t", bpo::value()->default_value(0), "Timestamp for genesis state (0=use value from file/example)") + ("num-blocks,n", bpo::value()->default_value(1000000), "Number of blocks to generate") + ("miss-rate,r", bpo::value()->default_value(3), "Percentage of blocks to miss") + ("verbose,v", "Enter verbose mode") + ; + + bpo::variables_map options; + try + { + boost::program_options::store( boost::program_options::parse_command_line(argc, argv, cli_options), options ); + } + catch (const boost::program_options::error& e) + { + std::cerr << "empty_blocks: error parsing command line: " << e.what() << "\n"; + return 1; + } + + if( options.count("help") ) + { + std::cout << cli_options << "\n"; + return 0; + } + + fc::path data_dir; + if( options.count("data-dir") ) + { + data_dir = options["data-dir"].as(); + if( data_dir.is_relative() ) + data_dir = fc::current_path() / data_dir; + } + + genesis_state_type genesis; + if( options.count("genesis-json") ) + { + fc::path genesis_json_filename = options["genesis-json"].as(); + std::cerr << "embed_genesis: Reading genesis from file " << genesis_json_filename.preferred_string() << "\n"; + std::string genesis_json; + read_file_contents( genesis_json_filename, genesis_json ); + genesis = fc::json::from_string( genesis_json ).as< genesis_state_type >(); + } + else + genesis = graphene::app::detail::create_example_genesis(); + uint32_t timestamp = options["genesis-time"].as(); + if( timestamp != 0 ) + { + genesis.initial_timestamp = fc::time_point_sec( timestamp ); + std::cerr << "embed_genesis: Genesis timestamp is " << genesis.initial_timestamp.sec_since_epoch() << " (from CLI)\n"; + } + else + std::cerr << "embed_genesis: Genesis timestamp is " << genesis.initial_timestamp.sec_since_epoch() << " (from state)\n"; + bool verbose = (options.count("verbose") != 0); + + uint32_t num_blocks = options["num-blocks"].as(); + uint32_t miss_rate = options["miss-rate"].as(); + + fc::ecc::private_key init_account_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")) ); + fc::ecc::private_key nathan_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan"))); + + database db; + fc::path db_path = data_dir / "db"; + db.open(db_path, [&]() { return genesis; } ); + + uint32_t slot = 1; + uint32_t missed = 0; + + for( uint32_t i = 1; i < num_blocks; ++i ) + { + signed_block b = db.generate_block(db.get_slot_time(slot), db.get_scheduled_witness(slot), nathan_priv_key, database::skip_nothing); + FC_ASSERT( db.head_block_id() == b.id() ); + fc::sha256 h = b.digest(); + uint64_t rand = h._hash[0]; + slot = 1; + while(true) + { + if( (rand % 100) < miss_rate ) + { + slot++; + rand = (rand/100) ^ h._hash[slot&3]; + missed++; + } + else + break; + } + + witness_id_type prev_witness = b.witness; + witness_id_type cur_witness = db.get_scheduled_witness(1); + if( verbose ) + { + wdump( (prev_witness)(cur_witness) ); + } + else if( (i%10000) == 0 ) + { + std::cerr << "\rblock #" << i << " missed " << missed; + } + if( slot == 1 ) // can possibly get consecutive production if block missed + { + FC_ASSERT( cur_witness != prev_witness ); + } + } + std::cerr << "\n"; + db.close(); + } + catch ( const fc::exception& e ) + { + std::cout << e.to_detail_string() << "\n"; + return 1; + } + return 0; +}