169 lines
6.5 KiB
C++
169 lines
6.5 KiB
C++
/*
|
|
* 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 <algorithm>
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
#include <iterator>
|
|
|
|
#include <fc/io/fstream.hpp>
|
|
#include <fc/io/json.hpp>
|
|
#include <fc/io/stdio.hpp>
|
|
#include <fc/smart_ref_impl.hpp>
|
|
|
|
#include <graphene/app/api.hpp>
|
|
#include <graphene/chain/protocol/protocol.hpp>
|
|
#include <graphene/egenesis/egenesis.hpp>
|
|
#include <graphene/utilities/key_conversion.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#ifndef WIN32
|
|
#include <csignal>
|
|
#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<boost::filesystem::path>()->default_value("empty_blocks_data_dir"), "Directory containing generator database")
|
|
("genesis-json,g", bpo::value<boost::filesystem::path>(), "File to read genesis state from")
|
|
("genesis-time,t", bpo::value<uint32_t>()->default_value(0), "Timestamp for genesis state (0=use value from file/example)")
|
|
("num-blocks,n", bpo::value<uint32_t>()->default_value(1000000), "Number of blocks to generate")
|
|
("miss-rate,r", bpo::value<uint32_t>()->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<boost::filesystem::path>();
|
|
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<boost::filesystem::path>();
|
|
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<uint32_t>();
|
|
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>();
|
|
uint32_t miss_rate = options["miss-rate"].as<uint32_t>();
|
|
|
|
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;
|
|
}
|