Ethereum  PoC-8
The C++ Implementation of Ethereum
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 */
17 
18 #pragma once
19 
20 #include "CommonNet.h"
21 
22 namespace dev
23 {
24 namespace p2p
25 {
26 class CapabilityHostFace;
27 }
28 
29 namespace eth
30 {
32 {
33 public:
34  EthereumPeer() = default;
35  EthereumPeer(std::shared_ptr<p2p::CapabilityHostFace> _host, NodeID const& _peerID,
36  u256 const& /*_capabilityVersion*/)
37  : m_host(std::move(_host)), m_id(_peerID)
38  {}
39 
40  void setStatus(unsigned _protocolVersion, u256 const& _networkId, u256 const& _totalDifficulty,
41  h256 const& _latestHash, h256 const& _genesisHash);
42 
43  std::string validate(h256 const& _hostGenesisHash, unsigned _hostProtocolVersion,
44  u256 const& _hostNetworkId) const;
45 
46  NodeID id() const { return m_id; }
47 
48  u256 totalDifficulty() const { return m_totalDifficulty; }
49 
50  time_t lastAsk() const { return m_lastAsk; }
51  void setLastAsk(time_t _lastAsk) { m_lastAsk = _lastAsk; }
52 
53  Asking asking() const { return m_asking; }
54  bool isConversing() const { return m_asking != Asking::Nothing; }
55  void setAsking(Asking _asking) { m_asking = _asking; }
56 
57  h256 latestHash() const { return m_latestHash; }
58  void setLatestHash(h256 const& _hash) { m_latestHash = _hash; }
59 
60  bool isWaitingForTransactions() const { return m_requireTransactions; }
61  void setWaitingForTransactions(bool _value) { m_requireTransactions = _value; }
62 
63  bool isTransactionKnown(h256 const& _hash) const { return m_knownTransactions.count(_hash); }
64  void markTransactionAsKnown(h256 const& _hash) { m_knownTransactions.insert(_hash); }
65 
66  bool isBlockKnown(h256 const& _hash) const { return m_knownBlocks.count(_hash); }
67  void markBlockAsKnown(h256 const& _hash) { m_knownBlocks.insert(_hash); }
68  void clearKnownBlocks() { m_knownBlocks.clear(); }
69 
70  unsigned unknownNewBlocks() const { return m_unknownNewBlocks; }
71  void incrementUnknownNewBlocks() { ++m_unknownNewBlocks; }
72 
73  void requestStatus(u256 _hostNetworkId, u256 _chainTotalDifficulty, h256 _chainCurrentHash,
74  h256 _chainGenesPeersh);
75 
78  h256 const& _startHash, unsigned _count, unsigned _skip, bool _reverse);
79  void requestBlockHeaders(unsigned _startNumber, unsigned _count, unsigned _skip, bool _reverse);
80 
82  void requestBlockBodies(h256s const& _blocks);
83 
85  void requestNodeData(h256s const& _hashes);
86 
88  void requestReceipts(h256s const& _blocks);
89 
90 private:
91  // Request of type _packetType with _hashes as input parameters
92  void requestByHashes(h256s const& _hashes, Asking _asking, SubprotocolPacketType _packetType);
93 
94  std::shared_ptr<p2p::CapabilityHostFace> m_host;
95 
96  NodeID const m_id;
97 
99  Asking m_asking = Asking::Nothing;
101  time_t m_lastAsk = 0;
103  unsigned m_protocolVersion = 0;
105  u256 m_networkId;
108  h256 m_latestHash;
110  u256 m_totalDifficulty;
111  h256 m_genesisHash;
112  bool m_requireTransactions = false;
114 
116  h256Hash m_knownBlocks;
117  h256Hash m_knownTransactions;
118  unsigned m_unknownNewBlocks = 0;
119  unsigned m_lastAskedHeaders = 0;
120 
121  Logger m_logger{createLogger(VerbosityDebug, "peer")};
122 };
123 } // namespace eth
124 } // namespace dev
dev::eth::EthereumPeer::requestBlockHeaders
void requestBlockHeaders(h256 const &_startHash, unsigned _count, unsigned _skip, bool _reverse)
Request hashes for given parent hash.
Definition: EthereumPeer.cpp:113
dev::eth::EthereumPeer::requestBlockBodies
void requestBlockBodies(h256s const &_blocks)
Request specified blocks from peer.
Definition: EthereumPeer.cpp:131
dev::eth::EthereumPeer::unknownNewBlocks
unsigned unknownNewBlocks() const
Definition: EthereumPeer.h:70
dev::eth::EthereumPeer::requestReceipts
void requestReceipts(h256s const &_blocks)
Request receipts for specified blocks from peer.
Definition: EthereumPeer.cpp:141
dev::eth::EthereumPeer::markBlockAsKnown
void markBlockAsKnown(h256 const &_hash)
Definition: EthereumPeer.h:67
dev::FixedHash< 32 >
dev::eth::EthereumPeer::incrementUnknownNewBlocks
void incrementUnknownNewBlocks()
Definition: EthereumPeer.h:71
dev::eth::EthereumPeer::setWaitingForTransactions
void setWaitingForTransactions(bool _value)
Definition: EthereumPeer.h:61
dev::eth::EthereumPeer::validate
std::string validate(h256 const &_hostGenesisHash, unsigned _hostProtocolVersion, u256 const &_hostNetworkId) const
Definition: EthereumPeer.cpp:67
dev::eth::SubprotocolPacketType
SubprotocolPacketType
Definition: CommonNet.h:54
dev::h256Hash
std::unordered_set< h256 > h256Hash
Definition: FixedHash.h:365
dev::eth::EthereumPeer
Definition: EthereumPeer.h:32
dev::h256s
std::vector< h256 > h256s
Definition: FixedHash.h:361
dev::Logger
boost::log::sources::severity_channel_logger<> Logger
Definition: Log.h:124
dev::eth::EthereumPeer::requestNodeData
void requestNodeData(h256s const &_hashes)
Request values for specified keys from peer.
Definition: EthereumPeer.cpp:136
dev::eth::EthereumPeer::isTransactionKnown
bool isTransactionKnown(h256 const &_hash) const
Definition: EthereumPeer.h:63
dev::eth::EthereumPeer::EthereumPeer
EthereumPeer(std::shared_ptr< p2p::CapabilityHostFace > _host, NodeID const &_peerID, u256 const &)
Definition: EthereumPeer.h:35
dev::eth::Asking
Asking
Definition: CommonNet.h:73
dev::createLogger
Logger createLogger(int _severity, std::string const &_channel)
Definition: Log.h:125
dev::eth::NodeID
p2p::NodeID NodeID
Definition: CommonNet.h:105
dev::eth::EthereumPeer::setLatestHash
void setLatestHash(h256 const &_hash)
Definition: EthereumPeer.h:58
dev::eth::EthereumPeer::setLastAsk
void setLastAsk(time_t _lastAsk)
Definition: EthereumPeer.h:51
dev::eth::EthereumPeer::id
NodeID id() const
Definition: EthereumPeer.h:46
dev::eth::EthereumPeer::isWaitingForTransactions
bool isWaitingForTransactions() const
Definition: EthereumPeer.h:60
dev::eth::EthereumPeer::requestStatus
void requestStatus(u256 _hostNetworkId, u256 _chainTotalDifficulty, h256 _chainCurrentHash, h256 _chainGenesPeersh)
Definition: EthereumPeer.cpp:83
dev::VerbosityDebug
@ VerbosityDebug
Definition: Log.h:71
dev::eth::EthereumPeer::asking
Asking asking() const
Definition: EthereumPeer.h:53
dev::eth::EthereumPeer::setStatus
void setStatus(unsigned _protocolVersion, u256 const &_networkId, u256 const &_totalDifficulty, h256 const &_latestHash, h256 const &_genesisHash)
Definition: EthereumPeer.cpp:56
dev::eth::EthereumPeer::markTransactionAsKnown
void markTransactionAsKnown(h256 const &_hash)
Definition: EthereumPeer.h:64
dev::eth::EthereumPeer::EthereumPeer
EthereumPeer()=default
dev::eth::EthereumPeer::isBlockKnown
bool isBlockKnown(h256 const &_hash) const
Definition: EthereumPeer.h:66
std
Definition: FixedHash.h:393
dev::eth::EthereumPeer::clearKnownBlocks
void clearKnownBlocks()
Definition: EthereumPeer.h:68
dev::u256
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
dev::eth::EthereumPeer::isConversing
bool isConversing() const
Definition: EthereumPeer.h:54
dev
Definition: Address.cpp:21
dev::eth::EthereumPeer::setAsking
void setAsking(Asking _asking)
Definition: EthereumPeer.h:55
CommonNet.h
dev::eth::EthereumPeer::totalDifficulty
u256 totalDifficulty() const
Definition: EthereumPeer.h:48
dev::eth::EthereumPeer::lastAsk
time_t lastAsk() const
Definition: EthereumPeer.h:50
dev::eth::Asking::Nothing
@ Nothing
dev::eth::EthereumPeer::latestHash
h256 latestHash() const
Definition: EthereumPeer.h:57