Skip to content

Commit

Permalink
Generate builtin.go
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed May 6, 2021
1 parent 3037d94 commit 504098d
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 42 deletions.
38 changes: 37 additions & 1 deletion chain/actors/agen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func main() {
fmt.Println(err)
return
}

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

func generateAdapters() error {
Expand Down Expand Up @@ -158,7 +163,7 @@ func generatePolicy(policyPath string) error {
return nil // skip
}

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

tpl := template.Must(template.New("").Funcs(template.FuncMap{
Expand All @@ -180,3 +185,34 @@ func generatePolicy(policyPath string) error {

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
}
166 changes: 125 additions & 41 deletions chain/actors/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,81 @@ import (
"golang.org/x/xerrors"

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

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

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

builtin4 "github.com/filecoin-project/specs-actors/v4/actors/builtin"
smoothing4 "github.com/filecoin-project/specs-actors/v4/actors/util/smoothing"

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/cbor"

"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/types"

smoothing0 "github.com/filecoin-project/specs-actors/actors/util/smoothing"
smoothing2 "github.com/filecoin-project/specs-actors/v2/actors/util/smoothing"
smoothing3 "github.com/filecoin-project/specs-actors/v3/actors/util/smoothing"
smoothing4 "github.com/filecoin-project/specs-actors/v4/actors/util/smoothing"

miner0 "github.com/filecoin-project/specs-actors/actors/builtin/miner"
proof0 "github.com/filecoin-project/specs-actors/actors/runtime/proof"
miner4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/miner"
proof4 "github.com/filecoin-project/specs-actors/v4/actors/runtime/proof"
)

var SystemActorAddr = builtin0.SystemActorAddr
var BurntFundsActorAddr = builtin0.BurntFundsActorAddr
var CronActorAddr = builtin0.CronActorAddr
var SystemActorAddr = builtin4.SystemActorAddr
var BurntFundsActorAddr = builtin4.BurntFundsActorAddr
var CronActorAddr = builtin4.CronActorAddr
var SaftAddress = makeAddress("t0122")
var ReserveAddress = makeAddress("t090")
var RootVerifierAddress = makeAddress("t080")

var (
ExpectedLeadersPerEpoch = builtin0.ExpectedLeadersPerEpoch
ExpectedLeadersPerEpoch = builtin4.ExpectedLeadersPerEpoch
)

const (
EpochDurationSeconds = builtin0.EpochDurationSeconds
EpochsInDay = builtin0.EpochsInDay
SecondsInDay = builtin0.SecondsInDay
EpochDurationSeconds = builtin4.EpochDurationSeconds
EpochsInDay = builtin4.EpochsInDay
SecondsInDay = builtin4.SecondsInDay
)

const (
MethodSend = builtin4.MethodSend
MethodConstructor = builtin4.MethodConstructor
)

// These are all just type aliases across actor versions 0, 2, & 3. In the future, that might change
// These are all just type aliases across actor versions. In the future, that might change
// and we might need to do something fancier.
type SectorInfo = proof0.SectorInfo
type PoStProof = proof0.PoStProof
type SectorInfo = proof4.SectorInfo
type PoStProof = proof4.PoStProof
type FilterEstimate = smoothing0.FilterEstimate

func QAPowerForWeight(size abi.SectorSize, duration abi.ChainEpoch, dealWeight, verifiedWeight abi.DealWeight) abi.StoragePower {
return miner4.QAPowerForWeight(size, duration, dealWeight, verifiedWeight)
}

func FromV0FilterEstimate(v0 smoothing0.FilterEstimate) FilterEstimate {

return (FilterEstimate)(v0) //nolint:unconvert
}

// Doesn't change between actors v0, v2, and v3.
func QAPowerForWeight(size abi.SectorSize, duration abi.ChainEpoch, dealWeight, verifiedWeight abi.DealWeight) abi.StoragePower {
return miner0.QAPowerForWeight(size, duration, dealWeight, verifiedWeight)
}

func FromV2FilterEstimate(v2 smoothing2.FilterEstimate) FilterEstimate {

return (FilterEstimate)(v2)

}

func FromV3FilterEstimate(v3 smoothing3.FilterEstimate) FilterEstimate {

return (FilterEstimate)(v3)

}

func FromV4FilterEstimate(v4 smoothing4.FilterEstimate) FilterEstimate {

return (FilterEstimate)(v4)

}

type ActorStateLoader func(store adt.Store, root cid.Cid) (cbor.Marshaler, error)
Expand All @@ -92,52 +101,127 @@ func Load(store adt.Store, act *types.Actor) (cbor.Marshaler, error) {

func ActorNameByCode(c cid.Cid) string {
switch {

case builtin0.IsBuiltinActor(c):
return builtin0.ActorNameByCode(c)

case builtin2.IsBuiltinActor(c):
return builtin2.ActorNameByCode(c)

case builtin3.IsBuiltinActor(c):
return builtin3.ActorNameByCode(c)

case builtin4.IsBuiltinActor(c):
return builtin4.ActorNameByCode(c)

default:
return "<unknown>"
}
}

func IsBuiltinActor(c cid.Cid) bool {
return builtin0.IsBuiltinActor(c) ||
builtin2.IsBuiltinActor(c) ||
builtin3.IsBuiltinActor(c) ||
builtin4.IsBuiltinActor(c)

if builtin0.IsBuiltinActor(c) {
return true
}

if builtin2.IsBuiltinActor(c) {
return true
}

if builtin3.IsBuiltinActor(c) {
return true
}

if builtin4.IsBuiltinActor(c) {
return true
}

return false
}

func IsAccountActor(c cid.Cid) bool {
return c == builtin0.AccountActorCodeID ||
c == builtin2.AccountActorCodeID ||
c == builtin3.AccountActorCodeID ||
c == builtin4.AccountActorCodeID

if c == builtin0.AccountActorCodeID {
return true
}

if c == builtin2.AccountActorCodeID {
return true
}

if c == builtin3.AccountActorCodeID {
return true
}

if c == builtin4.AccountActorCodeID {
return true
}

return false
}

func IsStorageMinerActor(c cid.Cid) bool {
return c == builtin0.StorageMinerActorCodeID ||
c == builtin2.StorageMinerActorCodeID ||
c == builtin3.StorageMinerActorCodeID ||
c == builtin4.StorageMinerActorCodeID

if c == builtin0.StorageMinerActorCodeID {
return true
}

if c == builtin2.StorageMinerActorCodeID {
return true
}

if c == builtin3.StorageMinerActorCodeID {
return true
}

if c == builtin4.StorageMinerActorCodeID {
return true
}

return false
}

func IsMultisigActor(c cid.Cid) bool {
return c == builtin0.MultisigActorCodeID ||
c == builtin2.MultisigActorCodeID ||
c == builtin3.MultisigActorCodeID ||
c == builtin4.MultisigActorCodeID

if c == builtin0.MultisigActorCodeID {
return true
}

if c == builtin2.MultisigActorCodeID {
return true
}

if c == builtin3.MultisigActorCodeID {
return true
}

if c == builtin4.MultisigActorCodeID {
return true
}

return false
}

func IsPaymentChannelActor(c cid.Cid) bool {
return c == builtin0.PaymentChannelActorCodeID ||
c == builtin2.PaymentChannelActorCodeID ||
c == builtin3.PaymentChannelActorCodeID ||
c == builtin4.PaymentChannelActorCodeID

if c == builtin0.PaymentChannelActorCodeID {
return true
}

if c == builtin2.PaymentChannelActorCodeID {
return true
}

if c == builtin3.PaymentChannelActorCodeID {
return true
}

if c == builtin4.PaymentChannelActorCodeID {
return true
}

return false
}

func makeAddress(addr string) address.Address {
Expand Down
Loading

0 comments on commit 504098d

Please sign in to comment.