EthereumPeer.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file EthereumPeer.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <mutex>
  20. #include <array>
  21. #include <memory>
  22. #include <utility>
  23. #include <libdevcore/RLP.h>
  24. #include <libdevcore/Guards.h>
  25. #include <libethcore/Common.h>
  26. #include <libp2p/Capability.h>
  27. #include "CommonNet.h"
  28. namespace dev
  29. {
  30. namespace eth
  31. {
  32. /**
  33. * @brief The EthereumPeer class
  34. * @todo Document fully.
  35. * @todo make state transitions thread-safe.
  36. */
  37. class EthereumPeer: public p2p::Capability
  38. {
  39. friend class EthereumHost; //TODO: remove this
  40. friend class BlockChainSync; //TODO: remove this
  41. public:
  42. /// Basic constructor.
  43. EthereumPeer(std::shared_ptr<p2p::Session> _s, p2p::HostCapabilityFace* _h, unsigned _i, p2p::CapDesc const& _cap, uint16_t _capID);
  44. /// Basic destructor.
  45. virtual ~EthereumPeer();
  46. /// What is our name?
  47. static std::string name() { return "eth"; }
  48. /// What is our version?
  49. static u256 version() { return c_protocolVersion; }
  50. /// How many message types do we have?
  51. static unsigned messageCount() { return PacketCount; }
  52. /// What is the ethereum subprotocol host object.
  53. EthereumHost* host() const;
  54. /// Abort sync and reset fetch
  55. void setIdle();
  56. /// Request hashes for given parent hash.
  57. void requestBlockHeaders(h256 const& _startHash, unsigned _count, unsigned _skip, bool _reverse);
  58. void requestBlockHeaders(unsigned _startNumber, unsigned _count, unsigned _skip, bool _reverse);
  59. /// Request specified blocks from peer.
  60. void requestBlockBodies(h256s const& _blocks);
  61. /// Check if this node is rude.
  62. bool isRude() const;
  63. /// Set that it's a rude node.
  64. void setRude();
  65. /// Abort the sync operation.
  66. void abortSync();
  67. private:
  68. using p2p::Capability::sealAndSend;
  69. /// Figure out the amount of blocks we should be asking for.
  70. unsigned askOverride() const;
  71. /// Interpret an incoming message.
  72. virtual bool interpret(unsigned _id, RLP const& _r);
  73. /// Request status. Called from constructor
  74. void requestStatus();
  75. /// Clear all known transactions.
  76. void clearKnownTransactions() { std::lock_guard<std::mutex> l(x_knownTransactions); m_knownTransactions.clear(); }
  77. /// Update our asking state.
  78. void setAsking(Asking _g);
  79. /// Do we presently need syncing with this peer?
  80. bool needsSyncing() const { return !isRude() && !!m_latestHash; }
  81. /// Are we presently in the process of communicating with this peer?
  82. bool isConversing() const;
  83. /// Are we presently in a critical part of the syncing process with this peer?
  84. bool isCriticalSyncing() const;
  85. /// Runs period checks to check up on the peer.
  86. void tick();
  87. /// Peer's protocol version.
  88. unsigned m_protocolVersion;
  89. /// Peer's network id.
  90. u256 m_networkId;
  91. /// What, if anything, we last asked the other peer for.
  92. Asking m_asking = Asking::Nothing;
  93. /// When we asked for it. Allows a time out.
  94. std::atomic<time_t> m_lastAsk;
  95. /// These are determined through either a Status message or from NewBlock.
  96. h256 m_latestHash; ///< Peer's latest block's hash that we know about or default null value if no need to sync.
  97. u256 m_totalDifficulty; ///< Peer's latest block's total difficulty.
  98. h256 m_genesisHash; ///< Peer's genesis hash
  99. /// This is built as we ask for hashes. Once no more hashes are given, we present this to the
  100. /// host who initialises the DownloadMan and m_sub becomes active for us to begin asking for blocks.
  101. u256 m_syncHashNumber = 0; ///< Number of latest hash we sync to (PV61+)
  102. u256 m_height = 0; ///< Chain height
  103. h256 m_syncHash; ///< Latest hash we sync to (PV60)
  104. u256 m_peerCapabilityVersion; ///< Protocol version this peer supports received as capability
  105. /// Have we received a GetTransactions packet that we haven't yet answered?
  106. bool m_requireTransactions = false;
  107. Mutex x_knownBlocks;
  108. h256Hash m_knownBlocks; ///< Blocks that the peer already knows about (that don't need to be sent to them).
  109. Mutex x_knownTransactions;
  110. h256Hash m_knownTransactions; ///< Transactions that the peer already knows of.
  111. unsigned m_unknownNewBlocks = 0; ///< Number of unknown NewBlocks received from this peer
  112. unsigned m_lastAskedHeaders = 0; ///< Number of hashes asked
  113. };
  114. }
  115. }