-
Notifications
You must be signed in to change notification settings - Fork 658
Expand file tree
/
Copy pathmain.cpp
More file actions
171 lines (153 loc) · 6.15 KB
/
main.cpp
File metadata and controls
171 lines (153 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <fc/io/json.hpp>
#include <fc/io/stdio.hpp>
#include <graphene/app/api.hpp>
#include <graphene/egenesis/egenesis.hpp>
#include <graphene/utilities/file_util.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("BitShares 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<std::string>(), "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") )
{
std::string genesis_json_filename = options["genesis-json"].as<std::string>();
std::cerr << "generate_empty_blocks: Reading genesis from file " << genesis_json_filename << "\n";
std::string genesis_json = graphene::utilities::read_file_contents( genesis_json_filename );
genesis = fc::json::from_string( genesis_json ).as< genesis_state_type >(20);
}
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 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; }, "TEST" );
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].value();
slot = 1;
while(true)
{
if( (rand % 100) < miss_rate )
{
slot++;
rand = (rand/100) ^ h._hash[slot&3].value();
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;
}