lotus/chain/vm/mkactor.go

145 lines
4.4 KiB
Go
Raw Normal View History

2019-07-26 04:54:22 +00:00
package vm
import (
"context"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/lotus/build"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/lotus/chain/actors"
2019-07-26 04:54:22 +00:00
2019-10-15 04:33:29 +00:00
"github.com/ipfs/go-cid"
2020-02-04 22:19:05 +00:00
cbor "github.com/ipfs/go-ipld-cbor"
2019-10-15 04:33:29 +00:00
2021-10-11 20:56:29 +00:00
/* inline-gen template
{{range .actorVersions}}
builtin{{.}} "github.com/filecoin-project/specs-actors{{import .}}actors/builtin"{{end}}
/* inline-gen start */
2021-10-11 20:56:29 +00:00
builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"
builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
2021-01-21 19:24:05 +00:00
builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin"
builtin4 "github.com/filecoin-project/specs-actors/v4/actors/builtin"
2021-05-06 05:44:11 +00:00
builtin5 "github.com/filecoin-project/specs-actors/v5/actors/builtin"
2021-09-15 15:22:25 +00:00
builtin6 "github.com/filecoin-project/specs-actors/v6/actors/builtin"
/* inline-gen end */
2021-10-11 20:56:29 +00:00
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/actors/aerrors"
2020-10-08 01:09:33 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors/builtin/account"
"github.com/filecoin-project/lotus/chain/types"
2019-07-26 04:54:22 +00:00
)
2019-07-25 22:15:33 +00:00
func init() {
2020-02-04 22:19:05 +00:00
cst := cbor.NewMemCborStore()
2020-02-17 17:19:06 +00:00
emptyobject, err := cst.Put(context.TODO(), []struct{}{})
2019-07-25 22:15:33 +00:00
if err != nil {
panic(err)
}
EmptyObjectCid = emptyobject
}
var EmptyObjectCid cid.Cid
// TryCreateAccountActor creates account actors from only BLS/SECP256K1 addresses.
func TryCreateAccountActor(rt *Runtime, addr address.Address) (*types.Actor, address.Address, aerrors.ActorError) {
if err := rt.chargeGasSafe(PricelistByEpoch(rt.height).OnCreateActor()); err != nil {
return nil, address.Undef, err
}
if addr == build.ZeroAddress && rt.NetworkVersion() >= network.Version10 {
return nil, address.Undef, aerrors.New(exitcode.ErrIllegalArgument, "cannot create the zero bls actor")
}
addrID, err := rt.state.RegisterNewAddress(addr)
2019-07-26 04:54:22 +00:00
if err != nil {
return nil, address.Undef, aerrors.Escalate(err, "registering actor address")
}
2020-03-25 19:13:09 +00:00
2021-08-10 17:07:30 +00:00
av, err := actors.VersionForNetwork(rt.NetworkVersion())
if err != nil {
return nil, address.Undef, aerrors.Escalate(err, "unsupported network version")
}
2021-10-26 18:11:00 +00:00
act, aerr := makeAccountActor(av, addr)
if aerr != nil {
return nil, address.Undef, aerr
2019-07-26 04:54:22 +00:00
}
if err := rt.state.SetActor(addrID, act); err != nil {
return nil, address.Undef, aerrors.Escalate(err, "creating new actor failed")
}
p, err := actors.SerializeParams(&addr)
if err != nil {
return nil, address.Undef, aerrors.Escalate(err, "couldn't serialize params for actor construction")
2019-07-26 04:54:22 +00:00
}
// call constructor on account
2019-07-26 04:54:22 +00:00
_, aerr = rt.internalSend(builtin.SystemActorAddr, addrID, account.Methods.Constructor, big.Zero(), p)
if aerr != nil {
return nil, address.Undef, aerrors.Wrap(aerr, "failed to invoke account constructor")
}
act, err = rt.state.GetActor(addrID)
if err != nil {
return nil, address.Undef, aerrors.Escalate(err, "loading newly created actor failed")
}
return act, addrID, nil
2019-07-26 04:54:22 +00:00
}
2021-10-26 18:11:00 +00:00
func makeAccountActor(ver actors.Version, addr address.Address) (*types.Actor, aerrors.ActorError) {
2019-07-26 04:54:22 +00:00
switch addr.Protocol() {
case address.BLS, address.SECP256K1:
return newAccountActor(ver), nil
2019-07-26 04:54:22 +00:00
case address.ID:
return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "no actor with given ID: %s", addr)
2019-07-26 04:54:22 +00:00
case address.Actor:
return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "no such actor: %s", addr)
2019-07-26 04:54:22 +00:00
default:
return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "address has unsupported protocol: %d", addr.Protocol())
2019-07-26 04:54:22 +00:00
}
}
func newAccountActor(ver actors.Version) *types.Actor {
// TODO: ActorsUpgrade use a global actor registry?
var code cid.Cid
switch ver {
2021-10-11 20:56:29 +00:00
/* inline-gen template
{{range .actorVersions}}
case actors.Version{{.}}:
code = builtin{{.}}.AccountActorCodeID{{end}}
/* inline-gen start */
2021-10-11 20:56:29 +00:00
case actors.Version0:
code = builtin0.AccountActorCodeID
case actors.Version2:
code = builtin2.AccountActorCodeID
2021-01-21 19:24:05 +00:00
case actors.Version3:
code = builtin3.AccountActorCodeID
case actors.Version4:
code = builtin4.AccountActorCodeID
2021-05-06 05:44:11 +00:00
case actors.Version5:
code = builtin5.AccountActorCodeID
2021-09-15 15:22:25 +00:00
case actors.Version6:
code = builtin6.AccountActorCodeID
/* inline-gen end */
default:
panic("unsupported actors version")
2019-07-26 04:54:22 +00:00
}
nact := &types.Actor{
Code: code,
2019-07-26 04:54:22 +00:00
Balance: types.NewInt(0),
Head: EmptyObjectCid,
2019-07-26 04:54:22 +00:00
}
return nact
2019-07-26 04:54:22 +00:00
}