// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see .
#pragma once
#include
#include
#include
#include
#include
#include
#include "Common/ObserverManager.h"
#include "INode.h"
namespace System {
class ContextGroup;
class Dispatcher;
class Event;
}
namespace CryptoNote {
class HttpClient;
class INodeRpcProxyObserver {
public:
virtual ~INodeRpcProxyObserver() {}
virtual void connectionStatusUpdated(bool connected) {}
};
class NodeRpcProxy : public CryptoNote::INode {
public:
NodeRpcProxy(const std::string& nodeHost, unsigned short nodePort);
virtual ~NodeRpcProxy();
virtual bool addObserver(CryptoNote::INodeObserver* observer) override;
virtual bool removeObserver(CryptoNote::INodeObserver* observer) override;
virtual bool addObserver(CryptoNote::INodeRpcProxyObserver* observer);
virtual bool removeObserver(CryptoNote::INodeRpcProxyObserver* observer);
virtual void init(const Callback& callback) override;
virtual bool shutdown() override;
virtual size_t getPeerCount() const override;
virtual uint32_t getLastLocalBlockHeight() const override;
virtual uint32_t getLastKnownBlockHeight() const override;
virtual uint32_t getLocalBlockCount() const override;
virtual uint32_t getKnownBlockCount() const override;
virtual uint64_t getLastLocalBlockTimestamp() const override;
virtual void relayTransaction(const CryptoNote::Transaction& transaction, const Callback& callback) override;
virtual void getRandomOutsByAmounts(std::vector&& amounts, uint64_t outsCount, std::vector& result, const Callback& callback) override;
virtual void getNewBlocks(std::vector&& knownBlockIds, std::vector& newBlocks, uint32_t& startHeight, const Callback& callback) override;
virtual void getTransactionOutsGlobalIndices(const Crypto::Hash& transactionHash, std::vector& outsGlobalIndices, const Callback& callback) override;
virtual void queryBlocks(std::vector&& knownBlockIds, uint64_t timestamp, std::vector& newBlocks, uint32_t& startHeight, const Callback& callback) override;
virtual void getPoolSymmetricDifference(std::vector&& knownPoolTxIds, Crypto::Hash knownBlockId, bool& isBcActual,
std::vector>& newTxs, std::vector& deletedTxIds, const Callback& callback) override;
virtual void getMultisignatureOutputByGlobalIndex(uint64_t amount, uint32_t gindex, MultisignatureOutput& out, const Callback& callback) override;
virtual void getBlocks(const std::vector& blockHeights, std::vector>& blocks, const Callback& callback) override;
virtual void getBlocks(const std::vector& blockHashes, std::vector& blocks, const Callback& callback) override;
virtual void getBlocks(uint64_t timestampBegin, uint64_t timestampEnd, uint32_t blocksNumberLimit, std::vector& blocks, uint32_t& blocksNumberWithinTimestamps, const Callback& callback) override;
virtual void getTransactions(const std::vector& transactionHashes, std::vector& transactions, const Callback& callback) override;
virtual void getTransactionsByPaymentId(const Crypto::Hash& paymentId, std::vector& transactions, const Callback& callback) override;
virtual void getPoolTransactions(uint64_t timestampBegin, uint64_t timestampEnd, uint32_t transactionsNumberLimit, std::vector& transactions, uint64_t& transactionsNumberWithinTimestamps, const Callback& callback) override;
virtual void isSynchronized(bool& syncStatus, const Callback& callback) override;
unsigned int rpcTimeout() const { return m_rpcTimeout; }
void rpcTimeout(unsigned int val) { m_rpcTimeout = val; }
private:
void resetInternalState();
void workerThread(const Callback& initialized_callback);
std::vector getKnownTxsVector() const;
void pullNodeStatusAndScheduleTheNext();
void updateNodeStatus();
void updateBlockchainStatus();
bool updatePoolStatus();
void updatePeerCount();
void updatePoolState(const std::vector>& addedTxs, const std::vector& deletedTxsIds);
std::error_code doRelayTransaction(const CryptoNote::Transaction& transaction);
std::error_code doGetRandomOutsByAmounts(std::vector& amounts, uint64_t outsCount,
std::vector& result);
std::error_code doGetNewBlocks(std::vector& knownBlockIds,
std::vector& newBlocks, uint32_t& startHeight);
std::error_code doGetTransactionOutsGlobalIndices(const Crypto::Hash& transactionHash,
std::vector& outsGlobalIndices);
std::error_code doQueryBlocksLite(const std::vector& knownBlockIds, uint64_t timestamp,
std::vector& newBlocks, uint32_t& startHeight);
std::error_code doGetPoolSymmetricDifference(std::vector&& knownPoolTxIds, Crypto::Hash knownBlockId, bool& isBcActual,
std::vector>& newTxs, std::vector& deletedTxIds);
void scheduleRequest(std::function&& procedure, const Callback& callback);
template
std::error_code binaryCommand(const std::string& url, const Request& req, Response& res);
template
std::error_code jsonCommand(const std::string& url, const Request& req, Response& res);
template
std::error_code jsonRpcCommand(const std::string& method, const Request& req, Response& res);
enum State {
STATE_NOT_INITIALIZED,
STATE_INITIALIZING,
STATE_INITIALIZED
};
private:
State m_state = STATE_NOT_INITIALIZED;
std::mutex m_mutex;
std::condition_variable m_cv_initialized;
std::thread m_workerThread;
System::Dispatcher* m_dispatcher = nullptr;
System::ContextGroup* m_context_group = nullptr;
Tools::ObserverManager m_observerManager;
Tools::ObserverManager m_rpcProxyObserverManager;
const std::string m_nodeHost;
const unsigned short m_nodePort;
unsigned int m_rpcTimeout;
HttpClient* m_httpClient = nullptr;
System::Event* m_httpEvent = nullptr;
uint64_t m_pullInterval;
// Internal state
bool m_stop = false;
std::atomic m_peerCount;
std::atomic m_nodeHeight;
std::atomic m_networkHeight;
//protect it with mutex if decided to add worker threads
Crypto::Hash m_lastKnowHash;
std::atomic m_lastLocalBlockTimestamp;
std::unordered_set m_knownTxs;
bool m_connected;
};
}