ethereum.forks.byzantium.vm.instructions.environment
Ethereum Virtual Machine (EVM) Environmental Instructions.
.. contents:: Table of Contents :backlinks: none :local:
Introduction
Implementations of the EVM environment related instructions.
address
Pushes the address of the current executing account to the stack.
Parameters
evm : The current EVM frame.
                def address(evm: Evm) -> None:
            
| 37 |     """ | 
|---|---|
| 38 |     Pushes the address of the current executing account to the stack. | 
| 39 |  | 
| 40 |     Parameters | 
| 41 |     ---------- | 
| 42 |     evm : | 
| 43 |         The current EVM frame. | 
| 44 |  | 
| 45 |     """ | 
| 46 |     # STACK | 
| 47 |     pass | 
| 48 | |
| 49 |     # GAS | 
| 50 |     charge_gas(evm, GAS_BASE) | 
| 51 | |
| 52 |     # OPERATION | 
| 53 |     push(evm.stack, U256.from_be_bytes(evm.message.current_target)) | 
| 54 | |
| 55 |     # PROGRAM COUNTER | 
| 56 |     evm.pc += Uint(1) | 
balance
Pushes the balance of the given account onto the stack.
Parameters
evm : The current EVM frame.
                def balance(evm: Evm) -> None:
            
| 60 |     """ | 
|---|---|
| 61 |     Pushes the balance of the given account onto the stack. | 
| 62 |  | 
| 63 |     Parameters | 
| 64 |     ---------- | 
| 65 |     evm : | 
| 66 |         The current EVM frame. | 
| 67 |  | 
| 68 |     """ | 
| 69 |     # STACK | 
| 70 |     address = to_address_masked(pop(evm.stack)) | 
| 71 | |
| 72 |     # GAS | 
| 73 |     charge_gas(evm, GAS_BALANCE) | 
| 74 | |
| 75 |     # OPERATION | 
| 76 |     # Non-existent accounts default to EMPTY_ACCOUNT, which has balance 0. | 
| 77 |     balance = get_account(evm.message.block_env.state, address).balance | 
| 78 | |
| 79 |     push(evm.stack, balance) | 
| 80 | |
| 81 |     # PROGRAM COUNTER | 
| 82 |     evm.pc += Uint(1) | 
origin
Pushes the address of the original transaction sender to the stack. The origin address can only be an EOA.
Parameters
evm : The current EVM frame.
                def origin(evm: Evm) -> None:
            
| 86 |     """ | 
|---|---|
| 87 |     Pushes the address of the original transaction sender to the stack. | 
| 88 |     The origin address can only be an EOA. | 
| 89 |  | 
| 90 |     Parameters | 
| 91 |     ---------- | 
| 92 |     evm : | 
| 93 |         The current EVM frame. | 
| 94 |  | 
| 95 |     """ | 
| 96 |     # STACK | 
| 97 |     pass | 
| 98 | |
| 99 |     # GAS | 
| 100 |     charge_gas(evm, GAS_BASE) | 
| 101 | |
| 102 |     # OPERATION | 
| 103 |     push(evm.stack, U256.from_be_bytes(evm.message.tx_env.origin)) | 
| 104 | |
| 105 |     # PROGRAM COUNTER | 
| 106 |     evm.pc += Uint(1) | 
caller
Pushes the address of the caller onto the stack.
Parameters
evm : The current EVM frame.
                def caller(evm: Evm) -> None:
            
| 110 |     """ | 
|---|---|
| 111 |     Pushes the address of the caller onto the stack. | 
| 112 |  | 
| 113 |     Parameters | 
| 114 |     ---------- | 
| 115 |     evm : | 
| 116 |         The current EVM frame. | 
| 117 |  | 
| 118 |     """ | 
| 119 |     # STACK | 
| 120 |     pass | 
| 121 | |
| 122 |     # GAS | 
| 123 |     charge_gas(evm, GAS_BASE) | 
| 124 | |
| 125 |     # OPERATION | 
| 126 |     push(evm.stack, U256.from_be_bytes(evm.message.caller)) | 
| 127 | |
| 128 |     # PROGRAM COUNTER | 
| 129 |     evm.pc += Uint(1) | 
callvalue
Push the value (in wei) sent with the call onto the stack.
Parameters
evm : The current EVM frame.
                def callvalue(evm: Evm) -> None:
            
| 133 |     """ | 
|---|---|
| 134 |     Push the value (in wei) sent with the call onto the stack. | 
| 135 |  | 
| 136 |     Parameters | 
| 137 |     ---------- | 
| 138 |     evm : | 
| 139 |         The current EVM frame. | 
| 140 |  | 
| 141 |     """ | 
| 142 |     # STACK | 
| 143 |     pass | 
| 144 | |
| 145 |     # GAS | 
| 146 |     charge_gas(evm, GAS_BASE) | 
| 147 | |
| 148 |     # OPERATION | 
| 149 |     push(evm.stack, evm.message.value) | 
| 150 | |
| 151 |     # PROGRAM COUNTER | 
| 152 |     evm.pc += Uint(1) | 
calldataload
Push a word (32 bytes) of the input data belonging to the current environment onto the stack.
Parameters
evm : The current EVM frame.
                def calldataload(evm: Evm) -> None:
            
| 156 |     """ | 
|---|---|
| 157 |     Push a word (32 bytes) of the input data belonging to the current | 
| 158 |     environment onto the stack. | 
| 159 |  | 
| 160 |     Parameters | 
| 161 |     ---------- | 
| 162 |     evm : | 
| 163 |         The current EVM frame. | 
| 164 |  | 
| 165 |     """ | 
| 166 |     # STACK | 
| 167 |     start_index = pop(evm.stack) | 
| 168 | |
| 169 |     # GAS | 
| 170 |     charge_gas(evm, GAS_VERY_LOW) | 
| 171 | |
| 172 |     # OPERATION | 
| 173 |     value = buffer_read(evm.message.data, start_index, U256(32)) | 
| 174 | |
| 175 |     push(evm.stack, U256.from_be_bytes(value)) | 
| 176 | |
| 177 |     # PROGRAM COUNTER | 
| 178 |     evm.pc += Uint(1) | 
calldatasize
Push the size of input data in current environment onto the stack.
Parameters
evm : The current EVM frame.
                def calldatasize(evm: Evm) -> None:
            
| 182 |     """ | 
|---|---|
| 183 |     Push the size of input data in current environment onto the stack. | 
| 184 |  | 
| 185 |     Parameters | 
| 186 |     ---------- | 
| 187 |     evm : | 
| 188 |         The current EVM frame. | 
| 189 |  | 
| 190 |     """ | 
| 191 |     # STACK | 
| 192 |     pass | 
| 193 | |
| 194 |     # GAS | 
| 195 |     charge_gas(evm, GAS_BASE) | 
| 196 | |
| 197 |     # OPERATION | 
| 198 |     push(evm.stack, U256(len(evm.message.data))) | 
| 199 | |
| 200 |     # PROGRAM COUNTER | 
| 201 |     evm.pc += Uint(1) | 
calldatacopy
Copy a portion of the input data in current environment to memory.
This will also expand the memory, in case that the memory is insufficient to store the data.
Parameters
evm : The current EVM frame.
                def calldatacopy(evm: Evm) -> None:
            
| 205 |     """ | 
|---|---|
| 206 |     Copy a portion of the input data in current environment to memory. | 
| 207 |  | 
| 208 |     This will also expand the memory, in case that the memory is insufficient | 
| 209 |     to store the data. | 
| 210 |  | 
| 211 |     Parameters | 
| 212 |     ---------- | 
| 213 |     evm : | 
| 214 |         The current EVM frame. | 
| 215 |  | 
| 216 |     """ | 
| 217 |     # STACK | 
| 218 |     memory_start_index = pop(evm.stack) | 
| 219 |     data_start_index = pop(evm.stack) | 
| 220 |     size = pop(evm.stack) | 
| 221 | |
| 222 |     # GAS | 
| 223 |     words = ceil32(Uint(size)) // Uint(32) | 
| 224 |     copy_gas_cost = GAS_COPY * words | 
| 225 |     extend_memory = calculate_gas_extend_memory( | 
| 226 |         evm.memory, [(memory_start_index, size)] | 
| 227 |     ) | 
| 228 |     charge_gas(evm, GAS_VERY_LOW + copy_gas_cost + extend_memory.cost) | 
| 229 | |
| 230 |     # OPERATION | 
| 231 |     evm.memory += b"\x00" * extend_memory.expand_by | 
| 232 |     value = buffer_read(evm.message.data, data_start_index, size) | 
| 233 |     memory_write(evm.memory, memory_start_index, value) | 
| 234 | |
| 235 |     # PROGRAM COUNTER | 
| 236 |     evm.pc += Uint(1) | 
codesize
Push the size of code running in current environment onto the stack.
Parameters
evm : The current EVM frame.
                def codesize(evm: Evm) -> None:
            
| 240 |     """ | 
|---|---|
| 241 |     Push the size of code running in current environment onto the stack. | 
| 242 |  | 
| 243 |     Parameters | 
| 244 |     ---------- | 
| 245 |     evm : | 
| 246 |         The current EVM frame. | 
| 247 |  | 
| 248 |     """ | 
| 249 |     # STACK | 
| 250 |     pass | 
| 251 | |
| 252 |     # GAS | 
| 253 |     charge_gas(evm, GAS_BASE) | 
| 254 | |
| 255 |     # OPERATION | 
| 256 |     push(evm.stack, U256(len(evm.code))) | 
| 257 | |
| 258 |     # PROGRAM COUNTER | 
| 259 |     evm.pc += Uint(1) | 
codecopy
Copy a portion of the code in current environment to memory.
This will also expand the memory, in case that the memory is insufficient to store the data.
Parameters
evm : The current EVM frame.
                def codecopy(evm: Evm) -> None:
            
| 263 |     """ | 
|---|---|
| 264 |     Copy a portion of the code in current environment to memory. | 
| 265 |  | 
| 266 |     This will also expand the memory, in case that the memory is insufficient | 
| 267 |     to store the data. | 
| 268 |  | 
| 269 |     Parameters | 
| 270 |     ---------- | 
| 271 |     evm : | 
| 272 |         The current EVM frame. | 
| 273 |  | 
| 274 |     """ | 
| 275 |     # STACK | 
| 276 |     memory_start_index = pop(evm.stack) | 
| 277 |     code_start_index = pop(evm.stack) | 
| 278 |     size = pop(evm.stack) | 
| 279 | |
| 280 |     # GAS | 
| 281 |     words = ceil32(Uint(size)) // Uint(32) | 
| 282 |     copy_gas_cost = GAS_COPY * words | 
| 283 |     extend_memory = calculate_gas_extend_memory( | 
| 284 |         evm.memory, [(memory_start_index, size)] | 
| 285 |     ) | 
| 286 |     charge_gas(evm, GAS_VERY_LOW + copy_gas_cost + extend_memory.cost) | 
| 287 | |
| 288 |     # OPERATION | 
| 289 |     evm.memory += b"\x00" * extend_memory.expand_by | 
| 290 |     value = buffer_read(evm.code, code_start_index, size) | 
| 291 |     memory_write(evm.memory, memory_start_index, value) | 
| 292 | |
| 293 |     # PROGRAM COUNTER | 
| 294 |     evm.pc += Uint(1) | 
gasprice
Push the gas price used in current environment onto the stack.
Parameters
evm : The current EVM frame.
                def gasprice(evm: Evm) -> None:
            
| 298 |     """ | 
|---|---|
| 299 |     Push the gas price used in current environment onto the stack. | 
| 300 |  | 
| 301 |     Parameters | 
| 302 |     ---------- | 
| 303 |     evm : | 
| 304 |         The current EVM frame. | 
| 305 |  | 
| 306 |     """ | 
| 307 |     # STACK | 
| 308 |     pass | 
| 309 | |
| 310 |     # GAS | 
| 311 |     charge_gas(evm, GAS_BASE) | 
| 312 | |
| 313 |     # OPERATION | 
| 314 |     push(evm.stack, U256(evm.message.tx_env.gas_price)) | 
| 315 | |
| 316 |     # PROGRAM COUNTER | 
| 317 |     evm.pc += Uint(1) | 
extcodesize
Push the code size of a given account onto the stack.
Parameters
evm : The current EVM frame.
                def extcodesize(evm: Evm) -> None:
            
| 321 |     """ | 
|---|---|
| 322 |     Push the code size of a given account onto the stack. | 
| 323 |  | 
| 324 |     Parameters | 
| 325 |     ---------- | 
| 326 |     evm : | 
| 327 |         The current EVM frame. | 
| 328 |  | 
| 329 |     """ | 
| 330 |     # STACK | 
| 331 |     address = to_address_masked(pop(evm.stack)) | 
| 332 | |
| 333 |     # GAS | 
| 334 |     charge_gas(evm, GAS_EXTERNAL) | 
| 335 | |
| 336 |     # OPERATION | 
| 337 |     code = get_account(evm.message.block_env.state, address).code | 
| 338 | |
| 339 |     codesize = U256(len(code)) | 
| 340 |     push(evm.stack, codesize) | 
| 341 | |
| 342 |     # PROGRAM COUNTER | 
| 343 |     evm.pc += Uint(1) | 
extcodecopy
Copy a portion of an account's code to memory.
Parameters
evm : The current EVM frame.
                def extcodecopy(evm: Evm) -> None:
            
| 347 |     """ | 
|---|---|
| 348 |     Copy a portion of an account's code to memory. | 
| 349 |  | 
| 350 |     Parameters | 
| 351 |     ---------- | 
| 352 |     evm : | 
| 353 |         The current EVM frame. | 
| 354 |  | 
| 355 |     """ | 
| 356 |     # STACK | 
| 357 |     address = to_address_masked(pop(evm.stack)) | 
| 358 |     memory_start_index = pop(evm.stack) | 
| 359 |     code_start_index = pop(evm.stack) | 
| 360 |     size = pop(evm.stack) | 
| 361 | |
| 362 |     # GAS | 
| 363 |     words = ceil32(Uint(size)) // Uint(32) | 
| 364 |     copy_gas_cost = GAS_COPY * words | 
| 365 |     extend_memory = calculate_gas_extend_memory( | 
| 366 |         evm.memory, [(memory_start_index, size)] | 
| 367 |     ) | 
| 368 |     charge_gas(evm, GAS_EXTERNAL + copy_gas_cost + extend_memory.cost) | 
| 369 | |
| 370 |     # OPERATION | 
| 371 |     evm.memory += b"\x00" * extend_memory.expand_by | 
| 372 |     code = get_account(evm.message.block_env.state, address).code | 
| 373 | |
| 374 |     value = buffer_read(code, code_start_index, size) | 
| 375 |     memory_write(evm.memory, memory_start_index, value) | 
| 376 | |
| 377 |     # PROGRAM COUNTER | 
| 378 |     evm.pc += Uint(1) | 
returndatasize
Pushes the size of the return data buffer onto the stack.
Parameters
evm : The current EVM frame.
                def returndatasize(evm: Evm) -> None:
            
| 382 |     """ | 
|---|---|
| 383 |     Pushes the size of the return data buffer onto the stack. | 
| 384 |  | 
| 385 |     Parameters | 
| 386 |     ---------- | 
| 387 |     evm : | 
| 388 |         The current EVM frame. | 
| 389 |  | 
| 390 |     """ | 
| 391 |     # STACK | 
| 392 |     pass | 
| 393 | |
| 394 |     # GAS | 
| 395 |     charge_gas(evm, GAS_BASE) | 
| 396 | |
| 397 |     # OPERATION | 
| 398 |     push(evm.stack, U256(len(evm.return_data))) | 
| 399 | |
| 400 |     # PROGRAM COUNTER | 
| 401 |     evm.pc += Uint(1) | 
returndatacopy
Copies data from the return data buffer code to memory.
Parameters
evm : The current EVM frame.
                def returndatacopy(evm: Evm) -> None:
            
| 405 |     """ | 
|---|---|
| 406 |     Copies data from the return data buffer code to memory. | 
| 407 |  | 
| 408 |     Parameters | 
| 409 |     ---------- | 
| 410 |     evm : | 
| 411 |         The current EVM frame. | 
| 412 |  | 
| 413 |     """ | 
| 414 |     # STACK | 
| 415 |     memory_start_index = pop(evm.stack) | 
| 416 |     return_data_start_position = pop(evm.stack) | 
| 417 |     size = pop(evm.stack) | 
| 418 | |
| 419 |     # GAS | 
| 420 |     words = ceil32(Uint(size)) // Uint(32) | 
| 421 |     copy_gas_cost = GAS_RETURN_DATA_COPY * words | 
| 422 |     extend_memory = calculate_gas_extend_memory( | 
| 423 |         evm.memory, [(memory_start_index, size)] | 
| 424 |     ) | 
| 425 |     charge_gas(evm, GAS_VERY_LOW + copy_gas_cost + extend_memory.cost) | 
| 426 |     if Uint(return_data_start_position) + Uint(size) > ulen(evm.return_data): | 
| 427 |         raise OutOfBoundsRead | 
| 428 | |
| 429 |     evm.memory += b"\x00" * extend_memory.expand_by | 
| 430 |     value = evm.return_data[ | 
| 431 |         return_data_start_position : return_data_start_position + size | 
| 432 |     ] | 
| 433 |     memory_write(evm.memory, memory_start_index, value) | 
| 434 | |
| 435 |     # PROGRAM COUNTER | 
| 436 |     evm.pc += Uint(1) |