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
/
deposit_sub_tree.go
71 lines (62 loc) · 2.05 KB
/
deposit_sub_tree.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
package storage
import (
"github.com/Worldcoin/hubble-commander/db"
"github.com/Worldcoin/hubble-commander/models"
bdg "github.com/dgraph-io/badger/v3"
"github.com/pkg/errors"
bh "github.com/timshannon/badgerhold/v4"
)
func (s *DepositStorage) AddPendingDepositSubtree(subtree *models.PendingDepositSubtree) error {
return s.database.Badger.Upsert(subtree.ID, *subtree)
}
func (s *DepositStorage) GetPendingDepositSubtree(subtreeID models.Uint256) (*models.PendingDepositSubtree, error) {
var subtree models.PendingDepositSubtree
err := s.database.Badger.Get(subtreeID, &subtree)
if errors.Is(err, bh.ErrNotFound) {
return nil, errors.WithStack(NewNotFoundError("deposit sub tree"))
}
if err != nil {
return nil, err
}
subtree.ID = subtreeID
return &subtree, nil
}
func (s *DepositStorage) GetFirstPendingDepositSubtree() (subtree *models.PendingDepositSubtree, err error) {
err = s.database.Badger.Iterator(models.PendingDepositSubtreePrefix, db.KeyIteratorOpts, func(item *bdg.Item) (bool, error) {
subtree, err = decodePendingDepositSubtree(item)
if err != nil {
return false, err
}
return true, nil
})
if errors.Is(err, db.ErrIteratorFinished) {
return nil, errors.WithStack(NewNotFoundError("deposit sub tree"))
}
return subtree, err
}
func (s *DepositStorage) RemovePendingDepositSubtrees(subtreeIDs ...models.Uint256) error {
return s.database.ExecuteInTransaction(TxOptions{}, func(txDatabase *Database) error {
for i := range subtreeIDs {
err := txDatabase.Badger.Delete(subtreeIDs[i], models.PendingDepositSubtree{})
if errors.Is(err, bh.ErrNotFound) {
return errors.WithStack(NewNotFoundError("deposit sub tree"))
}
if err != nil {
return err
}
}
return nil
})
}
func decodePendingDepositSubtree(item *bdg.Item) (*models.PendingDepositSubtree, error) {
var subtree models.PendingDepositSubtree
err := item.Value(subtree.SetBytes)
if err != nil {
return nil, err
}
err = db.DecodeKey(item.Key(), &subtree.ID, models.PendingDepositSubtreePrefix)
if err != nil {
return nil, err
}
return &subtree, nil
}