示例#1
0
    def __init__(self,
                 transaction=None,
                 previous=None,
                 key=None,
                 linked=None,
                 block_type=b'test'):
        crypto = default_eccrypto
        if linked:
            link_pk = linked.public_key
            link_seq = linked.sequence_number
        else:
            link_pk = crypto.generate_key(u"curve25519").pub().key_to_bin()
            link_seq = 0

        transaction = transaction or {'id': 42}

        if previous:
            self.key = previous.key
            TrustChainBlock.__init__(
                self, (block_type, json.dumps(transaction),
                       previous.public_key, previous.sequence_number + 1,
                       link_pk, link_seq, previous.hash, EMPTY_SIG, 0, 0))
        else:
            if key:
                self.key = key
            else:
                self.key = crypto.generate_key(u"curve25519")

            TrustChainBlock.__init__(self,
                                     (block_type, json.dumps(transaction),
                                      self.key.pub().key_to_bin(), 1, link_pk,
                                      link_seq, GENESIS_HASH, EMPTY_SIG, 0, 0))
        self.sign(self.key)

        self.transaction_validation_result = (ValidationResult.valid, [])
示例#2
0
 def test_hash(self):
     """
     Test the empty hash of a block
     """
     block = TrustChainBlock()
     block.timestamp = 0  # To make the hash the same between runs
     block.hash = block.calculate_hash()
     self.assertTrue(block.hash)
示例#3
0
    def test_connected_users(self):
        """
        Test returning connected users for a given public key works.
        """
        self.assertEqual(len(self.db.get_users()), 0)
        self.assertEqual(len(self.db.get_connected_users(self.public_key)), 0)

        # Add 10 random blocks, implying 10 unique peers
        random_blocks = []
        for i in range(0, 10):
            block = TestBlock()
            random_blocks.append(block)
            self.db.add_block(block)

        self.assertEqual(len(self.db.get_users()), 10)
        self.assertEqual(len(self.db.get_connected_users(self.public_key)), 0)

        # Create 5 link block implying 5 connected peers to the current user
        for i in range(0, 5):
            block = TrustChainBlock.create(b'test', {b'id': i},
                                           self.db,
                                           self.public_key,
                                           link=random_blocks[i])
            self.db.add_block(block)

        self.assertEqual(len(self.db.get_users()), 11)
        self.assertEqual(len(self.db.get_connected_users(self.public_key)), 5)
示例#4
0
 def test_create_next(self):
     """
     Test creating a block that points towards a previous block
     """
     db = MockDatabase()
     prev = TestBlock()
     prev.sequence_number = GENESIS_SEQ
     db.add_block(prev)
     block = TrustChainBlock.create(b'test', {'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)
示例#5
0
 def test_create_genesis(self):
     """
     Test creating a genesis block
     """
     key = default_eccrypto.generate_key(u"curve25519")
     db = MockDatabase()
     block = TrustChainBlock.create(b'test', {'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)
     self.assertEqual(block.type, b'test')
示例#6
0
 def test_create_link_genesis(self):
     """
     Test creating a linked half block
     """
     key = default_eccrypto.generate_key(u"curve25519")
     db = MockDatabase()
     link = TestBlock()
     db.add_block(link)
     block = TrustChainBlock.create(b'test', {b'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)
示例#7
0
 def test_create_link_next(self):
     """
     Test creating a linked half that points back towards a previous block
     """
     db = MockDatabase()
     prev = TestBlock()
     prev.sequence_number = GENESIS_SEQ
     db.add_block(prev)
     link = TestBlock()
     db.add_block(link)
     block = TrustChainBlock.create(b'test', {b'id': 42},
                                    db,
                                    prev.public_key,
                                    link=link)
     self.assertEqual(block.previous_hash, prev.hash)
     self.assertEqual(block.sequence_number, 2)
     self.assertEqual(block.public_key, prev.public_key)
     self.assertEqual(block.link_public_key, link.public_key)
     self.assertEqual(block.link_sequence_number, link.sequence_number)