def take_step(self): if not self._live_edges_enabled: return super(TrustChainCommunity, self).take_step() now = time() self._logger.debug("previous sync was %.1f seconds ago", now - self._last_sync_time if self._last_sync_time else -1) if not self._live_edge or len(self._live_edge) == 5 or self._live_edge_next is None: self._live_edge_id += 1 # New live edges always start with our member my_candidate = Candidate(("127.0.0.1", self.dispersy.endpoint.get_address()[1]), False) my_candidate.associate(self.my_member) self._live_edge = [my_candidate] # Callback our live edge handler if self._live_edge_cb: self._live_edge_cb(self._live_edge_id, self._live_edge) if self._live_edge_next: candidate = self._live_edge_next self._live_edge_next = None else: candidate = self.dispersy_get_walk_candidate() if candidate: self._logger.debug("%s %s taking step towards %s", self.cid.encode("HEX"), self.get_classification(), candidate) self.create_introduction_request(candidate, self.dispersy_enable_bloom_filter_sync) else: self._logger.debug("%s %s no candidate to take step", self.cid.encode("HEX"), self.get_classification()) self._last_sync_time = time()
def test_puncture_message_conversion(self): for c in self._conversions: if isinstance(c, PunctureConversion): meta = self._di._community.get_meta_message( PUNCTURE_MESSAGE_NAME) sender_lan = Address.ipv4("0.0.0.1:1232") sender_wan = Address.ipv4("23.12.32.123:1232") sender_id = os.urandom(16) address_vote = Address.ipv6("[::0]:12145") endpoint_id = os.urandom(16) message = meta.impl( authentication=(self._di._community.my_member, ), distribution=(self._di._community.claim_global_time(), ), destination=(Candidate(address_vote.addr(), True), ), payload=(sender_lan, sender_wan, sender_id, address_vote, endpoint_id)) encoded = c.encode_payload(message) placeholder = c.Placeholder(None, meta, 0, encoded, False, True) _, x = c.decode_payload(placeholder, 0, str(encoded[0]) + encoded[1]) self.assertEqual(x.sender_lan, sender_lan) self.assertEqual(x.sender_wan, sender_wan) self.assertEqual(x.sender_id, sender_id) self.assertEqual(x.address_vote, address_vote) self.assertEqual(x.endpoint_id, endpoint_id)
def take_step(self): if not self._live_edges_enabled: return super(TrustChainCommunity, self).take_step() now = time() self._logger.debug( "previous sync was %.1f seconds ago", now - self._last_sync_time if self._last_sync_time else -1) if not self._live_edge or len( self._live_edge) == 5 or self._live_edge_next is None: self._live_edge_id += 1 # New live edges always start with our member my_candidate = Candidate( ("127.0.0.1", self.dispersy.endpoint.get_address()[1]), False) my_candidate.associate(self.my_member) self._live_edge = [my_candidate] # Callback our live edge handler if self._live_edge_cb: self._live_edge_cb(self._live_edge_id, self._live_edge) if self._live_edge_next: candidate = self._live_edge_next self._live_edge_next = None else: candidate = self.dispersy_get_walk_candidate() if candidate: self._logger.debug("%s %s taking step towards %s", self.cid.encode("HEX"), self.get_classification(), candidate) self.create_introduction_request( candidate, self.dispersy_enable_bloom_filter_sync) else: self._logger.debug("%s %s no candidate to take step", self.cid.encode("HEX"), self.get_classification()) self._last_sync_time = time()
def send_start_transaction(self, transaction, start_transaction): assert isinstance(start_transaction, StartTransaction), type(start_transaction) payload = start_transaction.to_network() # Lookup the remote address of the peer with the pubkey candidate = Candidate(self.lookup_ip(transaction.partner_trader_id), False) meta = self.get_meta_message(u"start-transaction") message = meta.impl( authentication=(self.my_member,), distribution=(self.claim_global_time(),), destination=(candidate,), payload=payload ) self.dispersy.store_update_forward([message], True, False, True)
def send_declined_trade(self, declined_trade): assert isinstance(declined_trade, DeclinedTrade), type(declined_trade) destination, payload = declined_trade.to_network() # Lookup the remote address of the peer with the pubkey candidate = Candidate(self.lookup_ip(destination), False) meta = self.get_meta_message(u"declined-trade") message = meta.impl( authentication=(self.my_member,), distribution=(self.claim_global_time(),), destination=(candidate,), payload=payload ) self.dispersy.store_update_forward([message], True, False, True)
def send_counter_trade(self, counter_trade): assert isinstance(counter_trade, CounterTrade), type(counter_trade) destination, payload = counter_trade.to_network() # Add the local address to the payload payload += (self.dispersy.lan_address[0], self.dispersy.lan_address[1]) # Lookup the remote address of the peer with the pubkey candidate = Candidate(self.lookup_ip(destination), False) meta = self.get_meta_message(u"counter-trade") message = meta.impl( authentication=(self.my_member,), distribution=(self.claim_global_time(),), destination=(candidate,), payload=payload ) self.dispersy.store_update_forward([message], True, False, True)
def test_uav_message_conversion(self): for c in self._conversions: if isinstance(c, APIMessageConversion): meta = self._di._community.get_meta_message(API_MESSAGE_NAME) data = "asjfdioewf" addresses = [ Address.ipv4("0.0.0.1:1232"), Address.ipv6("[::0]:12145"), Address(port=32532) ] message = meta.impl( authentication=(self._di._community.my_member, ), distribution=(self._di._community.claim_global_time(), ), destination=(Candidate(addresses[0].addr(), True), ), payload=(data, )) encoded = c.encode_payload(message) placeholder = c.Placeholder(None, meta, 0, encoded, False, True) _, x = c.decode_payload(placeholder, 0, str(encoded[0]) + encoded[1]) self.assertEqual(x.message, data)
def send_end_transaction(self, transaction): # Lookup the remote address of the peer with the pubkey self._logger.debug("Sending end transaction (quantity: %s)", transaction.total_quantity) candidate = Candidate(self.lookup_ip(transaction.partner_trader_id), False) message_id = self.order_book.message_repository.next_identity() meta = self.get_meta_message(u"end-transaction") message = meta.impl( authentication=(self.my_member,), distribution=(self.claim_global_time(),), destination=(candidate,), payload=( message_id.trader_id, message_id.message_number, transaction.transaction_id.trader_id, transaction.transaction_id.transaction_number, Timestamp.now(), ) ) self.dispersy.store_update_forward([message], True, False, True)
def send_proposed_trade(self, proposed_trade): assert isinstance(proposed_trade, ProposedTrade), type(proposed_trade) destination, payload = proposed_trade.to_network() # Add the local address to the payload payload += (self.dispersy.lan_address[0], self.dispersy.lan_address[1]) # Lookup the remote address of the peer with the pubkey candidate = Candidate(self.lookup_ip(destination), False) self._logger.debug("Sending proposed trade with own order id %s and other order id %s to trader %s (ip: %s, port: %s)", str(proposed_trade.order_id), str(proposed_trade.recipient_order_id), destination, *self.lookup_ip(destination)) meta = self.get_meta_message(u"proposed-trade") message = meta.impl( authentication=(self.my_member,), distribution=(self.claim_global_time(),), destination=(candidate,), payload=payload ) self.dispersy.store_update_forward([message], True, False, True)
def test_addresses_message_conversion(self): for c in self._conversions: if isinstance(c, AddressesConversion): meta = self._di._community.get_meta_message( ADDRESSES_MESSAGE_NAME) ids = [os.urandom(16) for _ in range(3)] addresses = [ Address.ipv4("0.0.0.1:1232"), Address.ipv6("[::0]:12145"), Address(port=32532) ] id_addresses = zip(ids, addresses, addresses) message = meta.impl( authentication=(self._di._community.my_member, ), distribution=(self._di._community.claim_global_time(), ), destination=(Candidate(id_addresses[0][1].addr(), True), ), payload=(id_addresses, )) encoded = c.encode_payload(message) placeholder = c.Placeholder(None, meta, 0, encoded, False, True) _, x = c.decode_payload(placeholder, 0, str(encoded[0]) + encoded[1]) self.assertEqual(x.id_addresses, id_addresses)
def test_addresses_request_message_conversion(self): for c in self._conversions: if isinstance(c, AddressesRequestConversion): meta = self._di._community.get_meta_message( ADDRESSES_REQUEST_MESSAGE_NAME) sender_lan = Address.ipv4("0.0.0.1:1232") sender_wan = Address.ipv4("23.12.32.123:1232") endpoint_id = os.urandom(16) wan_address = Address.ipv4("12.23.21.21:1234") message = meta.impl( authentication=(self._di._community.my_member, ), distribution=(self._di._community.claim_global_time(), ), destination=(Candidate(wan_address.addr(), True), ), payload=(sender_lan, sender_wan, endpoint_id, wan_address)) encoded = c.encode_payload(message) placeholder = c.Placeholder(None, meta, 0, encoded, False, True) _, x = c.decode_payload(placeholder, 0, str(encoded[0]) + encoded[1]) self.assertEqual(x.sender_lan, sender_lan) self.assertEqual(x.sender_wan, sender_wan) self.assertEqual(x.endpoint_id, endpoint_id) self.assertEqual(x.wan_address, wan_address)
def send_bitcoin_payment(self, transaction, bitcoin_payment): assert isinstance(bitcoin_payment, BitcoinPayment), type(bitcoin_payment) payload = bitcoin_payment.to_network() # Lookup the remote address of the peer with the pubkey candidate = Candidate(self.lookup_ip(transaction.partner_trader_id), False) try: self._logger.debug("Paying %s BTC to %s", bitcoin_payment.price, transaction.partner_trader_id) self.bitcoin_payment_provider.transfer_bitcoin(bitcoin_payment.bitcoin_address, bitcoin_payment.price) meta = self.get_meta_message(u"bitcoin-payment") message = meta.impl( authentication=(self.my_member,), distribution=(self.claim_global_time(),), destination=(candidate,), payload=payload ) self.dispersy.store_update_forward([message], True, False, True) except InsufficientFunds: # not enough funds self._logger.warning("Not enough BitCoin for this transaction (have %s, need %s)!", self.bitcoin_payment_provider.balance(), bitcoin_payment.price)
def send_continue_transaction(self, transaction): assert isinstance(transaction, Transaction), type(transaction) # Lookup the remote address of the peer with the pubkey candidate = Candidate(self.lookup_ip(transaction.partner_trader_id), False) message_id = self.order_book.message_repository.next_identity() meta = self.get_meta_message(u"continue-transaction") message = meta.impl( authentication=(self.my_member,), distribution=(self.claim_global_time(),), destination=(candidate,), payload=( message_id.trader_id, message_id.message_number, transaction.transaction_id.trader_id, transaction.transaction_id.transaction_number, Timestamp.now(), ) ) self.dispersy.store_update_forward([message], True, False, True)
def send_multi_chain_payment(self, transaction, multi_chain_payment): assert isinstance(multi_chain_payment, MultiChainPayment), type(multi_chain_payment) payload = multi_chain_payment.to_network() # Lookup the remote address of the peer with the pubkey candidate = Candidate(self.lookup_ip(transaction.partner_trader_id), False) try: self._logger.debug("Paying %s MultiChain credits to %s", multi_chain_payment.transferor_quantity, transaction.partner_trader_id) self.multi_chain_payment_provider.transfer_multi_chain(candidate, multi_chain_payment.transferor_quantity) meta = self.get_meta_message(u"multi-chain-payment") message = meta.impl( authentication=(self.my_member,), distribution=(self.claim_global_time(),), destination=(candidate,), payload=payload ) self.dispersy.store_update_forward([message], True, False, True) except InsufficientFunds: # Not enough funds self._logger.warning("Not enough multichain credits for this transaction (have %s, need %s)!", self.multi_chain_payment_provider.balance(), multi_chain_payment.transferor_quantity)