Skip to content

Commit

Permalink
Fix vsize estimation of psbt (#191)
Browse files Browse the repository at this point in the history
feat(LightningService.cs): add error handling for PSBT virtual size estimation
feat(LightningService.cs): add validation for changeless channel operation request with outputs on the psbt
refactor(LightningService.cs): improve outputVirtualSize calculation by using estimatedVsize and changelessVSize
  • Loading branch information
Jossec101 committed Jun 5, 2023
1 parent 8cb34de commit ac6f3f3
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Services/LightningService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,20 @@ public async Task OpenChannel(ChannelOperationRequest channelOperationRequest)

try
{
// 8 value + 1 script pub key size + 34 script pub key hash (Segwit output 2-0f-2 multisig)
var outputVirtualSize = combinedPSBT.GetGlobalTransaction().GetVirtualSize() + 43;
if (!combinedPSBT.TryGetVirtualSize(out var estimatedVsize))
{
_logger.LogError("Could not estimate virtual size of the PSBT");
throw new InvalidOperationException("Could not estimate virtual size of the PSBT");
}

if(channelOperationRequest.Changeless && combinedPSBT.Outputs.Any())
{
_logger.LogError("Changeless channel operation request cannot have outputs at this stage");
throw new InvalidOperationException("Changeless channel operation request cannot have outputs at this stage");
}

var changelessVSize = channelOperationRequest.Changeless ? 43 : 0; // 8 value + 1 script pub key size + 34 script pub key hash (Segwit output 2-0f-2 multisig)
var outputVirtualSize = estimatedVsize + changelessVSize; // We add the change output if needed
var initialFeeRate = await LightningHelper.GetFeeRateResult(network, _nbXplorerService);

var totalFees = new Money(outputVirtualSize * initialFeeRate.FeeRate.SatoshiPerByte, MoneyUnit.Satoshi);
Expand Down

0 comments on commit ac6f3f3

Please sign in to comment.