summaryrefslogtreecommitdiff
path: root/test/e2e-go/features/transactions/sendReceive_test.go
blob: a04b86dd7a5161fbb2ff78643f87e3393d148a35 (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
// 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 (
	"fmt"
	"math/rand"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/config"
	v1 "github.com/algorand/go-algorand/daemon/algod/api/spec/v1"
	"github.com/algorand/go-algorand/test/framework/fixtures"
	"github.com/algorand/go-algorand/test/partitiontest"
)

func GenerateRandomBytes(n int) []byte {
	b := make([]byte, n)
	_, err := rand.Read(b)
	// Note that err == nil only if we read len(b) bytes.
	if err != nil {
		return nil
	}

	return b
}

// this test checks that two accounts' balances stay up to date
// as they send each other money many times
func TestAccountsCanSendMoney(t *testing.T) {
	partitiontest.PartitionTest(t)
	defer fixtures.ShutdownSynchronizedTest(t)

	numberOfSends := 25
	if testing.Short() {
		numberOfSends = 3
	}
	testAccountsCanSendMoney(t, filepath.Join("nettemplates", "TwoNodes50Each.json"), numberOfSends)
}

// this test checks that two accounts' balances stay up to date
// as they send each other money many times
func TestDevModeAccountsCanSendMoney(t *testing.T) {
	defer fixtures.ShutdownSynchronizedTest(t)

	numberOfSends := 25
	if testing.Short() {
		numberOfSends = 3
	}
	testAccountsCanSendMoney(t, filepath.Join("nettemplates", "DevModeNetwork.json"), numberOfSends)
}

func testAccountsCanSendMoney(t *testing.T, templatePath string, numberOfSends int) {
	t.Parallel()

	var fixture fixtures.RestClientFixture
	fixture.Setup(t, templatePath)
	defer fixture.Shutdown()
	testAccountsCanSendMoneyFixture(t, &fixture, numberOfSends)
}

func testAccountsCanSendMoneyFixture(t *testing.T, fixture *fixtures.RestClientFixture, numberOfSends int) {
	a := require.New(fixtures.SynchronizedTest(t))

	c := fixture.LibGoalClient

	pingClient := fixture.LibGoalClient
	pingAccountList, err := fixture.GetWalletsSortedByBalance()
	a.NoError(err, "fixture should be able to get wallets sorted by balance")
	pingAccount := pingAccountList[0].Address

	pongClient := fixture.GetLibGoalClientForNamedNode("Node")
	pongAccounts, err := fixture.GetNodeWalletsSortedByBalance(pongClient.DataDir())
	a.NoError(err)
	var pongAccount string
	for _, acct := range pongAccounts {
		if acct.Address == pingAccount {
			continue
		}
		// we found an account.
		pongAccount = acct.Address
		break
	}

	pingBalance, err := c.GetBalance(pingAccount)
	pongBalance, err := c.GetBalance(pongAccount)

	a.Equal(pingBalance, pongBalance, "both accounts should start with same balance")
	a.NotEqual(pingAccount, pongAccount, "accounts under study should be different")

	expectedPingBalance := pingBalance
	expectedPongBalance := pongBalance

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

	transactionFee := minTxnFee + 5
	amountPongSendsPing := minAcctBalance
	amountPingSendsPong := minAcctBalance * 3 / 2

	pongTxidsToAddresses := make(map[string]string)
	pingTxidsToAddresses := make(map[string]string)

	waitForTransaction := false

	for i := 0; i < numberOfSends; i++ {
		pongTx, err := pongClient.SendPaymentFromUnencryptedWallet(pongAccount, pingAccount, transactionFee, amountPongSendsPing, GenerateRandomBytes(8))
		pongTxidsToAddresses[pongTx.ID().String()] = pongAccount
		a.NoError(err, "fixture should be able to send money (pong -> ping), error on send number %v", i)
		pingTx, err := pingClient.SendPaymentFromUnencryptedWallet(pingAccount, pongAccount, transactionFee, amountPingSendsPong, GenerateRandomBytes(8))
		pingTxidsToAddresses[pingTx.ID().String()] = pingAccount
		a.NoError(err, "fixture should be able to send money (ping -> pong), error on send number %v", i)
		expectedPingBalance = expectedPingBalance - transactionFee - amountPingSendsPong + amountPongSendsPing
		expectedPongBalance = expectedPongBalance - transactionFee - amountPongSendsPing + amountPingSendsPong

		var pongTxInfo, pingTxInfo v1.Transaction
		pongTxInfo, err = pingClient.PendingTransactionInformation(pongTx.ID().String())
		if err == nil {
			pingTxInfo, err = pingClient.PendingTransactionInformation(pingTx.ID().String())
		}
		waitForTransaction = err != nil || pongTxInfo.ConfirmedRound == 0 || pingTxInfo.ConfirmedRound == 0

		if waitForTransaction {
			curStatus, _ := pongClient.Status()
			curRound := curStatus.LastRound
			err = fixture.WaitForRoundWithTimeout(curRound + uint64(1))
			a.NoError(err)
		}
	}
	curStatus, _ := pongClient.Status()
	curRound := curStatus.LastRound

	if waitForTransaction {
		fixture.AlgodClient = fixture.GetAlgodClientForController(fixture.GetNodeControllerForDataDir(pongClient.DataDir()))
		fixture.WaitForAllTxnsToConfirm(curRound+uint64(5), pingTxidsToAddresses)
	}

	pingBalance, _ = fixture.GetBalanceAndRound(pingAccount)
	pongBalance, _ = fixture.GetBalanceAndRound(pongAccount)
	a.True(expectedPingBalance <= pingBalance, "ping balance is different than expected.")
	a.True(expectedPongBalance <= pongBalance, "pong balance is different than expected.")

	if waitForTransaction {
		fixture.AlgodClient = fixture.GetAlgodClientForController(fixture.GetNodeControllerForDataDir(pingClient.DataDir()))
		fixture.WaitForAllTxnsToConfirm(curRound+uint64(5), pongTxidsToAddresses)
	}

	pingBalance, _ = fixture.GetBalanceAndRound(pingAccount)
	pongBalance, _ = fixture.GetBalanceAndRound(pongAccount)
	a.True(expectedPingBalance <= pingBalance, "ping balance is different than expected.")
	a.True(expectedPongBalance <= pongBalance, "pong balance is different than expected.")
}

// this test checks that two accounts' balances stay up to date
// as they send each other money many times
func TestAccountsCanSendMoneyAcrossTxSync(t *testing.T) {
	partitiontest.PartitionTest(t)
	defer fixtures.ShutdownSynchronizedTest(t)

	numberOfSends := 3
	a := require.New(fixtures.SynchronizedTest(t))

	networkProtocolVersions := []string{"3.0", "2.1"}

	testMoneySending := func(t *testing.T, primaryNodeVersionIdx, nodeVersionIdx int) {
		t.Run(fmt.Sprintf("%s->%s", networkProtocolVersions[primaryNodeVersionIdx], networkProtocolVersions[nodeVersionIdx]),
			func(t *testing.T) {
				t.Parallel()
				var fixture fixtures.RestClientFixture
				fixture.SetupNoStart(t, filepath.Join("nettemplates", "TwoNodes50Each.json"))
				defer fixture.Shutdown()

				cfg, err := config.LoadConfigFromDisk(fixture.PrimaryDataDir())
				a.NoError(err)
				cfg.NetworkProtocolVersion = networkProtocolVersions[primaryNodeVersionIdx]
				cfg.SaveToDisk(fixture.PrimaryDataDir())

				cfg, err = config.LoadConfigFromDisk(fixture.NodeDataDirs()[0])
				a.NoError(err)
				cfg.NetworkProtocolVersion = networkProtocolVersions[primaryNodeVersionIdx]
				cfg.SaveToDisk(fixture.NodeDataDirs()[0])

				fixture.Start()
				testAccountsCanSendMoneyFixture(t, &fixture, numberOfSends)
			})
	}

	// test to see that we can communicate correctly regardless of the network protocol version.
	for primaryNodeVersionIdx := 0; primaryNodeVersionIdx < 2; primaryNodeVersionIdx++ {
		for nodeVersionIdx := 0; nodeVersionIdx < 2; nodeVersionIdx++ {
			testMoneySending(t, primaryNodeVersionIdx, nodeVersionIdx)
		}
	}
}

// this test checks that a relay would relay a transaction
// received via the txnsync onto a node that doesn't support
// transaction sync.
func TestTransactionSyncRelayBridge(t *testing.T) {

	partitiontest.PartitionTest(t)
	defer fixtures.ShutdownSynchronizedTest(t)

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

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

	onlineNodeController, err := fixture.GetNodeController("OnlineNode")
	a.NoError(err)

	cfg, err := config.LoadConfigFromDisk(onlineNodeController.GetDataDir())
	a.NoError(err)
	cfg.NetworkProtocolVersion = "2.1"
	cfg.SaveToDisk(onlineNodeController.GetDataDir())

	offlineNodeController, err := fixture.GetNodeController("OfflineNode")
	a.NoError(err)

	cfg, err = config.LoadConfigFromDisk(offlineNodeController.GetDataDir())
	a.NoError(err)
	cfg.NetworkProtocolVersion = "3.0"
	cfg.SaveToDisk(offlineNodeController.GetDataDir())

	fixture.Start()

	client := fixture.GetLibGoalClientFromNodeController(offlineNodeController)
	accounts, err := fixture.GetNodeWalletsSortedByBalance(client.DataDir())
	a.NoError(err)

	a.Equal(1, len(accounts))

	sendingAccount := accounts[0].Address

	_, err = client.SendPaymentFromUnencryptedWallet(sendingAccount, sendingAccount, 1024*1024, 1024, nil)
	a.NoError(err)

	startRoundStatus, err := client.Status()
	a.NoError(err)
	for {
		pendingTxns, err := client.GetPendingTransactions(2)
		a.NoError(err)
		if pendingTxns.TotalTxns == 0 {
			break
		}
		status, err := client.Status()
		a.NoError(err)
		_, err = client.WaitForRound(status.LastRound)
		a.NoError(err)
		a.Less(uint64(status.LastRound), uint64(startRoundStatus.LastRound+5), "transaction is still pending after 5 rounds, whereas it should have been confirmed within 2 rounds.")
	}
}