2020-11-09 10:34:50 +03:00
|
|
|
package chanacceptor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-11-09 10:34:52 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2020-11-09 10:34:50 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2020-11-09 10:34:52 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chancloser"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2020-11-09 10:34:50 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestValidateAcceptorResponse test validation of acceptor responses.
|
|
|
|
func TestValidateAcceptorResponse(t *testing.T) {
|
2020-11-09 10:34:52 +03:00
|
|
|
var (
|
|
|
|
customError = errors.New("custom error")
|
|
|
|
validAddr = "bcrt1qwrmq9uca0t3dy9t9wtuq5tm4405r7tfzyqn9pp"
|
|
|
|
addr, _ = chancloser.ParseUpfrontShutdownAddress(
|
|
|
|
validAddr, &chaincfg.TestNet3Params,
|
|
|
|
)
|
|
|
|
)
|
2020-11-09 10:34:50 +03:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2020-11-09 10:34:52 +03:00
|
|
|
dustLimit btcutil.Amount
|
2020-11-09 10:34:50 +03:00
|
|
|
response lnrpc.ChannelAcceptResponse
|
|
|
|
accept bool
|
|
|
|
acceptorErr error
|
|
|
|
error error
|
2020-11-09 10:34:52 +03:00
|
|
|
shutdown lnwire.DeliveryAddress
|
2020-11-09 10:34:50 +03:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "accepted with error",
|
|
|
|
response: lnrpc.ChannelAcceptResponse{
|
|
|
|
Accept: true,
|
|
|
|
Error: customError.Error(),
|
|
|
|
},
|
|
|
|
accept: false,
|
|
|
|
acceptorErr: errChannelRejected,
|
|
|
|
error: errAcceptWithError,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "custom error too long",
|
|
|
|
response: lnrpc.ChannelAcceptResponse{
|
|
|
|
Accept: false,
|
|
|
|
Error: strings.Repeat(" ", maxErrorLength+1),
|
|
|
|
},
|
|
|
|
accept: false,
|
|
|
|
acceptorErr: errChannelRejected,
|
|
|
|
error: errCustomLength,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "accepted",
|
|
|
|
response: lnrpc.ChannelAcceptResponse{
|
2020-11-09 10:34:52 +03:00
|
|
|
Accept: true,
|
|
|
|
UpfrontShutdown: validAddr,
|
2020-11-09 10:34:50 +03:00
|
|
|
},
|
|
|
|
accept: true,
|
|
|
|
acceptorErr: nil,
|
|
|
|
error: nil,
|
2020-11-09 10:34:52 +03:00
|
|
|
shutdown: addr,
|
2020-11-09 10:34:50 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rejected with error",
|
|
|
|
response: lnrpc.ChannelAcceptResponse{
|
|
|
|
Accept: false,
|
|
|
|
Error: customError.Error(),
|
|
|
|
},
|
|
|
|
accept: false,
|
|
|
|
acceptorErr: customError,
|
|
|
|
error: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "rejected with no error",
|
|
|
|
response: lnrpc.ChannelAcceptResponse{
|
|
|
|
Accept: false,
|
|
|
|
},
|
|
|
|
accept: false,
|
|
|
|
acceptorErr: errChannelRejected,
|
|
|
|
error: nil,
|
|
|
|
},
|
2020-11-09 10:34:52 +03:00
|
|
|
{
|
|
|
|
name: "invalid upfront shutdown",
|
|
|
|
response: lnrpc.ChannelAcceptResponse{
|
|
|
|
Accept: true,
|
|
|
|
UpfrontShutdown: "invalid addr",
|
|
|
|
},
|
|
|
|
accept: false,
|
|
|
|
acceptorErr: errChannelRejected,
|
|
|
|
error: errInvalidUpfrontShutdown,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reserve too low",
|
|
|
|
dustLimit: 100,
|
|
|
|
response: lnrpc.ChannelAcceptResponse{
|
|
|
|
Accept: true,
|
|
|
|
ReserveSat: 10,
|
|
|
|
},
|
|
|
|
accept: false,
|
|
|
|
acceptorErr: errChannelRejected,
|
|
|
|
error: errInsufficientReserve,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "max htlcs too high",
|
|
|
|
dustLimit: 100,
|
|
|
|
response: lnrpc.ChannelAcceptResponse{
|
|
|
|
Accept: true,
|
|
|
|
MaxHtlcCount: 1 + input.MaxHTLCNumber/2,
|
|
|
|
},
|
|
|
|
accept: false,
|
|
|
|
acceptorErr: errChannelRejected,
|
|
|
|
error: errMaxHtlcTooHigh,
|
|
|
|
},
|
2020-11-09 10:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2020-11-09 10:34:52 +03:00
|
|
|
// Create an acceptor, everything can be nil because
|
|
|
|
// we just need the params.
|
|
|
|
acceptor := NewRPCAcceptor(
|
|
|
|
nil, nil, 0, &chaincfg.TestNet3Params, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
accept, acceptErr, shutdown, err := acceptor.validateAcceptorResponse(
|
|
|
|
test.dustLimit, test.response,
|
2020-11-09 10:34:50 +03:00
|
|
|
)
|
|
|
|
require.Equal(t, test.accept, accept)
|
|
|
|
require.Equal(t, test.acceptorErr, acceptErr)
|
|
|
|
require.Equal(t, test.error, err)
|
2020-11-09 10:34:52 +03:00
|
|
|
require.Equal(t, test.shutdown, shutdown)
|
2020-11-09 10:34:50 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|