summaryrefslogtreecommitdiff
path: root/test/e2e-go/features/transactions/txnsync_test.go
blob: ee373ea5eea57ddc3664bdc6711f4adb1730e735 (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
// 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 (
	"context"
	"fmt"
	"math"
	"path/filepath"
	"testing"
	"time"

	"github.com/algorand/go-deadlock"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/libgoal"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/framework/fixtures"
	"github.com/algorand/go-algorand/test/partitiontest"
)

// This test sets up a network with 2 nodes and 2 relays.
// The two nodes send payment transactions.

// For each transaction, the test checks if the relays and the nodes
// (including the node that originated the transaction) have the
// transaction in the pool (i.e. the transactionInfo.ConfirmedRound ==
// 0).

// The tests needs to check for the transactions in the pool fast
// enough before they get evicted from the pool to the block.

// To achieve this, it sends transactions during the first half of the
// round period, to give the test enough time to check for the
// transactions.
func TestTxnSync(t *testing.T) {
	partitiontest.PartitionTest(t)

	maxNumberOfSends := 1200
	maxRate := 1000 // txn/sec
	if testing.Short() {
		maxNumberOfSends = 300
	}
	templatePath := filepath.Join("nettemplates", "TwoNodes50EachWithTwoRelays.json")

	var fixture fixtures.RestClientFixture

	roundTime := time.Duration(8 * 1000 * time.Millisecond)

	proto, ok := config.Consensus[protocol.ConsensusCurrentVersion]
	require.True(t, ok)
	proto.AgreementFilterTimeoutPeriod0 = roundTime
	proto.AgreementFilterTimeout = roundTime
	fixture.SetConsensus(config.ConsensusProtocols{protocol.ConsensusCurrentVersion: proto})

	fixture.Setup(t, templatePath)
	defer fixture.Shutdown()

	node1 := fixture.GetLibGoalClientForNamedNode("Node1")
	node2 := fixture.GetLibGoalClientForNamedNode("Node2")
	relay1 := fixture.GetLibGoalClientForNamedNode("Relay1")
	relay2 := fixture.GetLibGoalClientForNamedNode("Relay2")

	n1chan := make(chan string, maxNumberOfSends*2)
	n2chan := make(chan string, maxNumberOfSends*2)
	r1chan := make(chan string, maxNumberOfSends*2)
	r2chan := make(chan string, maxNumberOfSends*2)

	ctx, cancel := context.WithCancel(context.Background())

	account1List, err := fixture.GetNodeWalletsSortedByBalance(node1.DataDir())
	require.NoError(t, err)
	account1 := account1List[0].Address

	account2List, err := fixture.GetNodeWalletsSortedByBalance(node2.DataDir())
	require.NoError(t, err)
	account2 := account2List[0].Address

	ttn1 := transactionTracker{
		t:                   t,
		ctx:                 ctx,
		client:              &node1,
		othersToVerify:      []chan string{n2chan, r1chan, r2chan, n1chan},
		selfToVerify:        n1chan,
		pendingVerification: make(map[string]bool),
		account1:            account1,
		account2:            account2,
		name:                "node1",
		cancelFunc:          cancel,
	}

	ttn2 := transactionTracker{
		t:                   t,
		ctx:                 ctx,
		client:              &node2,
		othersToVerify:      []chan string{n1chan, r1chan, r2chan, n2chan},
		selfToVerify:        n2chan,
		pendingVerification: make(map[string]bool),
		account1:            account1,
		account2:            account2,
		name:                "node2",
		cancelFunc:          cancel,
	}

	ttr1 := transactionTracker{
		t:                   t,
		ctx:                 ctx,
		client:              &relay1,
		othersToVerify:      []chan string{n1chan, n2chan, r2chan, r1chan},
		selfToVerify:        r1chan,
		pendingVerification: make(map[string]bool),
		account1:            account1,
		account2:            account2,
		name:                "relay1",
		cancelFunc:          cancel,
	}

	ttr2 := transactionTracker{
		t:                   t,
		ctx:                 ctx,
		client:              &relay2,
		othersToVerify:      []chan string{n1chan, n2chan, r1chan, r2chan},
		selfToVerify:        r2chan,
		pendingVerification: make(map[string]bool),
		account1:            account1,
		account2:            account2,
		name:                "relay2",
		cancelFunc:          cancel,
	}

	minTxnFee, minAcctBalance, err := fixture.CurrentMinFeeAndBalance()
	require.NoError(t, err)

	transactionFee := minTxnFee * 1000
	amount1 := minAcctBalance / uint64(maxNumberOfSends)
	amount2 := minAcctBalance / uint64(maxNumberOfSends)

	defer ttn1.terminate()
	defer ttn2.terminate()
	defer ttr1.terminate()
	defer ttr2.terminate()

	defer cancel()

	go ttn1.passTxnsToVeirfy()
	go ttn2.passTxnsToVeirfy()
	go ttr1.passTxnsToVeirfy()
	go ttr2.passTxnsToVeirfy()

	go ttn1.checkAll()
	go ttn2.checkAll()
	go ttr1.checkAll()
	go ttr2.checkAll()

	// wait for the 1st round
	nextRound := uint64(1)
	err = fixture.ClientWaitForRound(fixture.AlgodClient, nextRound, 20*roundTime)
	require.NoError(t, err)
	nextRound++

	st := time.Now()
	timeout := time.NewTimer(roundTime / 2)

	for i := 0; i < maxNumberOfSends; i++ {

		select {
		case <-ctx.Done():
			require.True(t, false, "Context canceled due to an error at iteration %d", i)
			return
		case <-timeout.C:
			// Send the transactions only during the first half of the round
			// Wait for the next round, and stop sending transactions after the first half
			err = fixture.ClientWaitForRound(fixture.AlgodClient, nextRound, 10*roundTime)
			require.NoError(t, err)
			fmt.Printf("Round %d\n", int(nextRound))
			nextRound++
			timeout = time.NewTimer(roundTime / 2)
		default:
		}
		throttleRate(st, maxRate, i*2)
		tx1, err := node1.SendPaymentFromUnencryptedWallet(account1, account2, transactionFee, amount1, GenerateRandomBytes(8))
		require.NoError(t, err, "Failed to send transaction on iteration %d", i)
		ttn1.addTransactionToVerify(tx1.ID().String())

		tx2, err := node2.SendPaymentFromUnencryptedWallet(account2, account1, transactionFee, amount2, GenerateRandomBytes(8))
		require.NoError(t, err, "Failed to send transaction on iteration %d", i)
		ttn2.addTransactionToVerify(tx2.ID().String())
		if i%100 == 0 {
			fmt.Printf("txn sent  %d / %d\n", i, maxNumberOfSends)
		}
	}
	close(ttn1.selfToVerify)
	close(ttn2.selfToVerify)
	close(ttr1.selfToVerify)
	close(ttr2.selfToVerify)

	// wait until all channels are empty for max 50 seconds
	for x := 0; x < 250; x++ {
		select {
		case <-ctx.Done():
			require.True(t, false, "Context canceled due to an error")
		default:
		}

		if ttn1.channelsAreEmpty() {
			break
		}
		time.Sleep(200 * time.Millisecond)
		if x%10 == 0 {
			fmt.Printf("waiting for channel flushing [%d %d %d %d]  %d / %d\n", len(n1chan), len(n2chan), len(r1chan), len(r2chan), x, 250)
		}
	}
	require.True(t, ttn1.channelsAreEmpty())

	unprocessed := 0
	maxWait := 100
	for x := 0; x < maxWait; x++ {
		select {
		case <-ctx.Done():
			require.True(t, false, "Context canceled due to an error")
		default:
		}
		ttn1.mu.Lock()
		unprocessed = len(ttn1.pendingVerification)
		ttn1.mu.Unlock()

		ttn2.mu.Lock()
		unprocessed += len(ttn2.pendingVerification)
		ttn2.mu.Unlock()

		ttr1.mu.Lock()
		unprocessed += len(ttr1.pendingVerification)
		ttr1.mu.Unlock()

		ttr2.mu.Lock()
		unprocessed += len(ttr2.pendingVerification)
		ttr2.mu.Unlock()

		if unprocessed == 0 {
			break
		}
		time.Sleep(200 * time.Millisecond)
		if x%10 == 0 {
			fmt.Printf("waiting for pending verificaitons [%d] %d / %d\n", unprocessed, x, maxWait)
		}
	}
	require.Equal(t, 0, unprocessed, "missing %d transactions", unprocessed)
}

type transactionTracker struct {
	t                   *testing.T
	ctx                 context.Context
	mu                  deadlock.Mutex
	client              *libgoal.Client
	othersToVerify      []chan string
	selfToVerify        chan string
	pendingVerification map[string]bool
	account1            string
	account2            string
	name                string
	cancelFunc          context.CancelFunc
}

// Adds the transaction to the channels of the nodes intended to receive the transaction
// This should not block to maintain the transaction rate. Hence, the channel is large enough.
func (tt *transactionTracker) addTransactionToVerify(transactionID string) {
	for _, c := range tt.othersToVerify {
		select {
		case <-tt.ctx.Done():
			return
		case c <- transactionID:
		}
	}
}

func (tt *transactionTracker) passTxnsToVeirfy() {
	for tid := range tt.selfToVerify {
		select {
		case <-tt.ctx.Done():
			return
		default:
		}

		tt.mu.Lock()
		tt.pendingVerification[tid] = true
		tt.mu.Unlock()
	}
}

func (tt *transactionTracker) checkAll() {
	for {
		select {
		case <-tt.ctx.Done():
			return
		case _, more := <-tt.selfToVerify:
			tt.mu.Lock()
			if !more && len(tt.pendingVerification) == 0 {
				tt.mu.Unlock()
				return
			}
			tt.mu.Unlock()
		default:
		}
		transactions, err := tt.client.GetPendingTransactionsByAddress(tt.account1, 1000000)
		if err != nil {
			tt.cancelFunc()
			require.NoError(tt.t, err)
		}

		for _, transactionInfo := range transactions.TruncatedTxns.Transactions {
			tt.mu.Lock()
			if _, ok := tt.pendingVerification[transactionInfo.TxID]; ok {
				delete(tt.pendingVerification, transactionInfo.TxID)
			}
			tt.mu.Unlock()
		}

		transactions, err = tt.client.GetPendingTransactionsByAddress(tt.account2, 1000000)
		if err != nil {
			tt.cancelFunc()
			require.NoError(tt.t, err)
		}

		for _, transactionInfo := range transactions.TruncatedTxns.Transactions {
			tt.mu.Lock()
			if _, ok := tt.pendingVerification[transactionInfo.TxID]; ok {
				delete(tt.pendingVerification, transactionInfo.TxID)
			}
			tt.mu.Unlock()
		}
		time.Sleep(time.Second)
	}
}

func (tt *transactionTracker) terminate() {
	tt.mu.Lock()
	defer tt.mu.Unlock()
	require.Equal(tt.t, 0, len(tt.pendingVerification), "%s is missing %d transactions", tt.name, len(tt.pendingVerification))
}

// Retruns true if all the associated channels are empty
func (tt *transactionTracker) channelsAreEmpty() bool {
	for _, c := range tt.othersToVerify {
		if len(c) > 0 {
			return false
		}
	}
	return true
}

// throttle transaction rate
func throttleRate(startTime time.Time, targetRate int, total int) {
	localTimeDelta := time.Now().Sub(startTime)
	currentTps := float64(total) / localTimeDelta.Seconds()
	if currentTps > float64(targetRate) {
		sleepSec := float64(total)/float64(targetRate) - localTimeDelta.Seconds()
		sleepTime := time.Duration(int64(math.Round(sleepSec*1000))) * time.Millisecond
		time.Sleep(sleepTime)
	}
}