Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove handling of ChannelNewDeposit event #496

Merged
merged 2 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/pathfinding_service/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,7 @@ def get_iou(

def upsert_channel_view(self, channel_view: ChannelView) -> None:
cv_dict = ChannelView.Schema().dump(channel_view)
for key in (
"channel_id",
"settle_timeout",
"capacity",
"reveal_timeout",
"deposit",
"update_nonce",
):
for key in ("channel_id", "settle_timeout", "capacity", "reveal_timeout", "update_nonce"):
cv_dict[key] = hex256(cv_dict[key])
cv_dict["fee_schedule_sender"] = json.dumps(cv_dict["fee_schedule_sender"])
cv_dict["fee_schedule_receiver"] = json.dumps(cv_dict["fee_schedule_receiver"])
Expand Down
12 changes: 1 addition & 11 deletions src/pathfinding_service/model/channel_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,13 @@ class ChannelView:
token_network_address: TokenNetworkAddress = field(
metadata={"marshmallow_field": ChecksumAddress(required=True)}
)
capacity: TokenAmount = None # type: ignore
capacity: TokenAmount = TokenAmount(0)
reveal_timeout: int = DEFAULT_REVEAL_TIMEOUT
deposit: TokenAmount = TokenAmount(0)
update_nonce: Nonce = Nonce(0)
fee_schedule_sender: FeeSchedule = field(default_factory=FeeSchedule)
fee_schedule_receiver: FeeSchedule = field(default_factory=FeeSchedule)
Schema: ClassVar[Type[marshmallow.Schema]]

def __post_init__(self) -> None:
if self.capacity is None:
self.capacity = self.deposit

def update_deposit(self, total_deposit: TokenAmount) -> None:
if total_deposit > self.deposit:
self.capacity = TokenAmount(self.capacity + total_deposit - self.deposit)
self.deposit = TokenAmount(total_deposit)

def update_capacity(
self, capacity: TokenAmount, nonce: Nonce = Nonce(0), reveal_timeout: int = None
) -> None:
Expand Down
28 changes: 0 additions & 28 deletions src/pathfinding_service/model/token_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,13 @@ def handle_channel_opened_event(
participant1=participant1,
participant2=participant2,
settle_timeout=settle_timeout,
deposit=TokenAmount(0),
),
ChannelView(
token_network_address=self.address,
channel_id=channel_identifier,
participant1=participant2,
participant2=participant1,
settle_timeout=settle_timeout,
deposit=TokenAmount(0),
),
]

Expand All @@ -208,32 +206,6 @@ def add_channel_view(self, channel_view: ChannelView) -> None:
)
self.G.add_edge(channel_view.participant1, channel_view.participant2, view=channel_view)

def handle_channel_new_deposit_event(
self, channel_identifier: ChannelID, receiver: Address, total_deposit: TokenAmount
) -> Optional[ChannelView]:
""" Register a new balance for the beneficiary.

Corresponds to the ChannelNewDeposit event. Called by the contract event listener. """

try:
participant1, participant2 = self.channel_id_to_addresses[channel_identifier]
if receiver == participant1:
channel_view = self.G[participant1][participant2]["view"]
elif receiver == participant2:
channel_view = self.G[participant2][participant1]["view"]
else:
log.error("Receiver in ChannelNewDeposit does not fit the internal channel")
return None
except KeyError:
log.error(
"Received ChannelNewDeposit event for unknown channel",
channel_identifier=channel_identifier,
)
return None

channel_view.update_deposit(total_deposit=total_deposit)
return channel_view

def handle_channel_closed_event(self, channel_identifier: ChannelID) -> None:
""" Close a channel. This doesn't mean that the channel is settled yet, but it cannot
transfer any more.
Expand Down
1 change: 0 additions & 1 deletion src/pathfinding_service/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ CREATE TABLE channel_view (
settle_timeout HEX_INT NOT NULL,
capacity HEX_INT NOT NULL,
reveal_timeout HEX_INT NOT NULL,
deposit HEX_INT NOT NULL,
update_nonce HEX_INT,
fee_schedule_sender JSON,
fee_schedule_receiver JSON,
Expand Down
18 changes: 0 additions & 18 deletions src/pathfinding_service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from raiden_libs.events import (
Event,
ReceiveChannelClosedEvent,
ReceiveChannelNewDepositEvent,
ReceiveChannelOpenedEvent,
ReceiveTokenNetworkCreatedEvent,
UpdatedHeadBlockEvent,
Expand Down Expand Up @@ -183,8 +182,6 @@ def handle_event(self, event: Event) -> None:
self.handle_token_network_created(event)
elif isinstance(event, ReceiveChannelOpenedEvent):
self.handle_channel_opened(event)
elif isinstance(event, ReceiveChannelNewDepositEvent):
self.handle_channel_new_deposit(event)
elif isinstance(event, ReceiveChannelClosedEvent):
self.handle_channel_closed(event)
elif isinstance(event, UpdatedHeadBlockEvent):
Expand Down Expand Up @@ -226,21 +223,6 @@ def handle_channel_opened(self, event: ReceiveChannelOpenedEvent) -> None:
):
self.handle_message(message)

def handle_channel_new_deposit(self, event: ReceiveChannelNewDepositEvent) -> None:
token_network = self.get_token_network(event.token_network_address)
if token_network is None:
return

log.info("Received ChannelNewDeposit event", event_=event)

channel_view = token_network.handle_channel_new_deposit_event(
channel_identifier=event.channel_identifier,
receiver=event.participant_address,
total_deposit=event.total_deposit,
)
if channel_view:
self.database.upsert_channel_view(channel_view)

def handle_channel_closed(self, event: ReceiveChannelClosedEvent) -> None:
token_network = self.get_token_network(event.token_network_address)
if token_network is None:
Expand Down
7 changes: 0 additions & 7 deletions src/raiden_libs/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from raiden_libs.events import (
Event,
ReceiveChannelClosedEvent,
ReceiveChannelNewDepositEvent,
ReceiveChannelOpenedEvent,
ReceiveChannelSettledEvent,
ReceiveMonitoringNewBalanceProofEvent,
Expand Down Expand Up @@ -103,12 +102,6 @@ def parse_token_network_event(event: dict) -> Optional[Event]:
settle_timeout=event["args"]["settle_timeout"],
**common_infos,
)
if event_name == ChannelEvent.DEPOSIT:
return ReceiveChannelNewDepositEvent(
participant_address=decode_hex(event["args"]["participant"]),
total_deposit=event["args"]["total_deposit"],
**common_infos,
)
if event_name == ChannelEvent.CLOSED:
return ReceiveChannelClosedEvent(
closing_participant=decode_hex(event["args"]["closing_participant"]), **common_infos
Expand Down
9 changes: 0 additions & 9 deletions src/raiden_libs/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ class ReceiveChannelOpenedEvent(Event):
block_number: BlockNumber


@dataclass
class ReceiveChannelNewDepositEvent(Event):
token_network_address: TokenNetworkAddress
channel_identifier: ChannelID
participant_address: Address
total_deposit: TokenAmount
block_number: BlockNumber


@dataclass
class ReceiveChannelClosedEvent(Event):
token_network_address: TokenNetworkAddress
Expand Down
49 changes: 15 additions & 34 deletions tests/pathfinding/fixtures/network_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ def channel_descriptions_case_1() -> List:
# The tuples in channel_descriptions define the following:
# (
# p1_index,
# p1_deposit,
# p1_capacity,
# p1_fee,
# p1_reveal_timeout,
# p1_reachability,
# p2_index,
# p2_deposit,
# p2_capacity,
# p2_fee,
# p2_reveal_timeout,
Expand All @@ -55,13 +53,13 @@ def channel_descriptions_case_1() -> List:
reach = AddressReachability.REACHABLE

channel_descriptions = [
(0, 100, 90, 10, 2, reach, 1, 50, 60, 15, 2, reach, 14), # capacities 90 -- 60
(1, 40, 130, 8, 2, reach, 2, 130, 40, 12, 2, reach, 14), # capacities 130 -- 40
(2, 90, 80, 7, 2, reach, 3, 10, 20, 10, 2, reach, 3), # capacities 80 -- 20
(3, 50, 50, 11, 2, reach, 4, 50, 50, 11, 2, reach, 14), # capacities 50 -- 50
(0, 40, 0, 15, 2, reach, 2, 80, 120, 25, 2, reach, 14), # capacities 0 -- 120
(1, 30, 35, 100, 2, reach, 4, 40, 35, 18, 2, reach, 14), # capacities 35 -- 35
(5, 500, 550, 30, 2, reach, 6, 750, 700, 40, 2, reach, 14), # capacities 550 -- 700
(0, 90, 10, 2, reach, 1, 60, 15, 2, reach, 14), # capacities 90 -- 60
(1, 130, 8, 2, reach, 2, 40, 12, 2, reach, 14), # capacities 130 -- 40
(2, 80, 7, 2, reach, 3, 20, 10, 2, reach, 3), # capacities 80 -- 20
(3, 50, 11, 2, reach, 4, 50, 11, 2, reach, 14), # capacities 50 -- 50
(0, 0, 15, 2, reach, 2, 120, 25, 2, reach, 14), # capacities 0 -- 120
(1, 35, 100, 2, reach, 4, 35, 18, 2, reach, 14), # capacities 35 -- 35
(5, 550, 30, 2, reach, 6, 700, 40, 2, reach, 14), # capacities 550 -- 700
]
return channel_descriptions

Expand All @@ -77,13 +75,11 @@ def channel_descriptions_case_2() -> List:
# The tuples in channel_descriptions define the following:
# (
# p1_index,
# p1_deposit,
# p1_capacity,
# p1_fee,
# p1_reveal_timeout,
# p1_reachability,
# p2_index,
# p2_deposit,
# p2_capacity,
# p2_fee,
# p2_reveal_timeout,
Expand All @@ -98,13 +94,13 @@ def channel_descriptions_case_2() -> List:
reach = AddressReachability.REACHABLE

channel_descriptions = [
(0, 100, 90, 3000, 2, reach, 1, 50, 60, 3000, 2, reach, 15), # capacities 90 -- 60
(1, 40, 130, 2000, 2, reach, 4, 130, 40, 2000, 2, reach, 15), # capacities 130 -- 40
(0, 90, 80, 1000, 2, reach, 2, 10, 10, 1000, 2, reach, 15), # capacities 80 -- 10
(2, 50, 50, 1500, 2, reach, 3, 50, 50, 1500, 2, reach, 15), # capacities 50 -- 50
(3, 100, 60, 1000, 2, reach, 4, 80, 120, 1000, 2, reach, 15), # capacities 60 -- 120
(2, 30, 35, 1000, 2, reach, 5, 40, 35, 1000, 2, reach, 15), # capacities 35 -- 35
(5, 500, 550, 1000, 2, reach, 4, 750, 700, 1000, 2, reach, 15), # capacities 550 -- 700
(0, 90, 3000, 2, reach, 1, 60, 3000, 2, reach, 15), # capacities 90 -- 60
(1, 130, 2000, 2, reach, 4, 40, 2000, 2, reach, 15), # capacities 130 -- 40
(0, 80, 1000, 2, reach, 2, 10, 1000, 2, reach, 15), # capacities 80 -- 10
(2, 50, 1500, 2, reach, 3, 50, 1500, 2, reach, 15), # capacities 50 -- 50
(3, 60, 1000, 2, reach, 4, 120, 1000, 2, reach, 15), # capacities 60 -- 120
(2, 35, 1000, 2, reach, 5, 35, 1000, 2, reach, 15), # capacities 35 -- 35
(5, 550, 1000, 2, reach, 4, 700, 1000, 2, reach, 15), # capacities 550 -- 700
]
return channel_descriptions

Expand All @@ -117,13 +113,11 @@ def channel_descriptions_case_3() -> List:
# The tuples in channel_descriptions define the following:
# (
# p1_index,
# p1_deposit,
# p1_capacity,
# p1_fee,
# p1_reveal_timeout,
# p1_reachability,
# p2_index,
# p2_deposit,
# p2_capacity,
# p2_fee,
# p2_reveal_timeout,
Expand All @@ -140,7 +134,7 @@ def channel_descriptions_case_3() -> List:
reach = AddressReachability.REACHABLE

channel_descriptions = [
(a, 100, 100, 0, 2, reach, b, 100, 100, 0, 2, reach, 15)
(a, 100, 0, 2, reach, b, 100, 0, 2, reach, 15)
for a, b in [
(0, 1),
(1, 2),
Expand Down Expand Up @@ -174,13 +168,11 @@ def populate_token_network(
channel_id,
(
p1_index,
p1_deposit,
p1_capacity,
_p1_fee,
p1_reveal_timeout,
p1_reachability,
p2_index,
p2_deposit,
p2_capacity,
_p2_fee,
p2_reveal_timeout,
Expand All @@ -197,17 +189,6 @@ def populate_token_network(
settle_timeout=settle_timeout,
)

token_network.handle_channel_new_deposit_event(
channel_identifier=ChannelID(channel_id),
receiver=participant1,
total_deposit=p1_deposit,
)
token_network.handle_channel_new_deposit_event(
channel_identifier=ChannelID(channel_id),
receiver=participant2,
total_deposit=p2_deposit,
)

token_network.handle_channel_balance_update_message(
PFSCapacityUpdate(
canonical_identifier=CanonicalIdentifier(
Expand Down
33 changes: 5 additions & 28 deletions tests/pathfinding/test_blockchain_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ def test_pfs_with_mocked_client( # pylint: disable=too-many-arguments
channel_identifiers = []
for (
p1_index,
p1_deposit,
_p1_capacity,
p1_capacity,
_p1_fee,
_p1_reveal_timeout,
_p1_reachability,
p2_index,
p2_deposit,
_p2_capacity,
p2_capacity,
_p2_fee,
_p2_reveal_timeout,
_p2_reachability,
Expand All @@ -88,8 +86,8 @@ def test_pfs_with_mocked_client( # pylint: disable=too-many-arguments
channel_identifiers.append(channel_id)

for address, partner_address, amount in [
(clients[p1_index], clients[p2_index], p1_deposit),
(clients[p2_index], clients[p1_index], p2_deposit),
(clients[p1_index], clients[p2_index], p1_capacity),
(clients[p2_index], clients[p1_index], p2_capacity),
]:
custom_token.functions.mint(amount).transact({"from": address})
custom_token.functions.approve(token_network.address, amount).transact(
Expand All @@ -108,30 +106,11 @@ def test_pfs_with_mocked_client( # pylint: disable=too-many-arguments
)

# check that deposits, settle_timeout and transfers got registered
for (
index,
(
_p1_index,
p1_deposit,
_p1_capacity,
_p1_fee,
_p1_reveal_timeout,
_p1_reachability,
_p2_index,
p2_deposit,
_p2_capacity,
_p2_fee,
_p2_reveal_timeout,
_p2_reachability,
_settle_timeout,
),
) in enumerate(channel_descriptions_case_1):
for index, _ in enumerate(channel_descriptions_case_1):
palango marked this conversation as resolved.
Show resolved Hide resolved
channel_identifier = channel_identifiers[index]
p1_address, p2_address = token_network_model.channel_id_to_addresses[channel_identifier]
view1: ChannelView = graph[p1_address][p2_address]["view"]
view2: ChannelView = graph[p2_address][p1_address]["view"]
assert view1.deposit == p1_deposit
assert view2.deposit == p2_deposit
assert view1.settle_timeout == TEST_SETTLE_TIMEOUT_MIN
assert view2.settle_timeout == TEST_SETTLE_TIMEOUT_MIN
assert view1.reveal_timeout == DEFAULT_REVEAL_TIMEOUT
Expand All @@ -142,13 +121,11 @@ def test_pfs_with_mocked_client( # pylint: disable=too-many-arguments
index,
(
p1_index,
_p1_deposit,
_p1_capacity,
_p1_fee,
_p1_reveal_timeout,
_p1_reachability,
p2_index,
_p2_deposit,
_p2_capacity,
_p2_fee,
_p2_reveal_timeout,
Expand Down
Loading