2022-09-22 20:25:51 +00:00
|
|
|
package datacap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2022-09-27 19:41:34 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin/v9/datacap"
|
2022-09-22 20:25:51 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// taking this as a function instead of asking the caller to call it helps reduce some of the error
|
|
|
|
// checking boilerplate.
|
|
|
|
//
|
|
|
|
// "go made me do it"
|
|
|
|
type rootFunc func() (adt.Map, error)
|
|
|
|
|
|
|
|
func getDataCap(store adt.Store, ver actors.Version, root rootFunc, addr address.Address) (bool, abi.StoragePower, error) {
|
|
|
|
if addr.Protocol() != address.ID {
|
|
|
|
return false, big.Zero(), xerrors.Errorf("can only look up ID addresses")
|
|
|
|
}
|
|
|
|
vh, err := root()
|
|
|
|
if err != nil {
|
|
|
|
return false, big.Zero(), xerrors.Errorf("loading datacap actor: %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
|
|
|
|
}
|
|
|
|
|
2022-09-27 19:41:34 +00:00
|
|
|
return true, big.Div(dcap, datacap.DatacapGranularity), nil
|
2022-09-22 20:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func forEachCap(store adt.Store, ver actors.Version, root rootFunc, cb func(addr address.Address, dcap abi.StoragePower) error) error {
|
|
|
|
vh, err := 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
|
|
|
|
}
|
2022-09-27 19:41:34 +00:00
|
|
|
return cb(a, big.Div(dcap, datacap.DatacapGranularity))
|
2022-09-22 20:25:51 +00:00
|
|
|
})
|
|
|
|
}
|