Skip to content

Commit 7e03a48

Browse files
committed
proof+tapcfg: add CourierDispatch interface
Instead of needing to create a special proof courier address and then creating a courier from that, we want to have a central dispatcher that can hold on to the configuration for each of the different couriers. A courier is then created through the dispatcher directly.
1 parent 744dbb3 commit 7e03a48

File tree

2 files changed

+117
-22
lines changed

2 files changed

+117
-22
lines changed

proof/courier.go

Lines changed: 114 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,113 @@ type Courier interface {
7272
Close() error
7373
}
7474

75+
// CourierCfg contains general config parameters applicable to all proof
76+
// couriers.
77+
type CourierCfg struct {
78+
// HashMailCfg contains hashmail protocol specific config parameters.
79+
HashMailCfg *HashMailCourierCfg
80+
81+
// UniverseRpcCfg contains universe RPC protocol specific config
82+
// parameters.
83+
UniverseRpcCfg *UniverseRpcCourierCfg
84+
85+
// TransferLog is a log for recording proof delivery and retrieval
86+
// attempts.
87+
TransferLog TransferLog
88+
}
89+
90+
// CourierDispatch is an interface that abstracts away the different proof
91+
// courier services that are supported.
92+
type CourierDispatch interface {
93+
// NewCourier instantiates a new courier service handle given a service
94+
// URL address.
95+
NewCourier(addr *url.URL, recipient Recipient) (Courier, error)
96+
}
97+
98+
// URLDispatch is a proof courier dispatch that uses the courier address URL
99+
// scheme to determine which courier service to use.
100+
type URLDispatch struct {
101+
cfg *CourierCfg
102+
}
103+
104+
// NewCourierDispatch creates a new proof courier dispatch.
105+
func NewCourierDispatch(cfg *CourierCfg) *URLDispatch {
106+
return &URLDispatch{
107+
cfg: cfg,
108+
}
109+
}
110+
111+
// NewCourier instantiates a new courier service handle given a service URL
112+
// address.
113+
func (u *URLDispatch) NewCourier(addr *url.URL,
114+
recipient Recipient) (Courier, error) {
115+
116+
subscribers := make(map[uint64]*fn.EventReceiver[fn.Event])
117+
118+
// Create new courier addr based on URL scheme.
119+
switch addr.Scheme {
120+
case HashmailCourierType:
121+
cfg := u.cfg.HashMailCfg
122+
backoffHandler := NewBackoffHandler(
123+
cfg.BackoffCfg, u.cfg.TransferLog,
124+
)
125+
126+
hashMailCfg := HashMailCourierCfg{
127+
ReceiverAckTimeout: cfg.ReceiverAckTimeout,
128+
}
129+
130+
hashMailBox, err := NewHashMailBox(addr)
131+
if err != nil {
132+
return nil, fmt.Errorf("unable to make mailbox: %v",
133+
err)
134+
}
135+
136+
return &HashMailCourier{
137+
cfg: &hashMailCfg,
138+
backoffHandle: backoffHandler,
139+
recipient: recipient,
140+
mailbox: hashMailBox,
141+
subscribers: subscribers,
142+
}, nil
143+
144+
case UniverseRpcCourierType:
145+
cfg := u.cfg.UniverseRpcCfg
146+
backoffHandler := NewBackoffHandler(
147+
cfg.BackoffCfg, u.cfg.TransferLog,
148+
)
149+
150+
// Connect to the universe RPC server.
151+
dialOpts, err := serverDialOpts()
152+
if err != nil {
153+
return nil, err
154+
}
155+
156+
serverAddr := fmt.Sprintf("%s:%s", addr.Hostname(), addr.Port())
157+
conn, err := grpc.Dial(serverAddr, dialOpts...)
158+
if err != nil {
159+
return nil, err
160+
}
161+
162+
client := unirpc.NewUniverseClient(conn)
163+
164+
return &UniverseRpcCourier{
165+
recipient: recipient,
166+
client: client,
167+
backoffHandle: backoffHandler,
168+
transfer: u.cfg.TransferLog,
169+
subscribers: subscribers,
170+
}, nil
171+
172+
default:
173+
return nil, fmt.Errorf("unknown courier address protocol "+
174+
"(consider updating tapd): %v", addr.Scheme)
175+
}
176+
}
177+
178+
// A compile-time assertion to ensure that the URLDispatch meets the
179+
// CourierDispatch interface.
180+
var _ CourierDispatch = (*URLDispatch)(nil)
181+
75182
// CourierAddr is a fully validated courier address (including protocol specific
76183
// validation).
77184
type CourierAddr interface {
@@ -126,10 +233,12 @@ func (h *HashMailCourierAddr) Url() *url.URL {
126233
func (h *HashMailCourierAddr) NewCourier(_ context.Context, cfg *CourierCfg,
127234
recipient Recipient) (Courier, error) {
128235

129-
backoffHandle := NewBackoffHandler(cfg.BackoffCfg, cfg.TransferLog)
236+
backoffHandle := NewBackoffHandler(
237+
cfg.HashMailCfg.BackoffCfg, cfg.TransferLog,
238+
)
130239

131240
hashMailCfg := HashMailCourierCfg{
132-
ReceiverAckTimeout: cfg.ReceiverAckTimeout,
241+
ReceiverAckTimeout: cfg.HashMailCfg.ReceiverAckTimeout,
133242
}
134243

135244
hashMailBox, err := NewHashMailBox(&h.addr)
@@ -185,7 +294,9 @@ func (h *UniverseRpcCourierAddr) Url() *url.URL {
185294
func (h *UniverseRpcCourierAddr) NewCourier(_ context.Context,
186295
cfg *CourierCfg, recipient Recipient) (Courier, error) {
187296

188-
backoffHandle := NewBackoffHandler(cfg.BackoffCfg, cfg.TransferLog)
297+
backoffHandle := NewBackoffHandler(
298+
cfg.UniverseRpcCfg.BackoffCfg, cfg.TransferLog,
299+
)
189300

190301
// Ensure that the courier address is a universe RPC address.
191302
if h.addr.Scheme != UniverseRpcCourierType {
@@ -251,22 +362,6 @@ func NewCourier(ctx context.Context, addr url.URL, cfg *CourierCfg,
251362
return courierAddr.NewCourier(ctx, cfg, recipient)
252363
}
253364

254-
// CourierCfg contains general config parameters applicable to all proof
255-
// couriers.
256-
type CourierCfg struct {
257-
// ReceiverAckTimeout is the maximum time we'll wait for the receiver to
258-
// acknowledge the proof.
259-
ReceiverAckTimeout time.Duration
260-
261-
// BackoffCfg configures the behaviour of the proof delivery
262-
// functionality.
263-
BackoffCfg *BackoffCfg
264-
265-
// TransferLog is a log for recording proof delivery and retrieval
266-
// attempts.
267-
TransferLog TransferLog
268-
}
269-
270365
// ProofMailbox represents an abstract store-and-forward mailbox that can be
271366
// used to send/receive proofs.
272367
type ProofMailbox interface {

tapcfg/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
322322
// types of couriers that currently exist will receive this config upon
323323
// initialization.
324324
proofCourierCfg := &proof.CourierCfg{
325-
ReceiverAckTimeout: cfg.HashMailCourier.ReceiverAckTimeout,
326-
BackoffCfg: cfg.HashMailCourier.BackoffCfg,
327-
TransferLog: assetStore,
325+
HashMailCfg: cfg.HashMailCourier,
326+
UniverseRpcCfg: cfg.UniverseRpcCourier,
327+
TransferLog: assetStore,
328328
}
329329

330330
return &tap.Config{

0 commit comments

Comments
 (0)