summaryrefslogtreecommitdiff
path: root/libgoal/participation/participation_test.go
blob: 69565a780007b62e54b90a63229e8bcf24b28518 (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
// Copyright (C) 2019-2023 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 (
	"github.com/algorand/go-algorand/data/account"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/test/partitiontest"

	"github.com/stretchr/testify/require"

	"testing"
)

func TestGenParticipationKeysTo_Install(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	testcases := []struct {
		name      string
		outDir    string
		installed bool
	}{
		{
			name:      "install",
			installed: true,
		},
		{
			name:      "do not install",
			outDir:    t.TempDir(),
			installed: false,
		},
	}

	for _, tc := range testcases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			var err error
			var called bool
			installFunc := func(keyPath string) error {
				called = true
				return nil
			}
			var addr basics.Address
			addr[1] = 1

			_, _, err = GenParticipationKeysTo(addr.String(), 1000, 2000, 0, tc.outDir, installFunc)
			require.NoError(t, err)
			require.Equal(t, tc.installed, called, "The install function should only be called when outDir is not set.")
		})
	}
}

func TestGenParticipationKeysTo_DefaultKeyDilution(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	var addr basics.Address
	addr[1] = 1
	first := uint64(1000)
	last := uint64(2000)

	testcases := []struct {
		name     string
		dilution uint64
		expected uint64
	}{
		{
			name:     "default",
			dilution: 0,
			expected: account.DefaultKeyDilution(basics.Round(first), basics.Round(last)),
		}, {
			name:     "override",
			dilution: 5,
			expected: 5,
		},
	}

	for _, tc := range testcases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			part, _, err := GenParticipationKeysTo(addr.String(), first, last, tc.dilution, t.TempDir(), nil)
			require.NoError(t, err)
			require.Equal(t, tc.expected, part.KeyDilution)
		})
	}
}

func TestBadInput(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	_, _, err := GenParticipationKeysTo("", 0, 0, 0, "", nil)
	require.ErrorContains(t, err, "must provide an install function when installing keys")
}