add close height to model

This commit is contained in:
Carsten Otto
2021-11-28 14:27:00 +01:00
parent 4f10dba0ac
commit df7e6639d2
14 changed files with 82 additions and 13 deletions

View File

@@ -87,6 +87,7 @@ public class GrpcClosedChannels extends GrpcChannelsBase {
.withRemotePubkey(Pubkey.create(channelCloseSummary.getRemotePubkey()))
.withCloseTransactionHash(channelCloseSummary.getClosingTxHash())
.withOpenInitiator(openInitiator)
.withCloseHeight(channelCloseSummary.getCloseHeight())
.build()
);
}

View File

@@ -25,6 +25,7 @@ import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID_SHORT;
import static de.cotto.lndmanagej.model.ChannelPointFixtures.CHANNEL_POINT;
import static de.cotto.lndmanagej.model.ChannelPointFixtures.TRANSACTION_HASH;
import static de.cotto.lndmanagej.model.ChannelPointFixtures.TRANSACTION_HASH_2;
import static de.cotto.lndmanagej.model.ClosedChannelFixtures.CLOSE_HEIGHT;
import static de.cotto.lndmanagej.model.CoopClosedChannelFixtures.CLOSED_CHANNEL;
import static de.cotto.lndmanagej.model.CoopClosedChannelFixtures.CLOSED_CHANNEL_2;
import static de.cotto.lndmanagej.model.ForceClosedChannelFixtures.FORCE_CLOSED_CHANNEL_BREACH;
@@ -202,6 +203,7 @@ class GrpcClosedChannelsTest {
.setCloseType(closeType)
.setOpenInitiator(openInitiator)
.setCloseInitiator(closeInitiator)
.setCloseHeight(CLOSE_HEIGHT)
.build();
}

View File

@@ -6,7 +6,8 @@ public class BreachForceClosedChannel extends ForceClosedChannel {
Pubkey ownPubkey,
Pubkey remotePubkey,
String closeTransactionHash,
OpenInitiator openInitiator
OpenInitiator openInitiator,
int closeHeight
) {
super(
channelCoreInformation,
@@ -14,7 +15,8 @@ public class BreachForceClosedChannel extends ForceClosedChannel {
remotePubkey,
closeTransactionHash,
openInitiator,
CloseInitiator.REMOTE
CloseInitiator.REMOTE,
closeHeight
);
}
}

View File

@@ -14,7 +14,8 @@ public class BreachForceClosedChannelBuilder extends ClosedChannelBuilder<Breach
requireNonNull(ownPubkey),
requireNonNull(remotePubkey),
requireNonNull(closeTransactionHash),
requireNonNull(openInitiator)
requireNonNull(openInitiator),
closeHeight
);
}
}

View File

@@ -6,6 +6,7 @@ import static de.cotto.lndmanagej.model.OpenCloseStatus.CLOSED;
public abstract class ClosedChannel extends ClosedOrClosingChannel {
private final CloseInitiator closeInitiator;
private final int closeHeight;
public ClosedChannel(
ChannelCoreInformation channelCoreInformation,
@@ -13,7 +14,8 @@ public abstract class ClosedChannel extends ClosedOrClosingChannel {
Pubkey remotePubkey,
String closeTransactionHash,
OpenInitiator openInitiator,
CloseInitiator closeInitiator
CloseInitiator closeInitiator,
int closeHeight
) {
super(
channelCoreInformation,
@@ -23,12 +25,20 @@ public abstract class ClosedChannel extends ClosedOrClosingChannel {
openInitiator
);
this.closeInitiator = closeInitiator;
if (closeHeight == 0) {
throw new IllegalArgumentException("Close height must be set");
}
this.closeHeight = closeHeight;
}
public CloseInitiator getCloseInitiator() {
return closeInitiator;
}
public int getCloseHeight() {
return closeHeight;
}
@Override
public ChannelStatus getStatus() {
return new ChannelStatus(isPrivateChannel(), false, true, CLOSED);
@@ -47,11 +57,11 @@ public abstract class ClosedChannel extends ClosedOrClosingChannel {
return false;
}
ClosedChannel that = (ClosedChannel) other;
return closeInitiator == that.closeInitiator;
return closeHeight == that.closeHeight && closeInitiator == that.closeInitiator;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), closeInitiator);
return Objects.hash(super.hashCode(), closeInitiator, closeHeight);
}
}

View File

@@ -29,6 +29,8 @@ public abstract class ClosedChannelBuilder<T extends ClosedChannel> {
@Nullable
CloseInitiator closeInitiator;
int closeHeight;
public ClosedChannelBuilder() {
// default constructor
}
@@ -73,6 +75,11 @@ public abstract class ClosedChannelBuilder<T extends ClosedChannel> {
return this;
}
public ClosedChannelBuilder<T> withCloseHeight(int closeHeight) {
this.closeHeight = closeHeight;
return this;
}
protected ChannelCoreInformation getChannelCoreInformation() {
return new ChannelCoreInformation(
requireNonNull(channelId),

View File

@@ -7,7 +7,8 @@ public class CoopClosedChannel extends ClosedChannel {
Pubkey remotePubkey,
String closeTransactionHash,
OpenInitiator openInitiator,
CloseInitiator closeInitiator
CloseInitiator closeInitiator,
int closeHeight
) {
super(
channelCoreInformation,
@@ -15,7 +16,8 @@ public class CoopClosedChannel extends ClosedChannel {
remotePubkey,
closeTransactionHash,
openInitiator,
closeInitiator
closeInitiator,
closeHeight
);
}
}

View File

@@ -15,7 +15,8 @@ public class CoopClosedChannelBuilder extends ClosedChannelBuilder<CoopClosedCha
requireNonNull(remotePubkey),
requireNonNull(closeTransactionHash),
requireNonNull(openInitiator),
requireNonNull(closeInitiator)
requireNonNull(closeInitiator),
closeHeight
);
}
}

View File

@@ -7,7 +7,8 @@ public class ForceClosedChannel extends ClosedChannel {
Pubkey remotePubkey,
String closeTransactionHash,
OpenInitiator openInitiator,
CloseInitiator closeInitiator
CloseInitiator closeInitiator,
int closeHeight
) {
super(
channelCoreInformation,
@@ -15,7 +16,8 @@ public class ForceClosedChannel extends ClosedChannel {
remotePubkey,
closeTransactionHash,
openInitiator,
closeInitiator
closeInitiator,
closeHeight
);
}
}

View File

@@ -15,7 +15,8 @@ public class ForceClosedChannelBuilder extends ClosedChannelBuilder<ForceClosedC
requireNonNull(remotePubkey),
requireNonNull(closeTransactionHash),
requireNonNull(openInitiator),
requireNonNull(closeInitiator)
requireNonNull(closeInitiator),
closeHeight
);
}
}

View File

@@ -7,6 +7,7 @@ import static de.cotto.lndmanagej.model.ChannelFixtures.CAPACITY;
import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID;
import static de.cotto.lndmanagej.model.ChannelPointFixtures.CHANNEL_POINT;
import static de.cotto.lndmanagej.model.ChannelPointFixtures.TRANSACTION_HASH_2;
import static de.cotto.lndmanagej.model.ClosedChannelFixtures.CLOSE_HEIGHT;
import static de.cotto.lndmanagej.model.ForceClosedChannelFixtures.FORCE_CLOSED_CHANNEL_BREACH;
import static de.cotto.lndmanagej.model.OpenCloseStatus.CLOSED;
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY;
@@ -24,6 +25,7 @@ class BreachForceClosedChannelTest {
.withRemotePubkey(PUBKEY_2)
.withCloseTransactionHash(TRANSACTION_HASH_2)
.withOpenInitiator(OpenInitiator.LOCAL)
.withCloseHeight(CLOSE_HEIGHT)
.build()
).isEqualTo(FORCE_CLOSED_CHANNEL_BREACH);
}
@@ -68,6 +70,11 @@ class BreachForceClosedChannelTest {
assertThat(FORCE_CLOSED_CHANNEL_BREACH.getCloseInitiator()).isEqualTo(CloseInitiator.REMOTE);
}
@Test
void getCloseHeight() {
assertThat(FORCE_CLOSED_CHANNEL_BREACH.getCloseHeight()).isEqualTo(987_654);
}
@Test
void getStatus() {
assertThat(FORCE_CLOSED_CHANNEL_BREACH.getStatus())

View File

@@ -6,12 +6,15 @@ import org.junit.jupiter.api.Test;
import static de.cotto.lndmanagej.model.ChannelFixtures.CAPACITY;
import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID;
import static de.cotto.lndmanagej.model.ChannelPointFixtures.CHANNEL_POINT;
import static de.cotto.lndmanagej.model.ChannelPointFixtures.TRANSACTION_HASH;
import static de.cotto.lndmanagej.model.ChannelPointFixtures.TRANSACTION_HASH_2;
import static de.cotto.lndmanagej.model.ClosedChannelFixtures.CLOSE_HEIGHT;
import static de.cotto.lndmanagej.model.CoopClosedChannelFixtures.CLOSED_CHANNEL;
import static de.cotto.lndmanagej.model.OpenCloseStatus.CLOSED;
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY;
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
class CoopClosedChannelTest {
@Test
@@ -26,11 +29,26 @@ class CoopClosedChannelTest {
.withCloseTransactionHash(TRANSACTION_HASH_2)
.withOpenInitiator(OpenInitiator.LOCAL)
.withCloseInitiator(CloseInitiator.REMOTE)
.withCloseHeight(CLOSE_HEIGHT)
.build()
).isEqualTo(CLOSED_CHANNEL);
// CPD-ON
}
@Test
void closeHeight_zero() {
ChannelCoreInformation channelCoreInformation = new ChannelCoreInformation(CHANNEL_ID, CHANNEL_POINT, CAPACITY);
assertThatIllegalArgumentException().isThrownBy(() -> new CoopClosedChannel(
channelCoreInformation,
PUBKEY,
PUBKEY_2,
TRANSACTION_HASH,
OpenInitiator.LOCAL,
CloseInitiator.LOCAL,
0
)).withMessage("Close height must be set");
}
@Test
void getId() {
assertThat(CLOSED_CHANNEL.getId()).isEqualTo(CHANNEL_ID);
@@ -81,6 +99,11 @@ class CoopClosedChannelTest {
assertThat(CLOSED_CHANNEL.getCloseInitiator()).isEqualTo(CloseInitiator.REMOTE);
}
@Test
void getCloseHeight() {
assertThat(CLOSED_CHANNEL.getCloseHeight()).isEqualTo(987_654);
}
@Test
void getStatus() {
assertThat(CLOSED_CHANNEL.getStatus())

View File

@@ -7,6 +7,7 @@ import static de.cotto.lndmanagej.model.ChannelFixtures.CAPACITY;
import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID;
import static de.cotto.lndmanagej.model.ChannelPointFixtures.CHANNEL_POINT;
import static de.cotto.lndmanagej.model.ChannelPointFixtures.TRANSACTION_HASH_2;
import static de.cotto.lndmanagej.model.ClosedChannelFixtures.CLOSE_HEIGHT;
import static de.cotto.lndmanagej.model.ForceClosedChannelFixtures.FORCE_CLOSED_CHANNEL;
import static de.cotto.lndmanagej.model.ForceClosedChannelFixtures.FORCE_CLOSED_CHANNEL_REMOTE;
import static de.cotto.lndmanagej.model.OpenCloseStatus.CLOSED;
@@ -26,6 +27,7 @@ class ForceClosedChannelTest {
.withCloseTransactionHash(TRANSACTION_HASH_2)
.withOpenInitiator(OpenInitiator.LOCAL)
.withCloseInitiator(CloseInitiator.REMOTE)
.withCloseHeight(CLOSE_HEIGHT)
.build()
).isEqualTo(FORCE_CLOSED_CHANNEL);
}
@@ -80,6 +82,11 @@ class ForceClosedChannelTest {
assertThat(FORCE_CLOSED_CHANNEL_REMOTE.getCloseInitiator()).isEqualTo(CloseInitiator.REMOTE);
}
@Test
void getCloseHeight() {
assertThat(FORCE_CLOSED_CHANNEL_REMOTE.getCloseHeight()).isEqualTo(987_654);
}
@Test
void getStatus() {
assertThat(FORCE_CLOSED_CHANNEL_REMOTE.getStatus())

View File

@@ -8,6 +8,8 @@ import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY;
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2;
public final class ClosedChannelFixtures {
public static final int CLOSE_HEIGHT = 987_654;
private ClosedChannelFixtures() {
// do not instantiate
}
@@ -20,6 +22,7 @@ public final class ClosedChannelFixtures {
.withRemotePubkey(PUBKEY_2)
.withCloseTransactionHash(TRANSACTION_HASH_2)
.withOpenInitiator(OpenInitiator.LOCAL)
.withCloseInitiator(CloseInitiator.REMOTE);
.withCloseInitiator(CloseInitiator.REMOTE)
.withCloseHeight(CLOSE_HEIGHT);
}
}