summaryrefslogtreecommitdiff
path: root/ledger/store/trackerdb/dualdriver/accounts_reader.go
blob: b39aadd7cbbd2d33fa4af046e742cbcd48caacc9 (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
// 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 (
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/ledger/store/trackerdb"
	"github.com/google/go-cmp/cmp"
)

type accountsReader struct {
	primary   trackerdb.AccountsReader
	secondary trackerdb.AccountsReader
}

// Close implements trackerdb.AccountsReader
func (ar *accountsReader) Close() {
	ar.primary.Close()
	ar.secondary.Close()
}

// LookupAccount implements trackerdb.AccountsReader
func (ar *accountsReader) LookupAccount(addr basics.Address) (data trackerdb.PersistedAccountData, err error) {
	dataP, errP := ar.primary.LookupAccount(addr)
	dataS, errS := ar.secondary.LookupAccount(addr)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// coalesce refs
	ref, err := coalesceAccountRefs(dataP.Ref, dataS.Ref)
	if err != nil {
		return
	}
	// update ref in results
	// Note: this is safe because the refs are engine specific
	dataP.Ref = ref
	dataS.Ref = ref
	// check results match
	if dataP != dataS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return dataP, nil
}

// LookupAllResources implements trackerdb.AccountsReader
func (ar *accountsReader) LookupAllResources(addr basics.Address) (data []trackerdb.PersistedResourcesData, rnd basics.Round, err error) {
	dataP, rndP, errP := ar.primary.LookupAllResources(addr)
	dataS, rndS, errS := ar.secondary.LookupAllResources(addr)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// coalesce refs
	if len(dataP) != len(dataS) {
		err = ErrInconsistentResult
		return
	}
	var ref trackerdb.AccountRef
	for i := range dataP {
		ref, err = coalesceAccountRefs(dataP[i].AcctRef, dataS[i].AcctRef)
		if err != nil {
			return data, rnd, err
		}
		// update ref in results
		dataP[i].AcctRef = ref
		dataS[i].AcctRef = ref
	}
	// check results match
	if !cmp.Equal(dataP, dataS, allowAllUnexported) {
		err = ErrInconsistentResult
		return
	}
	if rndP != rndS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return dataP, rndP, nil
}

// LookupCreator implements trackerdb.AccountsReader
func (ar *accountsReader) LookupCreator(cidx basics.CreatableIndex, ctype basics.CreatableType) (addr basics.Address, ok bool, dbRound basics.Round, err error) {
	addrP, okP, dbRoundP, errP := ar.primary.LookupCreator(cidx, ctype)
	addrS, okS, dbRoundS, errS := ar.secondary.LookupCreator(cidx, ctype)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if addrP != addrS {
		err = ErrInconsistentResult
		return
	}
	if okP != okS {
		err = ErrInconsistentResult
		return
	}
	if dbRoundP != dbRoundS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return addrP, okP, dbRoundP, nil
}

// LookupKeyValue implements trackerdb.AccountsReader
func (ar *accountsReader) LookupKeyValue(key string) (pv trackerdb.PersistedKVData, err error) {
	pvP, errP := ar.primary.LookupKeyValue(key)
	pvS, errS := ar.secondary.LookupKeyValue(key)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if !cmp.Equal(pvP, pvS) {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return pvP, nil
}

// LookupKeysByPrefix implements trackerdb.AccountsReader
func (ar *accountsReader) LookupKeysByPrefix(prefix string, maxKeyNum uint64, results map[string]bool, resultCount uint64) (round basics.Round, err error) {
	roundP, errP := ar.primary.LookupKeysByPrefix(prefix, maxKeyNum, results, resultCount)
	roundS, errS := ar.secondary.LookupKeysByPrefix(prefix, maxKeyNum, results, resultCount)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// check results match
	if roundP != roundS {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return roundP, nil
}

// LookupResources implements trackerdb.AccountsReader
func (ar *accountsReader) LookupResources(addr basics.Address, aidx basics.CreatableIndex, ctype basics.CreatableType) (data trackerdb.PersistedResourcesData, err error) {
	dataP, errP := ar.primary.LookupResources(addr, aidx, ctype)
	dataS, errS := ar.secondary.LookupResources(addr, aidx, ctype)
	// coalesce errors
	err = coalesceErrors(errP, errS)
	if err != nil {
		return
	}
	// coalesce refs
	ref, err := coalesceAccountRefs(dataP.AcctRef, dataS.AcctRef)
	if err != nil {
		return
	}
	// update ref in results
	// Note: this is safe because the refs are engine specific
	dataP.AcctRef = ref
	dataS.AcctRef = ref
	// check results match
	if !cmp.Equal(dataP, dataS, allowAllUnexported) {
		err = ErrInconsistentResult
		return
	}
	// return primary results
	return dataP, nil
}