Skip to content

Commit

Permalink
Merge pull request #7495 from filecoin-project/feat/inline-codegen
Browse files Browse the repository at this point in the history
Inline codegen
  • Loading branch information
magik6k authored Oct 19, 2021
2 parents ba17195 + 1d8a9c7 commit d910098
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 15 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ method-gen: api-gen
(cd ./lotuspond/front/src/chain && $(GOCC) run ./methodgen.go)

actors-gen:
$(GOCC) run ./gen/inline-gen . gen/inlinegen-data.json
$(GOCC) run ./chain/actors/agen
$(GOCC) fmt ./...

Expand Down
8 changes: 8 additions & 0 deletions build/params_shared_vals.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ const UnixfsLinksPerLevel = 1024
const AllowableClockDriftSecs = uint64(1)

// TODO: This is still terrible...What's the impact of updating this before mainnet actually upgrades
/* inline-gen template
const NewestNetworkVersion = network.Version{{.latestNetworkVersion}}
/* inline-gen start */

const NewestNetworkVersion = network.Version14

/* inline-gen end */

// Epochs
const ForkLengthThreshold = Finality

Expand Down
16 changes: 15 additions & 1 deletion chain/actors/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ import (

type Version int

/* inline-gen template
var LatestVersion = {{.latestActorsVersion}}
var Versions = []int{ {{range .actorVersions}} {{.}}, {{end}} }
const ({{range .actorVersions}}
Version{{.}} Version = {{.}}{{end}}
)
/* inline-gen start */

var LatestVersion = 6

var Versions = []int{0, 2, 3, 4, 5, LatestVersion}
var Versions = []int{0, 2, 3, 4, 5, 6}

const (
Version0 Version = 0
Expand All @@ -21,6 +33,8 @@ const (
Version6 Version = 6
)

/* inline-gen end */

// Converts a network version into an actors adt version.
func VersionForNetwork(version network.Version) (Version, error) {
switch version {
Expand Down
15 changes: 15 additions & 0 deletions chain/consensus/filcns/compute_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ import (
"github.com/filecoin-project/go-state-types/big"
blockadt "github.com/filecoin-project/specs-actors/actors/util/adt"

/* inline-gen template
{{range .actorVersions}}
exported{{.}} "github.com/filecoin-project/specs-actors{{import .}}actors/builtin/exported"{{end}}
/* inline-gen start */

exported0 "github.com/filecoin-project/specs-actors/actors/builtin/exported"
exported2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/exported"
exported3 "github.com/filecoin-project/specs-actors/v3/actors/builtin/exported"
exported4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/exported"
exported5 "github.com/filecoin-project/specs-actors/v5/actors/builtin/exported"
exported6 "github.com/filecoin-project/specs-actors/v6/actors/builtin/exported"

/* inline-gen end */

"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/builtin"
Expand All @@ -39,6 +47,11 @@ func NewActorRegistry() *vm.ActorRegistry {
inv := vm.NewActorRegistry()

// TODO: define all these properties on the actors themselves, in specs-actors.
/* inline-gen template
{{range .actorVersions}}
inv.Register(vm.ActorsVersionPredicate(actors.Version{{.}}), exported{{.}}.BuiltinActors()...){{end}}
/* inline-gen start */

inv.Register(vm.ActorsVersionPredicate(actors.Version0), exported0.BuiltinActors()...)
inv.Register(vm.ActorsVersionPredicate(actors.Version2), exported2.BuiltinActors()...)
Expand All @@ -47,6 +60,8 @@ func NewActorRegistry() *vm.ActorRegistry {
inv.Register(vm.ActorsVersionPredicate(actors.Version5), exported5.BuiltinActors()...)
inv.Register(vm.ActorsVersionPredicate(actors.Version6), exported6.BuiltinActors()...)

/* inline-gen end */

return inv
}

Expand Down
9 changes: 9 additions & 0 deletions chain/state/statetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,16 @@ func VersionForNetwork(ver network.Version) (types.StateTreeVersion, error) {
return types.StateTreeVersion2, nil
case network.Version12:
return types.StateTreeVersion3, nil

/* inline-gen template
{{$lastNv := .latestNetworkVersion}}
case{{range .networkVersions}} {{if (ge . 13.)}} network.Version{{.}}{{if (lt . $lastNv)}},{{end}}{{end}}{{end}}:
/* inline-gen start */

case network.Version13, network.Version14:

/* inline-gen end */
return types.StateTreeVersion4, nil
default:
panic(fmt.Sprintf("unsupported network version %d", ver))
Expand Down
15 changes: 15 additions & 0 deletions chain/vm/mkactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ import (
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"

/* inline-gen template
{{range .actorVersions}}
builtin{{.}} "github.com/filecoin-project/specs-actors{{import .}}actors/builtin"{{end}}
/* inline-gen start */

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"
builtin5 "github.com/filecoin-project/specs-actors/v5/actors/builtin"
builtin6 "github.com/filecoin-project/specs-actors/v6/actors/builtin"

/* inline-gen end */

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/actors/aerrors"
"github.com/filecoin-project/lotus/chain/actors/builtin"
Expand Down Expand Up @@ -104,6 +112,12 @@ func newAccountActor(ver actors.Version) *types.Actor {
// TODO: ActorsUpgrade use a global actor registry?
var code cid.Cid
switch ver {
/* inline-gen template
{{range .actorVersions}}
case actors.Version{{.}}:
code = builtin{{.}}.AccountActorCodeID{{end}}
/* inline-gen start */

case actors.Version0:
code = builtin0.AccountActorCodeID
case actors.Version2:
Expand All @@ -116,6 +130,7 @@ func newAccountActor(ver actors.Version) *types.Actor {
code = builtin5.AccountActorCodeID
case actors.Version6:
code = builtin6.AccountActorCodeID
/* inline-gen end */
default:
panic("unsupported actors version")
}
Expand Down
16 changes: 5 additions & 11 deletions documentation/misc/actors_version_checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@
- [ ] Import new actors
- [ ] Define upgrade heights in `build/params_`
- [ ] Generate adapters
- [ ] Add the new version in `chain/actors/agen/main.go`
- [ ] Update `gen/inlinegen-data.json`
- [ ] Update `chain/actors/version.go`
- [ ] Update adapter code in `chain/actors/builtin` if needed
- [ ] Update `chain/actors/policy/policy.go`
- [ ] Update `chain/actors/version.go`
- [ ] Register in `chain/vm/invoker.go`
- [ ] Register in `chain/vm/mkactor.go`
- [ ] Update `chain/types/state.go`
- [ ] Update `chain/state/statetree.go` (New / Load)
- [ ] Update `chain/stmgr/forks.go`
- [ ] Run `make actors-gen`
- [ ] Update `chain/consensus/filcns/upgrades.go`
- [ ] Schedule
- [ ] Migration
- [ ] Update upgrade schedule in `api/test/test.go` and `chain/sync_test.go`
- [ ] Update `NewestNetworkVersion` in `build/params_shared_vals.go`
- [ ] Register in init in `chain/stmgr/utils.go`
- [ ] Update upgrade schedule in `chain/sync_test.go`
123 changes: 123 additions & 0 deletions gen/inline-gen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)

const (
stateGlobal = iota
stateTemplate
stateGen
)

func main() {
db, err := ioutil.ReadFile(os.Args[2])
if err != nil {
panic(err)
}
var data map[string]interface{}
if err := json.Unmarshal(db, &data); err != nil {
panic(err)
}

err = filepath.WalkDir(os.Args[1], func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if filepath.Ext(path) != ".go" {
return nil
}
fb, err := ioutil.ReadFile(path)
if err != nil {
return err
}

lines := strings.Split(string(fb), "\n")

outLines := make([]string, 0, len(lines))
var templateLines []string

state := stateGlobal

rewrite := false

for i, line := range lines {
ln := i + 1
switch state {
case stateGlobal:
outLines = append(outLines, line)
if strings.TrimSpace(line) == `/* inline-gen template` {
state = stateTemplate
fmt.Printf("template section start %s:%d\n", path, ln)
}
case stateTemplate:
outLines = append(outLines, line) // output all template lines

if strings.TrimSpace(line) == `/* inline-gen start */` {
state = stateGen
fmt.Printf("generated section start %s:%d\n", path, ln)
continue
}
templateLines = append(templateLines, line)
case stateGen:
if strings.TrimSpace(line) != `/* inline-gen end */` {
continue
}
fmt.Printf("generated section end %s:%d\n", path, ln)

state = stateGlobal
rewrite = true

tpl, err := template.New("").Funcs(template.FuncMap{
"import": func(v float64) string {
if v == 0 {
return "/"
}
return fmt.Sprintf("/v%d/", int(v))
},
"add": func(a, b float64) float64 {
return a + b
},
}).Parse(strings.Join(templateLines, "\n"))
if err != nil {
fmt.Printf("%s:%d: parsing template: %s\n", path, ln, err)
os.Exit(1)
}

var b bytes.Buffer
err = tpl.Execute(&b, data)
if err != nil {
fmt.Printf("%s:%d: executing template: %s\n", path, ln, err)
os.Exit(1)
}

outLines = append(outLines, strings.Split(b.String(), "\n")...)
outLines = append(outLines, line)
templateLines = nil
}
}

if rewrite {
fmt.Printf("write %s\n", path)
if err := ioutil.WriteFile(path, []byte(strings.Join(outLines, "\n")), 0664); err != nil {
return err
}
}

return nil
})
if err != nil {
panic(err)
}
}
7 changes: 7 additions & 0 deletions gen/inlinegen-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"actorVersions": [0, 2, 3, 4, 5, 6],
"latestActorsVersion": 6,

"networkVersions": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
"latestNetworkVersion": 14
}
17 changes: 14 additions & 3 deletions itests/kit/ensemble_opts_nv.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,25 @@ func SDRUpgradeAt(calico, persian abi.ChainEpoch) EnsembleOpt {
}

func LatestActorsAt(upgradeHeight abi.ChainEpoch) EnsembleOpt {
/* inline-gen template
return UpgradeSchedule(stmgr.Upgrade{
Network: network.Version{{add .latestNetworkVersion -1}},
Height: -1,
}, stmgr.Upgrade{
Network: network.Version{{.latestNetworkVersion}},
Height: upgradeHeight,
Migration: filcns.UpgradeActorsV{{.latestActorsVersion}},
})
/* inline-gen start */
return UpgradeSchedule(stmgr.Upgrade{
Network: network.Version12,
Network: network.Version13,
Height: -1,
}, stmgr.Upgrade{
Network: network.Version13,
Network: network.Version14,
Height: upgradeHeight,
Migration: filcns.UpgradeActorsV5,
Migration: filcns.UpgradeActorsV6,
})
/* inline-gen end */
}

func TurboUpgradeAt(upgradeHeight abi.ChainEpoch) EnsembleOpt {
Expand Down

0 comments on commit d910098

Please sign in to comment.