Check the amounts when using RegisterPayments

This commit is contained in:
Yaacov Akiba Slama
2020-08-31 16:03:39 +03:00
parent 630c6c3803
commit 5e4f1a1aeb
2 changed files with 26 additions and 9 deletions

View File

@@ -24,6 +24,17 @@ import (
sphinx "github.com/lightningnetwork/lightning-onion"
)
func checkPayment(incomingAmountMsat, outgoingAmountMsat int64) error {
var fees int64 = 0
if incomingAmountMsat > channelFeeStartAmount {
fees += (incomingAmountMsat - channelFeeStartAmount) * channelFeeAmountNumerator / channelFeeAmountDenominator
}
if incomingAmountMsat-outgoingAmountMsat < fees {
return fmt.Errorf("not enough fees")
}
return nil
}
func openChannel(ctx context.Context, client lnrpc.LightningClient, paymentHash, destination []byte, incomingAmountMsat int64) ([]byte, uint32, error) {
capacity := incomingAmountMsat/1000 + channelFeeStartAmount
channelPoint, err := client.OpenChannelSync(ctx, &lnrpc.OpenChannelRequest{

View File

@@ -36,7 +36,8 @@ const (
feeRate = 0.000001
timeLockDelta = 144
channelFeeStartAmount = 100_000
channelFeeAmount = 0.001
channelFeeAmountNumerator = 1
channelFeeAmountDenominator = 1000
)
type server struct{}
@@ -63,7 +64,7 @@ func (s *server) ChannelInformation(ctx context.Context, in *lspdrpc.ChannelInfo
FeeRate: feeRate,
TimeLockDelta: timeLockDelta,
ChannelFeeStartAmount: channelFeeStartAmount,
ChannelFeeRate: channelFeeAmount,
ChannelFeeRate: 1.0 * channelFeeAmountNumerator / channelFeeAmountDenominator,
LspPubkey: publicKey.SerializeCompressed(),
}, nil
}
@@ -82,6 +83,11 @@ func (s *server) RegisterPayment(ctx context.Context, in *lspdrpc.RegisterPaymen
}
log.Printf("RegisterPayment - Destination: %x, pi.PaymentHash: %x, pi.PaymentSecret: %x, pi.IncomingAmountMsat: %v, pi.OutgoingAmountMsat: %v",
pi.Destination, pi.PaymentHash, pi.PaymentSecret, pi.IncomingAmountMsat, pi.OutgoingAmountMsat)
err = checkPayment(pi.IncomingAmountMsat, pi.OutgoingAmountMsat)
if err != nil {
log.Printf("checkPayment(%v, %v) error: %v", pi.IncomingAmountMsat, pi.OutgoingAmountMsat, err)
return nil, fmt.Errorf("checkPayment(%v, %v) error: %v", pi.IncomingAmountMsat, pi.OutgoingAmountMsat, err)
}
err = registerPayment(pi.Destination, pi.PaymentHash, pi.PaymentSecret, pi.IncomingAmountMsat, pi.OutgoingAmountMsat)
if err != nil {
log.Printf("RegisterPayment() error: %v", err)