summaryrefslogtreecommitdiff
path: root/ledger/store/trackerdb/generickv/accounts_reader.go
blob: 7cbdc93f1f8d823069b86750bebd0fe7493a62cb (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
// 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 generickv

import (
	"fmt"
	"io"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/ledger/store/trackerdb"
	"github.com/algorand/go-algorand/protocol"
)

// KvRead is a low level KV db interface for reading.
type KvRead interface {
	Get(key []byte) ([]byte, io.Closer, error)
	NewIter(low, high []byte, reverse bool) KvIter
}

// KvIter is a low level KV iterator.
type KvIter interface {
	Next() bool
	Key() []byte
	KeySlice() Slice
	Value() ([]byte, error)
	ValueSlice() (Slice, error)
	Valid() bool
	Close()
}

// Slice is a low level slice used during the KV iterator.
type Slice interface {
	Data() []byte
	Free()
	Size() int
	Exists() bool
}

type accountsReader struct {
	kvr   KvRead
	proto config.ConsensusParams
}

// MakeAccountsReader returns a kv db agnostic AccountsReader.
func MakeAccountsReader(kvr KvRead, proto config.ConsensusParams) *accountsReader {
	return &accountsReader{kvr, proto}
}

func (r *accountsReader) LookupAccount(addr basics.Address) (data trackerdb.PersistedAccountData, err error) {
	// SQL impl at time of writing:
	//
	// SELECT
	// 		accountbase.rowid,
	// 		acctrounds.rnd,
	// 		accountbase.data
	// FROM acctrounds
	// 		LEFT JOIN accountbase ON address=?
	// WHERE id='acctbase'

	data.Addr = addr

	// read the current db round
	data.Round, err = r.AccountsRound()
	if err != nil {
		return
	}

	key := accountKey(addr)
	value, closer, err := r.kvr.Get(key[:])
	if err == trackerdb.ErrNotFound {
		// Note: the SQL implementation returns a data value and no error even when the account does not exist.
		return data, nil
	} else if err != nil {
		return
	}
	defer closer.Close()

	err = protocol.Decode(value, &data.AccountData)
	if err != nil {
		return
	}

	data.Ref = accountRef{addr}

	return
}

func (r *accountsReader) LookupResources(addr basics.Address, aidx basics.CreatableIndex, ctype basics.CreatableType) (data trackerdb.PersistedResourcesData, err error) {
	data.Aidx = aidx

	// read the current db round
	data.Round, err = r.AccountsRound()
	if err != nil {
		return
	}

	key := resourceKey(addr, aidx)
	value, closer, err := r.kvr.Get(key[:])
	if err == trackerdb.ErrNotFound {
		// Note: the SQL implementation returns a data value and no error even when the account does not exist.
		data.Data = trackerdb.MakeResourcesData(0)
		return data, nil
	} else if err != nil {
		err = fmt.Errorf("unable to query resource data for address %v aidx %v ctype %v : %w", addr, aidx, ctype, err)
		return
	}
	defer closer.Close()

	err = protocol.Decode(value, &data.Data)
	if err != nil {
		return
	}

	// Note: the ctype is not filtered during the query, but rather asserted to be what the caller expected
	if ctype == basics.AssetCreatable && !data.Data.IsAsset() {
		err = fmt.Errorf("lookupResources asked for an asset but got %v", data.Data)
	}
	if ctype == basics.AppCreatable && !data.Data.IsApp() {
		err = fmt.Errorf("lookupResources asked for an app but got %v", data.Data)
	}

	data.AcctRef = accountRef{addr}

	return
}

func (r *accountsReader) LookupAllResources(addr basics.Address) (data []trackerdb.PersistedResourcesData, rnd basics.Round, err error) {
	low, high := resourceAddrOnlyRangePrefix(addr)

	iter := r.kvr.NewIter(low[:], high[:], false)
	defer iter.Close()

	var value []byte

	// read the current db round
	rnd, err = r.AccountsRound()
	if err != nil {
		return
	}

	for iter.Next() {
		pitem := trackerdb.PersistedResourcesData{AcctRef: accountRef{addr}, Round: rnd}

		key := iter.Key()

		// extract aidx from key
		pitem.Aidx = extractResourceAidx(key)

		// get value for current item in the iterator
		value, err = iter.Value()
		if err != nil {
			return
		}
		// decode raw value
		err = protocol.Decode(value, &pitem.Data)
		if err != nil {
			return
		}
		// append entry to accum
		data = append(data, pitem)
	}

	return
}

func (r *accountsReader) LookupKeyValue(key string) (pv trackerdb.PersistedKVData, err error) {
	// read the current db round
	pv.Round, err = r.AccountsRound()
	if err != nil {
		return
	}

	value, closer, err := r.kvr.Get(appKvKey(key))
	if err == trackerdb.ErrNotFound {
		// Note: the SQL implementation returns a data value and no error even when the account does not exist.
		return pv, nil
	} else if err != nil {
		return
	}
	defer closer.Close()

	pv.Value = value

	return
}

// TODO: lifted from sql.go, we might want to refactor it
func keyPrefixIntervalPreprocessing(prefix []byte) ([]byte, []byte) {
	if prefix == nil {
		prefix = []byte{}
	}
	prefixIncr := make([]byte, len(prefix))
	copy(prefixIncr, prefix)
	for i := len(prefix) - 1; i >= 0; i-- {
		currentByteIncr := int(prefix[i]) + 1
		if currentByteIncr > 0xFF {
			prefixIncr = prefixIncr[:len(prefixIncr)-1]
			continue
		}
		prefixIncr[i] = byte(currentByteIncr)
		return prefix, prefixIncr
	}
	return prefix, nil
}

func (r *accountsReader) LookupKeysByPrefix(prefix string, maxKeyNum uint64, results map[string]bool, resultCount uint64) (round basics.Round, err error) {
	// SQL at time of writing:
	//
	// SELECT acctrounds.rnd, kvstore.key
	// FROM acctrounds LEFT JOIN kvstore ON kvstore.key >= ? AND kvstore.key < ?
	// WHERE id='acctbase'

	// read the current db round
	round, err = r.AccountsRound()
	if err != nil {
		return
	}

	start, end := keyPrefixIntervalPreprocessing([]byte(prefix))

	iter := r.kvr.NewIter(start, end, false)
	defer iter.Close()

	var value []byte

	for iter.Next() {
		// end iteration if we reached max results
		if resultCount == maxKeyNum {
			return
		}

		// read the key
		key := string(iter.Key())

		// get value for current item in the iterator
		value, err = iter.Value()
		if err != nil {
			return
		}

		// mark if the key has data on the result map
		results[key] = len(value) > 0

		// inc results in range
		resultCount++
	}

	return
}

func (r *accountsReader) LookupCreator(cidx basics.CreatableIndex, ctype basics.CreatableType) (addr basics.Address, ok bool, dbRound basics.Round, err error) {
	// The old SQL impl:
	//
	// SELECT
	// 		acctrounds.rnd,
	// 		assetcreators.creator
	// FROM acctrounds
	// 		LEFT JOIN assetcreators ON asset = ? AND ctype = ?
	// WHERE id='acctbase'

	// read the current db round
	dbRound, err = r.AccountsRound()
	if err != nil {
		return
	}

	key := creatableKey(cidx)
	value, closer, err := r.kvr.Get(key[:])
	if err == trackerdb.ErrNotFound {
		// the record does not exist
		// clean up the error and just return ok=false
		err = nil
		ok = false
		return
	} else if err != nil {
		return
	}
	defer closer.Close()

	// decode the raw value
	var entry creatableEntry
	err = protocol.Decode(value, &entry)
	if err != nil {
		return
	}

	// assert that the ctype is the one expected
	if entry.Ctype != ctype {
		ok = false
		return
	}

	// copy the addr to the return
	copy(addr[:], entry.CreatorAddr)

	// mark result as ok
	ok = true

	return
}

func (r *accountsReader) Close() {

}