summaryrefslogtreecommitdiff
path: root/ledger/store/trackerdb/generickv/onlineaccounts_writer.go
blob: 83a194022f8f6ae707115571a8d5626911096ad2 (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
// 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 (
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/ledger/store/trackerdb"
	"github.com/algorand/go-algorand/protocol"
)

type onlineAccountsWriter struct {
	kvw KvWrite
}

type onlineAccountRef struct {
	addr        basics.Address
	normBalance uint64
	round       basics.Round
}

func (ref onlineAccountRef) OnlineAccountRefMarker() {}

// MakeOnlineAccountsWriter constructs an kv agnostic OnlineAccountsWriter
func MakeOnlineAccountsWriter(kvw KvWrite) trackerdb.OnlineAccountsWriter {
	return &onlineAccountsWriter{kvw}
}

func (w *onlineAccountsWriter) InsertOnlineAccount(addr basics.Address, normBalance uint64, data trackerdb.BaseOnlineAccountData, updRound uint64, voteLastValid uint64) (ref trackerdb.OnlineAccountRef, err error) {
	raw := protocol.Encode(&data)
	rnd := basics.Round(updRound)

	// write to the online account key
	key := onlineAccountKey(addr, rnd)
	err = w.kvw.Set(key[:], raw)
	if err != nil {
		return nil, err
	}

	// write to the secondary account balance key
	// TODO: this is not the most efficient use of space, but its a tradeoff with a second lookup per object.
	//       this impacts `AccountsOnlineTop`, and some experiments will be needed to determine if we do extra lookups or duplicate the values.
	bKey := onlineAccountBalanceKey(rnd, normBalance, addr)
	err = w.kvw.Set(bKey[:], raw)
	if err != nil {
		return nil, err
	}

	return onlineAccountRef{addr, normBalance, rnd}, nil
}

func (w *onlineAccountsWriter) Close() {
}