multi: add confirmation target to loop in

This commit is contained in:
carla 2020-04-15 09:10:21 +02:00
parent f726fc2bc8
commit fc99c8b320
No known key found for this signature in database
GPG key ID: 4CA7FE54A6213C91
7 changed files with 258 additions and 108 deletions

View file

@ -361,9 +361,16 @@ func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
log.Infof("Loop in quote request received")
htlcConfTarget, err := validateLoopInRequest(
req.ConfTarget, req.ExternalHtlc,
)
if err != nil {
return nil, err
}
quote, err := s.impl.LoopInQuote(ctx, &loop.LoopInQuoteRequest{
Amount: btcutil.Amount(req.Amt),
HtlcConfTarget: loop.DefaultHtlcConfTarget,
HtlcConfTarget: htlcConfTarget,
ExternalHtlc: req.ExternalHtlc,
})
if err != nil {
@ -381,11 +388,18 @@ func (s *swapClientServer) LoopIn(ctx context.Context,
log.Infof("Loop in request received")
htlcConfTarget, err := validateLoopInRequest(
in.HtlcConfTarget, in.ExternalHtlc,
)
if err != nil {
return nil, err
}
req := &loop.LoopInRequest{
Amount: btcutil.Amount(in.Amt),
MaxMinerFee: btcutil.Amount(in.MaxMinerFee),
MaxSwapFee: btcutil.Amount(in.MaxSwapFee),
HtlcConfTarget: loop.DefaultHtlcConfTarget,
HtlcConfTarget: htlcConfTarget,
ExternalHtlc: in.ExternalHtlc,
}
if in.LastHop != nil {
@ -489,3 +503,22 @@ func validateConfTarget(target, defaultTarget int32) (int32, error) {
return target, nil
}
}
// validateLoopInRequest fails if the mutually exclusive conf target and
// external parameters are both set.
func validateLoopInRequest(htlcConfTarget int32, external bool) (int32, error) {
// If the htlc is going to be externally set, the htlcConfTarget should
// not be set, because it has no relevance when the htlc is external.
if external && htlcConfTarget != 0 {
return 0, errors.New("external and htlc conf target cannot " +
"both be set")
}
// If the htlc is being externally published, we do not need to set a
// confirmation target.
if external {
return 0, nil
}
return validateConfTarget(htlcConfTarget, loop.DefaultHtlcConfTarget)
}

View file

@ -1,6 +1,10 @@
package loopd
import "testing"
import (
"testing"
"github.com/lightninglabs/loop"
)
// TestValidateConfTarget tests all failure and success cases for our conf
// target validation function, including the case where we replace a zero
@ -70,3 +74,72 @@ func TestValidateConfTarget(t *testing.T) {
})
}
}
// TestValidateLoopInRequest tests validation of loop in requests.
func TestValidateLoopInRequest(t *testing.T) {
tests := []struct {
name string
external bool
confTarget int32
expectErr bool
expectedTarget int32
}{
{
name: "external and htlc conf set",
external: true,
confTarget: 1,
expectErr: true,
expectedTarget: 0,
},
{
name: "external and no conf",
external: true,
confTarget: 0,
expectErr: false,
expectedTarget: 0,
},
{
name: "not external, zero conf",
external: false,
confTarget: 0,
expectErr: false,
expectedTarget: loop.DefaultHtlcConfTarget,
},
{
name: "not external, bad conf",
external: false,
confTarget: 1,
expectErr: true,
expectedTarget: 0,
},
{
name: "not external, ok conf",
external: false,
confTarget: 5,
expectErr: false,
expectedTarget: 5,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
external := test.external
conf, err := validateLoopInRequest(
test.confTarget, external,
)
haveErr := err != nil
if haveErr != test.expectErr {
t.Fatalf("expected err: %v, got: %v",
test.expectErr, err)
}
if conf != test.expectedTarget {
t.Fatalf("expected: %v, got: %v",
test.expectedTarget, conf)
}
})
}
}