Common.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 Common.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <string>
  20. #include <chrono>
  21. #include <libdevcore/Common.h>
  22. #include <libdevcore/Log.h>
  23. #include <libdevcore/RLP.h>
  24. #include <libp2p/Capability.h>
  25. namespace dev
  26. {
  27. namespace shh
  28. {
  29. /* this makes these symbols ambiguous on VS2013
  30. using h256 = dev::h256;
  31. using h256s = dev::h256s;
  32. using bytes = dev::bytes;
  33. using RLPStream = dev::RLPStream;
  34. using RLP = dev::RLP;
  35. using bytesRef = dev::bytesRef;
  36. using bytesConstRef = dev::bytesConstRef;
  37. using h256Set = dev::h256Set;
  38. */
  39. class WhisperHost;
  40. class WhisperPeer;
  41. class Whisper;
  42. class Envelope;
  43. enum WhisperPacket
  44. {
  45. StatusPacket = 0,
  46. MessagesPacket,
  47. TopicFilterPacket,
  48. PacketCount
  49. };
  50. static const unsigned TopicBloomFilterSize = 64;
  51. static const unsigned BitsPerBloom = 3;
  52. static const unsigned WhisperProtocolVersion = 3;
  53. using AbridgedTopic = FixedHash<4>;
  54. using Topic = h256;
  55. using AbridgedTopics = std::vector<AbridgedTopic>;
  56. using Topics = h256s;
  57. using TopicBloomFilterHash = FixedHash<TopicBloomFilterSize>;
  58. AbridgedTopic abridge(Topic const& _topic);
  59. AbridgedTopics abridge(Topics const& _topics);
  60. class BuildTopic
  61. {
  62. public:
  63. BuildTopic() {}
  64. template <class T> BuildTopic(T const& _t) { shift(_t); }
  65. template <class T> BuildTopic& shift(T const& _r) { return shiftBytes(RLPStream().append(_r).out()); }
  66. template <class T> BuildTopic& operator()(T const& _t) { return shift(_t); }
  67. BuildTopic& shiftRaw(h256 const& _part) { m_parts.push_back(_part); return *this; }
  68. operator AbridgedTopics() const { return toAbridgedTopics(); }
  69. operator Topics() const { return toTopics(); }
  70. AbridgedTopics toAbridgedTopics() const;
  71. Topics toTopics() const { return m_parts; }
  72. protected:
  73. BuildTopic& shiftBytes(bytes const& _b);
  74. h256s m_parts;
  75. };
  76. using TopicMask = std::vector<std::pair<AbridgedTopic, AbridgedTopic>>; // where pair::first is the actual abridged topic hash, pair::second is a constant (probably redundunt)
  77. using TopicMasks = std::vector<TopicMask>;
  78. class TopicFilter
  79. {
  80. public:
  81. TopicFilter() {}
  82. TopicFilter(Topics const& _m) { m_topicMasks.push_back(TopicMask()); for (auto const& h: _m) m_topicMasks.back().push_back(std::make_pair(abridge(h), h ? ~AbridgedTopic() : AbridgedTopic())); }
  83. TopicFilter(TopicMask const& _m): m_topicMasks(1, _m) {}
  84. TopicFilter(TopicMasks const& _m): m_topicMasks(_m) {}
  85. TopicFilter(RLP const& _r);
  86. void streamRLP(RLPStream& _s) const { _s << m_topicMasks; }
  87. h256 sha3() const;
  88. bool matches(Envelope const& _m) const;
  89. TopicBloomFilterHash exportBloom() const;
  90. private:
  91. TopicMasks m_topicMasks;
  92. };
  93. class BuildTopicMask: BuildTopic
  94. {
  95. public:
  96. BuildTopicMask() {}
  97. template <class T> BuildTopicMask(T const& _t) { shift(_t); }
  98. template <class T> BuildTopicMask& shift(T const& _r) { BuildTopic::shift(_r); return *this; }
  99. BuildTopicMask& shiftRaw(h256 const& _h) { BuildTopic::shiftRaw(_h); return *this; }
  100. template <class T> BuildTopicMask& operator()(T const& _t) { shift(_t); return *this; }
  101. operator TopicMask() const { return toTopicMask(); }
  102. operator Topics() const { return toTopics(); }
  103. TopicMask toTopicMask() const;
  104. Topics toTopics() const { return m_parts; }
  105. };
  106. }
  107. }