def test_get_statistics(self): """ Testing whether the API returns the correct statistics """ block = TrustChainBlock() block.public_key = self.session.lm.triblerchain_community.my_peer.public_key.key_to_bin() block.link_public_key = "deadbeef".decode("HEX") block.link_sequence_number = 21 block.transaction = {"up": 42, "down": 8, "total_up": 1024, "total_down": 2048} block.sequence_number = 3 block.previous_hash = "babecafe".decode("HEX") block.signature = "babebeef".decode("HEX") self.session.lm.triblerchain_community.persistence.add_block(block) def verify_response(response): response_json = json.loads(response) self.assertTrue("statistics" in response_json) stats = response_json["statistics"] self.assertEqual(stats["id"], self.session.lm.triblerchain_community.my_peer. public_key.key_to_bin().encode("HEX")) self.assertEqual(stats["total_blocks"], 3) self.assertEqual(stats["total_up"], 1024) self.assertEqual(stats["total_down"], 2048) self.assertEqual(stats["peers_that_pk_helped"], 1) self.assertEqual(stats["peers_that_helped_pk"], 1) self.assertIn("latest_block", stats) self.assertNotEqual(stats["latest_block"]["insert_time"], "") self.assertEqual(stats["latest_block"]["hash"], block.hash.encode("HEX")) self.assertEqual(stats["latest_block"]["link_public_key"], "deadbeef") self.assertEqual(stats["latest_block"]["link_sequence_number"], 21) self.assertEqual(stats["latest_block"]["up"], 42) self.assertEqual(stats["latest_block"]["down"], 8) self.should_check_equality = False return self.do_request('trustchain/statistics', expected_code=200).addCallback(verify_response)
def setUp(self, annotate=True): yield super(TestDatabase, self).setUp(annotate=annotate) path = os.path.join(self.getStateDir(), DATABASE_DIRECTORY) if not os.path.exists(path): os.makedirs(path) self.db = TriblerChainDB(self.getStateDir(), u'triblerchain') self.block1 = TrustChainBlock() self.block1.transaction = {'up': 42, 'down': 42} self.block1.public_key = 'a' self.block2 = TrustChainBlock() self.block2.transaction = {'up': 42, 'down': 42} self.block2.public_key = 'b'
def test_get_bootstrap_identity_not_enough_tokens_2(self): """ Testing whether the API returns error 400 if bandwidth is to low when bootstrapping a new identity """ transaction = {'up': 0, 'down': 100, 'total_up': 0, 'total_down': 100} test_block = TrustChainBlock() test_block.transaction = transaction test_block.public_key = self.session.lm.triblerchain_community.my_peer.public_key.key_to_bin() self.session.lm.triblerchain_community.persistence.add_block(test_block) self.should_check_equality = False return self.do_request('trustchain/bootstrap?amount=10', expected_code=400)
def test_get_blocks_unlimited(self): """ Testing whether the API takes no limit argument """ self.should_check_equality = False return self.do_request('trustchain/blocks/%s' % TrustChainBlock().public_key.encode("HEX"), expected_code=200)
def test_get_blocks_bad_limit_nothing(self): """ Testing whether the API takes no values for the limit """ self.should_check_equality = False return self.do_request('trustchain/blocks/%s?limit=' % TrustChainBlock().public_key.encode("HEX"), expected_code=400)
def test_get_bootstrap_identity_partial_tokens(self): """ Testing whether the API return partial available credit when argument is supplied """ transaction = {'up': 100, 'down': 0, 'total_up': 100, 'total_down': 0} transaction2 = {'up': 50, 'down': 0} def verify_response(response): response_json = json.loads(response) self.assertEqual(response_json['transaction'], transaction2) test_block = TrustChainBlock() test_block.transaction = transaction test_block.public_key = self.session.lm.triblerchain_community.my_peer.public_key.key_to_bin() self.session.lm.triblerchain_community.persistence.add_block(test_block) self.should_check_equality = False return self.do_request('trustchain/bootstrap?amount=50', expected_code=200).addCallback(verify_response)
def insert_transaction(self, pubkey1, pubkey2, assets_traded): transaction = { "tx": { "assets": assets_traded.to_dictionary(), "transferred": assets_traded.to_dictionary() }, } block = TrustChainBlock.create('tx_done', transaction, self.market_db, pubkey1, link=None, link_pk=pubkey2) link_block = TrustChainBlock.create('tx_done', transaction, self.market_db, pubkey2, link=block, link_pk=pubkey1) self.market_db.add_block(block) self.market_db.add_block(link_block)
def test_get_blocks(self): """ Testing whether the API returns the correct blocks """ def verify_response(response): response_json = json.loads(response) self.assertEqual(len(response_json["blocks"]), 1) test_block = TrustChainBlock() self.session.lm.triblerchain_community.persistence.add_block(test_block) self.should_check_equality = False return self.do_request('trustchain/blocks/%s?limit=10' % test_block.public_key.encode("HEX"), expected_code=200).addCallback(verify_response)
def insert_transaction(self, pubkey1, pubkey2, quantity, price): transaction = { "tx": { "quantity_type": quantity.wallet_id, "quantity": float(quantity), "price_type": price.wallet_id, "price": float(price) }, } block = TrustChainBlock.create('tx_done', transaction, self.market_db, pubkey1, link=None, link_pk=pubkey2) link_block = TrustChainBlock.create('tx_done', transaction, self.market_db, pubkey2, link=block, link_pk=pubkey1) self.market_db.add_block(block) self.market_db.add_block(link_block)
def test_get_bootstrap_identity_not_enough_tokens_2(self): """ Testing whether the API returns error 400 if bandwidth is to low when bootstrapping a new identity """ transaction = {'up': 0, 'down': 100, 'total_up': 0, 'total_down': 100} test_block = TrustChainBlock() test_block.type = 'tribler_bandwidth' test_block.transaction = transaction test_block._transaction = encode(transaction) test_block.public_key = self.session.lm.trustchain_community.my_peer.public_key.key_to_bin() test_block.hash = test_block.calculate_hash() self.session.lm.trustchain_community.persistence.add_block(test_block) self.should_check_equality = False return self.do_request('trustchain/bootstrap?amount=10', expected_code=400)
def test_get_bootstrap_identity_all_tokens(self): """ Testing whether the API return all available tokens when no argument is supplied """ transaction = {'up': 100, 'down': 0, 'total_up': 100, 'total_down': 0} transaction2 = {'up': 100, 'down': 0} def verify_response(response): response_json = json.loads(response) self.assertEqual(response_json['transaction'], transaction2) test_block = TrustChainBlock() test_block.type = 'tribler_bandwidth' test_block.transaction = transaction test_block._transaction = encode(transaction) test_block.public_key = self.session.lm.trustchain_community.my_peer.public_key.key_to_bin( ) test_block.hash = test_block.calculate_hash() self.session.lm.trustchain_community.persistence.add_block(test_block) self.should_check_equality = False return self.do_request('trustchain/bootstrap', expected_code=200).addCallback(verify_response)
def test_get_bootstrap_identity_partial_tokens(self): """ Testing whether the API return partial available credit when argument is supplied """ transaction = {'up': 100, 'down': 0, 'total_up': 100, 'total_down': 0} transaction2 = {'up': 50, 'down': 0} def verify_response(response): response_json = json.loads(response) self.assertEqual(response_json['transaction'], transaction2) test_block = TrustChainBlock() test_block.type = 'tribler_bandwidth' test_block.transaction = transaction test_block._transaction = encode(transaction) test_block.public_key = self.session.lm.trustchain_community.my_peer.public_key.key_to_bin() test_block.hash = test_block.calculate_hash() self.session.lm.trustchain_community.persistence.add_block(test_block) self.should_check_equality = False return self.do_request('trustchain/bootstrap?amount=50', expected_code=200).addCallback(verify_response)
def test_get_statistics(self): """ Testing whether the API returns the correct statistics """ block = TrustChainBlock() block.public_key = self.session.lm.trustchain_community.my_peer.public_key.key_to_bin() block.link_public_key = "deadbeef".decode("HEX") block.link_sequence_number = 21 block.type = 'tribler_bandwidth' block.transaction = {"up": 42, "down": 8, "total_up": 1024, "total_down": 2048, "type": "tribler_bandwidth"} block._transaction = encode(block.transaction) block.sequence_number = 3 block.previous_hash = "babecafe".decode("HEX") block.signature = "babebeef".decode("HEX") block.hash = block.calculate_hash() self.session.lm.trustchain_community.persistence.add_block(block) def verify_response(response): response_json = json.loads(response) self.assertTrue("statistics" in response_json) stats = response_json["statistics"] self.assertEqual(stats["id"], self.session.lm.trustchain_community.my_peer. public_key.key_to_bin().encode("HEX")) self.assertEqual(stats["total_blocks"], 3) self.assertEqual(stats["total_up"], 1024) self.assertEqual(stats["total_down"], 2048) self.assertEqual(stats["peers_that_pk_helped"], 1) self.assertEqual(stats["peers_that_helped_pk"], 1) self.assertIn("latest_block", stats) self.assertNotEqual(stats["latest_block"]["insert_time"], "") self.assertEqual(stats["latest_block"]["hash"], block.hash.encode("HEX")) self.assertEqual(stats["latest_block"]["link_public_key"], "deadbeef") self.assertEqual(stats["latest_block"]["link_sequence_number"], 21) self.assertEqual(stats["latest_block"]["up"], 42) self.assertEqual(stats["latest_block"]["down"], 8) self.should_check_equality = False return self.do_request('trustchain/statistics', expected_code=200).addCallback(verify_response)