aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2016-12-20 17:44:47 +0200
committerRiccardo Spagni <ric@spagni.net>2016-12-20 17:44:47 +0200
commitf5176cb6e86e578fa866463638d56b3c6b145e4d (patch)
treebe50063b61b1989ae140528959d7441ef4465050
parentMerge pull request #1466 (diff)
parentrpc: new function and RPC to get alternative chain info (diff)
downloadmonero-f5176cb6e86e578fa866463638d56b3c6b145e4d.tar.xz
Merge pull request #1467
55fa0479 rpc: new function and RPC to get alternative chain info (moneromooo-monero)
-rw-r--r--src/cryptonote_core/blockchain.cpp32
-rw-r--r--src/cryptonote_core/blockchain.h7
-rw-r--r--src/daemon/command_parser_executor.cpp11
-rw-r--r--src/daemon/command_parser_executor.h2
-rw-r--r--src/daemon/command_server.cpp5
-rw-r--r--src/daemon/rpc_command_executor.cpp33
-rw-r--r--src/daemon/rpc_command_executor.h2
-rw-r--r--src/rpc/core_rpc_server.cpp18
-rw-r--r--src/rpc/core_rpc_server.h2
-rw-r--r--src/rpc/core_rpc_server_commands_defs.h35
10 files changed, 147 insertions, 0 deletions
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp
index ba369afbd..a23d51126 100644
--- a/src/cryptonote_core/blockchain.cpp
+++ b/src/cryptonote_core/blockchain.cpp
@@ -3878,6 +3878,38 @@ std::map<uint64_t, std::tuple<uint64_t, uint64_t, uint64_t>> Blockchain:: get_ou
return m_db->get_output_histogram(amounts, unlocked, recent_cutoff);
}
+std::list<std::pair<Blockchain::block_extended_info,uint64_t>> Blockchain::get_alternative_chains() const
+{
+ std::list<std::pair<Blockchain::block_extended_info,uint64_t>> chains;
+
+ for (const auto &i: m_alternative_chains)
+ {
+ const crypto::hash &top = i.first;
+ bool found = false;
+ for (const auto &j: m_alternative_chains)
+ {
+ if (j.second.bl.prev_id == top)
+ {
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ {
+ uint64_t length = 1;
+ auto h = i.second.bl.prev_id;
+ blocks_ext_by_hash::const_iterator prev;
+ while ((prev = m_alternative_chains.find(h)) != m_alternative_chains.end())
+ {
+ h = prev->second.bl.prev_id;
+ ++length;
+ }
+ chains.push_back(std::make_pair(i.second, length));
+ }
+ }
+ return chains;
+}
+
void Blockchain::cancel()
{
m_cancel = true;
diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h
index 9afc22657..245dc6e73 100644
--- a/src/cryptonote_core/blockchain.h
+++ b/src/cryptonote_core/blockchain.h
@@ -844,6 +844,13 @@ namespace cryptonote
void block_longhash_worker(const uint64_t height, const std::vector<block> &blocks,
std::unordered_map<crypto::hash, crypto::hash> &map) const;
+ /**
+ * @brief returns a set of known alternate chains
+ *
+ * @return a list of chains
+ */
+ std::list<std::pair<block_extended_info,uint64_t>> get_alternative_chains() const;
+
void cancel();
private:
diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp
index 88b77f76d..f07ef1616 100644
--- a/src/daemon/command_parser_executor.cpp
+++ b/src/daemon/command_parser_executor.cpp
@@ -482,4 +482,15 @@ bool t_command_parser_executor::print_coinbase_tx_sum(const std::vector<std::str
return m_executor.print_coinbase_tx_sum(height, count);
}
+bool t_command_parser_executor::alt_chain_info(const std::vector<std::string>& args)
+{
+ if(args.size())
+ {
+ std::cout << "No parameters allowed" << std::endl;
+ return false;
+ }
+
+ return m_executor.alt_chain_info();
+}
+
} // namespace daemonize
diff --git a/src/daemon/command_parser_executor.h b/src/daemon/command_parser_executor.h
index 93b1fab56..cc929db00 100644
--- a/src/daemon/command_parser_executor.h
+++ b/src/daemon/command_parser_executor.h
@@ -119,6 +119,8 @@ public:
bool output_histogram(const std::vector<std::string>& args);
bool print_coinbase_tx_sum(const std::vector<std::string>& args);
+
+ bool alt_chain_info(const std::vector<std::string>& args);
};
} // namespace daemonize
diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp
index 729b19cbf..88c49d111 100644
--- a/src/daemon/command_server.cpp
+++ b/src/daemon/command_server.cpp
@@ -225,6 +225,11 @@ t_command_server::t_command_server(
, std::bind(&t_command_parser_executor::print_coinbase_tx_sum, &m_parser, p::_1)
, "Print sum of coinbase transactions (start height, block count)"
);
+ m_command_lookup.set_handler(
+ "alt_chain_info"
+ , std::bind(&t_command_parser_executor::alt_chain_info, &m_parser, p::_1)
+ , "Print information about alternative chains"
+ );
}
bool t_command_server::process_command_str(const std::string& cmd)
diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp
index 65b42cc47..41b1fad24 100644
--- a/src/daemon/rpc_command_executor.cpp
+++ b/src/daemon/rpc_command_executor.cpp
@@ -1367,5 +1367,38 @@ bool t_rpc_command_executor::print_coinbase_tx_sum(uint64_t height, uint64_t cou
return true;
}
+bool t_rpc_command_executor::alt_chain_info()
+{
+ cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::request req;
+ cryptonote::COMMAND_RPC_GET_ALTERNATE_CHAINS::response res;
+ epee::json_rpc::error error_resp;
+
+ std::string fail_message = "Unsuccessful";
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->json_rpc_request(req, res, "get_alternate_chains", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_get_alternate_chains(req, res, error_resp))
+ {
+ tools::fail_msg_writer() << fail_message.c_str();
+ return true;
+ }
+ }
+
+ tools::msg_writer() << boost::lexical_cast<std::string>(res.chains.size()) << " alternate chains found:";
+ for (const auto chain: res.chains)
+ {
+ tools::msg_writer() << chain.length << " blocks long, branching at height " << (chain.height - chain.length + 1)
+ << ", difficulty " << chain.difficulty << ": " << chain.block_hash;
+ }
+ return true;
+}
+
}// namespace daemonize
diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h
index c1c99155b..a6c712c04 100644
--- a/src/daemon/rpc_command_executor.h
+++ b/src/daemon/rpc_command_executor.h
@@ -137,6 +137,8 @@ public:
bool output_histogram(uint64_t min_count, uint64_t max_count);
bool print_coinbase_tx_sum(uint64_t height, uint64_t count);
+
+ bool alt_chain_info();
};
} // namespace daemonize
diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp
index 5bf500733..c2ff63fc7 100644
--- a/src/rpc/core_rpc_server.cpp
+++ b/src/rpc/core_rpc_server.cpp
@@ -1337,6 +1337,24 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
+ bool core_rpc_server::on_get_alternate_chains(const COMMAND_RPC_GET_ALTERNATE_CHAINS::request& req, COMMAND_RPC_GET_ALTERNATE_CHAINS::response& res, epee::json_rpc::error& error_resp)
+ {
+ try
+ {
+ std::list<std::pair<Blockchain::block_extended_info, uint64_t>> chains = m_core.get_blockchain_storage().get_alternative_chains();
+ for (const auto &i: chains)
+ {
+ res.chains.push_back(COMMAND_RPC_GET_ALTERNATE_CHAINS::chain_info{epee::string_tools::pod_to_hex(get_block_hash(i.first.bl)), i.first.height, i.second, i.first.cumulative_difficulty});
+ }
+ res.status = CORE_RPC_STATUS_OK;
+ }
+ catch (...)
+ {
+ res.status = "Error retrieving alternate chains";
+ }
+ return true;
+ }
+ //------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_out_peers(const COMMAND_RPC_OUT_PEERS::request& req, COMMAND_RPC_OUT_PEERS::response& res)
{
// TODO
diff --git a/src/rpc/core_rpc_server.h b/src/rpc/core_rpc_server.h
index b7f6cdd60..0c0668f3b 100644
--- a/src/rpc/core_rpc_server.h
+++ b/src/rpc/core_rpc_server.h
@@ -118,6 +118,7 @@ namespace cryptonote
MAP_JON_RPC_WE("get_version", on_get_version, COMMAND_RPC_GET_VERSION)
MAP_JON_RPC_WE("get_coinbase_tx_sum", on_get_coinbase_tx_sum, COMMAND_RPC_GET_COINBASE_TX_SUM)
MAP_JON_RPC_WE("get_fee_estimate", on_get_per_kb_fee_estimate, COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE)
+ MAP_JON_RPC_WE_IF("get_alternate_chains",on_get_alternate_chains, COMMAND_RPC_GET_ALTERNATE_CHAINS, !m_restricted)
END_JSON_RPC_MAP()
END_URI_MAP2()
@@ -166,6 +167,7 @@ namespace cryptonote
bool on_get_version(const COMMAND_RPC_GET_VERSION::request& req, COMMAND_RPC_GET_VERSION::response& res, epee::json_rpc::error& error_resp);
bool on_get_coinbase_tx_sum(const COMMAND_RPC_GET_COINBASE_TX_SUM::request& req, COMMAND_RPC_GET_COINBASE_TX_SUM::response& res, epee::json_rpc::error& error_resp);
bool on_get_per_kb_fee_estimate(const COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE::request& req, COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE::response& res, epee::json_rpc::error& error_resp);
+ bool on_get_alternate_chains(const COMMAND_RPC_GET_ALTERNATE_CHAINS::request& req, COMMAND_RPC_GET_ALTERNATE_CHAINS::response& res, epee::json_rpc::error& error_resp);
//-----------------------
private:
diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h
index 23fcb0a92..b09cad235 100644
--- a/src/rpc/core_rpc_server_commands_defs.h
+++ b/src/rpc/core_rpc_server_commands_defs.h
@@ -1336,4 +1336,39 @@ namespace cryptonote
END_KV_SERIALIZE_MAP()
};
};
+
+ struct COMMAND_RPC_GET_ALTERNATE_CHAINS
+ {
+ struct request
+ {
+ BEGIN_KV_SERIALIZE_MAP()
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct chain_info
+ {
+ std::string block_hash;
+ uint64_t height;
+ uint64_t length;
+ uint64_t difficulty;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(block_hash)
+ KV_SERIALIZE(height)
+ KV_SERIALIZE(length)
+ KV_SERIALIZE(difficulty)
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct response
+ {
+ std::string status;
+ std::list<chain_info> chains;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(status)
+ KV_SERIALIZE(chains)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
}