Exemple #1
0
    def test_get_statistics(self):
        """
        Testing whether the API returns the correct statistics
        """
        block = TrustChainBlock()
        block.public_key = self.member.public_key
        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.tc_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.member.public_key.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)
Exemple #2
0
 def test_validate_linked_double_pay_fraud(self):
     db = MockDatabase()
     (block1, _, _, _) = TestBlocks.setup_validate()
     # Act
     db.add_block(block1)
     db.add_block(
         TrustChainBlock.create(block1.transaction, db,
                                block1.link_public_key, block1))
     block2 = TrustChainBlock.create(block1.transaction, db,
                                     block1.link_public_key, block1)
     result = block2.validate(db)
     self.assertEqual(result[0], ValidationResult.invalid)
     self.assertIn("Double countersign fraud", result[1])
Exemple #3
0
 def test_pack(self):
     block = TrustChainBlock()
     block.transaction = {'id': 3}
     block.public_key = ' fish, so sad that it should come to this. - We tried to warn you all but '
     block.sequence_number = 1869095012
     block.link_public_key = 'ear! - You may not share our intellect, which might explain your disrespec'
     block.link_sequence_number = 1949048934
     block.previous_hash = 'or all the natural wonders that '
     block.signature = 'grow around you. - So long, so long, and thanks for all the fish'
     self.assertEqual(
         block.pack(),
         ' fish, so sad that it should come to this. - We tried to warn you '
         'all but oh dear! - You may not share our intellect, which might explain your '
         'disrespect, for all the natural wonders that grow around you. - So long, '
         'so long, and thanks for all the fish\x00\x00\x00\na1d2bid1i3')
    def __init__(self, transaction=None, previous=None):
        crypto = ECCrypto()
        other = crypto.generate_key(u"curve25519").pub().key_to_bin()

        transaction = transaction or {'id': 42}

        if previous:
            self.key = previous.key
            TrustChainBlock.__init__(self, (encode(transaction), previous.public_key, previous.sequence_number + 1,
                                            other, 0, previous.hash, 0, 0))
        else:
            self.key = crypto.generate_key(u"curve25519")
            TrustChainBlock.__init__(self, (
                encode(transaction), self.key.pub().key_to_bin(), random.randint(50, 100), other, 0,
                sha256(str(random.randint(0, 100000))).digest(), 0, 0))
        self.sign(self.key)
Exemple #5
0
    def _decode_block_pair(placeholder, offset, data):
        """
        Decode an incoming block pair message.
        :param placeholder:
        :param offset: Start of the BlockPair message in the data.
        :param data: ByteStream containing the message.
        :return: (offset, BlockPairPayload.impl)
        """
        if len(data) < offset + block_pack_size * 2:
            raise DropPacket("Unable to decode the payload")

        try:
            new_offset, block1 = TrustChainBlock.unpack(data, offset)
            _, block2 = TrustChainBlock.unpack(data, new_offset)
        except (IndexError, ValueError):
            raise DropPacket("Invalid block contents")

        return len(data), placeholder.meta.payload.implement(block1, block2)
Exemple #6
0
 def test_validate_linked_valid(self):
     db = MockDatabase()
     (block1, block2, _, _) = TestBlocks.setup_validate()
     db.add_block(block2)
     # Act
     db.add_block(
         TrustChainBlock.create(block1.transaction, db,
                                block1.link_public_key, block1))
     result = block1.validate(db)
     self.assertEqual(result[0], ValidationResult.valid)
Exemple #7
0
 def test_validate_linked_mismatch(self):
     db = MockDatabase()
     (block1, block2, _, _) = TestBlocks.setup_validate()
     # Act
     db.add_block(block1)
     block3 = TrustChainBlock.create(block2.transaction, db,
                                     block2.link_public_key, block1)
     result = block3.validate(db)
     self.assertEqual(result[0], ValidationResult.invalid)
     self.assertIn("Public key mismatch on linked block", result[1])
Exemple #8
0
 def test_create_genesis(self):
     key = ECCrypto().generate_key(u"curve25519")
     db = MockDatabase()
     block = TrustChainBlock.create({'id': 42},
                                    db,
                                    key.pub().key_to_bin(),
                                    link=None)
     self.assertEqual(block.previous_hash, GENESIS_HASH)
     self.assertEqual(block.sequence_number, GENESIS_SEQ)
     self.assertEqual(block.public_key, key.pub().key_to_bin())
     self.assertEqual(block.signature, EMPTY_SIG)
Exemple #9
0
 def test_create_next(self):
     db = MockDatabase()
     prev = TestBlock()
     prev.sequence_number = GENESIS_SEQ
     db.add_block(prev)
     block = TrustChainBlock.create({'id': 42},
                                    db,
                                    prev.public_key,
                                    link=None)
     self.assertEqual(block.previous_hash, prev.hash)
     self.assertEqual(block.sequence_number, 2)
     self.assertEqual(block.public_key, prev.public_key)
Exemple #10
0
 def test_create_link_genesis(self):
     key = ECCrypto().generate_key(u"curve25519")
     db = MockDatabase()
     link = TestBlock()
     db.add_block(link)
     block = TrustChainBlock.create({'id': 42},
                                    db,
                                    key.pub().key_to_bin(),
                                    link=link)
     self.assertEqual(block.previous_hash, GENESIS_HASH)
     self.assertEqual(block.sequence_number, GENESIS_SEQ)
     self.assertEqual(block.public_key, key.pub().key_to_bin())
     self.assertEqual(block.link_public_key, link.public_key)
     self.assertEqual(block.link_sequence_number, link.sequence_number)
Exemple #11
0
 def test_validate_existing_link_public_key(self):
     # Arrange
     db = MockDatabase()
     (block1, block2, block3, _) = TestBlocks.setup_validate()
     db.add_block(block1)
     db.add_block(block2)
     db.add_block(block3)
     # Act
     block2 = TrustChainBlock(block2.pack_db_insert())
     block2.link_public_key = EMPTY_PK
     block2.sign(db.get(block2.public_key, block2.sequence_number).key)
     result = block2.validate(db)
     # Assert
     self.assertEqual(result[0], ValidationResult.invalid)
     self.assertIn('Linked public key is not valid', result[1])
Exemple #12
0
    def _decode_half_block(placeholder, offset, data):
        """
        Decode an incoming half block message.
        :param placeholder:
        :param offset: Start of the HalfBlock message in the data.
        :param data: ByteStream containing the message.
        :return: (offset, HalfBlockPayload.impl)
        """
        if len(data) < offset + block_pack_size:
            raise DropPacket("Unable to decode the payload")

        try:
            block = TrustChainBlock.unpack(data, offset)
        except IndexError:
            raise DropPacket("Invalid block contents")

        return len(data), placeholder.meta.payload.implement(block)
Exemple #13
0
 def test_validate_existing_hash(self):
     # Arrange
     db = MockDatabase()
     (block1, block2, block3, _) = TestBlocks.setup_validate()
     db.add_block(block1)
     db.add_block(block2)
     db.add_block(block3)
     # Act
     block2 = TrustChainBlock(block2.pack_db_insert())
     block2.previous_hash = sha256(str(random.randint(0, 100000))).digest()
     block2.sign(db.get(block2.public_key, block2.sequence_number).key)
     result = block2.validate(db)
     # Assert
     self.assertEqual(result[0], ValidationResult.invalid)
     self.assertIn(
         'Previous hash is not equal to the hash id of the previous block',
         result[1])
Exemple #14
0
 def test_validate_existing_fraud(self):
     # Arrange
     db = MockDatabase()
     (block1, block2, _, _) = TestBlocks.setup_validate()
     db.add_block(block1)
     db.add_block(block2)
     # Act
     block3 = TrustChainBlock(block2.pack_db_insert())
     block3.previous_hash = sha256(str(random.randint(0, 100000))).digest()
     block3.sign(block2.key)
     result = block3.validate(db)
     # Assert
     self.assertEqual(result[0], ValidationResult.invalid)
     self.assertIn("Double sign fraud", result[1])
Exemple #15
0
 def test_hash(self):
     block = TrustChainBlock()
     self.assertEqual(
         block.hash,
         '\x1f\x1bp\x90\xe3>\x83\xf6\xcd\xafd\xd9\xee\xfb"&|<ZLsyB:Z\r'
         '<\xc5\xb0\x97\xa3\xaf')
Exemple #16
0
 def _getall(self, query, params):
     db_result = self.execute(self.get_sql_header() + query,
                              params).fetchall()
     return [TrustChainBlock(db_item) for db_item in db_result]
Exemple #17
0
 def _get(self, query, params):
     db_result = self.execute(self.get_sql_header() + query,
                              params).fetchone()
     return TrustChainBlock(db_result) if db_result else None