Skip to content

Commit

Permalink
Implement datacap actor in chain/builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-vball committed Sep 21, 2022
1 parent 25f55c6 commit a9863a9
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 6 deletions.
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
54 changes: 54 additions & 0 deletions chain/actors/builtin/datacap/actor.go.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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)
{{end}}
}
return nil, xerrors.Errorf("unknown actor version %d", av)
}

type State interface {
cbor.Marshaler

Governor() (address.Address, error)
GetState() interface{}
}
53 changes: 53 additions & 0 deletions chain/actors/builtin/datacap/datacap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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)

}
return nil, xerrors.Errorf("unknown actor version %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
}
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.20220920221126-fd7ccf3f2a41
github.com/filecoin-project/go-state-types v0.1.12-beta.0.20220921183113-b987e8c7e65a
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.20220920221126-fd7ccf3f2a41 h1:berTuM/XTOabRB/6In6Xmkv6/2AIs0HHe79+BhXNUx0=
github.com/filecoin-project/go-state-types v0.1.12-beta.0.20220920221126-fd7ccf3f2a41/go.mod h1:n/kujdC9JphvYTrmaD1+vJpvDPy/DwzckoMzP0nBKWI=
github.com/filecoin-project/go-state-types v0.1.12-beta.0.20220921183113-b987e8c7e65a h1:p5V+UogN8nmw34IUK//JTDjiQDACGsOCDtSHMiMBI9k=
github.com/filecoin-project/go-state-types v0.1.12-beta.0.20220921183113-b987e8c7e65a/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

0 comments on commit a9863a9

Please sign in to comment.