migrate lotus-shed verifreg to specs-actors abstractions

This commit is contained in:
Steven Allen 2020-09-21 15:04:39 -07:00
parent 916421b247
commit 4bab784e40
3 changed files with 76 additions and 67 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/go-state-types/big"
verifreg0 "github.com/filecoin-project/specs-actors/actors/builtin/verifreg" verifreg0 "github.com/filecoin-project/specs-actors/actors/builtin/verifreg"
adt0 "github.com/filecoin-project/specs-actors/actors/util/adt" adt0 "github.com/filecoin-project/specs-actors/actors/util/adt"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/lotus/chain/actors/adt" "github.com/filecoin-project/lotus/chain/actors/adt"
@ -18,22 +19,53 @@ type state0 struct {
store adt.Store store adt.Store
} }
func (s *state0) VerifiedClientDataCap(addr address.Address) (bool, abi.StoragePower, error) { func getDataCap(store adt.Store, root cid.Cid, addr address.Address) (bool, abi.StoragePower, error) {
if addr.Protocol() != address.ID { if addr.Protocol() != address.ID {
return false, big.Zero(), xerrors.Errorf("can only look up ID addresses") return false, big.Zero(), xerrors.Errorf("can only look up ID addresses")
} }
vh, err := adt0.AsMap(s.store, s.VerifiedClients) vh, err := adt0.AsMap(store, root)
if err != nil { if err != nil {
return false, big.Zero(), xerrors.Errorf("loading verified clients: %w", err) return false, big.Zero(), xerrors.Errorf("loading verifreg: %w", err)
} }
var dcap abi.StoragePower var dcap abi.StoragePower
if found, err := vh.Get(abi.AddrKey(addr), &dcap); err != nil { if found, err := vh.Get(abi.AddrKey(addr), &dcap); err != nil {
return false, big.Zero(), xerrors.Errorf("looking up verified clients: %w", err) return false, big.Zero(), xerrors.Errorf("looking up addr: %w", err)
} else if !found { } else if !found {
return false, big.Zero(), nil return false, big.Zero(), nil
} }
return true, dcap, nil return true, dcap, nil
} }
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)
}

View File

@ -31,4 +31,7 @@ type State interface {
cbor.Marshaler cbor.Marshaler
VerifiedClientDataCap(address.Address) (bool, abi.StoragePower, error) VerifiedClientDataCap(address.Address) (bool, abi.StoragePower, error)
VerifierDataCap(address.Address) (bool, abi.StoragePower, error)
ForEachVerifier(func(addr address.Address, dcap abi.StoragePower) error) error
ForEachClient(func(addr address.Address, dcap abi.StoragePower) error) error
} }

View File

@ -8,13 +8,15 @@ import (
"github.com/filecoin-project/go-address" "github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/builtin/verifreg" builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/util/adt" verifreg0 "github.com/filecoin-project/specs-actors/actors/builtin/verifreg"
"github.com/filecoin-project/lotus/api/apibstore" "github.com/filecoin-project/lotus/api/apibstore"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors" "github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin/verifreg"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
cbor "github.com/ipfs/go-ipld-cbor" cbor "github.com/ipfs/go-ipld-cbor"
@ -57,7 +59,7 @@ var verifRegAddVerifierCmd = &cli.Command{
return err return err
} }
params, err := actors.SerializeParams(&verifreg.AddVerifierParams{Address: target, Allowance: allowance}) params, err := actors.SerializeParams(&verifreg0.AddVerifierParams{Address: target, Allowance: allowance})
if err != nil { if err != nil {
return err return err
} }
@ -70,9 +72,9 @@ var verifRegAddVerifierCmd = &cli.Command{
ctx := lcli.ReqContext(cctx) ctx := lcli.ReqContext(cctx)
msg := &types.Message{ msg := &types.Message{
To: builtin.VerifiedRegistryActorAddr, To: verifreg.Address,
From: fromk, From: fromk,
Method: builtin.MethodsVerifiedRegistry.AddVerifier, Method: builtin0.MethodsVerifiedRegistry.AddVerifier,
Params: params, Params: params,
} }
@ -131,7 +133,7 @@ var verifRegVerifyClientCmd = &cli.Command{
return err return err
} }
params, err := actors.SerializeParams(&verifreg.AddVerifiedClientParams{Address: target, Allowance: allowance}) params, err := actors.SerializeParams(&verifreg0.AddVerifiedClientParams{Address: target, Allowance: allowance})
if err != nil { if err != nil {
return err return err
} }
@ -144,9 +146,9 @@ var verifRegVerifyClientCmd = &cli.Command{
ctx := lcli.ReqContext(cctx) ctx := lcli.ReqContext(cctx)
msg := &types.Message{ msg := &types.Message{
To: builtin.VerifiedRegistryActorAddr, To: verifreg.Address,
From: fromk, From: fromk,
Method: builtin.MethodsVerifiedRegistry.AddVerifiedClient, Method: builtin0.MethodsVerifiedRegistry.AddVerifiedClient,
Params: params, Params: params,
} }
@ -181,7 +183,7 @@ var verifRegListVerifiersCmd = &cli.Command{
defer closer() defer closer()
ctx := lcli.ReqContext(cctx) ctx := lcli.ReqContext(cctx)
act, err := api.StateGetActor(ctx, builtin.VerifiedRegistryActorAddr, types.EmptyTSK) act, err := api.StateGetActor(ctx, verifreg.Address, types.EmptyTSK)
if err != nil { if err != nil {
return err return err
} }
@ -189,31 +191,14 @@ var verifRegListVerifiersCmd = &cli.Command{
apibs := apibstore.NewAPIBlockstore(api) apibs := apibstore.NewAPIBlockstore(api)
store := adt.WrapStore(ctx, cbor.NewCborStore(apibs)) store := adt.WrapStore(ctx, cbor.NewCborStore(apibs))
var st verifreg.State st, err := verifreg.Load(store, act)
if err := store.Get(ctx, act.Head, &st); err != nil {
return err
}
vh, err := adt.AsMap(store, st.Verifiers)
if err != nil { if err != nil {
return err return err
} }
return st.ForEachVerifier(func(addr address.Address, dcap abi.StoragePower) error {
var dcap verifreg.DataCap _, err := fmt.Printf("%s: %s\n", addr, dcap)
if err := vh.ForEach(&dcap, func(k string) error {
addr, err := address.NewFromBytes([]byte(k))
if err != nil {
return err
}
fmt.Printf("%s: %s\n", addr, dcap)
return nil
}); err != nil {
return err return err
} })
return nil
}, },
} }
@ -228,7 +213,7 @@ var verifRegListClientsCmd = &cli.Command{
defer closer() defer closer()
ctx := lcli.ReqContext(cctx) ctx := lcli.ReqContext(cctx)
act, err := api.StateGetActor(ctx, builtin.VerifiedRegistryActorAddr, types.EmptyTSK) act, err := api.StateGetActor(ctx, verifreg.Address, types.EmptyTSK)
if err != nil { if err != nil {
return err return err
} }
@ -236,31 +221,14 @@ var verifRegListClientsCmd = &cli.Command{
apibs := apibstore.NewAPIBlockstore(api) apibs := apibstore.NewAPIBlockstore(api)
store := adt.WrapStore(ctx, cbor.NewCborStore(apibs)) store := adt.WrapStore(ctx, cbor.NewCborStore(apibs))
var st verifreg.State st, err := verifreg.Load(store, act)
if err := store.Get(ctx, act.Head, &st); err != nil {
return err
}
vh, err := adt.AsMap(store, st.VerifiedClients)
if err != nil { if err != nil {
return err return err
} }
return st.ForEachClient(func(addr address.Address, dcap abi.StoragePower) error {
var dcap verifreg.DataCap _, err := fmt.Printf("%s: %s\n", addr, dcap)
if err := vh.ForEach(&dcap, func(k string) error {
addr, err := address.NewFromBytes([]byte(k))
if err != nil {
return err
}
fmt.Printf("%s: %s\n", addr, dcap)
return nil
}); err != nil {
return err return err
} })
return nil
}, },
} }
@ -318,7 +286,17 @@ var verifRegCheckVerifierCmd = &cli.Command{
defer closer() defer closer()
ctx := lcli.ReqContext(cctx) ctx := lcli.ReqContext(cctx)
act, err := api.StateGetActor(ctx, builtin.VerifiedRegistryActorAddr, types.EmptyTSK) head, err := api.ChainHead(ctx)
if err != nil {
return err
}
vid, err := api.StateLookupID(ctx, vaddr, head.Key())
if err != nil {
return err
}
act, err := api.StateGetActor(ctx, verifreg.Address, head.Key())
if err != nil { if err != nil {
return err return err
} }
@ -326,20 +304,16 @@ var verifRegCheckVerifierCmd = &cli.Command{
apibs := apibstore.NewAPIBlockstore(api) apibs := apibstore.NewAPIBlockstore(api)
store := adt.WrapStore(ctx, cbor.NewCborStore(apibs)) store := adt.WrapStore(ctx, cbor.NewCborStore(apibs))
var st verifreg.State st, err := verifreg.Load(store, act)
if err := store.Get(ctx, act.Head, &st); err != nil {
return err
}
vh, err := adt.AsMap(store, st.Verifiers)
if err != nil { if err != nil {
return err return err
} }
var dcap verifreg.DataCap found, dcap, err := st.VerifierDataCap(vid)
if found, err := vh.Get(abi.AddrKey(vaddr), &dcap); err != nil { if err != nil {
return err return err
} else if !found { }
if !found {
return fmt.Errorf("not found") return fmt.Errorf("not found")
} }