def test_set_local_pow(self): """ Test if local_pow can be enabled/disabled dynamically. """ with patch('pow.ccurl_interface.attach_to_tangle', MagicMock(return_value=self.ccurl_bundle)) as mocked_ccurl: self.adapter = MockAdapter() self.adapter.seed_response('attachToTangle', { 'trytes': self.bundle, }) # First, we enable local_pow api = Iota(self.adapter, local_pow=True) result = api.attach_to_tangle(self.trunk, self.branch, self.bundle, self.mwm) # Ccurl was called self.assertTrue(mocked_ccurl.called) # Result comes from ccurl self.assertEqual(result['trytes'], self.ccurl_bundle) # Reset mock, this clears the called attribute mocked_ccurl.reset_mock() # Disable local_pow api.set_local_pow(local_pow=False) # Try again result = api.attach_to_tangle(self.trunk, self.branch, self.bundle, self.mwm) # Ccurl interface was not called self.assertFalse(mocked_ccurl.called) # Result is the one returned by MockAdapter self.assertEqual(result['trytes'], self.bundle) # And not by mocked pow pkg self.assertNotEqual(result['trytes'], self.ccurl_bundle)
def test_backward_compatibility(self): """ Test that the local_pow feature is backward compatible. That is, if `local_pow` argument is omitted, it takes no effect and the pow extension package is not called. """ with patch('pow.ccurl_interface.attach_to_tangle', MagicMock(return_value=self.ccurl_bundle)) as mocked_ccurl: self.adapter = MockAdapter() self.adapter.seed_response('attachToTangle',{ 'trytes': self.bundle, }) # No `local_pow` argument is passed to the api! api = Iota(self.adapter) result = api.attach_to_tangle( self.trunk, self.branch, self.bundle, self.mwm) # Ccurl interface was not called self.assertFalse(mocked_ccurl.called) # Result is the one returned by MockAdapter self.assertEqual(result['trytes'], self.bundle) # And not by mocked pow pkg self.assertNotEqual(result['trytes'], self.ccurl_bundle)
def test_mock_adapter(self): """ Test if local_pow feature works with MockAdapter. """ # Note that we need correct return value to pass the # response filter. with patch('pow.ccurl_interface.attach_to_tangle', MagicMock(return_value=self.bundle)) as mocked_ccurl: api = Iota(MockAdapter(), local_pow=True) result = api.attach_to_tangle(self.trunk, self.branch, self.bundle, self.mwm) self.assertTrue(mocked_ccurl.called) self.assertEqual(result['trytes'], self.bundle)
def test_sandbox_adapter(self): """ Test if local_pow feature works with SandboxAdapter. """ # Note that we need correct return value to pass the # response filter. with patch('pow.ccurl_interface.attach_to_tangle', MagicMock(return_value=self.bundle)) as mocked_ccurl: api = Iota(SandboxAdapter('https://sandbox.iota:14265/api/v1/', auth_token=None), local_pow=True) result = api.attach_to_tangle( self.trunk, self.branch, self.bundle, self.mwm) self.assertTrue(mocked_ccurl.called) self.assertEqual(result['trytes'], self.bundle)
def test_wireup(self): """ Verify that the command is wired up correctly. (sync) The API method indeed calls the appropiate command. """ with patch( 'iota.commands.core.attach_to_tangle.AttachToTangleCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = Iota(self.adapter) response = api.attach_to_tangle('trunk', 'branch', 'trytes') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_routing_wrapper(self): """ Test if local_pow feature works with RoutingWrapper. """ # Note that we need correct return value to pass the # response filter. with patch('pow.ccurl_interface.attach_to_tangle', MagicMock(return_value=self.bundle)) as mocked_ccurl: # We are trying to redirect `attach_to_tangle` calls to localhost # with a RoutingWrapper. However, if local_pow=true, the pow # request will not reach the adapter, but will be directed to # ccurl interface. api = Iota(RoutingWrapper('http://12.34.56.78:14265').add_route( 'attachToTangle', 'http://localhost:14265'), local_pow=True) result = api.attach_to_tangle(self.trunk, self.branch, self.bundle, self.mwm) self.assertTrue(mocked_ccurl.called) self.assertEqual(result['trytes'], self.bundle)
] # Prepare the original bundle original_trytes = api.prepare_transfer(transfers=[ ProposedTransaction(address=addys[0], value=0), ProposedTransaction(address=addys[1], value=0), ProposedTransaction(address=addys[2], value=0), ProposedTransaction(address=addys[3], value=0), ]).get('trytes') gtta_response = api.get_transactions_to_approve(3) trunk = gtta_response.get('trunkTransaction') branch = gtta_response.get('branchTransaction') attached_original_trytes = api.attach_to_tangle(trunk, branch, original_trytes).get('trytes') # So we have the original bundle attached, time to construct the new one # We need to re-attach, but take special care, so we dont use the api, rather we do it ourself re_attached_trytes = custom_attach(attached_original_trytes, 9) original_bundle = Bundle.from_tryte_strings(attached_original_trytes) re_attached_bundle = Bundle.from_tryte_strings(re_attached_trytes) pprint('Original bundle is:') pprint(original_bundle.as_json_compatible()) pprint('Reattached bundle is:') pprint(re_attached_bundle.as_json_compatible())
host = yaml_file['nodes']['nodeA']['host'] port = yaml_file['nodes']['nodeA']['ports']['api'] api = Iota('http://{}:{}'.format(host, port)) txn = ProposedTransaction( address = Address('KSAFREMKHHYHSXNLGZPFVHANVHVMKWSGEAHGTXZCSQMXTCZXOGBLVPCWFKVAEQYDJMQALKZRKOTWLGBSC'), value = 0 ) bundle = ProposedBundle() bundle.add_transaction(txn) bundle.add_transaction(txn) index_trytes = str(int_to_trytes(index, 9)) bundle[0]._legacy_tag = Tag(index_trytes) finalize.finalize(bundle) bundle_trytes = bundle.as_tryte_strings() tips = api.get_transactions_to_approve(depth=3) branch = tips['branchTransaction'] trunk = tips['trunkTransaction'] milestone_bundle = api.attach_to_tangle(trunk,branch, bundle_trytes,3) api.broadcast_and_store(milestone_bundle.get('trytes')) print("Milestone {} attached and stored: {}".format(index, milestone_bundle))