Skip to content

Commit

Permalink
Merge pull request #3791 from devos50/market_dht
Browse files Browse the repository at this point in the history
Removed hard-coded addresses in TrustChain blocks from market
  • Loading branch information
qstokkink authored Aug 20, 2018
2 parents d3fa427 + ee41bb4 commit 6095c7e
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 202 deletions.
3 changes: 2 additions & 1 deletion Tribler/Core/APIImplementation/LaunchManyCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,14 @@ def load_ipv8_overlays(self):
self.ipv8.strategies.append((RandomWalk(self.tunnel_community), 20))

# Market Community
if self.session.config.get_market_community_enabled():
if self.session.config.get_market_community_enabled() and self.session.config.get_dht_enabled():
from Tribler.community.market.community import MarketCommunity, MarketTestnetCommunity

community_cls = MarketTestnetCommunity if self.session.config.get_testnet() else MarketCommunity
self.market_community = community_cls(peer, self.ipv8.endpoint, self.ipv8.network,
tribler_session=self.session,
trustchain=self.trustchain_community,
dht=self.dht_community,
wallets=self.wallets,
working_directory=self.session.config.get_state_dir())

Expand Down
2 changes: 0 additions & 2 deletions Tribler/Test/Community/Market/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def setUp(self, annotate=True):
OrderId(TraderId('1' * 40), OrderNumber(1)), Timestamp(0.0))

ask_tx = self.ask.to_block_dict()
ask_tx["address"], ask_tx["port"] = "127.0.0.1", 1337
bid_tx = self.bid.to_block_dict()
bid_tx["address"], bid_tx["port"] = "127.0.0.1", 1337

self.tick_block = MarketBlock()
self.tick_block.type = 'tick'
Expand Down
73 changes: 54 additions & 19 deletions Tribler/Test/Community/Market/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from Tribler.pyipv8.ipv8.test.base import TestBase
from Tribler.pyipv8.ipv8.test.mocking.ipv8 import MockIPv8
from Tribler.pyipv8.ipv8.test.util import twisted_wrapper
from twisted.internet.defer import fail
from twisted.internet.defer import fail, succeed


class TestMarketCommunityBase(TestBase):
Expand All @@ -34,7 +34,7 @@ def create_node(self):

wallets = {'DUM1': dum1_wallet, 'DUM2': dum2_wallet}

mock_ipv8 = MockIPv8(u"curve25519", MarketCommunity, create_trustchain=True,
mock_ipv8 = MockIPv8(u"curve25519", MarketCommunity, create_trustchain=True, create_dht=True,
is_matchmaker=True, wallets=wallets, use_database=False, working_directory=u":memory:")
tc_wallet = TrustchainWallet(mock_ipv8.trustchain)
mock_ipv8.overlay.wallets['MB'] = tc_wallet
Expand All @@ -52,20 +52,6 @@ def setUp(self):
self.nodes[0].overlay.disable_matchmaker()
self.nodes[1].overlay.disable_matchmaker()

@twisted_wrapper
def test_info_message(self):
"""
Test sending info messages to other traders
"""
yield self.introduce_nodes()

self.nodes[0].overlay.send_info(self.nodes[1].my_peer)

yield self.deliver_messages()

self.assertTrue(self.nodes[0].overlay.matchmakers)
self.assertTrue(self.nodes[1].overlay.matchmakers)

@twisted_wrapper(2)
def test_create_ask(self):
"""
Expand Down Expand Up @@ -113,7 +99,6 @@ def test_decline_trade(self):
"""
Test declining a trade
"""
self.nodes[0].overlay.disable_matchmaker()
yield self.introduce_nodes()

order = yield self.nodes[0].overlay.create_ask(AssetPair(AssetAmount(1, 'DUM1'), AssetAmount(1, 'DUM2')), 3600)
Expand All @@ -134,7 +119,6 @@ def test_counter_trade(self):
"""
Test making a counter trade
"""
self.nodes[0].overlay.disable_matchmaker()
yield self.introduce_nodes()

order = yield self.nodes[0].overlay.create_ask(AssetPair(AssetAmount(2, 'DUM1'), AssetAmount(2, 'DUM2')), 3600)
Expand Down Expand Up @@ -179,6 +163,36 @@ def test_e2e_trade(self):
self.assertEqual(balance1['available'], 1050)
self.assertEqual(balance2['available'], -50)

@twisted_wrapper(2)
def test_e2e_trade_dht(self):
"""
Test a full trade with (dummy assets), where both traders are not connected to each other
"""
yield self.introduce_nodes()

for node in self.nodes:
for other in self.nodes:
if other != node:
node.dht.walk_to(other.endpoint.wan_address)
yield self.deliver_messages()

# Remove the address from the mid registry from the trading peers
self.nodes[0].overlay.mid_register.pop(TraderId(self.nodes[1].overlay.mid))
self.nodes[1].overlay.mid_register.pop(TraderId(self.nodes[0].overlay.mid))

for node in self.nodes:
node.dht.store_peer()
yield self.deliver_messages()

yield self.nodes[0].overlay.create_ask(AssetPair(AssetAmount(10, 'DUM1'), AssetAmount(10, 'DUM2')), 3600)
yield self.nodes[1].overlay.create_bid(AssetPair(AssetAmount(10, 'DUM1'), AssetAmount(10, 'DUM2')), 3600)

yield self.sleep(0.5)

# Verify that the trade has been made
self.assertTrue(self.nodes[0].overlay.transaction_manager.find_all())
self.assertTrue(self.nodes[1].overlay.transaction_manager.find_all())

@twisted_wrapper
def test_cancel(self):
"""
Expand Down Expand Up @@ -241,6 +255,27 @@ def test_proposed_trade_timeout(self):
self.assertEqual(bid_tick_entry.reserved_for_matching, 0)
self.assertEqual(ask_tick_entry.reserved_for_matching, 0)

@twisted_wrapper(3)
def test_address_resolv_fail(self):
"""
Test whether an order is unreserved when address resolution fails
"""
yield self.introduce_nodes()

self.nodes[1].overlay.get_address_for_trader = lambda *_: succeed(None)

ask_order = yield self.nodes[0].overlay.create_ask(
AssetPair(AssetAmount(1, 'DUM1'), AssetAmount(1, 'DUM2')), 3600)
bid_order = yield self.nodes[1].overlay.create_bid(
AssetPair(AssetAmount(1, 'DUM1'), AssetAmount(1, 'DUM2')), 3600)

yield self.sleep(0.5)

ask_tick_entry = self.nodes[2].overlay.order_book.get_tick(ask_order.order_id)
bid_tick_entry = self.nodes[2].overlay.order_book.get_tick(bid_order.order_id)
self.assertEqual(bid_tick_entry.reserved_for_matching, 0)
self.assertEqual(ask_tick_entry.reserved_for_matching, 0)

@twisted_wrapper(4)
def test_orderbook_sync(self):
"""
Expand Down Expand Up @@ -273,7 +308,7 @@ def test_orderbook_sync(self):
self.assertTrue(self.nodes[4].overlay.order_book.get_tick(ask_order.order_id))
self.assertTrue(self.nodes[4].overlay.order_book.get_tick(bid_order.order_id))

@twisted_wrapper(40000)
@twisted_wrapper(4)
def test_partial_trade(self):
"""
Test a partial trade between two nodes with a matchmaker
Expand Down
18 changes: 0 additions & 18 deletions Tribler/Test/Community/Market/test_socket_address.py

This file was deleted.

1 change: 1 addition & 0 deletions Tribler/Test/Core/Modules/RestApi/test_market_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def setUp(self, autoload_discovery=True):
self.mock_ipv8 = MockIPv8(u"low",
MarketCommunity,
create_trustchain=True,
create_dht=True,
wallets=wallets_dict,
working_directory=self.session.config.get_state_dir())
self.session.lm.market_community = self.mock_ipv8.overlay
Expand Down
4 changes: 2 additions & 2 deletions Tribler/community/market/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ def is_valid_tick(tick):
"""
Verify whether a dictionary that contains a tick, is valid.
"""
required_fields = ['trader_id', 'order_number', 'assets', 'timeout', 'timestamp', 'address', 'port', 'traded']
required_fields = ['trader_id', 'order_number', 'assets', 'timeout', 'timestamp', 'traded']
if not MarketBlock.has_fields(required_fields, tick):
return False

required_types = [('trader_id', str), ('order_number', int), ('assets', dict), ('timestamp', float),
('timeout', int), ('address', str), ('port', int)]
('timeout', int)]

if not MarketBlock.is_valid_trader_id(tick['trader_id']):
return False
Expand Down
Loading

0 comments on commit 6095c7e

Please sign in to comment.