-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_actor.go
113 lines (93 loc) · 3.65 KB
/
init_actor.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
package init
import (
addr "github.com/chenjianmei111/go-address"
"github.com/chenjianmei111/go-state-types/abi"
"github.com/chenjianmei111/go-state-types/cbor"
"github.com/chenjianmei111/go-state-types/exitcode"
init0 "github.com/chenjianmei111/specs-actors/actors/builtin/init"
cid "github.com/ipfs/go-cid"
"github.com/chenjianmei111/specs-actors/v2/actors/builtin"
"github.com/chenjianmei111/specs-actors/v2/actors/runtime"
autil "github.com/chenjianmei111/specs-actors/v2/actors/util"
"github.com/chenjianmei111/specs-actors/v2/actors/util/adt"
)
// The init actor uniquely has the power to create new actors.
// It maintains a table resolving pubkey and temporary actor addresses to the canonical ID-addresses.
type Actor struct{}
func (a Actor) Exports() []interface{} {
return []interface{}{
builtin.MethodConstructor: a.Constructor,
2: a.Exec,
}
}
func (a Actor) Code() cid.Cid {
return builtin.InitActorCodeID
}
func (a Actor) IsSingleton() bool {
return true
}
func (a Actor) State() cbor.Er { return new(State) }
var _ runtime.VMActor = Actor{}
//type ConstructorParams struct {
// NetworkName string
//}
type ConstructorParams = init0.ConstructorParams
func (a Actor) Constructor(rt runtime.Runtime, params *ConstructorParams) *abi.EmptyValue {
rt.ValidateImmediateCallerIs(builtin.SystemActorAddr)
emptyMap, err := adt.MakeEmptyMap(adt.AsStore(rt)).Root()
builtin.RequireNoErr(rt, err, exitcode.ErrIllegalState, "failed to construct state")
st := ConstructState(emptyMap, params.NetworkName)
rt.StateCreate(st)
return nil
}
//type ExecParams struct {
// CodeCID cid.Cid `checked:"true"` // invalid CIDs won't get committed to the state tree
// ConstructorParams []byte
//}
type ExecParams = init0.ExecParams
//type ExecReturn struct {
// IDAddress addr.Address // The canonical ID-based address for the actor.
// RobustAddress addr.Address // A more expensive but re-org-safe address for the newly created actor.
//}
type ExecReturn = init0.ExecReturn
func (a Actor) Exec(rt runtime.Runtime, params *ExecParams) *ExecReturn {
rt.ValidateImmediateCallerAcceptAny()
callerCodeCID, ok := rt.GetActorCodeCID(rt.Caller())
autil.AssertMsg(ok, "no code for actor at %s", rt.Caller())
if !canExec(callerCodeCID, params.CodeCID) {
rt.Abortf(exitcode.ErrForbidden, "caller type %v cannot exec actor type %v", callerCodeCID, params.CodeCID)
}
// Compute a re-org-stable address.
// This address exists for use by messages coming from outside the system, in order to
// stably address the newly created actor even if a chain re-org causes it to end up with
// a different ID.
uniqueAddress := rt.NewActorAddress()
// Allocate an ID for this actor.
// Store mapping of pubkey or actor address to actor ID
var st State
var idAddr addr.Address
rt.StateTransaction(&st, func() {
var err error
idAddr, err = st.MapAddressToNewID(adt.AsStore(rt), uniqueAddress)
builtin.RequireNoErr(rt, err, exitcode.ErrIllegalState, "failed to allocate ID address")
})
// Create an empty actor.
rt.CreateActor(params.CodeCID, idAddr)
// Invoke constructor.
code := rt.Send(idAddr, builtin.MethodConstructor, builtin.CBORBytes(params.ConstructorParams), rt.ValueReceived(), &builtin.Discard{})
builtin.RequireSuccess(rt, code, "constructor failed")
return &ExecReturn{IDAddress: idAddr, RobustAddress: uniqueAddress}
}
func canExec(callerCodeID cid.Cid, execCodeID cid.Cid) bool {
switch execCodeID {
case builtin.StorageMinerActorCodeID:
if callerCodeID == builtin.StoragePowerActorCodeID {
return true
}
return false
case builtin.PaymentChannelActorCodeID, builtin.MultisigActorCodeID:
return true
default:
return false
}
}