diff --git a/Makefile b/Makefile index 587c3c61c..f7b13cc18 100644 --- a/Makefile +++ b/Makefile @@ -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 ./... diff --git a/build/params_shared_vals.go b/build/params_shared_vals.go index 22d1c30e3..0a242f6f2 100644 --- a/build/params_shared_vals.go +++ b/build/params_shared_vals.go @@ -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 diff --git a/chain/actors/version.go b/chain/actors/version.go index 95dd09126..7b7a6393a 100644 --- a/chain/actors/version.go +++ b/chain/actors/version.go @@ -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 { diff --git a/chain/consensus/filcns/compute_state.go b/chain/consensus/filcns/compute_state.go index 927562840..3c333298e 100644 --- a/chain/consensus/filcns/compute_state.go +++ b/chain/consensus/filcns/compute_state.go @@ -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 } diff --git a/chain/state/statetree.go b/chain/state/statetree.go index b4323c04b..f230f7faa 100644 --- a/chain/state/statetree.go +++ b/chain/state/statetree.go @@ -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)) diff --git a/chain/vm/mkactor.go b/chain/vm/mkactor.go index b75f290dc..b0d6b454d 100644 --- a/chain/vm/mkactor.go +++ b/chain/vm/mkactor.go @@ -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") } diff --git a/documentation/misc/actors_version_checklist.md b/documentation/misc/actors_version_checklist.md index 1fae4bd8a..5e6038c2b 100644 --- a/documentation/misc/actors_version_checklist.md +++ b/documentation/misc/actors_version_checklist.md @@ -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` diff --git a/gen/inline-gen/main.go b/gen/inline-gen/main.go new file mode 100644 index 000000000..d97134cdd --- /dev/null +++ b/gen/inline-gen/main.go @@ -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) + } +} diff --git a/gen/inlinegen-data.json b/gen/inlinegen-data.json new file mode 100644 index 000000000..e26b1b28f --- /dev/null +++ b/gen/inlinegen-data.json @@ -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 +} diff --git a/itests/kit/ensemble_opts_nv.go b/itests/kit/ensemble_opts_nv.go index a03e63f4a..0d7d87e6a 100644 --- a/itests/kit/ensemble_opts_nv.go +++ b/itests/kit/ensemble_opts_nv.go @@ -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 {