/* This file is part of cpp-ethereum. cpp-ethereum is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. cpp-ethereum 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 General Public License for more details. You should have received a copy of the GNU General Public License along with cpp-ethereum. If not, see . */ /** @file EthereumHost.h * @author Gav Wood * @date 2014 */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include "CommonNet.h" #include "EthereumPeer.h" namespace dev { class RLPStream; namespace eth { class TransactionQueue; class BlockQueue; /** * @brief The EthereumHost class * @warning None of this is thread-safe. You have been warned. */ class EthereumHost: public p2p::HostCapability, Worker { friend class EthereumPeer; public: /// Start server, but don't listen. EthereumHost(BlockChain const& _ch, TransactionQueue& _tq, BlockQueue& _bq, u256 _networkId); /// Will block on network process events. virtual ~EthereumHost(); unsigned protocolVersion() const { return c_protocolVersion; } u256 networkId() const { return m_networkId; } void setNetworkId(u256 _n) { m_networkId = _n; } private: /// Session wants to pass us a block that we might not have. /// @returns true if we didn't have it. bool noteBlock(h256 _hash, bytesConstRef _data); /// Session has finished getting the chain of hashes. void noteHaveChain(EthereumPeer* _who); /// Called when the peer can no longer provide us with any needed blocks. void noteDoneBlocks(); /// Sync with the BlockChain. It might contain one of our mined blocks, we might have new candidates from the network. void doWork(); void maintainTransactions(TransactionQueue& _tq, h256 _currentBlock); void maintainBlocks(BlockQueue& _bq, h256 _currentBlock); /// Get a bunch of needed blocks. /// Removes them from our list of needed blocks. /// @returns empty if there's no more blocks left to fetch, otherwise the blocks to fetch. h256Set neededBlocks(h256Set const& _exclude); /// Check to see if the network peer-state initialisation has happened. bool isInitialised() const { return m_latestBlockSent; } /// Initialises the network peer-state, doing the stuff that needs to be once-only. @returns true if it really was first. bool ensureInitialised(TransactionQueue& _tq); virtual void onStarting() { startWorking(); } virtual void onStopping() { stopWorking(); } BlockChain const& m_chain; TransactionQueue& m_tq; ///< Maintains a list of incoming transactions not yet in a block on the blockchain. BlockQueue& m_bq; ///< Maintains a list of incoming blocks not yet on the blockchain (to be imported). u256 m_networkId; mutable std::recursive_mutex m_incomingLock; std::vector m_incomingTransactions; std::vector m_incomingBlocks; mutable std::mutex x_blocksNeeded; u256 m_totalDifficultyOfNeeded; h256s m_blocksNeeded; h256Set m_blocksOnWay; h256 m_latestBlockSent; std::set m_transactionsSent; }; } }