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

Fix: Bug: Eclair - Error in GUI when open channel #804 #828

Merged
merged 4 commits into from
Feb 3, 2024
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
126 changes: 114 additions & 12 deletions src/lib/lightning/eclair/eclairService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,50 @@ describe('EclairService', () => {
expect(actual).toEqual(expected);
});

it('should get a list of channels for < v0.8.0', async () => {
it('should get a list of channels for v0.7.0', async () => {
node.version = '0.7.0';
const chanResponse: ELN.ChannelResponse = {
nodeId: 'abcdef',
channelId: '65sdfd7',
state: ELN.ChannelState.NORMAL,
data: {
commitments: {
channelFlags: 1,
params: {
localParams: {
isInitiator: false,
},
channelFlags: {
announceChannel: false,
},
},
active: [
{
fundingTx: {
amountSatoshis: 0,
},
localCommit: {
spec: {
toLocal: 0,
toRemote: 0,
},
},
},
],
localParams: {
isFunder: true,
isInitiator: undefined as any,
isInitiator: false,
},
channelFlags: {
announceChannel: true,
},
localCommit: {
spec: {
toLocal: 100000000,
toRemote: 50000000,
toLocal: 250000000,
toRemote: 0,
},
},
commitInput: {
amountSatoshis: 150000,
amountSatoshis: 250000,
},
},
},
Expand All @@ -97,26 +121,104 @@ describe('EclairService', () => {
expect(actual).toEqual(expected);
});

it('should get a list of channels for >= v0.8.0', async () => {
it('should get a list of channels for v0.8.0', async () => {
node.version = '0.8.0';
const chanResponse: ELN.ChannelResponse = {
nodeId: 'abcdef',
channelId: '65sdfd7',
state: ELN.ChannelState.NORMAL,
data: {
commitments: {
channelFlags: 1,
params: {
localParams: {
isInitiator: false,
},
channelFlags: {
announceChannel: false,
},
},
active: [
{
fundingTx: {
amountSatoshis: 0,
},
localCommit: {
spec: {
toLocal: 0,
toRemote: 0,
},
},
},
],
localParams: {
isFunder: undefined as any,
isFunder: false,
isInitiator: true,
},
channelFlags: {
announceChannel: true,
},
localCommit: {
spec: {
toLocal: 250000000,
toRemote: 0,
},
},
commitInput: {
amountSatoshis: 250000,
},
},
},
};
eclairApiMock.httpPost.mockResolvedValue([chanResponse]);
const expected = [expect.objectContaining({ pubkey: 'abcdef' })];
const actual = await eclairService.getChannels(node);
expect(actual).toEqual(expected);
});

it('should get a list of channels for >= v0.9.0', async () => {
node.version = '0.9.0';
const chanResponse: ELN.ChannelResponse = {
nodeId: 'abcdef',
channelId: '65sdfd7',
state: ELN.ChannelState.NORMAL,
data: {
commitments: {
params: {
localParams: {
isInitiator: true,
},
channelFlags: {
announceChannel: true,
},
},
active: [
{
fundingTx: {
amountSatoshis: 250000,
},
localCommit: {
spec: {
toLocal: 250000000,
toRemote: 0,
},
},
},
],
localParams: {
isFunder: false,
isInitiator: false,
},
channelFlags: {
announceChannel: false,
},
localCommit: {
spec: {
toLocal: 100000000,
toRemote: 50000000,
toLocal: 0,
toRemote: 0,
},
},
commitInput: {
amountSatoshis: 150000,
amountSatoshis: 0,
},
},
},
Expand Down
33 changes: 26 additions & 7 deletions src/lib/lightning/eclair/eclairService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,42 @@ class EclairService implements LightningService {
return channels
.filter(
c =>
c.data.commitments.localParams.isFunder ||
c.data.commitments.localParams.isInitiator,
c.data.commitments?.localParams?.isFunder ||
c.data.commitments?.localParams?.isInitiator ||
c.data.commitments?.params?.localParams?.isInitiator,
)
.filter(c => ChannelStateToStatus[c.state] !== 'Closed')
.map(c => {
let capacity: string;
let localBalance: number;
let remoteBalance: number;
let isPrivate: boolean;

if ('0.8.0' === node.version || '0.7.0' === node.version) {
const { localCommit, commitInput } = c.data.commitments;
capacity = commitInput.amountSatoshis.toString(10);
localBalance = localCommit.spec.toLocal;
remoteBalance = localCommit.spec.toRemote;
isPrivate = c.data.commitments.channelFlags.announceChannel === false;
} else {
// v0.9.0+
const { fundingTx, localCommit } = c.data.commitments.active[0];
capacity = fundingTx.amountSatoshis.toString(10);
localBalance = localCommit.spec.toLocal;
remoteBalance = localCommit.spec.toRemote;
isPrivate = c.data.commitments.params.channelFlags.announceChannel === false;
}
const status = ChannelStateToStatus[c.state];
const { localCommit, commitInput } = c.data.commitments;
return {
pending: status !== 'Open',
uniqueId: c.channelId.slice(-12),
channelPoint: c.channelId,
pubkey: c.nodeId,
capacity: commitInput.amountSatoshis.toString(10),
localBalance: this.toSats(localCommit.spec.toLocal),
remoteBalance: this.toSats(localCommit.spec.toRemote),
capacity,
localBalance: this.toSats(localBalance),
remoteBalance: this.toSats(remoteBalance),
status,
isPrivate: c.data.commitments.channelFlags === 0,
isPrivate,
};
});
}
Expand Down
29 changes: 26 additions & 3 deletions src/lib/lightning/eclair/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,37 @@ type ChannelFlags = 0 | 1;
* https://acinq.github.io/eclair/#channel
*/
interface ChannelData {
// ChannelData interface has some repeated fields to be compatible with v0.9.0, 0.8.0 and 0.7.0 versions
commitments: {
params: {
localParams: {
isInitiator: boolean;
};
channelFlags: {
announceChannel: boolean;
};
};
active: [
{
fundingTx: {
amountSatoshis: number;
};
localCommit: {
spec: {
toLocal: number;
toRemote: number;
};
};
},
];
localParams: {
// The isFunder field was renamed to isInitiator in v0.8.0. We use both
// to maintain compatibility with older versions.
// The isFunder field was renamed to isInitiator in v0.8.0+
isFunder: boolean;
isInitiator: boolean;
};
channelFlags: ChannelFlags;
channelFlags: {
announceChannel: boolean;
};
localCommit: {
spec: {
toLocal: number;
Expand Down
Loading