summaryrefslogtreecommitdiff
path: root/test/e2e-go/upgrades/stateproof_participation_test.go
blob: 5c6a6ab940d9ea03e41ad85042d6dc7c646450fe (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
// 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 upgrades

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

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/data/basics"
	"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"
	"github.com/stretchr/testify/require"
)

func waitUntilProtocolUpgrades(a *require.Assertions, fixture *fixtures.RestClientFixture, nodeClient libgoal.Client) {

	curRound, err := nodeClient.CurrentRound()
	a.NoError(err)

	blk, err := nodeClient.BookkeepingBlock(curRound)
	a.NoError(err)
	curProtocol := blk.CurrentProtocol

	startTime := time.Now()

	// while consensus version has not upgraded
	for curProtocol == consensusTestFastUpgrade(protocol.ConsensusV30) {
		curRound = curRound + 1
		fixture.WaitForRoundWithTimeout(curRound + 1)

		// TODO: check node status instead of latest block?
		blk, err := nodeClient.BookkeepingBlock(curRound)
		a.NoError(err)

		curProtocol = blk.CurrentProtocol
		if time.Now().After(startTime.Add(5 * time.Minute)) {
			a.Fail("upgrade taking too long")
		}
	}

}

func TestKeysWithoutStateProofKeyCannotRegister(t *testing.T) {
	partitiontest.PartitionTest(t)
	defer fixtures.ShutdownSynchronizedTest(t)

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

	var fixture fixtures.RestClientFixture
	fixture.SetConsensus(consensus)
	fixture.Setup(t, filepath.Join("nettemplates", "TwoNodesWithoutStateProofPartkeys.json"))
	defer fixture.Shutdown()
	lastValid := uint64(1000 * 5)

	nodeClient := fixture.GetLibGoalClientForNamedNode("Node")

	waitUntilProtocolUpgrades(a, &fixture, nodeClient)

	a.Error(registerKeyInto(&nodeClient, a, lastValid+2, protocol.ConsensusV30))
	a.NoError(registerKeyInto(&nodeClient, a, lastValid+3, protocol.ConsensusV31))
}

func TestKeysWithoutStateProofKeyCanRegister(t *testing.T) {
	partitiontest.PartitionTest(t)
	defer fixtures.ShutdownSynchronizedTest(t)

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

	var fixture fixtures.RestClientFixture
	fixture.Setup(t, filepath.Join("nettemplates", "TwoNodes50EachV30.json"))
	defer fixture.Shutdown()
	lastValid := uint64(1000 * 5)

	nodeClient := fixture.GetLibGoalClientForNamedNode("Node")

	a.NoError(registerKeyInto(&nodeClient, a, lastValid, protocol.ConsensusV30))
	a.Error(registerKeyInto(&nodeClient, a, lastValid+1, protocol.ConsensusV31))
}

func registerKeyInto(client *libgoal.Client, a *require.Assertions, lastValid uint64, ver protocol.ConsensusVersion) error {

	wh, err := client.GetUnencryptedWalletHandle()
	a.NoError(err)
	actList, err := client.ListAddresses(wh)
	a.NoError(err)
	addr := actList[0]

	pongBalance, err := client.GetBalance(addr)
	a.NoError(err)
	a.Greater(pongBalance, uint64(10000))

	partKey, _, err := client.GenParticipationKeys(addr, 1, lastValid, 1000)
	a.NoError(err)

	cparams := config.Consensus[ver]

	tx := partKey.GenerateRegistrationTransaction(
		basics.MicroAlgos{Raw: 1000},
		0,
		100,
		[32]byte{},
		cparams.EnableStateProofKeyregCheck,
	)

	if cparams.SupportGenesisHash {
		prms, err := client.SuggestedParams()
		a.NoError(err)

		var genHash crypto.Digest
		copy(genHash[:], prms.GenesisHash)
		tx.GenesisHash = genHash
	}

	_, err = client.SignAndBroadcastTransaction(wh, nil, tx)
	return err
}

func getStateProofConsensus() config.ConsensusProtocols {
	consensus := generateFastUpgradeConsensus()

	consensus[consensusTestFastUpgrade(protocol.ConsensusV30)].
		ApprovedUpgrades[consensusTestFastUpgrade(protocol.ConsensusV31)] = 0
	return consensus
}

// TODO: copied code from other test: onlineOfflineParticipation_test.go.
//
//	consider how to avoid duplication
func waitForAccountToProposeBlock(a *require.Assertions, fixture *fixtures.RestClientFixture, account string, window int) bool {
	client := fixture.AlgodClient

	curStatus, err := client.Status()
	a.NoError(err)
	curRound := curStatus.LastRound

	// the below window controls the likelihood a block will be proposed by the account under test
	// since block proposer selection is probabilistic, it is not guaranteed that the account will be chosen
	// it is a trade-off between test flakiness and test duration
	for window > 0 {
		window--
		curRound++
		err := fixture.WaitForRoundWithTimeout(curRound)
		a.NoErrorf(err, "fixture failed waiting for round %d", curRound)

		// See if account was participating by looking at block proposers
		blockWasProposed := fixture.VerifyBlockProposed(account, 1)
		if blockWasProposed {
			return blockWasProposed
		}
	}
	return false
}

// This test starts with participation keys in Version30, then attempts to let the richest user participate even after
//
//	consensus upgrade.
func TestParticipationWithoutStateProofKeys(t *testing.T) {
	partitiontest.PartitionTest(t)
	defer fixtures.ShutdownSynchronizedTest(t)

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

	consensus := getStateProofConsensus()

	var fixture fixtures.RestClientFixture
	fixture.SetConsensus(consensus)
	fixture.Setup(t, filepath.Join("nettemplates", "TwoNodesWithoutStateProofPartkeys.json"))
	defer fixture.Shutdown()
	// Want someone to start with a key to participate with...

	act, err := fixture.GetRichestAccount()
	a.NoError(err)

	var address = act.Address

	nodeClient := fixture.GetLibGoalClientForNamedNode("Node")
	waitUntilProtocolUpgrades(a, &fixture, nodeClient)

	a.NotEmpty(address)

	proposalWindow := 50 // giving 50 rounds to participate, should be able to participate every second round.
	blockWasProposedByPartkeyOnlyAccountRecently := waitForAccountToProposeBlock(a, &fixture, address, proposalWindow)
	a.True(blockWasProposedByPartkeyOnlyAccountRecently, "partkey-only account should be proposing blocks")
}