2020-09-17 08:17:14 +00:00
|
|
|
package verifreg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2020-09-18 21:59:27 +00:00
|
|
|
verifreg0 "github.com/filecoin-project/specs-actors/actors/builtin/verifreg"
|
|
|
|
adt0 "github.com/filecoin-project/specs-actors/actors/util/adt"
|
2020-09-21 22:04:39 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-09-17 08:17:14 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
|
|
|
)
|
|
|
|
|
2020-09-21 06:19:32 +00:00
|
|
|
var _ State = (*state0)(nil)
|
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
type state0 struct {
|
|
|
|
verifreg0.State
|
2020-09-17 08:17:14 +00:00
|
|
|
store adt.Store
|
|
|
|
}
|
|
|
|
|
2020-09-21 22:04:39 +00:00
|
|
|
func getDataCap(store adt.Store, root cid.Cid, addr address.Address) (bool, abi.StoragePower, error) {
|
2020-09-17 08:17:14 +00:00
|
|
|
if addr.Protocol() != address.ID {
|
|
|
|
return false, big.Zero(), xerrors.Errorf("can only look up ID addresses")
|
|
|
|
}
|
|
|
|
|
2020-09-21 22:04:39 +00:00
|
|
|
vh, err := adt0.AsMap(store, root)
|
2020-09-17 08:17:14 +00:00
|
|
|
if err != nil {
|
2020-09-21 22:04:39 +00:00
|
|
|
return false, big.Zero(), xerrors.Errorf("loading verifreg: %w", err)
|
2020-09-17 08:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var dcap abi.StoragePower
|
|
|
|
if found, err := vh.Get(abi.AddrKey(addr), &dcap); err != nil {
|
2020-09-21 22:04:39 +00:00
|
|
|
return false, big.Zero(), xerrors.Errorf("looking up addr: %w", err)
|
2020-09-17 08:17:14 +00:00
|
|
|
} else if !found {
|
|
|
|
return false, big.Zero(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, dcap, nil
|
|
|
|
}
|
2020-09-21 22:04:39 +00:00
|
|
|
|
|
|
|
func (s *state0) VerifiedClientDataCap(addr address.Address) (bool, abi.StoragePower, error) {
|
|
|
|
return getDataCap(s.store, s.State.VerifiedClients, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *state0) VerifierDataCap(addr address.Address) (bool, abi.StoragePower, error) {
|
|
|
|
return getDataCap(s.store, s.State.Verifiers, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func forEachCap(store adt.Store, root cid.Cid, cb func(addr address.Address, dcap abi.StoragePower) error) error {
|
|
|
|
vh, err := adt0.AsMap(store, root)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("loading verified clients: %w", err)
|
|
|
|
}
|
|
|
|
var dcap abi.StoragePower
|
|
|
|
return vh.ForEach(&dcap, func(key string) error {
|
|
|
|
a, err := address.NewFromBytes([]byte(key))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cb(a, dcap)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *state0) ForEachVerifier(cb func(addr address.Address, dcap abi.StoragePower) error) error {
|
|
|
|
return forEachCap(s.store, s.State.Verifiers, cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *state0) ForEachClient(cb func(addr address.Address, dcap abi.StoragePower) error) error {
|
|
|
|
return forEachCap(s.store, s.State.VerifiedClients, cb)
|
|
|
|
}
|