summaryrefslogtreecommitdiff
path: root/ledger/store/trackerdb/generickv/accounts_writer.go
blob: 590ba4fcfd24c0a93bc3f455aefa7cf5fb4f5286 (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
// 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 (
	"fmt"

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

// KvWrite is a low level KV db interface for writing.
type KvWrite interface {
	Set(key, value []byte) error
	Delete(key []byte) error
	DeleteRange(start, end []byte) error
}

type accountsWriter struct {
	kvw KvWrite
	kvr KvRead
}

type accountRef struct {
	addr basics.Address
}

func (accountRef) AccountRefMarker() {}
func (ref accountRef) String() string {
	return fmt.Sprintf("accountRef{%s}", ref.addr.String())
}

type resourceRef struct {
	addr basics.Address
	aidx basics.CreatableIndex
}

func (ref resourceRef) ResourceRefMarker() {}

type creatableRef struct {
	cidx basics.CreatableIndex
}

func (ref creatableRef) CreatableRefMarker() {}

// MakeAccountsWriter returns a kv db agnostic AccountsWriter.
// TODO: we should discuss what is the best approach for this `kvr KvRead`.
// the problem is that `OnlineAccountsDelete` requires reading and writing.
// the cleanest approach is to move that method to a separate interface, and have it only be available on 'transactions'.
// although a snapshot+batch should be able to support it too since its all reads, then writes.
func MakeAccountsWriter(kvw KvWrite, kvr KvRead) *accountsWriter {
	return &accountsWriter{kvw, kvr}
}

func (w *accountsWriter) InsertAccount(addr basics.Address, normBalance uint64, data trackerdb.BaseAccountData) (ref trackerdb.AccountRef, err error) {
	// write account entry
	raw := protocol.Encode(&data)
	key := accountKey(addr)
	err = w.kvw.Set(key[:], raw)
	if err != nil {
		return nil, err
	}

	return accountRef{addr}, nil
}

func (w *accountsWriter) DeleteAccount(ref trackerdb.AccountRef) (rowsAffected int64, err error) {
	xref := ref.(accountRef)

	// delete account entry
	key := accountKey(xref.addr)
	err = w.kvw.Delete(key[:])
	if err != nil {
		return 0, err
	}

	return 1, nil
}

func (w *accountsWriter) UpdateAccount(ref trackerdb.AccountRef, normBalance uint64, data trackerdb.BaseAccountData) (rowsAffected int64, err error) {
	xref := ref.(accountRef)

	// overwrite account entry
	raw := protocol.Encode(&data)
	key := accountKey(xref.addr)
	err = w.kvw.Set(key[:], raw)
	if err != nil {
		return 0, err
	}

	return 1, nil
}

func (w *accountsWriter) InsertResource(acctRef trackerdb.AccountRef, aidx basics.CreatableIndex, data trackerdb.ResourcesData) (ref trackerdb.ResourceRef, err error) {
	xref := acctRef.(accountRef)

	// write resource entry
	raw := protocol.Encode(&data)
	key := resourceKey(xref.addr, aidx)
	err = w.kvw.Set(key[:], raw)
	if err != nil {
		return nil, err
	}

	return resourceRef{xref.addr, aidx}, nil
}

func (w *accountsWriter) DeleteResource(acctRef trackerdb.AccountRef, aidx basics.CreatableIndex) (rowsAffected int64, err error) {
	xref := acctRef.(accountRef)

	// delete resource entry
	key := resourceKey(xref.addr, aidx)
	err = w.kvw.Delete(key[:])
	if err != nil {
		return 0, err
	}

	return 1, nil
}

func (w *accountsWriter) UpdateResource(acctRef trackerdb.AccountRef, aidx basics.CreatableIndex, data trackerdb.ResourcesData) (rowsAffected int64, err error) {
	xref := acctRef.(accountRef)

	// update resource entry
	raw := protocol.Encode(&data)
	key := resourceKey(xref.addr, aidx)
	err = w.kvw.Set(key[:], raw)
	if err != nil {
		return 0, err
	}

	return 1, nil
}

func (w *accountsWriter) UpsertKvPair(key string, value []byte) error {
	// upsert kv entry
	err := w.kvw.Set(appKvKey(key), value)
	if err != nil {
		return err
	}

	return nil
}

func (w *accountsWriter) DeleteKvPair(key string) error {
	// delete kv entry
	err := w.kvw.Delete(appKvKey(key))
	if err != nil {
		return err
	}

	return nil
}

type creatableEntry struct {
	_struct     struct{} `codec:",omitempty,omitemptyarray"`
	Ctype       basics.CreatableType
	CreatorAddr []byte
}

func (w *accountsWriter) InsertCreatable(cidx basics.CreatableIndex, ctype basics.CreatableType, creator []byte) (ref trackerdb.CreatableRef, err error) {
	// insert creatable entry
	raw := protocol.Encode(&creatableEntry{Ctype: ctype, CreatorAddr: creator})
	key := creatableKey(cidx)
	err = w.kvw.Set(key[:], raw)
	if err != nil {
		return
	}

	return creatableRef{cidx}, nil
}

func (w *accountsWriter) DeleteCreatable(cidx basics.CreatableIndex, ctype basics.CreatableType) (rowsAffected int64, err error) {
	// delete creatable entry
	key := creatableKey(cidx)
	err = w.kvw.Delete(key[:])
	if err != nil {
		return 0, err
	}

	return 1, nil
}

func (w *accountsWriter) Close() {

}