Merge pull request #1467

55fa0479 rpc: new function and RPC to get alternative chain info (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2016-12-20 17:44:47 +02:00
commit f5176cb6e8
No known key found for this signature in database
GPG key ID: 55432DF31CCD4FCD
10 changed files with 147 additions and 0 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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