multi: add custom error messages to channel acceptor

This commit adds an optional error message to the channel acceptor's
reponse to allow operators to inform (or insult) unsuccessful channel
initiators as to the reason for their rejection.

This field is added in addition to the existing accept field to maintain
backwards compatibity. If we were to deprecate accept and interpret a
non-nil error as rejecting the channel, then received a response with
accept=false and a nil error, the server cannot tell whether this is a
legacy rejection or new mesage type acceptance (due to nil error),
so we keep both fields.
This commit is contained in:
carla
2020-11-09 09:34:50 +02:00
parent 54c3e98b40
commit 38fd7d206f
9 changed files with 1003 additions and 715 deletions

View File

@@ -0,0 +1,85 @@
package chanacceptor
import (
"errors"
"strings"
"testing"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/require"
)
// TestValidateAcceptorResponse test validation of acceptor responses.
func TestValidateAcceptorResponse(t *testing.T) {
customError := errors.New("custom error")
tests := []struct {
name string
response lnrpc.ChannelAcceptResponse
accept bool
acceptorErr error
error error
}{
{
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,
},
accept: true,
acceptorErr: nil,
error: nil,
},
{
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,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
accept, acceptErr, err := validateAcceptorResponse(
test.response,
)
require.Equal(t, test.accept, accept)
require.Equal(t, test.acceptorErr, acceptErr)
require.Equal(t, test.error, err)
})
}
}