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 3
/
state_leaf.go
55 lines (47 loc) · 1.62 KB
/
state_leaf.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
package storage
import (
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/stored"
"github.com/Worldcoin/hubble-commander/utils"
"github.com/pkg/errors"
bh "github.com/timshannon/badgerhold/v4"
)
func (s *StateTree) upsertStateLeaf(leaf *models.StateLeaf) error {
storedLeaf := stored.MakeStateLeaf(leaf)
return s.database.Badger.Upsert(leaf.StateID, storedLeaf)
}
func (s *Storage) GetStateLeavesByPublicKey(publicKey *models.PublicKey) (stateLeaves []models.StateLeaf, err error) {
accounts, err := s.AccountTree.Leaves(publicKey)
if err != nil {
return nil, err
}
pubKeyIDs := utils.ValueToInterfaceSlice(accounts, "PubKeyID")
storedStateLeaves := make([]stored.FlatStateLeaf, 0, 1)
err = s.database.Badger.Find(
&storedStateLeaves,
bh.Where("PubKeyID").In(pubKeyIDs...).Index("PubKeyID").SortBy("StateID"),
)
if err != nil {
return nil, err
}
if len(storedStateLeaves) == 0 {
return nil, errors.WithStack(NewNotFoundError("user states"))
}
stateLeaves = make([]models.StateLeaf, 0, len(storedStateLeaves))
for i := range storedStateLeaves {
stateLeaves = append(stateLeaves, *storedStateLeaves[i].ToModelsStateLeaf())
}
return stateLeaves, nil
}
func (s *Storage) GetFeeReceiverStateLeaf(pubKeyID uint32, tokenID models.Uint256) (*models.StateLeaf, error) {
stateID, ok := s.feeReceiverStateIDs[tokenID.String()]
if ok {
return s.StateTree.Leaf(stateID)
}
stateLeaf, err := s.StateTree.getLeafByPubKeyIDAndTokenID(pubKeyID, tokenID)
if err != nil {
return nil, err
}
s.feeReceiverStateIDs[stateLeaf.TokenID.String()] = stateLeaf.StateID
return stateLeaf, nil
}