This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstored_commitment.go
134 lines (119 loc) · 3.94 KB
/
stored_commitment.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
package storage
import (
"github.com/Worldcoin/hubble-commander/db"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/stored"
bdg "github.com/dgraph-io/badger/v3"
"github.com/pkg/errors"
bh "github.com/timshannon/badgerhold/v4"
)
type CommitmentStorage struct {
database *Database
}
func NewCommitmentStorage(database *Database) *CommitmentStorage {
return &CommitmentStorage{
database: database,
}
}
func (s *CommitmentStorage) copyWithNewDatabase(database *Database) *CommitmentStorage {
newCommitmentStorage := *s
newCommitmentStorage.database = database
return &newCommitmentStorage
}
func (s *CommitmentStorage) getStoredCommitment(id *models.CommitmentID) (*stored.Commitment, error) {
storedCommitment := new(stored.Commitment)
err := s.database.Badger.Get(*id, storedCommitment)
if errors.Is(err, bh.ErrNotFound) {
return nil, errors.WithStack(NewNotFoundError("commitment"))
}
if err != nil {
return nil, err
}
return storedCommitment, nil
}
func (s *CommitmentStorage) GetLatestCommitment() (*models.CommitmentBase, error) {
var storedCommitment *stored.Commitment
var err error
err = s.database.Badger.Iterator(stored.CommitmentPrefix, db.ReverseKeyIteratorOpts, func(item *bdg.Item) (bool, error) {
storedCommitment, err = decodeStoredCommitment(item)
return true, err
})
if errors.Is(err, db.ErrIteratorFinished) {
return nil, errors.WithStack(NewNotFoundError("commitment"))
}
if err != nil {
return nil, err
}
return &storedCommitment.CommitmentBase, nil
}
func (s *CommitmentStorage) RemoveCommitmentsByBatchIDs(batchIDs ...models.Uint256) error {
return s.database.ExecuteInTransaction(TxOptions{}, func(txDatabase *Database) error {
ids := make([]models.CommitmentID, 0, len(batchIDs))
for i := range batchIDs {
commitmentIDs, err := getCommitmentIDsByBatchID(txDatabase, batchIDs[i])
if err != nil {
return err
}
ids = append(ids, commitmentIDs...)
}
if len(ids) == 0 {
return errors.WithStack(NewNotFoundError("commitments"))
}
var storedCommitment stored.Commitment
for i := range ids {
err := txDatabase.Badger.Delete(ids[i], storedCommitment)
if err != nil {
return err
}
}
return nil
})
}
func (s *CommitmentStorage) getStoredCommitmentsByBatchID(batchID models.Uint256) ([]stored.Commitment, error) {
storedCommitments := make([]stored.Commitment, 0, 32)
prefix := getCommitmentPrefixWithBatchID(&batchID)
err := s.database.Badger.Iterator(prefix, bdg.DefaultIteratorOptions, func(item *bdg.Item) (bool, error) {
commitment, err := decodeStoredCommitment(item)
if err != nil {
return false, err
}
storedCommitments = append(storedCommitments, *commitment)
return false, nil
})
if err != nil && !errors.Is(err, db.ErrIteratorFinished) {
return nil, err
}
return storedCommitments, nil
}
func decodeStoredCommitment(item *bdg.Item) (*stored.Commitment, error) {
var storedCommitment stored.Commitment
err := item.Value(storedCommitment.SetBytes)
if err != nil {
return nil, errors.WithStack(err)
}
return &storedCommitment, nil
}
func getCommitmentIDsByBatchID(txn *Database, batchID models.Uint256) ([]models.CommitmentID, error) {
ids := make([]models.CommitmentID, 0, 32)
prefix := getCommitmentPrefixWithBatchID(&batchID)
err := txn.Badger.Iterator(prefix, db.KeyIteratorOpts, func(item *bdg.Item) (bool, error) {
var id models.CommitmentID
err := db.DecodeKey(item.Key(), &id, stored.CommitmentPrefix)
if err != nil {
return false, err
}
ids = append(ids, id)
return false, nil
})
if err != nil && !errors.Is(err, db.ErrIteratorFinished) {
return nil, err
}
return ids, nil
}
func getCommitmentPrefixWithBatchID(batchID *models.Uint256) []byte {
storedCommitmentPrefixLen := len(stored.CommitmentPrefix)
prefix := make([]byte, storedCommitmentPrefixLen+32)
copy(prefix[:storedCommitmentPrefixLen], stored.CommitmentPrefix)
copy(prefix[storedCommitmentPrefixLen:], batchID.Bytes())
return prefix
}