Fabcoin Core  0.16.2
P2P Digital Currency
Common.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 */
22 #pragma once
23 
24 #include <string>
25 #include <chrono>
26 
27 #include <libdevcore/Common.h>
28 #include <libdevcore/Log.h>
29 #include <libdevcore/RLP.h>
30 #include <libp2p/Capability.h>
31 
32 namespace dev
33 {
34 namespace shh
35 {
36 
37 /* this makes these symbols ambiguous on VS2013
38 using h256 = dev::h256;
39 using h256s = dev::h256s;
40 using bytes = dev::bytes;
41 using RLPStream = dev::RLPStream;
42 using RLP = dev::RLP;
43 using bytesRef = dev::bytesRef;
44 using bytesConstRef = dev::bytesConstRef;
45 using h256Set = dev::h256Set;
46 */
47 
48 class WhisperHost;
49 class WhisperPeer;
50 class Whisper;
51 class Envelope;
52 
54 {
59 };
60 
61 static const unsigned TopicBloomFilterSize = 64;
62 static const unsigned BitsPerBloom = 3;
63 static const unsigned WhisperProtocolVersion = 3;
64 
66 using Topic = h256;
67 
68 using AbridgedTopics = std::vector<AbridgedTopic>;
69 using Topics = h256s;
70 
72 
73 AbridgedTopic abridge(Topic const& _topic);
74 AbridgedTopics abridge(Topics const& _topics);
75 
77 {
78 public:
80  template <class T> BuildTopic(T const& _t) { shift(_t); }
81 
82  template <class T> BuildTopic& shift(T const& _r) { return shiftBytes(RLPStream().append(_r).out()); }
83  template <class T> BuildTopic& operator()(T const& _t) { return shift(_t); }
84 
85  BuildTopic& shiftRaw(h256 const& _part) { m_parts.push_back(_part); return *this; }
86 
87  operator AbridgedTopics() const { return toAbridgedTopics(); }
88  operator Topics() const { return toTopics(); }
90  Topics toTopics() const { return m_parts; }
91 
92 protected:
93  BuildTopic& shiftBytes(bytes const& _b);
94 
96 };
97 
98 using TopicMask = std::vector<std::pair<AbridgedTopic, AbridgedTopic>>; // where pair::first is the actual abridged topic hash, pair::second is a constant (probably redundunt)
99 using TopicMasks = std::vector<TopicMask>;
100 
102 {
103 public:
105  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())); }
106  TopicFilter(TopicMask const& _m): m_topicMasks(1, _m) {}
107  TopicFilter(TopicMasks const& _m): m_topicMasks(_m) {}
108  TopicFilter(RLP const& _r);
109 
110  void streamRLP(RLPStream& _s) const { _s << m_topicMasks; }
111  h256 sha3() const;
112  bool matches(Envelope const& _m) const;
113  TopicBloomFilterHash exportBloom() const;
114 
115 private:
117 };
118 
120 {
121 public:
123  template <class T> BuildTopicMask(T const& _t) { shift(_t); }
124 
125  template <class T> BuildTopicMask& shift(T const& _r) { BuildTopic::shift(_r); return *this; }
126  BuildTopicMask& shiftRaw(h256 const& _h) { BuildTopic::shiftRaw(_h); return *this; }
127 
128  template <class T> BuildTopicMask& operator()(T const& _t) { shift(_t); return *this; }
129 
130  operator TopicMask() const { return toTopicMask(); }
131  operator Topics() const { return toTopics(); }
132  TopicMask toTopicMask() const;
133  Topics toTopics() const { return m_parts; }
134 };
135 
136 }
137 }
BuildTopicMask & shiftRaw(h256 const &_h)
Definition: Common.h:126
TopicFilter(Topics const &_m)
Definition: Common.h:105
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
BuildTopicMask(T const &_t)
Definition: Common.h:123
void streamRLP(RLPStream &_s) const
Definition: Common.h:110
#define T(i, x)
std::vector< AbridgedTopic > AbridgedTopics
Definition: Common.h:68
#define h(i)
Definition: sha.cpp:736
BuildTopic & shiftRaw(h256 const &_part)
Definition: Common.h:85
h256s Topics
Definition: Common.h:69
BuildTopic & operator()(T const &_t)
Definition: Common.h:83
BuildTopicMask & shift(T const &_r)
Definition: Common.h:125
BuildTopicMask & operator()(T const &_t)
Definition: Common.h:128
std::vector< TopicMask > TopicMasks
Definition: Common.h:99
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
TopicFilter(TopicMask const &_m)
Definition: Common.h:106
TopicFilter(TopicMasks const &_m)
Definition: Common.h:107
FixedHash< 32 > h256
Definition: FixedHash.h:340
TopicMasks m_topicMasks
Definition: Common.h:116
BuildTopic(T const &_t)
Definition: Common.h:80
BuildTopic & shiftBytes(bytes const &_b)
Definition: Common.cpp:55
AbridgedTopic abridge(Topic const &_topic)
Definition: Common.cpp:32
Topics toTopics() const
Definition: Common.h:90
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
WhisperPacket
Definition: Common.h:53
FixedHash< 4 > AbridgedTopic
Definition: Common.h:65
Topics toTopics() const
Definition: Common.h:133
AbridgedTopics toAbridgedTopics() const
Definition: Common.cpp:46
std::vector< h256 > h256s
Definition: FixedHash.h:345
Class for writing to an RLP bytestream.
Definition: RLP.h:383
BuildTopic & shift(T const &_r)
Definition: Common.h:82
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64