Skip to content

Commit 652ce04

Browse files
authored
Merge pull request #1696 from lightninglabs/wip/simplify-chainporter-SM-fix-itest-flake
tapfreighter: simplify ChainPorter state machine and fix itest flake
2 parents 8785003 + e55780d commit 652ce04

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

docs/release-notes/release-notes-0.7.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@
130130
- All [`lndclient` wrapper services were moved to their own `lndservices` sub
131131
package](https://github.com/lightninglabs/taproot-assets/pull/1668).
132132

133+
- [Simplify](https://github.com/lightninglabs/taproot-assets/pull/1696)
134+
`ChainPorter` state machine by removing a goroutine and simplifying
135+
event emission. Fixes an itest flake.
136+
133137
## Breaking Changes
134138

135139
## Performance Improvements

tapfreighter/chain_porter.go

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,8 @@ func (p *ChainPorter) mainEventLoop() {
332332
func (p *ChainPorter) advanceState(pkg *sendPackage, kit *parcelKit) {
333333
// Continue state transitions whilst state complete has not yet
334334
// been reached.
335-
for pkg.SendState < SendStateComplete {
336-
log.Infof("ChainPorter executing state: %v",
337-
pkg.SendState)
335+
for pkg.SendState <= SendStateComplete {
336+
log.Infof("ChainPorter executing state: %v", pkg.SendState)
338337

339338
// Before we attempt a state transition, make sure that
340339
// we aren't trying to shut down.
@@ -360,14 +359,19 @@ func (p *ChainPorter) advanceState(pkg *sendPackage, kit *parcelKit) {
360359
}
361360

362361
// Notify subscribers that the state machine has executed a
363-
// state successfully. The only state that happens in a
364-
// goroutine outside the state machine is sending the proof to
365-
// the receiver using the proof courier service. That goroutine
366-
// will notify the subscribers itself, so we skip it here.
367-
if pkg.SendState < SendStateComplete {
368-
p.publishSubscriberEvent(newAssetSendEvent(
369-
stateToExecute, *updatedPkg,
370-
))
362+
// state successfully.
363+
p.publishSubscriberEvent(newAssetSendEvent(
364+
stateToExecute, *updatedPkg,
365+
))
366+
367+
// Exit the loop once the state machine has executed its final
368+
// state.
369+
if pkg.SendState == SendStateComplete {
370+
log.Infof("ChainPorter completed state machine for "+
371+
"parcel (anchor_txid=%v)",
372+
updatedPkg.OutboundPkg.AnchorTx.TxHash())
373+
374+
return
371375
}
372376

373377
pkg = updatedPkg
@@ -1028,18 +1032,6 @@ func (p *ChainPorter) transferReceiverProof(pkg *sendPackage) error {
10281032
return nil
10291033
}
10301034

1031-
// At this point, the transfer is fully finalised and successful:
1032-
// - The anchoring transaction has been confirmed on-chain.
1033-
// - The proof(s) have been delivered to the receiver(s).
1034-
// - The database has been updated to reflect the successful transfer.
1035-
log.Infof("Parcel transfer is fully complete (anchor_txid=%v)",
1036-
pkg.OutboundPkg.AnchorTx.TxHash())
1037-
1038-
// Send out the final notification that the transfer is complete.
1039-
p.publishSubscriberEvent(newAssetSendEvent(SendStateComplete, *pkg))
1040-
1041-
pkg.SendState = SendStateComplete
1042-
10431035
return nil
10441036
}
10451037

@@ -1631,26 +1623,24 @@ func (p *ChainPorter) stateStep(currentPkg sendPackage) (*sendPackage, error) {
16311623
// we've stored the sender and receiver proofs in the proof archive.
16321624
// We'll now attempt to transfer one or more proofs to the receiver(s).
16331625
case SendStateTransferProofs:
1634-
// We'll set the package state to complete early here so the
1635-
// main loop breaks out. We'll continue to attempt proof
1636-
// deliver in the background.
1637-
currentPkg.SendState = SendStateComplete
1638-
1639-
p.Wg.Add(1)
1640-
go func() {
1641-
defer p.Wg.Done()
1626+
err := p.transferReceiverProof(&currentPkg)
1627+
if err != nil {
1628+
return nil, fmt.Errorf("unable to transfer receiver "+
1629+
"proof: %w", err)
1630+
}
16421631

1643-
err := p.transferReceiverProof(&currentPkg)
1644-
if err != nil {
1645-
log.Errorf("unable to transfer receiver "+
1646-
"proof: %v", err)
1632+
currentPkg.SendState = SendStateComplete
1633+
return &currentPkg, nil
16471634

1648-
p.publishSubscriberEvent(newAssetSendErrorEvent(
1649-
err, SendStateTransferProofs,
1650-
currentPkg,
1651-
))
1652-
}
1653-
}()
1635+
case SendStateComplete:
1636+
// At this point, the transfer is fully finalised and
1637+
// successful:
1638+
// - The anchoring transaction has been confirmed on-chain.
1639+
// - The proof(s) have been delivered to the receiver(s).
1640+
// - The database has been updated to reflect the successful
1641+
// transfer.
1642+
log.Infof("Parcel transfer is fully complete (anchor_txid=%v)",
1643+
currentPkg.OutboundPkg.AnchorTx.TxHash())
16541644

16551645
return &currentPkg, nil
16561646

0 commit comments

Comments
 (0)