Skip to content

Commit

Permalink
Added testnet flag in daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptonotefoundation committed Jul 10, 2014
1 parent 7853c21 commit f4769d8
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/cryptonote_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define CRYPTONOTE_MAX_BLOCK_SIZE 500000000 // block header blob limit, never used!
#define CRYPTONOTE_MAX_TX_SIZE 1000000000
#define CRYPTONOTE_PUBLIC_ADDRESS_TEXTBLOB_VER 0
//TODO Define the first letter of your currency address
//TODO Currency-specific address prefix
#define CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX
//TODO Choose maturity period for your currency
#define CRYPTONOTE_MINED_MONEY_UNLOCK_WINDOW
Expand Down
40 changes: 34 additions & 6 deletions src/cryptonote_core/blockchain_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ uint64_t blockchain_storage::get_current_blockchain_height() {
return m_blocks.size();
}

bool blockchain_storage::init(const std::string& config_folder, bool load_existing) {
bool blockchain_storage::init(const std::string& config_folder, bool load_existing, bool testnet) {
CRITICAL_REGION_LOCAL(m_blockchain_lock);
if (!config_folder.empty() && !tools::create_directories_if_necessary(config_folder)) {
LOG_ERROR("Failed to create data directory: " << m_config_folder);
Expand Down Expand Up @@ -149,11 +149,24 @@ bool blockchain_storage::init(const std::string& config_folder, bool load_existi

if (m_blocks.empty()) {
LOG_PRINT_L0("Blockchain not loaded, generating genesis block.");
block bl = ::boost::value_initialized<block>();
block_verification_context bvc = boost::value_initialized<block_verification_context>();
generate_genesis_block(bl);
add_new_block(bl, bvc);
CHECK_AND_ASSERT_MES(!bvc.m_verifivation_failed, false, "Failed to add genesis block to blockchain");

if (!storeGenesisBlock(testnet)) {
return false;
}
} else {
cryptonote::block b;
if (testnet) {
generateTestnetGenesisBlock(b);
} else {
generateGenesisBlock(b);
}

crypto::hash genesis_hash = get_block_hash(m_blocks[0].bl);
crypto::hash testnet_genesis_hash = get_block_hash(b);
if (genesis_hash != testnet_genesis_hash) {
LOG_ERROR("Failed to init: genesis block mismatch. Probably you set --testnet flag with data dir with non-test blockchain or another network.");
return false;
}
}

uint64_t timestamp_diff = time(NULL) - m_blocks.back().bl.timestamp;
Expand All @@ -163,6 +176,21 @@ bool blockchain_storage::init(const std::string& config_folder, bool load_existi
return true;
}

bool blockchain_storage::storeGenesisBlock(bool testnet) {
block bl = ::boost::value_initialized<block>();
block_verification_context bvc = boost::value_initialized<block_verification_context>();

if (testnet) {
generateTestnetGenesisBlock(bl);
} else {
generateGenesisBlock(bl);
}

add_new_block(bl, bvc);
CHECK_AND_ASSERT_MES(!bvc.m_verifivation_failed, false, "Failed to add genesis block to blockchain");
return true;
}

bool blockchain_storage::store_blockchain() {
try {
std::ofstream file(appendPath(m_config_folder, CRYPTONOTE_BLOCKSCACHE_FILENAME), std::ios::binary);
Expand Down
3 changes: 2 additions & 1 deletion src/cryptonote_core/blockchain_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace cryptonote {
{};

bool init() { return init(tools::get_default_data_dir(), true); }
bool init(const std::string& config_folder, bool load_existing);
bool init(const std::string& config_folder, bool load_existing, bool testnet = false);
bool deinit();

void set_checkpoints(checkpoints&& chk_pts) { m_checkpoints = chk_pts; }
Expand Down Expand Up @@ -182,6 +182,7 @@ namespace cryptonote {
bool pushTransaction(Block& block, const crypto::hash& transactionHash, TransactionIndex transactionIndex);
void popTransaction(const transaction& transaction, const crypto::hash& transactionHash);
void popTransactions(const Block& block, const crypto::hash& minerTransactionHash);
bool storeGenesisBlock(bool testnet);
};


Expand Down
4 changes: 2 additions & 2 deletions src/cryptonote_core/cryptonote_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ namespace cryptonote
return m_blockchain_storage.get_alternative_blocks_count();
}
//-----------------------------------------------------------------------------------------------
bool core::init(const boost::program_options::variables_map& vm, bool load_existing)
bool core::init(const boost::program_options::variables_map& vm, bool load_existing, bool testnet)
{
bool r = handle_command_line(vm);

r = m_mempool.init(m_config_folder);
CHECK_AND_ASSERT_MES(r, false, "Failed to initialize memory pool");

r = m_blockchain_storage.init(m_config_folder, load_existing);
r = m_blockchain_storage.init(m_config_folder, load_existing, testnet);
CHECK_AND_ASSERT_MES(r, false, "Failed to initialize blockchain storage");

r = m_miner.init(vm);
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_core/cryptonote_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace cryptonote

miner& get_miner(){return m_miner;}
static void init_options(boost::program_options::options_description& desc);
bool init(const boost::program_options::variables_map& vm, bool load_existing);
bool init(const boost::program_options::variables_map& vm, bool load_existing, bool testnet);
bool set_genesis_block(const block& b);
bool deinit();
uint64_t get_current_blockchain_height();
Expand Down
14 changes: 12 additions & 2 deletions src/cryptonote_core/cryptonote_format_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ namespace cryptonote
return p;
}
//---------------------------------------------------------------
bool generate_genesis_block(block& bl)
bool generateGenesisBlock(block& bl)
{
//genesis block
bl = boost::value_initialized<block>();
Expand All @@ -630,7 +630,7 @@ namespace cryptonote
construct_miner_tx(0, 0, 0, 0, 0, ac, bl.miner_tx); // zero fee in genesis
blobdata txb = tx_to_blob(bl.miner_tx);
std::string hex_tx_represent = string_tools::buff_to_hex_nodelimer(txb);
std::cout << "Genesis coinbase tx hex: " << hex_to_represent << std::endl;
std::cout << "Genesis coinbase tx hex: " << hex_tx_represent << std::endl;
*/

//hard code coinbase tx in genesis block, because "true" generating tx use random, but genesis should be always the same
Expand All @@ -648,6 +648,16 @@ namespace cryptonote
//miner::find_nonce_for_given_block(bl, 1, 0);
return true;
}

bool generateTestnetGenesisBlock(cryptonote::block& b) {
if (!generateGenesisBlock(b)) {
return false;
}

b.nonce += 1;
return true;
}

//---------------------------------------------------------------
bool get_block_longhash(crypto::cn_context &context, const block& b, crypto::hash& res, uint64_t height)
{
Expand Down
3 changes: 2 additions & 1 deletion src/cryptonote_core/cryptonote_format_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ namespace cryptonote
crypto::hash get_block_hash(const block& b);
bool get_block_longhash(crypto::cn_context &context, const block& b, crypto::hash& res, uint64_t height);
crypto::hash get_block_longhash(crypto::cn_context &context, const block& b, uint64_t height);
bool generate_genesis_block(block& bl);
bool generateGenesisBlock(block& bl);
bool generateTestnetGenesisBlock(block& bl);
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b);
bool get_inputs_money_amount(const transaction& tx, uint64_t& money);
uint64_t get_outs_money_amount(const transaction& tx);
Expand Down
17 changes: 13 additions & 4 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace
const command_line::arg_descriptor<std::string> arg_log_file = {"log-file", "", ""};
const command_line::arg_descriptor<int> arg_log_level = {"log-level", "", LOG_LEVEL_0};
const command_line::arg_descriptor<bool> arg_console = {"no-console", "Disable daemon console commands"};
const command_line::arg_descriptor<bool> arg_testnet_on = {"testnet", "Used to deploy test nets. Checkpoints and hardcoded seeds are ignored, "
"network id is changed. Use it with --data-dir flag. The wallet must be launched with --testnet flag.", false};
}

bool command_line_preprocessor(const boost::program_options::variables_map& vm);
Expand Down Expand Up @@ -66,7 +68,7 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, arg_log_file);
command_line::add_arg(desc_cmd_sett, arg_log_level);
command_line::add_arg(desc_cmd_sett, arg_console);

command_line::add_arg(desc_cmd_sett, arg_testnet_on);

cryptonote::core::init_options(desc_cmd_sett);
cryptonote::core_rpc_server::init_options(desc_cmd_sett);
Expand Down Expand Up @@ -134,7 +136,14 @@ int main(int argc, char* argv[])

//create objects and link them
cryptonote::core ccore(NULL);
ccore.set_checkpoints(std::move(checkpoints));

bool testnet_mode = command_line::get_arg(vm, arg_testnet_on);
if (testnet_mode) {
LOG_PRINT_L0("Starting in testnet mode!");
} else {
ccore.set_checkpoints(std::move(checkpoints));
}

cryptonote::t_cryptonote_protocol_handler<cryptonote::core> cprotocol(ccore, NULL);
nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> > p2psrv(cprotocol);
cryptonote::core_rpc_server rpc_server(ccore, p2psrv);
Expand All @@ -144,7 +153,7 @@ int main(int argc, char* argv[])

//initialize objects
LOG_PRINT_L0("Initializing p2p server...");
res = p2psrv.init(vm);
res = p2psrv.init(vm, testnet_mode);
CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize p2p server.");
LOG_PRINT_L0("P2p server initialized OK");

Expand All @@ -160,7 +169,7 @@ int main(int argc, char* argv[])

//initialize core here
LOG_PRINT_L0("Initializing core...");
res = ccore.init(vm, true);
res = ccore.init(vm, true, testnet_mode);
CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize core");
LOG_PRINT_L0("Core initialized OK");

Expand Down
5 changes: 3 additions & 2 deletions src/p2p/net_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ namespace nodetool
public:
typedef t_payload_net_handler payload_net_handler;
// Some code
node_server(t_payload_net_handler& payload_handler):m_payload_handler(payload_handler), m_allow_local_ip(false), m_hide_my_port(false)
node_server(t_payload_net_handler& payload_handler):m_payload_handler(payload_handler), m_allow_local_ip(false), m_hide_my_port(false), m_network_id(CRYPTONOTE_NETWORK)
{}

static void init_options(boost::program_options::options_description& desc);

bool run();
bool init(const boost::program_options::variables_map& vm);
bool init(const boost::program_options::variables_map& vm, bool testnet);
bool deinit();
bool send_stop_signal();
uint32_t get_this_peer_port(){return m_listenning_port;}
Expand Down Expand Up @@ -203,6 +203,7 @@ namespace nodetool
uint64_t m_peer_livetime;
//keep connections to initiate some interactions
net_server m_net_server;
boost::uuids::uuid m_network_id;
};
}

Expand Down
20 changes: 12 additions & 8 deletions src/p2p/net_node.inl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ namespace nodetool
command_line::add_arg(desc, arg_p2p_add_priority_node);
command_line::add_arg(desc, arg_p2p_add_exclusive_node);
command_line::add_arg(desc, arg_p2p_seed_node);
command_line::add_arg(desc, arg_p2p_hide_my_port); }
command_line::add_arg(desc, arg_p2p_hide_my_port);
}
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::init_config()
Expand Down Expand Up @@ -191,10 +192,14 @@ namespace nodetool
#define ADD_HARDCODED_SEED_NODE(addr) append_net_address(m_seed_nodes, addr);
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::init(const boost::program_options::variables_map& vm)
bool node_server<t_payload_net_handler>::init(const boost::program_options::variables_map& vm, bool testnet)
{
//TODO add seed for your network
//ADD_HARDCODED_SEED_NODE("your_seed_ip.com:8080");
if (!testnet) {
//TODO add seed for your network
//ADD_HARDCODED_SEED_NODE("your_seed_ip.com:8080");
} else {
m_network_id.data[0] += 1;
}

bool res = handle_command_line(vm);
CHECK_AND_ASSERT_MES(res, false, "Failed to handle command line");
Expand Down Expand Up @@ -368,7 +373,7 @@ namespace nodetool
return;
}

if(rsp.node_data.network_id != CRYPTONOTE_NETWORK)
if(rsp.node_data.network_id != m_network_id)
{
LOG_ERROR_CCONTEXT("COMMAND_HANDSHAKE Failed, wrong network! (" << epee::string_tools::get_str_from_guid_a(rsp.node_data.network_id) << "), closing connection.");
return;
Expand Down Expand Up @@ -776,7 +781,7 @@ namespace nodetool
node_data.my_port = m_external_port ? m_external_port : m_listenning_port;
else
node_data.my_port = 0;
node_data.network_id = CRYPTONOTE_NETWORK;
node_data.network_id = m_network_id;
return true;
}
//-----------------------------------------------------------------------------------
Expand Down Expand Up @@ -996,9 +1001,8 @@ namespace nodetool
template<class t_payload_net_handler>
int node_server<t_payload_net_handler>::handle_handshake(int command, typename COMMAND_HANDSHAKE::request& arg, typename COMMAND_HANDSHAKE::response& rsp, p2p_connection_context& context)
{
if(arg.node_data.network_id != CRYPTONOTE_NETWORK)
if(arg.node_data.network_id != m_network_id)
{

LOG_PRINT_CCONTEXT_L0("WRONG NETWORK AGENT CONNECTED! id=" << epee::string_tools::get_str_from_guid_a(arg.node_data.network_id));
drop_connection(context);
return 1;
Expand Down
21 changes: 13 additions & 8 deletions src/simplewallet/simplewallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace
const command_line::arg_descriptor<std::string> arg_password = {"password", "Wallet password", "", true};
const command_line::arg_descriptor<int> arg_daemon_port = {"daemon-port", "Use daemon instance at port <arg> instead of 8081", 0};
const command_line::arg_descriptor<uint32_t> arg_log_level = {"set_log", "", 0, true};
const command_line::arg_descriptor<bool> arg_testnet = {"testnet", "Used to deploy test nets. The daemon must be launched with --testnet flag", false};

const command_line::arg_descriptor< std::vector<std::string> > arg_command = {"command", ""};

Expand Down Expand Up @@ -266,6 +267,8 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
if (m_daemon_address.empty())
m_daemon_address = std::string("http://") + m_daemon_host + ":" + std::to_string(m_daemon_port);

bool testnet = command_line::get_arg(vm, arg_testnet);

tools::password_container pwd_container;
if (command_line::has_arg(vm, arg_password))
{
Expand All @@ -283,12 +286,12 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)

if (!m_generate_new.empty())
{
bool r = new_wallet(m_generate_new, pwd_container.password());
bool r = new_wallet(m_generate_new, pwd_container.password(), testnet);
CHECK_AND_ASSERT_MES(r, false, "account creation failed");
}
else
{
bool r = open_wallet(m_wallet_file, pwd_container.password());
bool r = open_wallet(m_wallet_file, pwd_container.password(), testnet);
CHECK_AND_ASSERT_MES(r, false, "could not open account");
}

Expand Down Expand Up @@ -324,11 +327,11 @@ bool simple_wallet::try_connect_to_daemon()
return true;
}
//----------------------------------------------------------------------------------------------------
bool simple_wallet::new_wallet(const string &wallet_file, const std::string& password)
bool simple_wallet::new_wallet(const string &wallet_file, const std::string& password, bool testnet)
{
m_wallet_file = wallet_file;

m_wallet.reset(new tools::wallet2());
m_wallet.reset(new tools::wallet2(testnet));
m_wallet->callback(this);
try
{
Expand All @@ -355,10 +358,10 @@ bool simple_wallet::new_wallet(const string &wallet_file, const std::string& pas
return true;
}
//----------------------------------------------------------------------------------------------------
bool simple_wallet::open_wallet(const string &wallet_file, const std::string& password)
bool simple_wallet::open_wallet(const string &wallet_file, const std::string& password, bool testnet)
{
m_wallet_file = wallet_file;
m_wallet.reset(new tools::wallet2());
m_wallet.reset(new tools::wallet2(testnet));
m_wallet->callback(this);

try
Expand Down Expand Up @@ -899,6 +902,7 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_params, arg_daemon_port);
command_line::add_arg(desc_params, arg_command);
command_line::add_arg(desc_params, arg_log_level);
command_line::add_arg(desc_params, arg_testnet);
tools::wallet_rpc_server::init_options(desc_params);

po::positional_options_description positional_options;
Expand All @@ -914,7 +918,7 @@ int main(int argc, char* argv[])

if (command_line::get_arg(vm, command_line::arg_help))
{
success_msg_writer() << "bytecoin wallet v" << PROJECT_VERSION_LONG;
success_msg_writer() << CRYPTONOTE_NAME " wallet v" << PROJECT_VERSION_LONG;
success_msg_writer() << "Usage: simplewallet [--wallet-file=<file>|--generate-new-wallet=<file>] [--daemon-address=<host>:<port>] [<COMMAND>]";
success_msg_writer() << desc_all << '\n' << w.get_commands_str();
return false;
Expand Down Expand Up @@ -968,6 +972,7 @@ int main(int argc, char* argv[])
return 1;
}

bool testnet = command_line::get_arg(vm, arg_testnet);
std::string wallet_file = command_line::get_arg(vm, arg_wallet_file);
std::string wallet_password = command_line::get_arg(vm, arg_password);
std::string daemon_address = command_line::get_arg(vm, arg_daemon_address);
Expand All @@ -980,7 +985,7 @@ int main(int argc, char* argv[])
if (daemon_address.empty())
daemon_address = std::string("http://") + daemon_host + ":" + std::to_string(daemon_port);

tools::wallet2 wal;
tools::wallet2 wal(testnet);
try
{
LOG_PRINT_L0("Loading wallet...");
Expand Down
4 changes: 2 additions & 2 deletions src/simplewallet/simplewallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ namespace cryptonote

bool run_console_handler();

bool new_wallet(const std::string &wallet_file, const std::string& password);
bool open_wallet(const std::string &wallet_file, const std::string& password);
bool new_wallet(const std::string &wallet_file, const std::string& password, bool testnet);
bool open_wallet(const std::string &wallet_file, const std::string& password, bool testnet);
bool close_wallet();

bool help(const std::vector<std::string> &args = std::vector<std::string>());
Expand Down

0 comments on commit f4769d8

Please sign in to comment.