mirror of
https://github.com/lightningnetwork/lnd.git
synced 2024-11-19 09:53:54 +01:00
5679dde1bc
Add more fields to channel acceptor response so that users can have more fine grained control over their incoming channels. With our chained acceptor, it is possible that we get inconsistent responses from multiple chained acceptors. We create a conjugate repsponse from all the set fields in our various responses, but fail if we get different, non- zero responses from our various acceptors. Separate merge functions are used per type so that we avoid unexpected outcomes comparing interfaces (panic on comparing types that aren't comparable), with casting used where applicable to avoid code duplication.
140 lines
3.3 KiB
Go
140 lines
3.3 KiB
Go
package chanacceptor
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
"github.com/btcsuite/btcutil"
|
|
"github.com/lightningnetwork/lnd/input"
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
"github.com/lightningnetwork/lnd/lnwallet/chancloser"
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestValidateAcceptorResponse test validation of acceptor responses.
|
|
func TestValidateAcceptorResponse(t *testing.T) {
|
|
var (
|
|
customError = errors.New("custom error")
|
|
validAddr = "bcrt1qwrmq9uca0t3dy9t9wtuq5tm4405r7tfzyqn9pp"
|
|
addr, _ = chancloser.ParseUpfrontShutdownAddress(
|
|
validAddr, &chaincfg.TestNet3Params,
|
|
)
|
|
)
|
|
|
|
tests := []struct {
|
|
name string
|
|
dustLimit btcutil.Amount
|
|
response lnrpc.ChannelAcceptResponse
|
|
accept bool
|
|
acceptorErr error
|
|
error error
|
|
shutdown lnwire.DeliveryAddress
|
|
}{
|
|
{
|
|
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{
|
|
Accept: true,
|
|
UpfrontShutdown: validAddr,
|
|
},
|
|
accept: true,
|
|
acceptorErr: nil,
|
|
error: nil,
|
|
shutdown: addr,
|
|
},
|
|
{
|
|
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,
|
|
},
|
|
{
|
|
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,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
// 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,
|
|
)
|
|
require.Equal(t, test.accept, accept)
|
|
require.Equal(t, test.acceptorErr, acceptErr)
|
|
require.Equal(t, test.error, err)
|
|
require.Equal(t, test.shutdown, shutdown)
|
|
})
|
|
}
|
|
}
|