Fabcoin Core  0.16.2
P2P Digital Currency
EthereumHost.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #pragma once
23 
24 #include <mutex>
25 #include <unordered_map>
26 #include <vector>
27 #include <unordered_set>
28 #include <memory>
29 #include <utility>
30 #include <thread>
31 
32 #include <libdevcore/Guards.h>
33 #include <libdevcore/Worker.h>
34 #include <libethcore/Common.h>
35 #include <libp2p/Common.h>
36 #include <libdevcore/OverlayDB.h>
37 #include <libethcore/BlockHeader.h>
39 #include "CommonNet.h"
40 #include "EthereumPeer.h"
41 
42 namespace dev
43 {
44 
45 class RLPStream;
46 
47 namespace eth
48 {
49 
50 class TransactionQueue;
51 class BlockQueue;
52 class BlockChainSync;
53 
54 struct EthereumHostTrace: public LogChannel { static const char* name(); static const int verbosity = 6; };
55 
61 class EthereumHost: public p2p::HostCapability<EthereumPeer>, Worker
62 {
63 public:
65  EthereumHost(BlockChain const& _ch, OverlayDB const& _db, TransactionQueue& _tq, BlockQueue& _bq, u256 _networkId);
66 
68  virtual ~EthereumHost();
69 
70  unsigned protocolVersion() const { return c_protocolVersion; }
71  u256 networkId() const { return m_networkId; }
72  void setNetworkId(u256 _n) { m_networkId = _n; }
73 
74  void reset();
76  void completeSync();
77 
78  bool isSyncing() const;
79  bool isBanned(p2p::NodeID const& _id) const { return !!m_banned.count(_id); }
80 
81  void noteNewTransactions() { m_newTransactions = true; }
82  void noteNewBlocks() { m_newBlocks = true; }
83  void onBlockImported(BlockHeader const& _info) { m_sync->onBlockImported(_info); }
84 
85  BlockChain const& chain() const { return m_chain; }
86  OverlayDB const& db() const { return m_db; }
87  BlockQueue& bq() { return m_bq; }
88  BlockQueue const& bq() const { return m_bq; }
89  SyncStatus status() const;
90  h256 latestBlockSent() { return m_latestBlockSent; }
91  static char const* stateName(SyncState _s) { return s_stateNames[static_cast<int>(_s)]; }
92 
93  static unsigned const c_oldProtocolVersion;
94  void foreachPeer(std::function<bool(std::shared_ptr<EthereumPeer>)> const& _f) const;
95 
96 protected:
97  std::shared_ptr<p2p::Capability> newPeerCapability(std::shared_ptr<p2p::SessionFace> const& _s, unsigned _idOffset, p2p::CapDesc const& _cap, uint16_t _capID) override;
98 
99 private:
100  static char const* const s_stateNames[static_cast<int>(SyncState::Size)];
101 
102  std::tuple<std::vector<std::shared_ptr<EthereumPeer>>, std::vector<std::shared_ptr<EthereumPeer>>, std::vector<std::shared_ptr<p2p::SessionFace>>> randomSelection(unsigned _percent = 25, std::function<bool(EthereumPeer*)> const& _allow = [](EthereumPeer const*){ return true; });
103 
105  virtual void doWork() override;
106 
107  void maintainTransactions();
108  void maintainBlocks(h256 const& _currentBlock);
109  void onTransactionImported(ImportResult _ir, h256 const& _h, h512 const& _nodeId);
110 
112  bool isInitialised() const { return (bool)m_latestBlockSent; }
113 
115  bool ensureInitialised();
116 
117  virtual void onStarting() override { startWorking(); }
118  virtual void onStopping() override { stopWorking(); }
119 
121  OverlayDB const& m_db;
124 
126 
129 
130  std::unordered_set<p2p::NodeID> m_banned;
131 
132  bool m_newTransactions = false;
133  bool m_newBlocks = false;
134 
137  std::unique_ptr<BlockChainSync> m_sync;
138  std::atomic<time_t> m_lastTick = { 0 };
139 
140  std::shared_ptr<EthereumHostDataFace> m_hostData;
141  std::shared_ptr<EthereumPeerObserverFace> m_peerObserver;
142 };
143 
144 }
145 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
bool isInitialised() const
Check to see if the network peer-state initialisation has happened.
Definition: EthereumHost.h:112
#define function(a, b, c, d, k, s)
const unsigned c_protocolVersion
Current protocol version.
Definition: Common.cpp:43
A queue of Transactions, each stored as RLP.
Implements the blockchain database.
Definition: BlockChain.h:105
std::pair< std::string, u256 > CapDesc
Definition: Common.h:142
BlockQueue const & bq() const
Definition: EthereumHost.h:88
Encapsulation of a block header.
Definition: BlockHeader.h:95
virtual void onStarting() override
Definition: EthereumHost.h:117
static const char * name()
RecursiveMutex x_sync
Definition: EthereumHost.h:135
virtual void onStopping() override
Definition: EthereumHost.h:118
The EthereumHost class.
Definition: EthereumHost.h:61
unsigned protocolVersion() const
Definition: EthereumHost.h:70
OverlayDB const & m_db
References to DB, needed for some of the Ethereum Protocol responses.
Definition: EthereumHost.h:121
vector< T > randomSelection(vector< T > const &_t, unsigned _n)
Definition: Session.cpp:100
bool isBanned(p2p::NodeID const &_id) const
Definition: EthereumHost.h:79
std::unique_ptr< BlockChainSync > m_sync
Definition: EthereumHost.h:137
ImportResult
Definition: Common.h:97
std::recursive_mutex RecursiveMutex
Definition: Guards.h:38
void setNetworkId(u256 _n)
Definition: EthereumHost.h:72
static unsigned const c_oldProtocolVersion
Definition: EthereumHost.h:93
std::unordered_set< p2p::NodeID > m_banned
Definition: EthereumHost.h:130
std::shared_ptr< EthereumHostDataFace > m_hostData
Definition: EthereumHost.h:140
OverlayDB const & db() const
Definition: EthereumHost.h:86
BlockChain const & m_chain
Definition: EthereumHost.h:120
Must be kept last.
BlockChain const & chain() const
Definition: EthereumHost.h:85
void onBlockImported(BlockHeader const &_info)
Definition: EthereumHost.h:83
std::shared_ptr< EthereumPeerObserverFace > m_peerObserver
Definition: EthereumHost.h:141
A queue of blocks.
Definition: BlockQueue.h:225
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
TransactionQueue & m_tq
Maintains a list of incoming transactions not yet in a block on the blockchain.
Definition: EthereumHost.h:122
The EthereumPeer class.
Definition: EthereumPeer.h:83
BlockQueue & m_bq
Maintains a list of incoming blocks not yet on the blockchain (to be imported).
Definition: EthereumHost.h:123
BlockQueue & bq()
Definition: EthereumHost.h:87
u256 networkId() const
Definition: EthereumHost.h:71
The default logging channels.
Definition: Log.h:130
std::unordered_set< h256 > h256Hash
Definition: FixedHash.h:349
std::mutex Mutex
Definition: Guards.h:37
static const int verbosity
Definition: EthereumHost.h:54
static char const * stateName(SyncState _s)
Definition: EthereumHost.h:91