2021-04-26 14:48:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-04-04 11:29:52 +00:00
|
|
|
"go/format"
|
2021-04-27 18:48:32 +00:00
|
|
|
"os"
|
2021-04-26 14:48:20 +00:00
|
|
|
"path/filepath"
|
2021-05-15 01:11:23 +00:00
|
|
|
"strconv"
|
2021-04-26 14:48:20 +00:00
|
|
|
"text/template"
|
2021-04-30 16:02:57 +00:00
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
|
|
|
lotusactors "github.com/filecoin-project/lotus/chain/actors"
|
2021-04-26 14:48:20 +00:00
|
|
|
)
|
|
|
|
|
2021-04-27 18:48:32 +00:00
|
|
|
var actors = map[string][]int{
|
2021-05-15 01:11:23 +00:00
|
|
|
"account": lotusactors.Versions,
|
|
|
|
"cron": lotusactors.Versions,
|
|
|
|
"init": lotusactors.Versions,
|
|
|
|
"market": lotusactors.Versions,
|
|
|
|
"miner": lotusactors.Versions,
|
|
|
|
"multisig": lotusactors.Versions,
|
|
|
|
"paych": lotusactors.Versions,
|
|
|
|
"power": lotusactors.Versions,
|
|
|
|
"system": lotusactors.Versions,
|
|
|
|
"reward": lotusactors.Versions,
|
|
|
|
"verifreg": lotusactors.Versions,
|
2022-09-21 19:51:28 +00:00
|
|
|
"datacap": lotusactors.Versions[8:],
|
2022-11-17 14:55:40 +00:00
|
|
|
"evm": lotusactors.Versions[9:],
|
2021-04-27 18:48:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 14:48:20 +00:00
|
|
|
func main() {
|
2021-04-30 16:43:06 +00:00
|
|
|
if err := generateAdapters(); err != nil {
|
2021-04-26 14:48:20 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-04 17:53:08 +00:00
|
|
|
|
|
|
|
if err := generatePolicy("chain/actors/policy/policy.go"); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-06 04:17:35 +00:00
|
|
|
|
|
|
|
if err := generateBuiltin("chain/actors/builtin/builtin.go"); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2022-09-06 15:49:29 +00:00
|
|
|
|
|
|
|
if err := generateRegistry("chain/actors/builtin/registry.go"); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2021-04-26 14:48:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 16:43:06 +00:00
|
|
|
func generateAdapters() error {
|
2021-04-26 14:48:20 +00:00
|
|
|
for act, versions := range actors {
|
|
|
|
actDir := filepath.Join("chain/actors/builtin", act)
|
|
|
|
|
2022-09-21 19:51:28 +00:00
|
|
|
if err := generateState(actDir, versions); err != nil {
|
2021-04-27 18:48:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-26 14:48:20 +00:00
|
|
|
|
2021-04-27 18:48:32 +00:00
|
|
|
if err := generateMessages(actDir); err != nil {
|
|
|
|
return err
|
2021-04-26 14:48:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-03-29 19:24:07 +00:00
|
|
|
af, err := os.ReadFile(filepath.Join(actDir, "actor.go.template"))
|
2021-04-26 14:48:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("loading actor template: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl := template.Must(template.New("").Funcs(template.FuncMap{
|
2021-05-15 01:11:23 +00:00
|
|
|
"import": func(v int) string { return getVersionImports()[v] },
|
2021-04-26 14:48:20 +00:00
|
|
|
}).Parse(string(af)))
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
err = tpl.Execute(&b, map[string]interface{}{
|
2021-04-27 18:48:32 +00:00
|
|
|
"versions": versions,
|
2021-05-15 01:11:23 +00:00
|
|
|
"latestVersion": lotusactors.LatestVersion,
|
2021-04-26 14:48:20 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-04 11:29:52 +00:00
|
|
|
fmted, err := format.Source(b.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(actDir, fmt.Sprintf("%s.go", act)), fmted, 0666); err != nil {
|
2021-04-26 14:48:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-27 18:48:32 +00:00
|
|
|
|
2022-09-21 19:51:28 +00:00
|
|
|
func generateState(actDir string, versions []int) error {
|
2023-03-29 19:24:07 +00:00
|
|
|
af, err := os.ReadFile(filepath.Join(actDir, "state.go.template"))
|
2021-04-27 18:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil // skip
|
|
|
|
}
|
|
|
|
|
|
|
|
return xerrors.Errorf("loading state adapter template: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:51:28 +00:00
|
|
|
for _, version := range versions {
|
2021-04-27 18:48:32 +00:00
|
|
|
tpl := template.Must(template.New("").Funcs(template.FuncMap{}).Parse(string(af)))
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
err := tpl.Execute(&b, map[string]interface{}{
|
2022-09-06 15:49:29 +00:00
|
|
|
"v": version,
|
|
|
|
"import": getVersionImports()[version],
|
|
|
|
"latestVersion": lotusactors.LatestVersion,
|
2021-04-27 18:48:32 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(actDir, fmt.Sprintf("v%d.go", version)), b.Bytes(), 0666); err != nil {
|
2021-04-27 18:48:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateMessages(actDir string) error {
|
2023-03-29 19:24:07 +00:00
|
|
|
af, err := os.ReadFile(filepath.Join(actDir, "message.go.template"))
|
2021-04-27 18:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil // skip
|
|
|
|
}
|
|
|
|
|
2021-04-28 14:20:53 +00:00
|
|
|
return xerrors.Errorf("loading message adapter template: %w", err)
|
2021-04-27 18:48:32 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
for _, version := range lotusactors.Versions {
|
2021-04-27 18:48:32 +00:00
|
|
|
tpl := template.Must(template.New("").Funcs(template.FuncMap{}).Parse(string(af)))
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
err := tpl.Execute(&b, map[string]interface{}{
|
2022-09-06 15:49:29 +00:00
|
|
|
"v": version,
|
|
|
|
"import": getVersionImports()[version],
|
|
|
|
"latestVersion": lotusactors.LatestVersion,
|
2021-04-27 18:48:32 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(actDir, fmt.Sprintf("message%d.go", version)), b.Bytes(), 0666); err != nil {
|
2021-04-27 18:48:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-04 17:53:08 +00:00
|
|
|
|
|
|
|
func generatePolicy(policyPath string) error {
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
pf, err := os.ReadFile(policyPath + ".template")
|
2021-05-04 17:53:08 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil // skip
|
|
|
|
}
|
|
|
|
|
2021-05-06 04:17:35 +00:00
|
|
|
return xerrors.Errorf("loading policy template file: %w", err)
|
2021-05-04 17:53:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tpl := template.Must(template.New("").Funcs(template.FuncMap{
|
2021-05-15 01:11:23 +00:00
|
|
|
"import": func(v int) string { return getVersionImports()[v] },
|
2021-05-04 17:53:08 +00:00
|
|
|
}).Parse(string(pf)))
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
err = tpl.Execute(&b, map[string]interface{}{
|
2021-05-15 01:11:23 +00:00
|
|
|
"versions": lotusactors.Versions,
|
|
|
|
"latestVersion": lotusactors.LatestVersion,
|
2021-05-04 17:53:08 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
if err := os.WriteFile(policyPath, b.Bytes(), 0666); err != nil {
|
2021-05-04 17:53:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-06 04:17:35 +00:00
|
|
|
|
|
|
|
func generateBuiltin(builtinPath string) error {
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
bf, err := os.ReadFile(builtinPath + ".template")
|
2021-05-06 04:17:35 +00:00
|
|
|
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{
|
2021-05-15 01:11:23 +00:00
|
|
|
"import": func(v int) string { return getVersionImports()[v] },
|
2021-05-06 04:17:35 +00:00
|
|
|
}).Parse(string(bf)))
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
err = tpl.Execute(&b, map[string]interface{}{
|
2021-05-15 01:11:23 +00:00
|
|
|
"versions": lotusactors.Versions,
|
|
|
|
"latestVersion": lotusactors.LatestVersion,
|
2021-05-06 04:17:35 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
if err := os.WriteFile(builtinPath, b.Bytes(), 0666); err != nil {
|
2021-05-06 04:17:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-15 01:11:23 +00:00
|
|
|
|
2022-09-06 15:49:29 +00:00
|
|
|
func generateRegistry(registryPath string) error {
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
bf, err := os.ReadFile(registryPath + ".template")
|
2022-09-06 15:49:29 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil // skip
|
|
|
|
}
|
|
|
|
|
2022-09-10 14:34:47 +00:00
|
|
|
return xerrors.Errorf("loading registry template file: %w", err)
|
2022-09-06 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tpl := template.Must(template.New("").Funcs(template.FuncMap{
|
|
|
|
"import": func(v int) string { return getVersionImports()[v] },
|
|
|
|
}).Parse(string(bf)))
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
err = tpl.Execute(&b, map[string]interface{}{
|
|
|
|
"versions": lotusactors.Versions,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
if err := os.WriteFile(registryPath, b.Bytes(), 0666); err != nil {
|
2022-09-06 15:49:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
func getVersionImports() map[int]string {
|
|
|
|
versionImports := make(map[int]string, lotusactors.LatestVersion)
|
|
|
|
for _, v := range lotusactors.Versions {
|
|
|
|
if v == 0 {
|
|
|
|
versionImports[v] = "/"
|
|
|
|
} else {
|
|
|
|
versionImports[v] = "/v" + strconv.Itoa(v) + "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return versionImports
|
|
|
|
}
|