| | | 1 | | namespace NLightning.Domain.Channels.Models; |
| | | 2 | | |
| | | 3 | | using Bitcoin.Transactions.Outputs; |
| | | 4 | | using Bitcoin.ValueObjects; |
| | | 5 | | using Crypto.ValueObjects; |
| | | 6 | | using Domain.Bitcoin.Wallet.Models; |
| | | 7 | | using Domain.Protocol.Models; |
| | | 8 | | using Enums; |
| | | 9 | | using Money; |
| | | 10 | | using ValueObjects; |
| | | 11 | | |
| | | 12 | | public class ChannelModel |
| | | 13 | | { |
| | | 14 | | #region Base Properties |
| | | 15 | | |
| | 844 | 16 | | public ChannelConfig ChannelConfig { get; private set; } |
| | 68 | 17 | | public ChannelId ChannelId { get; private set; } |
| | 0 | 18 | | public ShortChannelId ShortChannelId { get; set; } |
| | 204 | 19 | | public CommitmentNumber? CommitmentNumber { get; private set; } |
| | 0 | 20 | | public uint FundingCreatedAtBlockHeight { get; set; } |
| | 204 | 21 | | public FundingOutputInfo? FundingOutput { get; private set; } |
| | 68 | 22 | | public bool IsInitiator { get; } |
| | 0 | 23 | | public CompactPubKey RemoteNodeId { get; } |
| | 68 | 24 | | public ChannelState State { get; private set; } |
| | 0 | 25 | | public ChannelVersion Version { get; } |
| | 0 | 26 | | public WalletAddressModel? ChangeAddress { get; set; } |
| | | 27 | | |
| | | 28 | | #endregion |
| | | 29 | | |
| | | 30 | | #region Signatures |
| | 0 | 31 | | |
| | 68 | 32 | | public CompactSignature? LastSentSignature { get; private set; } |
| | 68 | 33 | | public CompactSignature? LastReceivedSignature { get; private set; } |
| | | 34 | | |
| | | 35 | | #endregion |
| | | 36 | | |
| | | 37 | | #region Local Information |
| | 0 | 38 | | |
| | 0 | 39 | | public ICollection<ShortChannelId>? LocalAliases { get; set; } |
| | 68 | 40 | | public LightningMoney LocalBalance { get; } |
| | 204 | 41 | | public ChannelKeySetModel LocalKeySet { get; } |
| | 0 | 42 | | public ulong LocalNextHtlcId { get; } |
| | 68 | 43 | | public ICollection<Htlc>? LocalOfferedHtlcs { get; } |
| | 0 | 44 | | public ICollection<Htlc>? LocalFulfilledHtlcs { get; } |
| | 0 | 45 | | public ICollection<Htlc>? LocalOldHtlcs { get; } |
| | 0 | 46 | | public ulong LocalRevocationNumber { get; } |
| | 0 | 47 | | public BitcoinScript? LocalUpfrontShutdownScript { get; } |
| | | 48 | | |
| | | 49 | | #endregion |
| | | 50 | | |
| | | 51 | | #region Remote Information |
| | 0 | 52 | | |
| | 0 | 53 | | public ShortChannelId? RemoteAlias { get; set; } |
| | 68 | 54 | | public LightningMoney RemoteBalance { get; } |
| | 544 | 55 | | public ChannelKeySetModel? RemoteKeySet { get; private set; } |
| | 0 | 56 | | public ulong RemoteNextHtlcId { get; } |
| | 0 | 57 | | public ulong RemoteRevocationNumber { get; } |
| | 0 | 58 | | public ICollection<Htlc>? RemoteFulfilledHtlcs { get; } |
| | 68 | 59 | | public ICollection<Htlc>? RemoteOfferedHtlcs { get; } |
| | 0 | 60 | | public ICollection<Htlc>? RemoteOldHtlcs { get; } |
| | 0 | 61 | | public BitcoinScript? RemoteUpfrontShutdownScript { get; } |
| | | 62 | | |
| | | 63 | | #endregion |
| | 0 | 64 | | |
| | 68 | 65 | | public ChannelModel(ChannelConfig channelConfig, ChannelId channelId, CommitmentNumber? commitmentNumber, |
| | 68 | 66 | | FundingOutputInfo? fundingOutput, bool isInitiator, CompactSignature? lastSentSignature, |
| | 68 | 67 | | CompactSignature? lastReceivedSignature, LightningMoney localBalance, |
| | 68 | 68 | | ChannelKeySetModel localKeySet, ulong localNextHtlcId, ulong localRevocationNumber, |
| | 68 | 69 | | LightningMoney remoteBalance, ChannelKeySetModel? remoteKeySet, ulong remoteNextHtlcId, |
| | 68 | 70 | | CompactPubKey remoteNodeId, ulong remoteRevocationNumber, ChannelState state, |
| | 68 | 71 | | ChannelVersion version, ICollection<Htlc>? localOfferedHtlcs = null, |
| | 68 | 72 | | ICollection<Htlc>? localFulfilledHtlcs = null, ICollection<Htlc>? localOldHtlcs = null, |
| | 68 | 73 | | BitcoinScript? localUpfrontShutdownScript = null, ICollection<Htlc>? remoteOfferedHtlcs = null, |
| | 68 | 74 | | ICollection<Htlc>? remoteFulfilledHtlcs = null, ICollection<Htlc>? remoteOldHtlcs = null, |
| | 68 | 75 | | BitcoinScript? remoteUpfrontShutdownScript = null) |
| | 0 | 76 | | { |
| | 68 | 77 | | ChannelConfig = channelConfig; |
| | 68 | 78 | | ChannelId = channelId; |
| | 68 | 79 | | CommitmentNumber = commitmentNumber; |
| | 68 | 80 | | FundingOutput = fundingOutput; |
| | 68 | 81 | | IsInitiator = isInitiator; |
| | 68 | 82 | | LastSentSignature = lastSentSignature; |
| | 68 | 83 | | LastReceivedSignature = lastReceivedSignature; |
| | 68 | 84 | | LocalBalance = localBalance; |
| | 68 | 85 | | LocalKeySet = localKeySet; |
| | 68 | 86 | | LocalNextHtlcId = localNextHtlcId; |
| | 68 | 87 | | LocalRevocationNumber = localRevocationNumber; |
| | 68 | 88 | | RemoteBalance = remoteBalance; |
| | 68 | 89 | | RemoteKeySet = remoteKeySet; |
| | 68 | 90 | | RemoteNextHtlcId = remoteNextHtlcId; |
| | 68 | 91 | | RemoteRevocationNumber = remoteRevocationNumber; |
| | 68 | 92 | | State = state; |
| | 68 | 93 | | Version = version; |
| | 68 | 94 | | RemoteNodeId = remoteNodeId; |
| | 68 | 95 | | LocalOfferedHtlcs = localOfferedHtlcs ?? new List<Htlc>(); |
| | 68 | 96 | | LocalFulfilledHtlcs = localFulfilledHtlcs ?? new List<Htlc>(); |
| | 68 | 97 | | LocalOldHtlcs = localOldHtlcs ?? new List<Htlc>(); |
| | 68 | 98 | | RemoteOfferedHtlcs = remoteOfferedHtlcs ?? new List<Htlc>(); |
| | 68 | 99 | | RemoteFulfilledHtlcs = remoteFulfilledHtlcs ?? new List<Htlc>(); |
| | 68 | 100 | | RemoteOldHtlcs = remoteOldHtlcs ?? new List<Htlc>(); |
| | 68 | 101 | | LocalUpfrontShutdownScript = localUpfrontShutdownScript; |
| | 68 | 102 | | RemoteUpfrontShutdownScript = remoteUpfrontShutdownScript; |
| | 68 | 103 | | } |
| | | 104 | | |
| | | 105 | | public void UpdateState(ChannelState newState) |
| | 0 | 106 | | { |
| | 0 | 107 | | if (State == ChannelState.V2Opening && newState < ChannelState.V2Opening |
| | 0 | 108 | | || State >= ChannelState.V1Opening && newState == ChannelState.V2Opening) |
| | 0 | 109 | | throw new ArgumentOutOfRangeException(nameof(newState), "Invalid channel state for update."); |
| | 0 | 110 | | |
| | 0 | 111 | | if (newState <= State) |
| | 0 | 112 | | throw new ArgumentOutOfRangeException(nameof(newState), "New state must be greater than current state."); |
| | 0 | 113 | | |
| | 0 | 114 | | State = newState; |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | public void UpdateChannelId(ChannelId newChannelId) |
| | 0 | 118 | | { |
| | 0 | 119 | | if (newChannelId == ChannelId.Zero) |
| | 0 | 120 | | throw new ArgumentException("New channel ID cannot be empty.", nameof(newChannelId)); |
| | 0 | 121 | | |
| | 0 | 122 | | ChannelId = newChannelId; |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | public void UpdateChannelConfig(ChannelConfig channelConfig) |
| | 0 | 126 | | { |
| | 0 | 127 | | ChannelConfig = channelConfig; |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | public void AddRemoteKeySet(ChannelKeySetModel remoteKeySet) |
| | | 131 | | { |
| | 0 | 132 | | if (RemoteKeySet is not null) |
| | 0 | 133 | | throw new InvalidOperationException("Remote key set already set"); |
| | | 134 | | |
| | 0 | 135 | | RemoteKeySet = remoteKeySet; |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | public void AddCommitmentNumber(CommitmentNumber commitmentNumber) |
| | | 139 | | { |
| | 0 | 140 | | if (CommitmentNumber is not null) |
| | 0 | 141 | | throw new InvalidOperationException("Commitment number already set"); |
| | | 142 | | |
| | 0 | 143 | | CommitmentNumber = commitmentNumber; |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | public void AddFundingOutput(FundingOutputInfo fundingOutput) |
| | | 147 | | { |
| | 0 | 148 | | if (FundingOutput is not null) |
| | 0 | 149 | | throw new InvalidOperationException("Funding output already set"); |
| | | 150 | | |
| | 0 | 151 | | FundingOutput = fundingOutput; |
| | 0 | 152 | | } |
| | | 153 | | |
| | | 154 | | public void UpdateLastSentSignature(CompactSignature lastSentSignature) |
| | | 155 | | { |
| | 0 | 156 | | LastSentSignature = lastSentSignature; |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | public void UpdateLastReceivedSignature(CompactSignature lastReceivedSignature) |
| | | 160 | | { |
| | 0 | 161 | | LastReceivedSignature = lastReceivedSignature; |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | public ChannelSigningInfo GetSigningInfo() |
| | | 165 | | { |
| | 0 | 166 | | return new ChannelSigningInfo(FundingOutput!.TransactionId!.Value, FundingOutput.Index!.Value, |
| | 0 | 167 | | FundingOutput.Amount, LocalKeySet.FundingCompactPubKey, |
| | 0 | 168 | | RemoteKeySet!.FundingCompactPubKey, LocalKeySet.KeyIndex); |
| | | 169 | | } |
| | | 170 | | } |