Skip to content

Commit 8af3cd3

Browse files
committed
multi: use proof courier dispatcher
This commit switches the custodian and porter over to using the new proof courier dispatcher. This then allows us to delete a bunch of now unused code related to proof courier addresses.
1 parent 7e03a48 commit 8af3cd3

File tree

8 files changed

+235
-253
lines changed

8 files changed

+235
-253
lines changed

proof/courier.go

Lines changed: 19 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func (u *URLDispatch) NewCourier(addr *url.URL,
167167
backoffHandle: backoffHandler,
168168
transfer: u.cfg.TransferLog,
169169
subscribers: subscribers,
170+
rawConn: conn,
170171
}, nil
171172

172173
default:
@@ -179,187 +180,40 @@ func (u *URLDispatch) NewCourier(addr *url.URL,
179180
// CourierDispatch interface.
180181
var _ CourierDispatch = (*URLDispatch)(nil)
181182

182-
// CourierAddr is a fully validated courier address (including protocol specific
183-
// validation).
184-
type CourierAddr interface {
185-
// Url returns the url.URL representation of the courier address.
186-
Url() *url.URL
187-
188-
// NewCourier generates a new courier service handle.
189-
NewCourier(ctx context.Context, cfg *CourierCfg,
190-
recipient Recipient) (Courier, error)
191-
}
192-
193-
// ParseCourierAddrString parses a proof courier address string and returns a
194-
// protocol specific courier address instance.
195-
func ParseCourierAddrString(addr string) (CourierAddr, error) {
196-
// Parse URI.
183+
// ParseCourierAddress attempts to parse the given string as a proof courier
184+
// address, validates that all required fields are present and ensures the
185+
// protocol is one of the supported protocols.
186+
func ParseCourierAddress(addr string) (*url.URL, error) {
197187
urlAddr, err := url.ParseRequestURI(addr)
198188
if err != nil {
199189
return nil, fmt.Errorf("invalid proof courier URI address: %w",
200190
err)
201191
}
202192

203-
return ParseCourierAddrUrl(*urlAddr)
204-
}
205-
206-
// ParseCourierAddrUrl parses a proof courier address url.URL and returns a
207-
// protocol specific courier address instance.
208-
func ParseCourierAddrUrl(addr url.URL) (CourierAddr, error) {
209-
// Create new courier addr based on URL scheme.
210-
switch addr.Scheme {
211-
case HashmailCourierType:
212-
return NewHashMailCourierAddr(addr)
213-
case UniverseRpcCourierType:
214-
return NewUniverseRpcCourierAddr(addr)
215-
}
216-
217-
return nil, fmt.Errorf("unknown courier address protocol "+
218-
"(consider updating tapd): %v", addr.Scheme)
219-
}
220-
221-
// HashMailCourierAddr is a hashmail protocol specific implementation of the
222-
// CourierAddr interface.
223-
type HashMailCourierAddr struct {
224-
addr url.URL
225-
}
226-
227-
// Url returns the url.URL representation of the hashmail courier address.
228-
func (h *HashMailCourierAddr) Url() *url.URL {
229-
return &h.addr
230-
}
231-
232-
// NewCourier generates a new courier service handle.
233-
func (h *HashMailCourierAddr) NewCourier(_ context.Context, cfg *CourierCfg,
234-
recipient Recipient) (Courier, error) {
235-
236-
backoffHandle := NewBackoffHandler(
237-
cfg.HashMailCfg.BackoffCfg, cfg.TransferLog,
238-
)
239-
240-
hashMailCfg := HashMailCourierCfg{
241-
ReceiverAckTimeout: cfg.HashMailCfg.ReceiverAckTimeout,
242-
}
243-
244-
hashMailBox, err := NewHashMailBox(&h.addr)
245-
if err != nil {
246-
return nil, fmt.Errorf("unable to make mailbox: %v",
247-
err)
248-
}
249-
250-
subscribers := make(
251-
map[uint64]*fn.EventReceiver[fn.Event],
252-
)
253-
return &HashMailCourier{
254-
cfg: &hashMailCfg,
255-
backoffHandle: backoffHandle,
256-
recipient: recipient,
257-
mailbox: hashMailBox,
258-
subscribers: subscribers,
259-
}, nil
260-
}
261-
262-
// NewHashMailCourierAddr generates a new hashmail courier address from a given
263-
// URL. This function also performs hashmail protocol specific address
264-
// validation.
265-
func NewHashMailCourierAddr(addr url.URL) (*HashMailCourierAddr, error) {
266-
if addr.Scheme != HashmailCourierType {
267-
return nil, fmt.Errorf("expected hashmail courier protocol: %v",
268-
addr.Scheme)
269-
}
270-
271-
// We expect the port number to be specified for a hashmail service.
272-
if addr.Port() == "" {
273-
return nil, fmt.Errorf("hashmail proof courier URI address " +
274-
"port unspecified")
275-
}
276-
277-
return &HashMailCourierAddr{
278-
addr,
279-
}, nil
280-
}
281-
282-
// UniverseRpcCourierAddr is a universe RPC protocol specific implementation of
283-
// the CourierAddr interface.
284-
type UniverseRpcCourierAddr struct {
285-
addr url.URL
286-
}
287-
288-
// Url returns the url.URL representation of the courier address.
289-
func (h *UniverseRpcCourierAddr) Url() *url.URL {
290-
return &h.addr
291-
}
292-
293-
// NewCourier generates a new courier service handle.
294-
func (h *UniverseRpcCourierAddr) NewCourier(_ context.Context,
295-
cfg *CourierCfg, recipient Recipient) (Courier, error) {
296-
297-
backoffHandle := NewBackoffHandler(
298-
cfg.UniverseRpcCfg.BackoffCfg, cfg.TransferLog,
299-
)
300-
301-
// Ensure that the courier address is a universe RPC address.
302-
if h.addr.Scheme != UniverseRpcCourierType {
303-
return nil, fmt.Errorf("unsupported courier protocol: %v",
304-
h.addr.Scheme)
305-
}
306-
307-
// Connect to the universe RPC server.
308-
dialOpts, err := serverDialOpts()
309-
if err != nil {
193+
if err := ValidateCourierAddress(urlAddr); err != nil {
310194
return nil, err
311195
}
312196

313-
serverAddr := fmt.Sprintf(
314-
"%s:%s", h.addr.Hostname(), h.addr.Port(),
315-
)
316-
conn, err := grpc.Dial(serverAddr, dialOpts...)
317-
if err != nil {
318-
return nil, err
319-
}
320-
321-
client := unirpc.NewUniverseClient(conn)
322-
323-
// Instantiate the events subscribers map.
324-
subscribers := make(
325-
map[uint64]*fn.EventReceiver[fn.Event],
326-
)
327-
328-
return &UniverseRpcCourier{
329-
recipient: recipient,
330-
client: client,
331-
backoffHandle: backoffHandle,
332-
transfer: cfg.TransferLog,
333-
subscribers: subscribers,
334-
rawConn: conn,
335-
}, nil
197+
return urlAddr, nil
336198
}
337199

338-
// NewUniverseRpcCourierAddr generates a new universe RPC courier address from a
339-
// given URL. This function also performs protocol specific address validation.
340-
func NewUniverseRpcCourierAddr(addr url.URL) (*UniverseRpcCourierAddr, error) {
200+
// ValidateCourierAddress validates that all required fields are present and
201+
// ensures the protocol is one of the supported protocols.
202+
func ValidateCourierAddress(addr *url.URL) error {
341203
// We expect the port number to be specified.
342204
if addr.Port() == "" {
343-
return nil, fmt.Errorf("proof courier URI address port " +
344-
"unspecified")
205+
return fmt.Errorf("proof courier URI address port unspecified")
345206
}
346207

347-
return &UniverseRpcCourierAddr{
348-
addr,
349-
}, nil
350-
}
351-
352-
// NewCourier instantiates a new courier service handle given a service URL
353-
// address.
354-
func NewCourier(ctx context.Context, addr url.URL, cfg *CourierCfg,
355-
recipient Recipient) (Courier, error) {
208+
switch addr.Scheme {
209+
case HashmailCourierType, UniverseRpcCourierType:
210+
// Valid and known courier address protocol.
211+
return nil
356212

357-
courierAddr, err := ParseCourierAddrUrl(addr)
358-
if err != nil {
359-
return nil, err
213+
default:
214+
return fmt.Errorf("unknown courier address protocol "+
215+
"(consider updating tapd): %v", addr.Scheme)
360216
}
361-
362-
return courierAddr.NewCourier(ctx, cfg, recipient)
363217
}
364218

365219
// ProofMailbox represents an abstract store-and-forward mailbox that can be
@@ -564,7 +418,7 @@ func (h *HashMailBox) RecvAck(ctx context.Context, sid streamID) error {
564418
return fmt.Errorf("expected ack, got %x", msg.Msg)
565419
}
566420

567-
// CleanUp atempts to tear down the mailbox as specified by the passed sid.
421+
// CleanUp attempts to tear down the mailbox as specified by the passed sid.
568422
func (h *HashMailBox) CleanUp(ctx context.Context, sid streamID) error {
569423
streamAuth := &hashmailrpc.CipherBoxAuth{
570424
Desc: &hashmailrpc.CipherBoxDesc{

proof/mock.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/hex"
77
"io"
8+
"net/url"
89
"sync"
910
"testing"
1011
"time"
@@ -76,6 +77,20 @@ func MockGroupAnchorVerifier(gen *asset.Genesis,
7677
return nil
7778
}
7879

80+
// MockProofCourierDispatcher is a mock proof courier dispatcher which returns
81+
// the same courier for all requests.
82+
type MockProofCourierDispatcher struct {
83+
Courier Courier
84+
}
85+
86+
// NewCourier instantiates a new courier service handle given a service
87+
// URL address.
88+
func (m *MockProofCourierDispatcher) NewCourier(*url.URL, Recipient) (Courier,
89+
error) {
90+
91+
return m.Courier, nil
92+
}
93+
7994
// MockProofCourier is a mock proof courier which stores the last proof it
8095
// received.
8196
type MockProofCourier struct {

rpcserver.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,19 +1054,14 @@ func (r *rpcServer) NewAddr(ctx context.Context,
10541054
// the default specified in the config.
10551055
courierAddr := r.cfg.DefaultProofCourierAddr
10561056
if req.ProofCourierAddr != "" {
1057-
addr, err := proof.ParseCourierAddrString(
1057+
var err error
1058+
courierAddr, err = proof.ParseCourierAddress(
10581059
req.ProofCourierAddr,
10591060
)
10601061
if err != nil {
10611062
return nil, fmt.Errorf("invalid proof courier "+
10621063
"address: %w", err)
10631064
}
1064-
1065-
// At this point, we do not intend on creating a proof courier
1066-
// service instance. We are only interested in parsing and
1067-
// validating the address. We therefore convert the address into
1068-
// an url.URL type for storage in the address book.
1069-
courierAddr = addr.Url()
10701065
}
10711066

10721067
// Check that the proof courier address is set. This should never
@@ -1075,7 +1070,6 @@ func (r *rpcServer) NewAddr(ctx context.Context,
10751070
if courierAddr == nil {
10761071
return nil, fmt.Errorf("no proof courier address provided")
10771072
}
1078-
proofCourierAddr := *courierAddr
10791073

10801074
if len(req.AssetId) != 32 {
10811075
return nil, fmt.Errorf("invalid asset id length")
@@ -1112,8 +1106,7 @@ func (r *rpcServer) NewAddr(ctx context.Context,
11121106
// Now that we have all the params, we'll try to add a new
11131107
// address to the addr book.
11141108
addr, err = r.cfg.AddrBook.NewAddress(
1115-
ctx, assetID, req.Amt, tapscriptSibling,
1116-
proofCourierAddr,
1109+
ctx, assetID, req.Amt, tapscriptSibling, *courierAddr,
11171110
address.WithAssetVersion(assetVersion),
11181111
)
11191112
if err != nil {
@@ -1154,7 +1147,7 @@ func (r *rpcServer) NewAddr(ctx context.Context,
11541147
// address to the addr book.
11551148
addr, err = r.cfg.AddrBook.NewAddressWithKeys(
11561149
ctx, assetID, req.Amt, *scriptKey, internalKey,
1157-
tapscriptSibling, proofCourierAddr,
1150+
tapscriptSibling, *courierAddr,
11581151
address.WithAssetVersion(assetVersion),
11591152
)
11601153
if err != nil {

tapcfg/server.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
212212
fallbackHashmailCourierAddr := fmt.Sprintf(
213213
"%s://%s", proof.HashmailCourierType, fallbackHashMailAddr,
214214
)
215-
proofCourierAddr, err := proof.ParseCourierAddrString(
215+
proofCourierAddr, err := proof.ParseCourierAddress(
216216
fallbackHashmailCourierAddr,
217217
)
218218
if err != nil {
@@ -222,7 +222,7 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
222222

223223
// If default proof courier address is set, use it as the default.
224224
if cfg.DefaultProofCourierAddr != "" {
225-
proofCourierAddr, err = proof.ParseCourierAddrString(
225+
proofCourierAddr, err = proof.ParseCourierAddress(
226226
cfg.DefaultProofCourierAddr,
227227
)
228228
if err != nil {
@@ -321,11 +321,11 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
321321
// Addresses can have different proof couriers configured, but both
322322
// types of couriers that currently exist will receive this config upon
323323
// initialization.
324-
proofCourierCfg := &proof.CourierCfg{
324+
proofCourierDispatcher := proof.NewCourierDispatch(&proof.CourierCfg{
325325
HashMailCfg: cfg.HashMailCourier,
326326
UniverseRpcCfg: cfg.UniverseRpcCourier,
327327
TransferLog: assetStore,
328-
}
328+
})
329329

330330
return &tap.Config{
331331
DebugLevel: cfg.DebugLevel,
@@ -359,19 +359,18 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
359359
GroupVerifier: tapgarden.GenGroupVerifier(
360360
context.Background(), assetMintingStore,
361361
),
362-
AddrBook: addrBook,
363-
ProofArchive: proofArchive,
364-
ProofNotifier: assetStore,
365-
ErrChan: mainErrChan,
366-
ProofCourierCfg: proofCourierCfg,
367-
ProofRetrievalDelay: cfg.CustodianProofRetrievalDelay,
368-
ProofWatcher: reOrgWatcher,
362+
AddrBook: addrBook,
363+
ProofArchive: proofArchive,
364+
ProofNotifier: assetStore,
365+
ErrChan: mainErrChan,
366+
ProofCourierDispatcher: proofCourierDispatcher,
367+
ProofRetrievalDelay: cfg.CustodianProofRetrievalDelay, ProofWatcher: reOrgWatcher,
369368
},
370369
),
371370
ChainBridge: chainBridge,
372371
AddrBook: addrBook,
373372
AddrBookDisableSyncer: cfg.AddrBook.DisableSyncer,
374-
DefaultProofCourierAddr: proofCourierAddr.Url(),
373+
DefaultProofCourierAddr: proofCourierAddr,
375374
ProofArchive: proofArchive,
376375
AssetWallet: assetWallet,
377376
CoinSelect: coinSelect,
@@ -384,13 +383,13 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
384383
GroupVerifier: tapgarden.GenGroupVerifier(
385384
context.Background(), assetMintingStore,
386385
),
387-
Wallet: walletAnchor,
388-
KeyRing: keyRing,
389-
AssetWallet: assetWallet,
390-
AssetProofs: proofFileStore,
391-
ProofCourierCfg: proofCourierCfg,
392-
ProofWatcher: reOrgWatcher,
393-
ErrChan: mainErrChan,
386+
Wallet: walletAnchor,
387+
KeyRing: keyRing,
388+
AssetWallet: assetWallet,
389+
AssetProofs: proofFileStore,
390+
ProofCourierDispatcher: proofCourierDispatcher,
391+
ProofWatcher: reOrgWatcher,
392+
ErrChan: mainErrChan,
394393
},
395394
),
396395
UniverseArchive: baseUni,

0 commit comments

Comments
 (0)