Fabcoin Core  0.16.2
P2P Digital Currency
Common.cpp
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 */
22 #include "Common.h"
23 #include <libdevcore/SHA3.h>
24 #include "Message.h"
25 #include "BloomFilter.h"
26 
27 using namespace std;
28 using namespace dev;
29 using namespace dev::p2p;
30 using namespace dev::shh;
31 
33 {
34  return AbridgedTopic(sha3(_p));
35 }
36 
38 {
39  AbridgedTopics ret;
40  ret.reserve(_topics.size());
41  for (auto const& t: _topics)
42  ret.push_back(abridge(t));
43  return ret;
44 }
45 
46 AbridgedTopics BuildTopic::toAbridgedTopics() const
47 {
48  AbridgedTopics ret;
49  ret.reserve(m_parts.size());
50  for (auto const& h: m_parts)
51  ret.push_back(abridge(h));
52  return ret;
53 }
54 
55 BuildTopic& BuildTopic::shiftBytes(bytes const& _b)
56 {
57  m_parts.push_back(dev::sha3(_b));
58  return *this;
59 }
60 
62 {
63  RLPStream s;
64  streamRLP(s);
65  return dev::sha3(s.out());
66 }
67 
68 bool TopicFilter::matches(Envelope const& _e) const
69 {
70  for (TopicMask const& t: m_topicMasks)
71  {
72  for (unsigned i = 0; i < t.size(); ++i)
73  {
74  for (auto et: _e.topic())
75  if (((t[i].first ^ et) & t[i].second) == AbridgedTopic())
76  goto NEXT_TOPICPART;
77  // failed to match topicmask against any topics: move on to next mask
78  goto NEXT_TOPICMASK;
79  NEXT_TOPICPART:;
80  }
81  // all topicmasks matched.
82  return true;
83  NEXT_TOPICMASK:;
84  }
85  return false;
86 }
87 
88 TopicFilter::TopicFilter(RLP const& _r)
89 {
90  for (RLP const& i: _r)
91  {
92  m_topicMasks.push_back(TopicMask());
93  for (RLP const& j: i)
94  m_topicMasks.back().push_back(j.toPair<FixedHash<4>, FixedHash<4>>());
95  }
96 }
97 
98 TopicBloomFilterHash TopicFilter::exportBloom() const
99 {
101  for (TopicMask const& t: m_topicMasks)
102  for (auto const& i: t)
103  ret |= TopicBloomFilter::bloom(i.first);
104 
105  return ret;
106 }
107 
108 TopicMask BuildTopicMask::toTopicMask() const
109 {
110  TopicMask ret;
111  ret.reserve(m_parts.size());
112  for (auto const& h: m_parts)
113  ret.push_back(make_pair(abridge(h), ~AbridgedTopic()));
114  return ret;
115 }
116 
117 /*
118 web3.shh.watch({}).arrived(function(m) { env.note("New message:\n"+JSON.stringify(m)); })
119 k = web3.shh.newIdentity()
120 web3.shh.post({from: k, topic: web3.fromAscii("test"), payload: web3.fromAscii("Hello world!")})
121 */
122 
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::vector< AbridgedTopic > AbridgedTopics
Definition: Common.h:68
#define h(i)
Definition: sha.cpp:736
bytes const & out() const
Read the byte stream.
Definition: RLP.h:433
h256s Topics
Definition: Common.h:69
std::hash for asio::adress
Definition: Common.h:323
Definition: Eth.h:45
AbridgedTopics const & topic() const
Definition: Message.h:76
std::vector< byte > bytes
Definition: Common.h:75
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
AbridgedTopic abridge(Topic const &_topic)
Definition: Common.cpp:32
std::vector< std::pair< AbridgedTopic, AbridgedTopic >> TopicMask
Definition: Common.h:98
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
FixedHash< 4 > AbridgedTopic
Definition: Common.h:65
Class for writing to an RLP bytestream.
Definition: RLP.h:383
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
LogBloom bloom(LogEntries const &_logs)
Definition: ExtVMFace.h:158