-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_sfc_delegators.go
70 lines (61 loc) · 1.91 KB
/
store_sfc_delegators.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
package sfcapi
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp"
)
// SetSfcDelegation stores SfcDelegation
func (s *Store) SetSfcDelegation(id DelegationID, v *SfcDelegation) {
s.rlp.Set(s.table.Delegations, id.Bytes(), v)
}
// DelSfcDelegation deletes SfcDelegation
func (s *Store) DelSfcDelegation(id DelegationID) {
err := s.table.Delegations.Delete(id.Bytes())
if err != nil {
s.Log.Crit("Failed to erase delegation")
}
}
// ForEachSfcDelegation iterates all stored SfcDelegations
func (s *Store) ForEachSfcDelegation(do func(SfcDelegationAndID)) {
it := s.table.Delegations.NewIterator(nil, nil)
defer it.Release()
s.forEachSfcDelegation(it, func(id SfcDelegationAndID) bool {
do(id)
return true
})
}
// GetSfcDelegationsByAddr returns a lsit of delegations by address
func (s *Store) GetSfcDelegationsByAddr(addr common.Address, limit int) []SfcDelegationAndID {
it := s.table.Delegations.NewIterator(addr.Bytes(), nil)
defer it.Release()
res := make([]SfcDelegationAndID, 0, limit)
s.forEachSfcDelegation(it, func(id SfcDelegationAndID) bool {
if limit == 0 {
return false
}
limit--
res = append(res, id)
return true
})
return res
}
func (s *Store) forEachSfcDelegation(it ethdb.Iterator, do func(SfcDelegationAndID) bool) {
_continue := true
for _continue && it.Next() {
delegation := &SfcDelegation{}
err := rlp.DecodeBytes(it.Value(), delegation)
if err != nil {
s.Log.Crit("Failed to decode rlp while iteration", "err", err)
}
addr := it.Key()[len(it.Key())-DelegationIDSize:]
_continue = do(SfcDelegationAndID{
ID: BytesToDelegationID(addr),
Delegation: delegation,
})
}
}
// GetSfcDelegation returns stored SfcDelegation
func (s *Store) GetSfcDelegation(id DelegationID) *SfcDelegation {
w, _ := s.rlp.Get(s.table.Delegations, id.Bytes(), &SfcDelegation{}).(*SfcDelegation)
return w
}