2019-07-26 04:54:22 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
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"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"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()
|
2019-07-25 22:15:33 +00:00
|
|
|
emptyobject, err := cst.Put(context.TODO(), map[string]string{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
EmptyObjectCid = emptyobject
|
|
|
|
}
|
|
|
|
|
|
|
|
var EmptyObjectCid cid.Cid
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
func TryCreateAccountActor(st *state.StateTree, addr address.Address) (*types.Actor, aerrors.ActorError) {
|
2019-07-26 04:54:22 +00:00
|
|
|
act, err := makeActor(st, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
if _, err := st.RegisterNewAddress(addr, act); err != nil {
|
|
|
|
return nil, aerrors.Escalate(err, "registering actor address")
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return act, nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
func makeActor(st *state.StateTree, addr address.Address) (*types.Actor, aerrors.ActorError) {
|
2019-07-26 04:54:22 +00:00
|
|
|
switch addr.Protocol() {
|
|
|
|
case address.BLS:
|
|
|
|
return NewBLSAccountActor(st, addr)
|
|
|
|
case address.SECP256K1:
|
|
|
|
return NewSecp256k1AccountActor(st, addr)
|
|
|
|
case address.ID:
|
2019-12-06 14:06:42 +00:00
|
|
|
return nil, aerrors.Newf(1, "no actor with given ID: %s", addr)
|
2019-07-26 04:54:22 +00:00
|
|
|
case address.Actor:
|
2019-09-10 23:03:17 +00:00
|
|
|
return nil, aerrors.Newf(1, "no such actor: %s", addr)
|
2019-07-26 04:54:22 +00:00
|
|
|
default:
|
2019-09-10 23:03:17 +00:00
|
|
|
return nil, aerrors.Newf(1, "address has unsupported protocol: %d", addr.Protocol())
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
func NewBLSAccountActor(st *state.StateTree, addr address.Address) (*types.Actor, aerrors.ActorError) {
|
2019-07-26 04:54:22 +00:00
|
|
|
var acstate actors.AccountActorState
|
|
|
|
acstate.Address = addr
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
c, err := st.Store.Put(context.TODO(), &acstate)
|
2019-07-26 04:54:22 +00:00
|
|
|
if err != nil {
|
2019-09-10 23:03:17 +00:00
|
|
|
return nil, aerrors.Escalate(err, "serializing account actor state")
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nact := &types.Actor{
|
2019-10-21 18:12:11 +00:00
|
|
|
Code: actors.AccountCodeCid,
|
2019-07-26 04:54:22 +00:00
|
|
|
Balance: types.NewInt(0),
|
|
|
|
Head: c,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nact, nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
func NewSecp256k1AccountActor(st *state.StateTree, addr address.Address) (*types.Actor, aerrors.ActorError) {
|
2019-07-26 04:54:22 +00:00
|
|
|
nact := &types.Actor{
|
2019-10-21 18:12:11 +00:00
|
|
|
Code: actors.AccountCodeCid,
|
2019-07-26 04:54:22 +00:00
|
|
|
Balance: types.NewInt(0),
|
2019-07-25 22:15:33 +00:00
|
|
|
Head: EmptyObjectCid,
|
2019-07-26 04:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nact, nil
|
|
|
|
}
|