-
Notifications
You must be signed in to change notification settings - Fork 671
/
utxo_state.go
208 lines (172 loc) · 4.68 KB
/
utxo_state.go
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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avax
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/cache/metercacher"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/linkeddb"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
)
var (
utxoPrefix = []byte("utxo")
indexPrefix = []byte("index")
)
const (
utxoCacheSize = 8192
indexCacheSize = 64
)
// UTXOState is a thin wrapper around a database to provide, caching,
// serialization, and de-serialization for UTXOs.
type UTXOState interface {
// GetUTXO attempts to load a utxo from storage.
GetUTXO(utxoID ids.ID) (*UTXO, error)
// PutUTXO saves the provided utxo to storage.
PutUTXO(utxoID ids.ID, utxo *UTXO) error
// DeleteUTXO deletes the provided utxo from storage.
DeleteUTXO(utxoID ids.ID) error
// UTXOIDs returns the slice of IDs associated with [addr], starting after
// [previous].
// If [previous] is not in the list, starts at beginning.
// Returns at most [limit] IDs.
UTXOIDs(addr []byte, previous ids.ID, limit int) ([]ids.ID, error)
}
type utxoState struct {
codec codec.Manager
// UTXO ID -> *UTXO. If the *UTXO is nil the UTXO doesn't exist
utxoCache cache.Cacher
utxoDB database.Database
indexDB database.Database
indexCache cache.Cacher
}
func NewUTXOState(db database.Database, codec codec.Manager) UTXOState {
return &utxoState{
codec: codec,
utxoCache: &cache.LRU{Size: utxoCacheSize},
utxoDB: prefixdb.New(utxoPrefix, db),
indexDB: prefixdb.New(indexPrefix, db),
indexCache: &cache.LRU{Size: indexCacheSize},
}
}
func NewMeteredUTXOState(db database.Database, codec codec.Manager, metrics prometheus.Registerer) (UTXOState, error) {
utxoCache, err := metercacher.New(
"utxo_cache",
metrics,
&cache.LRU{Size: utxoCacheSize},
)
if err != nil {
return nil, err
}
indexCache, err := metercacher.New(
"index_cache",
metrics,
&cache.LRU{
Size: indexCacheSize,
},
)
return &utxoState{
codec: codec,
utxoCache: utxoCache,
utxoDB: prefixdb.New(utxoPrefix, db),
indexDB: prefixdb.New(indexPrefix, db),
indexCache: indexCache,
}, err
}
func (s *utxoState) GetUTXO(utxoID ids.ID) (*UTXO, error) {
if utxoIntf, found := s.utxoCache.Get(utxoID); found {
if utxoIntf == nil {
return nil, database.ErrNotFound
}
return utxoIntf.(*UTXO), nil
}
bytes, err := s.utxoDB.Get(utxoID[:])
if err == database.ErrNotFound {
s.utxoCache.Put(utxoID, nil)
return nil, database.ErrNotFound
}
if err != nil {
return nil, err
}
// The key was in the database
utxo := &UTXO{}
if _, err := s.codec.Unmarshal(bytes, utxo); err != nil {
return nil, err
}
s.utxoCache.Put(utxoID, utxo)
return utxo, nil
}
func (s *utxoState) PutUTXO(utxoID ids.ID, utxo *UTXO) error {
utxoBytes, err := s.codec.Marshal(codecVersion, utxo)
if err != nil {
return err
}
s.utxoCache.Put(utxoID, utxo)
if err := s.utxoDB.Put(utxoID[:], utxoBytes); err != nil {
return err
}
addressable, ok := utxo.Out.(Addressable)
if !ok {
return nil
}
addresses := addressable.Addresses()
for _, addr := range addresses {
indexList := s.getIndexDB(addr)
if err := indexList.Put(utxoID[:], nil); err != nil {
return err
}
}
return nil
}
func (s *utxoState) DeleteUTXO(utxoID ids.ID) error {
utxo, err := s.GetUTXO(utxoID)
if err != nil {
return err
}
s.utxoCache.Put(utxoID, nil)
if err := s.utxoDB.Delete(utxoID[:]); err != nil {
return err
}
addressable, ok := utxo.Out.(Addressable)
if !ok {
return nil
}
addresses := addressable.Addresses()
for _, addr := range addresses {
indexList := s.getIndexDB(addr)
if err := indexList.Delete(utxoID[:]); err != nil {
return err
}
}
return nil
}
func (s *utxoState) UTXOIDs(addr []byte, start ids.ID, limit int) ([]ids.ID, error) {
indexList := s.getIndexDB(addr)
iter := indexList.NewIteratorWithStart(start[:])
defer iter.Release()
utxoIDs := []ids.ID(nil)
for len(utxoIDs) < limit && iter.Next() {
utxoID, err := ids.ToID(iter.Key())
if err != nil {
return nil, err
}
if utxoID == start {
continue
}
start = ids.Empty
utxoIDs = append(utxoIDs, utxoID)
}
return utxoIDs, iter.Error()
}
func (s *utxoState) getIndexDB(addr []byte) linkeddb.LinkedDB {
addrStr := string(addr)
if indexList, exists := s.indexCache.Get(addrStr); exists {
return indexList.(linkeddb.LinkedDB)
}
indexDB := prefixdb.NewNested(addr, s.indexDB)
indexList := linkeddb.NewDefault(indexDB)
s.indexCache.Put(addrStr, indexList)
return indexList
}