File size: 12,144 Bytes
62cfde8 | 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | # Patch-overlapping Python context after the fix
## `tests/parser/functions/test_raw_call.py` — `function:test_checkable_raw_call` (lines 292-363)
```python
def test_checkable_raw_call(get_contract, assert_tx_failed):
target_source = """
baz: int128
@external
def fail1(should_raise: bool):
if should_raise:
raise "fail"
# test both paths for raw_call -
# they are different depending if callee has or doesn't have returntype
# (fail2 fails because of staticcall)
@external
def fail2(should_raise: bool) -> int128:
if should_raise:
self.baz = self.baz + 1
return self.baz
"""
caller_source = """
@external
@view
def foo(_addr: address, should_raise: bool) -> uint256:
success: bool = True
response: Bytes[32] = b""
success, response = raw_call(
_addr,
_abi_encode(should_raise, method_id=method_id("fail1(bool)")),
max_outsize=32,
is_static_call=True,
revert_on_failure=False,
)
assert success == (not should_raise)
return 1
@external
@view
def bar(_addr: address, should_raise: bool) -> uint256:
success: bool = True
response: Bytes[32] = b""
success, response = raw_call(
_addr,
_abi_encode(should_raise, method_id=method_id("fail2(bool)")),
max_outsize=32,
is_static_call=True,
revert_on_failure=False,
)
assert success == (not should_raise)
return 2
# test max_outsize not set case
@external
@nonpayable
def baz(_addr: address, should_raise: bool) -> uint256:
success: bool = True
success = raw_call(
_addr,
_abi_encode(should_raise, method_id=method_id("fail1(bool)")),
revert_on_failure=False,
)
assert success == (not should_raise)
return 3
"""
target = get_contract(target_source)
caller = get_contract(caller_source)
assert caller.foo(target.address, True) == 1
assert caller.foo(target.address, False) == 1
assert caller.bar(target.address, True) == 2
assert caller.bar(target.address, False) == 2
assert caller.baz(target.address, True) == 3
assert caller.baz(target.address, False) == 3
```
## `vyper/builtins/functions.py` — `class:RawCall` (lines 1067-1218)
```python
class RawCall(BuiltinFunction):
_id = "raw_call"
_inputs = [("to", AddressT()), ("data", BytesT.any())]
_kwargs = {
"max_outsize": KwargSettings(UINT256_T, 0, require_literal=True),
"gas": KwargSettings(UINT256_T, "gas"),
"value": KwargSettings(UINT256_T, zero_value),
"is_delegate_call": KwargSettings(BoolT(), False, require_literal=True),
"is_static_call": KwargSettings(BoolT(), False, require_literal=True),
"revert_on_failure": KwargSettings(BoolT(), True, require_literal=True),
}
_return_type = None
def fetch_call_return(self, node):
self._validate_arg_types(node)
kwargz = {i.arg: i.value for i in node.keywords}
outsize = kwargz.get("max_outsize")
revert_on_failure = kwargz.get("revert_on_failure")
revert_on_failure = revert_on_failure.value if revert_on_failure is not None else True
if outsize is None:
if revert_on_failure:
return None
return BoolT()
if not isinstance(outsize, vy_ast.Int) or outsize.value < 0:
raise
if outsize.value:
return_type = BytesT()
return_type.set_min_length(outsize.value)
if revert_on_failure:
return return_type
return TupleT([BoolT(), return_type])
def infer_arg_types(self, node):
self._validate_arg_types(node)
# return a concrete type for `data`
data_type = get_possible_types_from_node(node.args[1]).pop()
return [self._inputs[0][1], data_type]
@process_inputs
def build_IR(self, expr, args, kwargs, context):
to, data = args
# TODO: must compile in source code order, left-to-right
gas, value, outsize, delegate_call, static_call, revert_on_failure = (
kwargs["gas"],
kwargs["value"],
kwargs["max_outsize"],
kwargs["is_delegate_call"],
kwargs["is_static_call"],
kwargs["revert_on_failure"],
)
if delegate_call and static_call:
raise ArgumentException(
"Call may use one of `is_delegate_call` or `is_static_call`, not both", expr
)
if not static_call and context.is_constant():
raise StateAccessViolation(
f"Cannot make modifying calls from {context.pp_constancy()},"
" use `is_static_call=True` to perform this action",
expr,
)
if data.value == "~calldata":
call_ir = ["with", "mem_ofst", "msize"]
args_ofst = ["seq", ["calldatacopy", "mem_ofst", 0, "calldatasize"], "mem_ofst"]
args_len = "calldatasize"
else:
# some gymnastics to propagate constants (if eval_input_buf
# returns a static memory location)
eval_input_buf = ensure_in_memory(data, context)
input_buf = eval_seq(eval_input_buf)
if input_buf is None:
call_ir = ["with", "arg_buf", eval_input_buf]
input_buf = IRnode.from_list("arg_buf")
else:
call_ir = ["seq", eval_input_buf]
args_ofst = add_ofst(input_buf, 32)
args_len = ["mload", input_buf]
output_node = IRnode.from_list(
context.new_internal_variable(BytesT(outsize)), typ=BytesT(outsize), location=MEMORY
)
bool_ty = BoolT()
# build IR for call or delegatecall
common_call_args = [
args_ofst,
args_len,
# if there is no return value, the return offset can be 0
add_ofst(output_node, 32) if outsize else 0,
outsize,
]
if delegate_call:
call_op = ["delegatecall", gas, to, *common_call_args]
elif static_call:
call_op = ["staticcall", gas, to, *common_call_args]
else:
call_op = ["call", gas, to, value, *common_call_args]
call_ir += [call_op]
# build sequence IR
if outsize:
# return minimum of outsize and returndatasize
size = ["select", ["lt", outsize, "returndatasize"], outsize, "returndatasize"]
# store output size and return output location
store_output_size = ["seq", ["mstore", output_node, size], output_node]
bytes_ty = BytesT(outsize)
if revert_on_failure:
typ = bytes_ty
# check the call success flag, and store returndata in memory
ret_ir = ["seq", check_external_call(call_ir), store_output_size]
return IRnode.from_list(ret_ir, typ=typ, location=MEMORY)
else:
typ = TupleT([bool_ty, bytes_ty])
ret_ir = [
"multi",
# use IRnode.from_list to make sure the types are
# set properly on the "multi" members
IRnode.from_list(call_ir, typ=bool_ty),
IRnode.from_list(store_output_size, typ=bytes_ty, location=MEMORY),
]
# return an IR tuple of call success flag and returndata pointer
return IRnode.from_list(ret_ir, typ=typ)
# max_outsize is 0.
if not revert_on_failure:
# return call flag as stack item
typ = bool_ty
return IRnode.from_list(call_ir, typ=typ)
else:
# check the call success flag and don't return anything
ret_ir = check_external_call(call_ir)
return IRnode.from_list(ret_ir, typ=None)
raise CompilerPanic("unreachable!")
```
## `vyper/builtins/functions.py` — `function:RawCall.build_IR` (lines 1111-1218)
```python
def build_IR(self, expr, args, kwargs, context):
to, data = args
# TODO: must compile in source code order, left-to-right
gas, value, outsize, delegate_call, static_call, revert_on_failure = (
kwargs["gas"],
kwargs["value"],
kwargs["max_outsize"],
kwargs["is_delegate_call"],
kwargs["is_static_call"],
kwargs["revert_on_failure"],
)
if delegate_call and static_call:
raise ArgumentException(
"Call may use one of `is_delegate_call` or `is_static_call`, not both", expr
)
if not static_call and context.is_constant():
raise StateAccessViolation(
f"Cannot make modifying calls from {context.pp_constancy()},"
" use `is_static_call=True` to perform this action",
expr,
)
if data.value == "~calldata":
call_ir = ["with", "mem_ofst", "msize"]
args_ofst = ["seq", ["calldatacopy", "mem_ofst", 0, "calldatasize"], "mem_ofst"]
args_len = "calldatasize"
else:
# some gymnastics to propagate constants (if eval_input_buf
# returns a static memory location)
eval_input_buf = ensure_in_memory(data, context)
input_buf = eval_seq(eval_input_buf)
if input_buf is None:
call_ir = ["with", "arg_buf", eval_input_buf]
input_buf = IRnode.from_list("arg_buf")
else:
call_ir = ["seq", eval_input_buf]
args_ofst = add_ofst(input_buf, 32)
args_len = ["mload", input_buf]
output_node = IRnode.from_list(
context.new_internal_variable(BytesT(outsize)), typ=BytesT(outsize), location=MEMORY
)
bool_ty = BoolT()
# build IR for call or delegatecall
common_call_args = [
args_ofst,
args_len,
# if there is no return value, the return offset can be 0
add_ofst(output_node, 32) if outsize else 0,
outsize,
]
if delegate_call:
call_op = ["delegatecall", gas, to, *common_call_args]
elif static_call:
call_op = ["staticcall", gas, to, *common_call_args]
else:
call_op = ["call", gas, to, value, *common_call_args]
call_ir += [call_op]
# build sequence IR
if outsize:
# return minimum of outsize and returndatasize
size = ["select", ["lt", outsize, "returndatasize"], outsize, "returndatasize"]
# store output size and return output location
store_output_size = ["seq", ["mstore", output_node, size], output_node]
bytes_ty = BytesT(outsize)
if revert_on_failure:
typ = bytes_ty
# check the call success flag, and store returndata in memory
ret_ir = ["seq", check_external_call(call_ir), store_output_size]
return IRnode.from_list(ret_ir, typ=typ, location=MEMORY)
else:
typ = TupleT([bool_ty, bytes_ty])
ret_ir = [
"multi",
# use IRnode.from_list to make sure the types are
# set properly on the "multi" members
IRnode.from_list(call_ir, typ=bool_ty),
IRnode.from_list(store_output_size, typ=bytes_ty, location=MEMORY),
]
# return an IR tuple of call success flag and returndata pointer
return IRnode.from_list(ret_ir, typ=typ)
# max_outsize is 0.
if not revert_on_failure:
# return call flag as stack item
typ = bool_ty
return IRnode.from_list(call_ir, typ=typ)
else:
# check the call success flag and don't return anything
ret_ir = check_external_call(call_ir)
return IRnode.from_list(ret_ir, typ=None)
raise CompilerPanic("unreachable!")
```
|