1
1
// Copyright 2019-2022 ChainSafe Systems
2
2
// SPDX-License-Identifier: Apache-2.0, MIT
3
3
4
+ use std:: iter;
5
+
4
6
use cid:: Cid ;
5
7
use fil_actors_runtime:: runtime:: { ActorCode , Runtime } ;
8
+ use fil_actors_runtime:: EAM_ACTOR_ADDR ;
6
9
use fil_actors_runtime:: { actor_error, cbor, ActorDowncast , ActorError , SYSTEM_ACTOR_ADDR } ;
7
10
use fvm_ipld_blockstore:: Blockstore ;
8
11
use fvm_ipld_encoding:: RawBytes ;
@@ -28,8 +31,9 @@ fil_actors_runtime::wasm_trampoline!(Actor);
28
31
pub enum Method {
29
32
Constructor = METHOD_CONSTRUCTOR ,
30
33
Exec = 2 ,
34
+ Exec4 = 3 ,
31
35
#[ cfg( feature = "m2-native" ) ]
32
- InstallCode = 3 ,
36
+ InstallCode = 4 ,
33
37
}
34
38
35
39
/// Init actor
@@ -85,7 +89,7 @@ impl Actor {
85
89
log:: trace!( "robust address: {:?}" , & robust_address) ;
86
90
87
91
// Allocate an ID for this actor.
88
- // Store mapping of pubkey or actor address to actor ID
92
+ // Store mapping of actor addresses to the actor ID.
89
93
let id_address: ActorID = rt. transaction ( |s : & mut State , rt| {
90
94
s. map_address_to_new_id ( rt. store ( ) , & robust_address) . map_err ( |e| {
91
95
e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to allocate ID address" )
@@ -107,6 +111,58 @@ impl Actor {
107
111
Ok ( ExecReturn { id_address : Address :: new_id ( id_address) , robust_address } )
108
112
}
109
113
114
+ /// Exec init actor
115
+ pub fn exec4 < BS , RT > ( rt : & mut RT , params : Exec4Params ) -> Result < Exec4Return , ActorError >
116
+ where
117
+ BS : Blockstore ,
118
+ RT : Runtime < BS > ,
119
+ {
120
+ if cfg ! ( feature = "m2-native" ) {
121
+ rt. validate_immediate_caller_accept_any ( ) ?;
122
+ } else {
123
+ rt. validate_immediate_caller_is ( iter:: once ( & EAM_ACTOR_ADDR ) ) ?;
124
+ }
125
+
126
+ // Compute the f4 address.
127
+ let caller_id = rt. message ( ) . caller ( ) . id ( ) . unwrap ( ) ;
128
+ let delegated_address =
129
+ Address :: new_delegated ( caller_id, & params. subaddress ) . map_err ( |e| {
130
+ ActorError :: illegal_argument ( format ! ( "invalid delegated address: {}" , e) )
131
+ } ) ?;
132
+
133
+ log:: trace!( "delegated address: {:?}" , & delegated_address) ;
134
+
135
+ // Compute a re-org-stable address.
136
+ // This address exists for use by messages coming from outside the system, in order to
137
+ // stably address the newly created actor even if a chain re-org causes it to end up with
138
+ // a different ID.
139
+ let robust_address = rt. new_actor_address ( ) ?;
140
+
141
+ log:: trace!( "robust address: {:?}" , & robust_address) ;
142
+
143
+ // Allocate an ID for this actor.
144
+ // Store mapping of actor addresses to the actor ID.
145
+ let id_address: ActorID = rt. transaction ( |s : & mut State , rt| {
146
+ s. map_address_to_f4 ( rt. store ( ) , & robust_address, & delegated_address) . map_err ( |e| {
147
+ e. downcast_default ( ExitCode :: USR_ILLEGAL_STATE , "failed to allocate ID address" )
148
+ } )
149
+ } ) ?;
150
+
151
+ // Create an empty actor
152
+ rt. create_actor ( params. code_cid , id_address) ?;
153
+
154
+ // Invoke constructor
155
+ rt. send (
156
+ & Address :: new_id ( id_address) ,
157
+ METHOD_CONSTRUCTOR ,
158
+ params. constructor_params ,
159
+ rt. message ( ) . value_received ( ) ,
160
+ )
161
+ . map_err ( |err| err. wrap ( "constructor failed" ) ) ?;
162
+
163
+ Ok ( Exec4Return { id_address : Address :: new_id ( id_address) , robust_address } )
164
+ }
165
+
110
166
#[ cfg( feature = "m2-native" ) ]
111
167
pub fn install < BS , RT > ( rt : & mut RT , params : InstallParams ) -> Result < InstallReturn , ActorError >
112
168
where
@@ -176,6 +232,10 @@ impl ActorCode for Actor {
176
232
let res = Self :: exec ( rt, cbor:: deserialize_params ( params) ?) ?;
177
233
Ok ( RawBytes :: serialize ( res) ?)
178
234
}
235
+ Some ( Method :: Exec4 ) => {
236
+ let res = Self :: exec4 ( rt, cbor:: deserialize_params ( params) ?) ?;
237
+ Ok ( RawBytes :: serialize ( res) ?)
238
+ }
179
239
#[ cfg( feature = "m2-native" ) ]
180
240
Some ( Method :: InstallCode ) => {
181
241
let res = Self :: install ( rt, cbor:: deserialize_params ( params) ?) ?;
0 commit comments