/
build.go
247 lines (231 loc) · 7.42 KB
/
build.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package fsm
import (
"math"
"time"
b "github.com/stellar/go/build"
"github.com/stellar/go/xdr"
"github.com/interstellar/starlight/math/checked"
"github.com/interstellar/starlight/worizon/xlm"
)
func (ch *Channel) buildWalletTx(seqnum xdr.SequenceNumber, m ...b.TransactionMutator) (*b.TransactionBuilder, error) {
return ch.buildTx(ch.HostAcct, seqnum, ch.HostFeerate, m...)
}
func (ch *Channel) buildEscrowTx(seqnum xdr.SequenceNumber, m ...b.TransactionMutator) (*b.TransactionBuilder, error) {
return ch.buildTx(ch.EscrowAcct, seqnum, ch.ChannelFeerate, m...)
}
func (ch *Channel) buildTx(acct AccountID, seqnum xdr.SequenceNumber, basefee xlm.Amount, m ...b.TransactionMutator) (*b.TransactionBuilder, error) {
args := []b.TransactionMutator{
b.Network{Passphrase: ch.Passphrase},
b.SourceAccount{AddressOrSeed: acct.Address()},
b.Sequence{Sequence: uint64(seqnum)},
b.BaseFee{Amount: uint64(basefee)},
}
args = append(args, m...)
return b.Transaction(args...)
}
/* *** */
func buildSetupAccountTx(ch *Channel, account AccountID, seqnum xdr.SequenceNumber) (*b.TransactionBuilder, error) {
tb, err := ch.buildWalletTx(
seqnum,
b.CreateAccount(
b.Destination{AddressOrSeed: account.Address()},
b.NativeAmount{Amount: xlm.Lumen.HorizonString()},
),
)
return tb, err
}
func buildSettleOnlyWithHostTx(ch *Channel, paymentTime time.Time) (*b.TransactionBuilder, error) {
minTime := uint64(paymentTime.Add(2 * ch.FinalityDelay).Add(ch.MaxRoundDuration).Unix())
if minTime > math.MaxInt64 {
return nil, checked.ErrOverflow
}
return ch.buildEscrowTx(
ch.roundSeqNum()+2,
b.Timebounds{MinTime: minTime},
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.EscrowAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.GuestRatchetAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.HostRatchetAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
)
}
func buildRatchetTx(ch *Channel, ledgerTime time.Time, acct AccountID, seqnum xdr.SequenceNumber) (*b.TransactionBuilder, error) {
maxTime := uint64(ledgerTime.Add(ch.FinalityDelay).Add(ch.MaxRoundDuration).Unix())
if maxTime > math.MaxInt64 {
return nil, checked.ErrOverflow
}
return ch.buildTx(
acct,
seqnum+1,
ch.ChannelFeerate,
b.Timebounds{MaxTime: maxTime},
b.BumpSequence(
b.SourceAccount{AddressOrSeed: ch.EscrowAcct.Address()},
b.BumpTo(ch.roundSeqNum()+1),
),
)
}
func buildFundingTx(ch *Channel, h *WalletAcct) (*b.TransactionBuilder, error) {
maxTime := uint64(ch.FundingTime.Add(ch.MaxRoundDuration).Add(ch.FinalityDelay).Unix())
if maxTime > math.MaxInt64 {
return nil, checked.ErrOverflow
}
tb, err := ch.buildWalletTx(
h.Seqnum,
b.Timebounds{MaxTime: maxTime},
b.Payment(
b.SourceAccount{AddressOrSeed: ch.HostAcct.Address()},
b.Destination{AddressOrSeed: ch.EscrowAcct.Address()},
b.NativeAmount{Amount: (ch.HostAmount + 500*xlm.Millilumen + 8*ch.ChannelFeerate).HorizonString()},
),
b.SetOptions(
b.SourceAccount{AddressOrSeed: ch.EscrowAcct.Address()},
b.SetLowThreshold(2),
b.SetMediumThreshold(2),
b.SetHighThreshold(2),
b.AddSigner(ch.GuestAcct.Address(), 1),
),
b.Payment(
b.SourceAccount{AddressOrSeed: ch.HostAcct.Address()},
b.Destination{AddressOrSeed: ch.GuestRatchetAcct.Address()},
b.NativeAmount{Amount: (xlm.Lumen + ch.ChannelFeerate).HorizonString()},
),
b.SetOptions(
b.SourceAccount{AddressOrSeed: ch.GuestRatchetAcct.Address()},
b.MasterWeight(0),
b.SetLowThreshold(2),
b.SetMediumThreshold(2),
b.SetHighThreshold(2),
b.AddSigner(ch.GuestAcct.Address(), 1),
),
b.SetOptions(
b.SourceAccount{AddressOrSeed: ch.GuestRatchetAcct.Address()},
b.AddSigner(ch.EscrowAcct.Address(), 1),
),
b.Payment(
b.SourceAccount{AddressOrSeed: ch.HostAcct.Address()},
b.Destination{AddressOrSeed: ch.HostRatchetAcct.Address()},
b.NativeAmount{Amount: (500*xlm.Millilumen + ch.ChannelFeerate).HorizonString()},
),
b.SetOptions(
b.SourceAccount{AddressOrSeed: ch.HostRatchetAcct.Address()},
b.MasterWeight(0),
b.AddSigner(ch.EscrowAcct.Address(), 1),
),
)
return tb, err
}
func buildSettleWithGuestTx(ch *Channel, paymentTime time.Time) (*b.TransactionBuilder, error) {
minTime := uint64(paymentTime.Add(2 * ch.FinalityDelay).Add(ch.MaxRoundDuration).Unix())
if minTime > math.MaxInt64 {
return nil, checked.ErrOverflow
}
return ch.buildEscrowTx(
ch.roundSeqNum()+2,
b.Timebounds{MinTime: minTime},
b.Payment(
b.SourceAccount{AddressOrSeed: ch.EscrowAcct.Address()},
b.Destination{AddressOrSeed: ch.GuestAcct.Address()},
b.NativeAmount{Amount: ch.GuestAmount.HorizonString()},
),
)
}
func buildSettleWithHostTx(ch *Channel, paymentTime time.Time) (*b.TransactionBuilder, error) {
minTime := uint64(paymentTime.Add(2 * ch.FinalityDelay).Add(ch.MaxRoundDuration).Unix())
if minTime > math.MaxInt64 {
return nil, checked.ErrOverflow
}
return ch.buildEscrowTx(
ch.roundSeqNum()+3,
b.Timebounds{MinTime: minTime},
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.EscrowAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.GuestRatchetAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.HostRatchetAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
)
}
func buildCooperativeCloseTx(ch *Channel) (*b.TransactionBuilder, error) {
tb, err := ch.buildEscrowTx(ch.BaseSequenceNumber + 1)
if err != nil {
return nil, err
}
if ch.GuestAmount > 0 {
err = tb.Mutate(
b.Payment(
b.SourceAccount{AddressOrSeed: ch.EscrowAcct.Address()},
b.Destination{AddressOrSeed: ch.GuestAcct.Address()},
b.NativeAmount{Amount: ch.GuestAmount.HorizonString()},
),
)
if err != nil {
return nil, err
}
}
err = tb.Mutate(
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.EscrowAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.GuestRatchetAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.HostRatchetAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
)
if err != nil {
return nil, err
}
err = tb.Mutate(b.Defaults{})
return tb, err
}
func buildCleanupTx(ch *Channel, h *WalletAcct) (*b.TransactionBuilder, error) {
var seqnum xdr.SequenceNumber
if ch.FundingTimedOut {
seqnum = ch.FundingTxSeqnum
} else {
seqnum = h.Seqnum
}
return ch.buildWalletTx(
seqnum,
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.EscrowAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.HostRatchetAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
b.AccountMerge(
b.SourceAccount{AddressOrSeed: ch.GuestRatchetAcct.Address()},
b.Destination{AddressOrSeed: ch.HostAcct.Address()},
),
)
}
func buildTopUpTx(ch *Channel, h *WalletAcct) (*b.TransactionBuilder, error) {
return ch.buildWalletTx(
h.Seqnum,
b.Payment(
b.SourceAccount{AddressOrSeed: ch.HostAcct.Address()},
b.Destination{AddressOrSeed: ch.EscrowAcct.Address()},
b.NativeAmount{Amount: ch.TopUpAmount.HorizonString()},
),
)
}