Merge pull request #7495 from filecoin-project/feat/inline-codegen

Inline codegen
This commit is contained in:
Łukasz Magiera 2021-10-19 10:25:32 +01:00 committed by GitHub
commit d9100981ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 212 additions and 15 deletions

View File

@ -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 ./...

View File

@ -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

View File

@ -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
@ -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 {

View File

@ -16,6 +16,12 @@ 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"
@ -23,6 +29,8 @@ import (
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"
@ -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()...)
@ -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
}

View File

@ -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))

View File

@ -14,6 +14,12 @@ 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"
@ -21,6 +27,8 @@ import (
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"
@ -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:
@ -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")
}

View File

@ -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
gen/inline-gen/main.go Normal file
View File

@ -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
gen/inlinegen-data.json Normal file
View File

@ -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
}

View File

@ -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 {