Fabcoin Core  0.16.2
P2P Digital Currency
EthereumPeer.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 <array>
26 #include <memory>
27 #include <utility>
28 
29 #include <libdevcore/RLP.h>
30 #include <libdevcore/Guards.h>
31 #include <libethcore/Common.h>
32 #include <libp2p/Capability.h>
33 #include "CommonNet.h"
34 
35 namespace dev
36 {
37 namespace eth
38 {
39 
41 {
42 public:
44 
45  virtual void onPeerStatus(std::shared_ptr<EthereumPeer> _peer) = 0;
46 
47  virtual void onPeerTransactions(std::shared_ptr<EthereumPeer> _peer, RLP const& _r) = 0;
48 
49  virtual void onPeerBlockHeaders(std::shared_ptr<EthereumPeer> _peer, RLP const& _headers) = 0;
50 
51  virtual void onPeerBlockBodies(std::shared_ptr<EthereumPeer> _peer, RLP const& _r) = 0;
52 
53  virtual void onPeerNewHashes(std::shared_ptr<EthereumPeer> _peer, std::vector<std::pair<h256, u256>> const& _hashes) = 0;
54 
55  virtual void onPeerNewBlock(std::shared_ptr<EthereumPeer> _peer, RLP const& _r) = 0;
56 
57  virtual void onPeerNodeData(std::shared_ptr<EthereumPeer> _peer, RLP const& _r) = 0;
58 
59  virtual void onPeerReceipts(std::shared_ptr<EthereumPeer> _peer, RLP const& _r) = 0;
60 
61  virtual void onPeerAborting() = 0;
62 };
63 
65 {
66 public:
67  virtual ~EthereumHostDataFace() {}
68 
69  virtual std::pair<bytes, unsigned> blockHeaders(RLP const& _blockId, unsigned _maxHeaders, u256 _skip, bool _reverse) const = 0;
70 
71  virtual std::pair<bytes, unsigned> blockBodies(RLP const& _blockHashes) const = 0;
72 
73  virtual strings nodeData(RLP const& _dataHashes) const = 0;
74 
75  virtual std::pair<bytes, unsigned> receipts(RLP const& _blockHashes) const = 0;
76 };
77 
84 {
85  friend class EthereumHost; //TODO: remove this
86  friend class BlockChainSync; //TODO: remove this
87 
88 public:
90  EthereumPeer(std::shared_ptr<p2p::SessionFace> _s, p2p::HostCapabilityFace* _h, unsigned _i, p2p::CapDesc const& _cap, uint16_t _capID);
91 
93  virtual ~EthereumPeer();
94 
96  static std::string name() { return "eth"; }
97 
99  static u256 version() { return c_protocolVersion; }
100 
102  static unsigned messageCount() { return PacketCount; }
103 
104  void init(unsigned _hostProtocolVersion, u256 _hostNetworkId, u256 _chainTotalDifficulty, h256 _chainCurrentHash, h256 _chainGenesisHash, std::shared_ptr<EthereumHostDataFace> _hostData, std::shared_ptr<EthereumPeerObserverFace> _observer);
105 
106  p2p::NodeID id() const { return session()->id(); }
107 
109  void setIdle();
110 
112  void requestBlockHeaders(h256 const& _startHash, unsigned _count, unsigned _skip, bool _reverse);
113  void requestBlockHeaders(unsigned _startNumber, unsigned _count, unsigned _skip, bool _reverse);
114 
116  void requestBlockBodies(h256s const& _blocks);
117 
119  void requestNodeData(h256s const& _hashes);
120 
122  void requestReceipts(h256s const& _blocks);
123 
125  bool isRude() const;
126 
128  void setRude();
129 
131  void abortSync();
132 
133 private:
135 
137  unsigned askOverride() const;
138 
140  virtual bool interpret(unsigned _id, RLP const& _r);
141 
143  void requestStatus(u256 _hostNetworkId, u256 _chainTotalDifficulty, h256 _chainCurrentHash, h256 _chainGenesisHash);
144 
146  void clearKnownTransactions() { std::lock_guard<std::mutex> l(x_knownTransactions); m_knownTransactions.clear(); }
147 
148  // Request of type _packetType with _hashes as input parameters
149  void requestByHashes(h256s const& _hashes, Asking _asking, SubprotocolPacketType _packetType);
150 
152  void setAsking(Asking _g);
153 
155  bool needsSyncing() const { return !isRude() && !!m_latestHash; }
156 
158  bool isConversing() const;
159 
161  bool isCriticalSyncing() const;
162 
164  void tick();
165 
166  unsigned m_hostProtocolVersion = 0;
167 
170 
173 
177  std::atomic<time_t> m_lastAsk;
178 
183 
185  bool m_requireTransactions = false;
187 
192  unsigned m_unknownNewBlocks = 0;
193  unsigned m_lastAskedHeaders = 0;
194 
195  std::shared_ptr<EthereumPeerObserverFace> m_observer;
196  std::shared_ptr<EthereumHostDataFace> m_hostData;
197 };
198 
199 }
200 }
virtual void onPeerNewBlock(std::shared_ptr< EthereumPeer > _peer, RLP const &_r)=0
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
virtual void onPeerNewHashes(std::shared_ptr< EthereumPeer > _peer, std::vector< std::pair< h256, u256 >> const &_hashes)=0
p2p::NodeID id() const
Definition: EthereumPeer.h:106
u256 const m_peerCapabilityVersion
Protocol version this peer supports received as capability.
Definition: EthereumPeer.h:184
const unsigned c_protocolVersion
Current protocol version.
Definition: Common.cpp:43
u256 m_totalDifficulty
Peer&#39;s latest block&#39;s total difficulty.
Definition: EthereumPeer.h:181
std::pair< std::string, u256 > CapDesc
Definition: Common.h:142
bool needsSyncing() const
Do we presently need syncing with this peer?
Definition: EthereumPeer.h:155
std::vector< std::string > strings
Definition: Common.h:147
The EthereumHost class.
Definition: EthereumHost.h:61
std::atomic< time_t > m_lastAsk
When we asked for it. Allows a time out.
Definition: EthereumPeer.h:177
static u256 version()
What is our version?
Definition: EthereumPeer.h:99
virtual void onPeerReceipts(std::shared_ptr< EthereumPeer > _peer, RLP const &_r)=0
virtual void onPeerNodeData(std::shared_ptr< EthereumPeer > _peer, RLP const &_r)=0
h256Hash m_knownBlocks
Blocks that the peer already knows about (that don&#39;t need to be sent to them).
Definition: EthereumPeer.h:189
u256 m_networkId
Peer&#39;s network id.
Definition: EthereumPeer.h:172
virtual void onPeerBlockBodies(std::shared_ptr< EthereumPeer > _peer, RLP const &_r)=0
unsigned m_protocolVersion
Peer&#39;s protocol version.
Definition: EthereumPeer.h:169
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
h256 m_latestHash
These are determined through either a Status message or from NewBlock.
Definition: EthereumPeer.h:180
Base BlockChain synchronization strategy class.
The EthereumPeer class.
Definition: EthereumPeer.h:83
SubprotocolPacketType
Definition: CommonNet.h:60
void sealAndSend(RLPStream &_s)
Definition: Capability.cpp:48
std::shared_ptr< EthereumPeerObserverFace > m_observer
Definition: EthereumPeer.h:195
static std::string name()
What is our name?
Definition: EthereumPeer.h:96
void clearKnownTransactions()
Clear all known transactions.
Definition: EthereumPeer.h:146
std::shared_ptr< EthereumHostDataFace > m_hostData
Definition: EthereumPeer.h:196
virtual void onPeerStatus(std::shared_ptr< EthereumPeer > _peer)=0
h256Hash m_knownTransactions
Transactions that the peer already knows of.
Definition: EthereumPeer.h:191
std::unordered_set< h256 > h256Hash
Definition: FixedHash.h:349
std::mutex Mutex
Definition: Guards.h:37
std::vector< h256 > h256s
Definition: FixedHash.h:345
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
virtual void onPeerTransactions(std::shared_ptr< EthereumPeer > _peer, RLP const &_r)=0
static unsigned messageCount()
How many message types do we have?
Definition: EthereumPeer.h:102
h256 m_genesisHash
Peer&#39;s genesis hash.
Definition: EthereumPeer.h:182
virtual void onPeerBlockHeaders(std::shared_ptr< EthereumPeer > _peer, RLP const &_headers)=0