Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ jobs:
- name: check
run: make docs-check

- name: check generated FSM diagrams
run: make fsm-check

########################
# run unit-test sqlite3 race
########################
Expand Down
18 changes: 17 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,20 @@ docs-check: docs
fsm:
@$(call print, "Generating state machine docs")
./scripts/fsm-generate.sh;
.PHONY: fsm

FSM_FILES := \
fsm/example_fsm.md \
instantout/fsm.md \
instantout/reservation/fsm.md \
staticaddr/deposit/fsm.md \
staticaddr/loopin/fsm.md

fsm-check: fsm
@$(call print, "Verifying generated state machine docs")
if test -n "$$(git status --porcelain -- $(FSM_FILES))"; then \
echo "Generated FSM diagrams are not up-to-date!"; \
git status --porcelain -- $(FSM_FILES); \
git diff -- $(FSM_FILES); \
exit 1; \
fi
Comment thread
GustavoStingelin marked this conversation as resolved.
.PHONY: fsm fsm-check
4 changes: 2 additions & 2 deletions fsm/example_fsm.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
stateDiagram-v2
[*] --> InitFSM: OnRequestStuff
InitFSM
InitFSM --> StuffSentOut: OnStuffSentOut
InitFSM --> StuffFailed: OnError
InitFSM --> StuffSentOut: OnStuffSentOut
StuffFailed
StuffSentOut
StuffSentOut --> StuffSuccess: OnStuffSuccess
StuffSentOut --> StuffFailed: OnError
StuffSentOut --> StuffSuccess: OnStuffSuccess
StuffSuccess
```
68 changes: 47 additions & 21 deletions fsm/stateparser/stateparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import (
"github.com/lightninglabs/loop/fsm"
"github.com/lightninglabs/loop/instantout"
"github.com/lightninglabs/loop/instantout/reservation"
"github.com/lightninglabs/loop/staticaddr/deposit"
"github.com/lightninglabs/loop/staticaddr/loopin"
)

var errInvalidFSMSelector = errors.New("missing or unknown fsm selector")

func main() {
if err := run(); err != nil {
fmt.Println(err)
Expand All @@ -35,35 +39,43 @@ func run() error {
return err
}

switch *stateMachine {
states, err := getStates(*stateMachine)
if err != nil {
return err
}

return writeMermaidFile(fp, states)
}

func getStates(stateMachine string) (fsm.States, error) {
switch stateMachine {
case "example":
exampleFSM := &fsm.ExampleFSM{}
err = writeMermaidFile(fp, exampleFSM.GetStates())
if err != nil {
return err
}
return exampleFSM.GetStates(), nil

case "reservation":
reservationFSM := &reservation.FSM{}
err = writeMermaidFile(fp, reservationFSM.GetServerInitiatedReservationStates())
if err != nil {
return err
}
return reservationFSM.GetServerInitiatedReservationStates(), nil

case "instantout":
instantout := &instantout.FSM{}
err = writeMermaidFile(fp, instantout.GetV1ReservationStates())
if err != nil {
return err
}
instantOutFSM := &instantout.FSM{}
return instantOutFSM.GetV1ReservationStates(), nil

case "staticaddr-deposit":
depositFSM := &deposit.FSM{}
return depositFSM.DepositStatesV0(), nil

case "staticaddr-loopin":
loopInFSM := &loopin.FSM{}
return loopInFSM.LoopInStatesV0(), nil

default:
fmt.Println("Missing or wrong argument: fsm must be one of:")
fmt.Println("\treservations")
fmt.Println("\texample")
return nil, fmt.Errorf(
"%w %q; supported selectors: example, instantout, "+
"reservation, staticaddr-deposit, staticaddr-loopin",
errInvalidFSMSelector, stateMachine,
)
}

return nil
}

func writeMermaidFile(filename string, states fsm.States) error {
Expand All @@ -86,8 +98,11 @@ func writeMermaidFile(filename string, states fsm.States) error {
state = "[*]"
}
// write transitions
for edge, target := range edges.Transitions {
fmt.Fprintf(&b, "%s --> %s: %s\n", state, target, edge)
for _, edge := range sortedTransitionKeys(edges.Transitions) {
fmt.Fprintf(
&b, "%s --> %s: %s\n", state,
edges.Transitions[fsm.EventType(edge)], edge,
)
}
}

Expand All @@ -110,3 +125,14 @@ func sortedKeys(m fsm.States) []string {
sort.Strings(keys)
return keys
}

func sortedTransitionKeys(m fsm.Transitions) []string {
keys := make([]string, len(m))
i := 0
for k := range m {
keys[i] = string(k)
i++
}
sort.Strings(keys)
return keys
}
32 changes: 19 additions & 13 deletions instantout/fsm.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,41 @@
stateDiagram-v2
[*] --> Init: OnStart
BuildHtlc
BuildHtlc --> InstantOutFailed: OnError
BuildHtlc --> PushPreimage: OnHtlcSigReceived
BuildHtlc --> InstantFailedOutFailed: OnError
BuildHtlc --> InstantFailedOutFailed: OnRecover
BuildHtlc --> InstantOutFailed: OnRecover
FailedHtlcSweep
FailedHtlcSweep --> PublishHtlcSweep: OnRecover
FinishedHtlcPreimageSweep
FinishedSweeplessSweep
Init
Init --> InstantOutFailed: OnError
Init --> SendPaymentAndPollAccepted: OnInit
Init --> InstantFailedOutFailed: OnError
Init --> InstantFailedOutFailed: OnRecover
InstantFailedOutFailed
Init --> InstantOutFailed: OnRecover
InstantOutFailed
PublishHtlc
PublishHtlc --> FailedHtlcSweep: OnError
PublishHtlc --> PublishHtlcSweep: OnHtlcPublished
PublishHtlc --> PublishHtlc: OnRecover
PublishHtlc --> WaitForHtlcSweepConfirmed: OnHtlcBroadcasted
PublishHtlcSweep
PublishHtlcSweep --> FailedHtlcSweep: OnError
PublishHtlcSweep --> WaitForHtlcSweepConfirmed: OnHtlcSweepPublished
PublishHtlcSweep --> PublishHtlcSweep: OnRecover
PushPreimage
PushPreimage --> InstantOutFailed: OnError
PushPreimage --> PublishHtlc: OnErrorPublishHtlc
PushPreimage --> PushPreimage: OnRecover
PushPreimage --> WaitForSweeplessSweepConfirmed: OnSweeplessSweepPublished
PushPreimage --> InstantFailedOutFailed: OnError
PushPreimage --> PublishHtlc: OnErrorPublishHtlc
SendPaymentAndPollAccepted
SendPaymentAndPollAccepted --> InstantOutFailed: OnError
SendPaymentAndPollAccepted --> BuildHtlc: OnPaymentAccepted
SendPaymentAndPollAccepted --> InstantFailedOutFailed: OnError
SendPaymentAndPollAccepted --> InstantFailedOutFailed: OnRecover
SendPaymentAndPollAccepted --> InstantOutFailed: OnRecover
WaitForHtlcSweepConfirmed
WaitForHtlcSweepConfirmed --> FailedHtlcSweep: OnError
WaitForHtlcSweepConfirmed --> FinishedHtlcPreimageSweep: OnHtlcSwept
WaitForHtlcSweepConfirmed --> WaitForHtlcSweepConfirmed: OnRecover
WaitForHtlcSweepConfirmed --> FailedHtlcSweep: OnError
WaitForSweeplessSweepConfirmed
WaitForSweeplessSweepConfirmed --> FinishedSweeplessSweep: OnSweeplessSweepConfirmed
WaitForSweeplessSweepConfirmed --> WaitForSweeplessSweepConfirmed: OnRecover
WaitForSweeplessSweepConfirmed --> PublishHtlc: OnError
WaitForSweeplessSweepConfirmed --> WaitForSweeplessSweepConfirmed: OnRecover
WaitForSweeplessSweepConfirmed --> FinishedSweeplessSweep: OnSweeplessSweepConfirmed
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@
stateDiagram-v2
[*] --> Init: OnServerRequest
Confirmed
Confirmed --> SpendBroadcasted: OnSpendBroadcasted
Confirmed --> TimedOut: OnTimedOut
Confirmed --> Confirmed: OnError
Confirmed --> Locked: OnLocked
Confirmed --> Confirmed: OnRecover
Confirmed --> Spent: OnSpent
Confirmed --> TimedOut: OnTimedOut
Failed
Init
Init --> WaitForConfirmation: OnBroadcast
Init --> Failed: OnRecover
Init --> Failed: OnError
SpendBroadcasted
SpendBroadcasted --> SpendConfirmed: OnSpendConfirmed
SpendConfirmed
Init --> Failed: OnRecover
Locked
Locked --> Locked: OnError
Locked --> Locked: OnRecover
Locked --> Spent: OnSpent
Locked --> TimedOut: OnTimedOut
Locked --> Confirmed: OnUnlocked
Spent
Spent --> Spent: OnSpent
TimedOut
TimedOut --> TimedOut: OnTimedOut
WaitForConfirmation
WaitForConfirmation --> WaitForConfirmation: OnRecover
WaitForConfirmation --> Confirmed: OnConfirmed
WaitForConfirmation --> WaitForConfirmation: OnRecover
WaitForConfirmation --> TimedOut: OnTimedOut
```
8 changes: 6 additions & 2 deletions scripts/fsm-generate.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail

go run ./fsm/stateparser/stateparser.go --out ./fsm/example_fsm.md --fsm example
go run ./fsm/stateparser/stateparser.go --out ./reservation/reservation_fsm.md --fsm reservation
go run ./fsm/stateparser/stateparser.go --out ./instantout/fsm.md --fsm instantout
go run ./fsm/stateparser/stateparser.go --out ./instantout/reservation/fsm.md --fsm reservation
go run ./fsm/stateparser/stateparser.go --out ./instantout/fsm.md --fsm instantout
go run ./fsm/stateparser/stateparser.go --out ./staticaddr/deposit/fsm.md --fsm staticaddr-deposit
go run ./fsm/stateparser/stateparser.go --out ./staticaddr/loopin/fsm.md --fsm staticaddr-loopin
Comment thread
GustavoStingelin marked this conversation as resolved.
52 changes: 52 additions & 0 deletions staticaddr/deposit/fsm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
```mermaid
stateDiagram-v2
[*] --> Deposited: OnStart
ChannelPublished
ChannelPublished --> ChannelPublished: OnExpiry
Deposited
Deposited --> Deposited: OnError
Deposited --> PublishExpirySweep: OnExpiry
Deposited --> LoopingIn: OnLoopInInitiated
Deposited --> OpeningChannel: OnOpeningChannel
Deposited --> Deposited: OnRecover
Deposited --> SweepHtlcTimeout: OnSweepingHtlcTimeout
Deposited --> Withdrawing: OnWithdrawInitiated
Expired
Expired --> Expired: OnExpiry
HtlcTimeoutSwept
HtlcTimeoutSwept --> HtlcTimeoutSwept: OnExpiry
LoopedIn
LoopedIn --> LoopedIn: OnExpiry
LoopingIn
LoopingIn --> Deposited: OnError
LoopingIn --> PublishExpirySweep: OnExpiry
LoopingIn --> LoopingIn: OnLoopInInitiated
LoopingIn --> LoopedIn: OnLoopedIn
LoopingIn --> LoopingIn: OnRecover
LoopingIn --> SweepHtlcTimeout: OnSweepingHtlcTimeout
OpeningChannel
OpeningChannel --> ChannelPublished: OnChannelPublished
OpeningChannel --> Deposited: OnError
OpeningChannel --> OpeningChannel: OnExpiry
OpeningChannel --> OpeningChannel: OnRecover
PublishExpirySweep
PublishExpirySweep --> Deposited: OnError
PublishExpirySweep --> WaitForExpirySweep: OnExpiryPublished
PublishExpirySweep --> PublishExpirySweep: OnRecover
SweepHtlcTimeout
SweepHtlcTimeout --> HtlcTimeoutSwept: OnHtlcTimeoutSwept
SweepHtlcTimeout --> SweepHtlcTimeout: OnRecover
WaitForExpirySweep
WaitForExpirySweep --> Deposited: OnError
WaitForExpirySweep --> Expired: OnExpirySwept
WaitForExpirySweep --> PublishExpirySweep: OnRecover
Withdrawing
Withdrawing --> Deposited: OnError
Withdrawing --> Withdrawing: OnExpiry
Withdrawing --> Withdrawing: OnRecover
Withdrawing --> Withdrawing: OnWithdrawInitiated
Withdrawing --> Withdrawn: OnWithdrawn
Withdrawn
Withdrawn --> Withdrawn: OnExpiry
Withdrawn --> Withdrawn: OnWithdrawn
```
37 changes: 37 additions & 0 deletions staticaddr/loopin/fsm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
```mermaid
stateDiagram-v2
Comment on lines +1 to +2

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GitHub rendering looks a bit strange, but it renders well in my local GoLand

Image

[*] --> InitHtlcTx: OnInitHtlc
Failed
HtlcTimeoutSwept
InitHtlcTx
InitHtlcTx --> UnlockDeposits: OnError
InitHtlcTx --> SignHtlcTx: OnHtlcInitiated
InitHtlcTx --> UnlockDeposits: OnRecover
MonitorHtlcTimeoutSweep
MonitorHtlcTimeoutSweep --> Failed: OnError
MonitorHtlcTimeoutSweep --> HtlcTimeoutSwept: OnHtlcTimeoutSwept
MonitorHtlcTimeoutSweep --> MonitorHtlcTimeoutSweep: OnRecover
MonitorInvoiceAndHtlcTx
MonitorInvoiceAndHtlcTx --> UnlockDeposits: OnError
MonitorInvoiceAndHtlcTx --> PaymentReceived: OnPaymentReceived
MonitorInvoiceAndHtlcTx --> MonitorInvoiceAndHtlcTx: OnRecover
MonitorInvoiceAndHtlcTx --> Failed: OnSwapTimedOut
MonitorInvoiceAndHtlcTx --> SweepHtlcTimeout: OnSweepHtlcTimeout
PaymentReceived
PaymentReceived --> SucceededTransitioningFailed: OnError
PaymentReceived --> Succeeded: OnRecover
PaymentReceived --> Succeeded: OnSucceeded
SignHtlcTx
SignHtlcTx --> UnlockDeposits: OnError
SignHtlcTx --> MonitorInvoiceAndHtlcTx: OnHtlcTxSigned
SignHtlcTx --> UnlockDeposits: OnRecover
Succeeded
SucceededTransitioningFailed
SweepHtlcTimeout
SweepHtlcTimeout --> Failed: OnError
SweepHtlcTimeout --> MonitorHtlcTimeoutSweep: OnHtlcTimeoutSweepPublished
SweepHtlcTimeout --> SweepHtlcTimeout: OnRecover
UnlockDeposits
UnlockDeposits --> Failed: OnError
UnlockDeposits --> UnlockDeposits: OnRecover
```
Loading