Skip to content

Commit

Permalink
Generate wrappers for new actor versions
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k authored and arajasek committed May 6, 2021
1 parent 19da73d commit 549b63a
Show file tree
Hide file tree
Showing 71 changed files with 3,665 additions and 300 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ type-gen:
method-gen:
(cd ./lotuspond/front/src/chain && go run ./methodgen.go)

actors-gen:
go run ./chain/actors/agen
go fmt ./...

api-gen:
go run ./gen/api > api/apistruct/struct.go
goimports -w api/apistruct
Expand All @@ -333,9 +337,9 @@ api-gen:

docsgen: docsgen-md docsgen-openrpc

docsgen-md-bin:
docsgen-md-bin: actors-gen
go build $(GOFLAGS) -o docgen-md ./api/docgen/cmd
docsgen-openrpc-bin:
docsgen-openrpc-bin: actors-gen
go build $(GOFLAGS) -o docgen-openrpc ./api/docgen-openrpc/cmd

docsgen-md: docsgen-md-full docsgen-md-storage docsgen-md-worker
Expand All @@ -358,7 +362,7 @@ docsgen-openrpc-worker: docsgen-openrpc-bin

.PHONY: docsgen docsgen-md-bin docsgen-openrpc-bin

gen: type-gen method-gen docsgen api-gen
gen: actors-gen type-gen method-gen docsgen api-gen
.PHONY: gen

print-%:
Expand Down
2 changes: 1 addition & 1 deletion api/test/ccupgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCCUpgrade(t *testing.T, b APIBuilder, blocktime time.Duration) {

func testCCUpgrade(t *testing.T, b APIBuilder, blocktime time.Duration, upgradeHeight abi.ChainEpoch) {
ctx := context.Background()
n, sn := b(t, []FullNodeOpts{FullNodeWithActorsV4At(upgradeHeight)}, OneMiner)
n, sn := b(t, []FullNodeOpts{FullNodeWithLatestActorsAt(upgradeHeight)}, OneMiner)
client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]

Expand Down
2 changes: 1 addition & 1 deletion api/test/deadlines.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestDeadlineToggling(t *testing.T, b APIBuilder, blocktime time.Duration) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

n, sn := b(t, []FullNodeOpts{FullNodeWithActorsV4At(upgradeH)}, OneMiner)
n, sn := b(t, []FullNodeOpts{FullNodeWithLatestActorsAt(upgradeH)}, OneMiner)

client := n[0].FullNode.(*impl.FullNodeAPI)
minerA := sn[0]
Expand Down
2 changes: 1 addition & 1 deletion api/test/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestDealMining(t *testing.T, b APIBuilder, blocktime time.Duration, carExpo
func (ts *testSuite) testNonGenesisMiner(t *testing.T) {
ctx := context.Background()
n, sn := ts.makeNodes(t, []FullNodeOpts{
FullNodeWithActorsV4At(-1),
FullNodeWithLatestActorsAt(-1),
}, []StorageMiner{
{Full: 0, Preseal: PresealGenesis},
})
Expand Down
2 changes: 1 addition & 1 deletion api/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ var OneMiner = []StorageMiner{{Full: 0, Preseal: PresealGenesis}}
var OneFull = DefaultFullOpts(1)
var TwoFull = DefaultFullOpts(2)

var FullNodeWithActorsV4At = func(upgradeHeight abi.ChainEpoch) FullNodeOpts {
var FullNodeWithLatestActorsAt = func(upgradeHeight abi.ChainEpoch) FullNodeOpts {
if upgradeHeight == -1 {
upgradeHeight = 3
}
Expand Down
8 changes: 4 additions & 4 deletions api/test/window_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func testWindowPostUpgrade(t *testing.T, b APIBuilder, blocktime time.Duration,
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

n, sn := b(t, []FullNodeOpts{FullNodeWithActorsV4At(upgradeHeight)}, OneMiner)
n, sn := b(t, []FullNodeOpts{FullNodeWithLatestActorsAt(upgradeHeight)}, OneMiner)

client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]
Expand Down Expand Up @@ -442,7 +442,7 @@ func TestTerminate(t *testing.T, b APIBuilder, blocktime time.Duration) {

nSectors := uint64(2)

n, sn := b(t, []FullNodeOpts{FullNodeWithActorsV4At(-1)}, []StorageMiner{{Full: 0, Preseal: int(nSectors)}})
n, sn := b(t, []FullNodeOpts{FullNodeWithLatestActorsAt(-1)}, []StorageMiner{{Full: 0, Preseal: int(nSectors)}})

client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]
Expand Down Expand Up @@ -617,7 +617,7 @@ func TestWindowPostDispute(t *testing.T, b APIBuilder, blocktime time.Duration)
///
// Then we're going to manually submit bad proofs.
n, sn := b(t, []FullNodeOpts{
FullNodeWithActorsV4At(-1),
FullNodeWithLatestActorsAt(-1),
}, []StorageMiner{
{Full: 0, Preseal: PresealGenesis},
{Full: 0},
Expand Down Expand Up @@ -900,7 +900,7 @@ func TestWindowPostDisputeFails(t *testing.T, b APIBuilder, blocktime time.Durat
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

n, sn := b(t, []FullNodeOpts{FullNodeWithActorsV4At(-1)}, OneMiner)
n, sn := b(t, []FullNodeOpts{FullNodeWithLatestActorsAt(-1)}, OneMiner)

client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]
Expand Down
218 changes: 218 additions & 0 deletions chain/actors/agen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package main

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/template"

"golang.org/x/xerrors"
)

var latestVersion = 4

var versions = []int{0, 2, 3, latestVersion}

var versionImports = map[int]string{
0: "/",
2: "/v2/",
3: "/v3/",
latestVersion: "/v4/",
}

var actors = map[string][]int{
"account": versions,
"cron": versions,
"init": versions,
"market": versions,
"miner": versions,
"multisig": versions,
"paych": versions,
"power": versions,
"reward": versions,
"verifreg": versions,
}

func main() {
if err := generateAdapters(); err != nil {
fmt.Println(err)
return
}

if err := generatePolicy("chain/actors/policy/policy.go"); err != nil {
fmt.Println(err)
return
}

if err := generateBuiltin("chain/actors/builtin/builtin.go"); err != nil {
fmt.Println(err)
return
}
}

func generateAdapters() error {
for act, versions := range actors {
actDir := filepath.Join("chain/actors/builtin", act)

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

if err := generateMessages(actDir); err != nil {
return err
}

{
af, err := ioutil.ReadFile(filepath.Join(actDir, "actor.go.template"))
if err != nil {
return xerrors.Errorf("loading actor template: %w", err)
}

tpl := template.Must(template.New("").Funcs(template.FuncMap{
"import": func(v int) string { return versionImports[v] },
}).Parse(string(af)))

var b bytes.Buffer

err = tpl.Execute(&b, map[string]interface{}{
"versions": versions,
"latestVersion": latestVersion,
})
if err != nil {
return err
}

if err := ioutil.WriteFile(filepath.Join(actDir, fmt.Sprintf("%s.go", act)), b.Bytes(), 0666); err != nil {
return err
}
}
}

return nil
}

func generateState(actDir string) error {
af, err := ioutil.ReadFile(filepath.Join(actDir, "state.go.template"))
if err != nil {
if os.IsNotExist(err) {
return nil // skip
}

return xerrors.Errorf("loading state adapter template: %w", err)
}

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

var b bytes.Buffer

err := tpl.Execute(&b, map[string]interface{}{
"v": version,
"import": versionImports[version],
})
if err != nil {
return err
}

if err := ioutil.WriteFile(filepath.Join(actDir, fmt.Sprintf("v%d.go", version)), b.Bytes(), 0666); err != nil {
return err
}
}

return nil
}

func generateMessages(actDir string) error {
af, err := ioutil.ReadFile(filepath.Join(actDir, "message.go.template"))
if err != nil {
if os.IsNotExist(err) {
return nil // skip
}

return xerrors.Errorf("loading message adapter template: %w", err)
}

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

var b bytes.Buffer

err := tpl.Execute(&b, map[string]interface{}{
"v": version,
"import": versionImports[version],
})
if err != nil {
return err
}

if err := ioutil.WriteFile(filepath.Join(actDir, fmt.Sprintf("message%d.go", version)), b.Bytes(), 0666); err != nil {
return err
}
}

return nil
}

func generatePolicy(policyPath string) error {

pf, err := ioutil.ReadFile(policyPath + ".template")
if err != nil {
if os.IsNotExist(err) {
return nil // skip
}

return xerrors.Errorf("loading policy template file: %w", err)
}

tpl := template.Must(template.New("").Funcs(template.FuncMap{
"import": func(v int) string { return versionImports[v] },
}).Parse(string(pf)))
var b bytes.Buffer

err = tpl.Execute(&b, map[string]interface{}{
"versions": versions,
"latestVersion": latestVersion,
})
if err != nil {
return err
}

if err := ioutil.WriteFile(policyPath, b.Bytes(), 0666); err != nil {
return err
}

return nil
}

func generateBuiltin(builtinPath string) error {

bf, err := ioutil.ReadFile(builtinPath + ".template")
if err != nil {
if os.IsNotExist(err) {
return nil // skip
}

return xerrors.Errorf("loading builtin template file: %w", err)
}

tpl := template.Must(template.New("").Funcs(template.FuncMap{
"import": func(v int) string { return versionImports[v] },
}).Parse(string(bf)))
var b bytes.Buffer

err = tpl.Execute(&b, map[string]interface{}{
"versions": versions,
"latestVersion": latestVersion,
})
if err != nil {
return err
}

if err := ioutil.WriteFile(builtinPath, b.Bytes(), 0666); err != nil {
return err
}

return nil
}
12 changes: 12 additions & 0 deletions chain/actors/builtin/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,28 @@ import (
"github.com/filecoin-project/lotus/chain/types"

builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"

builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"

builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin"

builtin4 "github.com/filecoin-project/specs-actors/v4/actors/builtin"
)

func init() {

builtin.RegisterActorState(builtin0.AccountActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
return load0(store, root)
})

builtin.RegisterActorState(builtin2.AccountActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
return load2(store, root)
})

builtin.RegisterActorState(builtin3.AccountActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
return load3(store, root)
})

builtin.RegisterActorState(builtin4.AccountActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
return load4(store, root)
})
Expand All @@ -36,14 +43,19 @@ var Methods = builtin4.MethodsAccount

func Load(store adt.Store, act *types.Actor) (State, error) {
switch act.Code {

case builtin0.AccountActorCodeID:
return load0(store, act.Head)

case builtin2.AccountActorCodeID:
return load2(store, act.Head)

case builtin3.AccountActorCodeID:
return load3(store, act.Head)

case builtin4.AccountActorCodeID:
return load4(store, act.Head)

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

0 comments on commit 549b63a

Please sign in to comment.