-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathmkactor.go
131 lines (112 loc) · 4.3 KB
/
mkactor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package vm
import (
"context"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/filecoin-project/go-address"
actorstypes "github.com/filecoin-project/go-state-types/actors"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/go-state-types/network"
builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"
builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin"
builtin4 "github.com/filecoin-project/specs-actors/v4/actors/builtin"
builtin5 "github.com/filecoin-project/specs-actors/v5/actors/builtin"
builtin6 "github.com/filecoin-project/specs-actors/v6/actors/builtin"
builtin7 "github.com/filecoin-project/specs-actors/v7/actors/builtin"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/aerrors"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors/builtin/account"
"github.com/filecoin-project/lotus/chain/types"
)
func init() {
cst := cbor.NewMemCborStore()
emptyobject, err := cst.Put(context.TODO(), []struct{}{})
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)
if err != nil {
return nil, address.Undef, aerrors.Escalate(err, "registering actor address")
}
av, err := actorstypes.VersionForNetwork(rt.NetworkVersion())
if err != nil {
return nil, address.Undef, aerrors.Escalate(err, "unsupported network version")
}
act, aerr := makeAccountActor(av, addr)
if aerr != nil {
return nil, address.Undef, aerr
}
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")
}
// call constructor on account
_, 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
}
func makeAccountActor(ver actorstypes.Version, addr address.Address) (*types.Actor, aerrors.ActorError) {
switch addr.Protocol() {
case address.BLS, address.SECP256K1:
return newAccountActor(ver, addr), nil
case address.ID:
return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "no actor with given ID: %s", addr)
case address.Actor:
return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "no such actor: %s", addr)
default:
return nil, aerrors.Newf(exitcode.SysErrInvalidReceiver, "address has unsupported protocol: %d", addr.Protocol())
}
}
func newAccountActor(ver actorstypes.Version, addr address.Address) *types.Actor {
// TODO: ActorsUpgrade use a global actor registry?
var code cid.Cid
switch ver {
case actorstypes.Version0:
code = builtin0.AccountActorCodeID
case actorstypes.Version2:
code = builtin2.AccountActorCodeID
case actorstypes.Version3:
code = builtin3.AccountActorCodeID
case actorstypes.Version4:
code = builtin4.AccountActorCodeID
case actorstypes.Version5:
code = builtin5.AccountActorCodeID
case actorstypes.Version6:
code = builtin6.AccountActorCodeID
case actorstypes.Version7:
code = builtin7.AccountActorCodeID
default:
panic("unsupported actors version")
}
nact := &types.Actor{
Code: code,
Balance: types.NewInt(0),
Head: EmptyObjectCid,
Address: &addr,
}
return nact
}