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