-
Notifications
You must be signed in to change notification settings - Fork 3
/
HTLCUser.tla
151 lines (136 loc) · 5.98 KB
/
HTLCUser.tla
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
------------------------------ MODULE HTLCUser ------------------------------
EXTENDS Naturals, FiniteSets, TLC
CONSTANTS EmptyMessage,
SingleSignature,
AllSignatures,
SingleSigHashLock,
AllSigHashLock,
IDs,
Users,
Keys,
Hashes,
Time,
Preimages,
AvailableTransactionIds,
Amounts,
MAX_TIME
VARIABLES State,
PCState,
Inventory,
Vars,
MyName,
OtherName,
Honest,
Balance,
PendingBalance,
Message,
LedgerTx,
LedgerTime,
TIOUsedTransactionIds,
UnchangedVars
TIO == INSTANCE TransactionIdOracle
RequestInvoice ==
/\ State = "ready"
/\ PCState = "rev-keys-exchanged"
/\ State' = "invoice-requested"
/\ Cardinality(Vars.NewPaymentData) > 0
/\ Cardinality(Vars.OtherPaymentHashes) < Cardinality(Vars.NewPaymentData)
/\ Message = EmptyMessage
/\ Message' = [EmptyMessage EXCEPT !.recipient = OtherName,
!.type = "RequestInvoice"]
/\ UNCHANGED <<Inventory, Vars, LedgerTx, Balance, PendingBalance, TIOUsedTransactionIds>>
/\ UNCHANGED UnchangedVars
GenerateAndSendPaymentHash ==
/\ State = "ready"
/\ Message.recipient = MyName
/\ Message.type = "RequestInvoice"
/\ LET preimage == TIO!GetNewTransactionId
IN /\ TIO!MarkAsUsed(preimage)
/\ ~ preimage \in Inventory.preimages
/\ LET hash == preimage
IN /\ Message' = [EmptyMessage EXCEPT !.recipient = OtherName,
!.type = "PaymentHash",
!.data.hash = hash]
/\ Inventory' = [Inventory EXCEPT !.preimages = @ \cup {preimage}]
/\ UNCHANGED <<State, Vars, LedgerTx, Balance, PendingBalance>>
/\ UNCHANGED UnchangedVars
ReceivePaymentHash ==
/\ State = "invoice-requested"
/\ State' = "ready"
/\ Message.recipient = MyName
/\ Message.type = "PaymentHash"
/\ Vars' = [Vars EXCEPT !.OtherPaymentHashes = @ \cup {Message.data.hash}]
/\ Message' = EmptyMessage
/\ UNCHANGED <<Inventory, LedgerTx, TIOUsedTransactionIds, Balance, PendingBalance>>
/\ UNCHANGED UnchangedVars
AddAndSendOutgoingHTLC ==
/\ State = "ready"
/\ PCState = "rev-keys-exchanged"
/\ State' = "update-htlc-sent"
/\ Message = EmptyMessage
/\ LedgerTime <= MAX_TIME - 10
/\ \E payment \in Vars.NewPaymentData :
/\ Balance >= payment.amount
/\ \E hash \in Vars.OtherPaymentHashes :
LET htlcData == [amount |-> payment.amount, hash |-> hash, absTimelock |-> LedgerTime + 5, state |-> "NEW"]
IN /\ Vars' = [Vars EXCEPT !.OutgoingHTLCs = @ \cup {htlcData},
!.OtherPaymentHashes = @ \ {hash},
!.NewPaymentData = @ \ {payment}]
/\ PendingBalance' = PendingBalance + htlcData.amount
/\ Balance' = Balance - htlcData.amount
/\ Message' = [EmptyMessage EXCEPT !.recipient = OtherName,
!.type = "UpdateAddHTLC",
!.data.htlcData = htlcData]
/\ UNCHANGED <<Inventory, LedgerTx, TIOUsedTransactionIds>>
/\ UNCHANGED UnchangedVars
ReceiveUpdateAddHTLC ==
/\ State = "ready"
/\ State' = "update-htlc-added"
/\ Message.recipient = MyName
/\ Message.type = "UpdateAddHTLC"
/\ LedgerTime < Message.data.htlcData.absTimelock - 1
/\ LET newHTLC == [amount |-> Message.data.htlcData.amount, hash |-> Message.data.htlcData.hash, absTimelock |-> Message.data.htlcData.absTimelock, state |-> "NEW"]
IN Vars' = [Vars EXCEPT !.IncomingHTLCs = @ \cup {newHTLC}]
/\ Message' = EmptyMessage
/\ UNCHANGED <<Inventory, LedgerTx, TIOUsedTransactionIds, Balance, PendingBalance>>
/\ UNCHANGED UnchangedVars
SendHTLCPreimage ==
/\ State = "update-htlc-added"
/\ PCState = "rev-keys-exchanged"
/\ State' = "ready"
/\ Message = EmptyMessage
/\ \E htlc \in {htlc \in Vars.IncomingHTLCs : htlc.state = "COMMITTED"} :
/\ htlc.hash \in (Inventory.preimages \ Vars.OtherKnownPreimages)
/\ LedgerTime < htlc.absTimelock
/\ Message' = [EmptyMessage EXCEPT !.recipient = OtherName,
!.type = "HTLCPreimage",
!.data.preimage = htlc.hash]
/\ Vars' = [Vars EXCEPT !.OtherKnownPreimages = @ \cup {htlc.hash},
!.IncomingHTLCs = (@ \ {htlc}) \cup {[htlc EXCEPT !.state = "FULFILLED"]} ]
/\ Balance' = Balance + htlc.amount
/\ PendingBalance' = PendingBalance - htlc.amount
/\ UNCHANGED <<Inventory, LedgerTx, TIOUsedTransactionIds>>
/\ UNCHANGED UnchangedVars
ReceiveHTLCPreimage ==
/\ State = "update-htlc-sent"
/\ State' = "ready"
/\ Message.recipient = MyName
/\ Message.type = "HTLCPreimage"
/\ Message' = EmptyMessage
/\ Inventory' = [Inventory EXCEPT !.preimages = @ \cup {Message.data.preimage}]
/\ LET fulfilledHTLCs == {htlc \in (Vars.OutgoingHTLCs) : htlc.state = "COMMITTED" /\ htlc.hash = Message.data.preimage}
IN Vars' = [Vars EXCEPT !.OutgoingHTLCs = (@ \ fulfilledHTLCs) \cup {[htlc EXCEPT !.state = "FULFILLED"] : htlc \in (fulfilledHTLCs \cap Vars.OutgoingHTLCs)} ]
/\ UNCHANGED <<LedgerTx, TIOUsedTransactionIds, Balance, PendingBalance>>
/\ UNCHANGED UnchangedVars
Next ==
\/ RequestInvoice
\/ GenerateAndSendPaymentHash
\/ ReceivePaymentHash
\/ AddAndSendOutgoingHTLC
\/ ReceiveUpdateAddHTLC
\/ SendHTLCPreimage
\/ ReceiveHTLCPreimage
=============================================================================
\* Modification History
\* Last modified Fri Sep 24 18:47:09 CEST 2021 by matthias
\* Created Thu Jun 10 13:36:19 CEST 2021 by matthias