2020-09-23 22:36:56 +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-24 00:01:40 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2020-09-23 22:36:56 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
2020-09-24 00:01:40 +00:00
|
|
|
func getDataCap(store adt.Store, ver actors.Version, root cid.Cid, addr address.Address) (bool, abi.StoragePower, error) {
|
2020-09-23 22:36:56 +00:00
|
|
|
if addr.Protocol() != address.ID {
|
|
|
|
return false, big.Zero(), xerrors.Errorf("can only look up ID addresses")
|
|
|
|
}
|
|
|
|
|
|
|
|
vh, err := adt.AsMap(store, root, ver)
|
|
|
|
if err != nil {
|
|
|
|
return false, big.Zero(), xerrors.Errorf("loading verifreg: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var dcap abi.StoragePower
|
|
|
|
if found, err := vh.Get(abi.AddrKey(addr), &dcap); err != nil {
|
|
|
|
return false, big.Zero(), xerrors.Errorf("looking up addr: %w", err)
|
|
|
|
} else if !found {
|
|
|
|
return false, big.Zero(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, dcap, nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 00:01:40 +00:00
|
|
|
func forEachCap(store adt.Store, ver actors.Version, root cid.Cid, cb func(addr address.Address, dcap abi.StoragePower) error) error {
|
2020-09-23 22:36:56 +00:00
|
|
|
vh, err := adt.AsMap(store, root, ver)
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|