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

mark other output as foreign for cooperative close #74

Merged
merged 1 commit into from
Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.Set;
import java.util.stream.Collectors;

import static de.cotto.bitbook.ownership.OwnershipStatus.OWNED;

@Component
public class ClosedChannelsService {
private static final String DEFAULT_ADDRESS_DESCRIPTION = "lnd";
Expand Down Expand Up @@ -46,6 +48,7 @@ private ClosedChannel addFromClosedChannel(ClosedChannel closedChannel) {

setTransactionDescriptions(closedChannel);
setForSettlementAddress(closedChannel);
setOtherOutputAsForeignForCooperativeClose(closedChannel);
setChannelAddressOwnershipAndDescription(channelAddress, closedChannel.getOpenInitiator(), remotePubkey);
addFromHtlcSweepTransactions(closedChannel);
return closedChannel;
Expand Down Expand Up @@ -78,6 +81,14 @@ private void setForSettlementAddress(ClosedChannel closedChannel) {
});
}

private void setOtherOutputAsForeignForCooperativeClose(ClosedChannel closedChannel) {
if (closedChannel.getCloseType().isCooperative()) {
closedChannel.getClosingTransaction().getOutputAddresses().stream()
.filter(address -> !OWNED.equals(addressOwnershipService.getOwnershipStatus(address)))
.forEach(addressOwnershipService::setAddressAsForeign);
}
}

private void setChannelAddressOwnershipAndDescription(
String channelAddress,
Initiator openInitiator,
Expand All @@ -92,7 +103,7 @@ private void setChannelAddressOwnership(String channelAddress, Initiator openIni
addressOwnershipService.setAddressAsOwned(channelAddress);
} else if (openInitiator.equals(Initiator.REMOTE)) {
OwnershipStatus ownershipStatus = addressOwnershipService.getOwnershipStatus(channelAddress);
if (!OwnershipStatus.OWNED.equals(ownershipStatus)) {
if (!OWNED.equals(ownershipStatus)) {
addressOwnershipService.setAddressAsForeign(channelAddress);
}
}
Expand Down
4 changes: 4 additions & 0 deletions lnd/src/main/java/de/cotto/bitbook/lnd/model/CloseType.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public static CloseType fromStringAndInitiator(String closeType, String closeIni
return COOPERATIVE;
}

public boolean isCooperative() {
return this == COOPERATIVE || this == COOPERATIVE_LOCAL || this == COOPERATIVE_REMOTE;
}

@Override
public String toString() {
return stringRepresentation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import de.cotto.bitbook.backend.TransactionDescriptionService;
import de.cotto.bitbook.lnd.model.CloseType;
import de.cotto.bitbook.lnd.model.ClosedChannel;
import de.cotto.bitbook.lnd.model.Initiator;
import de.cotto.bitbook.ownership.AddressOwnershipService;
import de.cotto.bitbook.ownership.OwnershipStatus;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -13,14 +15,14 @@

import java.util.Set;

import static de.cotto.bitbook.backend.transaction.model.OutputFixtures.OUTPUT_ADDRESS_2;
import static de.cotto.bitbook.backend.transaction.model.TransactionFixtures.TRANSACTION;
import static de.cotto.bitbook.lnd.model.ClosedChannelFixtures.AMBIGUOUS_SETTLEMENT_ADDRESS;
import static de.cotto.bitbook.lnd.model.ClosedChannelFixtures.CLOSED_CHANNEL;
import static de.cotto.bitbook.lnd.model.ClosedChannelFixtures.SWEEP_TRANSACTION_HASH;
import static de.cotto.bitbook.lnd.model.ClosedChannelFixtures.WITH_RESOLUTION;
import static de.cotto.bitbook.lnd.model.Initiator.LOCAL;
import static de.cotto.bitbook.lnd.model.Initiator.REMOTE;
import static de.cotto.bitbook.lnd.model.Initiator.UNKNOWN;
import static de.cotto.bitbook.ownership.OwnershipStatus.OWNED;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -67,6 +69,7 @@ void marks_channel_address_as_foreign_for_initiator_remote() {

@Test
void does_not_change_channel_address_ownership_if_already_marked_as_owned() {
when(addressOwnershipService.getOwnershipStatus(any())).thenReturn(OwnershipStatus.UNKNOWN);
when(addressOwnershipService.getOwnershipStatus(CLOSED_CHANNEL.getChannelAddress())).thenReturn(OWNED);
load(CLOSED_CHANNEL.toBuilder().withOpenInitiator(REMOTE).build());
verify(addressOwnershipService, never()).setAddressAsForeign(CLOSED_CHANNEL.getChannelAddress());
Expand All @@ -80,7 +83,7 @@ void marks_channel_address_as_owned_for_initiator_local() {

@Test
void does_not_set_channel_ownership_for_initiator_unknown() {
load(CLOSED_CHANNEL.toBuilder().withOpenInitiator(UNKNOWN).build());
load(CLOSED_CHANNEL.toBuilder().withOpenInitiator(Initiator.UNKNOWN).build());
verify(addressOwnershipService, never()).setAddressAsOwned(CLOSED_CHANNEL.getChannelAddress());
verify(addressOwnershipService, never()).setAddressAsForeign(CLOSED_CHANNEL.getChannelAddress());
}
Expand Down Expand Up @@ -135,6 +138,25 @@ void marks_settlement_address_as_owned() {
verify(addressOwnershipService).setAddressAsOwned(CLOSED_CHANNEL.getSettlementAddress().orElseThrow());
}

@Test
void for_cooperative_close_marks_other_output_address_as_foreign() {
load(CLOSED_CHANNEL);
verify(addressOwnershipService).setAddressAsForeign(OUTPUT_ADDRESS_2);
}

@Test
void for_force_close_does_not_mark_other_output_address_as_foreign() {
load(CLOSED_CHANNEL.toBuilder().withCloseType(CloseType.FORCE_REMOTE).build());
verify(addressOwnershipService, never()).setAddressAsForeign(OUTPUT_ADDRESS_2);
}

@Test
void for_cooperative_close_does_not_mark_other_output_address_as_foreign_if_already_marked_as_owned() {
when(addressOwnershipService.getOwnershipStatus(OUTPUT_ADDRESS_2)).thenReturn(OWNED);
load(CLOSED_CHANNEL);
verify(addressOwnershipService, never()).setAddressAsForeign(OUTPUT_ADDRESS_2);
}

@Test
void adds_description_for_settlement_address() {
load(CLOSED_CHANNEL);
Expand Down
14 changes: 14 additions & 0 deletions lnd/src/test/java/de/cotto/bitbook/lnd/model/CloseTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ void testToString_force_local() {
assertThat(FORCE_LOCAL).hasToString("force local");
}

@Test
void isCooperative_false() {
assertThat(FORCE_LOCAL.isCooperative()).isFalse();
assertThat(FORCE_REMOTE.isCooperative()).isFalse();
}

@Test
@SuppressWarnings("PMD.JUnitTestContainsTooManyAsserts")
void isCooperative_true() {
assertThat(COOPERATIVE.isCooperative()).isTrue();
assertThat(COOPERATIVE_LOCAL.isCooperative()).isTrue();
assertThat(COOPERATIVE_REMOTE.isCooperative()).isTrue();
}

@Test
void fromStringAndInitiator_cooperative_unknown() {
assertThat(CloseType.fromStringAndInitiator("COOPERATIVE_CLOSE", "INITIATOR_UNKNOWN"))
Expand Down