Ethereum  PoC-8
The C++ Implementation of Ethereum
WarpCapability.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 */
17 
18 #pragma once
19 
20 #include "CommonNet.h"
21 #include <libp2p/Capability.h>
22 #include <libp2p/CapabilityHost.h>
23 
24 namespace dev
25 {
26 namespace eth
27 {
28 class SnapshotStorageFace;
29 
30 unsigned const c_WarpProtocolVersion = 1;
31 
33 {
38  SnapshotData = 0x14,
39 
41 };
42 
44 {
46  unsigned m_protocolVersion = 0;
47 
50 
54  time_t m_lastAsk;
55 
62 };
63 
64 
66 {
67 public:
68  virtual ~WarpPeerObserverFace() {}
69 
70  virtual void onPeerStatus(NodeID const& _peerID) = 0;
71 
72  virtual void onPeerManifest(NodeID const& _peerID, RLP const& _r) = 0;
73 
74  virtual void onPeerBlockHeaders(NodeID const& _peerID, RLP const& _r) = 0;
75 
76  virtual void onPeerData(NodeID const& _peerID, RLP const& _r) = 0;
77 
78  virtual void onPeerDisconnect(NodeID const& _peerID, Asking _asking) = 0;
79 };
80 
81 
82 class WarpCapability : public p2p::CapabilityFace
83 {
84 public:
85  WarpCapability(std::shared_ptr<p2p::CapabilityHostFace> _host, BlockChain const& _blockChain,
86  u256 const& _networkId, boost::filesystem::path const& _snapshotDownloadPath,
87  std::shared_ptr<SnapshotStorageFace> _snapshotStorage);
88 
89  std::string name() const override { return "par"; }
90  u256 version() const override { return c_WarpProtocolVersion; }
91  unsigned messageCount() const override { return WarpSubprotocolPacketCount; }
92 
93  void onStarting() override;
94  void onStopping() override;
95 
96  unsigned protocolVersion() const { return c_WarpProtocolVersion; }
97  u256 networkId() const { return m_networkId; }
98 
99  void onConnect(NodeID const& _peerID, u256 const& _peerCapabilityVersion) override;
100  bool interpretCapabilityPacket(NodeID const& _peerID, unsigned _id, RLP const&) override;
101  void onDisconnect(NodeID const& _peerID) override;
102 
103  p2p::CapabilityHostFace& capabilityHost() { return *m_host; }
104 
105  void requestStatus(NodeID const& _peerID, unsigned _hostProtocolVersion,
106  u256 const& _hostNetworkId, u256 const& _chainTotalDifficulty,
107  h256 const& _chainCurrentHash, h256 const& _chainGenesisHash,
108  h256 const& _snapshotBlockHash, u256 const& _snapshotBlockNumber);
109  void requestBlockHeaders(NodeID const& _peerID, unsigned _startNumber, unsigned _count,
110  unsigned _skip, bool _reverse);
111  void requestManifest(NodeID const& _peerID);
112  bool requestData(NodeID const& _peerID, h256 const& _chunkHash);
113 
115  bool validateStatus(NodeID const& _peerID, h256 const& _genesisHash,
116  std::vector<unsigned> const& _protocolVersions, u256 const& _networkId);
117 
118  void disablePeer(NodeID const& _peerID, std::string const& _problem);
119 
120 private:
121  std::shared_ptr<WarpPeerObserverFace> createPeerObserver(
122  boost::filesystem::path const& _snapshotDownloadPath);
123 
124  void doBackgroundWork();
125 
126  void setAsking(NodeID const& _peerID, Asking _a);
127 
128  void setIdle(NodeID const& _peerID) { setAsking(_peerID, Asking::Nothing); }
129 
130  std::shared_ptr<p2p::CapabilityHostFace> m_host;
131 
132  BlockChain const& m_blockChain;
133  u256 const m_networkId;
134 
135  std::shared_ptr<SnapshotStorageFace> m_snapshot;
136  std::shared_ptr<WarpPeerObserverFace> m_peerObserver;
137 
138  std::unordered_map<NodeID, WarpPeerStatus> m_peers;
139 
140  std::atomic<bool> m_backgroundWorkEnabled = {false};
141 };
142 
143 } // namespace eth
144 } // namespace dev
void disablePeer(NodeID const &_peerID, std::string const &_problem)
Definition: Address.cpp:20
bool interpretCapabilityPacket(NodeID const &_peerID, unsigned _id, RLP const &) override
virtual void onPeerDisconnect(NodeID const &_peerID, Asking _asking)=0
WarpSubprotocolPacketType
uint8_t byte
Definition: Common.h:57
unsigned protocolVersion() const
WarpCapability(std::shared_ptr< p2p::CapabilityHostFace > _host, BlockChain const &_blockChain, u256 const &_networkId, boost::filesystem::path const &_snapshotDownloadPath, std::shared_ptr< SnapshotStorageFace > _snapshotStorage)
Implements the blockchain database. All data this gives is disk-backed. .
Definition: BlockChain.h:104
u256 m_totalDifficulty
Peer&#39;s latest block&#39;s total difficulty.
Asking m_asking
What, if anything, we last asked the other peer for.
virtual void onPeerBlockHeaders(NodeID const &_peerID, RLP const &_r)=0
h256 m_genesisHash
Peer&#39;s genesis hash.
void requestBlockHeaders(NodeID const &_peerID, unsigned _startNumber, unsigned _count, unsigned _skip, bool _reverse)
time_t m_lastAsk
When we asked for it. Allows a time out.
void requestManifest(NodeID const &_peerID)
h256 m_latestHash
These are determined through either a Status message.
unsigned m_protocolVersion
Peer&#39;s protocol version.
virtual void onPeerData(NodeID const &_peerID, RLP const &_r)=0
u256 version() const override
p2p::NodeID NodeID
Definition: CommonNet.h:105
void onConnect(NodeID const &_peerID, u256 const &_peerCapabilityVersion) override
p2p::CapabilityHostFace & capabilityHost()
unsigned const c_WarpProtocolVersion
void onDisconnect(NodeID const &_peerID) override
void requestStatus(NodeID const &_peerID, unsigned _hostProtocolVersion, u256 const &_hostNetworkId, u256 const &_chainTotalDifficulty, h256 const &_chainCurrentHash, h256 const &_chainGenesisHash, h256 const &_snapshotBlockHash, u256 const &_snapshotBlockNumber)
bool validateStatus(NodeID const &_peerID, h256 const &_genesisHash, std::vector< unsigned > const &_protocolVersions, u256 const &_networkId)
Validates whether peer is able to communicate with the host, disables peer if not.
unsigned messageCount() const override
virtual void onPeerManifest(NodeID const &_peerID, RLP const &_r)=0
u256 m_networkId
Peer&#39;s network id.
std::string name() const override
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
Definition: RLP.h:47
virtual void onPeerStatus(NodeID const &_peerID)=0
bool requestData(NodeID const &_peerID, h256 const &_chunkHash)