-
Notifications
You must be signed in to change notification settings - Fork 0
/
imported_pruning_point.go
240 lines (203 loc) · 6.32 KB
/
imported_pruning_point.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
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
package pruningstore
import (
"github.com/golang/protobuf/proto"
"github.com/karlsend/PYVERT/testfork/karlsend/domain/consensus/database/serialization"
"github.com/karlsend/PYVERT/testfork/karlsend/domain/consensus/model"
"github.com/karlsend/PYVERT/testfork/karlsend/domain/consensus/model/externalapi"
"github.com/pkg/errors"
)
var importedPruningPointUTXOsBucketName = []byte("imported-pruning-point-utxos")
var importedPruningPointMultisetKeyName = []byte("imported-pruning-point-multiset")
func (ps *pruningStore) ClearImportedPruningPointUTXOs(dbContext model.DBWriter) error {
cursor, err := dbContext.Cursor(ps.importedPruningPointUTXOsBucket)
if err != nil {
return err
}
defer cursor.Close()
for ok := cursor.First(); ok; ok = cursor.Next() {
key, err := cursor.Key()
if err != nil {
return err
}
err = dbContext.Delete(key)
if err != nil {
return err
}
}
return nil
}
func (ps *pruningStore) AppendImportedPruningPointUTXOs(dbTx model.DBTransaction,
outpointAndUTXOEntryPairs []*externalapi.OutpointAndUTXOEntryPair) error {
for _, outpointAndUTXOEntryPair := range outpointAndUTXOEntryPairs {
key, err := ps.importedPruningPointUTXOKey(outpointAndUTXOEntryPair.Outpoint)
if err != nil {
return err
}
serializedUTXOEntry, err := serializeUTXOEntry(outpointAndUTXOEntryPair.UTXOEntry)
if err != nil {
return err
}
err = dbTx.Put(key, serializedUTXOEntry)
if err != nil {
return err
}
}
return nil
}
func (ps *pruningStore) ImportedPruningPointUTXOIterator(dbContext model.DBReader) (externalapi.ReadOnlyUTXOSetIterator, error) {
cursor, err := dbContext.Cursor(ps.importedPruningPointUTXOsBucket)
if err != nil {
return nil, err
}
return ps.newCursorUTXOSetIterator(cursor), nil
}
type utxoSetIterator struct {
cursor model.DBCursor
isClosed bool
}
func (ps *pruningStore) newCursorUTXOSetIterator(cursor model.DBCursor) externalapi.ReadOnlyUTXOSetIterator {
return &utxoSetIterator{cursor: cursor}
}
func (u *utxoSetIterator) First() bool {
if u.isClosed {
panic("Tried using a closed utxoSetIterator")
}
return u.cursor.First()
}
func (u *utxoSetIterator) Next() bool {
if u.isClosed {
panic("Tried using a closed utxoSetIterator")
}
return u.cursor.Next()
}
func (u *utxoSetIterator) Get() (outpoint *externalapi.DomainOutpoint, utxoEntry externalapi.UTXOEntry, err error) {
if u.isClosed {
return nil, nil, errors.New("Tried using a closed utxoSetIterator")
}
key, err := u.cursor.Key()
if err != nil {
panic(err)
}
utxoEntryBytes, err := u.cursor.Value()
if err != nil {
return nil, nil, err
}
outpoint, err = deserializeOutpoint(key.Suffix())
if err != nil {
return nil, nil, err
}
utxoEntry, err = deserializeUTXOEntry(utxoEntryBytes)
if err != nil {
return nil, nil, err
}
return outpoint, utxoEntry, nil
}
func (u *utxoSetIterator) Close() error {
if u.isClosed {
return errors.New("Tried using a closed utxoSetIterator")
}
u.isClosed = true
err := u.cursor.Close()
if err != nil {
return err
}
u.cursor = nil
return nil
}
func (ps *pruningStore) importedPruningPointUTXOKey(outpoint *externalapi.DomainOutpoint) (model.DBKey, error) {
serializedOutpoint, err := serializeOutpoint(outpoint)
if err != nil {
return nil, err
}
return ps.importedPruningPointUTXOsBucket.Key(serializedOutpoint), nil
}
func serializeOutpoint(outpoint *externalapi.DomainOutpoint) ([]byte, error) {
return proto.Marshal(serialization.DomainOutpointToDbOutpoint(outpoint))
}
func serializeUTXOEntry(entry externalapi.UTXOEntry) ([]byte, error) {
return proto.Marshal(serialization.UTXOEntryToDBUTXOEntry(entry))
}
func deserializeOutpoint(outpointBytes []byte) (*externalapi.DomainOutpoint, error) {
dbOutpoint := &serialization.DbOutpoint{}
err := proto.Unmarshal(outpointBytes, dbOutpoint)
if err != nil {
return nil, err
}
return serialization.DbOutpointToDomainOutpoint(dbOutpoint)
}
func deserializeUTXOEntry(entryBytes []byte) (externalapi.UTXOEntry, error) {
dbEntry := &serialization.DbUtxoEntry{}
err := proto.Unmarshal(entryBytes, dbEntry)
if err != nil {
return nil, err
}
return serialization.DBUTXOEntryToUTXOEntry(dbEntry)
}
func (ps *pruningStore) ClearImportedPruningPointMultiset(dbContext model.DBWriter) error {
return dbContext.Delete(ps.importedPruningPointMultisetKey)
}
func (ps *pruningStore) ImportedPruningPointMultiset(dbContext model.DBReader) (model.Multiset, error) {
multisetBytes, err := dbContext.Get(ps.importedPruningPointMultisetKey)
if err != nil {
return nil, err
}
return ps.deserializeMultiset(multisetBytes)
}
func (ps *pruningStore) UpdateImportedPruningPointMultiset(dbTx model.DBTransaction, multiset model.Multiset) error {
multisetBytes, err := ps.serializeMultiset(multiset)
if err != nil {
return err
}
return dbTx.Put(ps.importedPruningPointMultisetKey, multisetBytes)
}
func (ps *pruningStore) serializeMultiset(multiset model.Multiset) ([]byte, error) {
return proto.Marshal(serialization.MultisetToDBMultiset(multiset))
}
func (ps *pruningStore) deserializeMultiset(multisetBytes []byte) (model.Multiset, error) {
dbMultiset := &serialization.DbMultiset{}
err := proto.Unmarshal(multisetBytes, dbMultiset)
if err != nil {
return nil, err
}
return serialization.DBMultisetToMultiset(dbMultiset)
}
func (ps *pruningStore) CommitImportedPruningPointUTXOSet(dbContext model.DBWriter) error {
// Delete all the old UTXOs from the database
deleteCursor, err := dbContext.Cursor(ps.pruningPointUTXOSetBucket)
if err != nil {
return err
}
defer deleteCursor.Close()
for ok := deleteCursor.First(); ok; ok = deleteCursor.Next() {
key, err := deleteCursor.Key()
if err != nil {
return err
}
err = dbContext.Delete(key)
if err != nil {
return err
}
}
// Insert all the new UTXOs into the database
insertCursor, err := dbContext.Cursor(ps.importedPruningPointUTXOsBucket)
if err != nil {
return err
}
defer insertCursor.Close()
for ok := insertCursor.First(); ok; ok = insertCursor.Next() {
importedPruningPointUTXOSetKey, err := insertCursor.Key()
if err != nil {
return err
}
pruningPointUTXOSetKey := ps.pruningPointUTXOSetBucket.Key(importedPruningPointUTXOSetKey.Suffix())
serializedUTXOEntry, err := insertCursor.Value()
if err != nil {
return err
}
err = dbContext.Put(pruningPointUTXOSetKey, serializedUTXOEntry)
if err != nil {
return err
}
}
return nil
}