Ejemplo n.º 1
0
    def bootstrap_new_identity(self, amount):
        """
        One-way payment channel.
        Create a new temporary identity, and transfer funds to the new identity.
        A different party can then take the result and do a transfer from the temporary identity to itself
        """

        # Create new identity for the temporary identity
        crypto = ECCrypto()
        tmp_peer = Peer(crypto.generate_key(u"curve25519"))

        # Create the transaction specification
        transaction = {'up': 0, 'down': amount, 'type': 'tribler_bandwidth'}

        # Create the two half blocks that form the transaction
        local_half_block = TriblerBandwidthBlock.create(
            'tribler_bandwidth',
            transaction,
            self.trustchain.persistence,
            self.trustchain.my_peer.public_key.key_to_bin(),
            link_pk=tmp_peer.public_key.key_to_bin())
        local_half_block.sign(self.trustchain.my_peer.key)
        tmp_half_block = TriblerBandwidthBlock.create(
            'tribler_bandwidth',
            transaction,
            self.trustchain.persistence,
            tmp_peer.public_key.key_to_bin(),
            link=local_half_block,
            link_pk=self.trustchain.my_peer.public_key.key_to_bin())
        tmp_half_block.sign(tmp_peer.key)

        self.trustchain.persistence.add_block(local_half_block)
        self.trustchain.persistence.add_block(tmp_half_block)

        # Create the bootstrapped identity format
        block = {
            'block_hash': tmp_half_block.hash.encode('base64'),
            'sequence_number': tmp_half_block.sequence_number
        }

        result = {
            'private_key': tmp_peer.key.key_to_bin().encode('base64'),
            'transaction': {
                'up': amount,
                'down': 0
            },
            'block': block
        }
        return result
Ejemplo n.º 2
0
    def do_payout(self, peer, circuit_id, amount, base_amount):
        """
        Perform a payout to a specific peer.
        :param peer: The peer to perform the payout to, usually the next node in the circuit.
        :param circuit_id: The circuit id of the payout, used by the subsequent node.
        :param amount: The amount to put in the transaction, multiplier of base_amount.
        :param base_amount: The base amount for the payouts.
        """
        self.logger.info("Sending payout of %d (base: %d) to %s (cid: %s)",
                         amount, base_amount, peer, circuit_id)

        block = TriblerBandwidthBlock.create(
            'tribler_bandwidth', {
                'up': 0,
                'down': amount
            },
            self.bandwidth_wallet.trustchain.persistence,
            self.my_peer.public_key.key_to_bin(),
            link_pk=peer.public_key.key_to_bin())
        block.sign(self.my_peer.key)
        self.bandwidth_wallet.trustchain.persistence.add_block(block)

        payload = PayoutPayload.from_half_block(block, circuit_id,
                                                base_amount).to_pack_list()
        packet = self._ez_pack(self._prefix, 23, [payload], False)
        self.send_packet([peer], u"payout", packet)
Ejemplo n.º 3
0
 def test_validate_linked_down(self):
     db = MockDatabase()
     (block1, block2, _, _) = TestBlocks.setup_validate()
     db.add_block(block2)
     # Act
     db.add_block(TriblerBandwidthBlock.create('tribler_bandwidth', block1.transaction, db,
                                               block1.link_public_key, block1))
     block1.transaction["down"] -= 5
     result = block1.validate(db)
     self.assertEqual(result[0], ValidationResult.invalid)
     self.assertIn("Down/up mismatch on linked block", result[1])
Ejemplo n.º 4
0
 def test_validate_linked_down(self):
     db = MockDatabase()
     (block1, block2, _, _) = TestBlocks.setup_validate()
     db.add_block(block2)
     # Act
     db.add_block(TriblerBandwidthBlock.create('tribler_bandwidth', block1.transaction, db,
                                               block1.link_public_key, block1))
     block1.transaction["down"] -= 5
     result = block1.validate(db)
     self.assertEqual(result[0], ValidationResult.invalid)
     self.assertIn("Down/up mismatch on linked block", result[1])
Ejemplo n.º 5
0
    def bootstrap_new_identity(self, amount):
        """
        One-way payment channel.
        Create a new temporary identity, and transfer funds to the new identity.
        A different party can then take the result and do a transfer from the temporary identity to itself
        """

        # Create new identity for the temporary identity
        crypto = ECCrypto()
        tmp_peer = Peer(crypto.generate_key(u"curve25519"))

        # Create the transaction specification
        transaction = {
            'up': 0,
            'down': amount,
            'type': 'tribler_bandwidth'
        }

        # Create the two half blocks that form the transaction
        local_half_block = TriblerBandwidthBlock.create('tribler_bandwidth', transaction, self.trustchain.persistence,
                                                        self.trustchain.my_peer.public_key.key_to_bin(),
                                                        link_pk=tmp_peer.public_key.key_to_bin())
        local_half_block.sign(self.trustchain.my_peer.key)
        tmp_half_block = TriblerBandwidthBlock.create('tribler_bandwidth', transaction, self.trustchain.persistence,
                                                      tmp_peer.public_key.key_to_bin(),
                                                      link=local_half_block,
                                                      link_pk=self.trustchain.my_peer.public_key.key_to_bin())
        tmp_half_block.sign(tmp_peer.key)

        self.trustchain.persistence.add_block(local_half_block)
        self.trustchain.persistence.add_block(tmp_half_block)

        # Create the bootstrapped identity format
        block = {'block_hash': tmp_half_block.hash.encode('base64'),
                 'sequence_number': tmp_half_block.sequence_number}

        result = {'private_key': tmp_peer.key.key_to_bin().encode('base64'),
                  'transaction': {'up': amount, 'down': 0}, 'block': block}
        return result
Ejemplo n.º 6
0
    def do_payout(self, peer, circuit_id, amount, base_amount):
        """
        Perform a payout to a specific peer.
        :param peer: The peer to perform the payout to, usually the next node in the circuit.
        :param circuit_id: The circuit id of the payout, used by the subsequent node.
        :param amount: The amount to put in the transaction, multiplier of base_amount.
        :param base_amount: The base amount for the payouts.
        """
        self.logger.info("Sending payout of %d (base: %d) to %s (cid: %s)", amount, base_amount, peer, circuit_id)

        block = TriblerBandwidthBlock.create(
            'tribler_bandwidth',
            {'up': 0, 'down': amount},
            self.bandwidth_wallet.trustchain.persistence,
            self.my_peer.public_key.key_to_bin(),
            link_pk=peer.public_key.key_to_bin())
        block.sign(self.my_peer.key)
        self.bandwidth_wallet.trustchain.persistence.add_block(block)

        payload = PayoutPayload.from_half_block(block, circuit_id, base_amount).to_pack_list()
        packet = self._ez_pack(self._prefix, 23, [payload], False)
        self.send_packet([peer], packet)