summaryrefslogtreecommitdiff
path: root/test/e2e-go/features/transactions/lease_test.go
blob: 3e96cee5ccaba07b8d674d5665d5ee7e0f16451b (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package transactions

import (
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/test/framework/fixtures"
	"github.com/algorand/go-algorand/test/partitiontest"
)

func TestLeaseTransactionsSameSender(t *testing.T) {
	partitiontest.PartitionTest(t)

	t.Parallel()
	a := require.New(fixtures.SynchronizedTest(t))

	var fixture fixtures.RestClientFixture
	fixture.Setup(t, filepath.Join("nettemplates", "TwoNodes50Each.json"))
	defer fixture.Shutdown()

	client := fixture.LibGoalClient
	accountList, err := fixture.GetWalletsSortedByBalance()
	a.NoError(err)
	account0 := accountList[0].Address
	wh, err := client.GetUnencryptedWalletHandle()
	a.NoError(err)

	account1, err := client.GenerateAddress(wh)
	a.NoError(err)

	account2, err := client.GenerateAddress(wh)
	a.NoError(err)

	lease := [32]byte{1, 2, 3, 4}

	// construct transactions for sending money to account1 and account2
	// from same sender with identical lease
	tx1, err := client.ConstructPayment(account0, account1, 0, 1000000, nil, "", lease, 0, 0)
	a.NoError(err)

	tx2, err := client.ConstructPayment(account0, account2, 0, 2000000, nil, "", lease, 0, 0)
	a.NoError(err)

	stx1, err := client.SignTransactionWithWallet(wh, nil, tx1)
	a.NoError(err)

	stx2, err := client.SignTransactionWithWallet(wh, nil, tx2)
	a.NoError(err)

	// submitting the first transaction should succeed
	_, err = client.BroadcastTransaction(stx1)
	a.NoError(err)

	// submitting the second transaction should fail
	_, err = client.BroadcastTransaction(stx2)
	a.Error(err)

	// wait for the txids and check balance
	txids := make(map[string]string)
	txids[stx1.Txn.ID().String()] = account0

	_, curRound := fixture.GetBalanceAndRound(account0)
	confirmed := fixture.WaitForAllTxnsToConfirm(curRound+5, txids)
	a.True(confirmed, "lease txn confirmed")

	bal1, _ := fixture.GetBalanceAndRound(account1)
	bal2, _ := fixture.GetBalanceAndRound(account2)
	a.Equal(bal1, uint64(1000000))
	a.Equal(bal2, uint64(0))
}

func TestLeaseRegressionFaultyFirstValidCheckOld_2f3880f7(t *testing.T) {
	partitiontest.PartitionTest(t)

	t.Parallel()
	a := require.New(fixtures.SynchronizedTest(t))

	var fixture fixtures.RestClientFixture
	fixture.Setup(t, filepath.Join("nettemplates", "TwoNodes50EachV22.json"))
	defer fixture.Shutdown()

	client := fixture.LibGoalClient
	accountList, err := fixture.GetWalletsSortedByBalance()
	a.NoError(err)
	account0 := accountList[0].Address
	wh, err := client.GetUnencryptedWalletHandle()
	a.NoError(err)

	account1, err := client.GenerateAddress(wh)
	a.NoError(err)

	account2, err := client.GenerateAddress(wh)
	a.NoError(err)

	lease := [32]byte{1, 2, 3, 4}

	// construct transactions for sending money to account1 and account2
	// from same sender with identical lease
	tx1, err := client.ConstructPayment(account0, account1, 0, 1000000, nil, "", lease, 0, 0)
	a.NoError(err)

	stx1, err := client.SignTransactionWithWallet(wh, nil, tx1)
	a.NoError(err)

	// submitting the first transaction should succeed
	_, err = client.BroadcastTransaction(stx1)
	a.NoError(err)

	// wait for the txids and check balance
	txids := make(map[string]string)
	txids[stx1.Txn.ID().String()] = account0

	_, curRound := fixture.GetBalanceAndRound(account0)
	confirmed := fixture.WaitForAllTxnsToConfirm(curRound+5, txids)
	a.True(confirmed, "lease txn confirmed")

	bal1, _ := fixture.GetBalanceAndRound(account1)
	bal2, _ := fixture.GetBalanceAndRound(account2)
	a.Equal(bal1, uint64(1000000))
	a.Equal(bal2, uint64(0))

	tx2, err := client.ConstructPayment(account0, account2, 0, 2000000, nil, "", lease, 0, 0)
	a.NoError(err)

	stx2, err := client.SignTransactionWithWallet(wh, nil, tx2)
	a.NoError(err)

	// submitting the second transaction should succeed
	_, err = client.BroadcastTransaction(stx2)
	a.NoError(err)

	// wait for the txids and check balance
	txids = make(map[string]string)
	txids[stx2.Txn.ID().String()] = account0

	_, curRound = fixture.GetBalanceAndRound(account0)
	confirmed = fixture.WaitForAllTxnsToConfirm(curRound+5, txids)
	a.True(confirmed, "lease txn confirmed")

	bal1, _ = fixture.GetBalanceAndRound(account1)
	bal2, _ = fixture.GetBalanceAndRound(account2)
	a.Equal(bal1, uint64(1000000))
	a.Equal(bal2, uint64(2000000))
}

func TestLeaseRegressionFaultyFirstValidCheckNew_2f3880f7(t *testing.T) {
	partitiontest.PartitionTest(t)

	t.Parallel()
	a := require.New(fixtures.SynchronizedTest(t))

	var fixture fixtures.RestClientFixture
	fixture.Setup(t, filepath.Join("nettemplates", "TwoNodes50Each.json"))
	defer fixture.Shutdown()

	client := fixture.LibGoalClient
	accountList, err := fixture.GetWalletsSortedByBalance()
	a.NoError(err)
	account0 := accountList[0].Address
	wh, err := client.GetUnencryptedWalletHandle()
	a.NoError(err)

	account1, err := client.GenerateAddress(wh)
	a.NoError(err)

	account2, err := client.GenerateAddress(wh)
	a.NoError(err)

	lease := [32]byte{1, 2, 3, 4}

	// construct transactions for sending money to account1 and account2
	// from same sender with identical lease
	tx1, err := client.ConstructPayment(account0, account1, 0, 1000000, nil, "", lease, 0, 0)
	a.NoError(err)

	stx1, err := client.SignTransactionWithWallet(wh, nil, tx1)
	a.NoError(err)

	// submitting the first transaction should succeed
	_, err = client.BroadcastTransaction(stx1)
	a.NoError(err)

	// wait for the txids and check balance
	txids := make(map[string]string)
	txids[stx1.Txn.ID().String()] = account0

	_, curRound := fixture.GetBalanceAndRound(account0)
	confirmed := fixture.WaitForAllTxnsToConfirm(curRound+5, txids)
	a.True(confirmed, "lease txn confirmed")

	bal1, _ := fixture.GetBalanceAndRound(account1)
	bal2, _ := fixture.GetBalanceAndRound(account2)
	a.Equal(bal1, uint64(1000000))
	a.Equal(bal2, uint64(0))

	tx2, err := client.ConstructPayment(account0, account2, 0, 2000000, nil, "", lease, 0, 0)
	a.NoError(err)

	stx2, err := client.SignTransactionWithWallet(wh, nil, tx2)
	a.NoError(err)

	// submitting the second transaction should fail
	_, err = client.BroadcastTransaction(stx2)
	a.Error(err)
}

func TestLeaseTransactionsSameSenderDifferentLease(t *testing.T) {
	partitiontest.PartitionTest(t)

	t.Parallel()
	a := require.New(fixtures.SynchronizedTest(t))

	var fixture fixtures.RestClientFixture
	fixture.Setup(t, filepath.Join("nettemplates", "TwoNodes50Each.json"))
	defer fixture.Shutdown()

	client := fixture.LibGoalClient
	accountList, err := fixture.GetWalletsSortedByBalance()
	a.NoError(err)
	account0 := accountList[0].Address
	wh, err := client.GetUnencryptedWalletHandle()
	a.NoError(err)

	account1, err := client.GenerateAddress(wh)
	a.NoError(err)

	account2, err := client.GenerateAddress(wh)
	a.NoError(err)

	lease1 := [32]byte{1, 2, 3, 4}
	lease2 := [32]byte{4, 3, 2, 1}

	// construct transactions for sending money to account1 and account2
	// from same sender with different leases
	tx1, err := client.ConstructPayment(account0, account1, 0, 1000000, nil, "", lease1, 0, 0)
	a.NoError(err)

	tx2, err := client.ConstructPayment(account0, account2, 0, 2000000, nil, "", lease2, 0, 0)
	a.NoError(err)

	stx1, err := client.SignTransactionWithWallet(wh, nil, tx1)
	a.NoError(err)

	stx2, err := client.SignTransactionWithWallet(wh, nil, tx2)
	a.NoError(err)

	// submitting the first transaction should succeed
	_, err = client.BroadcastTransaction(stx1)
	a.NoError(err)

	// submitting the second transaction should succeed
	_, err = client.BroadcastTransaction(stx2)
	a.NoError(err)

	// wait for the txids and check balance
	txids := make(map[string]string)
	txids[stx1.Txn.ID().String()] = account0
	txids[stx2.Txn.ID().String()] = account0

	_, curRound := fixture.GetBalanceAndRound(account0)
	confirmed := fixture.WaitForAllTxnsToConfirm(curRound+5, txids)
	a.True(confirmed, "lease txn confirmed")

	bal1, _ := fixture.GetBalanceAndRound(account1)
	bal2, _ := fixture.GetBalanceAndRound(account2)
	a.Equal(bal1, uint64(1000000))
	a.Equal(bal2, uint64(2000000))
}

func TestLeaseTransactionsDifferentSender(t *testing.T) {
	partitiontest.PartitionTest(t)

	t.Parallel()
	a := require.New(fixtures.SynchronizedTest(t))

	var fixture fixtures.RestClientFixture
	fixture.Setup(t, filepath.Join("nettemplates", "TwoNodes50Each.json"))
	defer fixture.Shutdown()

	client := fixture.LibGoalClient
	accountList, err := fixture.GetWalletsSortedByBalance()
	a.NoError(err)
	account0 := accountList[0].Address
	wh, err := client.GetUnencryptedWalletHandle()
	a.NoError(err)

	account1, err := client.GenerateAddress(wh)
	a.NoError(err)

	account2, err := client.GenerateAddress(wh)
	a.NoError(err)

	account3, err := client.GenerateAddress(wh)
	a.NoError(err)

	// Fund account1
	tx, err := client.SendPaymentFromWallet(wh, nil, account0, account1, 0, 100000000, nil, "", 0, 0)
	a.NoError(err)

	// Wait to confirm
	txids := make(map[string]string)
	txids[tx.ID().String()] = account0
	_, curRound := fixture.GetBalanceAndRound(account0)
	confirmed := fixture.WaitForAllTxnsToConfirm(curRound+5, txids)
	a.True(confirmed, "funding txn confirmed")

	lease := [32]byte{1, 2, 3, 4}

	// construct transactions for sending money to account1 and account2
	// from different senders with identical lease
	tx1, err := client.ConstructPayment(account0, account2, 0, 1000000, nil, "", lease, 0, 0)
	a.NoError(err)

	tx2, err := client.ConstructPayment(account1, account3, 0, 2000000, nil, "", lease, 0, 0)
	a.NoError(err)

	stx1, err := client.SignTransactionWithWallet(wh, nil, tx1)
	a.NoError(err)

	stx2, err := client.SignTransactionWithWallet(wh, nil, tx2)
	a.NoError(err)

	// submitting the first transaction should succeed
	_, err = client.BroadcastTransaction(stx1)
	a.NoError(err)

	// submitting the second transaction should succeed
	_, err = client.BroadcastTransaction(stx2)
	a.NoError(err)

	// wait for the txids and check balance
	txids = make(map[string]string)
	txids[stx1.Txn.ID().String()] = account0
	txids[stx2.Txn.ID().String()] = account1

	_, curRound = fixture.GetBalanceAndRound(account0)
	confirmed = fixture.WaitForAllTxnsToConfirm(curRound+5, txids)
	a.True(confirmed, "lease txns confirmed")

	bal1, _ := fixture.GetBalanceAndRound(account2)
	bal2, _ := fixture.GetBalanceAndRound(account3)
	a.Equal(bal1, uint64(1000000))
	a.Equal(bal2, uint64(2000000))
}

func TestOverlappingLeases(t *testing.T) {
	partitiontest.PartitionTest(t)

	t.Parallel()
	a := require.New(fixtures.SynchronizedTest(t))

	var fixture fixtures.RestClientFixture
	fixture.Setup(t, filepath.Join("nettemplates", "TwoNodes50Each.json"))
	defer fixture.Shutdown()

	client := fixture.LibGoalClient
	accountList, err := fixture.GetWalletsSortedByBalance()
	a.NoError(err)
	account0 := accountList[0].Address
	wh, err := client.GetUnencryptedWalletHandle()
	a.NoError(err)

	account1, err := client.GenerateAddress(wh)
	a.NoError(err)

	account2, err := client.GenerateAddress(wh)
	a.NoError(err)

	lease := [32]byte{1, 2, 3, 4}

	_, curRound := fixture.GetBalanceAndRound(account0)
	leaseStart := curRound

	// first lease
	// [xxxxxxx]oooooooo
	// [xxxxxxxxxxxxx]oo
	// second lease

	// we will submit the first transaction, and ensure the second
	// transaction isn't valid until the first transaction's lease
	// has expired.

	const firstTxLeaseLife = 20
	const secondTxLeaseLife = 100

	// construct transactions for sending money to account1 and account2
	// from same sender with identical lease, but different, overlapping ranges
	tx1, err := client.ConstructPayment(account0, account1, 0, 1000000, nil, "", lease, basics.Round(leaseStart), basics.Round(leaseStart+firstTxLeaseLife))
	a.NoError(err)

	tx2, err := client.ConstructPayment(account0, account2, 0, 2000000, nil, "", lease, basics.Round(leaseStart), basics.Round(leaseStart+secondTxLeaseLife))
	a.NoError(err)

	stx1, err := client.SignTransactionWithWallet(wh, nil, tx1)
	a.NoError(err)

	stx2, err := client.SignTransactionWithWallet(wh, nil, tx2)
	a.NoError(err)

	// submitting the first transaction should succeed
	_, err = client.BroadcastTransaction(stx1)
	a.NoError(err)

	// submitting the second transaction should fail right away
	_, err = client.BroadcastTransaction(stx2)
	a.Error(err)

	// wait for the first tx to confirm
	txids := make(map[string]string)
	txids[stx1.Txn.ID().String()] = account0
	_, curRound = fixture.GetBalanceAndRound(account0)
	confirmed := fixture.WaitForAllTxnsToConfirm(curRound+5, txids)
	a.True(confirmed, "first lease txn confirmed")

	// submitting the second transaction should still fail
	_, err = client.BroadcastTransaction(stx2)
	a.Error(err)

	// wait for a round after the first txn was confirmed, but before its
	// lease has expired
	fixture.WaitForRoundWithTimeout(leaseStart + firstTxLeaseLife/2)

	// submitting the second transaction should still fail
	_, err = client.BroadcastTransaction(stx2)
	a.Error(err)

	// wait for us to be building leaseStart + firstTxLeaseLife + 1, where
	// the first txn's lease should have expired
	fixture.WaitForRoundWithTimeout(leaseStart + firstTxLeaseLife)

	// submitting the second transaction should succeed
	_, err = client.BroadcastTransaction(stx2)
	a.NoError(err)

	// wait for the second tx to confirm
	txids = make(map[string]string)
	txids[stx2.Txn.ID().String()] = account0
	_, curRound = fixture.GetBalanceAndRound(account0)
	confirmed = fixture.WaitForAllTxnsToConfirm(curRound+5, txids)
	a.True(confirmed, "second lease txn confirmed")
}