Common.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include "Common.h"
  19. #include <libdevcore/SHA3.h>
  20. #include "Message.h"
  21. #include "BloomFilter.h"
  22. using namespace std;
  23. using namespace dev;
  24. using namespace dev::p2p;
  25. using namespace dev::shh;
  26. AbridgedTopic dev::shh::abridge(Topic const& _p)
  27. {
  28. return AbridgedTopic(sha3(_p));
  29. }
  30. AbridgedTopics dev::shh::abridge(Topics const& _topics)
  31. {
  32. AbridgedTopics ret;
  33. ret.reserve(_topics.size());
  34. for (auto const& t: _topics)
  35. ret.push_back(abridge(t));
  36. return ret;
  37. }
  38. AbridgedTopics BuildTopic::toAbridgedTopics() const
  39. {
  40. AbridgedTopics ret;
  41. ret.reserve(m_parts.size());
  42. for (auto const& h: m_parts)
  43. ret.push_back(abridge(h));
  44. return ret;
  45. }
  46. BuildTopic& BuildTopic::shiftBytes(bytes const& _b)
  47. {
  48. m_parts.push_back(dev::sha3(_b));
  49. return *this;
  50. }
  51. h256 TopicFilter::sha3() const
  52. {
  53. RLPStream s;
  54. streamRLP(s);
  55. return dev::sha3(s.out());
  56. }
  57. bool TopicFilter::matches(Envelope const& _e) const
  58. {
  59. for (TopicMask const& t: m_topicMasks)
  60. {
  61. for (unsigned i = 0; i < t.size(); ++i)
  62. {
  63. for (auto et: _e.topic())
  64. if (((t[i].first ^ et) & t[i].second) == AbridgedTopic())
  65. goto NEXT_TOPICPART;
  66. // failed to match topicmask against any topics: move on to next mask
  67. goto NEXT_TOPICMASK;
  68. NEXT_TOPICPART:;
  69. }
  70. // all topicmasks matched.
  71. return true;
  72. NEXT_TOPICMASK:;
  73. }
  74. return false;
  75. }
  76. TopicFilter::TopicFilter(RLP const& _r)
  77. {
  78. for (RLP const& i: _r)
  79. {
  80. m_topicMasks.push_back(TopicMask());
  81. for (RLP const& j: i)
  82. m_topicMasks.back().push_back(j.toPair<FixedHash<4>, FixedHash<4>>());
  83. }
  84. }
  85. TopicBloomFilterHash TopicFilter::exportBloom() const
  86. {
  87. TopicBloomFilterHash ret;
  88. for (TopicMask const& t: m_topicMasks)
  89. for (auto const& i: t)
  90. ret |= TopicBloomFilter::bloom(i.first);
  91. return ret;
  92. }
  93. TopicMask BuildTopicMask::toTopicMask() const
  94. {
  95. TopicMask ret;
  96. ret.reserve(m_parts.size());
  97. for (auto const& h: m_parts)
  98. ret.push_back(make_pair(abridge(h), ~AbridgedTopic()));
  99. return ret;
  100. }
  101. /*
  102. web3.shh.watch({}).arrived(function(m) { env.note("New message:\n"+JSON.stringify(m)); })
  103. k = web3.shh.newIdentity()
  104. web3.shh.post({from: k, topic: web3.fromAscii("test"), payload: web3.fromAscii("Hello world!")})
  105. */