2019-07-26 04:54:22 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-02 14:29:39 +00:00
|
|
|
|
2020-03-25 02:25:20 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
2020-02-25 20:54:58 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
2020-03-27 03:03:04 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime/exitcode"
|
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
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
|
|
|
"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
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
// TryCreateAccountActor creates account actors from only BLS/SECP256K1 addresses.
|
2020-03-27 03:13:32 +00:00
|
|
|
func TryCreateAccountActor(rt *Runtime, addr address.Address) (*types.Actor, aerrors.ActorError) {
|
2020-03-25 02:25:20 +00:00
|
|
|
addrID, err := rt.state.RegisterNewAddress(addr)
|
2019-07-26 04:54:22 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:45:52 +00:00
|
|
|
return nil, aerrors.Escalate(err, "registering actor address")
|
2020-03-25 02:25:20 +00:00
|
|
|
}
|
2020-03-25 19:13:09 +00:00
|
|
|
|
2020-04-23 16:45:52 +00:00
|
|
|
act, aerr := makeActor(addr)
|
2020-03-25 02:25:20 +00:00
|
|
|
if aerr != nil {
|
|
|
|
return nil, aerr
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 02:25:20 +00:00
|
|
|
if err := rt.state.SetActor(addrID, act); err != nil {
|
2020-04-23 16:45:52 +00:00
|
|
|
return nil, aerrors.Escalate(err, "creating new actor failed")
|
2020-03-25 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p, err := actors.SerializeParams(&addr)
|
|
|
|
if err != nil {
|
2020-04-30 17:46:33 +00:00
|
|
|
return nil, aerrors.Escalate(err, "couldn't serialize params for actor construction")
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|
2020-03-25 02:25:20 +00:00
|
|
|
// call constructor on account
|
2019-07-26 04:54:22 +00:00
|
|
|
|
2020-04-30 03:21:19 +00:00
|
|
|
if err := rt.chargeGasSafe(PricelistByEpoch(rt.height).OnCreateActor()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-25 02:25:20 +00:00
|
|
|
_, aerr = rt.internalSend(builtin.SystemActorAddr, addrID, builtin.MethodsAccount.Constructor, big.Zero(), p)
|
|
|
|
if aerr != nil {
|
2020-03-27 03:03:04 +00:00
|
|
|
return nil, aerrors.Wrap(aerr, "failed to invoke account constructor")
|
2020-03-25 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
act, err = rt.state.GetActor(addrID)
|
|
|
|
if err != nil {
|
2020-04-23 16:45:52 +00:00
|
|
|
return nil, aerrors.Escalate(err, "loading newly created actor failed")
|
2020-03-25 02:25:20 +00:00
|
|
|
}
|
2019-07-26 04:54:22 +00:00
|
|
|
return act, nil
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:45:52 +00:00
|
|
|
func makeActor(addr address.Address) (*types.Actor, aerrors.ActorError) {
|
2019-07-26 04:54:22 +00:00
|
|
|
switch addr.Protocol() {
|
|
|
|
case address.BLS:
|
2020-04-23 16:45:52 +00:00
|
|
|
return NewBLSAccountActor(), nil
|
2019-07-26 04:54:22 +00:00
|
|
|
case address.SECP256K1:
|
2020-04-23 16:45:52 +00:00
|
|
|
return NewSecp256k1AccountActor(), nil
|
2019-07-26 04:54:22 +00:00
|
|
|
case address.ID:
|
2020-04-23 16:45:52 +00:00
|
|
|
return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "no actor with given ID: %s", addr)
|
2019-07-26 04:54:22 +00:00
|
|
|
case address.Actor:
|
2020-04-23 16:45:52 +00:00
|
|
|
return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "no such actor: %s", addr)
|
2019-07-26 04:54:22 +00:00
|
|
|
default:
|
2020-04-23 16:45:52 +00:00
|
|
|
return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "address has unsupported protocol: %d", addr.Protocol())
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:45:52 +00:00
|
|
|
func NewBLSAccountActor() *types.Actor {
|
2019-07-26 04:54:22 +00:00
|
|
|
nact := &types.Actor{
|
2020-02-25 20:54:58 +00:00
|
|
|
Code: builtin.AccountActorCodeID,
|
2019-07-26 04:54:22 +00:00
|
|
|
Balance: types.NewInt(0),
|
2020-03-25 02:25:20 +00:00
|
|
|
Head: EmptyObjectCid,
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 16:45:52 +00:00
|
|
|
return nact
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 16:45:52 +00:00
|
|
|
func NewSecp256k1AccountActor() *types.Actor {
|
2019-07-26 04:54:22 +00:00
|
|
|
nact := &types.Actor{
|
2020-02-25 20:54:58 +00:00
|
|
|
Code: builtin.AccountActorCodeID,
|
2019-07-26 04:54:22 +00:00
|
|
|
Balance: types.NewInt(0),
|
2020-03-25 02:25:20 +00:00
|
|
|
Head: EmptyObjectCid,
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 16:45:52 +00:00
|
|
|
return nact
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|