summaryrefslogtreecommitdiff
path: root/ledger/store/trackerdb/testsuite/stateproofs_kv_test.go
blob: bc42141735e2bb75970f25e2a20c0d63687dc7e8 (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
// 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 testsuite

import (
	"context"

	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/ledger/ledgercore"
	"github.com/algorand/go-algorand/ledger/store/trackerdb"
	"github.com/stretchr/testify/require"
)

func init() {
	// register tests that will run on each KV implementation
	registerTest("stateproofs-crud", CustomTestStateproofsReadWrite)
	registerTest("stateproofs-query-all", CustomTestStateproofsQueryAll)
}

func CustomTestStateproofsReadWrite(t *customT) {
	spw := t.db.MakeSpVerificationCtxWriter()
	spr := t.db.MakeSpVerificationCtxReader()

	//
	// test
	//

	// store no items
	err := spw.StoreSPContexts(context.Background(), []*ledgercore.StateProofVerificationContext{})
	require.NoError(t, err)

	// store some items
	vcs := []*ledgercore.StateProofVerificationContext{
		{
			LastAttestedRound: basics.Round(0),
			OnlineTotalWeight: basics.MicroAlgos{Raw: 42},
		},
		{
			LastAttestedRound: basics.Round(1),
			OnlineTotalWeight: basics.MicroAlgos{Raw: 100},
		},
		{
			LastAttestedRound: basics.Round(2),
			OnlineTotalWeight: basics.MicroAlgos{Raw: 200},
		},
	}
	err = spw.StoreSPContexts(context.Background(), vcs)
	require.NoError(t, err)

	// read non-existing item
	vc, err := spr.LookupSPContext(basics.Round(9000))
	require.Error(t, err)
	require.Equal(t, trackerdb.ErrNotFound, err)

	// read back a single item
	vc, err = spr.LookupSPContext(basics.Round(0))
	require.NoError(t, err)
	require.Equal(t, basics.Round(0), vc.LastAttestedRound)            // check round is set
	require.Equal(t, basics.MicroAlgos{Raw: 42}, vc.OnlineTotalWeight) // check payload is read

	// delete some items
	err = spw.DeleteOldSPContexts(context.Background(), basics.Round(1))
	require.NoError(t, err)

	// read delete items
	vc, err = spr.LookupSPContext(basics.Round(0))
	require.Error(t, err)
	require.Equal(t, trackerdb.ErrNotFound, err)

	// read back remaining items
	vc, err = spr.LookupSPContext(basics.Round(1))
	require.NoError(t, err)
	require.Equal(t, basics.Round(1), vc.LastAttestedRound)             // check round is set
	require.Equal(t, basics.MicroAlgos{Raw: 100}, vc.OnlineTotalWeight) // check payload is read

	// read back remaining items
	vc, err = spr.LookupSPContext(basics.Round(2))
	require.NoError(t, err)
	require.Equal(t, basics.Round(2), vc.LastAttestedRound)             // check round is set
	require.Equal(t, basics.MicroAlgos{Raw: 200}, vc.OnlineTotalWeight) // check payload is read
}

func CustomTestStateproofsQueryAll(t *customT) {
	spw := t.db.MakeSpVerificationCtxWriter()
	spr := t.db.MakeSpVerificationCtxReader()

	// prepare the test with some data
	// store some items
	vcs := []*ledgercore.StateProofVerificationContext{
		{
			LastAttestedRound: basics.Round(0),
			OnlineTotalWeight: basics.MicroAlgos{Raw: 42},
		},
		{
			LastAttestedRound: basics.Round(1),
			OnlineTotalWeight: basics.MicroAlgos{Raw: 100},
		},
		{
			LastAttestedRound: basics.Round(2),
			OnlineTotalWeight: basics.MicroAlgos{Raw: 200},
		},
	}
	err := spw.StoreSPContexts(context.Background(), vcs)
	require.NoError(t, err)

	//
	// test
	//

	// read all data
	result, err := spr.GetAllSPContexts(context.Background())
	require.NoError(t, err)
	require.Len(t, result, 3)                                      // check all items are present
	require.Equal(t, basics.Round(0), result[0].LastAttestedRound) // check first item
	require.Equal(t, basics.Round(2), result[2].LastAttestedRound) // check last item
}