Fabcoin Core  0.16.2
P2P Digital Currency
EthereumPeerTest.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 */
17 
19 #include <libp2p/Host.h>
21 
22 using namespace std;
23 using namespace dev;
24 using namespace dev::eth;
25 using namespace dev::p2p;
26 using namespace dev::test;
27 
29 {
30 protected:
31  string name() const override { return "mock capability name"; }
32  u256 version() const override { return 0; }
33  unsigned messageCount() const override { return 0; }
34  shared_ptr<Capability> newPeerCapability(shared_ptr<SessionFace> const&, unsigned, CapDesc const&, uint16_t) override { return shared_ptr<Capability>(); }
35 };
36 
37 class MockSession: public SessionFace
38 {
39 public:
40  void start() override { }
41  void disconnect(DisconnectReason /*_reason*/) override { }
42 
43  void ping() override { }
44 
45  bool isConnected() const override { return true; }
46 
47  NodeID id() const override { return {}; }
48 
49  void sealAndSend(RLPStream& _s, uint16_t /*_protocolID*/) override
50  {
51  _s.swapOut(m_bytesSent);
52  }
53 
54  int rating() const override { return 0; }
55  void addRating(int /*_r*/) override { }
56 
57  void addNote(string const& _k, string const& _v) override
58  {
59  m_notes[_k] = _v;
60  }
61 
62  PeerSessionInfo info() const override { return PeerSessionInfo{ NodeID{}, "", "", 0, chrono::steady_clock::duration{}, {}, 0, {}, 0 }; }
64 
65  void registerCapability(CapDesc const& /*_desc*/, shared_ptr<Capability> /*_p*/) override { }
66  void registerFraming(uint16_t /*_id*/) override { }
67 
68  map<CapDesc, shared_ptr<Capability>> const& capabilities() const override { return m_capabilities; }
69 
70  shared_ptr<Peer> peer() const override { return shared_ptr<Peer>(); }
71 
73 
74  ReputationManager& repMan() override { return m_repMan; }
75 
77  map<CapDesc, std::shared_ptr<Capability>> m_capabilities;
79  map<string, string> m_notes;
80 };
81 
82 
84 {
85 public:
86  void onPeerStatus(std::shared_ptr<EthereumPeer>) override {}
87 
88  void onPeerTransactions(std::shared_ptr<EthereumPeer>, RLP const&) override {}
89 
90  void onPeerBlockHeaders(std::shared_ptr<EthereumPeer>, RLP const&) override {}
91 
92  void onPeerBlockBodies(std::shared_ptr<EthereumPeer>, RLP const&) override {}
93 
94  void onPeerNewHashes(std::shared_ptr<EthereumPeer>, std::vector<std::pair<h256, u256>> const&) override {}
95 
96  void onPeerNewBlock(std::shared_ptr<EthereumPeer>, RLP const&) override {}
97 
98  void onPeerNodeData(std::shared_ptr<EthereumPeer>, RLP const&) override {}
99 
100  void onPeerReceipts(std::shared_ptr<EthereumPeer>, RLP const&) override {}
101 
102  void onPeerAborting() override {}
103 };
104 
106 {
107 public:
109  session(make_shared<MockSession>()),
110  observer(make_shared<MockEthereumPeerObserver>()),
111  offset(UserPacket),
112  peer(session, &hostCap, offset, { "eth", 0 }, 0)
113  {
114  peer.init(63, 2, 0, h256(0), h256(0), shared_ptr<EthereumHostDataFace>(), observer);
115  }
116 
118  shared_ptr<MockSession> session;
119  shared_ptr<MockEthereumPeerObserver> observer;
120  uint8_t offset;
122 };
123 
125 
126 BOOST_AUTO_TEST_CASE(EthereumPeerSuite_requestNodeData)
127 {
128  h256 dataHash("0x949d991d685738352398dff73219ab19c62c06e6f8ce899fbae755d5127ed1ef");
129  peer.requestNodeData({ dataHash });
130 
131  uint8_t code = static_cast<uint8_t>(session->m_bytesSent[0]);
132  BOOST_REQUIRE_EQUAL(code, offset + 0x0d);
133 
134  bytes payloadSent(session->m_bytesSent.begin() + 1, session->m_bytesSent.end());
135  RLP rlp(payloadSent);
136  BOOST_REQUIRE(rlp.isList());
137  BOOST_REQUIRE_EQUAL(rlp.itemCount(), 1);
138  BOOST_REQUIRE_EQUAL(static_cast<h256>(rlp[0]), dataHash);
139 }
140 
141 BOOST_AUTO_TEST_CASE(EthereumPeerSuite_requestNodeDataSeveralHashes)
142 {
143  h256 dataHash0("0x949d991d685738352398dff73219ab19c62c06e6f8ce899fbae755d5127ed1ef");
144  h256 dataHash1("0x0e4562a10381dec21b205ed72637e6b1b523bdd0e4d4d50af5cd23dd4500a217");
145  h256 dataHash2("0x53ab44f45948543775a4c405085b918e5e648db1201283bb54a59701afdaedf3");
146  peer.requestNodeData({ dataHash0, dataHash1, dataHash2 });
147 
148  bytes payloadSent(session->m_bytesSent.begin() + 1, session->m_bytesSent.end());
149  RLP rlp(payloadSent);
150  BOOST_REQUIRE_EQUAL(rlp.itemCount(), 3);
151  BOOST_REQUIRE_EQUAL(static_cast<h256>(rlp[0]), dataHash0);
152  BOOST_REQUIRE_EQUAL(static_cast<h256>(rlp[1]), dataHash1);
153  BOOST_REQUIRE_EQUAL(static_cast<h256>(rlp[2]), dataHash2);
154 }
155 
156 BOOST_AUTO_TEST_CASE(EthereumPeerSuite_requestNodeDataAddsAskingNote)
157 {
158  h256 dataHash("0x949d991d685738352398dff73219ab19c62c06e6f8ce899fbae755d5127ed1ef");
159  peer.requestNodeData({ h256("0x949d991d685738352398dff73219ab19c62c06e6f8ce899fbae755d5127ed1ef") });
160 
161  BOOST_REQUIRE(session->m_notes.find("ask") != session->m_notes.end());
162  BOOST_REQUIRE(session->m_notes["ask"] == "NodeData");
163 }
164 
165 BOOST_AUTO_TEST_CASE(EthereumPeerSuite_requestNodeDataWithNoHashesSetsAskNoteToNothing)
166 {
167  peer.requestNodeData({});
168 
169  BOOST_REQUIRE(session->m_notes.find("ask") != session->m_notes.end());
170  BOOST_REQUIRE(session->m_notes["ask"] == "Nothing");
171 }
172 
173 BOOST_AUTO_TEST_CASE(EthereumPeerSuite_requestReceipts)
174 {
175  h256 blockHash0("0x949d991d685738352398dff73219ab19c62c06e6f8ce899fbae755d5127ed1ef");
176  h256 blockHash1("0x0e4562a10381dec21b205ed72637e6b1b523bdd0e4d4d50af5cd23dd4500a217");
177  h256 blockHash2("0x53ab44f45948543775a4c405085b918e5e648db1201283bb54a59701afdaedf3");
178  peer.requestReceipts({ blockHash0, blockHash1, blockHash2 });
179 
180  bytes payloadSent(session->m_bytesSent.begin() + 1, session->m_bytesSent.end());
181  RLP rlp(payloadSent);
182  BOOST_REQUIRE_EQUAL(rlp.itemCount(), 3);
183  BOOST_REQUIRE_EQUAL(static_cast<h256>(rlp[0]), blockHash0);
184  BOOST_REQUIRE_EQUAL(static_cast<h256>(rlp[1]), blockHash1);
185  BOOST_REQUIRE_EQUAL(static_cast<h256>(rlp[2]), blockHash2);
186 }
187 
ReputationManager m_repMan
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
void disconnect(DisconnectReason) override
string name() const override
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:467
void start() override
std::pair< std::string, u256 > CapDesc
Definition: Common.h:142
PeerSessionInfo info() const override
BOOST_AUTO_TEST_CASE(EthereumPeerSuite_requestNodeData)
chrono::steady_clock::time_point lastReceived() const override
void onPeerReceipts(std::shared_ptr< EthereumPeer >, RLP const &) override
void onPeerNodeData(std::shared_ptr< EthereumPeer >, RLP const &) override
shared_ptr< MockEthereumPeerObserver > observer
void ping() override
std::hash for asio::adress
Definition: Common.h:323
bool isConnected() const override
void onPeerBlockHeaders(std::shared_ptr< EthereumPeer >, RLP const &) override
void onPeerBlockBodies(std::shared_ptr< EthereumPeer >, RLP const &) override
bytes code
Definition: SmartVM.cpp:45
shared_ptr< MockSession > session
unsigned messageCount() const override
DisconnectReason
Definition: Common.h:106
shared_ptr< Capability > newPeerCapability(shared_ptr< SessionFace > const &, unsigned, CapDesc const &, uint16_t) override
void registerCapability(CapDesc const &, shared_ptr< Capability >) override
ReputationManager & repMan() override
clock::duration duration
Definition: bench.h:50
void addNote(string const &_k, string const &_v) override
NodeID id() const override
std::vector< byte > bytes
Definition: Common.h:75
shared_ptr< Peer > peer() const override
FixedHash< 32 > h256
Definition: FixedHash.h:340
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
u256 version() const override
The EthereumPeer class.
Definition: EthereumPeer.h:83
MockHostCapability hostCap
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
chrono::steady_clock::time_point connectionTime() override
clock::time_point time_point
Definition: bench.h:49
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
void onPeerNewBlock(std::shared_ptr< EthereumPeer >, RLP const &) override
void onPeerTransactions(std::shared_ptr< EthereumPeer >, RLP const &) override
void addRating(int) override
map< CapDesc, shared_ptr< Capability > > const & capabilities() const override
map< CapDesc, std::shared_ptr< Capability > > m_capabilities
void swapOut(bytes &_dest)
Swap the contents of the output stream out for some other byte array.
Definition: RLP.h:439
Class for writing to an RLP bytestream.
Definition: RLP.h:383
int rating() const override
map< string, string > m_notes
void registerFraming(uint16_t) override
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
void onPeerNewHashes(std::shared_ptr< EthereumPeer >, std::vector< std::pair< h256, u256 >> const &) override
void onPeerStatus(std::shared_ptr< EthereumPeer >) override
Helper functions to work with json::spirit and test files.
void sealAndSend(RLPStream &_s, uint16_t) override