summaryrefslogtreecommitdiff
path: root/ledger/store/trackerdb/generickv/accounts_ext_writer.go
blob: b50a5363f1ec2393d8170c3f5cf4ec2f75cd1072 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// 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 generickv

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/algorand/go-algorand/protocol"
)

func (w *accountsWriter) AccountsReset(ctx context.Context) error {
	// TODO: catchpoint
	return nil
}

func (w *accountsWriter) ResetAccountHashes(ctx context.Context) (err error) {
	// TODO: catchpoint
	return
}

func (w *accountsWriter) TxtailNewRound(ctx context.Context, baseRound basics.Round, roundData [][]byte, forgetBeforeRound basics.Round) error {
	// The SQL at the time fo writing:
	//
	// for i, data := range roundData:
	// 		the inserted rnd value is baseRound + i
	//
	// 		INSERT INTO txtail(rnd, data) VALUES(?, ?)
	//
	// then it also cleans up everything before `forgetBeforeRound`:
	//
	// DELETE FROM txtail WHERE rnd < ?

	// insert the new txTail's
	for i, data := range roundData {
		rnd := basics.Round(int(baseRound) + i)
		key := txTailKey(rnd)
		err := w.kvw.Set(key[:], data)
		if err != nil {
			return err
		}
	}

	// delete old ones
	start, end := txTailRoundRangePrefix(forgetBeforeRound)
	err := w.kvw.DeleteRange(start[:], end[:])
	if err != nil {
		return err
	}

	return nil
}

func (w *accountsWriter) UpdateAccountsRound(rnd basics.Round) (err error) {
	// The SQL at the time of writing:
	//
	// UPDATE acctrounds SET rnd=? WHERE id='acctbase' AND rnd<?",

	// TODO: read the row for sanity? wont help the kv with race conditions, but we will need it for test parity
	//       inside a batch we wont have a read ptr..

	// write round entry
	raw := bigEndianUint64(uint64(rnd))
	key := roundKey()
	err = w.kvw.Set(key[:], raw[:])
	if err != nil {
		return err
	}

	return nil
}

func (w *accountsWriter) UpdateAccountsHashRound(ctx context.Context, hashRound basics.Round) (err error) {
	// TODO: catchpoint
	return nil
}

func (w *accountsWriter) AccountsPutTotals(totals ledgercore.AccountTotals, catchpointStaging bool) (err error) {
	// The SQL at the time of impl:
	//
	// id := ""
	// if catchpointStaging {
	// 	id = "catchpointStaging"
	// }
	// "REPLACE INTO accounttotals
	//		(id, online, onlinerewardunits, offline, offlinerewardunits, notparticipating, notparticipatingrewardunits, rewardslevel)
	// VALUES (?, ?, ?, ?, ?, ?, ?, ?)"

	// write totals entry
	raw := protocol.Encode(&totals)
	key := totalsKey(catchpointStaging)
	err = w.kvw.Set(key[:], raw)
	if err != nil {
		return err
	}

	return nil
}

func (w *accountsWriter) OnlineAccountsDelete(forgetBefore basics.Round) (err error) {
	// The SQL at the time of impl:
	//
	// SELECT
	//		rowid, address, updRound, data
	// FROM onlineaccounts
	// WHERE updRound < ?
	// ORDER BY address, updRound DESC
	//
	// The it would delete by  rowid in chunks with:
	//
	// 		DELETE FROM onlineaccounts WHERE rowid IN (..)

	// On the KV implmentation:
	//
	// We have two ranges of keys associated with online accounts:
	// - the `onlineAccountKey(address, round)` -> "-".join(kvPrefixOnlineAccount, addr, round)
	// - and the `onlineAccountBalanceKey(round, normBalance, addr) -> "-".join(kvPrefixOnlineAccountBalance, round, normBalance, addr)

	// 1. read from the `onlineAccountBalanceKey` range since we need the addresses that will need to be deleted
	start, end := onlineAccountBalanceForRoundRangePrefix(forgetBefore)
	iter := w.kvr.NewIter(start[:], end[:], true)
	defer iter.Close()

	seenAddrs := make(map[basics.Address]struct{})

	toDeletePrimaryIndex := make([]struct {
		basics.Address
		basics.Round
	}, 0)

	toDeleteSecondaryIndex := make([][]byte, 0)

	// loop through the rounds in reverse order (latest first)
	for iter.Next() {
		key := iter.Key()

		// extract address & round from the key
		addr := extractOnlineAccountBalanceAddress(key)
		round := extractOnlineAccountBalanceRound(key)

		// check that we have NOT seen this address before
		if _, ok := seenAddrs[addr]; !ok {
			// new address
			// if the first time (latest in rnd, order reversed) we see it the entry is:
			//  - offline -> then delete all
			//  - online -> then safe to delete all previous except this first (latest)

			// check if voting data is empty (it means the account is offline)
			var oad trackerdb.BaseOnlineAccountData
			var data []byte
			data, err = iter.Value()
			if err != nil {
				return err
			}
			err = protocol.Decode(data, &oad)
			if err != nil {
				return err
			}
			if oad.IsVotingEmpty() {
				// delete this entry (all subsequent will be deleted too outside the if)
				toDeletePrimaryIndex = append(toDeletePrimaryIndex, struct {
					basics.Address
					basics.Round
				}{addr, round})
				toDeleteSecondaryIndex = append(toDeleteSecondaryIndex, key)
			}

			// mark addr as seen
			seenAddrs[addr] = struct{}{}

			// restart the loop
			// if there are some subsequent entries, they will deleted on the next iteration
			// if no subsequent entries, the loop will reset the state and the latest entry does not get deleted
			continue
		}

		// mark the item for deletion
		toDeletePrimaryIndex = append(toDeletePrimaryIndex, struct {
			basics.Address
			basics.Round
		}{addr, round})
		toDeleteSecondaryIndex = append(toDeleteSecondaryIndex, key)
	}

	// 2. delete the individual addr+round entries
	for _, item := range toDeletePrimaryIndex {
		// TODO: [perf] we might be able to optimize this with a SingleDelete call
		key := onlineAccountKey(item.Address, item.Round)
		err = w.kvw.Delete(key[:])
		if err != nil {
			return
		}
	}

	// 3. delete the range from `onlineAccountBalanceKey`
	for _, key := range toDeleteSecondaryIndex {
		// TODO: [perf] we might be able to optimize this with a SingleDelete call
		err = w.kvw.Delete(key)
		if err != nil {
			return
		}
	}

	return
}

func (w *accountsWriter) AccountsPutOnlineRoundParams(onlineRoundParamsData []ledgercore.OnlineRoundParamsData, startRound basics.Round) error {
	// The SQL at the time of impl:
	//
	// for i, data := range onlineRoundParamsData {
	// 		the inserted rnd value is startRound + i
	//
	//		INSERT INTO onlineroundparamstail (rnd, data) VALUES (?, ?)
	//

	// insert the round params
	for i := range onlineRoundParamsData {
		rnd := basics.Round(int(startRound) + i)
		raw := protocol.Encode(&onlineRoundParamsData[i])
		key := onlineAccountRoundParamsKey(rnd)
		err := w.kvw.Set(key[:], raw)
		if err != nil {
			return err
		}
	}

	return nil
}

func (w *accountsWriter) AccountsPruneOnlineRoundParams(deleteBeforeRound basics.Round) error {
	// The SQL at the time of impl:
	//
	// DELETE FROM onlineroundparamstail WHERE rnd<?

	// delete old ones
	start, end := onlineAccountRoundParamsRoundRangePrefix(deleteBeforeRound)
	err := w.kvw.DeleteRange(start[:], end[:])
	if err != nil {
		return err
	}

	return nil
}