summaryrefslogtreecommitdiff
path: root/ledger/store/trackerdb/testsuite/migration_test.go
blob: 10d9c74737e0a1050d7a03f981453b4b00ca2339 (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
// 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/crypto"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/ledger/ledgercore"
	"github.com/algorand/go-algorand/ledger/store/trackerdb"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/protocol"
	"github.com/stretchr/testify/require"
)

func init() {
	// register tests that will run on each KV implementation
	registerTest("db-migration-check-basic", CustomTestChecBasicMigration)
	// Disabled since it's technically broken the way its written.
	// registerTest("db-migration-check-with-accounts", CustomTestCheckMigrationWithAccounts)
}

func CustomTestChecBasicMigration(t *customT) {
	ar, err := t.db.MakeAccountsReader()
	require.NoError(t, err)

	// check round
	round, err := ar.AccountsRound()
	require.NoError(t, err)
	require.Equal(t, basics.Round(0), round) // initialized to round 0

	// check account totals
	totals, err := ar.AccountsTotals(context.Background(), false)
	require.NoError(t, err)
	require.Equal(t, uint64(0), totals.RewardsLevel)
	require.Equal(t, ledgercore.AlgoCount{}, totals.Online)
	require.Equal(t, ledgercore.AlgoCount{}, totals.Offline)
	require.Equal(t, ledgercore.AlgoCount{}, totals.NotParticipating)

	// check tx-tails
	txTailData, hashes, baseRound, err := ar.LoadTxTail(context.Background(), basics.Round(0))
	require.NoError(t, err)
	require.Len(t, txTailData, 0)                // no data
	require.Len(t, hashes, 0)                    // no data
	require.Equal(t, basics.Round(1), baseRound) // (the impls return +1 at the end)

	// check online accounts
	oas, err := ar.OnlineAccountsAll(99)
	require.NoError(t, err)
	require.Len(t, oas, 0)

	// check online round params
	oparams, endRound, err := ar.AccountsOnlineRoundParams()
	require.NoError(t, err)
	require.Len(t, oparams, 1)
	require.Equal(t, basics.Round(0), endRound)
	require.Equal(t, uint64(0), oparams[0].OnlineSupply)
	require.Equal(t, uint64(0), oparams[0].RewardsLevel)
	require.Equal(t, protocol.ConsensusCurrentVersion, oparams[0].CurrentProtocol)
}

func makeAccountData(status basics.Status, algos basics.MicroAlgos) basics.AccountData {
	ad := basics.AccountData{Status: status, MicroAlgos: algos}
	if status == basics.Online {
		ad.VoteFirstValid = 1
		ad.VoteLastValid = 100_000
	}
	return ad
}

func CustomTestCheckMigrationWithAccounts(t *customT) {
	aw, err := t.db.MakeAccountsWriter()
	require.NoError(t, err)

	ar, err := t.db.MakeAccountsReader()
	require.NoError(t, err)

	// reset
	aw.AccountsReset(context.Background())

	initAccounts := make(map[basics.Address]basics.AccountData)

	addrA := basics.Address(crypto.Hash([]byte("a")))
	initAccounts[addrA] = makeAccountData(basics.Online, basics.MicroAlgos{Raw: 100})

	addrB := basics.Address(crypto.Hash([]byte("b")))
	initAccounts[addrB] = makeAccountData(basics.Online, basics.MicroAlgos{Raw: 42})

	addrC := basics.Address(crypto.Hash([]byte("c")))
	initAccounts[addrC] = makeAccountData(basics.Offline, basics.MicroAlgos{Raw: 30})

	addrD := basics.Address(crypto.Hash([]byte("d")))
	initAccounts[addrD] = makeAccountData(basics.NotParticipating, basics.MicroAlgos{Raw: 7})

	params := trackerdb.Params{
		InitProto:    protocol.ConsensusCurrentVersion,
		InitAccounts: initAccounts,
	}

	// re-run migrations
	_, err = t.db.RunMigrations(context.Background(), params, logging.TestingLog(t), trackerdb.AccountDBVersion)
	require.NoError(t, err)

	// check account totals
	totals, err := ar.AccountsTotals(context.Background(), false)
	require.NoError(t, err)
	require.Equal(t, uint64(0), totals.RewardsLevel)
	require.Equal(t, ledgercore.AlgoCount{Money: basics.MicroAlgos{Raw: 142}}, totals.Online)
	require.Equal(t, ledgercore.AlgoCount{Money: basics.MicroAlgos{Raw: 30}}, totals.Offline)
	require.Equal(t, ledgercore.AlgoCount{Money: basics.MicroAlgos{Raw: 7}}, totals.NotParticipating)

	// check online accounts
	oas, err := ar.OnlineAccountsAll(99)
	require.NoError(t, err)
	require.Len(t, oas, 2)

	// check online round params
	oparams, endRound, err := ar.AccountsOnlineRoundParams()
	require.NoError(t, err)
	require.Len(t, oparams, 1)
	require.Equal(t, basics.Round(0), endRound)
	require.Equal(t, uint64(142), oparams[0].OnlineSupply)
	require.Equal(t, uint64(0), oparams[0].RewardsLevel)
	require.Equal(t, protocol.ConsensusCurrentVersion, oparams[0].CurrentProtocol)
}