summaryrefslogtreecommitdiff
path: root/ledger/store/trackerdb/generickv/onlineaccounts_reader.go
blob: 573bf478f5761b1ecdd2149aef15260d05df302c (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
// 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 (
	"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"
)

// LookupOnline pulls the Online Account data for a given account+round
func (r *accountsReader) LookupOnline(addr basics.Address, rnd basics.Round) (data trackerdb.PersistedOnlineAccountData, err error) {
	// SQL at the time of writing this:
	//
	// SELECT
	// 		onlineaccounts.rowid, onlineaccounts.updround,
	//	    acctrounds.rnd,
	//      onlineaccounts.data
	// FROM acctrounds
	//		LEFT JOIN onlineaccounts ON address=? AND updround <= ?
	// WHERE id='acctbase'
	// ORDER BY updround DESC LIMIT 1

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

	// addr is set in the sqlite impl even if we dont find the account
	data.Addr = addr

	// read latest account up to `rnd``
	low, high := onlineAccountLatestRangePrefix(addr, rnd)
	iter := r.kvr.NewIter(low[:], high[:], true)
	defer iter.Close()

	var value []byte

	if iter.Next() {
		key := iter.Key()

		// extract round
		updRound := extractOnlineAccountRound(key)

		data.UpdRound = updRound

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

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

		normBalance := data.AccountData.NormalizedOnlineBalance(r.proto)
		data.Ref = onlineAccountRef{addr, normBalance, rnd}

		// we have the record, we can leave
		return
	}

	// nothing was found
	// Note: the SQL implementation returns a data value and no error even when the account does not exist.
	return data, nil
}

func (r *accountsReader) LookupOnlineHistory(addr basics.Address) (result []trackerdb.PersistedOnlineAccountData, rnd basics.Round, err error) {
	low, high := onlineAccountAddressRangePrefix(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.PersistedOnlineAccountData{}

		key := iter.Key()

		// extract round
		updRound := extractOnlineAccountRound(key)

		pitem.Addr = addr
		pitem.UpdRound = updRound
		// Note: for compatibility with the SQL impl, this is not included on each item
		// pitem.Round = rnd

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

		// set the ref
		pitem.Ref = onlineAccountRef{addr, pitem.AccountData.NormalizedOnlineBalance(r.proto), pitem.UpdRound}

		// append entry to accum
		result = append(result, pitem)
	}

	return
}

func (r *accountsReader) LookupOnlineRoundParams(rnd basics.Round) (onlineRoundParamsData ledgercore.OnlineRoundParamsData, err error) {
	// SQL impl at time of writing:
	//
	// SELECT data
	// FROM onlineroundparamstail
	// WHERE rnd=?

	key := onlineAccountRoundParamsKey(rnd)
	value, closer, err := r.kvr.Get(key[:])
	if err != nil {
		return
	}
	defer closer.Close()

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

	return
}