Skip to content

Commit

Permalink
Fixed bug in relay payouts
Browse files Browse the repository at this point in the history
When a relay initiates a payout to the next hop in the circuit, the values were computed incorrectly. This PR fixes the behaviour and extends the payout tests to capture such bugs.
  • Loading branch information
devos50 committed Dec 15, 2020
1 parent 2e7f154 commit b6774ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def on_payout(self, source_address: Address, data: bytes) -> None:
if payload.circuit_id in self.relay_from_to and tx.amount > payload.base_amount:
relay = self.relay_from_to[payload.circuit_id]
self._logger.info("Sending next payout to peer %s", relay.peer)
self.do_payout(relay.peer, relay.circuit_id, tx.amount - payload.base_amount * 2, payload.base_amount)
self.do_payout(relay.peer, relay.circuit_id, payload.base_amount * 2, payload.base_amount)

def clean_from_slots(self, circuit_id):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ async def test_payouts(self):
self.add_node_to_experiment(self.create_node())
self.add_node_to_experiment(self.create_node())

# Make sure that every node has some initial transactions. This will help us to detect bugs in the
# relay payout logic, e.g. https://github.com/Tribler/tribler/issues/5789.
for node in self.nodes:
for other_node in self.nodes:
if node == other_node:
continue

await node.overlay.bandwidth_community.do_payout(other_node.my_peer, 100 * 1024 * 1024)

# Build a tunnel
self.nodes[2].overlay.settings.peer_flags.add(PEER_FLAG_EXIT_BT)
await self.introduce_nodes()
Expand All @@ -302,6 +311,15 @@ async def test_payouts(self):
self.assertTrue(self.nodes[1].overlay.bandwidth_community.database.get_my_balance() > 0)
self.assertTrue(self.nodes[2].overlay.bandwidth_community.database.get_my_balance() > 0)

balances = []
for node_nr in [0, 1, 2]:
balances.append(self.nodes[node_nr].overlay.bandwidth_community.database.get_my_balance())

balances.sort()
self.assertEqual(balances[0], -750 * 1024 * 1024)
self.assertEqual(balances[1], 250 * 1024 * 1024)
self.assertEqual(balances[2], 500 * 1024 * 1024)

async def test_invalid_payout(self):
"""
Test whether an invalid payout to another peer is ignored
Expand Down

0 comments on commit b6774ca

Please sign in to comment.