Skip to content

Commit bab648f

Browse files
committed
tapchannel: assert proof courier connection
To make sure the universe proof courier address configured isn't only formally valid but can also be connected to, we do a quick connection check before requesting or accepting a channel funding action.
1 parent 95577c0 commit bab648f

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

tapchannel/aux_funding_controller.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ const (
5353
// level ACK from the remote party before timing out.
5454
ackTimeout = time.Second * 30
5555

56+
// proofCourierCheckTimeout is the amount of time we'll wait before we
57+
// time out an attempt to connect to a proof courier when checking the
58+
// configured address.
59+
proofCourierCheckTimeout = time.Second * 5
60+
5661
// maxNumAssetIDs is the maximum number of fungible asset pieces (asset
5762
// IDs) that can be committed to a single channel. The number needs to
5863
// be limited to prevent the number of required HTLC signatures to be
@@ -1373,7 +1378,7 @@ func (f *FundingController) processFundingMsg(ctx context.Context,
13731378
// We can only support asset channels if we have the correct proof
13741379
// courier type configured, so we're ready to receive the channel funds
13751380
// once the channel is (force) closed.
1376-
if err := f.validateLocalProofCourier(); err != nil {
1381+
if err := f.validateLocalProofCourier(ctx); err != nil {
13771382
return tempPID, fmt.Errorf("unable to accept channel funding "+
13781383
"request, local proof courier is invalid: %w", err)
13791384
}
@@ -1542,7 +1547,7 @@ func (f *FundingController) processFundingReq(fundingFlows fundingFlowIndex,
15421547

15431548
// We need to make sure we're ready to receive the channel funds once
15441549
// the channel is (force) closed.
1545-
if err := f.validateLocalProofCourier(); err != nil {
1550+
if err := f.validateLocalProofCourier(fundReq.ctx); err != nil {
15461551
return fmt.Errorf("unable to fund channel, local proof "+
15471552
"courier is invalid: %w", err)
15481553
}
@@ -2070,7 +2075,9 @@ func (f *FundingController) validateWitness(outAsset asset.Asset,
20702075
// universe based proof courier configured. A hashmail based courier can't deal
20712076
// with the OP_TRUE funding output script key, as that's the same for asset
20722077
// channels out there. So the single mailbox would always be occupied.
2073-
func (f *FundingController) validateLocalProofCourier() error {
2078+
func (f *FundingController) validateLocalProofCourier(
2079+
ctx context.Context) error {
2080+
20742081
courierURL := f.cfg.DefaultCourierAddr
20752082

20762083
flagHelp := "please set a universe based (universerpc://) proof " +
@@ -2090,7 +2097,9 @@ func (f *FundingController) validateLocalProofCourier() error {
20902097
courierURL.Scheme, flagHelp)
20912098
}
20922099

2093-
return nil
2100+
return proof.CheckUniverseRpcCourierConnection(
2101+
ctx, proofCourierCheckTimeout, courierURL,
2102+
)
20942103
}
20952104

20962105
// FundReq is a message that's sent to the funding controller to request a new

tapchannel/aux_funding_controller_test.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
package tapchannel
22

33
import (
4+
"context"
45
"net/url"
56
"testing"
67

8+
"github.com/lightninglabs/taproot-assets/internal/test"
79
"github.com/lightninglabs/taproot-assets/proof"
10+
"github.com/lightninglabs/taproot-assets/taprpc/universerpc"
811
"github.com/stretchr/testify/require"
12+
"google.golang.org/grpc"
13+
"google.golang.org/grpc/credentials/insecure"
914
)
1015

1116
// TestValidateLocalProofCourier tests that the local proof courier is
1217
// validated correctly.
1318
func TestValidateLocalProofCourier(t *testing.T) {
19+
serverOpts := []grpc.ServerOption{
20+
grpc.Creds(insecure.NewCredentials()),
21+
}
22+
grpcServer := grpc.NewServer(serverOpts...)
23+
24+
server := proof.MockUniverseServer{}
25+
universerpc.RegisterUniverseServer(grpcServer, &server)
26+
27+
mockServerAddr, cleanup, err := test.StartMockGRPCServer(
28+
t, grpcServer, true,
29+
)
30+
require.NoError(t, err)
31+
t.Cleanup(cleanup)
32+
1433
tests := []struct {
1534
name string
1635
courierAddr *url.URL
@@ -19,13 +38,13 @@ func TestValidateLocalProofCourier(t *testing.T) {
1938
{
2039
name: "valid universe rpc courier",
2140
courierAddr: proof.MockCourierURL(
22-
t, proof.UniverseRpcCourierType, ":1234",
41+
t, proof.UniverseRpcCourierType, mockServerAddr,
2342
),
2443
},
2544
{
2645
name: "invalid courier type",
2746
courierAddr: proof.MockCourierURL(
28-
t, proof.HashmailCourierType, ":1234",
47+
t, proof.HashmailCourierType, mockServerAddr,
2948
),
3049
expectErr: "unsupported proof courier type " +
3150
"'hashmail'",
@@ -49,7 +68,16 @@ func TestValidateLocalProofCourier(t *testing.T) {
4968
},
5069
}
5170

52-
err := fc.validateLocalProofCourier()
71+
// We use a short timeout here, since we don't want to
72+
// wait for the full default timeout of the funding
73+
// controller
74+
ctxb := context.Background()
75+
ctxb, cancel := context.WithTimeout(
76+
ctxb, test.StartupWaitTime*2,
77+
)
78+
defer cancel()
79+
80+
err := fc.validateLocalProofCourier(ctxb)
5381
if tt.expectErr != "" {
5482
require.ErrorContains(t, err, tt.expectErr)
5583

0 commit comments

Comments
 (0)