danicoin/src/CryptoNoteProtocol/CryptoNoteProtocolHandler.h

103 lines
4.4 KiB
C
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2014-03-03 22:07:58 +00:00
#pragma once
#include <atomic>
2015-05-27 12:08:46 +00:00
#include <Common/ObserverManager.h>
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
#include "CryptoNoteCore/ICore.h"
2015-07-30 15:22:07 +00:00
#include "CryptoNoteProtocol/CryptoNoteProtocolDefinitions.h"
#include "CryptoNoteProtocol/CryptoNoteProtocolHandlerCommon.h"
#include "CryptoNoteProtocol/ICryptoNoteProtocolObserver.h"
#include "CryptoNoteProtocol/ICryptoNoteProtocolQuery.h"
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
#include "P2p/P2pProtocolDefinitions.h"
#include "P2p/NetNodeCommon.h"
#include "P2p/ConnectionContext.h"
2015-05-27 12:08:46 +00:00
#include <Logging/LoggerRef.h>
namespace System {
class Dispatcher;
}
namespace CryptoNote
{
class Currency;
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
class CryptoNoteProtocolHandler :
2015-05-27 12:08:46 +00:00
public i_cryptonote_protocol,
2015-07-30 15:22:07 +00:00
public ICryptoNoteProtocolQuery
{
2014-03-03 22:07:58 +00:00
public:
2015-07-30 15:22:07 +00:00
CryptoNoteProtocolHandler(const Currency& currency, System::Dispatcher& dispatcher, ICore& rcore, IP2pEndpoint* p_net_layout, Logging::ILogger& log);
2015-10-01 15:27:18 +00:00
virtual bool addObserver(ICryptoNoteProtocolObserver* observer) override;
virtual bool removeObserver(ICryptoNoteProtocolObserver* observer) override;
2015-07-30 15:22:07 +00:00
void set_p2p_endpoint(IP2pEndpoint* p2p);
2015-05-27 12:08:46 +00:00
// ICore& get_core() { return m_core; }
2015-07-15 12:23:00 +00:00
virtual bool isSynchronized() const override { return m_synchronized; }
2014-03-20 11:46:11 +00:00
void log_connections();
// Interface t_payload_net_handler, where t_payload_net_handler is template argument of nodetool::node_server
void stop();
2015-07-30 15:22:07 +00:00
bool start_sync(CryptoNoteConnectionContext& context);
bool on_idle();
2015-07-30 15:22:07 +00:00
void onConnectionOpened(CryptoNoteConnectionContext& context);
void onConnectionClosed(CryptoNoteConnectionContext& context);
bool get_stat_info(core_stat_info& stat_inf);
bool get_payload_sync_data(CORE_SYNC_DATA& hshd);
2015-07-30 15:22:07 +00:00
bool process_payload_sync_data(const CORE_SYNC_DATA& hshd, CryptoNoteConnectionContext& context, bool is_inital);
int handleCommand(bool is_notify, int command, const BinaryArray& in_buff, BinaryArray& buff_out, CryptoNoteConnectionContext& context, bool& handled);
2015-10-01 15:27:18 +00:00
virtual size_t getPeerCount() const override;
virtual uint32_t getObservedHeight() const override;
2015-07-30 15:22:07 +00:00
void requestMissingPoolTransactions(const CryptoNoteConnectionContext& context);
2014-03-03 22:07:58 +00:00
private:
//----------------- commands handlers ----------------------------------------------
2015-07-30 15:22:07 +00:00
int handle_notify_new_block(int command, NOTIFY_NEW_BLOCK::request& arg, CryptoNoteConnectionContext& context);
int handle_notify_new_transactions(int command, NOTIFY_NEW_TRANSACTIONS::request& arg, CryptoNoteConnectionContext& context);
int handle_request_get_objects(int command, NOTIFY_REQUEST_GET_OBJECTS::request& arg, CryptoNoteConnectionContext& context);
int handle_response_get_objects(int command, NOTIFY_RESPONSE_GET_OBJECTS::request& arg, CryptoNoteConnectionContext& context);
int handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, CryptoNoteConnectionContext& context);
int handle_response_chain_entry(int command, NOTIFY_RESPONSE_CHAIN_ENTRY::request& arg, CryptoNoteConnectionContext& context);
int handleRequestTxPool(int command, NOTIFY_REQUEST_TX_POOL::request& arg, CryptoNoteConnectionContext& context);
2014-03-03 22:07:58 +00:00
//----------------- i_cryptonote_protocol ----------------------------------
2015-05-27 12:08:46 +00:00
virtual void relay_block(NOTIFY_NEW_BLOCK::request& arg) override;
virtual void relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg) override;
2015-05-27 12:08:46 +00:00
//----------------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
uint32_t get_current_blockchain_height();
bool request_missing_objects(CryptoNoteConnectionContext& context, bool check_having_blocks);
2014-03-03 22:07:58 +00:00
bool on_connection_synchronized();
2015-07-30 15:22:07 +00:00
void updateObservedHeight(uint32_t peerHeight, const CryptoNoteConnectionContext& context);
void recalculateMaxObservedHeight(const CryptoNoteConnectionContext& context);
int processObjects(CryptoNoteConnectionContext& context, const std::vector<block_complete_entry>& blocks);
2015-05-27 12:08:46 +00:00
Logging::LoggerRef logger;
private:
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
System::Dispatcher& m_dispatcher;
ICore& m_core;
const Currency& m_currency;
p2p_endpoint_stub m_p2p_stub;
2015-07-30 15:22:07 +00:00
IP2pEndpoint* m_p2p;
2014-03-20 11:46:11 +00:00
std::atomic<bool> m_synchronized;
std::atomic<bool> m_stop;
2014-03-03 22:07:58 +00:00
mutable std::mutex m_observedHeightMutex;
2015-07-30 15:22:07 +00:00
uint32_t m_observedHeight;
std::atomic<size_t> m_peersCount;
2015-07-30 15:22:07 +00:00
Tools::ObserverManager<ICryptoNoteProtocolObserver> m_observerManager;
2014-03-03 22:07:58 +00:00
};
}