summaryrefslogtreecommitdiff
path: root/test/e2e-go/features/participation/participationExpiration_test.go
blob: 5cb4f941dc9478c5e84425d19fb91d48f114de7d (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
// Copyright (C) 2019-2024 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 participation

import (
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

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

func testExpirationAccounts(t *testing.T, fixture *fixtures.RestClientFixture, finalStatus basics.Status, protocolCheck string, includeStateProofs bool) {

	a := require.New(fixtures.SynchronizedTest(t))
	pClient := fixture.GetLibGoalClientForNamedNode("Primary")

	sClient := fixture.GetLibGoalClientForNamedNode("Secondary")
	sWH, err := sClient.GetUnencryptedWalletHandle()
	a.NoError(err)
	sAccount, err := sClient.GenerateAddress(sWH)
	a.NoError(err)

	// send money to new account from some other account in the template, so that account can go online
	accountList, err := fixture.GetWalletsSortedByBalance()
	a.NoError(err)
	richAccount := accountList[0].Address
	latestRound := fetchLatestRound(fixture, a)

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

	transactionFee := minTxnFee
	amountToSendInitial := 5 * minAcctBalance

	initialAmt, err := sClient.GetBalance(sAccount)
	a.NoError(err)

	fixture.SendMoneyAndWait(latestRound, amountToSendInitial, transactionFee, richAccount, sAccount, "")

	newAmt, err := sClient.GetBalance(sAccount)
	a.NoError(err)

	a.GreaterOrEqual(newAmt, initialAmt)

	newAccountStatus, err := pClient.AccountInformation(sAccount, false)
	a.NoError(err)
	a.Equal(basics.Offline.String(), newAccountStatus.Status)

	var onlineTxID string
	var partKeyLastValid uint64

	startTime := time.Now()
	for time.Since(startTime) < 2*time.Minute {
		currentRound := fetchLatestRound(fixture, a)

		// account adds part key
		partKeyFirstValid := uint64(0)
		partKeyValidityPeriod := uint64(10)
		partKeyLastValid = currentRound + partKeyValidityPeriod
		partkeyResponse, _, err := sClient.GenParticipationKeys(sAccount, partKeyFirstValid, partKeyLastValid, 0)
		a.NoError(err)
		a.Equal(sAccount, partkeyResponse.Parent.String())

		// account uses part key to go online
		goOnlineTx, err := sClient.MakeRegistrationTransactionWithGenesisID(partkeyResponse, transactionFee, 0, 0, [32]byte{}, includeStateProofs)
		a.NoError(err)

		a.Equal(sAccount, goOnlineTx.Src().String())
		onlineTxID, err = sClient.SignAndBroadcastTransaction(sWH, nil, goOnlineTx)

		if err == nil {
			break
		}

		if strings.Contains(err.Error(), "transaction tries to mark an account as online with last voting round in the past") {
			continue
		}

		// Error occurred
		logging.TestingLog(t).Errorf("signAndBroadcastTransaction error: %s", err.Error())
		logging.TestingLog(t).Errorf("first valid: %d, last valid: %d, current round: %d", partKeyFirstValid, partKeyLastValid, currentRound)
		a.NoError(err)
	}

	fixture.AssertValidTxid(onlineTxID)
	maxRoundsToWaitForTxnConfirm := uint64(3)

	sNodeStatus, err := sClient.Status()
	a.NoError(err)
	seededRound := sNodeStatus.LastRound

	txnConfirmed := fixture.WaitForTxnConfirmation(seededRound+maxRoundsToWaitForTxnConfirm, sAccount, onlineTxID)
	a.True(txnConfirmed)

	newAccountStatus, err = pClient.AccountInformation(sAccount, false)
	a.NoError(err)
	a.Equal(basics.Online.String(), newAccountStatus.Status)

	// get the round number of the primary node
	pNodeStatus, err := pClient.Status()
	a.NoError(err)

	// ensure the secondary node reaches that number
	_, err = sClient.WaitForRound(pNodeStatus.LastRound)
	a.NoError(err)

	// get the account data ( which now is syncronized across the network )
	sAccountData, err := sClient.AccountData(sAccount)
	a.NoError(err)
	lastValidRound := sAccountData.VoteLastValid

	a.Equal(basics.Round(partKeyLastValid), lastValidRound)

	// We want to wait until we get to one round past the last valid round
	err = fixture.WaitForRoundWithTimeout(uint64(lastValidRound) + 1)
	newAccountStatus, err = pClient.AccountInformation(sAccount, false)
	a.NoError(err)

	// The account should be online still...
	a.Equal(basics.Online.String(), newAccountStatus.Status)

	// Now we want to send a transaction to the account and test that
	// it was taken offline after we sent it something

	latestRound = fetchLatestRound(fixture, a)

	// making certain sClient has the same blocks as pClient.
	_, err = sClient.WaitForRound(uint64(lastValidRound + 1))
	a.NoError(err)

	blk, err := sClient.BookkeepingBlock(latestRound)
	a.NoError(err)
	a.Equal(string(blk.CurrentProtocol), protocolCheck)

	sendMoneyTxn := fixture.SendMoneyAndWait(latestRound, amountToSendInitial, transactionFee, richAccount, sAccount, "")

	txnConfirmed = fixture.WaitForTxnConfirmation(latestRound+maxRoundsToWaitForTxnConfirm, sAccount, sendMoneyTxn.Txn.ID().String())
	a.True(txnConfirmed)

	newAccountStatus, err = pClient.AccountInformation(sAccount, false)
	a.NoError(err)

	// The account should be equal to the target status now
	a.Equal(finalStatus.String(), newAccountStatus.Status)
}

func fetchLatestRound(fixture *fixtures.RestClientFixture, a *require.Assertions) uint64 {
	status, err := fixture.LibGoalClient.Status()
	a.NoError(err)
	return status.LastRound
}

// TestParticipationAccountsExpirationFuture tests that sending a transaction to an account with
// its last valid round being less than the current round will turn it offline.  This test will only
// work when the consensus protocol enables it (in this case the future protocol)
func TestParticipationAccountsExpirationFuture(t *testing.T) {
	partitiontest.PartitionTest(t)

	if testing.Short() {
		t.Skip()
	}

	t.Parallel()

	var fixture fixtures.RestClientFixture

	fixture.SetupNoStart(t, filepath.Join("nettemplates", "TwoNodesExpiredOfflineVFuture.json"))

	fixture.Start()
	defer fixture.Shutdown()

	testExpirationAccounts(t, &fixture, basics.Offline, "future", true)
}

// TestParticipationAccountsExpirationNonFuture tests that sending a transaction to an account with
// its last valid round being less than the current round will NOT turn it offline.  This tests that
// when the consensus protocol is less than the required version, it will not turn nodes offline
func TestParticipationAccountsExpirationNonFuture(t *testing.T) {
	partitiontest.PartitionTest(t)

	if testing.Short() {
		t.Skip()
	}

	t.Parallel()

	var fixture fixtures.RestClientFixture

	// V29 is the version before participation key expiration checking was enabled
	fixture.SetupNoStart(t, filepath.Join("nettemplates", "TwoNodesExpiredOfflineV29.json"))

	fixture.Start()
	defer fixture.Shutdown()

	testExpirationAccounts(t, &fixture, basics.Online, string(protocol.ConsensusV29), false)
}