Ejemplo n.º 1
0
    def test_create_bc_normal_fields(self):
        msg = b'payload'
        nodes = ['A' * 64, 'B' * 64, 'C' * 64, 'D' * 64]

        sig1, sk1, vk1 = self._create_merkle_sig(msg)
        sig2, sk2, vk2 = self._create_merkle_sig(msg)
        sig3, sk3, vk3 = self._create_merkle_sig(msg)
        sig4, sk4, vk4 = self._create_merkle_sig(msg)

        signatures = [sig1, sig2, sig3, sig4]

        BlockContender.create(signatures, nodes, 'A' * 64)  # should not throw an error
Ejemplo n.º 2
0
    def test_deserialize_valid_object(self):
        """
        Tests that the same object is recovered when serialized and deserialized
        """
        msg = b'payload'
        nodes = ['A' * 64, 'B' * 64, 'C' * 64, 'D' * 64]

        sig1, sk1, vk1 = self._create_merkle_sig(msg)
        sig2, sk2, vk2 = self._create_merkle_sig(msg)
        sig3, sk3, vk3 = self._create_merkle_sig(msg)
        sig4, sk4, vk4 = self._create_merkle_sig(msg)

        signatures = [sig1, sig2, sig3, sig4]

        bc = BlockContender.create(signatures, nodes, 'A' * 64)

        bc_ser = bc.serialize()

        clone = BlockContender.from_bytes(bc_ser)

        for i in range(len(signatures)):
            self.assertEqual(bc.signatures[i], clone.signatures[i])

        for i in range(len(nodes)):
            self.assertEqual(bc.merkle_leaves[i], clone.merkle_leaves[i])
Ejemplo n.º 3
0
    def test_eq(self):
        nodes = [secrets.token_bytes(8) for _ in range(4)]
        tree = MerkleTree.from_raw_transactions(nodes)

        msg = tree.root

        sig1, sk1, vk1 = self._create_merkle_sig(msg)
        sig2, sk2, vk2 = self._create_merkle_sig(msg)
        sig3, sk3, vk3 = self._create_merkle_sig(msg)
        sig4, sk4, vk4 = self._create_merkle_sig(msg)

        signatures = [sig1, sig2, sig3, sig4]

        bc1 = BlockContender.create(signatures, merkle_leaves=tree.leaves_as_hex, prev_block_hash='A' * 64)
        bc2 = BlockContender.create(signatures, merkle_leaves=tree.leaves_as_hex, prev_block_hash='A' * 64)

        self.assertEquals(bc1, bc2)
Ejemplo n.º 4
0
    def check_majority(self):
        self.log.debug("delegate has {} signatures out of {} total TESTNET_DELEGATES"
                       .format(len(self.signatures), self.NUM_DELEGATES))

        if len(self.signatures) >= MAJORITY and not self.in_consensus:
            self.log.important("Delegate in consensus!")
            self.in_consensus = True

            # Create BlockContender and send it to all Masternode(s)
            bc = BlockContender.create(signatures=self.signatures, merkle_leaves=self.merkle.leaves_as_hex,
                                       prev_block_hash=self.parent.current_hash)
            for mn_vk in VKBook.get_masternodes():
                self.log.debug("Delegate sending block contender to masternode with VK {}".format(mn_vk))
                self.parent.composer.send_request_msg(message=bc, vk=mn_vk)
Ejemplo n.º 5
0
    def check_majority(self):
        self.log.debug(
            "delegate has {} signatures out of {} total delegates".format(
                len(self.signatures), self.NUM_DELEGATES))

        if len(self.signatures) >= majority:
            self.log.important("Delegate in consensus!")
            self.in_consensus = True

            # DEBUG LINE -- todo remove later
            # self.log.notice("Delegate creating contender with merk leaves {}".format(self.merkle.leaves_as_hex))

            # Create BlockContender and send it to all Masternode(s)
            bc = BlockContender.create(signatures=self.signatures,
                                       merkle_leaves=self.merkle.leaves_as_hex)
            for mn_vk in VKBook.get_masternodes():
                self.parent.composer.send_request_msg(message=bc, vk=mn_vk)
Ejemplo n.º 6
0
    def test_bc_creation_from_bytes(self):
        msg = b'DEADBEEF'
        nodes = ['A' * 64, 'B' * 64]

        sig1, sk1, vk1 = self._create_merkle_sig(msg)
        sig2, sk2, vk2 = self._create_merkle_sig(msg)
        signatures = [sig1, sig2]

        bc = BlockContender.create(signatures, nodes, 'A' * 64)

        clone = BlockContender.from_bytes(bc.serialize())

        # assert bc.signatures = signature over all signatures
        for i in range(len(signatures)):
            self.assertTrue(clone.signatures[i] == (signatures[i]))

        for i in range(len(nodes)):
            self.assertTrue(clone.merkle_leaves[i] == (nodes[i]))
Ejemplo n.º 7
0
    def test_validate_signatures_invalid(self):
        nodes = [secrets.token_bytes(8) for _ in range(4)]
        tree = MerkleTree.from_raw_transactions(nodes)

        msg = tree.root

        bad_msg = b'lol this is def not a merkle root'

        sig1, sk1, vk1 = self._create_merkle_sig(msg)
        sig2, sk2, vk2 = self._create_merkle_sig(msg)
        sig3, sk3, vk3 = self._create_merkle_sig(msg)
        sig4, sk4, vk4 = self._create_merkle_sig(bad_msg)

        signatures = [sig1, sig2, sig3, sig4]

        bc = BlockContender.create(signatures, merkle_leaves=tree.leaves_as_hex, prev_block_hash="A" * 64)
        is_valid = bc.validate_signatures()

        self.assertFalse(is_valid)
Ejemplo n.º 8
0
    def test_bc_creation(self):
        """
        Tests that a created BlockContender has the expected properties
        """
        msg = b'DEADBEEF'
        nodes = ['A' * 64, 'B' * 64]

        sig1, sk1, vk1 = self._create_merkle_sig(msg)
        sig2, sk2, vk2 = self._create_merkle_sig(msg)
        signatures = [sig1, sig2]
        prev_b_hash = 'A' * 64

        bc = BlockContender.create(signatures, nodes, prev_b_hash)

        # assert bc.signatures = signature over all signatures
        for i in range(len(signatures)):
            self.assertTrue(bc.signatures[i] == (signatures[i]))

        for i in range(len(nodes)):
            self.assertTrue(bc.merkle_leaves[i] == (nodes[i]))

        self.assertEquals(bc.prev_block_hash, prev_b_hash)