Skip to content

Commit

Permalink
Merge pull request #4248 from devos50/validate_block
Browse files Browse the repository at this point in the history
Validating payout block when receiving it
  • Loading branch information
devos50 authored Feb 25, 2019
2 parents 0162dbb + 44ddb95 commit a3bff8c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 2 additions & 3 deletions Tribler/Core/Modules/wallet/bandwidth_block.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import absolute_import

from Tribler.pyipv8.ipv8.attestation.trustchain.block import TrustChainBlock, EMPTY_SIG, ValidationResult,\
GENESIS_SEQ, GENESIS_HASH
from Tribler.pyipv8.ipv8.attestation.trustchain.block import EMPTY_SIG, GENESIS_HASH, GENESIS_SEQ, TrustChainBlock, \
ValidationResult
from Tribler.pyipv8.ipv8.messaging.deprecated.encoding import encode


Expand Down Expand Up @@ -60,7 +60,6 @@ def create(cls, block_type, transaction, database, public_key, link=None, link_p
def validate_transaction(self, database):
"""
Validates this transaction
:param transaction the transaction to validate
:param database: the database to check against
:return: A tuple consisting of a ValidationResult and a list of user string errors
"""
Expand Down
24 changes: 24 additions & 0 deletions Tribler/Test/Community/Tunnel/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,30 @@ def test_payouts_e2e(self):
self.assertTrue(self.nodes[1].overlay.bandwidth_wallet.get_bandwidth_tokens() >= 0)
self.assertTrue(self.nodes[2].overlay.bandwidth_wallet.get_bandwidth_tokens() > 0)

@inlineCallbacks
def test_payouts_invalid_block(self):
"""
Test whether we do not payout if we received an invalid payout block
"""
self.add_node_to_experiment(self.create_node())

# Build a tunnel
self.nodes[1].overlay.settings.become_exitnode = True
yield self.introduce_nodes()
self.nodes[0].overlay.build_tunnels(1)
yield self.deliver_messages(timeout=.5)

self.assertEqual(self.nodes[0].overlay.tunnels_ready(1), 1.0)

# Perform an invalid payout
payout_amount = -250 * 1024 * 1024
self.nodes[0].overlay.do_payout(self.nodes[1].my_peer, 1234, payout_amount, 1)

yield self.deliver_messages(timeout=.5)

# Node 1 should not have counter-signed this block and thus not received tokens
self.assertFalse(self.nodes[1].overlay.bandwidth_wallet.get_bandwidth_tokens())

@inlineCallbacks
def test_decline_competing_slot(self):
"""
Expand Down
5 changes: 5 additions & 0 deletions Tribler/community/triblertunnel/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ def on_payout_block(self, source_address, data):
block = TriblerBandwidthBlock.from_payload(payload, self.serializer)
self.bandwidth_wallet.trustchain.process_half_block(block, peer)

# Check whether the block has been added to the database and has been verified
if not self.bandwidth_wallet.trustchain.persistence.contains(block):
self.logger.warning("Not proceeding with payout - received payout block is not valid")
return

# Send the next payout
if payload.circuit_id in self.relay_from_to and block.transaction['down'] > payload.base_amount:
relay = self.relay_from_to[payload.circuit_id]
Expand Down

0 comments on commit a3bff8c

Please sign in to comment.