summaryrefslogtreecommitdiff
path: root/agreement/keyManager_test.go
blob: f9337e26d735cad4f6d488aa8f93b83340305120 (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
// 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 agreement

import (
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-deadlock"

	"github.com/algorand/go-algorand/data/account"
	"github.com/algorand/go-algorand/data/basics"
)

func makeRecordingKeyManager(accounts []account.Participation) *recordingKeyManager {
	return &recordingKeyManager{
		keys:      accounts,
		recording: make(map[basics.Address]map[account.ParticipationAction]basics.Round),
	}
}

// recordingKeyManager provides a simple implementation of a KeyManager for unit tests.
type recordingKeyManager struct {
	keys      []account.Participation
	recording map[basics.Address]map[account.ParticipationAction]basics.Round
	mutex     deadlock.Mutex
}

// VotingKeys implements KeyManager.VotingKeys.
func (m *recordingKeyManager) VotingKeys(votingRound, _ basics.Round) []account.ParticipationRecordForRound {
	var km []account.ParticipationRecordForRound
	for _, acc := range m.keys {
		if acc.OverlapsInterval(votingRound, votingRound) {
			partRecordForRound := account.ParticipationRecordForRound{
				ParticipationRecord: account.ParticipationRecord{
					ParticipationID:   acc.ID(),
					Account:           acc.Parent,
					FirstValid:        acc.FirstValid,
					LastValid:         acc.LastValid,
					KeyDilution:       acc.KeyDilution,
					LastVote:          0,
					LastBlockProposal: 0,
					LastStateProof:    0,
					EffectiveFirst:    0,
					EffectiveLast:     acc.LastValid,
					VRF:               acc.VRF,
					Voting:            acc.Voting,
				},
			}
			km = append(km, partRecordForRound)
		}
	}
	return km
}

// DeleteOldKeys implements KeyManager.DeleteOldKeys.
func (m *recordingKeyManager) DeleteOldKeys(r basics.Round) {
}

// Record implements KeyManager.Record.
func (m *recordingKeyManager) Record(acct basics.Address, round basics.Round, action account.ParticipationAction) {
	m.mutex.Lock()
	defer m.mutex.Unlock()
	if _, ok := m.recording[acct]; !ok {
		m.recording[acct] = make(map[account.ParticipationAction]basics.Round)
	}
	m.recording[acct][action] = round
}

// ValidateVoteRound requires that the given address voted on a particular round.
func (m *recordingKeyManager) ValidateVoteRound(t *testing.T, address basics.Address, round basics.Round) {
	m.mutex.Lock()
	require.Equal(t, round, m.recording[address][account.Vote])
	require.Equal(t, round, m.recording[address][account.BlockProposal])
	m.mutex.Unlock()
}