mirror of
				https://github.com/ethereum/solidity
				synced 2023-10-03 13:03:40 +00:00 
			
		
		
		
	Merge remote-tracking branch 'upstream/develop' into blockTests
This commit is contained in:
		
						commit
						6971bd2af7
					
				| @ -22,7 +22,7 @@ | ||||
| 
 | ||||
| #include <boost/test/unit_test.hpp> | ||||
| #include <libsolidity/CompilerStack.h> | ||||
| #include <jsoncpp/json/json.h> | ||||
| #include <json/json.h> | ||||
| #include <libdevcore/Exceptions.h> | ||||
| 
 | ||||
| namespace dev | ||||
|  | ||||
| @ -668,7 +668,7 @@ BOOST_AUTO_TEST_CASE(mapping_state) | ||||
| 	testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0)); | ||||
| 	testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1)); | ||||
| 	testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2)); | ||||
| 	// voting without vote right shourd be rejected
 | ||||
| 	// voting without vote right should be rejected
 | ||||
| 	testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(2)); | ||||
| 	testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0)); | ||||
| 	testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1)); | ||||
| @ -963,7 +963,7 @@ BOOST_AUTO_TEST_CASE(multiple_elementary_accessors) | ||||
| 	compileAndRun(sourceCode); | ||||
| 	BOOST_CHECK(callContractFunction("data()") == encodeArgs(8)); | ||||
| 	BOOST_CHECK(callContractFunction("name()") == encodeArgs("Celina")); | ||||
| 	BOOST_CHECK(callContractFunction("a_hash()") == encodeArgs(dev::sha3(bytes{0x7b}))); | ||||
| 	BOOST_CHECK(callContractFunction("a_hash()") == encodeArgs(dev::sha3(bytes(1, 0x7b)))); | ||||
| 	BOOST_CHECK(callContractFunction("an_address()") == encodeArgs(toBigEndian(u160(0x1337)))); | ||||
| 	BOOST_CHECK(callContractFunction("super_secret_data()") == bytes()); | ||||
| } | ||||
| @ -2203,7 +2203,7 @@ BOOST_AUTO_TEST_CASE(sha3_multiple_arguments_with_numeric_literals) | ||||
| 					dev::sha3( | ||||
| 						toBigEndian(u256(10)) + | ||||
| 						bytes{0x0, 0xc} + | ||||
| 						bytes{0x91}))); | ||||
| 						bytes(1, 0x91)))); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(sha3_multiple_arguments_with_string_literals) | ||||
| @ -2227,7 +2227,7 @@ BOOST_AUTO_TEST_CASE(sha3_multiple_arguments_with_string_literals) | ||||
| 					dev::sha3( | ||||
| 						toBigEndian(u256(10)) + | ||||
| 						bytes{0x0, 0xc} + | ||||
| 						bytes{0x91} + | ||||
| 						bytes(1, 0x91) + | ||||
| 						bytes{0x66, 0x6f, 0x6f}))); | ||||
| } | ||||
| 
 | ||||
| @ -2254,6 +2254,176 @@ BOOST_AUTO_TEST_CASE(generic_call) | ||||
| 	BOOST_CHECK_EQUAL(m_state.balance(m_contractAddress), 50 - 2); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(store_bytes) | ||||
| { | ||||
| 	// this test just checks that the copy loop does not mess up the stack
 | ||||
| 	char const* sourceCode = R"( | ||||
| 		contract C { | ||||
| 			function save() returns (uint r) { | ||||
| 				r = 23; | ||||
| 				savedData = msg.data; | ||||
| 				r = 24; | ||||
| 			} | ||||
| 			bytes savedData; | ||||
| 		} | ||||
| 	)"; | ||||
| 	compileAndRun(sourceCode); | ||||
| 	// empty copy loop
 | ||||
| 	BOOST_CHECK(callContractFunction("save()") == encodeArgs(24)); | ||||
| 	BOOST_CHECK(callContractFunction("save()", "abcdefg") == encodeArgs(24)); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(call_forward_bytes) | ||||
| { | ||||
| 	char const* sourceCode = R"( | ||||
| 		contract receiver { | ||||
| 			uint public received; | ||||
| 			function receive(uint x) { received += x + 1; } | ||||
| 			function() { received = 0x80; } | ||||
| 		} | ||||
| 		contract sender { | ||||
| 			function sender() { rec = new receiver(); } | ||||
| 			function() { savedData = msg.data; } | ||||
| 			function forward() returns (bool) { rec.call(savedData); return true; } | ||||
| 			function clear() returns (bool) { delete savedData; return true; } | ||||
| 			function val() returns (uint) { return rec.received(); } | ||||
| 			receiver rec; | ||||
| 			bytes savedData; | ||||
| 		} | ||||
| 	)"; | ||||
| 	compileAndRun(sourceCode, 0, "sender"); | ||||
| 	BOOST_CHECK(callContractFunction("receive(uint256)", 7) == bytes()); | ||||
| 	BOOST_CHECK(callContractFunction("val()") == encodeArgs(0)); | ||||
| 	BOOST_CHECK(callContractFunction("forward()") == encodeArgs(true)); | ||||
| 	BOOST_CHECK(callContractFunction("val()") == encodeArgs(8)); | ||||
| 	BOOST_CHECK(callContractFunction("clear()") == encodeArgs(true)); | ||||
| 	BOOST_CHECK(callContractFunction("val()") == encodeArgs(8)); | ||||
| 	BOOST_CHECK(callContractFunction("forward()") == encodeArgs(true)); | ||||
| 	BOOST_CHECK(callContractFunction("val()") == encodeArgs(0x80)); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(copying_bytes_multiassign) | ||||
| { | ||||
| 	char const* sourceCode = R"( | ||||
| 		contract receiver { | ||||
| 			uint public received; | ||||
| 			function receive(uint x) { received += x + 1; } | ||||
| 			function() { received = 0x80; } | ||||
| 		} | ||||
| 		contract sender { | ||||
| 			function sender() { rec = new receiver(); } | ||||
| 			function() { savedData1 = savedData2 = msg.data; } | ||||
| 			function forward(bool selector) returns (bool) { | ||||
| 				if (selector) { rec.call(savedData1); delete savedData1; } | ||||
| 				else { rec.call(savedData2); delete savedData2; } | ||||
| 				return true; | ||||
| 			} | ||||
| 			function val() returns (uint) { return rec.received(); } | ||||
| 			receiver rec; | ||||
| 			bytes savedData1; | ||||
| 			bytes savedData2; | ||||
| 		} | ||||
| 	)"; | ||||
| 	compileAndRun(sourceCode, 0, "sender"); | ||||
| 	BOOST_CHECK(callContractFunction("receive(uint256)", 7) == bytes()); | ||||
| 	BOOST_CHECK(callContractFunction("val()") == encodeArgs(0)); | ||||
| 	BOOST_CHECK(callContractFunction("forward(bool)", true) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(callContractFunction("val()") == encodeArgs(8)); | ||||
| 	BOOST_CHECK(callContractFunction("forward(bool)", false) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(callContractFunction("val()") == encodeArgs(16)); | ||||
| 	BOOST_CHECK(callContractFunction("forward(bool)", true) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(callContractFunction("val()") == encodeArgs(0x80)); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(delete_removes_bytes_data) | ||||
| { | ||||
| 	char const* sourceCode = R"( | ||||
| 		contract c { | ||||
| 			function() { data = msg.data; } | ||||
| 			function del() returns (bool) { delete data; return true; } | ||||
| 			bytes data; | ||||
| 		} | ||||
| 	)"; | ||||
| 	compileAndRun(sourceCode); | ||||
| 	BOOST_CHECK(callContractFunction("---", 7) == bytes()); | ||||
| 	BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); | ||||
| 	BOOST_CHECK(callContractFunction("del()", 7) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(m_state.storage(m_contractAddress).empty()); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(copy_from_calldata_removes_bytes_data) | ||||
| { | ||||
| 	char const* sourceCode = R"( | ||||
| 		contract c { | ||||
| 			function set() returns (bool) { data = msg.data; return true; } | ||||
| 			function() { data = msg.data; } | ||||
| 			bytes data; | ||||
| 		} | ||||
| 	)"; | ||||
| 	compileAndRun(sourceCode); | ||||
| 	BOOST_CHECK(callContractFunction("set()", 1, 2, 3, 4, 5) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); | ||||
| 	sendMessage(bytes(), false); | ||||
| 	BOOST_CHECK(m_output == bytes()); | ||||
| 	BOOST_CHECK(m_state.storage(m_contractAddress).empty()); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(copy_removes_bytes_data) | ||||
| { | ||||
| 	char const* sourceCode = R"( | ||||
| 		contract c { | ||||
| 			function set() returns (bool) { data1 = msg.data; return true; } | ||||
| 			function reset() returns (bool) { data1 = data2; return true; } | ||||
| 			bytes data1; | ||||
| 			bytes data2; | ||||
| 		} | ||||
| 	)"; | ||||
| 	compileAndRun(sourceCode); | ||||
| 	BOOST_CHECK(callContractFunction("set()", 1, 2, 3, 4, 5) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); | ||||
| 	BOOST_CHECK(callContractFunction("reset()") == encodeArgs(true)); | ||||
| 	BOOST_CHECK(m_state.storage(m_contractAddress).empty()); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(bytes_inside_mappings) | ||||
| { | ||||
| 	char const* sourceCode = R"( | ||||
| 		contract c { | ||||
| 			function set(uint key) returns (bool) { data[key] = msg.data; return true; } | ||||
| 			function copy(uint from, uint to) returns (bool) { data[to] = data[from]; return true; } | ||||
| 			mapping(uint => bytes) data; | ||||
| 		} | ||||
| 	)"; | ||||
| 	compileAndRun(sourceCode); | ||||
| 	// store a short byte array at 1 and a longer one at 2
 | ||||
| 	BOOST_CHECK(callContractFunction("set(uint256)", 1, 2) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(callContractFunction("set(uint256)", 2, 2, 3, 4, 5) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); | ||||
| 	// copy shorter to longer
 | ||||
| 	BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 1, 2) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); | ||||
| 	// copy empty to both
 | ||||
| 	BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 99, 1) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(!m_state.storage(m_contractAddress).empty()); | ||||
| 	BOOST_CHECK(callContractFunction("copy(uint256,uint256)", 99, 2) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(m_state.storage(m_contractAddress).empty()); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(bytes_length_member) | ||||
| { | ||||
| 	char const* sourceCode = R"( | ||||
| 		contract c { | ||||
| 			function set() returns (bool) { data = msg.data; return true; } | ||||
| 			function getLength() returns (uint) { return data.length; } | ||||
| 			bytes data; | ||||
| 		} | ||||
| 	)"; | ||||
| 	compileAndRun(sourceCode); | ||||
| 	BOOST_CHECK(callContractFunction("getLength()") == encodeArgs(0)); | ||||
| 	BOOST_CHECK(callContractFunction("set()", 1, 2) == encodeArgs(true)); | ||||
| 	BOOST_CHECK(callContractFunction("getLength()") == encodeArgs(4+32+32)); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_SUITE_END() | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -21,7 +21,7 @@ | ||||
|  */ | ||||
| 
 | ||||
| #include <boost/test/unit_test.hpp> | ||||
| #include <jsoncpp/json/json.h> | ||||
| #include <json/json.h> | ||||
| #include <libsolidity/CompilerStack.h> | ||||
| #include <libsolidity/Exceptions.h> | ||||
| #include <libdevcore/Exceptions.h> | ||||
|  | ||||
							
								
								
									
										19
									
								
								net.cpp
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								net.cpp
									
									
									
									
									
								
							| @ -30,7 +30,7 @@ using namespace dev::p2p; | ||||
| namespace ba = boost::asio; | ||||
| namespace bi = ba::ip; | ||||
| 
 | ||||
| BOOST_AUTO_TEST_SUITE(p2p) | ||||
| BOOST_AUTO_TEST_SUITE(net) | ||||
| 
 | ||||
| /**
 | ||||
|  * Only used for testing. Not useful beyond tests. | ||||
| @ -87,7 +87,7 @@ struct TestNodeTable: public NodeTable | ||||
| 		bi::address ourIp = bi::address::from_string("127.0.0.1"); | ||||
| 		for (auto& n: _testNodes) | ||||
| 			if (_count--) | ||||
| 				noteNode(n.first.pub(), bi::udp::endpoint(ourIp, n.second)); | ||||
| 				noteActiveNode(n.first.pub(), bi::udp::endpoint(ourIp, n.second)); | ||||
| 			else | ||||
| 				break; | ||||
| 	} | ||||
| @ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(test_neighbours_packet) | ||||
| 	out.sign(k.sec()); | ||||
| 
 | ||||
| 	bytesConstRef packet(out.data.data(), out.data.size()); | ||||
| 	bytesConstRef rlpBytes(packet.cropped(97, packet.size() - 97)); | ||||
| 	bytesConstRef rlpBytes(packet.cropped(h256::size + Signature::size + 1)); | ||||
| 	Neighbours in = Neighbours::fromBytesConstRef(to, rlpBytes); | ||||
| 	int count = 0; | ||||
| 	for (auto n: in.nodes) | ||||
| @ -182,7 +182,7 @@ BOOST_AUTO_TEST_CASE(kademlia) | ||||
| 	// Not yet a 'real' test.
 | ||||
| 	TestNodeTableHost node(8); | ||||
| 	node.start(); | ||||
| 	node.nodeTable->join(); // ideally, joining with empty node table logs warning we can check for
 | ||||
| 	node.nodeTable->discover(); // ideally, joining with empty node table logs warning we can check for
 | ||||
| 	node.setup(); | ||||
| 	node.populate(); | ||||
| 	clog << "NodeTable:\n" << *node.nodeTable.get() << endl; | ||||
| @ -190,15 +190,24 @@ BOOST_AUTO_TEST_CASE(kademlia) | ||||
| 	node.populateAll(); | ||||
| 	clog << "NodeTable:\n" << *node.nodeTable.get() << endl; | ||||
| 	 | ||||
| 	auto nodes = node.nodeTable->nodes(); | ||||
| 	nodes.sort(); | ||||
| 	 | ||||
| 	node.nodeTable->reset(); | ||||
| 	clog << "NodeTable:\n" << *node.nodeTable.get() << endl; | ||||
| 
 | ||||
| 	node.populate(1); | ||||
| 	clog << "NodeTable:\n" << *node.nodeTable.get() << endl; | ||||
| 	 | ||||
| 	node.nodeTable->join(); | ||||
| 	node.nodeTable->discover(); | ||||
| 	this_thread::sleep_for(chrono::milliseconds(2000)); | ||||
| 	clog << "NodeTable:\n" << *node.nodeTable.get() << endl; | ||||
| 
 | ||||
| 	BOOST_REQUIRE_EQUAL(node.nodeTable->count(), 8); | ||||
| 	 | ||||
| 	auto netNodes = node.nodeTable->nodes(); | ||||
| 	netNodes.sort(); | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(test_udp_once) | ||||
|  | ||||
							
								
								
									
										78
									
								
								peer.cpp
									
									
									
									
									
								
							
							
						
						
									
										78
									
								
								peer.cpp
									
									
									
									
									
								
							| @ -20,6 +20,7 @@ | ||||
|  * Peer Network test functions. | ||||
|  */ | ||||
| 
 | ||||
| #include <boost/test/unit_test.hpp> | ||||
| #include <chrono> | ||||
| #include <thread> | ||||
| #include <libp2p/Host.h> | ||||
| @ -27,12 +28,74 @@ using namespace std; | ||||
| using namespace dev; | ||||
| using namespace dev::p2p; | ||||
| 
 | ||||
| BOOST_AUTO_TEST_SUITE(p2p) | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(host) | ||||
| { | ||||
| 	NetworkPreferences host1prefs(30301, "127.0.0.1", true, true); | ||||
| 	NetworkPreferences host2prefs(30302, "127.0.0.1", true, true); | ||||
| 	 | ||||
| 	Host host1("Test", host1prefs); | ||||
| 	host1.start(); | ||||
| 		 | ||||
| 	Host host2("Test", host2prefs); | ||||
| 	auto node2 = host2.id(); | ||||
| 	host2.start(); | ||||
| 	 | ||||
| 	host1.addNode(node2, "127.0.0.1", host2prefs.listenPort, host2prefs.listenPort); | ||||
| 	 | ||||
| 	this_thread::sleep_for(chrono::seconds(1)); | ||||
| 	 | ||||
| 	BOOST_REQUIRE_EQUAL(host1.peerCount(), 1); | ||||
| 	BOOST_REQUIRE_EQUAL(host2.peerCount(), host1.peerCount()); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_CASE(save_nodes) | ||||
| { | ||||
| 	std::list<Host*> hosts; | ||||
| 	for (auto i:{0,1,2,3,4,5}) | ||||
| 	{ | ||||
| 		Host* h = new Host("Test", NetworkPreferences(30300 + i, "127.0.0.1", true, true)); | ||||
| 		// starting host is required so listenport is available
 | ||||
| 		h->start(); | ||||
| 		while (!h->isStarted()) | ||||
| 			this_thread::sleep_for(chrono::milliseconds(2)); | ||||
| 		hosts.push_back(h); | ||||
| 	} | ||||
| 	 | ||||
| 	Host& host = *hosts.front(); | ||||
| 	for (auto const& h: hosts) | ||||
| 		host.addNode(h->id(), "127.0.0.1", h->listenPort(), h->listenPort()); | ||||
| 	 | ||||
| 	Host& host2 = *hosts.back(); | ||||
| 	for (auto const& h: hosts) | ||||
| 		host2.addNode(h->id(), "127.0.0.1", h->listenPort(), h->listenPort()); | ||||
| 
 | ||||
| 	this_thread::sleep_for(chrono::milliseconds(1000)); | ||||
| 	bytes firstHostNetwork(host.saveNetwork()); | ||||
| 	bytes secondHostNetwork(host.saveNetwork()); | ||||
| 	 | ||||
| 	BOOST_REQUIRE_EQUAL(sha3(firstHostNetwork), sha3(secondHostNetwork)); | ||||
| 	 | ||||
| 	BOOST_CHECK_EQUAL(host.peerCount(), 5); | ||||
| 	BOOST_CHECK_EQUAL(host2.peerCount(), 5); | ||||
| 	 | ||||
| 	RLP r(firstHostNetwork); | ||||
| 	BOOST_REQUIRE(r.itemCount() == 3); | ||||
| 	BOOST_REQUIRE(r[0].toInt<int>() == 1); | ||||
| 	BOOST_REQUIRE_EQUAL(r[1].toBytes().size(), 32); // secret
 | ||||
| 	BOOST_REQUIRE_EQUAL(r[2].itemCount(), 5); | ||||
| } | ||||
| 
 | ||||
| BOOST_AUTO_TEST_SUITE_END() | ||||
| 
 | ||||
| int peerTest(int argc, char** argv) | ||||
| { | ||||
| 	Public remoteAlias; | ||||
| 	short listenPort = 30303; | ||||
| 	string remoteHost; | ||||
| 	short remotePort = 30303; | ||||
| 
 | ||||
| 	 | ||||
| 	for (int i = 1; i < argc; ++i) | ||||
| 	{ | ||||
| 		string arg = argv[i]; | ||||
| @ -42,21 +105,18 @@ int peerTest(int argc, char** argv) | ||||
| 			remoteHost = argv[++i]; | ||||
| 		else if (arg == "-p" && i + 1 < argc) | ||||
| 			remotePort = (short)atoi(argv[++i]); | ||||
| 		else if (arg == "-ra" && i + 1 < argc) | ||||
| 			remoteAlias = Public(dev::fromHex(argv[++i])); | ||||
| 		else | ||||
| 			remoteHost = argv[i]; | ||||
| 	} | ||||
| 
 | ||||
| 	Host ph("Test", NetworkPreferences(listenPort)); | ||||
| 
 | ||||
| 	if (!remoteHost.empty()) | ||||
| 		ph.connect(remoteHost, remotePort); | ||||
| 	if (!remoteHost.empty() && !remoteAlias) | ||||
| 		ph.addNode(remoteAlias, remoteHost, remotePort, remotePort); | ||||
| 
 | ||||
| 	for (int i = 0; ; ++i) | ||||
| 	{ | ||||
| 		this_thread::sleep_for(chrono::milliseconds(100)); | ||||
| 		if (!(i % 10)) | ||||
| 			ph.pingAll(); | ||||
| 	} | ||||
| 	this_thread::sleep_for(chrono::milliseconds(200)); | ||||
| 
 | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| @ -139,6 +139,7 @@ private: | ||||
| 		return encode(_cppFunction(_arguments...)); | ||||
| 	} | ||||
| 
 | ||||
| protected: | ||||
| 	void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0) | ||||
| 	{ | ||||
| 		m_state.addBalance(m_sender, _value); // just in case
 | ||||
| @ -171,7 +172,6 @@ private: | ||||
| 		m_logs = executive.logs(); | ||||
| 	} | ||||
| 
 | ||||
| protected: | ||||
| 	bool m_optimize = false; | ||||
| 	bool m_addStandardSources = false; | ||||
| 	Address m_sender; | ||||
|  | ||||
							
								
								
									
										157
									
								
								whisperTopic.cpp
									
									
									
									
									
								
							
							
						
						
									
										157
									
								
								whisperTopic.cpp
									
									
									
									
									
								
							| @ -36,27 +36,31 @@ BOOST_AUTO_TEST_CASE(topic) | ||||
| 	auto oldLogVerbosity = g_logVerbosity; | ||||
| 	g_logVerbosity = 0; | ||||
| 
 | ||||
| 	Host host1("Test", NetworkPreferences(30303, "127.0.0.1", false, true)); | ||||
| 	auto whost1 = host1.registerCapability(new WhisperHost()); | ||||
| 	host1.start(); | ||||
| 	 | ||||
| 	while (!host1.isStarted()) | ||||
| 		this_thread::sleep_for(chrono::milliseconds(2)); | ||||
| 	 | ||||
| 	bool started = false; | ||||
| 	unsigned result = 0; | ||||
| 	std::thread listener([&]() | ||||
| 	{ | ||||
| 		setThreadName("other"); | ||||
| 
 | ||||
| 		Host ph("Test", NetworkPreferences(50303, "", false, true)); | ||||
| 		auto wh = ph.registerCapability(new WhisperHost()); | ||||
| 		ph.start(); | ||||
| 		started = true; | ||||
| 
 | ||||
| 		/// Only interested in odd packets
 | ||||
| 		auto w = wh->installWatch(BuildTopicMask("odd")); | ||||
| 		auto w = whost1->installWatch(BuildTopicMask("odd")); | ||||
| 
 | ||||
| 		started = true; | ||||
| 		set<unsigned> received; | ||||
| 
 | ||||
| 		for (int iterout = 0, last = 0; iterout < 200 && last < 81; ++iterout) | ||||
| 		{ | ||||
| 			for (auto i: wh->checkWatch(w)) | ||||
| 			for (auto i: whost1->checkWatch(w)) | ||||
| 			{ | ||||
| 				Message msg = wh->envelope(i).open(wh->fullTopic(w)); | ||||
| 				Message msg = whost1->envelope(i).open(whost1->fullTopic(w)); | ||||
| 				last = RLP(msg.payload()).toInt<unsigned>(); | ||||
| 				if (received.count(last)) | ||||
| 					continue; | ||||
| @ -66,22 +70,28 @@ BOOST_AUTO_TEST_CASE(topic) | ||||
| 			} | ||||
| 			this_thread::sleep_for(chrono::milliseconds(50)); | ||||
| 		} | ||||
| 		 | ||||
| 	}); | ||||
| 	 | ||||
| 	Host host2("Test", NetworkPreferences(30300, "127.0.0.1", false, true)); | ||||
| 	auto whost2 = host2.registerCapability(new WhisperHost()); | ||||
| 	host2.start(); | ||||
| 	 | ||||
| 	while (!host2.isStarted()) | ||||
| 		this_thread::sleep_for(chrono::milliseconds(2)); | ||||
| 	 | ||||
| 	this_thread::sleep_for(chrono::milliseconds(100)); | ||||
| 	host2.addNode(host1.id(), "127.0.0.1", 30303, 30303); | ||||
| 	 | ||||
| 	this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 
 | ||||
| 	while (!started) | ||||
| 		this_thread::sleep_for(chrono::milliseconds(50)); | ||||
| 
 | ||||
| 	Host ph("Test", NetworkPreferences(50300, "", false, true)); | ||||
| 	shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost()); | ||||
| 	this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 	ph.start(); | ||||
| 	this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 	ph.connect("127.0.0.1", 50303); | ||||
| 		this_thread::sleep_for(chrono::milliseconds(2)); | ||||
| 
 | ||||
| 	KeyPair us = KeyPair::create(); | ||||
| 	for (int i = 0; i < 10; ++i) | ||||
| 	{ | ||||
| 		wh->post(us.sec(), RLPStream().append(i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even")); | ||||
| 		whost2->post(us.sec(), RLPStream().append(i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even")); | ||||
| 		this_thread::sleep_for(chrono::milliseconds(250)); | ||||
| 	} | ||||
| 
 | ||||
| @ -96,7 +106,15 @@ BOOST_AUTO_TEST_CASE(forwarding) | ||||
| 	cnote << "Testing Whisper forwarding..."; | ||||
| 	auto oldLogVerbosity = g_logVerbosity; | ||||
| 	g_logVerbosity = 0; | ||||
| 
 | ||||
| 	 | ||||
| 	// Host must be configured not to share peers.
 | ||||
| 	Host host1("Listner", NetworkPreferences(30303, "", false, true)); | ||||
| 	host1.setIdealPeerCount(0); | ||||
| 	auto whost1 = host1.registerCapability(new WhisperHost()); | ||||
| 	host1.start(); | ||||
| 	while (!host1.isStarted()) | ||||
| 		this_thread::sleep_for(chrono::milliseconds(2)); | ||||
| 	 | ||||
| 	unsigned result = 0; | ||||
| 	bool done = false; | ||||
| 
 | ||||
| @ -105,22 +123,16 @@ BOOST_AUTO_TEST_CASE(forwarding) | ||||
| 	{ | ||||
| 		setThreadName("listener"); | ||||
| 
 | ||||
| 		// Host must be configured not to share peers.
 | ||||
| 		Host ph("Listner", NetworkPreferences(50303, "", false, true)); | ||||
| 		ph.setIdealPeerCount(0); | ||||
| 		auto wh = ph.registerCapability(new WhisperHost()); | ||||
| 		ph.start(); | ||||
| 
 | ||||
| 		startedListener = true; | ||||
| 
 | ||||
| 		/// Only interested in odd packets
 | ||||
| 		auto w = wh->installWatch(BuildTopicMask("test")); | ||||
| 		auto w = whost1->installWatch(BuildTopicMask("test")); | ||||
| 
 | ||||
| 		for (int i = 0; i < 200 && !result; ++i) | ||||
| 		{ | ||||
| 			for (auto i: wh->checkWatch(w)) | ||||
| 			for (auto i: whost1->checkWatch(w)) | ||||
| 			{ | ||||
| 				Message msg = wh->envelope(i).open(wh->fullTopic(w)); | ||||
| 				Message msg = whost1->envelope(i).open(whost1->fullTopic(w)); | ||||
| 				unsigned last = RLP(msg.payload()).toInt<unsigned>(); | ||||
| 				cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>(); | ||||
| 				result = last; | ||||
| @ -129,6 +141,16 @@ BOOST_AUTO_TEST_CASE(forwarding) | ||||
| 		} | ||||
| 	}); | ||||
| 
 | ||||
| 	 | ||||
| 	// Host must be configured not to share peers.
 | ||||
| 	Host host2("Forwarder", NetworkPreferences(30305, "", false, true)); | ||||
| 	host2.setIdealPeerCount(1); | ||||
| 	auto whost2 = host2.registerCapability(new WhisperHost()); | ||||
| 	host2.start(); | ||||
| 	while (!host2.isStarted()) | ||||
| 		this_thread::sleep_for(chrono::milliseconds(2)); | ||||
| 	 | ||||
| 	Public fwderid; | ||||
| 	bool startedForwarder = false; | ||||
| 	std::thread forwarder([&]() | ||||
| 	{ | ||||
| @ -137,26 +159,19 @@ BOOST_AUTO_TEST_CASE(forwarding) | ||||
| 		while (!startedListener) | ||||
| 			this_thread::sleep_for(chrono::milliseconds(50)); | ||||
| 
 | ||||
| 		// Host must be configured not to share peers.
 | ||||
| 		Host ph("Forwarder", NetworkPreferences(50305, "", false, true)); | ||||
| 		ph.setIdealPeerCount(0); | ||||
| 		auto wh = ph.registerCapability(new WhisperHost()); | ||||
| 		this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 		ph.start(); | ||||
| 
 | ||||
| 		this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 		ph.connect("127.0.0.1", 50303); | ||||
| 		host2.addNode(host1.id(), "127.0.0.1", 30303, 30303); | ||||
| 
 | ||||
| 		startedForwarder = true; | ||||
| 
 | ||||
| 		/// Only interested in odd packets
 | ||||
| 		auto w = wh->installWatch(BuildTopicMask("test")); | ||||
| 		auto w = whost2->installWatch(BuildTopicMask("test")); | ||||
| 
 | ||||
| 		while (!done) | ||||
| 		{ | ||||
| 			for (auto i: wh->checkWatch(w)) | ||||
| 			for (auto i: whost2->checkWatch(w)) | ||||
| 			{ | ||||
| 				Message msg = wh->envelope(i).open(wh->fullTopic(w)); | ||||
| 				Message msg = whost2->envelope(i).open(whost2->fullTopic(w)); | ||||
| 				cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>(); | ||||
| 			} | ||||
| 			this_thread::sleep_for(chrono::milliseconds(50)); | ||||
| @ -166,13 +181,13 @@ BOOST_AUTO_TEST_CASE(forwarding) | ||||
| 	while (!startedForwarder) | ||||
| 		this_thread::sleep_for(chrono::milliseconds(50)); | ||||
| 
 | ||||
| 	Host ph("Sender", NetworkPreferences(50300, "", false, true)); | ||||
| 	ph.setIdealPeerCount(0); | ||||
| 	Host ph("Sender", NetworkPreferences(30300, "", false, true)); | ||||
| 	ph.setIdealPeerCount(1); | ||||
| 	shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost()); | ||||
| 	this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 	ph.start(); | ||||
| 	this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 	ph.connect("127.0.0.1", 50305); | ||||
| 	ph.addNode(host2.id(), "127.0.0.1", 30305, 30305); | ||||
| 	while (!ph.isStarted()) | ||||
| 		this_thread::sleep_for(chrono::milliseconds(10)); | ||||
| 
 | ||||
| 	KeyPair us = KeyPair::create(); | ||||
| 	wh->post(us.sec(), RLPStream().append(1).out(), BuildTopic("test")); | ||||
| @ -194,32 +209,33 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) | ||||
| 
 | ||||
| 	unsigned result = 0; | ||||
| 	bool done = false; | ||||
| 	 | ||||
| 	// Host must be configured not to share peers.
 | ||||
| 	Host host1("Forwarder", NetworkPreferences(30305, "", false, true)); | ||||
| 	host1.setIdealPeerCount(1); | ||||
| 	auto whost1 = host1.registerCapability(new WhisperHost()); | ||||
| 	host1.start(); | ||||
| 	while (!host1.isStarted()) | ||||
| 		this_thread::sleep_for(chrono::milliseconds(2)); | ||||
| 
 | ||||
| 	bool startedForwarder = false; | ||||
| 	std::thread forwarder([&]() | ||||
| 	{ | ||||
| 		setThreadName("forwarder"); | ||||
| 
 | ||||
| 		// Host must be configured not to share peers.
 | ||||
| 		Host ph("Forwarder", NetworkPreferences(50305, "", false, true)); | ||||
| 		ph.setIdealPeerCount(0); | ||||
| 		auto wh = ph.registerCapability(new WhisperHost()); | ||||
| 		 | ||||
| 		this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 		ph.start(); | ||||
| 
 | ||||
| 		this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 		ph.connect("127.0.0.1", 50303); | ||||
| //		ph.addNode("127.0.0.1", 30303, 30303);
 | ||||
| 
 | ||||
| 		startedForwarder = true; | ||||
| 
 | ||||
| 		/// Only interested in odd packets
 | ||||
| 		auto w = wh->installWatch(BuildTopicMask("test")); | ||||
| 		auto w = whost1->installWatch(BuildTopicMask("test")); | ||||
| 
 | ||||
| 		while (!done) | ||||
| 		{ | ||||
| 			for (auto i: wh->checkWatch(w)) | ||||
| 			for (auto i: whost1->checkWatch(w)) | ||||
| 			{ | ||||
| 				Message msg = wh->envelope(i).open(wh->fullTopic(w)); | ||||
| 				Message msg = whost1->envelope(i).open(whost1->fullTopic(w)); | ||||
| 				cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>(); | ||||
| 			} | ||||
| 			this_thread::sleep_for(chrono::milliseconds(50)); | ||||
| @ -227,30 +243,33 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) | ||||
| 	}); | ||||
| 
 | ||||
| 	while (!startedForwarder) | ||||
| 		this_thread::sleep_for(chrono::milliseconds(50)); | ||||
| 
 | ||||
| 		this_thread::sleep_for(chrono::milliseconds(2)); | ||||
| 	 | ||||
| 	{ | ||||
| 		Host ph("Sender", NetworkPreferences(50300, "", false, true)); | ||||
| 		ph.setIdealPeerCount(0); | ||||
| 		shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost()); | ||||
| 		this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 		ph.start(); | ||||
| 		this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 		ph.connect("127.0.0.1", 50305); | ||||
| 		Host host2("Sender", NetworkPreferences(30300, "", false, true)); | ||||
| 		host2.setIdealPeerCount(1); | ||||
| 		shared_ptr<WhisperHost> whost2 = host2.registerCapability(new WhisperHost()); | ||||
| 		host2.start(); | ||||
| 		while (!host2.isStarted()) | ||||
| 			this_thread::sleep_for(chrono::milliseconds(2)); | ||||
| 		host2.addNode(host1.id(), "127.0.0.1", 30305, 30305); | ||||
| 
 | ||||
| 		while (!host2.peerCount()) | ||||
| 			this_thread::sleep_for(chrono::milliseconds(5)); | ||||
| 		 | ||||
| 		KeyPair us = KeyPair::create(); | ||||
| 		wh->post(us.sec(), RLPStream().append(1).out(), BuildTopic("test")); | ||||
| 		whost2->post(us.sec(), RLPStream().append(1).out(), BuildTopic("test")); | ||||
| 		this_thread::sleep_for(chrono::milliseconds(250)); | ||||
| 	} | ||||
| 
 | ||||
| 	{ | ||||
| 		Host ph("Listener", NetworkPreferences(50300, "", false, true)); | ||||
| 		ph.setIdealPeerCount(0); | ||||
| 		Host ph("Listener", NetworkPreferences(30300, "", false, true)); | ||||
| 		ph.setIdealPeerCount(1); | ||||
| 		shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost()); | ||||
| 		this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 		ph.start(); | ||||
| 		this_thread::sleep_for(chrono::milliseconds(500)); | ||||
| 		ph.connect("127.0.0.1", 50305); | ||||
| 		while (!ph.isStarted()) | ||||
| 			this_thread::sleep_for(chrono::milliseconds(2)); | ||||
| 		ph.addNode(host1.id(), "127.0.0.1", 30305, 30305); | ||||
| 
 | ||||
| 		/// Only interested in odd packets
 | ||||
| 		auto w = wh->installWatch(BuildTopicMask("test")); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user