Added testnet flag in daemon

This commit is contained in:
Albert Werner 2014-07-10 20:47:56 +04:00
parent 7853c212f4
commit f4769d87ad
18 changed files with 135 additions and 54 deletions

View file

@ -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

View file

@ -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);
@ -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;
@ -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);

View file

@ -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; }
@ -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);
};

View file

@ -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);

View file

@ -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();

View file

@ -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>();
@ -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
@ -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)
{

View file

@ -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);

View file

@ -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);
@ -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);
@ -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);
@ -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");
@ -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");

View file

@ -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;}
@ -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;
};
}

View file

@ -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()
@ -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");
@ -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;
@ -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;
}
//-----------------------------------------------------------------------------------
@ -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;

View file

@ -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", ""};
@ -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))
{
@ -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");
}
@ -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
{
@ -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
@ -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;
@ -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;
@ -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);
@ -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...");

View file

@ -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>());

View file

@ -1,4 +1,4 @@
#define BUILD_COMMIT_ID "@VERSION@"
#define PROJECT_VERSION "0.8.11"
#define PROJECT_VERSION_BUILD_NO "65"
#define PROJECT_VERSION "1.0.0"
#define PROJECT_VERSION_BUILD_NO "1"
#define PROJECT_VERSION_LONG PROJECT_VERSION "." PROJECT_VERSION_BUILD_NO "(" BUILD_COMMIT_ID ")"

View file

@ -106,7 +106,7 @@ void Wallet::initAndGenerate(const std::string& password) {
void Wallet::storeGenesisBlock() {
cryptonote::block b;
cryptonote::generate_genesis_block(b);
cryptonote::generateGenesisBlock(b);
m_blockchain.push_back(get_block_hash(b));
}

View file

@ -373,9 +373,6 @@ bool wallet2::clear()
{
m_blockchain.clear();
m_transfers.clear();
cryptonote::block b;
cryptonote::generate_genesis_block(b);
m_blockchain.push_back(get_block_hash(b));
m_local_bc_height = 1;
return true;
}
@ -455,6 +452,10 @@ void wallet2::generate(const std::string& wallet_, const std::string& password)
r = file_io_utils::save_string_to_file(m_wallet_file + ".address.txt", m_account.get_public_address_str());
if(!r) LOG_PRINT_RED_L0("String with address text not saved");
cryptonote::block b;
generateGenesis(b);
m_blockchain.push_back(get_block_hash(b));
store();
}
//----------------------------------------------------------------------------------------------------
@ -527,15 +528,25 @@ void wallet2::load(const std::string& wallet_, const std::string& password)
m_account_public_address.m_view_public_key != m_account.get_keys().m_account_address.m_view_public_key,
error::wallet_files_doesnt_correspond, m_keys_file, m_wallet_file);
if(m_blockchain.empty())
{
cryptonote::block b;
cryptonote::generate_genesis_block(b);
m_blockchain.push_back(get_block_hash(b));
cryptonote::block genesis;
generateGenesis(genesis);
crypto::hash genesisHash = get_block_hash(genesis);
if (m_blockchain.empty()) {
m_blockchain.push_back(genesisHash);
} else {
checkGenesis(genesisHash);
}
m_local_bc_height = m_blockchain.size();
}
//----------------------------------------------------------------------------------------------------
void wallet2::checkGenesis(const crypto::hash& genesisHash) {
std::string what("Genesis block missmatch. You probably use wallet without testnet flag with blockchain from test network or vice versa");
THROW_WALLET_EXCEPTION_IF(genesisHash != m_blockchain[0], error::wallet_internal_error, what);
}
//----------------------------------------------------------------------------------------------------
void wallet2::store()
{
bool r = tools::serialize_obj_to_file(*this, m_wallet_file);
@ -690,4 +701,12 @@ void wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts
transfer(dsts, fake_outputs_count, unlock_time, fee, extra, tx);
}
//----------------------------------------------------------------------------------------------------
void wallet2::generateGenesis(cryptonote::block& b) {
if (m_testnet) {
cryptonote::generateTestnetGenesisBlock(b);
} else {
cryptonote::generateGenesisBlock(b);
}
}
//----------------------------------------------------------------------------------------------------
}

View file

@ -53,9 +53,9 @@ namespace tools
class wallet2
{
wallet2(const wallet2&) : m_run(true), m_callback(0) {};
wallet2(const wallet2&) : m_run(true), m_callback(0), m_testnet(false) {};
public:
wallet2() : m_run(true), m_callback(0) {};
wallet2(bool testnet = false) : m_run(true), m_callback(0), m_testnet(testnet) {};
struct transfer_details
{
uint64_t m_block_height;
@ -162,6 +162,8 @@ namespace tools
bool prepare_file_names(const std::string& file_path);
void process_unconfirmed(const cryptonote::transaction& tx);
void add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t change_amount);
void generateGenesis(cryptonote::block& b);
void checkGenesis(const crypto::hash& genesis_hash); //throws
cryptonote::account_base m_account;
std::string m_daemon_address;
@ -181,6 +183,7 @@ namespace tools
std::atomic<bool> m_run;
i_wallet2_callback* m_callback;
bool m_testnet;
};
}
BOOST_CLASS_VERSION(tools::wallet2, 7)

View file

@ -86,7 +86,7 @@ int main(int argc, char* argv[])
//initialize objects
LOG_PRINT_L0("Initializing p2p server...");
bool res = p2psrv.init(vm);
bool res = p2psrv.init(vm, false);
CHECK_AND_ASSERT_MES(res, 1, "Failed to initialize p2p server.");
LOG_PRINT_L0("P2p server initialized OK");
@ -198,7 +198,7 @@ bool tests::proxy_core::get_blockchain_top(uint64_t& height, crypto::hash& top_i
}
bool tests::proxy_core::init(const boost::program_options::variables_map& /*vm*/) {
generate_genesis_block(m_genesis);
generateGenesisBlock(m_genesis);
crypto::hash h = get_block_hash(m_genesis);
add_block(h, get_block_longhash(m_cn_context, m_genesis, 0), m_genesis, block_to_blob(m_genesis));
return true;

View file

@ -461,7 +461,7 @@ inline bool do_replay_events(std::vector<test_event_entry>& events)
cryptonote::cryptonote_protocol_stub pr; //TODO: stub only for this kind of test, make real validation of relayed objects
cryptonote::core c(&pr);
if (!c.init(vm, false))
if (!c.init(vm, false, false))
{
std::cout << concolor::magenta << "Failed to init core" << concolor::normal << std::endl;
return false;