This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Actor.sol
193 lines (164 loc) · 8.29 KB
/
Actor.sol
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*******************************************************************************
* (c) 2022 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
// THIS CODE WAS SECURITY REVIEWED BY KUDELSKI SECURITY, BUT NOT FORMALLY AUDITED
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.17;
import "./Misc.sol";
import "../types/CommonTypes.sol";
/// @title Call actors utilities library, meant to interact with Filecoin builtin actors
/// @author Zondax AG
library Actor {
/// @notice precompile address for the call_actor precompile
address constant CALL_ACTOR_ADDRESS = 0xfe00000000000000000000000000000000000003;
/// @notice precompile address for the call_actor_id precompile
address constant CALL_ACTOR_ID = 0xfe00000000000000000000000000000000000005;
/// @notice flag used to indicate that the call_actor or call_actor_id should perform a static_call to the desired actor
uint64 constant READ_ONLY_FLAG = 0x00000001;
/// @notice flag used to indicate that the call_actor or call_actor_id should perform a delegate_call to the desired actor
uint64 constant DEFAULT_FLAG = 0x00000000;
/// @notice the provided address is not valid
error InvalidAddress(bytes addr);
/// @notice the smart contract has no enough balance to transfer
error NotEnoughBalance(uint256 balance, uint256 value);
/// @notice the provided actor id is not valid
error InvalidActorID(CommonTypes.FilActorId actorId);
/// @notice an error happened trying to call the actor
error FailToCallActor();
/// @notice the response received is not correct. In some case no response is expected and we received one, or a response was indeed expected and we received none.
error InvalidResponseLength();
/// @notice the codec received is not valid
error InvalidCodec(uint64);
/// @notice the called actor returned an error as part of its expected behaviour
error ActorError(int256 errorCode);
/// @notice the actor is not found
error ActorNotFound();
/// @notice allows to interact with an specific actor by its address (bytes format)
/// @param actor_address actor address (bytes format) to interact with
/// @param method_num id of the method from the actor to call
/// @param codec how the request data passed as argument is encoded
/// @param raw_request encoded arguments to be passed in the call
/// @param value tokens to be transferred to the called actor
/// @param static_call indicates if the call will be allowed to change the actor state or not (just read the state)
/// @return payload (in bytes) with the actual response data (without codec or response code)
function callByAddress(
bytes memory actor_address,
uint256 method_num,
uint64 codec,
bytes memory raw_request,
uint256 value,
bool static_call
) internal returns (bytes memory) {
if (actor_address.length < 2) {
revert InvalidAddress(actor_address);
}
validatePrecompileCall(CALL_ACTOR_ADDRESS, value);
// We have to delegate-call the call-actor precompile because the call-actor precompile will
// call the target actor on our behalf. This will _not_ delegate to the target `actor_address`.
//
// Specifically:
//
// - `static_call == false`: `CALLER (you) --(DELEGATECALL)-> CALL_ACTOR_PRECOMPILE --(CALL)-> actor_address
// - `static_call == true`: `CALLER (you) --(DELEGATECALL)-> CALL_ACTOR_PRECOMPILE --(STATICCALL)-> actor_address
(bool success, bytes memory data) = address(CALL_ACTOR_ADDRESS).delegatecall(
abi.encode(uint64(method_num), value, static_call ? READ_ONLY_FLAG : DEFAULT_FLAG, codec, raw_request, actor_address)
);
if (!success) {
revert FailToCallActor();
}
return readRespData(data);
}
/// @notice allows to interact with an specific actor by its id (uint64)
/// @param target actor id (uint64) to interact with
/// @param method_num id of the method from the actor to call
/// @param codec how the request data passed as argument is encoded
/// @param raw_request encoded arguments to be passed in the call
/// @param value tokens to be transferred to the called actor
/// @param static_call indicates if the call will be allowed to change the actor state or not (just read the state)
/// @return payload (in bytes) with the actual response data (without codec or response code)
function callByID(
CommonTypes.FilActorId target,
uint256 method_num,
uint64 codec,
bytes memory raw_request,
uint256 value,
bool static_call
) internal returns (bytes memory) {
validatePrecompileCall(CALL_ACTOR_ID, value);
(bool success, bytes memory data) = address(CALL_ACTOR_ID).delegatecall(
abi.encode(uint64(method_num), value, static_call ? READ_ONLY_FLAG : DEFAULT_FLAG, codec, raw_request, target)
);
if (!success) {
revert FailToCallActor();
}
return readRespData(data);
}
/// @notice allows to run some generic validations before calling the precompile actor
/// @param addr precompile actor address to run check to
/// @param value tokens to be transferred to the called actor
function validatePrecompileCall(address addr, uint256 value) internal view {
uint balance = address(this).balance;
if (balance < value) {
revert NotEnoughBalance(balance, value);
}
bool actorExists = Misc.addressExists(addr);
if (!actorExists) {
revert ActorNotFound();
}
}
/// @notice allows to interact with an non-singleton actors by its id (uint64)
/// @param target actor id (uint64) to interact with
/// @param method_num id of the method from the actor to call
/// @param codec how the request data passed as argument is encoded
/// @param raw_request encoded arguments to be passed in the call
/// @param value tokens to be transfered to the called actor
/// @param static_call indicates if the call will be allowed to change the actor state or not (just read the state)
/// @dev it requires the id to be bigger than 99, as singleton actors are smaller than that
function callNonSingletonByID(
CommonTypes.FilActorId target,
uint256 method_num,
uint64 codec,
bytes memory raw_request,
uint256 value,
bool static_call
) internal returns (bytes memory) {
if (CommonTypes.FilActorId.unwrap(target) < 100) {
revert InvalidActorID(target);
}
return callByID(target, method_num, codec, raw_request, value, static_call);
}
/// @notice parse the response an actor returned
/// @notice it will validate the return code (success) and the codec (valid one)
/// @param raw_response raw data (bytes) the actor returned
/// @return the actual raw data (payload, in bytes) to be parsed according to the actor and method called
function readRespData(bytes memory raw_response) internal pure returns (bytes memory) {
(int256 exit, uint64 return_codec, bytes memory return_value) = abi.decode(raw_response, (int256, uint64, bytes));
if (return_codec == Misc.NONE_CODEC) {
if (return_value.length != 0) {
revert InvalidResponseLength();
}
} else if (return_codec == Misc.CBOR_CODEC || return_codec == Misc.DAG_CBOR_CODEC) {
if (return_value.length == 0) {
revert InvalidResponseLength();
}
} else {
revert InvalidCodec(return_codec);
}
if (exit != 0) {
revert ActorError(exit);
}
return return_value;
}
}