summaryrefslogtreecommitdiff
path: root/ledger/store/trackerdb/dualdriver/accounts_reader_ext.go
blob: 6f241d3e5e2920cab5feece72300791a85b8d5b3 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// 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 dualdriver

import (
	"context"

	"github.com/algorand/go-algorand/config"
	"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/google/go-cmp/cmp"
)

type accountsReaderExt struct {
	primary   trackerdb.AccountsReaderExt
	secondary trackerdb.AccountsReaderExt
}

// AccountsHashRound implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) AccountsHashRound(ctx context.Context) (hashrnd basics.Round, err error) {
	hashrndP, errP := ar.primary.AccountsHashRound(ctx)
	hashrndS, errS := ar.secondary.AccountsHashRound(ctx)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if hashrndP != hashrndS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return hashrndP, nil
}

// AccountsOnlineRoundParams implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) AccountsOnlineRoundParams() (onlineRoundParamsData []ledgercore.OnlineRoundParamsData, endRound basics.Round, err error) {
	onlineRoundParamsDataP, endRoundP, errP := ar.primary.AccountsOnlineRoundParams()
	onlineRoundParamsDataS, endRoundS, errS := ar.secondary.AccountsOnlineRoundParams()
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if !cmp.Equal(onlineRoundParamsDataP, onlineRoundParamsDataS, allowAllUnexported) {
		err = ErrInconsistentResult
		return
	}
	if endRoundP != endRoundS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return onlineRoundParamsDataP, endRoundP, nil
}

// AccountsOnlineTop implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) AccountsOnlineTop(rnd basics.Round, offset uint64, n uint64, proto config.ConsensusParams) (onlineAccounts map[basics.Address]*ledgercore.OnlineAccount, err error) {
	onlineAccountsP, errP := ar.primary.AccountsOnlineTop(rnd, offset, n, proto)
	onlineAccountsS, errS := ar.secondary.AccountsOnlineTop(rnd, offset, n, proto)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if !cmp.Equal(onlineAccountsP, onlineAccountsS) {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return onlineAccountsP, nil
}

// AccountsRound implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) AccountsRound() (rnd basics.Round, err error) {
	rndP, errP := ar.primary.AccountsRound()
	rndS, errS := ar.secondary.AccountsRound()
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if rndP != rndS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return rndP, nil
}

// AccountsTotals implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) AccountsTotals(ctx context.Context, catchpointStaging bool) (totals ledgercore.AccountTotals, err error) {
	totalsP, errP := ar.primary.AccountsTotals(ctx, catchpointStaging)
	totalsS, errS := ar.secondary.AccountsTotals(ctx, catchpointStaging)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if totalsP != totalsS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return totalsP, nil
}

// LoadAllFullAccounts implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) LoadAllFullAccounts(ctx context.Context, balancesTable string, resourcesTable string, acctCb func(basics.Address, basics.AccountData)) (count int, err error) {
	countP, errP := ar.primary.LoadAllFullAccounts(ctx, balancesTable, resourcesTable, acctCb)
	countS, errS := ar.secondary.LoadAllFullAccounts(ctx, balancesTable, resourcesTable, acctCb)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if countP != countS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return countP, nil
}

// LoadTxTail implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) LoadTxTail(ctx context.Context, dbRound basics.Round) (roundData []*trackerdb.TxTailRound, roundHash []crypto.Digest, baseRound basics.Round, err error) {
	roundDataP, roundHashP, baseRoundP, errP := ar.primary.LoadTxTail(ctx, dbRound)
	roundDataS, roundHashS, baseRoundS, errS := ar.secondary.LoadTxTail(ctx, dbRound)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if !cmp.Equal(roundDataP, roundDataS, allowAllUnexported) {
		err = ErrInconsistentResult
		return
	}
	if !cmp.Equal(roundHashP, roundHashS) {
		err = ErrInconsistentResult
		return
	}
	if baseRoundP != baseRoundS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return roundDataP, roundHashP, baseRoundP, nil
}

// LookupAccountAddressFromAddressID implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) LookupAccountAddressFromAddressID(ctx context.Context, ref trackerdb.AccountRef) (address basics.Address, err error) {
	if ref == nil {
		return address, trackerdb.ErrNotFound
	}
	// parse ref
	xRef := ref.(accountRef)

	addressP, errP := ar.primary.LookupAccountAddressFromAddressID(ctx, xRef.primary)
	addressS, errS := ar.secondary.LookupAccountAddressFromAddressID(ctx, xRef.secondary)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if addressP != addressS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return addressP, nil
}

// LookupAccountRowID implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) LookupAccountRowID(addr basics.Address) (ref trackerdb.AccountRef, err error) {
	// Note: we do not check the refs since they are internal to the engines and wont match
	refP, errP := ar.primary.LookupAccountRowID(addr)
	refS, errS := ar.secondary.LookupAccountRowID(addr)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	return accountRef{refP, refS}, nil
}

// LookupOnlineAccountDataByAddress implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) LookupOnlineAccountDataByAddress(addr basics.Address) (ref trackerdb.OnlineAccountRef, data []byte, err error) {
	// Note: we do not check the refs since they are internal to the engines and wont match
	refP, dataP, errP := ar.primary.LookupOnlineAccountDataByAddress(addr)
	refS, dataS, errS := ar.secondary.LookupOnlineAccountDataByAddress(addr)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if !cmp.Equal(dataP, dataS) {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return onlineAccountRef{refP, refS}, dataP, nil
}

// LookupResourceDataByAddrID implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) LookupResourceDataByAddrID(accRef trackerdb.AccountRef, aidx basics.CreatableIndex) (data []byte, err error) {
	if accRef == nil {
		return data, trackerdb.ErrNotFound
	}
	// parse ref
	xRef := accRef.(accountRef)
	// lookup
	dataP, errP := ar.primary.LookupResourceDataByAddrID(xRef.primary, aidx)
	dataS, errS := ar.secondary.LookupResourceDataByAddrID(xRef.secondary, aidx)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if !cmp.Equal(dataP, dataS) {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return dataP, nil
}

// OnlineAccountsAll implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) OnlineAccountsAll(maxAccounts uint64) (accounts []trackerdb.PersistedOnlineAccountData, err error) {
	accountsP, errP := ar.primary.OnlineAccountsAll(maxAccounts)
	accountsS, errS := ar.secondary.OnlineAccountsAll(maxAccounts)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// coalesce refs
	if len(accountsP) != len(accountsS) {
		err = ErrInconsistentResult
		return
	}
	var ref trackerdb.OnlineAccountRef
	for i := range accountsP {
		ref, err = coalesceOnlineAccountRefs(accountsP[i].Ref, accountsS[i].Ref)
		if err != nil {
			return accounts, err
		}
		// update ref in results
		accountsP[i].Ref = ref
		accountsS[i].Ref = ref
	}
	// check results match
	if !cmp.Equal(accountsP, accountsS, allowAllUnexported) {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return accountsP, nil
}

// ExpiredOnlineAccountsForRound implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) ExpiredOnlineAccountsForRound(rnd basics.Round, voteRnd basics.Round, proto config.ConsensusParams, rewardsLevel uint64) (expAccounts map[basics.Address]*ledgercore.OnlineAccountData, err error) {
	expAccountsP, errP := ar.primary.ExpiredOnlineAccountsForRound(rnd, voteRnd, proto, rewardsLevel)
	expAccountsS, errS := ar.secondary.ExpiredOnlineAccountsForRound(rnd, voteRnd, proto, rewardsLevel)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if !cmp.Equal(expAccountsP, expAccountsS) {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return expAccountsP, nil
}

// Testing implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) Testing() trackerdb.AccountsReaderTestExt {
	// TODO
	return nil
}

// TotalAccounts implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) TotalAccounts(ctx context.Context) (total uint64, err error) {
	totalP, errP := ar.primary.TotalAccounts(ctx)
	totalS, errS := ar.secondary.TotalAccounts(ctx)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if totalP != totalS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return totalP, nil
}

// TotalKVs implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) TotalKVs(ctx context.Context) (total uint64, err error) {
	totalP, errP := ar.primary.TotalKVs(ctx)
	totalS, errS := ar.secondary.TotalKVs(ctx)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if totalP != totalS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return totalP, nil
}

// TotalResources implements trackerdb.AccountsReaderExt
func (ar *accountsReaderExt) TotalResources(ctx context.Context) (total uint64, err error) {
	totalP, errP := ar.primary.TotalResources(ctx)
	totalS, errS := ar.secondary.TotalResources(ctx)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if totalP != totalS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return totalP, nil
}