Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: actors: Integrate datacap actor into lotus #9348

Merged
merged 4 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/builtin_actors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestRegistration(t *testing.T) {
require.True(t, found)
require.True(t, manifestCid.Defined())

for _, key := range actors.GetBuiltinActorsKeys() {
for _, key := range actors.GetBuiltinActorsKeys(actorstypes.Version8) {
actorCid, found := actors.GetActorCodeID(actorstypes.Version8, key)
require.True(t, found)
name, version, found := actors.GetActorMetaByCode(actorCid)
Expand Down
2 changes: 1 addition & 1 deletion chain/actors/actor_cids.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func GetActorCodeIDs(av actorstypes.Version) (map[string]cid.Cid, error) {
return cids, nil
}

actorsKeys := GetBuiltinActorsKeys()
actorsKeys := GetBuiltinActorsKeys(av)
synthCids := make(map[string]cid.Cid)

for _, key := range actorsKeys {
Expand Down
7 changes: 4 additions & 3 deletions chain/actors/agen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var actors = map[string][]int{
"system": lotusactors.Versions,
"reward": lotusactors.Versions,
"verifreg": lotusactors.Versions,
"datacap": lotusactors.Versions[8:],
}

func main() {
Expand Down Expand Up @@ -55,7 +56,7 @@ func generateAdapters() error {
for act, versions := range actors {
actDir := filepath.Join("chain/actors/builtin", act)

if err := generateState(actDir); err != nil {
if err := generateState(actDir, versions); err != nil {
return err
}

Expand Down Expand Up @@ -97,7 +98,7 @@ func generateAdapters() error {
return nil
}

func generateState(actDir string) error {
func generateState(actDir string, versions []int) error {
af, err := ioutil.ReadFile(filepath.Join(actDir, "state.go.template"))
if err != nil {
if os.IsNotExist(err) {
Expand All @@ -107,7 +108,7 @@ func generateState(actDir string) error {
return xerrors.Errorf("loading state adapter template: %w", err)
}

for _, version := range lotusactors.Versions {
for _, version := range versions {
tpl := template.Must(template.New("").Funcs(template.FuncMap{}).Parse(string(af)))

var b bytes.Buffer
Expand Down
55 changes: 55 additions & 0 deletions chain/actors/builtin/datacap/actor.go.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package datacap

import (
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
actorstypes "github.com/filecoin-project/go-state-types/actors"
builtin{{.latestVersion}} "github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/cbor"

"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/types"
)

var (
Address = builtin9.DatacapActorAddr
Methods = builtin9.MethodsDatacap
)

func Load(store adt.Store, act *types.Actor) (State, error) {
if name, av, ok := actors.GetActorMetaByCode(act.Code); ok {
if name != actors.DatacapKey {
return nil, xerrors.Errorf("actor code is not datacap: %s", name)
}

switch av {
{{range .versions}}
case actorstypes.Version{{.}}:
return load{{.}}(store, act.Head)
{{end}}
}
}

return nil, xerrors.Errorf("unknown actor code %s", act.Code)
}

func MakeState(store adt.Store, av actorstypes.Version, governor address.Address, bitwidth uint64) (State, error) {
switch av {
{{range .versions}}
case actorstypes.Version{{.}}:
return make{{.}}(store, governor, bitwidth)

default: return nil, xerrors.Errorf("datacap actor only valid for actors v9 and above, got %d", av)
{{end}}
geoff-vball marked this conversation as resolved.
Show resolved Hide resolved
}
}

type State interface {
cbor.Marshaler

Governor() (address.Address, error)
GetState() interface{}
}
55 changes: 55 additions & 0 deletions chain/actors/builtin/datacap/datacap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package datacap

import (
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
actorstypes "github.com/filecoin-project/go-state-types/actors"
builtin9 "github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/cbor"

"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/types"
)

var (
Address = builtin9.DatacapActorAddr
Methods = builtin9.MethodsDatacap
)

func Load(store adt.Store, act *types.Actor) (State, error) {
if name, av, ok := actors.GetActorMetaByCode(act.Code); ok {
if name != actors.DatacapKey {
return nil, xerrors.Errorf("actor code is not datacap: %s", name)
}

switch av {

case actorstypes.Version9:
return load9(store, act.Head)

}
}

return nil, xerrors.Errorf("unknown actor code %s", act.Code)
}

func MakeState(store adt.Store, av actorstypes.Version, governor address.Address, bitwidth uint64) (State, error) {
switch av {

case actorstypes.Version9:
return make9(store, governor, bitwidth)

default:
return nil, xerrors.Errorf("datacap actor only valid for actors v9 and above, got %d", av)

}
}

type State interface {
cbor.Marshaler

Governor() (address.Address, error)
GetState() interface{}
}
50 changes: 50 additions & 0 deletions chain/actors/builtin/datacap/state.go.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package datacap

import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/ipfs/go-cid"

"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/adt"

datacap{{.v}} "github.com/filecoin-project/go-state-types/builtin{{.import}}datacap"
adt{{.v}} "github.com/filecoin-project/go-state-types/builtin{{.import}}util/adt"
builtin{{.v}} "github.com/filecoin-project/go-state-types/builtin"
)

var _ State = (*state{{.v}})(nil)

func load{{.v}}(store adt.Store, root cid.Cid) (State, error) {
out := state{{.v}}{store: store}
err := store.Get(store.Context(), root, &out)
if err != nil {
return nil, err
}
return &out, nil
}

func make{{.v}}(store adt.Store, governor address.Address, bitwidth uint64) (State, error) {
out := state{{.v}}{store: store}
s, err := datacap{{.v}}.ConstructState(store, governor, bitwidth)
if err != nil {
return nil, err
}

out.State = *s

return &out, nil
}

type state{{.v}} struct {
datacap{{.v}}.State
store adt.Store
}

func (s *state{{.v}}) Governor() (address.Address, error) {
return s.State.Governor, nil
}

func (s *state{{.v}}) GetState() interface{} {
return &s.State
}
46 changes: 46 additions & 0 deletions chain/actors/builtin/datacap/v9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package datacap

import (
"github.com/ipfs/go-cid"

"github.com/filecoin-project/go-address"
datacap9 "github.com/filecoin-project/go-state-types/builtin/v9/datacap"

"github.com/filecoin-project/lotus/chain/actors/adt"
)

var _ State = (*state9)(nil)

func load9(store adt.Store, root cid.Cid) (State, error) {
out := state9{store: store}
err := store.Get(store.Context(), root, &out)
if err != nil {
return nil, err
}
return &out, nil
}

func make9(store adt.Store, governor address.Address, bitwidth uint64) (State, error) {
out := state9{store: store}
s, err := datacap9.ConstructState(store, governor, bitwidth)
if err != nil {
return nil, err
}

out.State = *s

return &out, nil
}

type state9 struct {
datacap9.State
store adt.Store
}

func (s *state9) Governor() (address.Address, error) {
return s.State.Governor, nil
}

func (s *state9) GetState() interface{} {
return &s.State
}
25 changes: 16 additions & 9 deletions chain/actors/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ const (
RewardKey = "reward"
SystemKey = "system"
VerifregKey = "verifiedregistry"
DatacapKey = "datacap"
)

func GetBuiltinActorsKeys() []string {
return []string{
func GetBuiltinActorsKeys(av actorstypes.Version) []string {
keys := []string{
AccountKey,
CronKey,
InitKey,
Expand All @@ -47,6 +48,10 @@ func GetBuiltinActorsKeys() []string {
SystemKey,
VerifregKey,
}
if av >= 9 {
keys = append(keys, DatacapKey)
}
return keys
}

var (
Expand All @@ -58,7 +63,7 @@ type actorEntry struct {
version actorstypes.Version
}

// ClearManifest clears all known manifests. This is usually used in tests that need to switch networks.
// ClearManifests clears all known manifests. This is usually used in tests that need to switch networks.
func ClearManifests() {
manifestMx.Lock()
defer manifestMx.Unlock()
Expand Down Expand Up @@ -103,12 +108,14 @@ func ReadManifest(ctx context.Context, store cbor.IpldStore, mfCid cid.Cid) (map
return nil, xerrors.Errorf("error loading manifest (cid: %s): %w", mfCid, err)
}

actorKeys := GetBuiltinActorsKeys() // TODO: we should be able to enumerate manifests directly.
metadata := make(map[string]cid.Cid, len(actorKeys))
for _, name := range actorKeys {
if c, ok := mf.Get(name); ok {
metadata[name] = c
}
var manifestData manifest.ManifestData
if err := store.Get(ctx, mf.Data, &manifestData); err != nil {
return nil, xerrors.Errorf("error loading manifest data: %w", err)
}

metadata := make(map[string]cid.Cid)
for _, entry := range manifestData.Entries {
metadata[entry.Name] = entry.Code
}

return metadata, nil
Expand Down
8 changes: 4 additions & 4 deletions chain/consensus/filcns/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,7 @@ func upgradeActorsV9Common(
// return LiteMigration(ctx, bstore, newActorsManifestCid, root, av, types.StateTreeVersion4, newStateTreeVersion)
//}

func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsManifestCid cid.Cid, root cid.Cid, av actorstypes.Version, oldStateTreeVersion types.StateTreeVersion, newStateTreeVersion types.StateTreeVersion) (cid.Cid, error) {
func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsManifestCid cid.Cid, root cid.Cid, oldAv actorstypes.Version, newAv actorstypes.Version, oldStateTreeVersion types.StateTreeVersion, newStateTreeVersion types.StateTreeVersion) (cid.Cid, error) {
buf := blockstore.NewTieredBstore(bstore, blockstore.NewMemorySync())
store := store.ActorStore(ctx, buf)
adtStore := gstStore.WrapStore(ctx, store)
Expand Down Expand Up @@ -1615,10 +1615,10 @@ func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsM
return cid.Undef, xerrors.Errorf("error loading new manifest data: %w", err)
}

if len(oldManifestData.Entries) != len(actors.GetBuiltinActorsKeys()) {
if len(oldManifestData.Entries) != len(actors.GetBuiltinActorsKeys(oldAv)) {
return cid.Undef, xerrors.Errorf("incomplete old manifest with %d code CIDs", len(oldManifestData.Entries))
}
if len(newManifestData.Entries) != len(actors.GetBuiltinActorsKeys()) {
if len(newManifestData.Entries) != len(actors.GetBuiltinActorsKeys(newAv)) {
return cid.Undef, xerrors.Errorf("incomplete new manifest with %d code CIDs", len(newManifestData.Entries))
}

Expand Down Expand Up @@ -1650,7 +1650,7 @@ func LiteMigration(ctx context.Context, bstore blockstore.Blockstore, newActorsM
}
var head cid.Cid
if addr == system.Address {
newSystemState, err := system.MakeState(store, av, newManifest.Data)
newSystemState, err := system.MakeState(store, newAv, newManifest.Data)
if err != nil {
return xerrors.Errorf("could not make system actor state: %w", err)
}
Expand Down
9 changes: 7 additions & 2 deletions chain/vm/fvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,15 @@ func NewDebugFVM(ctx context.Context, opts *VMOpts) (*FVM, error) {
return xerrors.Errorf("loading debug manifest: %w", err)
}

av, err := actorstypes.VersionForNetwork(opts.NetworkVersion)
if err != nil {
return xerrors.Errorf("getting actors version: %w", err)
}

// create actor redirect mapping
actorRedirect := make(map[cid.Cid]cid.Cid)
for _, key := range actors.GetBuiltinActorsKeys() {
from, ok := actors.GetActorCodeID(actorstypes.Version8, key)
for _, key := range actors.GetBuiltinActorsKeys(av) {
from, ok := actors.GetActorCodeID(av, key)
if !ok {
log.Warnf("actor missing in the from manifest %s", key)
continue
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ require (
github.com/filecoin-project/go-legs v0.4.4
github.com/filecoin-project/go-padreader v0.0.1
github.com/filecoin-project/go-paramfetch v0.0.4
github.com/filecoin-project/go-state-types v0.1.12-beta.0.20220920181425-d683559e386b
github.com/filecoin-project/go-state-types v0.1.12-beta.0.20220921193302-60676bda3cb5
github.com/filecoin-project/go-statemachine v1.0.2
github.com/filecoin-project/go-statestore v0.2.0
github.com/filecoin-project/go-storedcounter v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ github.com/filecoin-project/go-state-types v0.1.0/go.mod h1:ezYnPf0bNkTsDibL/psS
github.com/filecoin-project/go-state-types v0.1.6/go.mod h1:UwGVoMsULoCK+bWjEdd/xLCvLAQFBC7EDT477SKml+Q=
github.com/filecoin-project/go-state-types v0.1.8/go.mod h1:UwGVoMsULoCK+bWjEdd/xLCvLAQFBC7EDT477SKml+Q=
github.com/filecoin-project/go-state-types v0.1.10/go.mod h1:UwGVoMsULoCK+bWjEdd/xLCvLAQFBC7EDT477SKml+Q=
github.com/filecoin-project/go-state-types v0.1.12-beta.0.20220920181425-d683559e386b h1:s4F3e3EBo56j+4xmrzmoAIvtgvDwLn4nsIf5bqcU0Qg=
github.com/filecoin-project/go-state-types v0.1.12-beta.0.20220920181425-d683559e386b/go.mod h1:n/kujdC9JphvYTrmaD1+vJpvDPy/DwzckoMzP0nBKWI=
github.com/filecoin-project/go-state-types v0.1.12-beta.0.20220921193302-60676bda3cb5 h1:5FCdArFQ/nzsv2vBJ7ibLnocTf+pnnTVxcF0Ohe97O0=
github.com/filecoin-project/go-state-types v0.1.12-beta.0.20220921193302-60676bda3cb5/go.mod h1:n/kujdC9JphvYTrmaD1+vJpvDPy/DwzckoMzP0nBKWI=
github.com/filecoin-project/go-statemachine v0.0.0-20200925024713-05bd7c71fbfe/go.mod h1:FGwQgZAt2Gh5mjlwJUlVB62JeYdo+if0xWxSEfBD9ig=
github.com/filecoin-project/go-statemachine v1.0.2 h1:421SSWBk8GIoCoWYYTE/d+qCWccgmRH0uXotXRDjUbc=
github.com/filecoin-project/go-statemachine v1.0.2/go.mod h1:jZdXXiHa61n4NmgWFG4w8tnqgvZVHYbJ3yW7+y8bF54=
Expand Down
Loading