Skip to content

Commit

Permalink
tapfreighter: chain porter inits proof courier using transfer output
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Aug 30, 2023
1 parent 7e5042a commit 0be6f9d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 51 deletions.
52 changes: 17 additions & 35 deletions tapcfg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,32 +180,14 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
}
}

var hashMailCourier proof.Courier
if cfg.HashMailCourier != nil &&
proofCourierAddr.Type() == proof.ApertureCourier {

hashMailBox, err := proof.NewHashMailBox(
proofCourierAddr.Url(), cfg.HashMailCourier.TlsCertPath,
)
if err != nil {
return nil, fmt.Errorf("unable to make mailbox: %v",
err)
}

hashMailCourier, err = proof.NewHashMailCourier(
cfg.HashMailCourier, hashMailBox, assetStore,
)
if err != nil {
return nil, fmt.Errorf("unable to make hashmail "+
"courier: %v", err)
var proofCourierCfg *proof.CourierCfg
if cfg.HashMailCourier != nil {
proofCourierCfg = &proof.CourierCfg{
HashMailCfg: cfg.HashMailCourier,
DeliveryLog: assetStore,
}
}

proofCourierCfg := proof.CourierCfg{
HashMailCfg: cfg.HashMailCourier,
DeliveryLog: assetStore,
}

reOrgWatcher := tapgarden.NewReOrgWatcher(&tapgarden.ReOrgWatcherConfig{
ChainBridge: chainBridge,
ProofArchive: proofArchive,
Expand Down Expand Up @@ -315,7 +297,7 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
ProofArchive: proofArchive,
ProofNotifier: assetStore,
ErrChan: mainErrChan,
ProofCourierCfg: &proofCourierCfg,
ProofCourierCfg: proofCourierCfg,
ProofWatcher: reOrgWatcher,
},
),
Expand All @@ -327,17 +309,17 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
CoinSelect: coinSelect,
ChainPorter: tapfreighter.NewChainPorter(
&tapfreighter.ChainPorterConfig{
Signer: virtualTxSigner,
TxValidator: &tap.ValidatorV0{},
ExportLog: assetStore,
ChainBridge: chainBridge,
Wallet: walletAnchor,
KeyRing: keyRing,
AssetWallet: assetWallet,
AssetProofs: proofFileStore,
ProofCourier: hashMailCourier,
ProofWatcher: reOrgWatcher,
ErrChan: mainErrChan,
Signer: virtualTxSigner,
TxValidator: &tap.ValidatorV0{},
ExportLog: assetStore,
ChainBridge: chainBridge,
Wallet: walletAnchor,
KeyRing: keyRing,
AssetWallet: assetWallet,
AssetProofs: proofFileStore,
ProofCourierCfg: proofCourierCfg,
ProofWatcher: reOrgWatcher,
ErrChan: mainErrChan,
},
),
BaseUniverse: baseUni,
Expand Down
49 changes: 33 additions & 16 deletions tapfreighter/chain_porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ type ChainPorterConfig struct {
// TODO(roasbeef): replace with proof.Courier in the future/
AssetProofs proof.Archiver

// ProofCourier is used to optionally deliver the final proof to the
// user using an asynchronous transport mechanism.
ProofCourier proof.Courier
// ProofCourierCfg is a general config applicable to all proof courier
// service handles.
ProofCourierCfg *proof.CourierCfg

// ProofWatcher is used to watch new proofs for their anchor transaction
// to be confirmed safely with a minimum number of confirmations.
Expand Down Expand Up @@ -607,14 +607,41 @@ func (p *ChainPorter) transferReceiverProof(pkg *sendPackage) error {
log.Debugf("Attempting to deliver proof for script key %x",
key.SerializeCompressed())

proofCourierAddr, err := proof.ParseCourierAddrString(
string(out.ProofCourierAddr),
)
if err != nil {
return fmt.Errorf("failed to parse proof courier "+
"address: %w", err)
}

// Initiate proof courier service handle from the proof
// courier address found in the Tap address.
recipient := proof.Recipient{
ScriptKey: key,
AssetID: *receiverProof.AssetID,
Amount: out.Amount,
}
err := p.cfg.ProofCourier.DeliverProof(
ctx, recipient, receiverProof,
courier, err := proofCourierAddr.NewCourier(
ctx, p.cfg.ProofCourierCfg, recipient,
)
if err != nil {
return fmt.Errorf("unable to initiate proof courier "+
"service handle: %w", err)
}

// Update courier events subscribers before attempting to
// deliver proof.
p.subscriberMtx.Lock()
courier.SetSubscribers(p.subscribers)
p.subscriberMtx.Unlock()

// Deliver proof to proof courier service.
err = courier.DeliverProof(ctx, receiverProof)
if err != nil {
return fmt.Errorf("failed to deliver proof via "+
"courier service: %w", err)
}

// If the proof courier returned a backoff error, then
// we'll just return nil here so that we can retry
Expand All @@ -632,7 +659,7 @@ func (p *ChainPorter) transferReceiverProof(pkg *sendPackage) error {

// If we have a proof courier instance active, then we'll launch several
// goroutines to deliver the proof(s) to the receiver(s).
if p.cfg.ProofCourier != nil {
if p.cfg.ProofCourierCfg != nil {
ctx, cancel := p.WithCtxQuitNoTimeout()
defer cancel()

Expand Down Expand Up @@ -1016,11 +1043,6 @@ func (p *ChainPorter) RegisterSubscriber(

p.subscribers[receiver.ID()] = receiver

// If we have a proof courier, we'll also update its subscribers.
if p.cfg.ProofCourier != nil {
p.cfg.ProofCourier.SetSubscribers(p.subscribers)
}

return nil
}

Expand All @@ -1041,11 +1063,6 @@ func (p *ChainPorter) RemoveSubscriber(
subscriber.Stop()
delete(p.subscribers, subscriber.ID())

// If we have a proof courier, we'll also update its subscribers.
if p.cfg.ProofCourier != nil {
p.cfg.ProofCourier.SetSubscribers(p.subscribers)
}

return nil
}

Expand Down

0 comments on commit 0be6f9d

Please sign in to comment.